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