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.jampack; 025 026 import java.io.IOException; 027 import java.io.ObjectInputStream; 028 import java.io.ObjectOutputStream; 029 030 import org.ujmp.core.Matrix; 031 import org.ujmp.core.calculation.Calculation.Ret; 032 import org.ujmp.core.doublematrix.stub.AbstractDenseDoubleMatrix2D; 033 import org.ujmp.core.exceptions.MatrixException; 034 import org.ujmp.core.interfaces.Wrapper; 035 036 import Jampack.Eig; 037 import Jampack.H; 038 import Jampack.Inv; 039 import Jampack.JampackException; 040 import Jampack.Parameters; 041 import Jampack.Pivot; 042 import Jampack.Solve; 043 import Jampack.Times; 044 import Jampack.Z; 045 import Jampack.Zchol; 046 import Jampack.Zludpp; 047 import Jampack.Zmat; 048 import Jampack.Zqrd; 049 import Jampack.Zsvd; 050 051 public class JampackDenseDoubleMatrix2D extends AbstractDenseDoubleMatrix2D 052 implements Wrapper<Zmat> { 053 private static final long serialVersionUID = 4929284378405884509L; 054 055 static { 056 try { 057 Parameters.setBaseIndex(0); 058 } catch (JampackException e) { 059 e.printStackTrace(); 060 } 061 } 062 063 private transient Zmat matrix = null; 064 065 public JampackDenseDoubleMatrix2D(long... size) { 066 this.matrix = new Zmat((int) size[ROW], (int) size[COLUMN]); 067 } 068 069 public JampackDenseDoubleMatrix2D(Zmat matrix) { 070 this.matrix = matrix; 071 } 072 073 public JampackDenseDoubleMatrix2D(Matrix source) throws MatrixException { 074 this(source.getSize()); 075 for (long[] c : source.availableCoordinates()) { 076 setDouble(source.getAsDouble(c), c); 077 } 078 } 079 080 public Matrix inv() throws MatrixException { 081 try { 082 return new JampackDenseDoubleMatrix2D(Inv.o(matrix)); 083 } catch (Exception e) { 084 throw new MatrixException(e); 085 } 086 } 087 088 public double getDouble(long row, long column) { 089 return matrix.get((int) row, (int) column).re; 090 } 091 092 public double getDouble(int row, int column) { 093 return matrix.get(row, column).re; 094 } 095 096 public long[] getSize() { 097 return new long[] { matrix.nr, matrix.nc }; 098 } 099 100 public void setDouble(double value, long row, long column) { 101 matrix.put((int) row, (int) column, new Z(value, 0)); 102 } 103 104 public void setDouble(double value, int row, int column) { 105 matrix.put(row, column, new Z(value, 0)); 106 } 107 108 public Zmat getWrappedObject() { 109 return matrix; 110 } 111 112 public void setWrappedObject(Zmat object) { 113 this.matrix = object; 114 } 115 116 public final Matrix copy() throws MatrixException { 117 Matrix m = new JampackDenseDoubleMatrix2D(new Zmat(matrix)); 118 if (getAnnotation() != null) { 119 m.setAnnotation(getAnnotation().clone()); 120 } 121 return m; 122 } 123 124 public Matrix transpose() { 125 return new JampackDenseDoubleMatrix2D(H.trans(matrix)); 126 } 127 128 public Matrix[] svd() { 129 if (isSquare()) { 130 try { 131 Zsvd svd = new Zsvd(matrix); 132 Matrix u = new JampackDenseDoubleMatrix2D(svd.U); 133 Matrix s = new JampackDenseDoubleMatrix2D(new Zmat(svd.S)); 134 Matrix v = new JampackDenseDoubleMatrix2D(svd.V); 135 return new Matrix[] { u, s, v }; 136 } catch (Exception e) { 137 throw new MatrixException(e); 138 } 139 } else { 140 return super.svd(); 141 } 142 } 143 144 public Matrix[] qr() { 145 try { 146 Zqrd qr = new Zqrd(matrix); 147 Matrix q = new JampackDenseDoubleMatrix2D(qr.Q); 148 Matrix r = new JampackDenseDoubleMatrix2D(qr.R); 149 return new Matrix[] { q, r }; 150 } catch (Exception e) { 151 throw new MatrixException(e); 152 } 153 } 154 155 public Matrix[] lu() { 156 try { 157 Zludpp lu = new Zludpp(matrix); 158 Matrix l = new JampackDenseDoubleMatrix2D(lu.L); 159 Matrix u = new JampackDenseDoubleMatrix2D(lu.U); 160 int m = (int) getRowCount(); 161 JampackDenseDoubleMatrix2D eye = new JampackDenseDoubleMatrix2D(m, 162 m); 163 eye.eye(Ret.ORIG); 164 Matrix p = new JampackDenseDoubleMatrix2D(Pivot.row(eye 165 .getWrappedObject(), lu.pvt)); 166 return new Matrix[] { l, u, p }; 167 } catch (Exception e) { 168 throw new MatrixException(e); 169 } 170 } 171 172 public Matrix[] eig() { 173 try { 174 Eig eig = new Eig(matrix); 175 Matrix v = new JampackDenseDoubleMatrix2D(eig.X); 176 Matrix d = new JampackDenseDoubleMatrix2D(new Zmat(eig.D)); 177 return new Matrix[] { v, d }; 178 } catch (Exception e) { 179 throw new MatrixException(e); 180 } 181 } 182 183 public Matrix chol() { 184 try { 185 Zchol chol = new Zchol(matrix); 186 Matrix r = new JampackDenseDoubleMatrix2D(chol.R); 187 return r; 188 } catch (Exception e) { 189 throw new MatrixException(e); 190 } 191 } 192 193 public Matrix mtimes(Matrix m) { 194 try { 195 if (m instanceof JampackDenseDoubleMatrix2D) { 196 return new JampackDenseDoubleMatrix2D(Times.o(matrix, 197 ((JampackDenseDoubleMatrix2D) m).matrix)); 198 } else { 199 return super.mtimes(m); 200 } 201 } catch (Exception e) { 202 throw new MatrixException(e); 203 } 204 } 205 206 public Matrix times(double value) { 207 try { 208 return new JampackDenseDoubleMatrix2D(Times.o(new Z(value, 0), 209 matrix)); 210 211 } catch (Exception e) { 212 throw new MatrixException(e); 213 } 214 } 215 216 public Matrix divide(double value) { 217 try { 218 return new JampackDenseDoubleMatrix2D(Times.o( 219 new Z(1.0 / value, 0), matrix)); 220 221 } catch (Exception e) { 222 throw new MatrixException(e); 223 } 224 } 225 226 private void readObject(ObjectInputStream s) throws IOException, 227 ClassNotFoundException { 228 s.defaultReadObject(); 229 double[][] values = (double[][]) s.readObject(); 230 matrix = new Zmat(values); 231 } 232 233 private void writeObject(ObjectOutputStream s) throws IOException, 234 MatrixException { 235 s.defaultWriteObject(); 236 s.writeObject(matrix.getRe()); 237 } 238 239 public Matrix solve(Matrix b) { 240 try { 241 if (isSquare() && b instanceof JampackDenseDoubleMatrix2D) { 242 JampackDenseDoubleMatrix2D b2 = (JampackDenseDoubleMatrix2D) b; 243 Zmat x = Solve.aib(matrix, b2.matrix); 244 return new JampackDenseDoubleMatrix2D(x); 245 } else { 246 throw new MatrixException("only supported for square matrices"); 247 } 248 } catch (Exception e) { 249 throw new MatrixException(e); 250 } 251 } 252 253 }