1717
1818package org .apache .commons .math3 .linear ;
1919
20- import java .lang .reflect .Array ;
2120import java .util .ArrayList ;
22- import java .util .Arrays ;
2321
2422import org .apache .commons .math3 .Field ;
2523import org .apache .commons .math3 .FieldElement ;
2624import org .apache .commons .math3 .exception .DimensionMismatchException ;
2725import org .apache .commons .math3 .exception .NoDataException ;
2826import org .apache .commons .math3 .exception .NotPositiveException ;
29- import org .apache .commons .math3 .exception .OutOfRangeException ;
30- import org .apache .commons .math3 .exception .NumberIsTooSmallException ;
3127import org .apache .commons .math3 .exception .NotStrictlyPositiveException ;
3228import org .apache .commons .math3 .exception .NullArgumentException ;
29+ import org .apache .commons .math3 .exception .NumberIsTooSmallException ;
30+ import org .apache .commons .math3 .exception .OutOfRangeException ;
3331import 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 ) {
0 commit comments