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.LU;
027    import org.ojalgo.matrix.decomposition.LUDecomposition;
028    import org.ojalgo.matrix.decomposition.QR;
029    import org.ojalgo.matrix.decomposition.QRDecomposition;
030    import org.ojalgo.matrix.store.PrimitiveDenseStore;
031    import org.ujmp.core.Matrix;
032    import org.ujmp.ojalgo.OjalgoDenseDoubleMatrix2D;
033    
034    public class Solve
035                    implements
036                    org.ujmp.core.doublematrix.calculation.general.decomposition.Solve<Matrix> {
037    
038            public static Solve INSTANCE = new Solve();
039    
040            public Matrix calc(Matrix a, Matrix b) {
041                    PrimitiveDenseStore a2 = null;
042                    PrimitiveDenseStore b2 = null;
043                    if (a instanceof OjalgoDenseDoubleMatrix2D) {
044                            a2 = ((OjalgoDenseDoubleMatrix2D) a).getWrappedObject();
045                    } else {
046                            a2 = new OjalgoDenseDoubleMatrix2D(a).getWrappedObject();
047                    }
048                    if (b instanceof OjalgoDenseDoubleMatrix2D) {
049                            b2 = ((OjalgoDenseDoubleMatrix2D) b).getWrappedObject();
050                    } else {
051                            b2 = new OjalgoDenseDoubleMatrix2D(b).getWrappedObject();
052                    }
053                    if (a.isSquare()) {
054                            final LU<Double> lu = LUDecomposition.makePrimitive();
055                            lu.compute(a2);
056                            return new OjalgoDenseDoubleMatrix2D(lu.solve(b2));
057                    } else {
058                            final QR<Double> qr = QRDecomposition.makePrimitive();
059                            qr.compute(a2);
060                            return new OjalgoDenseDoubleMatrix2D(qr.solve(b2));
061                    }
062            }
063    
064    }