You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by lu...@apache.org on 2015/11/29 21:10:52 UTC

[math] Cehck array dimensions in equations mapper.

Repository: commons-math
Updated Branches:
  refs/heads/field-ode 685b5ca91 -> a40d822c0


Cehck array dimensions in equations mapper.

Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/a40d822c
Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/a40d822c
Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/a40d822c

Branch: refs/heads/field-ode
Commit: a40d822c0fae3c887839627b7f3c38bd682f9dc2
Parents: 685b5ca
Author: Luc Maisonobe <lu...@apache.org>
Authored: Sun Nov 29 21:10:22 2015 +0100
Committer: Luc Maisonobe <lu...@apache.org>
Committed: Sun Nov 29 21:10:22 2015 +0100

----------------------------------------------------------------------
 .../commons/math3/ode/FieldEquationsMapper.java | 45 +++++++++++++++++---
 1 file changed, 39 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-math/blob/a40d822c/src/main/java/org/apache/commons/math3/ode/FieldEquationsMapper.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math3/ode/FieldEquationsMapper.java b/src/main/java/org/apache/commons/math3/ode/FieldEquationsMapper.java
index 2400fb7..0dd445a 100644
--- a/src/main/java/org/apache/commons/math3/ode/FieldEquationsMapper.java
+++ b/src/main/java/org/apache/commons/math3/ode/FieldEquationsMapper.java
@@ -20,6 +20,7 @@ package org.apache.commons.math3.ode;
 import java.io.Serializable;
 
 import org.apache.commons.math3.RealFieldElement;
+import org.apache.commons.math3.exception.DimensionMismatchException;
 import org.apache.commons.math3.exception.MathIllegalArgumentException;
 import org.apache.commons.math3.exception.util.LocalizedFormats;
 import org.apache.commons.math3.util.MathArrays;
@@ -110,8 +111,15 @@ public class FieldEquationsMapper<T extends RealFieldElement<T>> implements Seri
      * @param t time
      * @param y array to map, including primary and secondary components
      * @return mapped state
+     * @exception DimensionMismatchException if array does not match total dimension
      */
-    public FieldODEState<T> mapState(final T t, final T[] y) {
+    public FieldODEState<T> mapState(final T t, final T[] y)
+        throws DimensionMismatchException {
+
+        if (y.length != getTotalDimension()) {
+            throw new DimensionMismatchException(y.length, getTotalDimension());
+        }
+
         final int n = getNumberOfEquations();
         int index = 0;
         final T[] state = extractEquationData(index, y);
@@ -131,8 +139,19 @@ public class FieldEquationsMapper<T extends RealFieldElement<T>> implements Seri
      * @param y state array to map, including primary and secondary components
      * @param yDot state derivative array to map, including primary and secondary components
      * @return mapped state
+     * @exception DimensionMismatchException if an array does not match total dimension
      */
-    public FieldODEStateAndDerivative<T> mapStateAndDerivative(final T t, final T[] y, final T[] yDot) {
+    public FieldODEStateAndDerivative<T> mapStateAndDerivative(final T t, final T[] y, final T[] yDot)
+        throws DimensionMismatchException {
+
+        if (y.length != getTotalDimension()) {
+            throw new DimensionMismatchException(y.length, getTotalDimension());
+        }
+
+        if (yDot.length != getTotalDimension()) {
+            throw new DimensionMismatchException(yDot.length, getTotalDimension());
+        }
+
         final int n = getNumberOfEquations();
         int index = 0;
         final T[] state      = extractEquationData(index, y);
@@ -157,12 +176,17 @@ public class FieldEquationsMapper<T extends RealFieldElement<T>> implements Seri
      * equation data should be retrieved
      * @return equation data
      * @exception MathIllegalArgumentException if index is out of range
+     * @exception DimensionMismatchException if complete state has not enough elements
      */
     public T[] extractEquationData(final int index, final T[] complete)
-        throws MathIllegalArgumentException {
+        throws MathIllegalArgumentException, DimensionMismatchException {
         checkIndex(index);
         final int begin     = start[index];
-        final int dimension = start[index + 1] - begin;
+        final int end       = start[index + 1];
+        if (complete.length < end) {
+            throw new DimensionMismatchException(complete.length, end);
+        }
+        final int dimension = end - begin;
         final T[] equationData = MathArrays.buildArray(complete[0].getField(), dimension);
         System.arraycopy(complete, begin, equationData, 0, dimension);
         return equationData;
@@ -174,11 +198,20 @@ public class FieldEquationsMapper<T extends RealFieldElement<T>> implements Seri
      * @param equationData equation data to be inserted into the complete array
      * @param complete placeholder where to put equation data (only the
      * part corresponding to the equation will be overwritten)
+     * @exception DimensionMismatchException if either array has not enough elements
      */
-    public void insertEquationData(final int index, T[] equationData, T[] complete) {
+    public void insertEquationData(final int index, T[] equationData, T[] complete)
+        throws DimensionMismatchException {
         checkIndex(index);
         final int begin     = start[index];
-        final int dimension = start[index + 1] - begin;
+        final int end       = start[index + 1];
+        final int dimension = end - begin;
+        if (complete.length < end) {
+            throw new DimensionMismatchException(complete.length, end);
+        }
+        if (equationData.length != dimension) {
+            throw new DimensionMismatchException(equationData.length, dimension);
+        }
         System.arraycopy(equationData, 0, complete, begin, dimension);
     }