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;
025    
026    import org.jblas.DoubleMatrix;
027    import org.ujmp.core.Matrix;
028    import org.ujmp.core.doublematrix.DenseDoubleMatrix2D;
029    import org.ujmp.core.doublematrix.stub.AbstractDenseDoubleMatrix2D;
030    import org.ujmp.core.exceptions.MatrixException;
031    import org.ujmp.core.interfaces.HasColumnMajorDoubleArray1D;
032    import org.ujmp.core.interfaces.HasRowMajorDoubleArray2D;
033    import org.ujmp.core.interfaces.Wrapper;
034    import org.ujmp.jblas.calculation.Chol;
035    import org.ujmp.jblas.calculation.Eig;
036    import org.ujmp.jblas.calculation.Inv;
037    import org.ujmp.jblas.calculation.InvSPD;
038    import org.ujmp.jblas.calculation.LU;
039    import org.ujmp.jblas.calculation.Solve;
040    
041    public class JBlasDenseDoubleMatrix2D extends AbstractDenseDoubleMatrix2D
042                    implements Wrapper<DoubleMatrix> {
043            private static final long serialVersionUID = 4929284378405884509L;
044    
045            private DoubleMatrix matrix;
046    
047            public JBlasDenseDoubleMatrix2D(long... size) {
048                    this.matrix = new DoubleMatrix((int) size[ROW], (int) size[COLUMN]);
049            }
050    
051            public JBlasDenseDoubleMatrix2D(DoubleMatrix matrix) {
052                    this.matrix = matrix;
053            }
054    
055            public JBlasDenseDoubleMatrix2D(Matrix source) throws MatrixException {
056                    if (source instanceof HasColumnMajorDoubleArray1D) {
057                            final double[] data = ((HasColumnMajorDoubleArray1D) source)
058                                            .getColumnMajorDoubleArray1D();
059                            this.matrix = new DoubleMatrix((int) source.getRowCount(),
060                                            (int) source.getColumnCount(), data);
061                    } else if (source instanceof HasRowMajorDoubleArray2D) {
062                            final double[][] data = ((HasRowMajorDoubleArray2D) source)
063                                            .getRowMajorDoubleArray2D();
064                            this.matrix = new DoubleMatrix(data);
065                    } else if (source instanceof DenseDoubleMatrix2D) {
066                            this.matrix = new DoubleMatrix((int) source.getRowCount(),
067                                            (int) source.getColumnCount());
068                            final DenseDoubleMatrix2D m2 = (DenseDoubleMatrix2D) source;
069                            for (int r = (int) source.getRowCount(); --r >= 0;) {
070                                    for (int c = (int) source.getColumnCount(); --c >= 0;) {
071                                            matrix.put(r, c, m2.getDouble(r, c));
072                                    }
073                            }
074                    } else {
075                            this.matrix = new DoubleMatrix((int) source.getRowCount(),
076                                            (int) source.getColumnCount());
077                            for (long[] c : source.availableCoordinates()) {
078                                    setDouble(source.getAsDouble(c), c);
079                            }
080                    }
081            }
082    
083            public JBlasDenseDoubleMatrix2D(long rowCount, long columnCount,
084                            double[] doubleArray) {
085                    this.matrix = new DoubleMatrix((int) rowCount, (int) columnCount,
086                                    doubleArray);
087            }
088    
089            public Matrix inv() throws MatrixException {
090                    return Inv.INSTANCE.calc(this);
091            }
092    
093            public Matrix invSPD() throws MatrixException {
094                    return InvSPD.INSTANCE.calc(this);
095            }
096    
097            public double getDouble(long row, long column) {
098                    return matrix.get((int) row, (int) column);
099            }
100    
101            public double getDouble(int row, int column) {
102                    return matrix.get(row, column);
103            }
104    
105            public long[] getSize() {
106                    return new long[] { matrix.getRows(), matrix.getColumns() };
107            }
108    
109            public void setDouble(double value, long row, long column) {
110                    matrix.put((int) row, (int) column, value);
111            }
112    
113            public void setDouble(double value, int row, int column) {
114                    matrix.put(row, column, value);
115            }
116    
117            public DoubleMatrix getWrappedObject() {
118                    return matrix;
119            }
120    
121            public void setWrappedObject(DoubleMatrix object) {
122                    this.matrix = object;
123            }
124    
125            public final Matrix copy() throws MatrixException {
126                    Matrix m = new JBlasDenseDoubleMatrix2D(matrix.dup());
127                    if (getAnnotation() != null) {
128                            m.setAnnotation(getAnnotation().clone());
129                    }
130                    return m;
131            }
132    
133            public Matrix transpose() {
134                    return new JBlasDenseDoubleMatrix2D(matrix.transpose());
135            }
136    
137            public Matrix[] lu() {
138                    return LU.INSTANCE.calc(this);
139            }
140    
141            public Matrix[] eig() {
142                    return Eig.INSTANCE.calc(this);
143            }
144    
145            public Matrix chol() {
146                    return Chol.INSTANCE.calc(this);
147            }
148    
149            public Matrix mtimes(Matrix m) {
150                    if (m instanceof JBlasDenseDoubleMatrix2D) {
151                            DoubleMatrix r = new DoubleMatrix((int) getRowCount(), (int) m
152                                            .getColumnCount());
153                            matrix.mmuli(((JBlasDenseDoubleMatrix2D) m).matrix, r);
154                            return new JBlasDenseDoubleMatrix2D(r);
155                    } else {
156                            return super.mtimes(m);
157                    }
158            }
159    
160            public Matrix plus(Matrix m) {
161                    if (m instanceof JBlasDenseDoubleMatrix2D) {
162                            DoubleMatrix r = new DoubleMatrix((int) getRowCount(),
163                                            (int) getColumnCount());
164                            matrix.addi(((JBlasDenseDoubleMatrix2D) m).matrix, r);
165                            return new JBlasDenseDoubleMatrix2D(r);
166                    } else {
167                            return super.plus(m);
168                    }
169            }
170    
171            public Matrix minus(Matrix m) {
172                    if (m instanceof JBlasDenseDoubleMatrix2D) {
173                            DoubleMatrix r = new DoubleMatrix((int) getRowCount(),
174                                            (int) getColumnCount());
175                            matrix.subi(((JBlasDenseDoubleMatrix2D) m).matrix, r);
176                            return new JBlasDenseDoubleMatrix2D(r);
177                    } else {
178                            return super.minus(m);
179                    }
180            }
181    
182            public Matrix times(double value) {
183                    DoubleMatrix r = new DoubleMatrix((int) getRowCount(),
184                                    (int) getColumnCount());
185                    return new JBlasDenseDoubleMatrix2D(matrix.muli(value, r));
186            }
187    
188            public Matrix divide(double value) {
189                    DoubleMatrix r = new DoubleMatrix((int) getRowCount(),
190                                    (int) getColumnCount());
191                    return new JBlasDenseDoubleMatrix2D(matrix.divi(value, r));
192            }
193    
194            public Matrix plus(double value) {
195                    DoubleMatrix r = new DoubleMatrix((int) getRowCount(),
196                                    (int) getColumnCount());
197                    return new JBlasDenseDoubleMatrix2D(matrix.addi(value, r));
198            }
199    
200            public Matrix minus(double value) {
201                    DoubleMatrix r = new DoubleMatrix((int) getRowCount(),
202                                    (int) getColumnCount());
203                    return new JBlasDenseDoubleMatrix2D(matrix.subi(value, r));
204            }
205    
206            public Matrix solve(Matrix b) {
207                    return Solve.INSTANCE.calc(this, b);
208            }
209    
210    }