Author: luc
Date: Sun Feb 24 19:06:20 2013
New Revision: 1449528

URL: http://svn.apache.org/r1449528
Log:
Set up array building methods for generic types.

Modified:
    
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/AbstractFieldMatrix.java
    
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/Array2DRowFieldMatrix.java
    
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/ArrayFieldVector.java
    
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/BlockFieldMatrix.java
    
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/FieldLUDecomposition.java
    
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/MatrixUtils.java

Modified: 
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/AbstractFieldMatrix.java
URL: 
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/AbstractFieldMatrix.java?rev=1449528&r1=1449527&r2=1449528&view=diff
==============================================================================
--- 
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/AbstractFieldMatrix.java
 (original)
+++ 
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/AbstractFieldMatrix.java
 Sun Feb 24 19:06:20 2013
@@ -17,20 +17,19 @@
 
 package org.apache.commons.math3.linear;
 
-import java.lang.reflect.Array;
 import java.util.ArrayList;
-import java.util.Arrays;
 
 import org.apache.commons.math3.Field;
 import org.apache.commons.math3.FieldElement;
 import org.apache.commons.math3.exception.DimensionMismatchException;
 import org.apache.commons.math3.exception.NoDataException;
 import org.apache.commons.math3.exception.NotPositiveException;
-import org.apache.commons.math3.exception.OutOfRangeException;
-import org.apache.commons.math3.exception.NumberIsTooSmallException;
 import org.apache.commons.math3.exception.NotStrictlyPositiveException;
 import org.apache.commons.math3.exception.NullArgumentException;
+import org.apache.commons.math3.exception.NumberIsTooSmallException;
+import org.apache.commons.math3.exception.OutOfRangeException;
 import org.apache.commons.math3.exception.util.LocalizedFormats;
+import org.apache.commons.math3.util.MathArrays;
 
 /**
  * Basic implementation of {@link FieldMatrix} methods regardless of the 
underlying storage.
@@ -125,50 +124,6 @@ public abstract class AbstractFieldMatri
         return d[0].getField();
     }
 
-    /** Build an array of elements.
-     * <p>
-     * Complete arrays are filled with field.getZero()
-     * </p>
-     * @param <T> Type of the field elements
-     * @param field field to which array elements belong
-     * @param rows number of rows
-     * @param columns number of columns (may be negative to build partial
-     * arrays in the same way <code>new Field[rows][]</code> works)
-     * @return a new array
-     */
-    @SuppressWarnings("unchecked")
-    protected static <T extends FieldElement<T>> T[][] buildArray(final 
Field<T> field,
-                                                                  final int 
rows,
-                                                                  final int 
columns) {
-        if (columns < 0) {
-            T[] dummyRow = (T[]) Array.newInstance(field.getRuntimeClass(), 0);
-            return (T[][]) Array.newInstance(dummyRow.getClass(), rows);
-        }
-        T[][] array =
-            (T[][]) Array.newInstance(field.getRuntimeClass(), new int[] { 
rows, columns });
-        for (int i = 0; i < array.length; ++i) {
-            Arrays.fill(array[i], field.getZero());
-        }
-        return array;
-    }
-
-    /** Build an array of elements.
-     * <p>
-     * Arrays are filled with field.getZero()
-     * </p>
-     * @param <T> the type of the field elements
-     * @param field field to which array elements belong
-     * @param length of the array
-     * @return a new array
-     */
-    protected static <T extends FieldElement<T>> T[] buildArray(final Field<T> 
field,
-                                                                final int 
length) {
-        @SuppressWarnings("unchecked") // OK because field must be correct 
class
-        T[] array = (T[]) Array.newInstance(field.getRuntimeClass(), length);
-        Arrays.fill(array, field.getZero());
-        return array;
-    }
-
     /** {@inheritDoc} */
     public Field<T> getField() {
         return field;
@@ -337,7 +292,7 @@ public abstract class AbstractFieldMatri
 
     /** {@inheritDoc} */
     public T[][] getData() {
-        final T[][] data = buildArray(field, getRowDimension(), 
getColumnDimension());
+        final T[][] data = MathArrays.buildArray(field, getRowDimension(), 
getColumnDimension());
 
         for (int i = 0; i < data.length; ++i) {
             final T[] dataI = data[i];
@@ -606,7 +561,7 @@ public abstract class AbstractFieldMatri
     public T[] getRow(final int row) throws OutOfRangeException {
         checkRowIndex(row);
         final int nCols = getColumnDimension();
-        final T[] out = buildArray(field, nCols);
+        final T[] out = MathArrays.buildArray(field, nCols);
         for (int i = 0; i < nCols; ++i) {
             out[i] = getEntry(row, i);
         }
@@ -633,7 +588,7 @@ public abstract class AbstractFieldMatri
     public T[] getColumn(final int column) throws OutOfRangeException {
         checkColumnIndex(column);
         final int nRows = getRowDimension();
-        final T[] out = buildArray(field, nRows);
+        final T[] out = MathArrays.buildArray(field, nRows);
         for (int i = 0; i < nRows; ++i) {
             out[i] = getEntry(i, column);
         }
@@ -717,7 +672,7 @@ public abstract class AbstractFieldMatri
             throw new DimensionMismatchException(v.length, nCols);
         }
 
-        final T[] out = buildArray(field, nRows);
+        final T[] out = MathArrays.buildArray(field, nRows);
         for (int row = 0; row < nRows; ++row) {
             T sum = field.getZero();
             for (int i = 0; i < nCols; ++i) {
@@ -741,7 +696,7 @@ public abstract class AbstractFieldMatri
                 throw new DimensionMismatchException(v.getDimension(), nCols);
             }
 
-            final T[] out = buildArray(field, nRows);
+            final T[] out = MathArrays.buildArray(field, nRows);
             for (int row = 0; row < nRows; ++row) {
                 T sum = field.getZero();
                 for (int i = 0; i < nCols; ++i) {
@@ -763,7 +718,7 @@ public abstract class AbstractFieldMatri
             throw new DimensionMismatchException(v.length, nRows);
         }
 
-        final T[] out = buildArray(field, nCols);
+        final T[] out = MathArrays.buildArray(field, nCols);
         for (int col = 0; col < nCols; ++col) {
             T sum = field.getZero();
             for (int i = 0; i < nRows; ++i) {
@@ -787,7 +742,7 @@ public abstract class AbstractFieldMatri
                 throw new DimensionMismatchException(v.getDimension(), nRows);
             }
 
-            final T[] out = buildArray(field, nCols);
+            final T[] out = MathArrays.buildArray(field, nCols);
             for (int col = 0; col < nCols; ++col) {
                 T sum = field.getZero();
                 for (int i = 0; i < nRows; ++i) {

Modified: 
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/Array2DRowFieldMatrix.java
URL: 
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/Array2DRowFieldMatrix.java?rev=1449528&r1=1449527&r2=1449528&view=diff
==============================================================================
--- 
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/Array2DRowFieldMatrix.java
 (original)
+++ 
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/Array2DRowFieldMatrix.java
 Sun Feb 24 19:06:20 2013
@@ -29,6 +29,7 @@ import org.apache.commons.math3.exceptio
 import org.apache.commons.math3.exception.NumberIsTooSmallException;
 import org.apache.commons.math3.exception.OutOfRangeException;
 import org.apache.commons.math3.exception.util.LocalizedFormats;
+import org.apache.commons.math3.util.MathArrays;
 import org.apache.commons.math3.util.MathUtils;
 
 /**
@@ -70,7 +71,7 @@ public class Array2DRowFieldMatrix<T ext
                                  final int columnDimension)
         throws NotStrictlyPositiveException {
         super(field, rowDimension, columnDimension);
-        data = buildArray(field, rowDimension, columnDimension);
+        data = MathArrays.buildArray(field, rowDimension, columnDimension);
     }
 
     /**
@@ -197,7 +198,7 @@ public class Array2DRowFieldMatrix<T ext
     public Array2DRowFieldMatrix(final Field<T> field, final T[] v) {
         super(field);
         final int nRows = v.length;
-        data = buildArray(getField(), nRows, 1);
+        data = MathArrays.buildArray(getField(), nRows, 1);
         for (int row = 0; row < nRows; row++) {
             data[row][0] = v[row];
         }
@@ -232,7 +233,7 @@ public class Array2DRowFieldMatrix<T ext
 
         final int rowCount    = getRowDimension();
         final int columnCount = getColumnDimension();
-        final T[][] outData = buildArray(getField(), rowCount, columnCount);
+        final T[][] outData = MathArrays.buildArray(getField(), rowCount, 
columnCount);
         for (int row = 0; row < rowCount; row++) {
             final T[] dataRow    = data[row];
             final T[] mRow       = m.data[row];
@@ -260,7 +261,7 @@ public class Array2DRowFieldMatrix<T ext
 
         final int rowCount    = getRowDimension();
         final int columnCount = getColumnDimension();
-        final T[][] outData = buildArray(getField(), rowCount, columnCount);
+        final T[][] outData = MathArrays.buildArray(getField(), rowCount, 
columnCount);
         for (int row = 0; row < rowCount; row++) {
             final T[] dataRow    = data[row];
             final T[] mRow       = m.data[row];
@@ -290,7 +291,7 @@ public class Array2DRowFieldMatrix<T ext
         final int nRows = this.getRowDimension();
         final int nCols = m.getColumnDimension();
         final int nSum = this.getColumnDimension();
-        final T[][] outData = buildArray(getField(), nRows, nCols);
+        final T[][] outData = MathArrays.buildArray(getField(), nRows, nCols);
         for (int row = 0; row < nRows; row++) {
             final T[] dataRow    = data[row];
             final T[] outDataRow = outData[row];
@@ -345,7 +346,7 @@ public class Array2DRowFieldMatrix<T ext
             if (nCols == 0) {
                 throw new 
NoDataException(LocalizedFormats.AT_LEAST_ONE_COLUMN);
             }
-            data = buildArray(getField(), subMatrix.length, nCols);
+            data = MathArrays.buildArray(getField(), subMatrix.length, nCols);
             for (int i = 0; i < data.length; ++i) {
                 if (subMatrix[i].length != nCols) {
                     throw new DimensionMismatchException(nCols, 
subMatrix[i].length);
@@ -418,7 +419,7 @@ public class Array2DRowFieldMatrix<T ext
         if (v.length != nCols) {
             throw new DimensionMismatchException(v.length, nCols);
         }
-        final T[] out = buildArray(getField(), nRows);
+        final T[] out = MathArrays.buildArray(getField(), nRows);
         for (int row = 0; row < nRows; row++) {
             final T[] dataRow = data[row];
             T sum = getField().getZero();
@@ -439,7 +440,7 @@ public class Array2DRowFieldMatrix<T ext
             throw new DimensionMismatchException(v.length, nRows);
         }
 
-        final T[] out = buildArray(getField(), nCols);
+        final T[] out = MathArrays.buildArray(getField(), nCols);
         for (int col = 0; col < nCols; ++col) {
             T sum = getField().getZero();
             for (int i = 0; i < nRows; ++i) {
@@ -588,7 +589,7 @@ public class Array2DRowFieldMatrix<T ext
      */
     private T[][] copyOut() {
         final int nRows = this.getRowDimension();
-        final T[][] out = buildArray(getField(), nRows, getColumnDimension());
+        final T[][] out = MathArrays.buildArray(getField(), nRows, 
getColumnDimension());
         // can't copy 2-d array in one shot, otherwise get row references
         for (int i = 0; i < nRows; i++) {
             System.arraycopy(data[i], 0, out[i], 0, data[i].length);

Modified: 
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/ArrayFieldVector.java
URL: 
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/ArrayFieldVector.java?rev=1449528&r1=1449527&r2=1449528&view=diff
==============================================================================
--- 
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/ArrayFieldVector.java
 (original)
+++ 
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/ArrayFieldVector.java
 Sun Feb 24 19:06:20 2013
@@ -17,19 +17,19 @@
 package org.apache.commons.math3.linear;
 
 import java.io.Serializable;
-import java.lang.reflect.Array;
 import java.util.Arrays;
 
 import org.apache.commons.math3.Field;
 import org.apache.commons.math3.FieldElement;
+import org.apache.commons.math3.exception.DimensionMismatchException;
 import org.apache.commons.math3.exception.MathArithmeticException;
 import org.apache.commons.math3.exception.NotPositiveException;
-import org.apache.commons.math3.exception.ZeroException;
 import org.apache.commons.math3.exception.NullArgumentException;
-import org.apache.commons.math3.exception.OutOfRangeException;
-import org.apache.commons.math3.exception.DimensionMismatchException;
 import org.apache.commons.math3.exception.NumberIsTooLargeException;
+import org.apache.commons.math3.exception.OutOfRangeException;
+import org.apache.commons.math3.exception.ZeroException;
 import org.apache.commons.math3.exception.util.LocalizedFormats;
+import org.apache.commons.math3.util.MathArrays;
 
 /**
  * This class implements the {@link FieldVector} interface with a {@link 
FieldElement} array.
@@ -69,8 +69,7 @@ public class ArrayFieldVector<T extends 
      */
     public ArrayFieldVector(Field<T> field, int size) {
         this.field = field;
-        data = buildArray(size);
-        Arrays.fill(data, field.getZero());
+        this.data  = MathArrays.buildArray(field, size);
     }
 
     /**
@@ -202,7 +201,7 @@ public class ArrayFieldVector<T extends 
             throw new NumberIsTooLargeException(pos + size, d.length, true);
         }
         field = d[0].getField();
-        data = buildArray(size);
+        data = MathArrays.buildArray(field, size);
         System.arraycopy(d, pos, data, 0, size);
     }
 
@@ -226,7 +225,7 @@ public class ArrayFieldVector<T extends 
             throw new NumberIsTooLargeException(pos + size, d.length, true);
         }
         this.field = field;
-        data = buildArray(size);
+        data = MathArrays.buildArray(field, size);
         System.arraycopy(d, pos, data, 0, size);
     }
 
@@ -242,7 +241,7 @@ public class ArrayFieldVector<T extends 
             throw new NullArgumentException();
         }
         field = v.getField();
-        data = buildArray(v.getDimension());
+        data = MathArrays.buildArray(field, v.getDimension());
         for (int i = 0; i < data.length; ++i) {
             data[i] = v.getEntry(i);
         }
@@ -294,7 +293,7 @@ public class ArrayFieldVector<T extends 
             throw new NullArgumentException();
         }
         field = v1.getField();
-        data = buildArray(v1.data.length + v2.data.length);
+        data = MathArrays.buildArray(field, v1.data.length + v2.data.length);
         System.arraycopy(v1.data, 0, data, 0, v1.data.length);
         System.arraycopy(v2.data, 0, data, v1.data.length, v2.data.length);
     }
@@ -313,7 +312,7 @@ public class ArrayFieldVector<T extends 
             throw new NullArgumentException();
         }
         field = v1.getField();
-        data = buildArray(v1.data.length + v2.length);
+        data = MathArrays.buildArray(field, v1.data.length + v2.length);
         System.arraycopy(v1.data, 0, data, 0, v1.data.length);
         System.arraycopy(v2, 0, data, v1.data.length, v2.length);
     }
@@ -332,7 +331,7 @@ public class ArrayFieldVector<T extends 
             throw new NullArgumentException();
         }
         field = v2.getField();
-        data = buildArray(v1.length + v2.data.length);
+        data = MathArrays.buildArray(field, v1.length + v2.data.length);
         System.arraycopy(v1, 0, data, 0, v1.length);
         System.arraycopy(v2.data, 0, data, v1.length, v2.data.length);
     }
@@ -360,7 +359,7 @@ public class ArrayFieldVector<T extends 
         if (v1.length + v2.length == 0) {
             throw new 
ZeroException(LocalizedFormats.VECTOR_MUST_HAVE_AT_LEAST_ONE_ELEMENT);
         }
-        data = buildArray(v1.length + v2.length);
+        data = MathArrays.buildArray(v1[0].getField(), v1.length + v2.length);
         System.arraycopy(v1, 0, data, 0, v1.length);
         System.arraycopy(v2, 0, data, v1.length, v2.length);
         field = data[0].getField();
@@ -385,23 +384,12 @@ public class ArrayFieldVector<T extends 
         if (v1.length + v2.length == 0) {
             throw new 
ZeroException(LocalizedFormats.VECTOR_MUST_HAVE_AT_LEAST_ONE_ELEMENT);
         }
-        data = buildArray(v1.length + v2.length);
+        data = MathArrays.buildArray(field, v1.length + v2.length);
         System.arraycopy(v1, 0, data, 0, v1.length);
         System.arraycopy(v2, 0, data, v1.length, v2.length);
         this.field = field;
     }
 
-    /**
-     * Build an array of elements.
-     *
-     * @param length Size of the array to build.
-     * @return a new array.
-     */
-    @SuppressWarnings("unchecked") // field is of type T
-    private T[] buildArray(final int length) {
-        return (T[]) Array.newInstance(field.getRuntimeClass(), length);
-    }
-
     /** {@inheritDoc} */
     public Field<T> getField() {
         return field;
@@ -419,7 +407,7 @@ public class ArrayFieldVector<T extends 
             return add((ArrayFieldVector<T>) v);
         } catch (ClassCastException cce) {
             checkVectorDimensions(v);
-            T[] out = buildArray(data.length);
+            T[] out = MathArrays.buildArray(field, data.length);
             for (int i = 0; i < data.length; i++) {
                 out[i] = data[i].add(v.getEntry(i));
             }
@@ -437,7 +425,7 @@ public class ArrayFieldVector<T extends 
     public ArrayFieldVector<T> add(ArrayFieldVector<T> v)
         throws DimensionMismatchException {
         checkVectorDimensions(v.data.length);
-        T[] out = buildArray(data.length);
+        T[] out = MathArrays.buildArray(field, data.length);
         for (int i = 0; i < data.length; i++) {
             out[i] = data[i].add(v.data[i]);
         }
@@ -451,7 +439,7 @@ public class ArrayFieldVector<T extends 
             return subtract((ArrayFieldVector<T>) v);
         } catch (ClassCastException cce) {
             checkVectorDimensions(v);
-            T[] out = buildArray(data.length);
+            T[] out = MathArrays.buildArray(field, data.length);
             for (int i = 0; i < data.length; i++) {
                 out[i] = data[i].subtract(v.getEntry(i));
             }
@@ -469,7 +457,7 @@ public class ArrayFieldVector<T extends 
     public ArrayFieldVector<T> subtract(ArrayFieldVector<T> v)
         throws DimensionMismatchException {
         checkVectorDimensions(v.data.length);
-        T[] out = buildArray(data.length);
+        T[] out = MathArrays.buildArray(field, data.length);
         for (int i = 0; i < data.length; i++) {
             out[i] = data[i].subtract(v.data[i]);
         }
@@ -478,7 +466,7 @@ public class ArrayFieldVector<T extends 
 
     /** {@inheritDoc} */
     public FieldVector<T> mapAdd(T d) throws NullArgumentException {
-        T[] out = buildArray(data.length);
+        T[] out = MathArrays.buildArray(field, data.length);
         for (int i = 0; i < data.length; i++) {
             out[i] = data[i].add(d);
         }
@@ -495,7 +483,7 @@ public class ArrayFieldVector<T extends 
 
     /** {@inheritDoc} */
     public FieldVector<T> mapSubtract(T d) throws NullArgumentException {
-        T[] out = buildArray(data.length);
+        T[] out = MathArrays.buildArray(field, data.length);
         for (int i = 0; i < data.length; i++) {
             out[i] = data[i].subtract(d);
         }
@@ -512,7 +500,7 @@ public class ArrayFieldVector<T extends 
 
     /** {@inheritDoc} */
     public FieldVector<T> mapMultiply(T d) throws NullArgumentException {
-        T[] out = buildArray(data.length);
+        T[] out = MathArrays.buildArray(field, data.length);
         for (int i = 0; i < data.length; i++) {
             out[i] = data[i].multiply(d);
         }
@@ -533,7 +521,7 @@ public class ArrayFieldVector<T extends 
         if (d == null) {
             throw new NullArgumentException();
         }
-        T[] out = buildArray(data.length);
+        T[] out = MathArrays.buildArray(field, data.length);
         for (int i = 0; i < data.length; i++) {
             out[i] = data[i].divide(d);
         }
@@ -554,7 +542,7 @@ public class ArrayFieldVector<T extends 
 
     /** {@inheritDoc} */
     public FieldVector<T> mapInv() throws MathArithmeticException {
-        T[] out = buildArray(data.length);
+        T[] out = MathArrays.buildArray(field, data.length);
         final T one = field.getOne();
         for (int i = 0; i < data.length; i++) {
             try {
@@ -586,7 +574,7 @@ public class ArrayFieldVector<T extends 
             return ebeMultiply((ArrayFieldVector<T>) v);
         } catch (ClassCastException cce) {
             checkVectorDimensions(v);
-            T[] out = buildArray(data.length);
+            T[] out = MathArrays.buildArray(field, data.length);
             for (int i = 0; i < data.length; i++) {
                 out[i] = data[i].multiply(v.getEntry(i));
             }
@@ -604,7 +592,7 @@ public class ArrayFieldVector<T extends 
     public ArrayFieldVector<T> ebeMultiply(ArrayFieldVector<T> v)
         throws DimensionMismatchException {
         checkVectorDimensions(v.data.length);
-        T[] out = buildArray(data.length);
+        T[] out = MathArrays.buildArray(field, data.length);
         for (int i = 0; i < data.length; i++) {
             out[i] = data[i].multiply(v.data[i]);
         }
@@ -618,7 +606,7 @@ public class ArrayFieldVector<T extends 
             return ebeDivide((ArrayFieldVector<T>) v);
         } catch (ClassCastException cce) {
             checkVectorDimensions(v);
-            T[] out = buildArray(data.length);
+            T[] out = MathArrays.buildArray(field, data.length);
             for (int i = 0; i < data.length; i++) {
                 try {
                     out[i] = data[i].divide(v.getEntry(i));
@@ -641,7 +629,7 @@ public class ArrayFieldVector<T extends 
     public ArrayFieldVector<T> ebeDivide(ArrayFieldVector<T> v)
         throws DimensionMismatchException, MathArithmeticException {
         checkVectorDimensions(v.data.length);
-        T[] out = buildArray(data.length);
+        T[] out = MathArrays.buildArray(field, data.length);
         for (int i = 0; i < data.length; i++) {
             try {
                 out[i] = data[i].divide(v.data[i]);
@@ -780,7 +768,7 @@ public class ArrayFieldVector<T extends 
 
     /** {@inheritDoc} */
     public FieldVector<T> append(T in) {
-        final T[] out = buildArray(data.length + 1);
+        final T[] out = MathArrays.buildArray(field, data.length + 1);
         System.arraycopy(data, 0, out, 0, data.length);
         out[data.length] = in;
         return new ArrayFieldVector<T>(field, out, false);

Modified: 
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/BlockFieldMatrix.java
URL: 
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/BlockFieldMatrix.java?rev=1449528&r1=1449527&r2=1449528&view=diff
==============================================================================
--- 
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/BlockFieldMatrix.java
 (original)
+++ 
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/BlockFieldMatrix.java
 Sun Feb 24 19:06:20 2013
@@ -29,6 +29,7 @@ import org.apache.commons.math3.exceptio
 import org.apache.commons.math3.exception.OutOfRangeException;
 import org.apache.commons.math3.exception.util.LocalizedFormats;
 import org.apache.commons.math3.util.FastMath;
+import org.apache.commons.math3.util.MathArrays;
 import org.apache.commons.math3.util.MathUtils;
 
 /**
@@ -159,7 +160,7 @@ public class BlockFieldMatrix<T extends 
 
         if (copyArray) {
             // allocate storage blocks, taking care of smaller ones at right 
and bottom
-            blocks = buildArray(getField(), blockRows * blockColumns, -1);
+            blocks = MathArrays.buildArray(getField(), blockRows * 
blockColumns, -1);
         } else {
             // reference existing array
             blocks = blockData;
@@ -222,7 +223,7 @@ public class BlockFieldMatrix<T extends 
 
         // convert array
         final Field<T> field = extractField(rawData);
-        final T[][] blocks = buildArray(field, blockRows * blockColumns, -1);
+        final T[][] blocks = MathArrays.buildArray(field, blockRows * 
blockColumns, -1);
         int blockIndex = 0;
         for (int iBlock = 0; iBlock < blockRows; ++iBlock) {
             final int pStart  = iBlock * BLOCK_SIZE;
@@ -234,7 +235,7 @@ public class BlockFieldMatrix<T extends 
                 final int jWidth = qEnd - qStart;
 
                 // allocate new block
-                final T[] block = buildArray(field, iHeight * jWidth);
+                final T[] block = MathArrays.buildArray(field, iHeight * 
jWidth);
                 blocks[blockIndex] = block;
 
                 // copy data
@@ -271,7 +272,7 @@ public class BlockFieldMatrix<T extends 
         final int blockRows    = (rows    + BLOCK_SIZE - 1) / BLOCK_SIZE;
         final int blockColumns = (columns + BLOCK_SIZE - 1) / BLOCK_SIZE;
 
-        final T[][] blocks = buildArray(field, blockRows * blockColumns, -1);
+        final T[][] blocks = MathArrays.buildArray(field, blockRows * 
blockColumns, -1);
         int blockIndex = 0;
         for (int iBlock = 0; iBlock < blockRows; ++iBlock) {
             final int pStart  = iBlock * BLOCK_SIZE;
@@ -281,7 +282,7 @@ public class BlockFieldMatrix<T extends 
                 final int qStart = jBlock * BLOCK_SIZE;
                 final int qEnd   = FastMath.min(qStart + BLOCK_SIZE, columns);
                 final int jWidth = qEnd - qStart;
-                blocks[blockIndex] = buildArray(field, iHeight * jWidth);
+                blocks[blockIndex] = MathArrays.buildArray(field, iHeight * 
jWidth);
                 ++blockIndex;
             }
         }
@@ -627,7 +628,7 @@ public class BlockFieldMatrix<T extends 
     @Override
     public T[][] getData() {
 
-        final T[][] data = buildArray(getField(), getRowDimension(), 
getColumnDimension());
+        final T[][] data = MathArrays.buildArray(getField(), 
getRowDimension(), getColumnDimension());
         final int lastColumns = columns - (blockColumns - 1) * BLOCK_SIZE;
 
         for (int iBlock = 0; iBlock < blockRows; ++iBlock) {
@@ -1004,7 +1005,7 @@ public class BlockFieldMatrix<T extends 
     public FieldVector<T> getRowVector(final int row)
         throws OutOfRangeException {
         checkRowIndex(row);
-        final T[] outData = buildArray(getField(), columns);
+        final T[] outData = MathArrays.buildArray(getField(), columns);
 
         // perform copy block-wise, to ensure good cache behavior
         final int iBlock  = row / BLOCK_SIZE;
@@ -1036,7 +1037,7 @@ public class BlockFieldMatrix<T extends 
     public FieldVector<T> getColumnVector(final int column)
         throws OutOfRangeException {
         checkColumnIndex(column);
-        final T[] outData = buildArray(getField(), rows);
+        final T[] outData = MathArrays.buildArray(getField(), rows);
 
         // perform copy block-wise, to ensure good cache behavior
         final int jBlock  = column / BLOCK_SIZE;
@@ -1069,7 +1070,7 @@ public class BlockFieldMatrix<T extends 
     @Override
     public T[] getRow(final int row) throws OutOfRangeException {
         checkRowIndex(row);
-        final T[] out = buildArray(getField(), columns);
+        final T[] out = MathArrays.buildArray(getField(), columns);
 
         // perform copy block-wise, to ensure good cache behavior
         final int iBlock  = row / BLOCK_SIZE;
@@ -1111,7 +1112,7 @@ public class BlockFieldMatrix<T extends 
     @Override
     public T[] getColumn(final int column) throws OutOfRangeException {
         checkColumnIndex(column);
-        final T[] out = buildArray(getField(), rows);
+        final T[] out = MathArrays.buildArray(getField(), rows);
 
         // perform copy block-wise, to ensure good cache behavior
         final int jBlock  = column / BLOCK_SIZE;
@@ -1272,7 +1273,7 @@ public class BlockFieldMatrix<T extends 
         if (v.length != columns) {
             throw new DimensionMismatchException(v.length, columns);
         }
-        final T[] out = buildArray(getField(), rows);
+        final T[] out = MathArrays.buildArray(getField(), rows);
         final T zero = getField().getZero();
 
         // perform multiplication block-wise, to ensure good cache behavior
@@ -1314,7 +1315,7 @@ public class BlockFieldMatrix<T extends 
         if (v.length != rows) {
             throw new DimensionMismatchException(v.length, rows);
         }
-        final T[] out = buildArray(getField(), columns);
+        final T[] out = MathArrays.buildArray(getField(), columns);
         final T zero = getField().getZero();
 
         // perform multiplication block-wise, to ensure good cache behavior

Modified: 
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/FieldLUDecomposition.java
URL: 
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/FieldLUDecomposition.java?rev=1449528&r1=1449527&r2=1449528&view=diff
==============================================================================
--- 
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/FieldLUDecomposition.java
 (original)
+++ 
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/FieldLUDecomposition.java
 Sun Feb 24 19:06:20 2013
@@ -17,11 +17,10 @@
 
 package org.apache.commons.math3.linear;
 
-import java.lang.reflect.Array;
-
 import org.apache.commons.math3.Field;
 import org.apache.commons.math3.FieldElement;
 import org.apache.commons.math3.exception.DimensionMismatchException;
+import org.apache.commons.math3.util.MathArrays;
 
 /**
  * Calculates the LUP-decomposition of a square matrix.
@@ -307,10 +306,8 @@ public class FieldLUDecomposition<T exte
                     throw new SingularMatrixException();
                 }
 
-                @SuppressWarnings("unchecked") // field is of type T
-                final T[] bp = (T[]) 
Array.newInstance(field.getRuntimeClass(), m);
-
                 // Apply permutations to b
+                final T[] bp = MathArrays.buildArray(field, m);
                 for (int row = 0; row < m; row++) {
                     bp[row] = b.getEntry(pivot[row]);
                 }
@@ -354,12 +351,8 @@ public class FieldLUDecomposition<T exte
                 throw new SingularMatrixException();
             }
 
-            @SuppressWarnings("unchecked")
-            // field is of type T
-            final T[] bp = (T[]) Array.newInstance(field.getRuntimeClass(),
-                                                   m);
-
             // Apply permutations to b
+            final T[] bp = MathArrays.buildArray(field, m);
             for (int row = 0; row < m; row++) {
                 bp[row] = b.getEntry(pivot[row]);
             }
@@ -397,8 +390,7 @@ public class FieldLUDecomposition<T exte
             final int nColB = b.getColumnDimension();
 
             // Apply permutations to b
-            @SuppressWarnings("unchecked") // field is of type T
-            final T[][] bp = (T[][]) 
Array.newInstance(field.getRuntimeClass(), new int[] { m, nColB });
+            final T[][] bp = MathArrays.buildArray(field, m, nColB);
             for (int row = 0; row < m; row++) {
                 final T[] bpRow = bp[row];
                 final int pRow = pivot[row];

Modified: 
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/MatrixUtils.java
URL: 
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/MatrixUtils.java?rev=1449528&r1=1449527&r2=1449528&view=diff
==============================================================================
--- 
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/MatrixUtils.java
 (original)
+++ 
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/MatrixUtils.java
 Sun Feb 24 19:06:20 2013
@@ -20,22 +20,22 @@ package org.apache.commons.math3.linear;
 import java.io.IOException;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
-import java.lang.reflect.Array;
 import java.util.Arrays;
 
 import org.apache.commons.math3.Field;
 import org.apache.commons.math3.FieldElement;
+import org.apache.commons.math3.exception.DimensionMismatchException;
 import org.apache.commons.math3.exception.MathArithmeticException;
-import org.apache.commons.math3.exception.OutOfRangeException;
 import org.apache.commons.math3.exception.NoDataException;
-import org.apache.commons.math3.exception.NumberIsTooSmallException;
 import org.apache.commons.math3.exception.NullArgumentException;
-import org.apache.commons.math3.exception.DimensionMismatchException;
+import org.apache.commons.math3.exception.NumberIsTooSmallException;
+import org.apache.commons.math3.exception.OutOfRangeException;
 import org.apache.commons.math3.exception.ZeroException;
 import org.apache.commons.math3.exception.util.LocalizedFormats;
 import org.apache.commons.math3.fraction.BigFraction;
 import org.apache.commons.math3.fraction.Fraction;
 import org.apache.commons.math3.util.FastMath;
+import org.apache.commons.math3.util.MathArrays;
 import org.apache.commons.math3.util.Precision;
 
 /**
@@ -194,8 +194,7 @@ public class MatrixUtils {
         createFieldIdentityMatrix(final Field<T> field, final int dimension) {
         final T zero = field.getZero();
         final T one  = field.getOne();
-        @SuppressWarnings("unchecked")
-        final T[][] d = (T[][]) Array.newInstance(field.getRuntimeClass(), new 
int[] { dimension, dimension });
+        final T[][] d = MathArrays.buildArray(field, dimension, dimension);
         for (int row = 0; row < dimension; row++) {
             final T[] dRow = d[row];
             Arrays.fill(dRow, zero);


Reply via email to