30
30
* matrix elements. Derived class can provide faster implementations. </p>
32
32
* @param <T> the type of the field elements
33
* @version $Revision: 783702 $ $Date: 2009-06-11 04:54:02 -0400 (Thu, 11 Jun 2009) $
33
* @version $Revision: 903046 $ $Date: 2010-01-25 21:07:26 -0500 (Mon, 25 Jan 2010) $
36
36
public abstract class AbstractFieldMatrix<T extends FieldElement<T>> implements FieldMatrix<T> {
38
38
/** Field to which the elements belong. */
39
39
private final Field<T> field;
42
* Constructor for use with Serializable
44
protected AbstractFieldMatrix() {
49
* Creates a matrix with no data
50
* @param field field to which the elements belong
52
protected AbstractFieldMatrix(final Field<T> field) {
57
* Create a new FieldMatrix<T> with the supplied row and column dimensions.
59
* @param field field to which the elements belong
60
* @param rowDimension the number of rows in the new matrix
61
* @param columnDimension the number of columns in the new matrix
62
* @throws IllegalArgumentException if row or column dimension is not positive
64
protected AbstractFieldMatrix(final Field<T> field,
65
final int rowDimension, final int columnDimension)
66
throws IllegalArgumentException {
67
if (rowDimension <= 0 ) {
68
throw MathRuntimeException.createIllegalArgumentException(
69
"invalid row dimension {0} (must be positive)",
72
if (columnDimension <= 0) {
73
throw MathRuntimeException.createIllegalArgumentException(
74
"invalid column dimension {0} (must be positive)",
42
81
* Get the elements type from an array.
43
82
* @param <T> the type of the field elements
44
83
* @param d data array
48
87
protected static <T extends FieldElement<T>> Field<T> extractField(final T[][] d)
49
88
throws IllegalArgumentException {
50
89
if (d.length == 0) {
51
throw MathRuntimeException.createIllegalArgumentException("matrix must have at least one row");
90
throw MathRuntimeException.createIllegalArgumentException("matrix must have at least one row");
53
92
if (d[0].length == 0) {
54
throw MathRuntimeException.createIllegalArgumentException("matrix must have at least one column");
93
throw MathRuntimeException.createIllegalArgumentException("matrix must have at least one column");
56
95
return d[0][0].getField();
66
105
protected static <T extends FieldElement<T>> Field<T> extractField(final T[] d)
67
106
throws IllegalArgumentException {
68
107
if (d.length == 0) {
69
throw MathRuntimeException.createIllegalArgumentException("matrix must have at least one row");
108
throw MathRuntimeException.createIllegalArgumentException("matrix must have at least one row");
71
110
return d[0].getField();
88
127
final int columns) {
89
128
if (columns < 0) {
90
T[] dummyRow = (T[]) Array.newInstance(field.getZero().getClass(), 0);
91
return (T[][]) Array.newInstance(dummyRow.getClass(), rows);
129
T[] dummyRow = (T[]) Array.newInstance(field.getZero().getClass(), 0);
130
return (T[][]) Array.newInstance(dummyRow.getClass(), rows);
94
133
(T[][]) Array.newInstance(field.getZero().getClass(), new int[] { rows, columns });
107
146
* @param length of the array
108
147
* @return a new array
110
@SuppressWarnings("unchecked")
111
149
protected static <T extends FieldElement<T>> T[] buildArray(final Field<T> field,
112
150
final int length) {
151
@SuppressWarnings("unchecked") // OK because field must be correct class
113
152
T[] array = (T[]) Array.newInstance(field.getZero().getClass(), length);
114
153
Arrays.fill(array, field.getZero());
119
* Constructor for use with Serializable
121
protected AbstractFieldMatrix() {
126
* Creates a matrix with no data
127
* @param field field to which the elements belong
129
protected AbstractFieldMatrix(final Field<T> field) {
134
* Create a new FieldMatrix<T> with the supplied row and column dimensions.
136
* @param field field to which the elements belong
137
* @param rowDimension the number of rows in the new matrix
138
* @param columnDimension the number of columns in the new matrix
139
* @throws IllegalArgumentException if row or column dimension is not positive
141
protected AbstractFieldMatrix(final Field<T> field,
142
final int rowDimension, final int columnDimension)
143
throws IllegalArgumentException {
144
if (rowDimension <= 0 ) {
145
throw MathRuntimeException.createIllegalArgumentException(
146
"invalid row dimension {0} (must be positive)",
149
if (columnDimension <= 0) {
150
throw MathRuntimeException.createIllegalArgumentException(
151
"invalid column dimension {0} (must be positive)",
157
157
/** {@inheritDoc} */
158
158
public Field<T> getField() {
178
178
for (int row = 0; row < rowCount; ++row) {
179
179
for (int col = 0; col < columnCount; ++col) {
180
180
out.setEntry(row, col, getEntry(row, col).add(m.getEntry(row, col)));
197
197
for (int row = 0; row < rowCount; ++row) {
198
198
for (int col = 0; col < columnCount; ++col) {
199
199
out.setEntry(row, col, getEntry(row, col).subtract(m.getEntry(row, col)));
398
398
/** {@inheritDoc} */
399
public void setSubMatrix(final T[][] subMatrix, final int row, final int column)
399
public void setSubMatrix(final T[][] subMatrix, final int row, final int column)
400
400
throws MatrixIndexException {
402
402
final int nRows = subMatrix.length;
403
403
if (nRows == 0) {
404
throw MathRuntimeException.createIllegalArgumentException("matrix must have at least one row");
404
throw MathRuntimeException.createIllegalArgumentException("matrix must have at least one row");
407
407
final int nCols = subMatrix[0].length;
408
408
if (nCols == 0) {
409
throw MathRuntimeException.createIllegalArgumentException("matrix must have at least one column");
409
throw MathRuntimeException.createIllegalArgumentException("matrix must have at least one column");
412
412
for (int r = 1; r < nRows; ++r) {
413
413
if (subMatrix[r].length != nCols) {
414
414
throw MathRuntimeException.createIllegalArgumentException(
415
415
"some rows have length {0} while others have length {1}",
416
nCols, subMatrix[r].length);
416
nCols, subMatrix[r].length);
955
955
res.append(getEntry(i, j));
961
961
return res.toString();
966
966
* Returns true iff <code>object</code> is a
967
967
* <code>FieldMatrix</code> instance with the same dimensions as this
968
968
* and all corresponding matrix entries are equal.
970
970
* @param object the object to test equality against.
971
971
* @return true if object equals this
973
@SuppressWarnings("unchecked")
975
974
public boolean equals(final Object object) {
976
975
if (object == this ) {
979
if (object instanceof FieldMatrix == false) {
978
if (object instanceof FieldMatrix<?> == false) {
982
FieldMatrix<T> m = (FieldMatrix<T>) object;
981
FieldMatrix<?> m = (FieldMatrix<?>) object;
983
982
final int nRows = getRowDimension();
984
983
final int nCols = getColumnDimension();
985
984
if (m.getColumnDimension() != nCols || m.getRowDimension() != nRows) {