~ubuntu-branches/ubuntu/quantal/commons-math/quantal

« back to all changes in this revision

Viewing changes to src/main/java/org/apache/commons/math/linear/AbstractFieldMatrix.java

  • Committer: Bazaar Package Importer
  • Author(s): Damien Raude-Morvan
  • Date: 2010-04-05 23:33:02 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20100405233302-gpqlceked76nw28a
Tags: 2.1-1
* New upstream release.
* Bump Standards-Version to 3.8.4: no changes needed
* Bump debhelper to >= 7
* Switch to 3.0 (quilt) source format:
  - Remove B-D on quilt
  - Add d/source/format
  - Remove d/README.source

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
 * matrix elements. Derived class can provide faster implementations. </p>
31
31
 *
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) $
34
34
 * @since 2.0
35
35
 */
36
36
public abstract class AbstractFieldMatrix<T extends FieldElement<T>> implements FieldMatrix<T> {
37
 
    
 
37
 
38
38
    /** Field to which the elements belong. */
39
39
    private final Field<T> field;
40
40
 
41
41
    /**
 
42
     * Constructor for use with Serializable
 
43
     */
 
44
    protected AbstractFieldMatrix() {
 
45
        field = null;
 
46
    }
 
47
 
 
48
    /**
 
49
     * Creates a matrix with no data
 
50
     * @param field field to which the elements belong
 
51
     */
 
52
    protected AbstractFieldMatrix(final Field<T> field) {
 
53
        this.field = field;
 
54
    }
 
55
 
 
56
    /**
 
57
     * Create a new FieldMatrix<T> with the supplied row and column dimensions.
 
58
     *
 
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
 
63
     */
 
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)",
 
70
                    rowDimension);
 
71
        }
 
72
        if (columnDimension <= 0) {
 
73
            throw MathRuntimeException.createIllegalArgumentException(
 
74
                    "invalid column dimension {0} (must be positive)",
 
75
                    columnDimension);
 
76
        }
 
77
        this.field = field;
 
78
    }
 
79
 
 
80
    /**
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");
52
91
        }
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");
55
94
        }
56
95
        return d[0][0].getField();
57
96
    }
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");
70
109
        }
71
110
        return d[0].getField();
72
111
    }
87
126
                                                                  final int rows,
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);
92
131
        }
93
132
        T[][] array =
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
109
148
     */
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());
115
154
        return array;
116
155
    }
117
156
 
118
 
    /**
119
 
     * Constructor for use with Serializable
120
 
     */
121
 
    protected AbstractFieldMatrix() {
122
 
        field = null;
123
 
    }
124
 
    
125
 
    /**
126
 
     * Creates a matrix with no data
127
 
     * @param field field to which the elements belong
128
 
     */
129
 
    protected AbstractFieldMatrix(final Field<T> field) {
130
 
        this.field = field;
131
 
    }
132
 
 
133
 
    /**
134
 
     * Create a new FieldMatrix<T> with the supplied row and column dimensions.
135
 
     *
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
140
 
     */
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)",
147
 
                    rowDimension);
148
 
        }
149
 
        if (columnDimension <= 0) {
150
 
            throw MathRuntimeException.createIllegalArgumentException(
151
 
                    "invalid column dimension {0} (must be positive)",
152
 
                    columnDimension);
153
 
        }
154
 
        this.field = field;
155
 
    }
156
 
 
157
157
    /** {@inheritDoc} */
158
158
    public Field<T> getField() {
159
159
        return field;
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)));
181
 
            }  
 
181
            }
182
182
        }
183
183
 
184
184
        return out;
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)));
200
 
            }  
 
200
            }
201
201
        }
202
202
 
203
203
        return out;
324
324
 
325
325
        return subMatrix;
326
326
 
327
 
    } 
 
327
    }
328
328
 
329
329
    /** {@inheritDoc} */
330
330
    public void copySubMatrix(final int startRow, final int endRow,
396
396
    }
397
397
 
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 {
401
401
 
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");
405
405
        }
406
406
 
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");
410
410
        }
411
411
 
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);
417
417
            }
418
418
        }
419
419
 
426
426
            for (int j = 0; j < nCols; ++j) {
427
427
                setEntry(row + i, column + j, subMatrix[i][j]);
428
428
            }
429
 
        } 
 
429
        }
430
430
 
431
431
    }
432
432
 
444
444
        return out;
445
445
 
446
446
    }
447
 
    
 
447
 
448
448
    /** {@inheritDoc} */
449
449
    public void setRowMatrix(final int row, final FieldMatrix<T> matrix)
450
450
        throws MatrixIndexException, InvalidMatrixException {
462
462
        }
463
463
 
464
464
    }
465
 
    
 
465
 
466
466
    /** {@inheritDoc} */
467
467
    public FieldMatrix<T> getColumnMatrix(final int column)
468
468
        throws MatrixIndexException {
495
495
        }
496
496
 
497
497
    }
498
 
    
 
498
 
499
499
    /** {@inheritDoc} */
500
500
    public FieldVector<T> getRowVector(final int row)
501
501
        throws MatrixIndexException {
518
518
        }
519
519
 
520
520
    }
521
 
    
 
521
 
522
522
    /** {@inheritDoc} */
523
523
    public FieldVector<T> getColumnVector(final int column)
524
524
        throws MatrixIndexException {
541
541
        }
542
542
 
543
543
    }
544
 
    
 
544
 
545
545
    /** {@inheritDoc} */
546
546
    public T[] getRow(final int row)
547
547
        throws MatrixIndexException {
573
573
        }
574
574
 
575
575
    }
576
 
    
 
576
 
577
577
    /** {@inheritDoc} */
578
578
    public T[] getColumn(final int column)
579
579
        throws MatrixIndexException {
605
605
        }
606
606
 
607
607
    }
608
 
    
 
608
 
609
609
    /** {@inheritDoc} */
610
610
    public abstract T getEntry(int row, int column)
611
611
        throws MatrixIndexException;
644
644
 
645
645
    /** {@inheritDoc} */
646
646
    public boolean isSquare() {
647
 
        return (getColumnDimension() == getRowDimension());
 
647
        return getColumnDimension() == getRowDimension();
648
648
    }
649
649
 
650
650
    /** {@inheritDoc} */
953
953
                    res.append(",");
954
954
                }
955
955
                res.append(getEntry(i, j));
956
 
            } 
 
956
            }
957
957
            res.append("}");
958
 
        } 
 
958
        }
959
959
 
960
960
        res.append("}");
961
961
        return res.toString();
962
962
 
963
 
    } 
964
 
    
 
963
    }
 
964
 
965
965
    /**
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.
969
 
     * 
 
969
     *
970
970
     * @param object the object to test equality against.
971
971
     * @return true if object equals this
972
972
     */
973
 
    @SuppressWarnings("unchecked")
974
973
    @Override
975
974
    public boolean equals(final Object object) {
976
975
        if (object == this ) {
977
976
            return true;
978
977
        }
979
 
        if (object instanceof FieldMatrix == false) {
 
978
        if (object instanceof FieldMatrix<?> == false) {
980
979
            return false;
981
980
        }
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) {
994
993
        }
995
994
        return true;
996
995
    }
997
 
    
 
996
 
998
997
    /**
999
998
     * Computes a hashcode for the matrix.
1000
 
     * 
 
999
     *
1001
1000
     * @return hashcode for matrix
1002
1001
     */
1003
1002
    @Override
1066
1065
                                           startColumn, endColumn);
1067
1066
        }
1068
1067
 
1069
 
    
 
1068
 
1070
1069
    }
1071
1070
 
1072
1071
    /**