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.jlinalg; 025 026 import java.io.IOException; 027 import java.io.ObjectInputStream; 028 import java.io.ObjectOutputStream; 029 030 import org.jlinalg.MatrixMultiplication; 031 import org.jlinalg.doublewrapper.DoubleWrapper; 032 import org.ujmp.core.Matrix; 033 import org.ujmp.core.doublematrix.stub.AbstractDenseDoubleMatrix2D; 034 import org.ujmp.core.exceptions.MatrixException; 035 import org.ujmp.core.interfaces.Wrapper; 036 037 public class JLinAlgDenseDoubleMatrix2D extends AbstractDenseDoubleMatrix2D 038 implements Wrapper<org.jlinalg.Matrix<DoubleWrapper>> { 039 private static final long serialVersionUID = -3223474248020842822L; 040 041 private transient org.jlinalg.Matrix<DoubleWrapper> matrix = null; 042 043 // matrix must be filled with zeros 044 public JLinAlgDenseDoubleMatrix2D(long... size) { 045 this.matrix = new org.jlinalg.Matrix<DoubleWrapper>((int) size[ROW], 046 (int) size[COLUMN], DoubleWrapper.FACTORY); 047 for (long[] c : availableCoordinates()) { 048 setDouble(0, c); 049 } 050 } 051 052 public JLinAlgDenseDoubleMatrix2D(org.jlinalg.Matrix<DoubleWrapper> m) { 053 this.matrix = m; 054 } 055 056 public JLinAlgDenseDoubleMatrix2D(Matrix source) throws MatrixException { 057 this(source.getSize()); 058 for (long[] c : source.availableCoordinates()) { 059 setAsDouble(source.getAsDouble(c), c); 060 } 061 } 062 063 public double getDouble(long row, long column) { 064 DoubleWrapper d = matrix.get((int) row + 1, (int) column + 1); 065 return d == null ? 0.0 : d.doubleValue(); 066 } 067 068 public double getDouble(int row, int column) { 069 DoubleWrapper d = matrix.get((int) row + 1, (int) column + 1); 070 return d == null ? 0.0 : d.doubleValue(); 071 } 072 073 public long[] getSize() { 074 return new long[] { matrix.getRows(), matrix.getCols() }; 075 } 076 077 public void setDouble(double value, long row, long column) { 078 matrix.set((int) row + 1, (int) column + 1, new DoubleWrapper(value)); 079 } 080 081 public void setDouble(double value, int row, int column) { 082 matrix.set(row + 1, column + 1, new DoubleWrapper(value)); 083 } 084 085 public org.jlinalg.Matrix<DoubleWrapper> getWrappedObject() { 086 return matrix; 087 } 088 089 public void setWrappedObject(org.jlinalg.Matrix<DoubleWrapper> object) { 090 this.matrix = object; 091 } 092 093 public Matrix transpose() { 094 return new JLinAlgDenseDoubleMatrix2D(matrix.transpose()); 095 } 096 097 public Matrix inv() { 098 return new JLinAlgDenseDoubleMatrix2D(matrix.inverse()); 099 } 100 101 public Matrix plus(double value) { 102 return new JLinAlgDenseDoubleMatrix2D(matrix.add(new DoubleWrapper( 103 value))); 104 } 105 106 public Matrix times(double value) { 107 return new JLinAlgDenseDoubleMatrix2D(matrix 108 .multiply(new DoubleWrapper(value))); 109 } 110 111 public Matrix mtimes(Matrix m) { 112 if (m instanceof JLinAlgDenseDoubleMatrix2D) { 113 org.jlinalg.Matrix<DoubleWrapper> b = ((JLinAlgDenseDoubleMatrix2D) m) 114 .getWrappedObject(); 115 org.jlinalg.Matrix<DoubleWrapper> c = MatrixMultiplication 116 .strassenBodrato(matrix, b); 117 return new JLinAlgDenseDoubleMatrix2D(c); 118 } else { 119 return super.mtimes(m); 120 } 121 } 122 123 public Matrix copy() { 124 Matrix m = new JLinAlgDenseDoubleMatrix2D(matrix.copy()); 125 if (getAnnotation() != null) { 126 m.setAnnotation(getAnnotation().clone()); 127 } 128 return m; 129 } 130 131 private void readObject(ObjectInputStream s) throws IOException, 132 ClassNotFoundException { 133 s.defaultReadObject(); 134 double[][] data = (double[][]) s.readObject(); 135 matrix = new org.jlinalg.Matrix<DoubleWrapper>(data.length, 136 data[0].length, DoubleWrapper.FACTORY); 137 for (int r = 0; r < data.length; r++) { 138 for (int c = 0; c < data[0].length; c++) { 139 setDouble(data[r][c], r, c); 140 } 141 } 142 } 143 144 private void writeObject(ObjectOutputStream s) throws IOException, 145 MatrixException { 146 s.defaultWriteObject(); 147 s.writeObject(toDoubleArray()); 148 } 149 150 }