File: Matrix.java

package info (click to toggle)
rockhopper 2.0.3%2Bdfsg2-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 33,140 kB
  • sloc: java: 10,831; sh: 31; xml: 29; makefile: 14
file content (310 lines) | stat: -rw-r--r-- 7,671 bytes parent folder | download | duplicates (2)
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
 * Copyright 2013 Brian Tjaden
 *
 * This file is part of Rockhopper.
 *
 * Rockhopper is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * Rockhopper is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * (in the file gpl.txt) along with Rockhopper.  
 * If not, see <http://www.gnu.org/licenses/>.
 */

public class Matrix {

    /********************************************
     **********   INSTANCE VARIABLES   **********
     ********************************************/

    private double[][] M;
    private int rows, cols;  // Number or rows and columns



    /**************************************
     **********   CONSTRUCTORS   **********
     **************************************/

    public Matrix (int rows, int cols) {
	M = new double[rows][cols];
	this.rows = rows;
	this.cols = cols;
    }

    public Matrix (double[][] M) {
	this.M = M;
	rows = M.length;
	cols = M[0].length;
    }



    /*************************************************
     **********   PUBLIC INSTANCE METHODS   **********
     *************************************************/

    /**
     * Returns the number of rows in this Matrix.
     */
    public int getRows () {
	return rows;
    }

    /**
     * Returns the number of columns in this Matrix.
     */
    public int getCols () {
	return cols;
    }

    /**
     * Returns the 2D array representing this Matrix.
     */
    public double[][] getArray () {
	return M;
    }

    /**
     * Returns a copy of the 2D array representing this Matrix.
     */

    public double[][] getArrayCopy () {
	double[][] M2 = new double[rows][cols];
	for (int i = 0; i < rows; i++)
	    for (int j = 0; j < cols; j++)
		M2[i][j] = M[i][j];
	return M2;
    }

    /**
     * Return a submatrix of this Matrix.
     */
    public Matrix getMatrix (int[] r, int x, int y) {
	Matrix mtrx = new Matrix(r.length,y-x+1);
	double[][] M2 = mtrx.getArray();
	for (int i = 0; i < r.length; i++)
	    for (int j = x; j <= y; j++)
		M2[i][j-x] = M[r[i]][j];
	return mtrx;
    }

    /**
     * Return a submatrix of this Matrix.
     */
    public Matrix getMatrix (int x1, int x2, int y1, int y2) {
	Matrix mtrx = new Matrix(x2-x1+1,y2-y1+1);
	double[][] M2 = mtrx.getArray();
	for (int i = x1; i <= x2; i++)
	    for (int j = y1; j <= y2; j++)
		M2[i-x1][j-y1] = M[i][j];
	return mtrx;
    }

    public Matrix solve (Matrix B) {
	if (rows == cols) return (new LUDecomposition(this)).solve(B);
	else return (new QRDecomposition(this)).solve(B);
    }



    /***************************************
     **********   CLASS METHODS   **********
     ***************************************/

    public static double hypot(double x, double y) {
	double r;
	if (Math.abs(x) > Math.abs(y)) {
	    r = y/x;
	    r = Math.abs(x)*Math.sqrt(1+r*r);
	} else if (y != 0) {
	    r = x/y;
	    r = Math.abs(y)*Math.sqrt(1+r*r);
	} else {
	    r = 0.0;
	}
	return r;
    }

}



/*****************************************
 **********   LU MATRIX CLASS   **********
 *****************************************/

class LUDecomposition {

    /********************************************
     **********   INSTANCE VARIABLES   **********
     ********************************************/

    private double[][] LU;
    private int rows;
    private int cols;
    private int signOfPivot; 
    private int[] pivot;



    /**************************************
     **********   CONSTRUCTORS   **********
     **************************************/

    public LUDecomposition (Matrix A) {

	LU = A.getArrayCopy();
	rows = A.getRows();
	cols = A.getCols();
	pivot = new int[rows];
	for (int i = 0; i < rows; i++) pivot[i] = i;
	signOfPivot = 1;
	double[] LUrowi;
	double[] LUcolj = new double[rows];
	
	for (int j = 0; j < cols; j++) {	    
	    for (int i = 0; i < rows; i++) LUcolj[i] = LU[i][j];
	    for (int i = 0; i < rows; i++) {
		LUrowi = LU[i];
		int kmax = Math.min(i,j);
		double s = 0.0;
		for (int k = 0; k < kmax; k++) s += LUrowi[k]*LUcolj[k];
		LUrowi[j] = LUcolj[i] -= s;
	    }
	    int p = j;
	    for (int i = j+1; i < rows; i++) {
		if (Math.abs(LUcolj[i]) > Math.abs(LUcolj[p])) p = i;
	    }
	    if (p != j) {
		for (int k = 0; k < cols; k++) {
		    double t = LU[p][k];
		    LU[p][k] = LU[j][k];
		    LU[j][k] = t;
		}
		int k = pivot[p]; pivot[p] = pivot[j]; pivot[j] = k;
		signOfPivot = -signOfPivot;
	    }
	    if (j < rows & LU[j][j] != 0.0) {
		for (int i = j+1; i < rows; i++) LU[i][j] /= LU[j][j];
	    }
	}
    }

    

    /*************************************************
     **********   PUBLIC INSTANCE METHODS   **********
     *************************************************/

    public Matrix solve (Matrix B) {

	int cols2 = B.getCols();
	Matrix mtrx = B.getMatrix(pivot,0,cols2-1);
	double[][] M2 = mtrx.getArray();
	
	for (int k = 0; k < cols; k++)
	    for (int i = k+1; i < cols; i++)
		for (int j = 0; j < cols2; j++)
		    M2[i][j] -= M2[k][j]*LU[i][k];

	for (int k = cols-1; k >= 0; k--) {
	    for (int j = 0; j < cols2; j++)
		M2[k][j] /= LU[k][k];
	    for (int i = 0; i < k; i++)
		for (int j = 0; j < cols2; j++)
		    M2[i][j] -= M2[k][j]*LU[i][k];
	}
	return mtrx;
    }

}



/*****************************************
 **********   QR MATRIX CLASS   **********
 *****************************************/

class QRDecomposition {

    /********************************************
     **********   INSTANCE VARIABLES   **********
     ********************************************/

    private double[][] QR;
    private int rows;
    private int cols;
    private double[] Rdiag;



    /**************************************
     **********   CONSTRUCTORS   **********
     **************************************/

    public QRDecomposition (Matrix A) {
	QR = A.getArrayCopy();
	rows = A.getRows();
	cols = A.getCols();
	Rdiag = new double[cols];
	for (int k = 0; k < cols; k++) {
	    double nrm = 0;
	    for (int i = k; i < rows; i++) nrm = Matrix.hypot(nrm,QR[i][k]);
	    if (nrm != 0.0) {
		if (QR[k][k] < 0) nrm = -nrm;
		for (int i = k; i < rows; i++) QR[i][k] /= nrm;
		QR[k][k] += 1.0;
		for (int j = k+1; j < cols; j++) {
		    double s = 0.0; 
		    for (int i = k; i < rows; i++) s += QR[i][k]*QR[i][j];
		    s = -s/QR[k][k];
		    for (int i = k; i < rows; i++) QR[i][j] += s*QR[i][k];
		}
	    }
	    Rdiag[k] = -nrm;
	}
    }



    /*************************************************
     **********   PUBLIC INSTANCE METHODS   **********
     *************************************************/

    public boolean isFullRank () {
	for (int j = 0; j < cols; j++) {
	    if (Rdiag[j] == 0) return false;
	}
	return true;
    }

    public Matrix solve (Matrix B) {
	int cols2 = B.getCols();
	double[][] M2 = B.getArrayCopy();
	for (int k = 0; k < cols; k++) {
	    for (int j = 0; j < cols2; j++) {
		double s = 0.0; 
		for (int i = k; i < rows; i++) s += QR[i][k]*M2[i][j];
		s = -s/QR[k][k];
		for (int i = k; i < rows; i++) M2[i][j] += s*QR[i][k];
	    }
	}
	for (int k = cols-1; k >= 0; k--) {
	    for (int j = 0; j < cols2; j++) M2[k][j] /= Rdiag[k];
	    for (int i = 0; i < k; i++)
		for (int j = 0; j < cols2; j++)
		    M2[i][j] -= M2[k][j]*QR[i][k];
	}
	return (new Matrix(M2).getMatrix(0,cols-1,0,cols2-1));
    }
}