mirror of
https://github.com/apache/commons-math.git
synced 2025-02-07 02:29:20 +00:00
Added and used a specialized exception for duplicate abscissas in sampled functions
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@506592 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
21a95478c2
commit
1bd2978235
@ -0,0 +1,47 @@
|
|||||||
|
/*
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||||
|
* contributor license agreements. See the NOTICE file distributed with
|
||||||
|
* this work for additional information regarding copyright ownership.
|
||||||
|
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||||
|
* (the "License"); you may not use this file except in compliance with
|
||||||
|
* the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.apache.commons.math;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Exeption thrown when a sample contains several entries at the same abscissa.
|
||||||
|
* @version $Revision:$
|
||||||
|
*/
|
||||||
|
public class DuplicateSampleAbscissaException extends MathException {
|
||||||
|
|
||||||
|
/** Serializable version identifier */
|
||||||
|
private static final long serialVersionUID = -2271007547170169872L;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct an exception indicating the duplicate abscissa.
|
||||||
|
* @param abscissa duplicate abscissa
|
||||||
|
* @param i1 index of one entry having the duplicate abscissa
|
||||||
|
* @param i2 index of another entry having the duplicate abscissa
|
||||||
|
*/
|
||||||
|
public DuplicateSampleAbscissaException(double abscissa, int i1, int i2) {
|
||||||
|
super("Abscissa {0} is duplicated at both indices {1} and {2}",
|
||||||
|
new Object[] { new Double(abscissa), new Integer(i1), new Integer(i2) });
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the duplicate abscissa.
|
||||||
|
* @return duplicate abscissa
|
||||||
|
*/
|
||||||
|
public double getDuplicateAbscissa() {
|
||||||
|
return ((Double) getArguments()[0]).doubleValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
16
src/java/org/apache/commons/math/analysis/DividedDifferenceInterpolator.java
Executable file → Normal file
16
src/java/org/apache/commons/math/analysis/DividedDifferenceInterpolator.java
Executable file → Normal file
@ -17,7 +17,8 @@
|
|||||||
package org.apache.commons.math.analysis;
|
package org.apache.commons.math.analysis;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import org.apache.commons.math.MathException;
|
|
||||||
|
import org.apache.commons.math.DuplicateSampleAbscissaException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implements the <a href="
|
* Implements the <a href="
|
||||||
@ -35,7 +36,7 @@ public class DividedDifferenceInterpolator implements UnivariateRealInterpolator
|
|||||||
Serializable {
|
Serializable {
|
||||||
|
|
||||||
/** serializable version identifier */
|
/** serializable version identifier */
|
||||||
static final long serialVersionUID = 107049519551235069L;
|
private static final long serialVersionUID = 107049519551235069L;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Computes an interpolating function for the data set.
|
* Computes an interpolating function for the data set.
|
||||||
@ -43,10 +44,10 @@ public class DividedDifferenceInterpolator implements UnivariateRealInterpolator
|
|||||||
* @param x the interpolating points array
|
* @param x the interpolating points array
|
||||||
* @param y the interpolating values array
|
* @param y the interpolating values array
|
||||||
* @return a function which interpolates the data set
|
* @return a function which interpolates the data set
|
||||||
* @throws MathException if arguments are invalid
|
* @throws DuplicateSampleAbscissaException if arguments are invalid
|
||||||
*/
|
*/
|
||||||
public UnivariateRealFunction interpolate(double x[], double y[]) throws
|
public UnivariateRealFunction interpolate(double x[], double y[]) throws
|
||||||
MathException {
|
DuplicateSampleAbscissaException {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* a[] and c[] are defined in the general formula of Newton form:
|
* a[] and c[] are defined in the general formula of Newton form:
|
||||||
@ -86,10 +87,10 @@ public class DividedDifferenceInterpolator implements UnivariateRealInterpolator
|
|||||||
* The computational complexity is O(N^2).
|
* The computational complexity is O(N^2).
|
||||||
*
|
*
|
||||||
* @return a fresh copy of the divided difference array
|
* @return a fresh copy of the divided difference array
|
||||||
* @throws MathException if any abscissas coincide
|
* @throws DuplicateSampleAbscissaException if any abscissas coincide
|
||||||
*/
|
*/
|
||||||
protected static double[] computeDividedDifference(double x[], double y[])
|
protected static double[] computeDividedDifference(double x[], double y[])
|
||||||
throws MathException {
|
throws DuplicateSampleAbscissaException {
|
||||||
|
|
||||||
int i, j, n;
|
int i, j, n;
|
||||||
double divdiff[], a[], denominator;
|
double divdiff[], a[], denominator;
|
||||||
@ -109,8 +110,7 @@ public class DividedDifferenceInterpolator implements UnivariateRealInterpolator
|
|||||||
denominator = x[j+i] - x[j];
|
denominator = x[j+i] - x[j];
|
||||||
if (denominator == 0.0) {
|
if (denominator == 0.0) {
|
||||||
// This happens only when two abscissas are identical.
|
// This happens only when two abscissas are identical.
|
||||||
throw new MathException
|
throw new DuplicateSampleAbscissaException(x[j], j, j+i);
|
||||||
("Identical abscissas cause division by zero.");
|
|
||||||
}
|
}
|
||||||
divdiff[j] = (divdiff[j+1] - divdiff[j]) / denominator;
|
divdiff[j] = (divdiff[j+1] - divdiff[j]) / denominator;
|
||||||
}
|
}
|
||||||
|
16
src/java/org/apache/commons/math/analysis/PolynomialFunctionLagrangeForm.java
Executable file → Normal file
16
src/java/org/apache/commons/math/analysis/PolynomialFunctionLagrangeForm.java
Executable file → Normal file
@ -17,6 +17,8 @@
|
|||||||
package org.apache.commons.math.analysis;
|
package org.apache.commons.math.analysis;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
import org.apache.commons.math.DuplicateSampleAbscissaException;
|
||||||
import org.apache.commons.math.FunctionEvaluationException;
|
import org.apache.commons.math.FunctionEvaluationException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -83,7 +85,11 @@ public class PolynomialFunctionLagrangeForm implements UnivariateRealFunction,
|
|||||||
* @see UnivariateRealFunction#value(double)
|
* @see UnivariateRealFunction#value(double)
|
||||||
*/
|
*/
|
||||||
public double value(double z) throws FunctionEvaluationException {
|
public double value(double z) throws FunctionEvaluationException {
|
||||||
return evaluate(x, y, z);
|
try {
|
||||||
|
return evaluate(x, y, z);
|
||||||
|
} catch (DuplicateSampleAbscissaException e) {
|
||||||
|
throw new FunctionEvaluationException(z, e.getPattern(), e.getArguments(), e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -149,11 +155,11 @@ public class PolynomialFunctionLagrangeForm implements UnivariateRealFunction,
|
|||||||
* @param y the interpolating values array
|
* @param y the interpolating values array
|
||||||
* @param z the point at which the function value is to be computed
|
* @param z the point at which the function value is to be computed
|
||||||
* @return the function value
|
* @return the function value
|
||||||
* @throws FunctionEvaluationException if a runtime error occurs
|
* @throws DuplicateSampleAbscissaException if the sample has duplicate abscissas
|
||||||
* @throws IllegalArgumentException if inputs are not valid
|
* @throws IllegalArgumentException if inputs are not valid
|
||||||
*/
|
*/
|
||||||
public static double evaluate(double x[], double y[], double z) throws
|
public static double evaluate(double x[], double y[], double z) throws
|
||||||
FunctionEvaluationException, IllegalArgumentException {
|
DuplicateSampleAbscissaException, IllegalArgumentException {
|
||||||
|
|
||||||
int i, j, n, nearest = 0;
|
int i, j, n, nearest = 0;
|
||||||
double value, c[], d[], tc, td, divider, w, dist, min_dist;
|
double value, c[], d[], tc, td, divider, w, dist, min_dist;
|
||||||
@ -186,9 +192,7 @@ public class PolynomialFunctionLagrangeForm implements UnivariateRealFunction,
|
|||||||
divider = x[j] - x[i+j];
|
divider = x[j] - x[i+j];
|
||||||
if (divider == 0.0) {
|
if (divider == 0.0) {
|
||||||
// This happens only when two abscissas are identical.
|
// This happens only when two abscissas are identical.
|
||||||
throw new FunctionEvaluationException(z,
|
throw new DuplicateSampleAbscissaException(x[i], i, i+j);
|
||||||
"Identical abscissas cause division by zero: x[" +
|
|
||||||
i + "] = x[" + (i+j) + "] = " + x[i]);
|
|
||||||
}
|
}
|
||||||
// update the difference arrays
|
// update the difference arrays
|
||||||
w = (c[j+1] - d[j]) / divider;
|
w = (c[j+1] - d[j]) / divider;
|
||||||
|
@ -0,0 +1,38 @@
|
|||||||
|
/*
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||||
|
* contributor license agreements. See the NOTICE file distributed with
|
||||||
|
* this work for additional information regarding copyright ownership.
|
||||||
|
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||||
|
* (the "License"); you may not use this file except in compliance with
|
||||||
|
* the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.apache.commons.math;
|
||||||
|
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @version $Revision:$
|
||||||
|
*/
|
||||||
|
public class DuplicateSampleAbscissaExceptionTest extends TestCase {
|
||||||
|
|
||||||
|
public void testConstructor(){
|
||||||
|
DuplicateSampleAbscissaException ex = new DuplicateSampleAbscissaException(1.2, 10, 11);
|
||||||
|
assertNull(ex.getCause());
|
||||||
|
assertNotNull(ex.getMessage());
|
||||||
|
assertTrue(ex.getMessage().indexOf("1.2") > 0);
|
||||||
|
assertEquals(1.2, ex.getDuplicateAbscissa(), 0);
|
||||||
|
assertFalse(ex.getMessage().equals(ex.getMessage(Locale.FRENCH)));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user