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 LU implements
034                    org.ujmp.core.doublematrix.calculation.general.decomposition.LU<Matrix> {
035    
036            public static LU INSTANCE = new LU();
037    
038            public Matrix[] calc(Matrix source) {
039                    final DoubleMatrix matrix;
040                    if (source instanceof JBlasDenseDoubleMatrix2D) {
041                            matrix = ((JBlasDenseDoubleMatrix2D) source).getWrappedObject();
042                    } else if (source instanceof HasColumnMajorDoubleArray1D) {
043                            matrix = new JBlasDenseDoubleMatrix2D(source.getRowCount(), source
044                                            .getColumnCount(), ((HasColumnMajorDoubleArray1D) source)
045                                            .getColumnMajorDoubleArray1D()).getWrappedObject();
046                    } else {
047                            matrix = new JBlasDenseDoubleMatrix2D(source).getWrappedObject();
048                    }
049                    final Decompose.LUDecomposition<DoubleMatrix> lu = Decompose.lu(matrix);
050                    final Matrix l = new JBlasDenseDoubleMatrix2D(lu.l);
051                    final Matrix u = new JBlasDenseDoubleMatrix2D(lu.u);
052                    final Matrix p = new JBlasDenseDoubleMatrix2D(lu.p.transpose());
053                    return new Matrix[] { l, u, p };
054            }
055    
056            public Matrix solve(Matrix a, Matrix b) {
057                    final DoubleMatrix a2;
058                    final DoubleMatrix b2;
059                    if (a instanceof JBlasDenseDoubleMatrix2D) {
060                            a2 = ((JBlasDenseDoubleMatrix2D) a).getWrappedObject();
061                    } else if (a instanceof HasColumnMajorDoubleArray1D) {
062                            a2 = new JBlasDenseDoubleMatrix2D(a.getRowCount(), a
063                                            .getColumnCount(), ((HasColumnMajorDoubleArray1D) a)
064                                            .getColumnMajorDoubleArray1D()).getWrappedObject();
065                    } else {
066                            a2 = new JBlasDenseDoubleMatrix2D(a).getWrappedObject();
067                    }
068                    if (b instanceof JBlasDenseDoubleMatrix2D) {
069                            b2 = ((JBlasDenseDoubleMatrix2D) b).getWrappedObject();
070                    } else if (b instanceof HasColumnMajorDoubleArray1D) {
071                            b2 = new JBlasDenseDoubleMatrix2D(b.getRowCount(), b
072                                            .getColumnCount(), ((HasColumnMajorDoubleArray1D) b)
073                                            .getColumnMajorDoubleArray1D()).getWrappedObject();
074                    } else {
075                            b2 = new JBlasDenseDoubleMatrix2D(b).getWrappedObject();
076                    }
077    
078                    final DoubleMatrix x = Solve.solve(a2, b2);
079                    return new JBlasDenseDoubleMatrix2D(x);
080            }
081    
082    }