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.core.doublematrix.impl;
025    
026    import org.ujmp.core.Matrix;
027    import org.ujmp.core.doublematrix.stub.AbstractDenseDoubleMatrix2D;
028    import org.ujmp.core.exceptions.MatrixException;
029    import org.ujmp.core.interfaces.HasRowMajorDoubleArray2D;
030    
031    public class ArrayDenseDoubleMatrix2D extends AbstractDenseDoubleMatrix2D implements
032                    HasRowMajorDoubleArray2D {
033            private static final long serialVersionUID = 3132491298449205914L;
034    
035            private final double[][] values;
036    
037            public ArrayDenseDoubleMatrix2D(Matrix m) throws MatrixException {
038                    if (m instanceof ArrayDenseDoubleMatrix2D) {
039                            double[][] v = ((ArrayDenseDoubleMatrix2D) m).values;
040                            this.values = new double[v.length][v[0].length];
041                            for (int r = v.length; --r != -1;) {
042                                    for (int c = v[0].length; --c != -1;) {
043                                            values[r][c] = v[r][c];
044                                    }
045                            }
046                    } else {
047                            values = new double[(int) m.getRowCount()][(int) m.getColumnCount()];
048                            for (long[] c : m.allCoordinates()) {
049                                    setAsDouble(m.getAsDouble(c), c);
050                            }
051                    }
052            }
053    
054            public ArrayDenseDoubleMatrix2D(double[]... v) {
055                    this.values = v;
056            }
057    
058            public ArrayDenseDoubleMatrix2D(long... size) {
059                    values = new double[(int) size[ROW]][(int) size[COLUMN]];
060            }
061    
062            public ArrayDenseDoubleMatrix2D(long rows, long cols) {
063                    values = new double[(int) rows][(int) cols];
064            }
065    
066            public ArrayDenseDoubleMatrix2D(double[] v) {
067                    this.values = new double[v.length][1];
068                    for (int r = v.length; --r != -1;) {
069                            values[r][0] = v[r];
070                    }
071            }
072    
073            public long[] getSize() {
074                    return new long[] { values.length, values.length == 0 ? 0 : values[0].length };
075            }
076    
077            public long getRowCount() {
078                    return values.length;
079            }
080    
081            public long getColumnCount() {
082                    return values.length == 0 ? 0 : values[0].length;
083            }
084    
085            public double getDouble(long row, long column) {
086                    return values[(int) row][(int) column];
087            }
088    
089            public void setDouble(double value, long row, long column) {
090                    values[(int) row][(int) column] = value;
091            }
092    
093            public double getDouble(int row, int column) {
094                    return values[row][column];
095            }
096    
097            public void setDouble(double value, int row, int column) {
098                    values[row][column] = value;
099            }
100    
101            public final Matrix copy() throws MatrixException {
102                    double[][] result = new double[values.length][values[0].length];
103                    for (int r = result.length; --r != -1;) {
104                            for (int c = result[0].length; --c != -1;) {
105                                    result[r][c] = values[r][c];
106                            }
107                    }
108                    Matrix m = new ArrayDenseDoubleMatrix2D(result);
109                    if (getAnnotation() != null) {
110                            m.setAnnotation(getAnnotation().clone());
111                    }
112                    return m;
113            }
114    
115            public boolean containsNaN() {
116                    for (int r = values.length; --r != -1;) {
117                            for (int c = values[0].length; --c != -1;) {
118                                    if (Double.isNaN(values[r][c])) {
119                                            return true;
120                                    }
121                            }
122                    }
123                    return false;
124            }
125    
126            public double[][] getRowMajorDoubleArray2D() {
127                    return values;
128            }
129    
130    }