Return-Path: Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: (qmail 84259 invoked from network); 20 Apr 2009 20:16:01 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 20 Apr 2009 20:16:01 -0000 Received: (qmail 86733 invoked by uid 500); 20 Apr 2009 20:16:00 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 86647 invoked by uid 500); 20 Apr 2009 20:16:00 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 86633 invoked by uid 99); 20 Apr 2009 20:16:00 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 20 Apr 2009 20:16:00 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 20 Apr 2009 20:15:59 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 89B0A23889D9; Mon, 20 Apr 2009 20:15:39 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r766846 - /commons/proper/math/trunk/src/java/org/apache/commons/math/linear/AbstractFieldMatrix.java Date: Mon, 20 Apr 2009 20:15:39 -0000 To: commits@commons.apache.org From: luc@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20090420201539.89B0A23889D9@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: luc Date: Mon Apr 20 20:15:39 2009 New Revision: 766846 URL: http://svn.apache.org/viewvc?rev=766846&view=rev Log: converted buildArray to static, so it can be used from other static methods Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/linear/AbstractFieldMatrix.java Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/linear/AbstractFieldMatrix.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/linear/AbstractFieldMatrix.java?rev=766846&r1=766845&r2=766846&view=diff ============================================================================== --- commons/proper/math/trunk/src/java/org/apache/commons/math/linear/AbstractFieldMatrix.java (original) +++ commons/proper/math/trunk/src/java/org/apache/commons/math/linear/AbstractFieldMatrix.java Mon Apr 20 20:15:39 2009 @@ -19,6 +19,7 @@ import java.io.Serializable; import java.lang.reflect.Array; +import java.util.Arrays; import org.apache.commons.math.Field; import org.apache.commons.math.FieldElement; @@ -42,23 +43,75 @@ /** Field to which the elements belong. */ private final Field field; + /** + * Get the elements type from an array. + * @param d data array + * @return field to which array elements belong + * @exception IllegalArgumentException if array is empty + */ + protected static > Field extractField(final T[][] d) + throws IllegalArgumentException { + if (d.length == 0) { + throw MathRuntimeException.createIllegalArgumentException("matrix must have at least one row"); + } + if (d[0].length == 0) { + throw MathRuntimeException.createIllegalArgumentException("matrix must have at least one column"); + } + return d[0][0].getField(); + } + + /** + * Get the elements type from an array. + * @param d data array + * @return field to which array elements belong + * @exception IllegalArgumentException if array is empty + */ + protected static > Field extractField(final T[] d) + throws IllegalArgumentException { + if (d.length == 0) { + throw MathRuntimeException.createIllegalArgumentException("matrix must have at least one row"); + } + return d[0].getField(); + } + /** Build an array of elements. + *

+ * Complete arrays are filled with field.getZero() + *

* @param rows number of rows - * @param columns number of columns + * @param columns number of columns (may be negative to build partial + * arrays in the same way new Field[rows][] works) * @return a new array */ @SuppressWarnings("unchecked") - protected T[][] buildArray(final int rows, final int columns) { - return (T[][]) Array.newInstance(field.getZero().getClass(), new int[] { rows, columns }); + protected static > T[][] buildArray(final Field field, + final int rows, + final int columns) { + if (columns < 0) { + T[] dummyRow = (T[]) Array.newInstance(field.getZero().getClass(), 0); + return (T[][]) Array.newInstance(dummyRow.getClass(), rows); + } + T[][] array = + (T[][]) Array.newInstance(field.getZero().getClass(), rows, columns); + for (int i = 0; i < array.length; ++i) { + Arrays.fill(array[i], field.getZero()); + } + return array; } /** Build an array of elements. + *

+ * Arrays are filled with field.getZero() + *

* @param length of the array * @return a new array */ @SuppressWarnings("unchecked") - protected T[] buildArray(final int length) { - return (T[]) Array.newInstance(field.getZero().getClass(), length); + protected static > T[] buildArray(final Field field, + final int length) { + T[] array = (T[]) Array.newInstance(field.getZero().getClass(), length); + Arrays.fill(array, field.getZero()); + return array; } /** @@ -209,7 +262,7 @@ /** {@inheritDoc} */ public T[][] getData() { - final T[][] data = buildArray(getRowDimension(), getColumnDimension()); + final T[][] data = buildArray(field, getRowDimension(), getColumnDimension()); for (int i = 0; i < data.length; ++i) { final T[] dataI = data[i]; @@ -493,7 +546,7 @@ checkRowIndex(row); final int nCols = getColumnDimension(); - final T[] out = buildArray(nCols); + final T[] out = buildArray(field, nCols); for (int i = 0; i < nCols; ++i) { out[i] = getEntry(row, i); } @@ -525,7 +578,7 @@ checkColumnIndex(column); final int nRows = getRowDimension(); - final T[] out = buildArray(nRows); + final T[] out = buildArray(field, nRows); for (int i = 0; i < nRows; ++i) { out[i] = getEntry(i, column); } @@ -628,7 +681,7 @@ v.length, nCols); } - final T[] out = buildArray(nRows); + final T[] out = buildArray(field, nRows); for (int row = 0; row < nRows; ++row) { T sum = field.getZero(); for (int i = 0; i < nCols; ++i) { @@ -655,7 +708,7 @@ v.getDimension(), nCols); } - final T[] out = buildArray(nRows); + final T[] out = buildArray(field, nRows); for (int row = 0; row < nRows; ++row) { T sum = field.getZero(); for (int i = 0; i < nCols; ++i) { @@ -680,7 +733,7 @@ v.length, nRows); } - final T[] out = buildArray(nCols); + final T[] out = buildArray(field, nCols); for (int col = 0; col < nCols; ++col) { T sum = field.getZero(); for (int i = 0; i < nRows; ++i) { @@ -708,7 +761,7 @@ v.getDimension(), nRows); } - final T[] out = buildArray(nCols); + final T[] out = buildArray(field, nCols); for (int col = 0; col < nCols; ++col) { T sum = field.getZero(); for (int i = 0; i < nRows; ++i) {