Java Code Examples for org.apache.commons.math.exception.OutOfRangeException

Following code examples demonstrate how to use org.apache.commons.math.exception.OutOfRangeExceptionfrom android. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.apache.commons.math.exception.OutOfRangeExceptionand various code implementation of this class.

  









  public int getCount(int... c)
    throws OutOfRangeException
  {
    if (c.length != dimension) {
      throw new DimensionMismatchException(c.length, dimension);
    }
    int count = 0;
    for (int i = 0; i < dimension; i++) {
      int index = c[i];
      if ((index < 0) || (index >= size[i]))
      {
        throw new OutOfRangeException(Integer.valueOf(index), Integer.valueOf(0), Integer.valueOf(size[i] - 1));
      }
      count += uniCounterOffset[i] * c[i];
    }
    return count + c[last];
  } 


  




  public int getDimension()
  {
    return dimension;
  } 

    public void testValues() throws Exception {
        PolynomialSplineFunction spline =
            new PolynomialSplineFunction(knots, polynomials);
        UnivariateRealFunction dSpline = spline.derivative();

        
        double x = -1;
        int index = 0;
        for (int i = 0; i < 10; i++) {
           x+=0.25;
           index = findKnot(knots, x);
           assertEquals("spline function evaluation failed for x=" + x,
                   polynomials[index].value(x - knots[index]), spline.value(x), tolerance);
           assertEquals("spline derivative evaluation failed for x=" + x,
                   dp.value(x - knots[index]), dSpline.value(x), tolerance);
        }

        for (int i = 0; i < 3; i++) {
            assertEquals("spline function evaluation failed for knot=" + knots[i],
                    polynomials[i].value(0), spline.value(knots[i]), tolerance);
            assertEquals("spline function evaluation failed for knot=" + knots[i],
                    dp.value(0), dSpline.value(knots[i]), tolerance);
        }

            x = spline.value(-1.5);
            fail("Expecting OutOfRangeException");
        } catch (OutOfRangeException ex) {
        } 

     protected int findKnot(double[] knots, double x) {
         if (x < knots[0] || x >= knots[knots.length -1]) {
             throw new OutOfRangeException(x, knots[0], knots[knots.length -1]);
         }
         for (int i = 0; i < knots.length; i++) {
             if (knots[i] > x) {
                 return i - 1;
             }
         }
         throw new MathIllegalStateException();
     } 

  


  public double value(double x, double y)
  {
    if ((x < 0.0D) || (x > 1.0D)) {
      throw new OutOfRangeException(Double.valueOf(x), Integer.valueOf(0), Integer.valueOf(1));
    }
    if ((y < 0.0D) || (y > 1.0D)) {
      throw new OutOfRangeException(Double.valueOf(y), Integer.valueOf(0), Integer.valueOf(1));
    }
    
    double x2 = x * x;
    double x3 = x2 * x;
    double[] pX = { 1.0D, x, x2, x3 };
    
    double y2 = y * y;
    double y3 = y2 * y;
    double[] pY = { 1.0D, y, y2, y3 };
    
    return apply(pX, pY, a);
  } 

    public void testPreconditions() {
        MultidimensionalCounter c;

        try {
            c = new MultidimensionalCounter(0, 1);
            Assert.fail("NotStrictlyPositiveException expected");
        } catch (NotStrictlyPositiveException e) {
        }
        try {
            c = new MultidimensionalCounter(2, 0);
            Assert.fail("NotStrictlyPositiveException expected");
        } catch (NotStrictlyPositiveException e) {
        }
        try {
            c = new MultidimensionalCounter(-1, 1);
            Assert.fail("NotStrictlyPositiveException expected");
        } catch (NotStrictlyPositiveException e) {
        }

        c = new MultidimensionalCounter(2, 3);
        try {
            c.getCount(1, 1, 1);
            Assert.fail("DimensionMismatchException expected");
        } catch (DimensionMismatchException e) {
        }
        try {
            c.getCount(3, 1);
            Assert.fail("OutOfRangeException expected");
        } catch (OutOfRangeException e) {
        }
        try {
            c.getCount(0, -1);
            Assert.fail("OutOfRangeException expected");
        } catch (OutOfRangeException e) {
        }
        try {
            c.getCounts(-1);
            Assert.fail("OutOfRangeException expected");
        } catch (OutOfRangeException e) {
        }
        try {
            c.getCounts(6);
            Assert.fail("OutOfRangeException expected");
        } catch (OutOfRangeException e) {
        }
    } 

    
    private static double value(double x,
                                double lo,
                                double hi) {
        if (x < lo || x > hi) {
            throw new OutOfRangeException(x, lo, hi);
        }
        return FastMath.log((x - lo) / (hi - x));
    } 

    protected void checkIndex(final int index) {
        if (index < 0 ||
            index >= getDimension()) {
            throw new OutOfRangeException(LocalizedFormats.INDEX,
                                          index, 0, getDimension() - 1);
        }
    } 

  





  public double value(double x, double y, double z)
  {
    if ((x < 0.0D) || (x > 1.0D)) {
      throw new OutOfRangeException(Double.valueOf(x), Integer.valueOf(0), Integer.valueOf(1));
    }
    if ((y < 0.0D) || (y > 1.0D)) {
      throw new OutOfRangeException(Double.valueOf(y), Integer.valueOf(0), Integer.valueOf(1));
    }
    if ((z < 0.0D) || (z > 1.0D)) {
      throw new OutOfRangeException(Double.valueOf(z), Integer.valueOf(0), Integer.valueOf(1));
    }
    
    double x2 = x * x;
    double x3 = x2 * x;
    double[] pX = { 1.0D, x, x2, x3 };
    
    double y2 = y * y;
    double y3 = y2 * y;
    double[] pY = { 1.0D, y, y2, y3 };
    
    double z2 = z * z;
    double z3 = z2 * z;
    double[] pZ = { 1.0D, z, z2, z3 };
    
    double result = 0.0D;
    for (int i = 0; i < 4; i++) {
      for (int j = 0; j < 4; j++) {
        for (int k = 0; k < 4; k++) {
          result += a[i][j][k] * pX[i] * pY[j] * pZ[k];
        }
      }
    }
    
    return result;
  } 

  















  private double interpolateXAtY(WeightedObservedPoint[] points, int startIdx, int idxStep, double y)
    throws OutOfRangeException
  {
    if (idxStep == 0) {
      throw new ZeroException();
    }
    WeightedObservedPoint[] twoPoints = getInterpolationPointsForY(points, startIdx, idxStep, y);
    WeightedObservedPoint pointA = twoPoints[0];
    WeightedObservedPoint pointB = twoPoints[1];
    if (pointA.getY() == y) {
      return pointA.getX();
    }
    if (pointB.getY() == y) {
      return pointB.getX();
    }
    return pointA.getX() + (y - pointA.getY()) * (pointB.getX() - pointA.getX()) / (pointB.getY() - pointA.getY());
  } 

    public void addValue(DoubleMatrix1D vector) {
        if (currentColumn>= _length)
            throw new OutOfRangeException(currentColumn, 0, _length);

        if (vector.size() != _dim)
            throw new OutOfRangeException(vector.size(), 0, _dim);

        for(int i=0; i<_dim; ++i)
            _path.setQuick(i, currentColumn, vector.get(i));

        currentColumn++;
    }