2 |
26 Feb 07 |
jari |
1 |
/* |
2 |
26 Feb 07 |
jari |
Copyright @ 1999-2003, The Institute for Genomic Research (TIGR). |
2 |
26 Feb 07 |
jari |
All rights reserved. |
2 |
26 Feb 07 |
jari |
4 |
*/ |
2 |
26 Feb 07 |
jari |
5 |
/* |
2 |
26 Feb 07 |
jari |
* $RCSfile: FloatMatrix.java,v $ |
2 |
26 Feb 07 |
jari |
* $Revision: 1.6 $ |
2 |
26 Feb 07 |
jari |
* $Date: 2006/05/02 16:57:56 $ |
2 |
26 Feb 07 |
jari |
* $Author: eleanorahowe $ |
2 |
26 Feb 07 |
jari |
* $State: Exp $ |
2 |
26 Feb 07 |
jari |
11 |
*/ |
2 |
26 Feb 07 |
jari |
12 |
package org.tigr.util; |
2 |
26 Feb 07 |
jari |
13 |
|
2 |
26 Feb 07 |
jari |
14 |
import java.io.BufferedReader; |
2 |
26 Feb 07 |
jari |
15 |
import java.io.PrintWriter; |
2 |
26 Feb 07 |
jari |
16 |
import java.io.StreamTokenizer; |
2 |
26 Feb 07 |
jari |
17 |
import java.text.DecimalFormat; |
2 |
26 Feb 07 |
jari |
18 |
import java.text.NumberFormat; |
2 |
26 Feb 07 |
jari |
19 |
|
2 |
26 Feb 07 |
jari |
20 |
|
2 |
26 Feb 07 |
jari |
21 |
|
2 |
26 Feb 07 |
jari |
// Changed from double to float for memory demand reduction! |
2 |
26 Feb 07 |
jari |
// This Class is based on the Jama Matrix |
2 |
26 Feb 07 |
jari |
24 |
|
2 |
26 Feb 07 |
jari |
25 |
/** |
2 |
26 Feb 07 |
jari |
* Jama = Java Matrix class. |
2 |
26 Feb 07 |
jari |
* <P> |
2 |
26 Feb 07 |
jari |
* The Java Matrix Class provides the fundamental operations of numerical |
2 |
26 Feb 07 |
jari |
* linear algebra. Various constructors create Matrices from two dimensional |
2 |
26 Feb 07 |
jari |
* arrays of double precision floating point numbers. Various "gets" and |
2 |
26 Feb 07 |
jari |
* "sets" provide access to submatrices and matrix elements. Several methods |
2 |
26 Feb 07 |
jari |
* implement basic matrix arithmetic, including matrix addition and |
2 |
26 Feb 07 |
jari |
* multiplication, matrix norms, and element-by-element array operations. |
2 |
26 Feb 07 |
jari |
* Methods for reading and printing matrices are also included. All the |
2 |
26 Feb 07 |
jari |
* operations in this version of the Matrix Class involve real matrices. |
2 |
26 Feb 07 |
jari |
* Complex matrices may be handled in a future version. |
2 |
26 Feb 07 |
jari |
* <P> |
2 |
26 Feb 07 |
jari |
* Five fundamental matrix decompositions, which consist of pairs or triples |
2 |
26 Feb 07 |
jari |
* of matrices, permutation vectors, and the like, produce results in five |
2 |
26 Feb 07 |
jari |
* decomposition classes. These decompositions are accessed by the Matrix |
2 |
26 Feb 07 |
jari |
* class to compute solutions of simultaneous linear equations, determinants, |
2 |
26 Feb 07 |
jari |
* inverses and other matrix functions. The five decompositions are: |
2 |
26 Feb 07 |
jari |
* <P><UL> |
2 |
26 Feb 07 |
jari |
* <LI>Cholesky Decomposition of symmetric, positive definite matrices. |
2 |
26 Feb 07 |
jari |
* <LI>LU Decomposition of rectangular matrices. |
2 |
26 Feb 07 |
jari |
* <LI>QR Decomposition of rectangular matrices. |
2 |
26 Feb 07 |
jari |
* <LI>Singular Value Decomposition of rectangular matrices. |
2 |
26 Feb 07 |
jari |
* <LI>Eigenvalue Decomposition of both symmetric and nonsymmetric square matrices. |
2 |
26 Feb 07 |
jari |
* </UL> |
2 |
26 Feb 07 |
jari |
* <DL> |
2 |
26 Feb 07 |
jari |
* <DT><B>Example of use:</B></DT> |
2 |
26 Feb 07 |
jari |
* <P> |
2 |
26 Feb 07 |
jari |
* <DD>Solve a linear system A x = b and compute the residual norm, ||b - A x||. |
2 |
26 Feb 07 |
jari |
* <P><PRE> |
2 |
26 Feb 07 |
jari |
* double[][] vals = {{1.,2.,3},{4.,5.,6.},{7.,8.,10.}}; |
2 |
26 Feb 07 |
jari |
* Matrix A = new Matrix(vals); |
2 |
26 Feb 07 |
jari |
* Matrix b = Matrix.random(3,1); |
2 |
26 Feb 07 |
jari |
* Matrix x = A.solve(b); |
2 |
26 Feb 07 |
jari |
* Matrix r = A.times(x).minus(b); |
2 |
26 Feb 07 |
jari |
* double rnorm = r.normInf(); |
2 |
26 Feb 07 |
jari |
* </PRE></DD> |
2 |
26 Feb 07 |
jari |
* </DL> |
2 |
26 Feb 07 |
jari |
63 |
* |
2 |
26 Feb 07 |
jari |
* @author The MathWorks, Inc. and the National Institute of Standards and Technology. |
2 |
26 Feb 07 |
jari |
* @version 5 August 1998 |
2 |
26 Feb 07 |
jari |
66 |
*/ |
2 |
26 Feb 07 |
jari |
67 |
|
2 |
26 Feb 07 |
jari |
68 |
public class FloatMatrix implements Cloneable { |
2 |
26 Feb 07 |
jari |
69 |
|
2 |
26 Feb 07 |
jari |
70 |
/* ------------------------ |
2 |
26 Feb 07 |
jari |
Class variables |
2 |
26 Feb 07 |
jari |
72 |
* ------------------------ */ |
2 |
26 Feb 07 |
jari |
73 |
|
2 |
26 Feb 07 |
jari |
/** Array for internal storage of elements. |
2 |
26 Feb 07 |
jari |
* @serial internal array storage. |
2 |
26 Feb 07 |
jari |
76 |
*/ |
2 |
26 Feb 07 |
jari |
77 |
public float[][] A; |
2 |
26 Feb 07 |
jari |
78 |
|
2 |
26 Feb 07 |
jari |
/** Row and column dimensions. |
2 |
26 Feb 07 |
jari |
* @serial row dimension. |
2 |
26 Feb 07 |
jari |
* @serial column dimension. |
2 |
26 Feb 07 |
jari |
82 |
*/ |
2 |
26 Feb 07 |
jari |
83 |
public int m, n; |
2 |
26 Feb 07 |
jari |
84 |
|
2 |
26 Feb 07 |
jari |
85 |
/* ------------------------ |
2 |
26 Feb 07 |
jari |
Constructors |
2 |
26 Feb 07 |
jari |
87 |
* ------------------------ */ |
2 |
26 Feb 07 |
jari |
88 |
|
2 |
26 Feb 07 |
jari |
/** Construct an m-by-n matrix of zeros. |
2 |
26 Feb 07 |
jari |
* @param m Number of rows. |
2 |
26 Feb 07 |
jari |
* @param n Number of colums. |
2 |
26 Feb 07 |
jari |
92 |
*/ |
2 |
26 Feb 07 |
jari |
93 |
|
2 |
26 Feb 07 |
jari |
94 |
public FloatMatrix(int m, int n) { |
2 |
26 Feb 07 |
jari |
95 |
this.m = m; |
2 |
26 Feb 07 |
jari |
96 |
this.n = n; |
2 |
26 Feb 07 |
jari |
97 |
A = new float[m][n]; |
2 |
26 Feb 07 |
jari |
98 |
} |
2 |
26 Feb 07 |
jari |
99 |
|
2 |
26 Feb 07 |
jari |
/** Construct an m-by-n constant matrix. |
2 |
26 Feb 07 |
jari |
* @param m Number of rows. |
2 |
26 Feb 07 |
jari |
* @param n Number of colums. |
2 |
26 Feb 07 |
jari |
* @param s Fill the matrix with this scalar value. |
2 |
26 Feb 07 |
jari |
104 |
*/ |
2 |
26 Feb 07 |
jari |
105 |
|
2 |
26 Feb 07 |
jari |
106 |
public FloatMatrix(int m, int n, float s) { |
2 |
26 Feb 07 |
jari |
107 |
this.m = m; |
2 |
26 Feb 07 |
jari |
108 |
this.n = n; |
2 |
26 Feb 07 |
jari |
109 |
A = new float[m][n]; |
2 |
26 Feb 07 |
jari |
110 |
for (int i = 0; i < m; i++) { |
2 |
26 Feb 07 |
jari |
111 |
for (int j = 0; j < n; j++) { |
2 |
26 Feb 07 |
jari |
112 |
A[i][j] = s; |
2 |
26 Feb 07 |
jari |
113 |
} |
2 |
26 Feb 07 |
jari |
114 |
} |
2 |
26 Feb 07 |
jari |
115 |
} |
2 |
26 Feb 07 |
jari |
116 |
|
2 |
26 Feb 07 |
jari |
/** Construct a matrix from a 2-D array. |
2 |
26 Feb 07 |
jari |
* @param A Two-dimensional array of doubles. |
2 |
26 Feb 07 |
jari |
* @exception IllegalArgumentException All rows must have the same length |
2 |
26 Feb 07 |
jari |
* @see #constructWithCopy |
2 |
26 Feb 07 |
jari |
121 |
*/ |
2 |
26 Feb 07 |
jari |
122 |
|
2 |
26 Feb 07 |
jari |
123 |
public FloatMatrix(float[][] A) { |
2 |
26 Feb 07 |
jari |
124 |
m = A.length; |
2 |
26 Feb 07 |
jari |
125 |
n = A[0].length; |
2 |
26 Feb 07 |
jari |
126 |
for (int i = 0; i < m; i++) { |
2 |
26 Feb 07 |
jari |
127 |
if (A[i].length != n) { |
2 |
26 Feb 07 |
jari |
128 |
throw new IllegalArgumentException("All rows must have the same length."); |
2 |
26 Feb 07 |
jari |
129 |
} |
2 |
26 Feb 07 |
jari |
130 |
} |
2 |
26 Feb 07 |
jari |
131 |
this.A = A; |
2 |
26 Feb 07 |
jari |
132 |
} |
2 |
26 Feb 07 |
jari |
133 |
|
2 |
26 Feb 07 |
jari |
/** Construct a matrix quickly without checking arguments. |
2 |
26 Feb 07 |
jari |
* @param A Two-dimensional array of doubles. |
2 |
26 Feb 07 |
jari |
* @param m Number of rows. |
2 |
26 Feb 07 |
jari |
* @param n Number of colums. |
2 |
26 Feb 07 |
jari |
138 |
*/ |
2 |
26 Feb 07 |
jari |
139 |
|
2 |
26 Feb 07 |
jari |
140 |
public FloatMatrix(float[][] A, int m, int n) { |
2 |
26 Feb 07 |
jari |
141 |
this.A = A; |
2 |
26 Feb 07 |
jari |
142 |
this.m = m; |
2 |
26 Feb 07 |
jari |
143 |
this.n = n; |
2 |
26 Feb 07 |
jari |
144 |
} |
2 |
26 Feb 07 |
jari |
145 |
|
2 |
26 Feb 07 |
jari |
/** Construct a matrix from a one-dimensional packed array |
2 |
26 Feb 07 |
jari |
* @param vals One-dimensional array of doubles, packed by columns (ala Fortran). |
2 |
26 Feb 07 |
jari |
* @param m Number of rows. |
2 |
26 Feb 07 |
jari |
* @exception IllegalArgumentException Array length must be a multiple of m. |
2 |
26 Feb 07 |
jari |
150 |
*/ |
2 |
26 Feb 07 |
jari |
151 |
|
2 |
26 Feb 07 |
jari |
152 |
public FloatMatrix(float vals[], int m) { |
2 |
26 Feb 07 |
jari |
153 |
this.m = m; |
2 |
26 Feb 07 |
jari |
154 |
n = (m != 0 ? vals.length/m : 0); |
2 |
26 Feb 07 |
jari |
155 |
if (m*n != vals.length) { |
2 |
26 Feb 07 |
jari |
156 |
throw new IllegalArgumentException("Array length must be a multiple of m."); |
2 |
26 Feb 07 |
jari |
157 |
} |
2 |
26 Feb 07 |
jari |
158 |
A = new float[m][n]; |
2 |
26 Feb 07 |
jari |
159 |
for (int i = 0; i < m; i++) { |
2 |
26 Feb 07 |
jari |
160 |
for (int j = 0; j < n; j++) { |
2 |
26 Feb 07 |
jari |
161 |
A[i][j] = vals[i+j*m]; |
2 |
26 Feb 07 |
jari |
162 |
} |
2 |
26 Feb 07 |
jari |
163 |
} |
2 |
26 Feb 07 |
jari |
164 |
} |
2 |
26 Feb 07 |
jari |
165 |
|
2 |
26 Feb 07 |
jari |
166 |
/* ------------------------ |
2 |
26 Feb 07 |
jari |
Public Methods |
2 |
26 Feb 07 |
jari |
168 |
* ------------------------ */ |
2 |
26 Feb 07 |
jari |
169 |
|
2 |
26 Feb 07 |
jari |
170 |
public static String[] getPersistenceDelegateArgs() { |
2 |
26 Feb 07 |
jari |
171 |
return new String[]{"array", "rowDimension", "columnDimension"}; |
2 |
26 Feb 07 |
jari |
172 |
} |
2 |
26 Feb 07 |
jari |
173 |
|
2 |
26 Feb 07 |
jari |
/** Construct a matrix from a copy of a 2-D array. |
2 |
26 Feb 07 |
jari |
* @param A Two-dimensional array of doubles. |
2 |
26 Feb 07 |
jari |
* @exception IllegalArgumentException All rows must have the same length |
2 |
26 Feb 07 |
jari |
177 |
*/ |
2 |
26 Feb 07 |
jari |
178 |
|
2 |
26 Feb 07 |
jari |
179 |
public static FloatMatrix constructWithCopy(float[][] A) { |
2 |
26 Feb 07 |
jari |
180 |
int m = A.length; |
2 |
26 Feb 07 |
jari |
181 |
int n = A[0].length; |
2 |
26 Feb 07 |
jari |
182 |
FloatMatrix X = new FloatMatrix(m,n); |
2 |
26 Feb 07 |
jari |
183 |
float[][] C = X.getArray(); |
2 |
26 Feb 07 |
jari |
184 |
for (int i = 0; i < m; i++) { |
2 |
26 Feb 07 |
jari |
185 |
if (A[i].length != n) { |
2 |
26 Feb 07 |
jari |
186 |
throw new IllegalArgumentException |
2 |
26 Feb 07 |
jari |
187 |
("All rows must have the same length."); |
2 |
26 Feb 07 |
jari |
188 |
} |
2 |
26 Feb 07 |
jari |
189 |
for (int j = 0; j < n; j++) { |
2 |
26 Feb 07 |
jari |
190 |
C[i][j] = A[i][j]; |
2 |
26 Feb 07 |
jari |
191 |
} |
2 |
26 Feb 07 |
jari |
192 |
} |
2 |
26 Feb 07 |
jari |
193 |
return X; |
2 |
26 Feb 07 |
jari |
194 |
} |
2 |
26 Feb 07 |
jari |
195 |
|
2 |
26 Feb 07 |
jari |
/** Make a deep copy of a matrix |
2 |
26 Feb 07 |
jari |
197 |
*/ |
2 |
26 Feb 07 |
jari |
198 |
|
2 |
26 Feb 07 |
jari |
199 |
public FloatMatrix copy() { |
2 |
26 Feb 07 |
jari |
200 |
FloatMatrix X = new FloatMatrix(m,n); |
2 |
26 Feb 07 |
jari |
201 |
float[][] C = X.getArray(); |
2 |
26 Feb 07 |
jari |
202 |
for (int i = 0; i < m; i++) { |
2 |
26 Feb 07 |
jari |
203 |
for (int j = 0; j < n; j++) { |
2 |
26 Feb 07 |
jari |
204 |
C[i][j] = A[i][j]; |
2 |
26 Feb 07 |
jari |
205 |
} |
2 |
26 Feb 07 |
jari |
206 |
} |
2 |
26 Feb 07 |
jari |
207 |
return X; |
2 |
26 Feb 07 |
jari |
208 |
} |
2 |
26 Feb 07 |
jari |
209 |
|
2 |
26 Feb 07 |
jari |
/** Clone the Matrix object. |
2 |
26 Feb 07 |
jari |
211 |
*/ |
2 |
26 Feb 07 |
jari |
212 |
|
2 |
26 Feb 07 |
jari |
213 |
public Object clone() { |
2 |
26 Feb 07 |
jari |
214 |
return this.copy(); |
2 |
26 Feb 07 |
jari |
215 |
} |
2 |
26 Feb 07 |
jari |
216 |
|
2 |
26 Feb 07 |
jari |
/** Access the internal two-dimensional array. |
2 |
26 Feb 07 |
jari |
* @return Pointer to the two-dimensional array of matrix elements. |
2 |
26 Feb 07 |
jari |
219 |
*/ |
2 |
26 Feb 07 |
jari |
220 |
|
2 |
26 Feb 07 |
jari |
221 |
public float[][] getArray() { |
2 |
26 Feb 07 |
jari |
222 |
return A; |
2 |
26 Feb 07 |
jari |
223 |
} |
2 |
26 Feb 07 |
jari |
224 |
|
2 |
26 Feb 07 |
jari |
/** Copy the internal two-dimensional array. |
2 |
26 Feb 07 |
jari |
* @return Two-dimensional array copy of matrix elements. |
2 |
26 Feb 07 |
jari |
227 |
*/ |
2 |
26 Feb 07 |
jari |
228 |
|
2 |
26 Feb 07 |
jari |
229 |
public float[][] getArrayCopy() { |
2 |
26 Feb 07 |
jari |
230 |
float[][] C = new float[m][n]; |
2 |
26 Feb 07 |
jari |
231 |
for (int i = 0; i < m; i++) { |
2 |
26 Feb 07 |
jari |
232 |
for (int j = 0; j < n; j++) { |
2 |
26 Feb 07 |
jari |
233 |
C[i][j] = A[i][j]; |
2 |
26 Feb 07 |
jari |
234 |
} |
2 |
26 Feb 07 |
jari |
235 |
} |
2 |
26 Feb 07 |
jari |
236 |
return C; |
2 |
26 Feb 07 |
jari |
237 |
} |
2 |
26 Feb 07 |
jari |
238 |
|
2 |
26 Feb 07 |
jari |
/** Make a one-dimensional column packed copy of the internal array. |
2 |
26 Feb 07 |
jari |
* @return Matrix elements packed in a one-dimensional array by columns. |
2 |
26 Feb 07 |
jari |
241 |
*/ |
2 |
26 Feb 07 |
jari |
242 |
|
2 |
26 Feb 07 |
jari |
243 |
public float[] getColumnPackedCopy() { |
2 |
26 Feb 07 |
jari |
244 |
float[] vals = new float[m*n]; |
2 |
26 Feb 07 |
jari |
245 |
for (int i = 0; i < m; i++) { |
2 |
26 Feb 07 |
jari |
246 |
for (int j = 0; j < n; j++) { |
2 |
26 Feb 07 |
jari |
247 |
vals[i+j*m] = A[i][j]; |
2 |
26 Feb 07 |
jari |
248 |
} |
2 |
26 Feb 07 |
jari |
249 |
} |
2 |
26 Feb 07 |
jari |
250 |
return vals; |
2 |
26 Feb 07 |
jari |
251 |
} |
2 |
26 Feb 07 |
jari |
252 |
|
2 |
26 Feb 07 |
jari |
/** Make a one-dimensional row packed copy of the internal array. |
2 |
26 Feb 07 |
jari |
* @return Matrix elements packed in a one-dimensional array by rows. |
2 |
26 Feb 07 |
jari |
255 |
*/ |
2 |
26 Feb 07 |
jari |
256 |
|
2 |
26 Feb 07 |
jari |
257 |
public float[] getRowPackedCopy() { |
2 |
26 Feb 07 |
jari |
258 |
float[] vals = new float[m*n]; |
2 |
26 Feb 07 |
jari |
259 |
for (int i = 0; i < m; i++) { |
2 |
26 Feb 07 |
jari |
260 |
for (int j = 0; j < n; j++) { |
2 |
26 Feb 07 |
jari |
261 |
vals[i*n+j] = A[i][j]; |
2 |
26 Feb 07 |
jari |
262 |
} |
2 |
26 Feb 07 |
jari |
263 |
} |
2 |
26 Feb 07 |
jari |
264 |
return vals; |
2 |
26 Feb 07 |
jari |
265 |
} |
2 |
26 Feb 07 |
jari |
266 |
|
2 |
26 Feb 07 |
jari |
/** Get row dimension. |
2 |
26 Feb 07 |
jari |
* @return m, the number of rows. |
2 |
26 Feb 07 |
jari |
269 |
*/ |
2 |
26 Feb 07 |
jari |
270 |
|
2 |
26 Feb 07 |
jari |
271 |
public int getRowDimension() { |
2 |
26 Feb 07 |
jari |
272 |
return m; |
2 |
26 Feb 07 |
jari |
273 |
} |
2 |
26 Feb 07 |
jari |
274 |
|
2 |
26 Feb 07 |
jari |
/** Get column dimension. |
2 |
26 Feb 07 |
jari |
* @return n, the number of columns. |
2 |
26 Feb 07 |
jari |
277 |
*/ |
2 |
26 Feb 07 |
jari |
278 |
|
2 |
26 Feb 07 |
jari |
279 |
public int getColumnDimension() { |
2 |
26 Feb 07 |
jari |
280 |
return n; |
2 |
26 Feb 07 |
jari |
281 |
} |
2 |
26 Feb 07 |
jari |
282 |
|
2 |
26 Feb 07 |
jari |
/** Get a single element. |
2 |
26 Feb 07 |
jari |
* @param i Row index. |
2 |
26 Feb 07 |
jari |
* @param j Column index. |
2 |
26 Feb 07 |
jari |
* @return A(i,j) |
2 |
26 Feb 07 |
jari |
* @exception ArrayIndexOutOfBoundsException |
2 |
26 Feb 07 |
jari |
288 |
*/ |
2 |
26 Feb 07 |
jari |
289 |
|
2 |
26 Feb 07 |
jari |
290 |
public float get(int i, int j) { |
2 |
26 Feb 07 |
jari |
291 |
return A[i][j]; |
2 |
26 Feb 07 |
jari |
292 |
} |
2 |
26 Feb 07 |
jari |
293 |
|
2 |
26 Feb 07 |
jari |
/** Get a submatrix. |
2 |
26 Feb 07 |
jari |
* @param i0 Initial row index |
2 |
26 Feb 07 |
jari |
* @param i1 Final row index |
2 |
26 Feb 07 |
jari |
* @param j0 Initial column index |
2 |
26 Feb 07 |
jari |
* @param j1 Final column index |
2 |
26 Feb 07 |
jari |
* @return A(i0:i1,j0:j1) |
2 |
26 Feb 07 |
jari |
* @exception ArrayIndexOutOfBoundsException Submatrix indices |
2 |
26 Feb 07 |
jari |
301 |
*/ |
2 |
26 Feb 07 |
jari |
302 |
|
2 |
26 Feb 07 |
jari |
303 |
public FloatMatrix getMatrix(int i0, int i1, int j0, int j1) { |
2 |
26 Feb 07 |
jari |
304 |
FloatMatrix X = new FloatMatrix(i1-i0+1,j1-j0+1); |
2 |
26 Feb 07 |
jari |
305 |
float[][] B = X.getArray(); |
2 |
26 Feb 07 |
jari |
306 |
try { |
2 |
26 Feb 07 |
jari |
307 |
for (int i = i0; i <= i1; i++) { |
2 |
26 Feb 07 |
jari |
308 |
for (int j = j0; j <= j1; j++) { |
2 |
26 Feb 07 |
jari |
309 |
B[i-i0][j-j0] = A[i][j]; |
2 |
26 Feb 07 |
jari |
310 |
} |
2 |
26 Feb 07 |
jari |
311 |
} |
2 |
26 Feb 07 |
jari |
312 |
} catch (ArrayIndexOutOfBoundsException e) { |
2 |
26 Feb 07 |
jari |
313 |
throw new ArrayIndexOutOfBoundsException("Submatrix indices"); |
2 |
26 Feb 07 |
jari |
314 |
} |
2 |
26 Feb 07 |
jari |
315 |
return X; |
2 |
26 Feb 07 |
jari |
316 |
} |
2 |
26 Feb 07 |
jari |
317 |
|
2 |
26 Feb 07 |
jari |
/** Get a submatrix. |
2 |
26 Feb 07 |
jari |
* @param r Array of row indices. |
2 |
26 Feb 07 |
jari |
* @param c Array of column indices. |
2 |
26 Feb 07 |
jari |
* @return A(r(:),c(:)) |
2 |
26 Feb 07 |
jari |
* @exception ArrayIndexOutOfBoundsException Submatrix indices |
2 |
26 Feb 07 |
jari |
323 |
*/ |
2 |
26 Feb 07 |
jari |
324 |
|
2 |
26 Feb 07 |
jari |
325 |
public FloatMatrix getMatrix(int[] r, int[] c) { |
2 |
26 Feb 07 |
jari |
326 |
FloatMatrix X = new FloatMatrix(r.length,c.length); |
2 |
26 Feb 07 |
jari |
327 |
float[][] B = X.getArray(); |
2 |
26 Feb 07 |
jari |
328 |
try { |
2 |
26 Feb 07 |
jari |
329 |
for (int i = 0; i < r.length; i++) { |
2 |
26 Feb 07 |
jari |
330 |
for (int j = 0; j < c.length; j++) { |
2 |
26 Feb 07 |
jari |
331 |
B[i][j] = A[r[i]][c[j]]; |
2 |
26 Feb 07 |
jari |
332 |
} |
2 |
26 Feb 07 |
jari |
333 |
} |
2 |
26 Feb 07 |
jari |
334 |
} catch (ArrayIndexOutOfBoundsException e) { |
2 |
26 Feb 07 |
jari |
335 |
throw new ArrayIndexOutOfBoundsException("Submatrix indices"); |
2 |
26 Feb 07 |
jari |
336 |
} |
2 |
26 Feb 07 |
jari |
337 |
return X; |
2 |
26 Feb 07 |
jari |
338 |
} |
2 |
26 Feb 07 |
jari |
339 |
|
2 |
26 Feb 07 |
jari |
/** Get a submatrix. |
2 |
26 Feb 07 |
jari |
* @param i0 Initial row index |
2 |
26 Feb 07 |
jari |
* @param i1 Final row index |
2 |
26 Feb 07 |
jari |
* @param c Array of column indices. |
2 |
26 Feb 07 |
jari |
* @return A(i0:i1,c(:)) |
2 |
26 Feb 07 |
jari |
* @exception ArrayIndexOutOfBoundsException Submatrix indices |
2 |
26 Feb 07 |
jari |
346 |
*/ |
2 |
26 Feb 07 |
jari |
347 |
|
2 |
26 Feb 07 |
jari |
348 |
public FloatMatrix getMatrix(int i0, int i1, int[] c) { |
2 |
26 Feb 07 |
jari |
349 |
FloatMatrix X = new FloatMatrix(i1-i0+1,c.length); |
2 |
26 Feb 07 |
jari |
350 |
float[][] B = X.getArray(); |
2 |
26 Feb 07 |
jari |
351 |
try { |
2 |
26 Feb 07 |
jari |
352 |
for (int i = i0; i <= i1; i++) { |
2 |
26 Feb 07 |
jari |
353 |
for (int j = 0; j < c.length; j++) { |
2 |
26 Feb 07 |
jari |
354 |
B[i-i0][j] = A[i][c[j]]; |
2 |
26 Feb 07 |
jari |
355 |
} |
2 |
26 Feb 07 |
jari |
356 |
} |
2 |
26 Feb 07 |
jari |
357 |
} catch (ArrayIndexOutOfBoundsException e) { |
2 |
26 Feb 07 |
jari |
358 |
throw new ArrayIndexOutOfBoundsException("Submatrix indices"); |
2 |
26 Feb 07 |
jari |
359 |
} |
2 |
26 Feb 07 |
jari |
360 |
return X; |
2 |
26 Feb 07 |
jari |
361 |
} |
2 |
26 Feb 07 |
jari |
362 |
|
2 |
26 Feb 07 |
jari |
/** Get a submatrix. |
2 |
26 Feb 07 |
jari |
* @param r Array of row indices. |
2 |
26 Feb 07 |
jari |
* @param i0 Initial column index |
2 |
26 Feb 07 |
jari |
* @param i1 Final column index |
2 |
26 Feb 07 |
jari |
* @return A(r(:),j0:j1) |
2 |
26 Feb 07 |
jari |
* @exception ArrayIndexOutOfBoundsException Submatrix indices |
2 |
26 Feb 07 |
jari |
369 |
*/ |
2 |
26 Feb 07 |
jari |
370 |
|
2 |
26 Feb 07 |
jari |
371 |
public FloatMatrix getMatrix(int[] r, int j0, int j1) { |
2 |
26 Feb 07 |
jari |
372 |
FloatMatrix X = new FloatMatrix(r.length,j1-j0+1); |
2 |
26 Feb 07 |
jari |
373 |
float[][] B = X.getArray(); |
2 |
26 Feb 07 |
jari |
374 |
try { |
2 |
26 Feb 07 |
jari |
375 |
for (int i = 0; i < r.length; i++) { |
2 |
26 Feb 07 |
jari |
376 |
for (int j = j0; j <= j1; j++) { |
2 |
26 Feb 07 |
jari |
377 |
B[i][j-j0] = A[r[i]][j]; |
2 |
26 Feb 07 |
jari |
378 |
} |
2 |
26 Feb 07 |
jari |
379 |
} |
2 |
26 Feb 07 |
jari |
380 |
} catch (ArrayIndexOutOfBoundsException e) { |
2 |
26 Feb 07 |
jari |
381 |
throw new ArrayIndexOutOfBoundsException("Submatrix indices"); |
2 |
26 Feb 07 |
jari |
382 |
} |
2 |
26 Feb 07 |
jari |
383 |
return X; |
2 |
26 Feb 07 |
jari |
384 |
} |
2 |
26 Feb 07 |
jari |
385 |
|
2 |
26 Feb 07 |
jari |
/** Set a single element. |
2 |
26 Feb 07 |
jari |
* @param i Row index. |
2 |
26 Feb 07 |
jari |
* @param j Column index. |
2 |
26 Feb 07 |
jari |
* @param s A(i,j). |
2 |
26 Feb 07 |
jari |
* @exception ArrayIndexOutOfBoundsException |
2 |
26 Feb 07 |
jari |
391 |
*/ |
2 |
26 Feb 07 |
jari |
392 |
|
2 |
26 Feb 07 |
jari |
393 |
public void set(int i, int j, float s) { |
2 |
26 Feb 07 |
jari |
394 |
A[i][j] = s; |
2 |
26 Feb 07 |
jari |
395 |
} |
2 |
26 Feb 07 |
jari |
396 |
|
2 |
26 Feb 07 |
jari |
/** Set a submatrix. |
2 |
26 Feb 07 |
jari |
* @param i0 Initial row index |
2 |
26 Feb 07 |
jari |
* @param i1 Final row index |
2 |
26 Feb 07 |
jari |
* @param j0 Initial column index |
2 |
26 Feb 07 |
jari |
* @param j1 Final column index |
2 |
26 Feb 07 |
jari |
* @param X A(i0:i1,j0:j1) |
2 |
26 Feb 07 |
jari |
* @exception ArrayIndexOutOfBoundsException Submatrix indices |
2 |
26 Feb 07 |
jari |
404 |
*/ |
2 |
26 Feb 07 |
jari |
405 |
|
2 |
26 Feb 07 |
jari |
406 |
public void setMatrix(int i0, int i1, int j0, int j1, FloatMatrix X) { |
2 |
26 Feb 07 |
jari |
407 |
try { |
2 |
26 Feb 07 |
jari |
408 |
for (int i = i0; i <= i1; i++) { |
2 |
26 Feb 07 |
jari |
409 |
for (int j = j0; j <= j1; j++) { |
2 |
26 Feb 07 |
jari |
410 |
A[i][j] = X.get(i-i0,j-j0); |
2 |
26 Feb 07 |
jari |
411 |
} |
2 |
26 Feb 07 |
jari |
412 |
} |
2 |
26 Feb 07 |
jari |
413 |
} catch (ArrayIndexOutOfBoundsException e) { |
2 |
26 Feb 07 |
jari |
414 |
throw new ArrayIndexOutOfBoundsException("Submatrix indices"); |
2 |
26 Feb 07 |
jari |
415 |
} |
2 |
26 Feb 07 |
jari |
416 |
} |
2 |
26 Feb 07 |
jari |
417 |
|
2 |
26 Feb 07 |
jari |
/** Set a submatrix. |
2 |
26 Feb 07 |
jari |
* @param r Array of row indices. |
2 |
26 Feb 07 |
jari |
* @param c Array of column indices. |
2 |
26 Feb 07 |
jari |
* @param X A(r(:),c(:)) |
2 |
26 Feb 07 |
jari |
* @exception ArrayIndexOutOfBoundsException Submatrix indices |
2 |
26 Feb 07 |
jari |
423 |
*/ |
2 |
26 Feb 07 |
jari |
424 |
|
2 |
26 Feb 07 |
jari |
425 |
public void setMatrix(int[] r, int[] c, FloatMatrix X) { |
2 |
26 Feb 07 |
jari |
426 |
try { |
2 |
26 Feb 07 |
jari |
427 |
for (int i = 0; i < r.length; i++) { |
2 |
26 Feb 07 |
jari |
428 |
for (int j = 0; j < c.length; j++) { |
2 |
26 Feb 07 |
jari |
429 |
A[r[i]][c[j]] = X.get(i,j); |
2 |
26 Feb 07 |
jari |
430 |
} |
2 |
26 Feb 07 |
jari |
431 |
} |
2 |
26 Feb 07 |
jari |
432 |
} catch (ArrayIndexOutOfBoundsException e) { |
2 |
26 Feb 07 |
jari |
433 |
throw new ArrayIndexOutOfBoundsException("Submatrix indices"); |
2 |
26 Feb 07 |
jari |
434 |
} |
2 |
26 Feb 07 |
jari |
435 |
} |
2 |
26 Feb 07 |
jari |
436 |
|
2 |
26 Feb 07 |
jari |
/** Set a submatrix. |
2 |
26 Feb 07 |
jari |
* @param r Array of row indices. |
2 |
26 Feb 07 |
jari |
* @param j0 Initial column index |
2 |
26 Feb 07 |
jari |
* @param j1 Final column index |
2 |
26 Feb 07 |
jari |
* @param X A(r(:),j0:j1) |
2 |
26 Feb 07 |
jari |
* @exception ArrayIndexOutOfBoundsException Submatrix indices |
2 |
26 Feb 07 |
jari |
443 |
*/ |
2 |
26 Feb 07 |
jari |
444 |
|
2 |
26 Feb 07 |
jari |
445 |
public void setMatrix(int[] r, int j0, int j1, FloatMatrix X) { |
2 |
26 Feb 07 |
jari |
446 |
try { |
2 |
26 Feb 07 |
jari |
447 |
for (int i = 0; i < r.length; i++) { |
2 |
26 Feb 07 |
jari |
448 |
for (int j = j0; j <= j1; j++) { |
2 |
26 Feb 07 |
jari |
449 |
A[r[i]][j] = X.get(i,j-j0); |
2 |
26 Feb 07 |
jari |
450 |
} |
2 |
26 Feb 07 |
jari |
451 |
} |
2 |
26 Feb 07 |
jari |
452 |
} catch (ArrayIndexOutOfBoundsException e) { |
2 |
26 Feb 07 |
jari |
453 |
throw new ArrayIndexOutOfBoundsException("Submatrix indices"); |
2 |
26 Feb 07 |
jari |
454 |
} |
2 |
26 Feb 07 |
jari |
455 |
} |
2 |
26 Feb 07 |
jari |
456 |
|
2 |
26 Feb 07 |
jari |
/** Set a submatrix. |
2 |
26 Feb 07 |
jari |
* @param i0 Initial row index |
2 |
26 Feb 07 |
jari |
* @param i1 Final row index |
2 |
26 Feb 07 |
jari |
* @param c Array of column indices. |
2 |
26 Feb 07 |
jari |
* @param X A(i0:i1,c(:)) |
2 |
26 Feb 07 |
jari |
* @exception ArrayIndexOutOfBoundsException Submatrix indices |
2 |
26 Feb 07 |
jari |
463 |
*/ |
2 |
26 Feb 07 |
jari |
464 |
|
2 |
26 Feb 07 |
jari |
465 |
public void setMatrix(int i0, int i1, int[] c, FloatMatrix X) { |
2 |
26 Feb 07 |
jari |
466 |
try { |
2 |
26 Feb 07 |
jari |
467 |
for (int i = i0; i <= i1; i++) { |
2 |
26 Feb 07 |
jari |
468 |
for (int j = 0; j < c.length; j++) { |
2 |
26 Feb 07 |
jari |
469 |
A[i][c[j]] = X.get(i-i0,j); |
2 |
26 Feb 07 |
jari |
470 |
} |
2 |
26 Feb 07 |
jari |
471 |
} |
2 |
26 Feb 07 |
jari |
472 |
} catch (ArrayIndexOutOfBoundsException e) { |
2 |
26 Feb 07 |
jari |
473 |
throw new ArrayIndexOutOfBoundsException("Submatrix indices"); |
2 |
26 Feb 07 |
jari |
474 |
} |
2 |
26 Feb 07 |
jari |
475 |
} |
2 |
26 Feb 07 |
jari |
476 |
|
2 |
26 Feb 07 |
jari |
/** Matrix transpose. |
2 |
26 Feb 07 |
jari |
* @return A' |
2 |
26 Feb 07 |
jari |
479 |
*/ |
2 |
26 Feb 07 |
jari |
480 |
|
2 |
26 Feb 07 |
jari |
481 |
public FloatMatrix transpose() { |
2 |
26 Feb 07 |
jari |
482 |
FloatMatrix X = new FloatMatrix(n,m); |
2 |
26 Feb 07 |
jari |
483 |
float[][] C = X.getArray(); |
2 |
26 Feb 07 |
jari |
484 |
for (int i = 0; i < m; i++) { |
2 |
26 Feb 07 |
jari |
485 |
for (int j = 0; j < n; j++) { |
2 |
26 Feb 07 |
jari |
486 |
C[j][i] = A[i][j]; |
2 |
26 Feb 07 |
jari |
487 |
} |
2 |
26 Feb 07 |
jari |
488 |
} |
2 |
26 Feb 07 |
jari |
489 |
return X; |
2 |
26 Feb 07 |
jari |
490 |
} |
2 |
26 Feb 07 |
jari |
491 |
|
2 |
26 Feb 07 |
jari |
/** One norm |
2 |
26 Feb 07 |
jari |
* @return maximum column sum. |
2 |
26 Feb 07 |
jari |
494 |
*/ |
2 |
26 Feb 07 |
jari |
495 |
|
2 |
26 Feb 07 |
jari |
496 |
public float norm1() { |
2 |
26 Feb 07 |
jari |
497 |
float f = 0; |
2 |
26 Feb 07 |
jari |
498 |
for (int j = 0; j < n; j++) { |
2 |
26 Feb 07 |
jari |
499 |
float s = 0; |
2 |
26 Feb 07 |
jari |
500 |
for (int i = 0; i < m; i++) { |
2 |
26 Feb 07 |
jari |
501 |
s += Math.abs(A[i][j]); |
2 |
26 Feb 07 |
jari |
502 |
} |
2 |
26 Feb 07 |
jari |
503 |
f = Math.max(f,s); |
2 |
26 Feb 07 |
jari |
504 |
} |
2 |
26 Feb 07 |
jari |
505 |
return f; |
2 |
26 Feb 07 |
jari |
506 |
} |
2 |
26 Feb 07 |
jari |
507 |
|
2 |
26 Feb 07 |
jari |
/** Two norm |
2 |
26 Feb 07 |
jari |
* @return maximum singular value. |
2 |
26 Feb 07 |
jari |
510 |
*/ |
2 |
26 Feb 07 |
jari |
511 |
|
2 |
26 Feb 07 |
jari |
/* public float norm2 () { |
2 |
26 Feb 07 |
jari |
return (new SingularValueDecomposition(this).norm2()); |
2 |
26 Feb 07 |
jari |
514 |
}*/ |
2 |
26 Feb 07 |
jari |
515 |
|
2 |
26 Feb 07 |
jari |
/** Infinity norm |
2 |
26 Feb 07 |
jari |
* @return maximum row sum. |
2 |
26 Feb 07 |
jari |
518 |
*/ |
2 |
26 Feb 07 |
jari |
519 |
|
2 |
26 Feb 07 |
jari |
520 |
public float normInf() { |
2 |
26 Feb 07 |
jari |
521 |
float f = 0; |
2 |
26 Feb 07 |
jari |
522 |
for (int i = 0; i < m; i++) { |
2 |
26 Feb 07 |
jari |
523 |
float s = 0; |
2 |
26 Feb 07 |
jari |
524 |
for (int j = 0; j < n; j++) { |
2 |
26 Feb 07 |
jari |
525 |
s += Math.abs(A[i][j]); |
2 |
26 Feb 07 |
jari |
526 |
} |
2 |
26 Feb 07 |
jari |
527 |
f = Math.max(f,s); |
2 |
26 Feb 07 |
jari |
528 |
} |
2 |
26 Feb 07 |
jari |
529 |
return f; |
2 |
26 Feb 07 |
jari |
530 |
} |
2 |
26 Feb 07 |
jari |
531 |
|
2 |
26 Feb 07 |
jari |
/** Frobenius norm |
2 |
26 Feb 07 |
jari |
* @return sqrt of sum of squares of all elements. |
2 |
26 Feb 07 |
jari |
534 |
*/ |
2 |
26 Feb 07 |
jari |
535 |
|
2 |
26 Feb 07 |
jari |
536 |
public float normF() { |
2 |
26 Feb 07 |
jari |
537 |
float f = 0; |
2 |
26 Feb 07 |
jari |
538 |
for (int i = 0; i < m; i++) { |
2 |
26 Feb 07 |
jari |
539 |
for (int j = 0; j < n; j++) { |
2 |
26 Feb 07 |
jari |
540 |
f = (float)Maths.hypot(f,A[i][j]); |
2 |
26 Feb 07 |
jari |
541 |
} |
2 |
26 Feb 07 |
jari |
542 |
} |
2 |
26 Feb 07 |
jari |
543 |
return f; |
2 |
26 Feb 07 |
jari |
544 |
} |
2 |
26 Feb 07 |
jari |
545 |
|
2 |
26 Feb 07 |
jari |
/** Unary minus |
2 |
26 Feb 07 |
jari |
* @return -A |
2 |
26 Feb 07 |
jari |
548 |
*/ |
2 |
26 Feb 07 |
jari |
549 |
|
2 |
26 Feb 07 |
jari |
550 |
public FloatMatrix uminus() { |
2 |
26 Feb 07 |
jari |
551 |
FloatMatrix X = new FloatMatrix(m,n); |
2 |
26 Feb 07 |
jari |
552 |
float[][] C = X.getArray(); |
2 |
26 Feb 07 |
jari |
553 |
for (int i = 0; i < m; i++) { |
2 |
26 Feb 07 |
jari |
554 |
for (int j = 0; j < n; j++) { |
2 |
26 Feb 07 |
jari |
555 |
C[i][j] = -A[i][j]; |
2 |
26 Feb 07 |
jari |
556 |
} |
2 |
26 Feb 07 |
jari |
557 |
} |
2 |
26 Feb 07 |
jari |
558 |
return X; |
2 |
26 Feb 07 |
jari |
559 |
} |
2 |
26 Feb 07 |
jari |
560 |
|
2 |
26 Feb 07 |
jari |
/** C = A + B |
2 |
26 Feb 07 |
jari |
* @param B another matrix |
2 |
26 Feb 07 |
jari |
* @return A + B |
2 |
26 Feb 07 |
jari |
564 |
*/ |
2 |
26 Feb 07 |
jari |
565 |
|
2 |
26 Feb 07 |
jari |
566 |
public FloatMatrix plus(FloatMatrix B) { |
2 |
26 Feb 07 |
jari |
567 |
checkMatrixDimensions(B); |
2 |
26 Feb 07 |
jari |
568 |
FloatMatrix X = new FloatMatrix(m,n); |
2 |
26 Feb 07 |
jari |
569 |
float[][] C = X.getArray(); |
2 |
26 Feb 07 |
jari |
570 |
for (int i = 0; i < m; i++) { |
2 |
26 Feb 07 |
jari |
571 |
for (int j = 0; j < n; j++) { |
2 |
26 Feb 07 |
jari |
572 |
C[i][j] = A[i][j] + B.A[i][j]; |
2 |
26 Feb 07 |
jari |
573 |
} |
2 |
26 Feb 07 |
jari |
574 |
} |
2 |
26 Feb 07 |
jari |
575 |
return X; |
2 |
26 Feb 07 |
jari |
576 |
} |
2 |
26 Feb 07 |
jari |
577 |
|
2 |
26 Feb 07 |
jari |
/** A = A + B |
2 |
26 Feb 07 |
jari |
* @param B another matrix |
2 |
26 Feb 07 |
jari |
* @return A + B |
2 |
26 Feb 07 |
jari |
581 |
*/ |
2 |
26 Feb 07 |
jari |
582 |
|
2 |
26 Feb 07 |
jari |
583 |
public FloatMatrix plusEquals(FloatMatrix B) { |
2 |
26 Feb 07 |
jari |
584 |
checkMatrixDimensions(B); |
2 |
26 Feb 07 |
jari |
585 |
for (int i = 0; i < m; i++) { |
2 |
26 Feb 07 |
jari |
586 |
for (int j = 0; j < n; j++) { |
2 |
26 Feb 07 |
jari |
587 |
A[i][j] = A[i][j] + B.A[i][j]; |
2 |
26 Feb 07 |
jari |
588 |
} |
2 |
26 Feb 07 |
jari |
589 |
} |
2 |
26 Feb 07 |
jari |
590 |
return this; |
2 |
26 Feb 07 |
jari |
591 |
} |
2 |
26 Feb 07 |
jari |
592 |
|
2 |
26 Feb 07 |
jari |
/** C = A - B |
2 |
26 Feb 07 |
jari |
* @param B another matrix |
2 |
26 Feb 07 |
jari |
* @return A - B |
2 |
26 Feb 07 |
jari |
596 |
*/ |
2 |
26 Feb 07 |
jari |
597 |
|
2 |
26 Feb 07 |
jari |
598 |
public FloatMatrix minus(FloatMatrix B) { |
2 |
26 Feb 07 |
jari |
599 |
checkMatrixDimensions(B); |
2 |
26 Feb 07 |
jari |
600 |
FloatMatrix X = new FloatMatrix(m,n); |
2 |
26 Feb 07 |
jari |
601 |
float[][] C = X.getArray(); |
2 |
26 Feb 07 |
jari |
602 |
for (int i = 0; i < m; i++) { |
2 |
26 Feb 07 |
jari |
603 |
for (int j = 0; j < n; j++) { |
2 |
26 Feb 07 |
jari |
604 |
C[i][j] = A[i][j] - B.A[i][j]; |
2 |
26 Feb 07 |
jari |
605 |
} |
2 |
26 Feb 07 |
jari |
606 |
} |
2 |
26 Feb 07 |
jari |
607 |
return X; |
2 |
26 Feb 07 |
jari |
608 |
} |
2 |
26 Feb 07 |
jari |
609 |
|
2 |
26 Feb 07 |
jari |
/** A = A - B |
2 |
26 Feb 07 |
jari |
* @param B another matrix |
2 |
26 Feb 07 |
jari |
* @return A - B |
2 |
26 Feb 07 |
jari |
613 |
*/ |
2 |
26 Feb 07 |
jari |
614 |
|
2 |
26 Feb 07 |
jari |
615 |
public FloatMatrix minusEquals(FloatMatrix B) { |
2 |
26 Feb 07 |
jari |
616 |
checkMatrixDimensions(B); |
2 |
26 Feb 07 |
jari |
617 |
for (int i = 0; i < m; i++) { |
2 |
26 Feb 07 |
jari |
618 |
for (int j = 0; j < n; j++) { |
2 |
26 Feb 07 |
jari |
619 |
A[i][j] = A[i][j] - B.A[i][j]; |
2 |
26 Feb 07 |
jari |
620 |
} |
2 |
26 Feb 07 |
jari |
621 |
} |
2 |
26 Feb 07 |
jari |
622 |
return this; |
2 |
26 Feb 07 |
jari |
623 |
} |
2 |
26 Feb 07 |
jari |
624 |
|
2 |
26 Feb 07 |
jari |
/** Element-by-element multiplication, C = A.*B |
2 |
26 Feb 07 |
jari |
* @param B another matrix |
2 |
26 Feb 07 |
jari |
* @return A.*B |
2 |
26 Feb 07 |
jari |
628 |
*/ |
2 |
26 Feb 07 |
jari |
629 |
|
2 |
26 Feb 07 |
jari |
630 |
public FloatMatrix arrayTimes(FloatMatrix B) { |
2 |
26 Feb 07 |
jari |
631 |
checkMatrixDimensions(B); |
2 |
26 Feb 07 |
jari |
632 |
FloatMatrix X = new FloatMatrix(m,n); |
2 |
26 Feb 07 |
jari |
633 |
float[][] C = X.getArray(); |
2 |
26 Feb 07 |
jari |
634 |
for (int i = 0; i < m; i++) { |
2 |
26 Feb 07 |
jari |
635 |
for (int j = 0; j < n; j++) { |
2 |
26 Feb 07 |
jari |
636 |
C[i][j] = A[i][j] * B.A[i][j]; |
2 |
26 Feb 07 |
jari |
637 |
} |
2 |
26 Feb 07 |
jari |
638 |
} |
2 |
26 Feb 07 |
jari |
639 |
return X; |
2 |
26 Feb 07 |
jari |
640 |
} |
2 |
26 Feb 07 |
jari |
641 |
|
2 |
26 Feb 07 |
jari |
/** Element-by-element multiplication in place, A = A.*B |
2 |
26 Feb 07 |
jari |
* @param B another matrix |
2 |
26 Feb 07 |
jari |
* @return A.*B |
2 |
26 Feb 07 |
jari |
645 |
*/ |
2 |
26 Feb 07 |
jari |
646 |
|
2 |
26 Feb 07 |
jari |
647 |
public FloatMatrix arrayTimesEquals(FloatMatrix B) { |
2 |
26 Feb 07 |
jari |
648 |
checkMatrixDimensions(B); |
2 |
26 Feb 07 |
jari |
649 |
for (int i = 0; i < m; i++) { |
2 |
26 Feb 07 |
jari |
650 |
for (int j = 0; j < n; j++) { |
2 |
26 Feb 07 |
jari |
651 |
A[i][j] = A[i][j] * B.A[i][j]; |
2 |
26 Feb 07 |
jari |
652 |
} |
2 |
26 Feb 07 |
jari |
653 |
} |
2 |
26 Feb 07 |
jari |
654 |
return this; |
2 |
26 Feb 07 |
jari |
655 |
} |
2 |
26 Feb 07 |
jari |
656 |
|
2 |
26 Feb 07 |
jari |
/** Element-by-element right division, C = A./B |
2 |
26 Feb 07 |
jari |
* @param B another matrix |
2 |
26 Feb 07 |
jari |
* @return A./B |
2 |
26 Feb 07 |
jari |
660 |
*/ |
2 |
26 Feb 07 |
jari |
661 |
|
2 |
26 Feb 07 |
jari |
662 |
public FloatMatrix arrayRightDivide(FloatMatrix B) { |
2 |
26 Feb 07 |
jari |
663 |
checkMatrixDimensions(B); |
2 |
26 Feb 07 |
jari |
664 |
FloatMatrix X = new FloatMatrix(m,n); |
2 |
26 Feb 07 |
jari |
665 |
float[][] C = X.getArray(); |
2 |
26 Feb 07 |
jari |
666 |
for (int i = 0; i < m; i++) { |
2 |
26 Feb 07 |
jari |
667 |
for (int j = 0; j < n; j++) { |
2 |
26 Feb 07 |
jari |
668 |
C[i][j] = A[i][j] / B.A[i][j]; |
2 |
26 Feb 07 |
jari |
669 |
} |
2 |
26 Feb 07 |
jari |
670 |
} |
2 |
26 Feb 07 |
jari |
671 |
return X; |
2 |
26 Feb 07 |
jari |
672 |
} |
2 |
26 Feb 07 |
jari |
673 |
|
2 |
26 Feb 07 |
jari |
/** Element-by-element right division in place, A = A./B |
2 |
26 Feb 07 |
jari |
* @param B another matrix |
2 |
26 Feb 07 |
jari |
* @return A./B |
2 |
26 Feb 07 |
jari |
677 |
*/ |
2 |
26 Feb 07 |
jari |
678 |
|
2 |
26 Feb 07 |
jari |
679 |
public FloatMatrix arrayRightDivideEquals(FloatMatrix B) { |
2 |
26 Feb 07 |
jari |
680 |
checkMatrixDimensions(B); |
2 |
26 Feb 07 |
jari |
681 |
for (int i = 0; i < m; i++) { |
2 |
26 Feb 07 |
jari |
682 |
for (int j = 0; j < n; j++) { |
2 |
26 Feb 07 |
jari |
683 |
A[i][j] = A[i][j] / B.A[i][j]; |
2 |
26 Feb 07 |
jari |
684 |
} |
2 |
26 Feb 07 |
jari |
685 |
} |
2 |
26 Feb 07 |
jari |
686 |
return this; |
2 |
26 Feb 07 |
jari |
687 |
} |
2 |
26 Feb 07 |
jari |
688 |
|
2 |
26 Feb 07 |
jari |
/** Element-by-element left division, C = A.\B |
2 |
26 Feb 07 |
jari |
* @param B another matrix |
2 |
26 Feb 07 |
jari |
* @return A.\B |
2 |
26 Feb 07 |
jari |
692 |
*/ |
2 |
26 Feb 07 |
jari |
693 |
|
2 |
26 Feb 07 |
jari |
694 |
public FloatMatrix arrayLeftDivide(FloatMatrix B) { |
2 |
26 Feb 07 |
jari |
695 |
checkMatrixDimensions(B); |
2 |
26 Feb 07 |
jari |
696 |
FloatMatrix X = new FloatMatrix(m,n); |
2 |
26 Feb 07 |
jari |
697 |
float[][] C = X.getArray(); |
2 |
26 Feb 07 |
jari |
698 |
for (int i = 0; i < m; i++) { |
2 |
26 Feb 07 |
jari |
699 |
for (int j = 0; j < n; j++) { |
2 |
26 Feb 07 |
jari |
700 |
C[i][j] = B.A[i][j] / A[i][j]; |
2 |
26 Feb 07 |
jari |
701 |
} |
2 |
26 Feb 07 |
jari |
702 |
} |
2 |
26 Feb 07 |
jari |
703 |
return X; |
2 |
26 Feb 07 |
jari |
704 |
} |
2 |
26 Feb 07 |
jari |
705 |
|
2 |
26 Feb 07 |
jari |
/** Element-by-element left division in place, A = A.\B |
2 |
26 Feb 07 |
jari |
* @param B another matrix |
2 |
26 Feb 07 |
jari |
* @return A.\B |
2 |
26 Feb 07 |
jari |
709 |
*/ |
2 |
26 Feb 07 |
jari |
710 |
|
2 |
26 Feb 07 |
jari |
711 |
public FloatMatrix arrayLeftDivideEquals(FloatMatrix B) { |
2 |
26 Feb 07 |
jari |
712 |
checkMatrixDimensions(B); |
2 |
26 Feb 07 |
jari |
713 |
for (int i = 0; i < m; i++) { |
2 |
26 Feb 07 |
jari |
714 |
for (int j = 0; j < n; j++) { |
2 |
26 Feb 07 |
jari |
715 |
A[i][j] = B.A[i][j] / A[i][j]; |
2 |
26 Feb 07 |
jari |
716 |
} |
2 |
26 Feb 07 |
jari |
717 |
} |
2 |
26 Feb 07 |
jari |
718 |
return this; |
2 |
26 Feb 07 |
jari |
719 |
} |
2 |
26 Feb 07 |
jari |
720 |
|
2 |
26 Feb 07 |
jari |
/** Multiply a matrix by a scalar, C = s*A |
2 |
26 Feb 07 |
jari |
* @param s scalar |
2 |
26 Feb 07 |
jari |
* @return s*A |
2 |
26 Feb 07 |
jari |
724 |
*/ |
2 |
26 Feb 07 |
jari |
725 |
|
2 |
26 Feb 07 |
jari |
726 |
public FloatMatrix times(float s) { |
2 |
26 Feb 07 |
jari |
727 |
FloatMatrix X = new FloatMatrix(m,n); |
2 |
26 Feb 07 |
jari |
728 |
float[][] C = X.getArray(); |
2 |
26 Feb 07 |
jari |
729 |
for (int i = 0; i < m; i++) { |
2 |
26 Feb 07 |
jari |
730 |
for (int j = 0; j < n; j++) { |
2 |
26 Feb 07 |
jari |
731 |
C[i][j] = s*A[i][j]; |
2 |
26 Feb 07 |
jari |
732 |
} |
2 |
26 Feb 07 |
jari |
733 |
} |
2 |
26 Feb 07 |
jari |
734 |
return X; |
2 |
26 Feb 07 |
jari |
735 |
} |
2 |
26 Feb 07 |
jari |
736 |
|
2 |
26 Feb 07 |
jari |
/** Multiply a matrix by a scalar in place, A = s*A |
2 |
26 Feb 07 |
jari |
* @param s scalar |
2 |
26 Feb 07 |
jari |
* @return replace A by s*A |
2 |
26 Feb 07 |
jari |
740 |
*/ |
2 |
26 Feb 07 |
jari |
741 |
|
2 |
26 Feb 07 |
jari |
742 |
public FloatMatrix timesEquals(float s) { |
2 |
26 Feb 07 |
jari |
743 |
for (int i = 0; i < m; i++) { |
2 |
26 Feb 07 |
jari |
744 |
for (int j = 0; j < n; j++) { |
2 |
26 Feb 07 |
jari |
745 |
A[i][j] = s*A[i][j]; |
2 |
26 Feb 07 |
jari |
746 |
} |
2 |
26 Feb 07 |
jari |
747 |
} |
2 |
26 Feb 07 |
jari |
748 |
return this; |
2 |
26 Feb 07 |
jari |
749 |
} |
2 |
26 Feb 07 |
jari |
750 |
|
2 |
26 Feb 07 |
jari |
/** Linear algebraic matrix multiplication, A * B |
2 |
26 Feb 07 |
jari |
* @param B another matrix |
2 |
26 Feb 07 |
jari |
* @return Matrix product, A * B |
2 |
26 Feb 07 |
jari |
* @exception IllegalArgumentException Matrix inner dimensions must agree. |
2 |
26 Feb 07 |
jari |
755 |
*/ |
2 |
26 Feb 07 |
jari |
756 |
|
2 |
26 Feb 07 |
jari |
757 |
public FloatMatrix times(FloatMatrix B) { |
2 |
26 Feb 07 |
jari |
758 |
if (B.m != n) { |
2 |
26 Feb 07 |
jari |
759 |
throw new IllegalArgumentException("Matrix inner dimensions must agree."); |
2 |
26 Feb 07 |
jari |
760 |
} |
2 |
26 Feb 07 |
jari |
761 |
FloatMatrix X = new FloatMatrix(m,B.n); |
2 |
26 Feb 07 |
jari |
762 |
float[][] C = X.getArray(); |
2 |
26 Feb 07 |
jari |
763 |
float[] Bcolj = new float[n]; |
2 |
26 Feb 07 |
jari |
764 |
for (int j = 0; j < B.n; j++) { |
2 |
26 Feb 07 |
jari |
765 |
for (int k = 0; k < n; k++) { |
2 |
26 Feb 07 |
jari |
766 |
Bcolj[k] = B.A[k][j]; |
2 |
26 Feb 07 |
jari |
767 |
} |
2 |
26 Feb 07 |
jari |
768 |
for (int i = 0; i < m; i++) { |
2 |
26 Feb 07 |
jari |
769 |
float[] Arowi = A[i]; |
2 |
26 Feb 07 |
jari |
770 |
float s = 0; |
2 |
26 Feb 07 |
jari |
771 |
for (int k = 0; k < n; k++) { |
2 |
26 Feb 07 |
jari |
772 |
s += Arowi[k]*Bcolj[k]; |
2 |
26 Feb 07 |
jari |
773 |
} |
2 |
26 Feb 07 |
jari |
774 |
C[i][j] = s; |
2 |
26 Feb 07 |
jari |
775 |
} |
2 |
26 Feb 07 |
jari |
776 |
} |
2 |
26 Feb 07 |
jari |
777 |
return X; |
2 |
26 Feb 07 |
jari |
778 |
} |
2 |
26 Feb 07 |
jari |
779 |
|
2 |
26 Feb 07 |
jari |
/** LU Decomposition |
2 |
26 Feb 07 |
jari |
* @return LUDecomposition |
2 |
26 Feb 07 |
jari |
* @see LUDecomposition |
2 |
26 Feb 07 |
jari |
783 |
*/ |
2 |
26 Feb 07 |
jari |
784 |
|
2 |
26 Feb 07 |
jari |
/* public LUDecomposition lu () { |
2 |
26 Feb 07 |
jari |
return new LUDecomposition(this); |
2 |
26 Feb 07 |
jari |
787 |
}*/ |
2 |
26 Feb 07 |
jari |
788 |
|
2 |
26 Feb 07 |
jari |
/** QR Decomposition |
2 |
26 Feb 07 |
jari |
* @return QRDecomposition |
2 |
26 Feb 07 |
jari |
* @see QRDecomposition |
2 |
26 Feb 07 |
jari |
792 |
*/ |
2 |
26 Feb 07 |
jari |
793 |
|
2 |
26 Feb 07 |
jari |
/* public QRDecomposition qr () { |
2 |
26 Feb 07 |
jari |
return new QRDecomposition(this); |
2 |
26 Feb 07 |
jari |
796 |
}*/ |
2 |
26 Feb 07 |
jari |
797 |
|
2 |
26 Feb 07 |
jari |
/** Cholesky Decomposition |
2 |
26 Feb 07 |
jari |
* @return CholeskyDecomposition |
2 |
26 Feb 07 |
jari |
* @see CholeskyDecomposition |
2 |
26 Feb 07 |
jari |
801 |
*/ |
2 |
26 Feb 07 |
jari |
802 |
|
2 |
26 Feb 07 |
jari |
/* public CholeskyDecomposition chol () { |
2 |
26 Feb 07 |
jari |
return new CholeskyDecomposition(this); |
2 |
26 Feb 07 |
jari |
805 |
}*/ |
2 |
26 Feb 07 |
jari |
806 |
|
2 |
26 Feb 07 |
jari |
/** Singular Value Decomposition |
2 |
26 Feb 07 |
jari |
* @return SingularValueDecomposition |
2 |
26 Feb 07 |
jari |
* @see SingularValueDecomposition |
2 |
26 Feb 07 |
jari |
810 |
*/ |
2 |
26 Feb 07 |
jari |
811 |
|
2 |
26 Feb 07 |
jari |
/* public SingularValueDecomposition svd () { |
2 |
26 Feb 07 |
jari |
return new SingularValueDecomposition(this); |
2 |
26 Feb 07 |
jari |
814 |
}*/ |
2 |
26 Feb 07 |
jari |
815 |
|
2 |
26 Feb 07 |
jari |
/** Eigenvalue Decomposition |
2 |
26 Feb 07 |
jari |
* @return EigenvalueDecomposition |
2 |
26 Feb 07 |
jari |
* @see EigenvalueDecomposition |
2 |
26 Feb 07 |
jari |
819 |
*/ |
2 |
26 Feb 07 |
jari |
820 |
|
2 |
26 Feb 07 |
jari |
/* public EigenvalueDecomposition eig () { |
2 |
26 Feb 07 |
jari |
return new EigenvalueDecomposition(this); |
2 |
26 Feb 07 |
jari |
823 |
}*/ |
2 |
26 Feb 07 |
jari |
824 |
|
2 |
26 Feb 07 |
jari |
/** Solve A*X = B |
2 |
26 Feb 07 |
jari |
* @param B right hand side |
2 |
26 Feb 07 |
jari |
* @return solution if A is square, least squares solution otherwise |
2 |
26 Feb 07 |
jari |
828 |
*/ |
2 |
26 Feb 07 |
jari |
829 |
|
2 |
26 Feb 07 |
jari |
/* public FloatMatrix solve (FloatMatrix B) { |
2 |
26 Feb 07 |
jari |
return (m == n ? (new LUDecomposition(this)).solve(B) : |
2 |
26 Feb 07 |
jari |
(new QRDecomposition(this)).solve(B)); |
2 |
26 Feb 07 |
jari |
833 |
}*/ |
2 |
26 Feb 07 |
jari |
834 |
|
2 |
26 Feb 07 |
jari |
/** Solve X*A = B, which is also A'*X' = B' |
2 |
26 Feb 07 |
jari |
* @param B right hand side |
2 |
26 Feb 07 |
jari |
* @return solution if A is square, least squares solution otherwise. |
2 |
26 Feb 07 |
jari |
838 |
*/ |
2 |
26 Feb 07 |
jari |
839 |
|
2 |
26 Feb 07 |
jari |
/* public FloatMatrix solveTranspose (FloatMatrix B) { |
2 |
26 Feb 07 |
jari |
return transpose().solve(B.transpose()); |
2 |
26 Feb 07 |
jari |
842 |
}*/ |
2 |
26 Feb 07 |
jari |
843 |
|
2 |
26 Feb 07 |
jari |
/** Matrix inverse or pseudoinverse |
2 |
26 Feb 07 |
jari |
* @return inverse(A) if A is square, pseudoinverse otherwise. |
2 |
26 Feb 07 |
jari |
846 |
*/ |
2 |
26 Feb 07 |
jari |
847 |
|
2 |
26 Feb 07 |
jari |
/* public FloatMatrix inverse () { |
2 |
26 Feb 07 |
jari |
return solve(identity(m,m)); |
2 |
26 Feb 07 |
jari |
850 |
}*/ |
2 |
26 Feb 07 |
jari |
851 |
|
2 |
26 Feb 07 |
jari |
/** Matrix determinant |
2 |
26 Feb 07 |
jari |
* @return determinant |
2 |
26 Feb 07 |
jari |
854 |
*/ |
2 |
26 Feb 07 |
jari |
855 |
|
2 |
26 Feb 07 |
jari |
/* public float det () { |
2 |
26 Feb 07 |
jari |
return new LUDecomposition(this).det(); |
2 |
26 Feb 07 |
jari |
858 |
}*/ |
2 |
26 Feb 07 |
jari |
859 |
|
2 |
26 Feb 07 |
jari |
/** Matrix rank |
2 |
26 Feb 07 |
jari |
* @return effective numerical rank, obtained from SVD. |
2 |
26 Feb 07 |
jari |
862 |
*/ |
2 |
26 Feb 07 |
jari |
863 |
|
2 |
26 Feb 07 |
jari |
/* public int rank () { |
2 |
26 Feb 07 |
jari |
return new SingularValueDecomposition(this).rank(); |
2 |
26 Feb 07 |
jari |
866 |
}*/ |
2 |
26 Feb 07 |
jari |
867 |
|
2 |
26 Feb 07 |
jari |
/** Matrix condition (2 norm) |
2 |
26 Feb 07 |
jari |
* @return ratio of largest to smallest singular value. |
2 |
26 Feb 07 |
jari |
870 |
*/ |
2 |
26 Feb 07 |
jari |
871 |
|
2 |
26 Feb 07 |
jari |
/* public float cond () { |
2 |
26 Feb 07 |
jari |
return new SingularValueDecomposition(this).cond(); |
2 |
26 Feb 07 |
jari |
874 |
}*/ |
2 |
26 Feb 07 |
jari |
875 |
|
2 |
26 Feb 07 |
jari |
/** Matrix trace. |
2 |
26 Feb 07 |
jari |
* @return sum of the diagonal elements. |
2 |
26 Feb 07 |
jari |
878 |
*/ |
2 |
26 Feb 07 |
jari |
879 |
|
2 |
26 Feb 07 |
jari |
880 |
public float trace() { |
2 |
26 Feb 07 |
jari |
881 |
float t = 0; |
2 |
26 Feb 07 |
jari |
882 |
for (int i = 0; i < Math.min(m,n); i++) { |
2 |
26 Feb 07 |
jari |
883 |
t += A[i][i]; |
2 |
26 Feb 07 |
jari |
884 |
} |
2 |
26 Feb 07 |
jari |
885 |
return t; |
2 |
26 Feb 07 |
jari |
886 |
} |
2 |
26 Feb 07 |
jari |
887 |
|
2 |
26 Feb 07 |
jari |
/** Generate matrix with random elements |
2 |
26 Feb 07 |
jari |
* @param m Number of rows. |
2 |
26 Feb 07 |
jari |
* @param n Number of colums. |
2 |
26 Feb 07 |
jari |
* @return An m-by-n matrix with uniformly distributed random elements. |
2 |
26 Feb 07 |
jari |
892 |
*/ |
2 |
26 Feb 07 |
jari |
893 |
|
2 |
26 Feb 07 |
jari |
894 |
public static FloatMatrix random(int m, int n) { |
2 |
26 Feb 07 |
jari |
895 |
FloatMatrix A = new FloatMatrix(m,n); |
2 |
26 Feb 07 |
jari |
896 |
float[][] X = A.getArray(); |
2 |
26 Feb 07 |
jari |
897 |
for (int i = 0; i < m; i++) { |
2 |
26 Feb 07 |
jari |
898 |
for (int j = 0; j < n; j++) { |
2 |
26 Feb 07 |
jari |
899 |
X[i][j] = (float)Math.random(); |
2 |
26 Feb 07 |
jari |
900 |
} |
2 |
26 Feb 07 |
jari |
901 |
} |
2 |
26 Feb 07 |
jari |
902 |
return A; |
2 |
26 Feb 07 |
jari |
903 |
} |
2 |
26 Feb 07 |
jari |
904 |
|
2 |
26 Feb 07 |
jari |
/** Generate identity matrix |
2 |
26 Feb 07 |
jari |
* @param m Number of rows. |
2 |
26 Feb 07 |
jari |
* @param n Number of colums. |
2 |
26 Feb 07 |
jari |
* @return An m-by-n matrix with ones on the diagonal and zeros elsewhere. |
2 |
26 Feb 07 |
jari |
909 |
*/ |
2 |
26 Feb 07 |
jari |
910 |
|
2 |
26 Feb 07 |
jari |
911 |
public static FloatMatrix identity(int m, int n) { |
2 |
26 Feb 07 |
jari |
912 |
FloatMatrix A = new FloatMatrix(m,n); |
2 |
26 Feb 07 |
jari |
913 |
float[][] X = A.getArray(); |
2 |
26 Feb 07 |
jari |
914 |
for (int i = 0; i < m; i++) { |
2 |
26 Feb 07 |
jari |
915 |
for (int j = 0; j < n; j++) { |
2 |
26 Feb 07 |
jari |
916 |
X[i][j] = (float)(i == j ? 1.0 : 0.0); |
2 |
26 Feb 07 |
jari |
917 |
} |
2 |
26 Feb 07 |
jari |
918 |
} |
2 |
26 Feb 07 |
jari |
919 |
return A; |
2 |
26 Feb 07 |
jari |
920 |
} |
2 |
26 Feb 07 |
jari |
921 |
|
2 |
26 Feb 07 |
jari |
922 |
|
2 |
26 Feb 07 |
jari |
/** Print the matrix to stdout. Line the elements up in columns |
2 |
26 Feb 07 |
jari |
* with a Fortran-like 'Fw.d' style format. |
2 |
26 Feb 07 |
jari |
* @param w Column width. |
2 |
26 Feb 07 |
jari |
* @param d Number of digits after the decimal. |
2 |
26 Feb 07 |
jari |
927 |
*/ |
2 |
26 Feb 07 |
jari |
928 |
|
2 |
26 Feb 07 |
jari |
929 |
public void print(int w, int d) { |
2 |
26 Feb 07 |
jari |
930 |
print(new PrintWriter(System.out,true),w,d);} |
2 |
26 Feb 07 |
jari |
931 |
|
2 |
26 Feb 07 |
jari |
/** Print the matrix to the output stream. Line the elements up in |
2 |
26 Feb 07 |
jari |
* columns with a Fortran-like 'Fw.d' style format. |
2 |
26 Feb 07 |
jari |
* @param output Output stream. |
2 |
26 Feb 07 |
jari |
* @param w Column width. |
2 |
26 Feb 07 |
jari |
* @param d Number of digits after the decimal. |
2 |
26 Feb 07 |
jari |
937 |
*/ |
2 |
26 Feb 07 |
jari |
938 |
|
2 |
26 Feb 07 |
jari |
939 |
public void print(PrintWriter output, int w, int d) { |
2 |
26 Feb 07 |
jari |
940 |
DecimalFormat format = new DecimalFormat(); |
2 |
26 Feb 07 |
jari |
941 |
format.setMinimumIntegerDigits(1); |
2 |
26 Feb 07 |
jari |
942 |
format.setMaximumFractionDigits(d); |
2 |
26 Feb 07 |
jari |
943 |
format.setMinimumFractionDigits(d); |
2 |
26 Feb 07 |
jari |
944 |
format.setGroupingUsed(false); |
2 |
26 Feb 07 |
jari |
945 |
print(output,format,w+2); |
2 |
26 Feb 07 |
jari |
946 |
} |
2 |
26 Feb 07 |
jari |
947 |
|
2 |
26 Feb 07 |
jari |
/** Print the matrix to stdout. Line the elements up in columns. |
2 |
26 Feb 07 |
jari |
* Use the format object, and right justify within columns of width |
2 |
26 Feb 07 |
jari |
* characters. |
2 |
26 Feb 07 |
jari |
* @param format A Formatting object for individual elements. |
2 |
26 Feb 07 |
jari |
* @param width Field width for each column. |
2 |
26 Feb 07 |
jari |
953 |
*/ |
2 |
26 Feb 07 |
jari |
954 |
|
2 |
26 Feb 07 |
jari |
955 |
public void print(NumberFormat format, int width) { |
2 |
26 Feb 07 |
jari |
956 |
print(new PrintWriter(System.out,true),format,width);} |
2 |
26 Feb 07 |
jari |
957 |
|
2 |
26 Feb 07 |
jari |
// DecimalFormat is a little disappointing coming from Fortran or C's printf. |
2 |
26 Feb 07 |
jari |
// Since it doesn't pad on the left, the elements will come out different |
2 |
26 Feb 07 |
jari |
// widths. Consequently, we'll pass the desired column width in as an |
2 |
26 Feb 07 |
jari |
// argument and do the extra padding ourselves. |
2 |
26 Feb 07 |
jari |
962 |
|
2 |
26 Feb 07 |
jari |
/** Print the matrix to the output stream. Line the elements up in columns. |
2 |
26 Feb 07 |
jari |
* Use the format object, and right justify within columns of width |
2 |
26 Feb 07 |
jari |
* characters. |
2 |
26 Feb 07 |
jari |
* @param output the output stream. |
2 |
26 Feb 07 |
jari |
* @param format A formatting object to format the matrix elements |
2 |
26 Feb 07 |
jari |
* @param width Column width. |
2 |
26 Feb 07 |
jari |
969 |
*/ |
2 |
26 Feb 07 |
jari |
970 |
|
2 |
26 Feb 07 |
jari |
971 |
public void print(PrintWriter output, NumberFormat format, int width) { |
2 |
26 Feb 07 |
jari |
972 |
output.println(); // start on new line. |
2 |
26 Feb 07 |
jari |
973 |
for (int i = 0; i < m; i++) { |
2 |
26 Feb 07 |
jari |
974 |
for (int j = 0; j < n; j++) { |
2 |
26 Feb 07 |
jari |
975 |
String s = format.format(A[i][j]); // format the number |
2 |
26 Feb 07 |
jari |
976 |
int padding = Math.max(1,width-s.length()); // At _least_ 1 space |
2 |
26 Feb 07 |
jari |
977 |
for (int k = 0; k < padding; k++) |
2 |
26 Feb 07 |
jari |
978 |
output.print(' '); |
2 |
26 Feb 07 |
jari |
979 |
output.print(s); |
2 |
26 Feb 07 |
jari |
980 |
} |
2 |
26 Feb 07 |
jari |
981 |
output.println(); |
2 |
26 Feb 07 |
jari |
982 |
} |
2 |
26 Feb 07 |
jari |
983 |
output.println(); // end with blank line. |
2 |
26 Feb 07 |
jari |
984 |
} |
2 |
26 Feb 07 |
jari |
985 |
|
2 |
26 Feb 07 |
jari |
/** Read a matrix from a stream. The format is the same the print method, |
2 |
26 Feb 07 |
jari |
* so printed matrices can be read back in. Elements are separated by |
2 |
26 Feb 07 |
jari |
* whitespace, all the elements for each row appear on a single line, |
2 |
26 Feb 07 |
jari |
* the last row is followed by a blank line. |
2 |
26 Feb 07 |
jari |
* @param input the input stream. |
2 |
26 Feb 07 |
jari |
991 |
*/ |
2 |
26 Feb 07 |
jari |
992 |
|
2 |
26 Feb 07 |
jari |
993 |
public static FloatMatrix read(BufferedReader input) throws java.io.IOException { |
2 |
26 Feb 07 |
jari |
994 |
StreamTokenizer tokenizer= new StreamTokenizer(input); |
2 |
26 Feb 07 |
jari |
995 |
|
2 |
26 Feb 07 |
jari |
// Although StreamTokenizer will parse numbers, it doesn't recognize |
2 |
26 Feb 07 |
jari |
// scientific notation (E or D); however, Float.valueOf does. |
2 |
26 Feb 07 |
jari |
// The strategy here is to disable StreamTokenizer's number parsing. |
2 |
26 Feb 07 |
jari |
// We'll only get whitespace delimited words, EOL's and EOF's. |
2 |
26 Feb 07 |
jari |
// These words should all be numbers, for Float.valueOf to parse. |
2 |
26 Feb 07 |
jari |
1001 |
|
2 |
26 Feb 07 |
jari |
1002 |
tokenizer.resetSyntax(); |
2 |
26 Feb 07 |
jari |
1003 |
tokenizer.wordChars(0,255); |
2 |
26 Feb 07 |
jari |
1004 |
tokenizer.whitespaceChars(0, ' '); |
2 |
26 Feb 07 |
jari |
1005 |
tokenizer.eolIsSignificant(true); |
2 |
26 Feb 07 |
jari |
1006 |
java.util.Vector v = new java.util.Vector(); |
2 |
26 Feb 07 |
jari |
1007 |
|
2 |
26 Feb 07 |
jari |
// Ignore initial empty lines |
2 |
26 Feb 07 |
jari |
1009 |
while (tokenizer.nextToken() == StreamTokenizer.TT_EOL); |
2 |
26 Feb 07 |
jari |
1010 |
if (tokenizer.ttype == StreamTokenizer.TT_EOF) |
2 |
26 Feb 07 |
jari |
1011 |
throw new java.io.IOException("Unexpected EOF on matrix read."); |
2 |
26 Feb 07 |
jari |
1012 |
do { |
2 |
26 Feb 07 |
jari |
1013 |
v.addElement(Float.valueOf(tokenizer.sval)); // Read & store 1st row. |
2 |
26 Feb 07 |
jari |
1014 |
} while (tokenizer.nextToken() == StreamTokenizer.TT_WORD); |
2 |
26 Feb 07 |
jari |
1015 |
|
2 |
26 Feb 07 |
jari |
1016 |
int n = v.size(); // Now we've got the number of columns! |
2 |
26 Feb 07 |
jari |
1017 |
float row[] = new float[n]; |
2 |
26 Feb 07 |
jari |
1018 |
for (int j=0; j<n; j++) // extract the elements of the 1st row. |
2 |
26 Feb 07 |
jari |
1019 |
row[j]=((Float)v.elementAt(j)).floatValue(); |
2 |
26 Feb 07 |
jari |
1020 |
v.removeAllElements(); |
2 |
26 Feb 07 |
jari |
1021 |
v.addElement(row); // Start storing rows instead of columns. |
2 |
26 Feb 07 |
jari |
1022 |
while (tokenizer.nextToken() == StreamTokenizer.TT_WORD) { |
2 |
26 Feb 07 |
jari |
// While non-empty lines |
2 |
26 Feb 07 |
jari |
1024 |
v.addElement(row = new float[n]); |
2 |
26 Feb 07 |
jari |
1025 |
int j = 0; |
2 |
26 Feb 07 |
jari |
1026 |
do { |
2 |
26 Feb 07 |
jari |
1027 |
if (j >= n) throw new java.io.IOException |
2 |
26 Feb 07 |
jari |
1028 |
("Row " + v.size() + " is too long."); |
2 |
26 Feb 07 |
jari |
1029 |
row[j++] = Float.valueOf(tokenizer.sval).floatValue(); |
2 |
26 Feb 07 |
jari |
1030 |
} while (tokenizer.nextToken() == StreamTokenizer.TT_WORD); |
2 |
26 Feb 07 |
jari |
1031 |
if (j < n) throw new java.io.IOException |
2 |
26 Feb 07 |
jari |
1032 |
("Row " + v.size() + " is too short."); |
2 |
26 Feb 07 |
jari |
1033 |
} |
2 |
26 Feb 07 |
jari |
1034 |
int m = v.size(); // Now we've got the number of rows. |
2 |
26 Feb 07 |
jari |
1035 |
float[][] A = new float[m][]; |
2 |
26 Feb 07 |
jari |
1036 |
v.copyInto(A); // copy the rows out of the vector |
2 |
26 Feb 07 |
jari |
1037 |
return new FloatMatrix(A); |
2 |
26 Feb 07 |
jari |
1038 |
} |
2 |
26 Feb 07 |
jari |
1039 |
|
2 |
26 Feb 07 |
jari |
1040 |
|
2 |
26 Feb 07 |
jari |
1041 |
/* ------------------------ |
2 |
26 Feb 07 |
jari |
Private Methods |
2 |
26 Feb 07 |
jari |
1043 |
* ------------------------ */ |
2 |
26 Feb 07 |
jari |
1044 |
|
2 |
26 Feb 07 |
jari |
/** Check if size(A) == size(B) **/ |
2 |
26 Feb 07 |
jari |
1046 |
|
2 |
26 Feb 07 |
jari |
1047 |
private void checkMatrixDimensions(FloatMatrix B) { |
2 |
26 Feb 07 |
jari |
1048 |
if (B.m != m || B.n != n) { |
2 |
26 Feb 07 |
jari |
1049 |
throw new IllegalArgumentException("Matrix dimensions must agree."); |
2 |
26 Feb 07 |
jari |
1050 |
} |
2 |
26 Feb 07 |
jari |
1051 |
} |
2 |
26 Feb 07 |
jari |
1052 |
|
2 |
26 Feb 07 |
jari |
1053 |
|
2 |
26 Feb 07 |
jari |
/** Get a sub-matrix . |
2 |
26 Feb 07 |
jari |
* @return A' |
2 |
26 Feb 07 |
jari |
1056 |
*/ |
2 |
26 Feb 07 |
jari |
1057 |
|
2 |
26 Feb 07 |
jari |
1058 |
public FloatMatrix getSubMatrix(int displayInterval) { |
2 |
26 Feb 07 |
jari |
1059 |
|
2 |
26 Feb 07 |
jari |
1060 |
if (displayInterval <= 0) return this; |
2 |
26 Feb 07 |
jari |
1061 |
|
2 |
26 Feb 07 |
jari |
1062 |
int sub_m = m / displayInterval; |
2 |
26 Feb 07 |
jari |
1063 |
int sub_n = n / displayInterval; |
2 |
26 Feb 07 |
jari |
1064 |
|
2 |
26 Feb 07 |
jari |
1065 |
FloatMatrix X = new FloatMatrix(sub_m, sub_n); |
2 |
26 Feb 07 |
jari |
1066 |
|
2 |
26 Feb 07 |
jari |
1067 |
float[][] C = X.getArray(); |
2 |
26 Feb 07 |
jari |
1068 |
|
2 |
26 Feb 07 |
jari |
1069 |
for (int i = 0, k = 0; i < m && k < sub_m; i += displayInterval, k++) { |
2 |
26 Feb 07 |
jari |
1070 |
for (int j = 0, l = 0; j < n && l < sub_n; j += displayInterval, l++) { |
2 |
26 Feb 07 |
jari |
1071 |
C[k][l] = A[i][j]; |
2 |
26 Feb 07 |
jari |
1072 |
} |
2 |
26 Feb 07 |
jari |
1073 |
} |
2 |
26 Feb 07 |
jari |
1074 |
|
2 |
26 Feb 07 |
jari |
1075 |
return X; |
2 |
26 Feb 07 |
jari |
1076 |
|
2 |
26 Feb 07 |
jari |
1077 |
|
2 |
26 Feb 07 |
jari |
1078 |
} |
2 |
26 Feb 07 |
jari |
1079 |
|
2 |
26 Feb 07 |
jari |
1080 |
} |