File: Matrix.java

package info (click to toggle)
gpsprune 17-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,984 kB
  • ctags: 5,218
  • sloc: java: 39,403; sh: 25; makefile: 17; python: 15
file content (259 lines) | stat: -rw-r--r-- 6,344 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package tim.prune.function.estimate.jama;

/**
 * The Java Matrix Class provides the fundamental operations of numerical linear algebra.
 * Original authors The MathWorks, Inc. and the National Institute of Standards and Technology
 * The original public domain code has now been modified and reduced to only contain
 * the use of QR Decomposition of rectangular matrices, to solve least squares regression,
 * and is placed under GPL2 with the rest of the GpsPrune code.
 */
public class Matrix
{

	/** Array for internal storage of elements */
	private double[][] _matrix;

	/** Row and column dimensions */
	private int _m, _n;


	/**
	 * Construct an m-by-n matrix of zeros
	 * @param inM  Number of rows
	 * @param inN  Number of colums
	 */
	public Matrix(int inM, int inN)
	{
		_m = inM;
		_n = inN;
		_matrix = new double[inM][inN];
	}

	/**
	 * Construct a matrix from a 2-D array
	 * @param A   Two-dimensional array of doubles.
	 * @exception IllegalArgumentException All rows must have the same length
	 */
	public Matrix(double[][] A)
	{
		_m = A.length;
		_n = A[0].length;
		for (int i = 0; i < _m; i++) {
			if (A[i].length != _n) {
				throw new IllegalArgumentException("All rows must have the same length.");
			}
		}
		_matrix = A;
	}

	/**
	 * Construct a matrix quickly without checking arguments.
	 * @param inA   Two-dimensional array of doubles.
	 * @param inM   Number of rows
	 * @param inN   Number of columns
	 */
	public Matrix(double[][] inA, int inM, int inN)
	{
		_matrix = inA;
		_m = inM;
		_n = inN;
	}

	/*
	 * ------------------------ Public Methods ------------------------
	 */


	/**
	 * Set a value in a cell of the matrix
	 * @param inRow row index
	 * @param inCol column index
	 * @param inValue value to set
	 */
	public void setValue(int inRow, int inCol, double inValue)
	{
		_matrix[inRow][inCol] = inValue;
	}

	/**
	 * Access the internal two-dimensional array.
	 * @return Pointer to the two-dimensional array of matrix elements.
	 */
	public double[][] getArray() {
		return _matrix;
	}

	/**
	 * Copy the internal two-dimensional array.
	 * @return Two-dimensional array copy of matrix elements.
	 */
	public double[][] getArrayCopy()
	{
		double[][] C = new double[_m][_n];
		for (int i = 0; i < _m; i++) {
			for (int j = 0; j < _n; j++) {
				C[i][j] = _matrix[i][j];
			}
		}
		return C;
	}

	/**
	 * Get a single element.
	 * @param inRow   Row index
	 * @param inCol   Column index
	 * @return A(inRow,inCol)
	 * @exception ArrayIndexOutOfBoundsException
	 */
	public double get(int inRow, int inCol) {
		return _matrix[inRow][inCol];
	}

	/** @return number of rows _m */
	public int getNumRows() {
		return _m;
	}

	/** @return number of columns _n */
	public int getNumColumns() {
		return _n;
	}

	/**
	 * Get a submatrix
	 * @param i0  Initial row index
	 * @param i1  Final row index
	 * @param j0  Initial column index
	 * @param j1  Final column index
	 * @return A(i0:i1,j0:j1)
	 * @exception ArrayIndexOutOfBoundsException
	 */
	public Matrix getMatrix(int i0, int i1, int j0, int j1)
	{
		Matrix X = new Matrix(i1 - i0 + 1, j1 - j0 + 1);
		double[][] B = X.getArray();
		try {
			for (int i = i0; i <= i1; i++) {
				for (int j = j0; j <= j1; j++) {
					B[i - i0][j - j0] = _matrix[i][j];
				}
			}
		} catch (ArrayIndexOutOfBoundsException e) {
			throw new ArrayIndexOutOfBoundsException("Submatrix indices");
		}
		return X;
	}


	/**
	 * Linear algebraic matrix multiplication, A * B
	 * @param B   another matrix
	 * @return Matrix product, A * B
	 * @exception IllegalArgumentException if matrix dimensions don't agree
	 */
	public Matrix times(Matrix B)
	{
		if (B._m != _n) {
			throw new IllegalArgumentException("Matrix inner dimensions must agree.");
		}
		Matrix X = new Matrix(_m, B._n);
		double[][] C = X.getArray();
		double[] Bcolj = new double[_n];
		for (int j = 0; j < B._n; j++) {
			for (int k = 0; k < _n; k++) {
				Bcolj[k] = B._matrix[k][j];
			}
			for (int i = 0; i < _m; i++) {
				double[] Arowi = _matrix[i];
				double s = 0;
				for (int k = 0; k < _n; k++) {
					s += Arowi[k] * Bcolj[k];
				}
				C[i][j] = s;
			}
		}
		return X;
	}

	/**
	 * Subtract the other matrix from this one
	 * @param B   another matrix
	 * @return difference this - B
	 * @exception IllegalArgumentException if matrix dimensions don't agree
	 */
	public Matrix minus(Matrix B)
	{
		if (B._m != _m || B._n != _n) {
			throw new IllegalArgumentException("Matrix dimensions must agree.");
		}
		Matrix result = new Matrix(_m, _n);
		for (int i = 0; i < _m; i++) {
			for (int j = 0; j < _n; j++) {
				result.setValue(i, j, get(i, j) - B.get(i, j));
			}
		}
		return result;
	}

	/**
	 * Divide each element of this matrix by the corresponding element in the other one
	 * @param B   another matrix
	 * @return this[i,j]/other[i,j]
	 * @exception IllegalArgumentException if matrix dimensions don't agree
	 */
	public Matrix divideEach(Matrix B)
	{
		if (B._m != _m || B._n != _n) {
			throw new IllegalArgumentException("Matrix dimensions must agree.");
		}
		Matrix result = new Matrix(_m, _n);
		for (int i = 0; i < _m; i++) {
			for (int j = 0; j < _n; j++) {
				result.setValue(i, j, get(i, j) / B.get(i, j));
			}
		}
		return result;
	}

	/**
	 * Solve A*X = B
	 * @param B   right hand side
	 * @return least squares solution
	 */
	public Matrix solve(Matrix B) {
		return new QRDecomposition(this).solve(B);
	}

	/**
	 * @return the average absolute value of all the elements in the matrix
	 */
	public double getAverageAbsValue()
	{
		double total = 0.0;
		for (int i = 0; i < _m; i++) {
			for (int j = 0; j < _n; j++) {
				total += Math.abs(_matrix[i][j]);
			}
		}
		return total / _m / _n;
	}

	/**
	 * Primitive output for debugging
	 */
	public String toString()
	{
		StringBuilder builder = new StringBuilder();
		builder.append('(');
		for (int i = 0; i < _m; i++) {
			builder.append('(');
			for (int j = 0; j < _n; j++) {
				builder.append((_matrix[i][j]));
				builder.append(", ");
			}
			builder.append(") ");
		}
		builder.append(')');
		return builder.toString();
	}
}