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 DefaultDenseBigDecimalMatrix2D extends AbstractDenseBigDecimalMatrix2D {
034            private static final long serialVersionUID = -5227328974882402868L;
035    
036            private BigDecimal[] values = null;
037    
038            private long[] size = null;
039    
040            private int rows = 0;
041    
042            private int cols = 0;
043    
044            public DefaultDenseBigDecimalMatrix2D(Matrix m) throws MatrixException {
045                    this.rows = (int) m.getRowCount();
046                    this.cols = (int) m.getColumnCount();
047                    this.size = new long[] { rows, cols };
048                    if (m instanceof DefaultDenseBigDecimalMatrix2D) {
049                            BigDecimal[] v = ((DefaultDenseBigDecimalMatrix2D) m).values;
050                            this.values = new BigDecimal[v.length];
051                            System.arraycopy(v, 0, this.values, 0, v.length);
052                    } else {
053                            this.values = new BigDecimal[rows * cols];
054                            for (long[] c : m.allCoordinates()) {
055                                    setBigDecimal(m.getAsBigDecimal(c), c);
056                            }
057                    }
058            }
059    
060            public DefaultDenseBigDecimalMatrix2D(long... size) {
061                    this.rows = (int) size[ROW];
062                    this.cols = (int) size[COLUMN];
063                    this.size = new long[] { rows, cols };
064                    this.values = new BigDecimal[rows * cols];
065                    Arrays.fill(values, BigDecimal.ZERO);
066            }
067    
068            public DefaultDenseBigDecimalMatrix2D(BigDecimal[] v, int rows, int cols) {
069                    this.rows = rows;
070                    this.cols = cols;
071                    this.size = new long[] { rows, cols };
072                    this.values = v;
073            }
074    
075            public long[] getSize() {
076                    return size;
077            }
078    
079            public long getRowCount() {
080                    return rows;
081            }
082    
083            public long getColumnCount() {
084                    return cols;
085            }
086    
087            public BigDecimal getBigDecimal(long row, long column) {
088                    return values[(int) (column * rows + row)];
089            }
090    
091            public void setBigDecimal(BigDecimal value, long row, long column) {
092                    values[(int) (column * rows + row)] = value;
093            }
094    
095            public BigDecimal getBigDecimal(int row, int column) {
096                    return values[column * rows + row];
097            }
098    
099            public void setBigDecimal(BigDecimal value, int row, int column) {
100                    values[column * rows + row] = value;
101            }
102    
103    }