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.ojalgo.calculation; 025 026 import org.ojalgo.matrix.decomposition.LUDecomposition; 027 import org.ojalgo.matrix.store.PrimitiveDenseStore; 028 import org.ujmp.core.Matrix; 029 import org.ujmp.ojalgo.OjalgoDenseDoubleMatrix2D; 030 031 public class LU implements 032 org.ujmp.core.doublematrix.calculation.general.decomposition.LU<Matrix> { 033 034 public static LU INSTANCE = new LU(); 035 036 public Matrix[] calc(Matrix source) { 037 final org.ojalgo.matrix.decomposition.LU<Double> lu = LUDecomposition 038 .makePrimitive(); 039 PrimitiveDenseStore matrix = null; 040 if (source instanceof OjalgoDenseDoubleMatrix2D) { 041 matrix = ((OjalgoDenseDoubleMatrix2D) source).getWrappedObject(); 042 } else { 043 matrix = new OjalgoDenseDoubleMatrix2D(source).getWrappedObject(); 044 } 045 lu.compute(matrix); 046 final Matrix l = new OjalgoDenseDoubleMatrix2D(lu.getL()); 047 final Matrix u = new OjalgoDenseDoubleMatrix2D(lu.getU()); 048 final int m = (int) source.getRowCount(); 049 final int[] piv = lu.getPivotOrder(); 050 final Matrix p = new OjalgoDenseDoubleMatrix2D(m, m); 051 for (int i = 0; i < m; i++) { 052 p.setAsDouble(1, i, piv[i]); 053 } 054 return new Matrix[] { l, u, p }; 055 } 056 057 public Matrix solve(Matrix a, Matrix b) { 058 final org.ojalgo.matrix.decomposition.LU<Double> lu = LUDecomposition 059 .makePrimitive(); 060 PrimitiveDenseStore a2 = null; 061 PrimitiveDenseStore b2 = null; 062 if (a instanceof OjalgoDenseDoubleMatrix2D) { 063 a2 = ((OjalgoDenseDoubleMatrix2D) a).getWrappedObject(); 064 } else { 065 a2 = new OjalgoDenseDoubleMatrix2D(a).getWrappedObject(); 066 } 067 if (b instanceof OjalgoDenseDoubleMatrix2D) { 068 b2 = ((OjalgoDenseDoubleMatrix2D) b).getWrappedObject(); 069 } else { 070 b2 = new OjalgoDenseDoubleMatrix2D(b).getWrappedObject(); 071 } 072 lu.compute(a2); 073 return new OjalgoDenseDoubleMatrix2D(lu.solve(b2)); 074 } 075 076 }