001 /* 002 * Copyright (C) 2008-2010 by Holger Arndt 003 * 004 * This file is part of the Universal Java Matrix Package (UJMP). 005 * See the NOTICE file distributed with this work for additional 006 * information regarding copyright ownership and licensing. 007 * 008 * UJMP is free software; you can redistribute it and/or modify 009 * it under the terms of the GNU Lesser General Public License as 010 * published by the Free Software Foundation; either version 2 011 * of the License, or (at your option) any later version. 012 * 013 * UJMP is distributed in the hope that it will be useful, 014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 016 * GNU Lesser General Public License for more details. 017 * 018 * You should have received a copy of the GNU Lesser General Public 019 * License along with UJMP; if not, write to the 020 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, 021 * Boston, MA 02110-1301 USA 022 */ 023 024 package org.ujmp.jblas.calculation; 025 026 import org.jblas.Decompose; 027 import org.jblas.DoubleMatrix; 028 import org.jblas.Solve; 029 import org.ujmp.core.Matrix; 030 import org.ujmp.core.interfaces.HasColumnMajorDoubleArray1D; 031 import org.ujmp.jblas.JBlasDenseDoubleMatrix2D; 032 033 public class Chol 034 implements 035 org.ujmp.core.doublematrix.calculation.general.decomposition.Chol<Matrix> { 036 037 public static Chol INSTANCE = new Chol(); 038 039 public Matrix calc(Matrix source) { 040 final DoubleMatrix matrix; 041 if (source instanceof JBlasDenseDoubleMatrix2D) { 042 matrix = ((JBlasDenseDoubleMatrix2D) source).getWrappedObject(); 043 } else if (source instanceof HasColumnMajorDoubleArray1D) { 044 matrix = new JBlasDenseDoubleMatrix2D(source.getRowCount(), source 045 .getColumnCount(), ((HasColumnMajorDoubleArray1D) source) 046 .getColumnMajorDoubleArray1D()).getWrappedObject(); 047 } else { 048 matrix = new JBlasDenseDoubleMatrix2D(source).getWrappedObject(); 049 } 050 final DoubleMatrix r = Decompose.cholesky(matrix).transpose(); 051 return new JBlasDenseDoubleMatrix2D(r); 052 } 053 054 public Matrix solve(Matrix a, Matrix b) { 055 final DoubleMatrix a2; 056 final DoubleMatrix b2; 057 if (a instanceof JBlasDenseDoubleMatrix2D) { 058 a2 = ((JBlasDenseDoubleMatrix2D) a).getWrappedObject(); 059 } else if (a instanceof HasColumnMajorDoubleArray1D) { 060 a2 = new JBlasDenseDoubleMatrix2D(a.getRowCount(), a 061 .getColumnCount(), ((HasColumnMajorDoubleArray1D) a) 062 .getColumnMajorDoubleArray1D()).getWrappedObject(); 063 } else { 064 a2 = new JBlasDenseDoubleMatrix2D(a).getWrappedObject(); 065 } 066 if (b instanceof JBlasDenseDoubleMatrix2D) { 067 b2 = ((JBlasDenseDoubleMatrix2D) b).getWrappedObject(); 068 } else if (b instanceof HasColumnMajorDoubleArray1D) { 069 b2 = new JBlasDenseDoubleMatrix2D(b.getRowCount(), b 070 .getColumnCount(), ((HasColumnMajorDoubleArray1D) b) 071 .getColumnMajorDoubleArray1D()).getWrappedObject(); 072 } else { 073 b2 = new JBlasDenseDoubleMatrix2D(b).getWrappedObject(); 074 } 075 076 final DoubleMatrix x = Solve.solvePositive(a2, b2); 077 return new JBlasDenseDoubleMatrix2D(x); 078 } 079 080 }