Skip to content

Commit 58b32bc

Browse files
author
Luc Maisonobe
committed
Set up array building methods for generic types.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1449528 13f79535-47bb-0310-9956-ffa450edef68
1 parent 9b5247c commit 58b32bc

6 files changed

Lines changed: 69 additions & 133 deletions

File tree

src/main/java/org/apache/commons/math3/linear/AbstractFieldMatrix.java

Lines changed: 10 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,19 @@
1717

1818
package org.apache.commons.math3.linear;
1919

20-
import java.lang.reflect.Array;
2120
import java.util.ArrayList;
22-
import java.util.Arrays;
2321

2422
import org.apache.commons.math3.Field;
2523
import org.apache.commons.math3.FieldElement;
2624
import org.apache.commons.math3.exception.DimensionMismatchException;
2725
import org.apache.commons.math3.exception.NoDataException;
2826
import org.apache.commons.math3.exception.NotPositiveException;
29-
import org.apache.commons.math3.exception.OutOfRangeException;
30-
import org.apache.commons.math3.exception.NumberIsTooSmallException;
3127
import org.apache.commons.math3.exception.NotStrictlyPositiveException;
3228
import org.apache.commons.math3.exception.NullArgumentException;
29+
import org.apache.commons.math3.exception.NumberIsTooSmallException;
30+
import org.apache.commons.math3.exception.OutOfRangeException;
3331
import org.apache.commons.math3.exception.util.LocalizedFormats;
32+
import org.apache.commons.math3.util.MathArrays;
3433

3534
/**
3635
* Basic implementation of {@link FieldMatrix} methods regardless of the underlying storage.
@@ -125,50 +124,6 @@ protected static <T extends FieldElement<T>> Field<T> extractField(final T[] d)
125124
return d[0].getField();
126125
}
127126

128-
/** Build an array of elements.
129-
* <p>
130-
* Complete arrays are filled with field.getZero()
131-
* </p>
132-
* @param <T> Type of the field elements
133-
* @param field field to which array elements belong
134-
* @param rows number of rows
135-
* @param columns number of columns (may be negative to build partial
136-
* arrays in the same way <code>new Field[rows][]</code> works)
137-
* @return a new array
138-
*/
139-
@SuppressWarnings("unchecked")
140-
protected static <T extends FieldElement<T>> T[][] buildArray(final Field<T> field,
141-
final int rows,
142-
final int columns) {
143-
if (columns < 0) {
144-
T[] dummyRow = (T[]) Array.newInstance(field.getRuntimeClass(), 0);
145-
return (T[][]) Array.newInstance(dummyRow.getClass(), rows);
146-
}
147-
T[][] array =
148-
(T[][]) Array.newInstance(field.getRuntimeClass(), new int[] { rows, columns });
149-
for (int i = 0; i < array.length; ++i) {
150-
Arrays.fill(array[i], field.getZero());
151-
}
152-
return array;
153-
}
154-
155-
/** Build an array of elements.
156-
* <p>
157-
* Arrays are filled with field.getZero()
158-
* </p>
159-
* @param <T> the type of the field elements
160-
* @param field field to which array elements belong
161-
* @param length of the array
162-
* @return a new array
163-
*/
164-
protected static <T extends FieldElement<T>> T[] buildArray(final Field<T> field,
165-
final int length) {
166-
@SuppressWarnings("unchecked") // OK because field must be correct class
167-
T[] array = (T[]) Array.newInstance(field.getRuntimeClass(), length);
168-
Arrays.fill(array, field.getZero());
169-
return array;
170-
}
171-
172127
/** {@inheritDoc} */
173128
public Field<T> getField() {
174129
return field;
@@ -337,7 +292,7 @@ public FieldMatrix<T> power(final int p) throws NonSquareMatrixException,
337292

338293
/** {@inheritDoc} */
339294
public T[][] getData() {
340-
final T[][] data = buildArray(field, getRowDimension(), getColumnDimension());
295+
final T[][] data = MathArrays.buildArray(field, getRowDimension(), getColumnDimension());
341296

342297
for (int i = 0; i < data.length; ++i) {
343298
final T[] dataI = data[i];
@@ -606,7 +561,7 @@ public void setColumnVector(final int column, final FieldVector<T> vector)
606561
public T[] getRow(final int row) throws OutOfRangeException {
607562
checkRowIndex(row);
608563
final int nCols = getColumnDimension();
609-
final T[] out = buildArray(field, nCols);
564+
final T[] out = MathArrays.buildArray(field, nCols);
610565
for (int i = 0; i < nCols; ++i) {
611566
out[i] = getEntry(row, i);
612567
}
@@ -633,7 +588,7 @@ public void setRow(final int row, final T[] array)
633588
public T[] getColumn(final int column) throws OutOfRangeException {
634589
checkColumnIndex(column);
635590
final int nRows = getRowDimension();
636-
final T[] out = buildArray(field, nRows);
591+
final T[] out = MathArrays.buildArray(field, nRows);
637592
for (int i = 0; i < nRows; ++i) {
638593
out[i] = getEntry(i, column);
639594
}
@@ -717,7 +672,7 @@ public T[] operate(final T[] v) throws DimensionMismatchException {
717672
throw new DimensionMismatchException(v.length, nCols);
718673
}
719674

720-
final T[] out = buildArray(field, nRows);
675+
final T[] out = MathArrays.buildArray(field, nRows);
721676
for (int row = 0; row < nRows; ++row) {
722677
T sum = field.getZero();
723678
for (int i = 0; i < nCols; ++i) {
@@ -741,7 +696,7 @@ public FieldVector<T> operate(final FieldVector<T> v)
741696
throw new DimensionMismatchException(v.getDimension(), nCols);
742697
}
743698

744-
final T[] out = buildArray(field, nRows);
699+
final T[] out = MathArrays.buildArray(field, nRows);
745700
for (int row = 0; row < nRows; ++row) {
746701
T sum = field.getZero();
747702
for (int i = 0; i < nCols; ++i) {
@@ -763,7 +718,7 @@ public T[] preMultiply(final T[] v) throws DimensionMismatchException {
763718
throw new DimensionMismatchException(v.length, nRows);
764719
}
765720

766-
final T[] out = buildArray(field, nCols);
721+
final T[] out = MathArrays.buildArray(field, nCols);
767722
for (int col = 0; col < nCols; ++col) {
768723
T sum = field.getZero();
769724
for (int i = 0; i < nRows; ++i) {
@@ -787,7 +742,7 @@ public FieldVector<T> preMultiply(final FieldVector<T> v)
787742
throw new DimensionMismatchException(v.getDimension(), nRows);
788743
}
789744

790-
final T[] out = buildArray(field, nCols);
745+
final T[] out = MathArrays.buildArray(field, nCols);
791746
for (int col = 0; col < nCols; ++col) {
792747
T sum = field.getZero();
793748
for (int i = 0; i < nRows; ++i) {

src/main/java/org/apache/commons/math3/linear/Array2DRowFieldMatrix.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.apache.commons.math3.exception.NumberIsTooSmallException;
3030
import org.apache.commons.math3.exception.OutOfRangeException;
3131
import org.apache.commons.math3.exception.util.LocalizedFormats;
32+
import org.apache.commons.math3.util.MathArrays;
3233
import org.apache.commons.math3.util.MathUtils;
3334

3435
/**
@@ -70,7 +71,7 @@ public Array2DRowFieldMatrix(final Field<T> field, final int rowDimension,
7071
final int columnDimension)
7172
throws NotStrictlyPositiveException {
7273
super(field, rowDimension, columnDimension);
73-
data = buildArray(field, rowDimension, columnDimension);
74+
data = MathArrays.buildArray(field, rowDimension, columnDimension);
7475
}
7576

7677
/**
@@ -197,7 +198,7 @@ public Array2DRowFieldMatrix(final T[] v) throws NoDataException {
197198
public Array2DRowFieldMatrix(final Field<T> field, final T[] v) {
198199
super(field);
199200
final int nRows = v.length;
200-
data = buildArray(getField(), nRows, 1);
201+
data = MathArrays.buildArray(getField(), nRows, 1);
201202
for (int row = 0; row < nRows; row++) {
202203
data[row][0] = v[row];
203204
}
@@ -232,7 +233,7 @@ public Array2DRowFieldMatrix<T> add(final Array2DRowFieldMatrix<T> m)
232233

233234
final int rowCount = getRowDimension();
234235
final int columnCount = getColumnDimension();
235-
final T[][] outData = buildArray(getField(), rowCount, columnCount);
236+
final T[][] outData = MathArrays.buildArray(getField(), rowCount, columnCount);
236237
for (int row = 0; row < rowCount; row++) {
237238
final T[] dataRow = data[row];
238239
final T[] mRow = m.data[row];
@@ -260,7 +261,7 @@ public Array2DRowFieldMatrix<T> subtract(final Array2DRowFieldMatrix<T> m)
260261

261262
final int rowCount = getRowDimension();
262263
final int columnCount = getColumnDimension();
263-
final T[][] outData = buildArray(getField(), rowCount, columnCount);
264+
final T[][] outData = MathArrays.buildArray(getField(), rowCount, columnCount);
264265
for (int row = 0; row < rowCount; row++) {
265266
final T[] dataRow = data[row];
266267
final T[] mRow = m.data[row];
@@ -290,7 +291,7 @@ public Array2DRowFieldMatrix<T> multiply(final Array2DRowFieldMatrix<T> m)
290291
final int nRows = this.getRowDimension();
291292
final int nCols = m.getColumnDimension();
292293
final int nSum = this.getColumnDimension();
293-
final T[][] outData = buildArray(getField(), nRows, nCols);
294+
final T[][] outData = MathArrays.buildArray(getField(), nRows, nCols);
294295
for (int row = 0; row < nRows; row++) {
295296
final T[] dataRow = data[row];
296297
final T[] outDataRow = outData[row];
@@ -345,7 +346,7 @@ public void setSubMatrix(final T[][] subMatrix, final int row,
345346
if (nCols == 0) {
346347
throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_COLUMN);
347348
}
348-
data = buildArray(getField(), subMatrix.length, nCols);
349+
data = MathArrays.buildArray(getField(), subMatrix.length, nCols);
349350
for (int i = 0; i < data.length; ++i) {
350351
if (subMatrix[i].length != nCols) {
351352
throw new DimensionMismatchException(nCols, subMatrix[i].length);
@@ -418,7 +419,7 @@ public T[] operate(final T[] v) throws DimensionMismatchException {
418419
if (v.length != nCols) {
419420
throw new DimensionMismatchException(v.length, nCols);
420421
}
421-
final T[] out = buildArray(getField(), nRows);
422+
final T[] out = MathArrays.buildArray(getField(), nRows);
422423
for (int row = 0; row < nRows; row++) {
423424
final T[] dataRow = data[row];
424425
T sum = getField().getZero();
@@ -439,7 +440,7 @@ public T[] preMultiply(final T[] v) throws DimensionMismatchException {
439440
throw new DimensionMismatchException(v.length, nRows);
440441
}
441442

442-
final T[] out = buildArray(getField(), nCols);
443+
final T[] out = MathArrays.buildArray(getField(), nCols);
443444
for (int col = 0; col < nCols; ++col) {
444445
T sum = getField().getZero();
445446
for (int i = 0; i < nRows; ++i) {
@@ -588,7 +589,7 @@ public T walkInColumnOrder(final FieldMatrixPreservingVisitor<T> visitor,
588589
*/
589590
private T[][] copyOut() {
590591
final int nRows = this.getRowDimension();
591-
final T[][] out = buildArray(getField(), nRows, getColumnDimension());
592+
final T[][] out = MathArrays.buildArray(getField(), nRows, getColumnDimension());
592593
// can't copy 2-d array in one shot, otherwise get row references
593594
for (int i = 0; i < nRows; i++) {
594595
System.arraycopy(data[i], 0, out[i], 0, data[i].length);

0 commit comments

Comments
 (0)