1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.math3.linear;
19
20 import java.io.Serializable;
21
22 import org.apache.commons.math3.Field;
23 import org.apache.commons.math3.FieldElement;
24 import org.apache.commons.math3.exception.NoDataException;
25 import org.apache.commons.math3.exception.DimensionMismatchException;
26 import org.apache.commons.math3.exception.MathIllegalStateException;
27 import org.apache.commons.math3.exception.NotStrictlyPositiveException;
28 import org.apache.commons.math3.exception.NullArgumentException;
29 import org.apache.commons.math3.exception.NumberIsTooSmallException;
30 import org.apache.commons.math3.exception.OutOfRangeException;
31 import org.apache.commons.math3.exception.util.LocalizedFormats;
32 import org.apache.commons.math3.util.MathArrays;
33 import org.apache.commons.math3.util.MathUtils;
34
35
36
37
38
39
40
41
42
43
44
45
46 public class Array2DRowFieldMatrix<T extends FieldElement<T>>
47 extends AbstractFieldMatrix<T>
48 implements Serializable {
49
50 private static final long serialVersionUID = 7260756672015356458L;
51
52 private T[][] data;
53
54
55
56
57
58 public Array2DRowFieldMatrix(final Field<T> field) {
59 super(field);
60 }
61
62
63
64
65
66
67
68
69
70 public Array2DRowFieldMatrix(final Field<T> field, final int rowDimension,
71 final int columnDimension)
72 throws NotStrictlyPositiveException {
73 super(field, rowDimension, columnDimension);
74 data = MathArrays.buildArray(field, rowDimension, columnDimension);
75 }
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90 public Array2DRowFieldMatrix(final T[][] d)
91 throws DimensionMismatchException, NullArgumentException,
92 NoDataException {
93 this(extractField(d), d);
94 }
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110 public Array2DRowFieldMatrix(final Field<T> field, final T[][] d)
111 throws DimensionMismatchException, NullArgumentException,
112 NoDataException {
113 super(field);
114 copyIn(d);
115 }
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132 public Array2DRowFieldMatrix(final T[][] d, final boolean copyArray)
133 throws DimensionMismatchException, NoDataException,
134 NullArgumentException {
135 this(extractField(d), d, copyArray);
136 }
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154 public Array2DRowFieldMatrix(final Field<T> field, final T[][] d, final boolean copyArray)
155 throws DimensionMismatchException, NoDataException, NullArgumentException {
156 super(field);
157 if (copyArray) {
158 copyIn(d);
159 } else {
160 MathUtils.checkNotNull(d);
161 final int nRows = d.length;
162 if (nRows == 0) {
163 throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_ROW);
164 }
165 final int nCols = d[0].length;
166 if (nCols == 0) {
167 throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_COLUMN);
168 }
169 for (int r = 1; r < nRows; r++) {
170 if (d[r].length != nCols) {
171 throw new DimensionMismatchException(nCols, d[r].length);
172 }
173 }
174 data = d;
175 }
176 }
177
178
179
180
181
182
183
184
185
186 public Array2DRowFieldMatrix(final T[] v) throws NoDataException {
187 this(extractField(v), v);
188 }
189
190
191
192
193
194
195
196
197
198 public Array2DRowFieldMatrix(final Field<T> field, final T[] v) {
199 super(field);
200 final int nRows = v.length;
201 data = MathArrays.buildArray(getField(), nRows, 1);
202 for (int row = 0; row < nRows; row++) {
203 data[row][0] = v[row];
204 }
205 }
206
207
208 @Override
209 public FieldMatrix<T> createMatrix(final int rowDimension,
210 final int columnDimension)
211 throws NotStrictlyPositiveException {
212 return new Array2DRowFieldMatrix<T>(getField(), rowDimension, columnDimension);
213 }
214
215
216 @Override
217 public FieldMatrix<T> copy() {
218 return new Array2DRowFieldMatrix<T>(getField(), copyOut(), false);
219 }
220
221
222
223
224
225
226
227
228
229 public Array2DRowFieldMatrix<T> add(final Array2DRowFieldMatrix<T> m)
230 throws MatrixDimensionMismatchException {
231
232 checkAdditionCompatible(m);
233
234 final int rowCount = getRowDimension();
235 final int columnCount = getColumnDimension();
236 final T[][] outData = MathArrays.buildArray(getField(), rowCount, columnCount);
237 for (int row = 0; row < rowCount; row++) {
238 final T[] dataRow = data[row];
239 final T[] mRow = m.data[row];
240 final T[] outDataRow = outData[row];
241 for (int col = 0; col < columnCount; col++) {
242 outDataRow[col] = dataRow[col].add(mRow[col]);
243 }
244 }
245
246 return new Array2DRowFieldMatrix<T>(getField(), outData, false);
247 }
248
249
250
251
252
253
254
255
256
257 public Array2DRowFieldMatrix<T> subtract(final Array2DRowFieldMatrix<T> m)
258 throws MatrixDimensionMismatchException {
259
260 checkSubtractionCompatible(m);
261
262 final int rowCount = getRowDimension();
263 final int columnCount = getColumnDimension();
264 final T[][] outData = MathArrays.buildArray(getField(), rowCount, columnCount);
265 for (int row = 0; row < rowCount; row++) {
266 final T[] dataRow = data[row];
267 final T[] mRow = m.data[row];
268 final T[] outDataRow = outData[row];
269 for (int col = 0; col < columnCount; col++) {
270 outDataRow[col] = dataRow[col].subtract(mRow[col]);
271 }
272 }
273
274 return new Array2DRowFieldMatrix<T>(getField(), outData, false);
275
276 }
277
278
279
280
281
282
283
284
285
286 public Array2DRowFieldMatrix<T> multiply(final Array2DRowFieldMatrix<T> m)
287 throws DimensionMismatchException {
288
289 checkMultiplicationCompatible(m);
290
291 final int nRows = this.getRowDimension();
292 final int nCols = m.getColumnDimension();
293 final int nSum = this.getColumnDimension();
294 final T[][] outData = MathArrays.buildArray(getField(), nRows, nCols);
295 for (int row = 0; row < nRows; row++) {
296 final T[] dataRow = data[row];
297 final T[] outDataRow = outData[row];
298 for (int col = 0; col < nCols; col++) {
299 T sum = getField().getZero();
300 for (int i = 0; i < nSum; i++) {
301 sum = sum.add(dataRow[i].multiply(m.data[i][col]));
302 }
303 outDataRow[col] = sum;
304 }
305 }
306
307 return new Array2DRowFieldMatrix<T>(getField(), outData, false);
308
309 }
310
311
312 @Override
313 public T[][] getData() {
314 return copyOut();
315 }
316
317
318
319
320
321
322
323 public T[][] getDataRef() {
324 return data;
325 }
326
327
328 @Override
329 public void setSubMatrix(final T[][] subMatrix, final int row,
330 final int column)
331 throws OutOfRangeException, NullArgumentException, NoDataException,
332 DimensionMismatchException {
333 if (data == null) {
334 if (row > 0) {
335 throw new MathIllegalStateException(LocalizedFormats.FIRST_ROWS_NOT_INITIALIZED_YET, row);
336 }
337 if (column > 0) {
338 throw new MathIllegalStateException(LocalizedFormats.FIRST_COLUMNS_NOT_INITIALIZED_YET, column);
339 }
340 final int nRows = subMatrix.length;
341 if (nRows == 0) {
342 throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_ROW);
343 }
344
345 final int nCols = subMatrix[0].length;
346 if (nCols == 0) {
347 throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_COLUMN);
348 }
349 data = MathArrays.buildArray(getField(), subMatrix.length, nCols);
350 for (int i = 0; i < data.length; ++i) {
351 if (subMatrix[i].length != nCols) {
352 throw new DimensionMismatchException(nCols, subMatrix[i].length);
353 }
354 System.arraycopy(subMatrix[i], 0, data[i + row], column, nCols);
355 }
356 } else {
357 super.setSubMatrix(subMatrix, row, column);
358 }
359
360 }
361
362
363 @Override
364 public T getEntry(final int row, final int column)
365 throws OutOfRangeException {
366 checkRowIndex(row);
367 checkColumnIndex(column);
368
369 return data[row][column];
370 }
371
372
373 @Override
374 public void setEntry(final int row, final int column, final T value)
375 throws OutOfRangeException {
376 checkRowIndex(row);
377 checkColumnIndex(column);
378
379 data[row][column] = value;
380 }
381
382
383 @Override
384 public void addToEntry(final int row, final int column, final T increment)
385 throws OutOfRangeException {
386 checkRowIndex(row);
387 checkColumnIndex(column);
388
389 data[row][column] = data[row][column].add(increment);
390 }
391
392
393 @Override
394 public void multiplyEntry(final int row, final int column, final T factor)
395 throws OutOfRangeException {
396 checkRowIndex(row);
397 checkColumnIndex(column);
398
399 data[row][column] = data[row][column].multiply(factor);
400 }
401
402
403 @Override
404 public int getRowDimension() {
405 return (data == null) ? 0 : data.length;
406 }
407
408
409 @Override
410 public int getColumnDimension() {
411 return ((data == null) || (data[0] == null)) ? 0 : data[0].length;
412 }
413
414
415 @Override
416 public T[] operate(final T[] v) throws DimensionMismatchException {
417 final int nRows = this.getRowDimension();
418 final int nCols = this.getColumnDimension();
419 if (v.length != nCols) {
420 throw new DimensionMismatchException(v.length, nCols);
421 }
422 final T[] out = MathArrays.buildArray(getField(), nRows);
423 for (int row = 0; row < nRows; row++) {
424 final T[] dataRow = data[row];
425 T sum = getField().getZero();
426 for (int i = 0; i < nCols; i++) {
427 sum = sum.add(dataRow[i].multiply(v[i]));
428 }
429 out[row] = sum;
430 }
431 return out;
432 }
433
434
435 @Override
436 public T[] preMultiply(final T[] v) throws DimensionMismatchException {
437 final int nRows = getRowDimension();
438 final int nCols = getColumnDimension();
439 if (v.length != nRows) {
440 throw new DimensionMismatchException(v.length, nRows);
441 }
442
443 final T[] out = MathArrays.buildArray(getField(), nCols);
444 for (int col = 0; col < nCols; ++col) {
445 T sum = getField().getZero();
446 for (int i = 0; i < nRows; ++i) {
447 sum = sum.add(data[i][col].multiply(v[i]));
448 }
449 out[col] = sum;
450 }
451
452 return out;
453 }
454
455
456 @Override
457 public T walkInRowOrder(final FieldMatrixChangingVisitor<T> visitor) {
458 final int rows = getRowDimension();
459 final int columns = getColumnDimension();
460 visitor.start(rows, columns, 0, rows - 1, 0, columns - 1);
461 for (int i = 0; i < rows; ++i) {
462 final T[] rowI = data[i];
463 for (int j = 0; j < columns; ++j) {
464 rowI[j] = visitor.visit(i, j, rowI[j]);
465 }
466 }
467 return visitor.end();
468 }
469
470
471 @Override
472 public T walkInRowOrder(final FieldMatrixPreservingVisitor<T> visitor) {
473 final int rows = getRowDimension();
474 final int columns = getColumnDimension();
475 visitor.start(rows, columns, 0, rows - 1, 0, columns - 1);
476 for (int i = 0; i < rows; ++i) {
477 final T[] rowI = data[i];
478 for (int j = 0; j < columns; ++j) {
479 visitor.visit(i, j, rowI[j]);
480 }
481 }
482 return visitor.end();
483 }
484
485
486 @Override
487 public T walkInRowOrder(final FieldMatrixChangingVisitor<T> visitor,
488 final int startRow, final int endRow,
489 final int startColumn, final int endColumn)
490 throws OutOfRangeException, NumberIsTooSmallException {
491 checkSubMatrixIndex(startRow, endRow, startColumn, endColumn);
492 visitor.start(getRowDimension(), getColumnDimension(),
493 startRow, endRow, startColumn, endColumn);
494 for (int i = startRow; i <= endRow; ++i) {
495 final T[] rowI = data[i];
496 for (int j = startColumn; j <= endColumn; ++j) {
497 rowI[j] = visitor.visit(i, j, rowI[j]);
498 }
499 }
500 return visitor.end();
501 }
502
503
504 @Override
505 public T walkInRowOrder(final FieldMatrixPreservingVisitor<T> visitor,
506 final int startRow, final int endRow,
507 final int startColumn, final int endColumn)
508 throws OutOfRangeException, NumberIsTooSmallException {
509 checkSubMatrixIndex(startRow, endRow, startColumn, endColumn);
510 visitor.start(getRowDimension(), getColumnDimension(),
511 startRow, endRow, startColumn, endColumn);
512 for (int i = startRow; i <= endRow; ++i) {
513 final T[] rowI = data[i];
514 for (int j = startColumn; j <= endColumn; ++j) {
515 visitor.visit(i, j, rowI[j]);
516 }
517 }
518 return visitor.end();
519 }
520
521
522 @Override
523 public T walkInColumnOrder(final FieldMatrixChangingVisitor<T> visitor) {
524 final int rows = getRowDimension();
525 final int columns = getColumnDimension();
526 visitor.start(rows, columns, 0, rows - 1, 0, columns - 1);
527 for (int j = 0; j < columns; ++j) {
528 for (int i = 0; i < rows; ++i) {
529 final T[] rowI = data[i];
530 rowI[j] = visitor.visit(i, j, rowI[j]);
531 }
532 }
533 return visitor.end();
534 }
535
536
537 @Override
538 public T walkInColumnOrder(final FieldMatrixPreservingVisitor<T> visitor) {
539 final int rows = getRowDimension();
540 final int columns = getColumnDimension();
541 visitor.start(rows, columns, 0, rows - 1, 0, columns - 1);
542 for (int j = 0; j < columns; ++j) {
543 for (int i = 0; i < rows; ++i) {
544 visitor.visit(i, j, data[i][j]);
545 }
546 }
547 return visitor.end();
548 }
549
550
551 @Override
552 public T walkInColumnOrder(final FieldMatrixChangingVisitor<T> visitor,
553 final int startRow, final int endRow,
554 final int startColumn, final int endColumn)
555 throws OutOfRangeException, NumberIsTooSmallException {
556 checkSubMatrixIndex(startRow, endRow, startColumn, endColumn);
557 visitor.start(getRowDimension(), getColumnDimension(),
558 startRow, endRow, startColumn, endColumn);
559 for (int j = startColumn; j <= endColumn; ++j) {
560 for (int i = startRow; i <= endRow; ++i) {
561 final T[] rowI = data[i];
562 rowI[j] = visitor.visit(i, j, rowI[j]);
563 }
564 }
565 return visitor.end();
566 }
567
568
569 @Override
570 public T walkInColumnOrder(final FieldMatrixPreservingVisitor<T> visitor,
571 final int startRow, final int endRow,
572 final int startColumn, final int endColumn)
573 throws OutOfRangeException, NumberIsTooSmallException {
574 checkSubMatrixIndex(startRow, endRow, startColumn, endColumn);
575 visitor.start(getRowDimension(), getColumnDimension(),
576 startRow, endRow, startColumn, endColumn);
577 for (int j = startColumn; j <= endColumn; ++j) {
578 for (int i = startRow; i <= endRow; ++i) {
579 visitor.visit(i, j, data[i][j]);
580 }
581 }
582 return visitor.end();
583 }
584
585
586
587
588
589
590 private T[][] copyOut() {
591 final int nRows = this.getRowDimension();
592 final T[][] out = MathArrays.buildArray(getField(), nRows, getColumnDimension());
593
594 for (int i = 0; i < nRows; i++) {
595 System.arraycopy(data[i], 0, out[i], 0, data[i].length);
596 }
597 return out;
598 }
599
600
601
602
603
604
605
606
607
608 private void copyIn(final T[][] in)
609 throws NullArgumentException, NoDataException,
610 DimensionMismatchException {
611 setSubMatrix(in, 0, 0);
612 }
613 }