You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
java.lang.ArrayIndexOutOfBoundsException
at org.eclipse.jdt.internal.core.util.PublicScanner.getCurrentTokenSource(PublicScanner.java:461)
at br.usp.each.saeg.code.forest.source.parser.SourceCodeParser.readIt(SourceCodeParser.java:299)
at br.usp.each.saeg.code.forest.source.parser.SourceCodeParser.visit(SourceCodeParser.java:257)
at org.eclipse.jdt.core.dom.MethodDeclaration.accept0(MethodDeclaration.java:489)
at org.eclipse.jdt.core.dom.ASTNode.accept(ASTNode.java:2514)
at org.eclipse.jdt.core.dom.ASTNode.acceptChildren(ASTNode.java:2585)
at org.eclipse.jdt.core.dom.TypeDeclaration.accept0(TypeDeclaration.java:484)
at org.eclipse.jdt.core.dom.ASTNode.accept(ASTNode.java:2514)
at org.eclipse.jdt.core.dom.ASTNode.acceptChildren(ASTNode.java:2585)
at org.eclipse.jdt.core.dom.CompilationUnit.accept0(CompilationUnit.java:220)
at org.eclipse.jdt.core.dom.ASTNode.accept(ASTNode.java:2514)
at br.usp.each.saeg.code.forest.ui.handlers.RunAnalysisHandler.parse(RunAnalysisHandler.java:84)
at br.usp.each.saeg.code.forest.ui.handlers.RunAnalysisHandler.execute(RunAnalysisHandler.java:40)
at org.eclipse.ui.internal.handlers.HandlerProxy.execute(HandlerProxy.java:290)
at org.eclipse.ui.internal.handlers.E4HandlerProxy.execute(E4HandlerProxy.java:90)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.eclipse.e4.core.internal.di.MethodRequestor.execute(MethodRequestor.java:56)
at org.eclipse.e4.core.internal.di.InjectorImpl.invokeUsingClass(InjectorImpl.java:243)
at org.eclipse.e4.core.internal.di.InjectorImpl.invoke(InjectorImpl.java:224)
at org.eclipse.e4.core.contexts.ContextInjectionFactory.invoke(ContextInjectionFactory.java:132)
at org.eclipse.e4.core.commands.internal.HandlerServiceHandler.execute(HandlerServiceHandler.java:167)
at org.eclipse.core.commands.Command.executeWithChecks(Command.java:499)
at org.eclipse.core.commands.ParameterizedCommand.executeWithChecks(ParameterizedCommand.java:508)
at org.eclipse.e4.core.commands.internal.HandlerServiceImpl.executeHandler(HandlerServiceImpl.java:213)
at org.eclipse.e4.ui.workbench.renderers.swt.HandledContributionItem.executeItem(HandledContributionItem.java:850)
at org.eclipse.e4.ui.workbench.renderers.swt.HandledContributionItem.handleWidgetSelection(HandledContributionItem.java:743)
at org.eclipse.e4.ui.workbench.renderers.swt.HandledContributionItem.access$7(HandledContributionItem.java:727)
at org.eclipse.e4.ui.workbench.renderers.swt.HandledContributionItem$4.handleEvent(HandledContributionItem.java:662)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4166)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1466)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1489)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1474)
at org.eclipse.swt.widgets.Widget.notifyListeners(Widget.java:1279)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4012)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3651)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$9.run(PartRenderingEngine.java:1113)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:997)
at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:138)
at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:610)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:567)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:150)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:124)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:354)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:181)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:636)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:591)
at org.eclipse.equinox.launcher.Main.run(Main.java:1450)
No arquivo
/* * 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. */packageorg.apache.commons.math.util;
importjava.math.BigDecimal;
importjava.math.BigInteger;
importjava.util.Arrays;
importorg.apache.commons.math.MathRuntimeException;
/** * Some useful additions to the built-in functions in {@link Math}. * @version $Revision: 772119 $ $Date: 2009-05-06 06:43:28 -0300 (Wed, 06 May 2009) $ */publicfinalclassMathUtils {
/** Smallest positive number such that 1 - EPSILON is not numerically equal to 1. */publicstaticfinaldoubleEPSILON = 0x1.0p-53;
/** Safe minimum, such that 1 / SAFE_MIN does not overflow. * <p>In IEEE 754 arithmetic, this is also the smallest normalized * number 2<sup>-1022</sup>.</p> */publicstaticfinaldoubleSAFE_MIN = 0x1.0p-1022;
/** -1.0 cast as a byte. */privatestaticfinalbyteNB = (byte)-1;
/** -1.0 cast as a short. */privatestaticfinalshortNS = (short)-1;
/** 1.0 cast as a byte. */privatestaticfinalbytePB = (byte)1;
/** 1.0 cast as a short. */privatestaticfinalshortPS = (short)1;
/** 0.0 cast as a byte. */privatestaticfinalbyteZB = (byte)0;
/** 0.0 cast as a short. */privatestaticfinalshortZS = (short)0;
/** 2 π. */privatestaticfinaldoubleTWO_PI = 2 * Math.PI;
/** Gap between NaN and regular numbers. */privatestaticfinalintNAN_GAP = 4 * 1024 * 1024;
/** Offset to order signed double numbers lexicographically. */privatestaticfinallongSGN_MASK = 0x8000000000000000L;
/** * Private Constructor */privateMathUtils() {
super();
}
/** * Add two integers, checking for overflow. * * @param x an addend * @param y an addend * @return the sum <code>x+y</code> * @throws ArithmeticException if the result can not be represented as an * int * @since 1.1 */publicstaticintaddAndCheck(intx, inty) {
longs = (long)x + (long)y;
if (s < Integer.MIN_VALUE || s > Integer.MAX_VALUE) {
thrownewArithmeticException("overflow: add");
}
return (int)s;
}
/** * Add two long integers, checking for overflow. * * @param a an addend * @param b an addend * @return the sum <code>a+b</code> * @throws ArithmeticException if the result can not be represented as an * long * @since 1.2 */publicstaticlongaddAndCheck(longa, longb) {
returnaddAndCheck(a, b, "overflow: add");
}
/** * Add two long integers, checking for overflow. * * @param a an addend * @param b an addend * @param msg the message to use for any thrown exception. * @return the sum <code>a+b</code> * @throws ArithmeticException if the result can not be represented as an * long * @since 1.2 */privatestaticlongaddAndCheck(longa, longb, Stringmsg) {
longret;
if (a > b) {
// use symmetry to reduce boundary casesret = addAndCheck(b, a, msg);
} else {
// assert a <= bif (a < 0) {
if (b < 0) {
// check for negative overflowif (Long.MIN_VALUE - b <= a) {
ret = a + b;
} else {
thrownewArithmeticException(msg);
}
} else {
// opposite sign addition is always saferet = a + b;
}
} else {
// assert a >= 0// assert b >= 0// check for positive overflowif (a <= Long.MAX_VALUE - b) {
ret = a + b;
} else {
thrownewArithmeticException(msg);
}
}
}
returnret;
}
/** * Returns an exact representation of the <a * href="http://mathworld.wolfram.com/BinomialCoefficient.html"> Binomial * Coefficient</a>, "<code>n choose k</code>", the number of * <code>k</code>-element subsets that can be selected from an * <code>n</code>-element set. * <p> * <Strong>Preconditions</strong>: * <ul> * <li> <code>0 <= k <= n </code> (otherwise * <code>IllegalArgumentException</code> is thrown)</li> * <li> The result is small enough to fit into a <code>long</code>. The * largest value of <code>n</code> for which all coefficients are * <code> < Long.MAX_VALUE</code> is 66. If the computed value exceeds * <code>Long.MAX_VALUE</code> an <code>ArithMeticException</code> is * thrown.</li> * </ul></p> * * @param n the size of the set * @param k the size of the subsets to be counted * @return <code>n choose k</code> * @throws IllegalArgumentException if preconditions are not met. * @throws ArithmeticException if the result is too large to be represented * by a long integer. */publicstaticlongbinomialCoefficient(finalintn, finalintk) {
checkBinomial(n, k);
if ((n == k) || (k == 0)) {
return1;
}
if ((k == 1) || (k == n - 1)) {
returnn;
}
// Use symmetry for large kif (k > n / 2)
returnbinomialCoefficient(n, n - k);
// We use the formula// (n choose k) = n! / (n-k)! / k!// (n choose k) == ((n-k+1)*...*n) / (1*...*k)// which could be written// (n choose k) == (n-1 choose k-1) * n / klongresult = 1;
if (n <= 61) {
// For n <= 61, the naive implementation cannot overflow.for (intj = 1, i = n - k + 1; j <= k; i++, j++) {
result = result * i / j;
}
} elseif (n <= 66) {
// For n > 61 but n <= 66, the result cannot overflow,// but we must take care not to overflow intermediate values.for (intj = 1, i = n - k + 1; j <= k; i++, j++) {
// We know that (result * i) is divisible by j,// but (result * i) may overflow, so we split j:// Filter out the gcd, d, so j/d and i/d are integer.// result is divisible by (j/d) because (j/d)// is relative prime to (i/d) and is a divisor of// result * (i/d).longd = gcd(i, j);
result = (result / (j / d)) * (i / d);
}
} else {
// For n > 66, a result overflow might occur, so we check// the multiplication, taking care to not overflow// unnecessary.for (intj = 1, i = n - k + 1; j <= k; i++, j++) {
longd = gcd(i, j);
result = mulAndCheck((result / (j / d)), (i / d));
}
}
returnresult;
}
/** * Returns a <code>double</code> representation of the <a * href="http://mathworld.wolfram.com/BinomialCoefficient.html"> Binomial * Coefficient</a>, "<code>n choose k</code>", the number of * <code>k</code>-element subsets that can be selected from an * <code>n</code>-element set. * <p> * <Strong>Preconditions</strong>: * <ul> * <li> <code>0 <= k <= n </code> (otherwise * <code>IllegalArgumentException</code> is thrown)</li> * <li> The result is small enough to fit into a <code>double</code>. The * largest value of <code>n</code> for which all coefficients are < * Double.MAX_VALUE is 1029. If the computed value exceeds Double.MAX_VALUE, * Double.POSITIVE_INFINITY is returned</li> * </ul></p> * * @param n the size of the set * @param k the size of the subsets to be counted * @return <code>n choose k</code> * @throws IllegalArgumentException if preconditions are not met. */publicstaticdoublebinomialCoefficientDouble(finalintn, finalintk) {
checkBinomial(n, k);
if ((n == k) || (k == 0)) {
return1d;
}
if ((k == 1) || (k == n - 1)) {
returnn;
}
if (k > n/2) {
returnbinomialCoefficientDouble(n, n - k);
}
if (n < 67) {
returnbinomialCoefficient(n,k);
}
doubleresult = 1d;
for (inti = 1; i <= k; i++) {
result *= (double)(n - k + i) / (double)i;
}
returnMath.floor(result + 0.5);
}
/** * Returns the natural <code>log</code> of the <a * href="http://mathworld.wolfram.com/BinomialCoefficient.html"> Binomial * Coefficient</a>, "<code>n choose k</code>", the number of * <code>k</code>-element subsets that can be selected from an * <code>n</code>-element set. * <p> * <Strong>Preconditions</strong>: * <ul> * <li> <code>0 <= k <= n </code> (otherwise * <code>IllegalArgumentException</code> is thrown)</li> * </ul></p> * * @param n the size of the set * @param k the size of the subsets to be counted * @return <code>n choose k</code> * @throws IllegalArgumentException if preconditions are not met. */publicstaticdoublebinomialCoefficientLog(finalintn, finalintk) {
checkBinomial(n, k);
if ((n == k) || (k == 0)) {
return0;
}
if ((k == 1) || (k == n - 1)) {
returnMath.log(n);
}
/* * For values small enough to do exact integer computation, * return the log of the exact value */if (n < 67) {
returnMath.log(binomialCoefficient(n,k));
}
/* * Return the log of binomialCoefficientDouble for values that will not * overflow binomialCoefficientDouble */if (n < 1030) {
returnMath.log(binomialCoefficientDouble(n, k));
}
if (k > n / 2) {
returnbinomialCoefficientLog(n, n - k);
}
/* * Sum logs for values that could overflow */doublelogSum = 0;
// n!/(n-k)!for (inti = n - k + 1; i <= n; i++) {
logSum += Math.log(i);
}
// divide by k!for (inti = 2; i <= k; i++) {
logSum -= Math.log(i);
}
returnlogSum;
}
/** * Check binomial preconditions. * @param n the size of the set * @param k the size of the subsets to be counted * @exception IllegalArgumentException if preconditions are not met. */privatestaticvoidcheckBinomial(finalintn, finalintk)
throwsIllegalArgumentException {
if (n < k) {
throwMathRuntimeException.createIllegalArgumentException(
"must have n >= k for binomial coefficient (n,k), got n = {0}, k = {1}",
n, k);
}
if (n < 0) {
throwMathRuntimeException.createIllegalArgumentException(
"must have n >= 0 for binomial coefficient (n,k), got n = {0}",
n);
}
}
/** * Compares two numbers given some amount of allowed error. * * @param x the first number * @param y the second number * @param eps the amount of error to allow when checking for equality * @return <ul><li>0 if {@link #equals(double, double, double) equals(x, y, eps)}</li> * <li>< 0 if !{@link #equals(double, double, double) equals(x, y, eps)} && x < y</li> * <li>> 0 if !{@link #equals(double, double, double) equals(x, y, eps)} && x > y</li></ul> */publicstaticintcompareTo(doublex, doubley, doubleeps) {
if (equals(x, y, eps)) {
return0;
} elseif (x < y) {
return -1;
}
return1;
}
/** * Returns the <a href="http://mathworld.wolfram.com/HyperbolicCosine.html"> * hyperbolic cosine</a> of x. * * @param x double value for which to find the hyperbolic cosine * @return hyperbolic cosine of x */publicstaticdoublecosh(doublex) {
return (Math.exp(x) + Math.exp(-x)) / 2.0;
}
/** * Returns true iff both arguments are NaN or neither is NaN and they are * equal * * @param x first value * @param y second value * @return true if the values are equal or both are NaN */publicstaticbooleanequals(doublex, doubley) {
return ((Double.isNaN(x) && Double.isNaN(y)) || x == y);
}
/** * Returns true iff both arguments are equal or within the range of allowed * error (inclusive). * <p> * Two NaNs are considered equals, as are two infinities with same sign. * </p> * * @param x first value * @param y second value * @param eps the amount of absolute error to allow * @return true if the values are equal or within range of each other */publicstaticbooleanequals(doublex, doubley, doubleeps) {
returnequals(x, y) || (Math.abs(y - x) <= eps);
}
/** * Returns true iff both arguments are equal or within the range of allowed * error (inclusive). * Adapted from <a * href="http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm"> * Bruce Dawson</a> * * @param x first value * @param y second value * @param maxUlps {@code (maxUlps - 1)} is the number of floating point * values between {@code x} and {@code y}. * @return {@code true} if there are less than {@code maxUlps} floating * point values between {@code x} and {@code y} */publicstaticbooleanequals(doublex, doubley, intmaxUlps) {
// Check that "maxUlps" is non-negative and small enough so that the// default NAN won't compare as equal to anything.assertmaxUlps > 0 && maxUlps < NAN_GAP;
longxInt = Double.doubleToLongBits(x);
longyInt = Double.doubleToLongBits(y);
// Make lexicographically ordered as a two's-complement integer.if (xInt < 0) {
xInt = SGN_MASK - xInt;
}
if (yInt < 0) {
yInt = SGN_MASK - yInt;
}
returnMath.abs(xInt - yInt) <= maxUlps;
}
/** * Returns true iff both arguments are null or have same dimensions * and all their elements are {@link #equals(double,double) equals} * * @param x first array * @param y second array * @return true if the values are both null or have same dimension * and equal elements * @since 1.2 */publicstaticbooleanequals(double[] x, double[] y) {
if ((x == null) || (y == null)) {
return !((x == null) ^ (y == null));
}
if (x.length != y.length) {
returnfalse;
}
for (inti = 0; i < x.length; ++i) {
if (!equals(x[i], y[i])) {
returnfalse;
}
}
returntrue;
}
/** All long-representable factorials */privatestaticfinallong[] factorials = newlong[]
{1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800,
479001600, 6227020800l, 87178291200l, 1307674368000l, 20922789888000l,
355687428096000l, 6402373705728000l, 121645100408832000l,
2432902008176640000l};
/** * Returns n!. Shorthand for <code>n</code> <a * href="http://mathworld.wolfram.com/Factorial.html"> Factorial</a>, the * product of the numbers <code>1,...,n</code>. * <p> * <Strong>Preconditions</strong>: * <ul> * <li> <code>n >= 0</code> (otherwise * <code>IllegalArgumentException</code> is thrown)</li> * <li> The result is small enough to fit into a <code>long</code>. The * largest value of <code>n</code> for which <code>n!</code> < * Long.MAX_VALUE</code> is 20. If the computed value exceeds <code>Long.MAX_VALUE</code> * an <code>ArithMeticException </code> is thrown.</li> * </ul> * </p> * * @param n argument * @return <code>n!</code> * @throws ArithmeticException if the result is too large to be represented * by a long integer. * @throws IllegalArgumentException if n < 0 */publicstaticlongfactorial(finalintn) {
if (n < 0) {
throwMathRuntimeException.createIllegalArgumentException(
"must have n >= 0 for n!, got n = {0}",
n);
}
if (n > 20) {
thrownewArithmeticException(
"factorial value is too large to fit in a long");
}
returnfactorials[n];
}
/** * Returns n!. Shorthand for <code>n</code> <a * href="http://mathworld.wolfram.com/Factorial.html"> Factorial</a>, the * product of the numbers <code>1,...,n</code> as a <code>double</code>. * <p> * <Strong>Preconditions</strong>: * <ul> * <li> <code>n >= 0</code> (otherwise * <code>IllegalArgumentException</code> is thrown)</li> * <li> The result is small enough to fit into a <code>double</code>. The * largest value of <code>n</code> for which <code>n!</code> < * Double.MAX_VALUE</code> is 170. If the computed value exceeds * Double.MAX_VALUE, Double.POSITIVE_INFINITY is returned</li> * </ul> * </p> * * @param n argument * @return <code>n!</code> * @throws IllegalArgumentException if n < 0 */publicstaticdoublefactorialDouble(finalintn) {
if (n < 0) {
throwMathRuntimeException.createIllegalArgumentException(
"must have n >= 0 for n!, got n = {0}",
n);
}
if (n < 21) {
returnfactorial(n);
}
returnMath.floor(Math.exp(factorialLog(n)) + 0.5);
}
/** * Returns the natural logarithm of n!. * <p> * <Strong>Preconditions</strong>: * <ul> * <li> <code>n >= 0</code> (otherwise * <code>IllegalArgumentException</code> is thrown)</li> * </ul></p> * * @param n argument * @return <code>n!</code> * @throws IllegalArgumentException if preconditions are not met. */publicstaticdoublefactorialLog(finalintn) {
if (n < 0) {
throwMathRuntimeException.createIllegalArgumentException(
"must have n >= 0 for n!, got n = {0}",
n);
}
if (n < 21) {
returnMath.log(factorial(n));
}
doublelogSum = 0;
for (inti = 2; i <= n; i++) {
logSum += Math.log(i);
}
returnlogSum;
}
/** * <p> * Gets the greatest common divisor of the absolute value of two numbers, * using the "binary gcd" method which avoids division and modulo * operations. See Knuth 4.5.2 algorithm B. This algorithm is due to Josef * Stein (1961). * </p> * Special cases: * <ul> * <li>The invocations * <code>gcd(Integer.MIN_VALUE, Integer.MIN_VALUE)</code>, * <code>gcd(Integer.MIN_VALUE, 0)</code> and * <code>gcd(0, Integer.MIN_VALUE)</code> throw an * <code>ArithmeticException</code>, because the result would be 2^31, which * is too large for an int value.</li> * <li>The result of <code>gcd(x, x)</code>, <code>gcd(0, x)</code> and * <code>gcd(x, 0)</code> is the absolute value of <code>x</code>, except * for the special cases above. * <li>The invocation <code>gcd(0, 0)</code> is the only one which returns * <code>0</code>.</li> * </ul> * * @param p any number * @param q any number * @return the greatest common divisor, never negative * @throws ArithmeticException * if the result cannot be represented as a nonnegative int * value * @since 1.1 */publicstaticintgcd(finalintp, finalintq) {
intu = p;
intv = q;
if ((u == 0) || (v == 0)) {
if ((u == Integer.MIN_VALUE) || (v == Integer.MIN_VALUE)) {
throwMathRuntimeException.createArithmeticException(
"overflow: gcd({0}, {1}) is 2^31",
p, q);
}
return (Math.abs(u) + Math.abs(v));
}
// keep u and v negative, as negative integers range down to// -2^31, while positive numbers can only be as large as 2^31-1// (i.e. we can't necessarily negate a negative number without// overflow)/* assert u!=0 && v!=0; */if (u > 0) {
u = -u;
} // make u negativeif (v > 0) {
v = -v;
} // make v negative// B1. [Find power of 2]intk = 0;
while ((u & 1) == 0 && (v & 1) == 0 && k < 31) { // while u and v are// both even...u /= 2;
v /= 2;
k++; // cast out twos.
}
if (k == 31) {
throwMathRuntimeException.createArithmeticException(
"overflow: gcd({0}, {1}) is 2^31",
p, q);
}
// B2. Initialize: u and v have been divided by 2^k and at least// one is odd.intt = ((u & 1) == 1) ? v : -(u / 2)/* B3 */;
// t negative: u was odd, v may be even (t replaces v)// t positive: u was even, v is odd (t replaces u)do {
/* assert u<0 && v<0; */// B4/B3: cast out twos from t.while ((t & 1) == 0) { // while t is even..t /= 2; // cast out twos
}
// B5 [reset max(u,v)]if (t > 0) {
u = -t;
} else {
v = t;
}
// B6/B3. at this point both u and v should be odd.t = (v - u) / 2;
// |u| larger: t positive (replace u)// |v| larger: t negative (replace v)
} while (t != 0);
return -u * (1 << k); // gcd is u*2^k
}
/** * Returns an integer hash code representing the given double value. * * @param value the value to be hashed * @return the hash code */publicstaticinthash(doublevalue) {
returnnewDouble(value).hashCode();
}
/** * Returns an integer hash code representing the given double array. * * @param value the value to be hashed (may be null) * @return the hash code * @since 1.2 */publicstaticinthash(double[] value) {
returnArrays.hashCode(value);
}
/** * For a byte value x, this method returns (byte)(+1) if x >= 0 and * (byte)(-1) if x < 0. * * @param x the value, a byte * @return (byte)(+1) or (byte)(-1), depending on the sign of x */publicstaticbyteindicator(finalbytex) {
return (x >= ZB) ? PB : NB;
}
/** * For a double precision value x, this method returns +1.0 if x >= 0 and * -1.0 if x < 0. Returns <code>NaN</code> if <code>x</code> is * <code>NaN</code>. * * @param x the value, a double * @return +1.0 or -1.0, depending on the sign of x */publicstaticdoubleindicator(finaldoublex) {
if (Double.isNaN(x)) {
returnDouble.NaN;
}
return (x >= 0.0) ? 1.0 : -1.0;
}
/** * For a float value x, this method returns +1.0F if x >= 0 and -1.0F if x < * 0. Returns <code>NaN</code> if <code>x</code> is <code>NaN</code>. * * @param x the value, a float * @return +1.0F or -1.0F, depending on the sign of x */publicstaticfloatindicator(finalfloatx) {
if (Float.isNaN(x)) {
returnFloat.NaN;
}
return (x >= 0.0F) ? 1.0F : -1.0F;
}
/** * For an int value x, this method returns +1 if x >= 0 and -1 if x < 0. * * @param x the value, an int * @return +1 or -1, depending on the sign of x */publicstaticintindicator(finalintx) {
return (x >= 0) ? 1 : -1;
}
/** * For a long value x, this method returns +1L if x >= 0 and -1L if x < 0. * * @param x the value, a long * @return +1L or -1L, depending on the sign of x */publicstaticlongindicator(finallongx) {
return (x >= 0L) ? 1L : -1L;
}
/** * For a short value x, this method returns (short)(+1) if x >= 0 and * (short)(-1) if x < 0. * * @param x the value, a short * @return (short)(+1) or (short)(-1), depending on the sign of x */publicstaticshortindicator(finalshortx) {
return (x >= ZS) ? PS : NS;
}
/** * <p> * Returns the least common multiple of the absolute value of two numbers, * using the formula <code>lcm(a,b) = (a / gcd(a,b)) * b</code>. * </p> * Special cases: * <ul> * <li>The invocations <code>lcm(Integer.MIN_VALUE, n)</code> and * <code>lcm(n, Integer.MIN_VALUE)</code>, where <code>abs(n)</code> is a * power of 2, throw an <code>ArithmeticException</code>, because the result * would be 2^31, which is too large for an int value.</li> * <li>The result of <code>lcm(0, x)</code> and <code>lcm(x, 0)</code> is * <code>0</code> for any <code>x</code>. * </ul> * * @param a any number * @param b any number * @return the least common multiple, never negative * @throws ArithmeticException * if the result cannot be represented as a nonnegative int * value * @since 1.1 */publicstaticintlcm(inta, intb) {
if (a==0 || b==0){
return0;
}
intlcm = Math.abs(mulAndCheck(a / gcd(a, b), b));
if (lcm == Integer.MIN_VALUE){
thrownewArithmeticException("overflow: lcm is 2^31");
}
returnlcm;
}
/** * <p>Returns the * <a href="http://mathworld.wolfram.com/Logarithm.html">logarithm</a> * for base <code>b</code> of <code>x</code>. * </p> * <p>Returns <code>NaN<code> if either argument is negative. If * <code>base</code> is 0 and <code>x</code> is positive, 0 is returned. * If <code>base</code> is positive and <code>x</code> is 0, * <code>Double.NEGATIVE_INFINITY</code> is returned. If both arguments * are 0, the result is <code>NaN</code>.</p> * * @param base the base of the logarithm, must be greater than 0 * @param x argument, must be greater than 0 * @return the value of the logarithm - the number y such that base^y = x. * @since 1.2 */publicstaticdoublelog(doublebase, doublex) {
returnMath.log(x)/Math.log(base);
}
/** * Multiply two integers, checking for overflow. * * @param x a factor * @param y a factor * @return the product <code>x*y</code> * @throws ArithmeticException if the result can not be represented as an * int * @since 1.1 */publicstaticintmulAndCheck(intx, inty) {
longm = ((long)x) * ((long)y);
if (m < Integer.MIN_VALUE || m > Integer.MAX_VALUE) {
thrownewArithmeticException("overflow: mul");
}
return (int)m;
}
/** * Multiply two long integers, checking for overflow. * * @param a first value * @param b second value * @return the product <code>a * b</code> * @throws ArithmeticException if the result can not be represented as an * long * @since 1.2 */publicstaticlongmulAndCheck(longa, longb) {
longret;
Stringmsg = "overflow: multiply";
if (a > b) {
// use symmetry to reduce boundary casesret = mulAndCheck(b, a);
} else {
if (a < 0) {
if (b < 0) {
// check for positive overflow with negative a, negative bif (a >= Long.MAX_VALUE / b) {
ret = a * b;
} else {
thrownewArithmeticException(msg);
}
} elseif (b > 0) {
// check for negative overflow with negative a, positive bif (Long.MIN_VALUE / b <= a) {
ret = a * b;
} else {
thrownewArithmeticException(msg);
}
} else {
// assert b == 0ret = 0;
}
} elseif (a > 0) {
// assert a > 0// assert b > 0// check for positive overflow with positive a, positive bif (a <= Long.MAX_VALUE / b) {
ret = a * b;
} else {
thrownewArithmeticException(msg);
}
} else {
// assert a == 0ret = 0;
}
}
returnret;
}
/** * Get the next machine representable number after a number, moving * in the direction of another number. * <p> * If <code>direction</code> is greater than or equal to<code>d</code>, * the smallest machine representable number strictly greater than * <code>d</code> is returned; otherwise the largest representable number * strictly less than <code>d</code> is returned.</p> * <p> * If <code>d</code> is NaN or Infinite, it is returned unchanged.</p> * * @param d base number * @param direction (the only important thing is whether * direction is greater or smaller than d) * @return the next machine representable number in the specified direction * @since 1.2 */publicstaticdoublenextAfter(doubled, doubledirection) {
// handling of some important special casesif (Double.isNaN(d) || Double.isInfinite(d)) {
returnd;
} elseif (d == 0) {
return (direction < 0) ? -Double.MIN_VALUE : Double.MIN_VALUE;
}
// special cases MAX_VALUE to infinity and MIN_VALUE to 0// are handled just as normal numbers// split the double in raw componentslongbits = Double.doubleToLongBits(d);
longsign = bits & 0x8000000000000000L;
longexponent = bits & 0x7ff0000000000000L;
longmantissa = bits & 0x000fffffffffffffL;
if (d * (direction - d) >= 0) {
// we should increase the mantissaif (mantissa == 0x000fffffffffffffL) {
returnDouble.longBitsToDouble(sign |
(exponent + 0x0010000000000000L));
} else {
returnDouble.longBitsToDouble(sign |
exponent | (mantissa + 1));
}
} else {
// we should decrease the mantissaif (mantissa == 0L) {
returnDouble.longBitsToDouble(sign |
(exponent - 0x0010000000000000L) |
0x000fffffffffffffL);
} else {
returnDouble.longBitsToDouble(sign |
exponent | (mantissa - 1));
}
}
}
/** * Scale a number by 2<sup>scaleFactor</sup>. * <p>If <code>d</code> is 0 or NaN or Infinite, it is returned unchanged.</p> * * @param d base number * @param scaleFactor power of two by which d sould be multiplied * @return d × 2<sup>scaleFactor</sup> * @since 2.0 */publicstaticdoublescalb(finaldoubled, finalintscaleFactor) {
// handling of some important special casesif ((d == 0) || Double.isNaN(d) || Double.isInfinite(d)) {
returnd;
}
// split the double in raw componentsfinallongbits = Double.doubleToLongBits(d);
finallongexponent = bits & 0x7ff0000000000000L;
finallongrest = bits & 0x800fffffffffffffL;
// shift the exponentfinallongnewBits = rest | (exponent + (((long) scaleFactor) << 52));
returnDouble.longBitsToDouble(newBits);
}
/** * Normalize an angle in a 2&pi wide interval around a center value. * <p>This method has three main uses:</p> * <ul> * <li>normalize an angle between 0 and 2π:<br/> * <code>a = MathUtils.normalizeAngle(a, Math.PI);</code></li> * <li>normalize an angle between -π and +π<br/> * <code>a = MathUtils.normalizeAngle(a, 0.0);</code></li> * <li>compute the angle between two defining angular positions:<br> * <code>angle = MathUtils.normalizeAngle(end, start) - start;</code></li> * </ul> * <p>Note that due to numerical accuracy and since π cannot be represented * exactly, the result interval is <em>closed</em>, it cannot be half-closed * as would be more satisfactory in a purely mathematical view.</p> * @param a angle to normalize * @param center center of the desired 2π interval for the result * @return a-2kπ with integer k and center-π <= a-2kπ <= center+π * @since 1.2 */publicstaticdoublenormalizeAngle(doublea, doublecenter) {
returna - TWO_PI * Math.floor((a + Math.PI - center) / TWO_PI);
}
/** * Round the given value to the specified number of decimal places. The * value is rounded using the {@link BigDecimal#ROUND_HALF_UP} method. * * @param x the value to round. * @param scale the number of digits to the right of the decimal point. * @return the rounded value. * @since 1.1 */publicstaticdoubleround(doublex, intscale) {
returnround(x, scale, BigDecimal.ROUND_HALF_UP);
}
/** * Round the given value to the specified number of decimal places. The * value is rounded using the given method which is any method defined in * {@link BigDecimal}. * * @param x the value to round. * @param scale the number of digits to the right of the decimal point. * @param roundingMethod the rounding method as defined in * {@link BigDecimal}. * @return the rounded value. * @since 1.1 */publicstaticdoubleround(doublex, intscale, introundingMethod) {
try {
return (newBigDecimal
(Double.toString(x))
.setScale(scale, roundingMethod))
.doubleValue();
} catch (NumberFormatExceptionex) {
if (Double.isInfinite(x)) {
returnx;
} else {
returnDouble.NaN;
}
}
}
/** * Round the given value to the specified number of decimal places. The * value is rounding using the {@link BigDecimal#ROUND_HALF_UP} method. * * @param x the value to round. * @param scale the number of digits to the right of the decimal point. * @return the rounded value. * @since 1.1 */publicstaticfloatround(floatx, intscale) {
returnround(x, scale, BigDecimal.ROUND_HALF_UP);
}
/** * Round the given value to the specified number of decimal places. The * value is rounded using the given method which is any method defined in * {@link BigDecimal}. * * @param x the value to round. * @param scale the number of digits to the right of the decimal point. * @param roundingMethod the rounding method as defined in * {@link BigDecimal}. * @return the rounded value. * @since 1.1 */publicstaticfloatround(floatx, intscale, introundingMethod) {
floatsign = indicator(x);
floatfactor = (float)Math.pow(10.0f, scale) * sign;
return (float)roundUnscaled(x * factor, sign, roundingMethod) / factor;
}
/** * Round the given non-negative, value to the "nearest" integer. Nearest is * determined by the rounding method specified. Rounding methods are defined * in {@link BigDecimal}. * * @param unscaled the value to round. * @param sign the sign of the original, scaled value. * @param roundingMethod the rounding method as defined in * {@link BigDecimal}. * @return the rounded value. * @since 1.1 */privatestaticdoubleroundUnscaled(doubleunscaled, doublesign,
introundingMethod) {
switch (roundingMethod) {
caseBigDecimal.ROUND_CEILING :
if (sign == -1) {
unscaled = Math.floor(nextAfter(unscaled, Double.NEGATIVE_INFINITY));
} else {
unscaled = Math.ceil(nextAfter(unscaled, Double.POSITIVE_INFINITY));
}
break;
caseBigDecimal.ROUND_DOWN :
unscaled = Math.floor(nextAfter(unscaled, Double.NEGATIVE_INFINITY));
break;
caseBigDecimal.ROUND_FLOOR :
if (sign == -1) {
unscaled = Math.ceil(nextAfter(unscaled, Double.POSITIVE_INFINITY));
} else {
unscaled = Math.floor(nextAfter(unscaled, Double.NEGATIVE_INFINITY));
}
break;
caseBigDecimal.ROUND_HALF_DOWN : {
unscaled = nextAfter(unscaled, Double.NEGATIVE_INFINITY);
doublefraction = unscaled - Math.floor(unscaled);
if (fraction > 0.5) {
unscaled = Math.ceil(unscaled);
} else {
unscaled = Math.floor(unscaled);
}
break;
}
caseBigDecimal.ROUND_HALF_EVEN : {
doublefraction = unscaled - Math.floor(unscaled);
if (fraction > 0.5) {
unscaled = Math.ceil(unscaled);
} elseif (fraction < 0.5) {
unscaled = Math.floor(unscaled);
} else {
// The following equality test is intentional and needed for rounding purposesif (Math.floor(unscaled) / 2.0 == Math.floor(Math
.floor(unscaled) / 2.0)) { // evenunscaled = Math.floor(unscaled);
} else { // oddunscaled = Math.ceil(unscaled);
}
}
break;
}
caseBigDecimal.ROUND_HALF_UP : {
unscaled = nextAfter(unscaled, Double.POSITIVE_INFINITY);
doublefraction = unscaled - Math.floor(unscaled);
if (fraction >= 0.5) {
unscaled = Math.ceil(unscaled);
} else {
unscaled = Math.floor(unscaled);
}
break;
}
caseBigDecimal.ROUND_UNNECESSARY :
if (unscaled != Math.floor(unscaled)) {
thrownewArithmeticException("Inexact result from rounding");
}
break;
caseBigDecimal.ROUND_UP :
unscaled = Math.ceil(nextAfter(unscaled, Double.POSITIVE_INFINITY));
break;
default :
throwMathRuntimeException.createIllegalArgumentException(
"invalid rounding method {0}, valid methods: {1} ({2}), {3} ({4})," +
" {5} ({6}), {7} ({8}), {9} ({10}), {11} ({12}), {13} ({14}), {15} ({16})",
roundingMethod,
"ROUND_CEILING", BigDecimal.ROUND_CEILING,
"ROUND_DOWN", BigDecimal.ROUND_DOWN,
"ROUND_FLOOR", BigDecimal.ROUND_FLOOR,
"ROUND_HALF_DOWN", BigDecimal.ROUND_HALF_DOWN,
"ROUND_HALF_EVEN", BigDecimal.ROUND_HALF_EVEN,
"ROUND_HALF_UP", BigDecimal.ROUND_HALF_UP,
"ROUND_UNNECESSARY", BigDecimal.ROUND_UNNECESSARY,
"ROUND_UP", BigDecimal.ROUND_UP);
}
returnunscaled;
}
/** * Returns the <a href="http://mathworld.wolfram.com/Sign.html"> sign</a> * for byte value <code>x</code>. * <p> * For a byte value x, this method returns (byte)(+1) if x > 0, (byte)(0) if * x = 0, and (byte)(-1) if x < 0.</p> * * @param x the value, a byte * @return (byte)(+1), (byte)(0), or (byte)(-1), depending on the sign of x */publicstaticbytesign(finalbytex) {
return (x == ZB) ? ZB : (x > ZB) ? PB : NB;
}
/** * Returns the <a href="http://mathworld.wolfram.com/Sign.html"> sign</a> * for double precision <code>x</code>. * <p> * For a double value <code>x</code>, this method returns * <code>+1.0</code> if <code>x > 0</code>, <code>0.0</code> if * <code>x = 0.0</code>, and <code>-1.0</code> if <code>x < 0</code>. * Returns <code>NaN</code> if <code>x</code> is <code>NaN</code>.</p> * * @param x the value, a double * @return +1.0, 0.0, or -1.0, depending on the sign of x */publicstaticdoublesign(finaldoublex) {
if (Double.isNaN(x)) {
returnDouble.NaN;
}
return (x == 0.0) ? 0.0 : (x > 0.0) ? 1.0 : -1.0;
}
/** * Returns the <a href="http://mathworld.wolfram.com/Sign.html"> sign</a> * for float value <code>x</code>. * <p> * For a float value x, this method returns +1.0F if x > 0, 0.0F if x = * 0.0F, and -1.0F if x < 0. Returns <code>NaN</code> if <code>x</code> * is <code>NaN</code>.</p> * * @param x the value, a float * @return +1.0F, 0.0F, or -1.0F, depending on the sign of x */publicstaticfloatsign(finalfloatx) {
if (Float.isNaN(x)) {
returnFloat.NaN;
}
return (x == 0.0F) ? 0.0F : (x > 0.0F) ? 1.0F : -1.0F;
}
/** * Returns the <a href="http://mathworld.wolfram.com/Sign.html"> sign</a> * for int value <code>x</code>. * <p> * For an int value x, this method returns +1 if x > 0, 0 if x = 0, and -1 * if x < 0.</p> * * @param x the value, an int * @return +1, 0, or -1, depending on the sign of x */publicstaticintsign(finalintx) {
return (x == 0) ? 0 : (x > 0) ? 1 : -1;
}
/** * Returns the <a href="http://mathworld.wolfram.com/Sign.html"> sign</a> * for long value <code>x</code>. * <p> * For a long value x, this method returns +1L if x > 0, 0L if x = 0, and * -1L if x < 0.</p> * * @param x the value, a long * @return +1L, 0L, or -1L, depending on the sign of x */publicstaticlongsign(finallongx) {
return (x == 0L) ? 0L : (x > 0L) ? 1L : -1L;
}
/** * Returns the <a href="http://mathworld.wolfram.com/Sign.html"> sign</a> * for short value <code>x</code>. * <p> * For a short value x, this method returns (short)(+1) if x > 0, (short)(0) * if x = 0, and (short)(-1) if x < 0.</p> * * @param x the value, a short * @return (short)(+1), (short)(0), or (short)(-1), depending on the sign of * x */publicstaticshortsign(finalshortx) {
return (x == ZS) ? ZS : (x > ZS) ? PS : NS;
}
/** * Returns the <a href="http://mathworld.wolfram.com/HyperbolicSine.html"> * hyperbolic sine</a> of x. * * @param x double value for which to find the hyperbolic sine * @return hyperbolic sine of x */publicstaticdoublesinh(doublex) {
return (Math.exp(x) - Math.exp(-x)) / 2.0;
}
/** * Subtract two integers, checking for overflow. * * @param x the minuend * @param y the subtrahend * @return the difference <code>x-y</code> * @throws ArithmeticException if the result can not be represented as an * int * @since 1.1 */publicstaticintsubAndCheck(intx, inty) {
longs = (long)x - (long)y;
if (s < Integer.MIN_VALUE || s > Integer.MAX_VALUE) {
thrownewArithmeticException("overflow: subtract");
}
return (int)s;
}
/** * Subtract two long integers, checking for overflow. * * @param a first value * @param b second value * @return the difference <code>a-b</code> * @throws ArithmeticException if the result can not be represented as an * long * @since 1.2 */publicstaticlongsubAndCheck(longa, longb) {
longret;
Stringmsg = "overflow: subtract";
if (b == Long.MIN_VALUE) {
if (a < 0) {
ret = a - b;
} else {
thrownewArithmeticException(msg);
}
} else {
// use additive inverseret = addAndCheck(a, -b, msg);
}
returnret;
}
/** * Raise an int to an int power. * @param k number to raise * @param e exponent (must be positive or null) * @return k<sup>e</sup> * @exception IllegalArgumentException if e is negative */publicstaticintpow(finalintk, inte)
throwsIllegalArgumentException {
if (e < 0) {
throwMathRuntimeException.createIllegalArgumentException(
"cannot raise an integral value to a negative power ({0}^{1})",
k, e);
}
intresult = 1;
intk2p = k;
while (e != 0) {
if ((e & 0x1) != 0) {
result *= k2p;
}
k2p *= k2p;
e = e >> 1;
}
returnresult;
}
/** * Raise an int to a long power. * @param k number to raise * @param e exponent (must be positive or null) * @return k<sup>e</sup> * @exception IllegalArgumentException if e is negative */publicstaticintpow(finalintk, longe)
throwsIllegalArgumentException {
if (e < 0) {
throwMathRuntimeException.createIllegalArgumentException(
"cannot raise an integral value to a negative power ({0}^{1})",
k, e);
}
intresult = 1;
intk2p = k;
while (e != 0) {
if ((e & 0x1) != 0) {
result *= k2p;
}
k2p *= k2p;
e = e >> 1;
}
returnresult;
}
/** * Raise a long to an int power. * @param k number to raise * @param e exponent (must be positive or null) * @return k<sup>e</sup> * @exception IllegalArgumentException if e is negative */publicstaticlongpow(finallongk, inte)
throwsIllegalArgumentException {
if (e < 0) {
throwMathRuntimeException.createIllegalArgumentException(
"cannot raise an integral value to a negative power ({0}^{1})",
k, e);
}
longresult = 1l;
longk2p = k;
while (e != 0) {
if ((e & 0x1) != 0) {
result *= k2p;
}
k2p *= k2p;
e = e >> 1;
}
returnresult;
}
/** * Raise a long to a long power. * @param k number to raise * @param e exponent (must be positive or null) * @return k<sup>e</sup> * @exception IllegalArgumentException if e is negative */publicstaticlongpow(finallongk, longe)
throwsIllegalArgumentException {
if (e < 0) {
throwMathRuntimeException.createIllegalArgumentException(
"cannot raise an integral value to a negative power ({0}^{1})",
k, e);
}
longresult = 1l;
longk2p = k;
while (e != 0) {
if ((e & 0x1) != 0) {
result *= k2p;
}
k2p *= k2p;
e = e >> 1;
}
returnresult;
}
/** * Raise a BigInteger to an int power. * @param k number to raise * @param e exponent (must be positive or null) * @return k<sup>e</sup> * @exception IllegalArgumentException if e is negative */publicstaticBigIntegerpow(finalBigIntegerk, inte)
throwsIllegalArgumentException {
if (e < 0) {
throwMathRuntimeException.createIllegalArgumentException(
"cannot raise an integral value to a negative power ({0}^{1})",
k, e);
}
returnk.pow(e);
}
/** * Raise a BigInteger to a long power. * @param k number to raise * @param e exponent (must be positive or null) * @return k<sup>e</sup> * @exception IllegalArgumentException if e is negative */publicstaticBigIntegerpow(finalBigIntegerk, longe)
throwsIllegalArgumentException {
if (e < 0) {
throwMathRuntimeException.createIllegalArgumentException(
"cannot raise an integral value to a negative power ({0}^{1})",
k, e);
}
BigIntegerresult = BigInteger.ONE;
BigIntegerk2p = k;
while (e != 0) {
if ((e & 0x1) != 0) {
result = result.multiply(k2p);
}
k2p = k2p.multiply(k2p);
e = e >> 1;
}
returnresult;
}
/** * Raise a BigInteger to a BigInteger power. * @param k number to raise * @param e exponent (must be positive or null) * @return k<sup>e</sup> * @exception IllegalArgumentException if e is negative */publicstaticBigIntegerpow(finalBigIntegerk, BigIntegere)
throwsIllegalArgumentException {
if (e.compareTo(BigInteger.ZERO) < 0) {
throwMathRuntimeException.createIllegalArgumentException(
"cannot raise an integral value to a negative power ({0}^{1})",
k, e);
}
BigIntegerresult = BigInteger.ONE;
BigIntegerk2p = k;
while (!BigInteger.ZERO.equals(e)) {
if (e.testBit(0)) {
result = result.multiply(k2p);
}
k2p = k2p.multiply(k2p);
e = e.shiftRight(1);
}
returnresult;
}
/** * Calculates the L<sub>1</sub> (sum of abs) distance between two points. * * @param p1 the first point * @param p2 the second point * @return the L<sub>1</sub> distance between the two points */publicstaticfinaldoubledistance1(double[] p1, double[] p2) {
doublesum = 0;
for (inti = 0; i < p1.length; i++) {
sum += Math.abs(p1[i] - p2[i]);
}
returnsum;
}
/** * Calculates the L<sub>1</sub> (sum of abs) distance between two points. * * @param p1 the first point * @param p2 the second point * @return the L<sub>1</sub> distance between the two points */publicstaticfinalintdistance1(int[] p1, int[] p2) {
intsum = 0;
for (inti = 0; i < p1.length; i++) {
sum += Math.abs(p1[i] - p2[i]);
}
returnsum;
}
/** * Calculates the L<sub>2</sub> (Euclidean) distance between two points. * * @param p1 the first point * @param p2 the second point * @return the L<sub>2</sub> distance between the two points */publicstaticfinaldoubledistance(double[] p1, double[] p2) {
doublesum = 0;
for (inti = 0; i < p1.length; i++) {
finaldoubledp = p1[i] - p2[i];
sum += dp * dp;
}
returnMath.sqrt(sum);
}
/** * Calculates the L<sub>2</sub> (Euclidean) distance between two points. * * @param p1 the first point * @param p2 the second point * @return the L<sub>2</sub> distance between the two points */publicstaticfinaldoubledistance(int[] p1, int[] p2) {
intsum = 0;
for (inti = 0; i < p1.length; i++) {
finalintdp = p1[i] - p2[i];
sum += dp * dp;
}
returnMath.sqrt(sum);
}
/** * Calculates the L<sub>∞</sub> (max of abs) distance between two points. * * @param p1 the first point * @param p2 the second point * @return the L<sub>∞</sub> distance between the two points */publicstaticfinaldoubledistanceInf(double[] p1, double[] p2) {
doublemax = 0;
for (inti = 0; i < p1.length; i++) {
max = Math.max(max, Math.abs(p1[i] - p2[i]));
}
returnmax;
}
/** * Calculates the L<sub>∞</sub> (max of abs) distance between two points. * * @param p1 the first point * @param p2 the second point * @return the L<sub>∞</sub> distance between the two points */publicstaticfinalintdistanceInf(int[] p1, int[] p2) {
intmax = 0;
for (inti = 0; i < p1.length; i++) {
max = Math.max(max, Math.abs(p1[i] - p2[i]));
}
returnmax;
}
}
The text was updated successfully, but these errors were encountered:
No arquivo
The text was updated successfully, but these errors were encountered: