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.bigdecimalmatrix.impl; 025 026 import java.math.BigDecimal; 027 import java.util.Arrays; 028 029 import org.ujmp.core.Matrix; 030 import org.ujmp.core.bigdecimalmatrix.stub.AbstractDenseBigDecimalMatrix2D; 031 import org.ujmp.core.exceptions.MatrixException; 032 033 public class ArrayDenseBigDecimalMatrix2D extends AbstractDenseBigDecimalMatrix2D { 034 private static final long serialVersionUID = 5701752483223767209L; 035 036 private BigDecimal[][] values = null; 037 038 public ArrayDenseBigDecimalMatrix2D(Matrix m) throws MatrixException { 039 if (m instanceof ArrayDenseBigDecimalMatrix2D) { 040 BigDecimal[][] v = ((ArrayDenseBigDecimalMatrix2D) m).values; 041 this.values = new BigDecimal[v.length][v[0].length]; 042 for (int r = v.length; --r >= 0;) { 043 for (int c = v[0].length; --c >= 0;) { 044 values[r][c] = v[r][c]; 045 } 046 } 047 } else { 048 values = new BigDecimal[(int) m.getRowCount()][(int) m.getColumnCount()]; 049 for (long[] c : m.allCoordinates()) { 050 setAsBigDecimal(m.getAsBigDecimal(c), c); 051 } 052 } 053 } 054 055 public ArrayDenseBigDecimalMatrix2D(BigDecimal[]... v) { 056 this.values = v; 057 } 058 059 public ArrayDenseBigDecimalMatrix2D(long... size) { 060 values = new BigDecimal[(int) size[ROW]][(int) size[COLUMN]]; 061 for (int r = values.length; --r != -1;) { 062 Arrays.fill(values[r], BigDecimal.ZERO); 063 } 064 } 065 066 public ArrayDenseBigDecimalMatrix2D(BigDecimal... v) { 067 this.values = new BigDecimal[v.length][1]; 068 for (int r = v.length; --r >= 0;) { 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 BigDecimal getBigDecimal(long row, long column) { 086 return values[(int) row][(int) column]; 087 } 088 089 public void setBigDecimal(BigDecimal value, long row, long column) { 090 values[(int) row][(int) column] = value; 091 } 092 093 public BigDecimal getBigDecimal(int row, int column) { 094 return values[row][column]; 095 } 096 097 public void setBigDecimal(BigDecimal value, int row, int column) { 098 values[row][column] = value; 099 } 100 101 }