当前位置: 首页>>代码示例>>Java>>正文


Java FastMath.abs方法代码示例

本文整理汇总了Java中org.apache.commons.math.util.FastMath.abs方法的典型用法代码示例。如果您正苦于以下问题:Java FastMath.abs方法的具体用法?Java FastMath.abs怎么用?Java FastMath.abs使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.commons.math.util.FastMath的用法示例。


在下文中一共展示了FastMath.abs方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: distance

import org.apache.commons.math.util.FastMath; //导入方法依赖的package包/类
/** Compute the shortest distance between the instance and another line.
 * @param line line to check agains the instance
 * @return shortest distance between the instance and the line
 */
public double distance(final Line line) {

    final Vector3D normal = Vector3D.crossProduct(direction, line.direction);
    if (normal.getNorm() < 1.0e-10) {
        // lines are parallel
        return distance(line.zero);
    }

    // separating middle plane
    final Plane middle = new Plane(new Vector3D(0.5, zero, 0.5, line.zero), normal);

    // the lines are at the same distance on either side of the plane
    return 2 * FastMath.abs(middle.getOffset(zero));

}
 
开发者ID:SpoonLabs,项目名称:astor,代码行数:20,代码来源:Line.java

示例2: testSmallError

import org.apache.commons.math.util.FastMath; //导入方法依赖的package包/类
@Test
public void testSmallError() {
    Random randomizer = new Random(53882150042l);
    double maxError = 0;
    for (int degree = 0; degree < 10; ++degree) {
        PolynomialFunction p = buildRandomPolynomial(degree, randomizer);

        PolynomialFitter fitter =
            new PolynomialFitter(degree, new LevenbergMarquardtOptimizer());
        for (double x = -1.0; x < 1.0; x += 0.01) {
            fitter.addObservedPoint(1.0, x,
                                    p.value(x) + 0.1 * randomizer.nextGaussian());
        }

        PolynomialFunction fitted = new PolynomialFunction(fitter.fit());

        for (double x = -1.0; x < 1.0; x += 0.01) {
            double error = FastMath.abs(p.value(x) - fitted.value(x)) /
                          (1.0 + FastMath.abs(p.value(x)));
            maxError = FastMath.max(maxError, error);
            Assert.assertTrue(FastMath.abs(error) < 0.1);
        }
    }
    Assert.assertTrue(maxError > 0.01);

}
 
开发者ID:SpoonLabs,项目名称:astor,代码行数:27,代码来源:PolynomialFitterTest.java

示例3: testSmallLastStep

import org.apache.commons.math.util.FastMath; //导入方法依赖的package包/类
@Test
public void testSmallLastStep()
  throws MathUserException, IntegratorException {

  TestProblemAbstract pb = new TestProblem5();
  double minStep = 1.25;
  double maxStep = FastMath.abs(pb.getFinalTime() - pb.getInitialTime());
  double scalAbsoluteTolerance = 6.0e-4;
  double scalRelativeTolerance = 6.0e-4;

  AdaptiveStepsizeIntegrator integ =
    new DormandPrince54Integrator(minStep, maxStep,
                                  scalAbsoluteTolerance,
                                  scalRelativeTolerance);

  DP54SmallLastHandler handler = new DP54SmallLastHandler(minStep);
  integ.addStepHandler(handler);
  integ.setInitialStepSize(1.7);
  integ.integrate(pb,
                  pb.getInitialTime(), pb.getInitialState(),
                  pb.getFinalTime(), new double[pb.getDimension()]);
  Assert.assertTrue(handler.wasLastSeen());
  Assert.assertEquals("Dormand-Prince 5(4)", integ.getName());

}
 
开发者ID:SpoonLabs,项目名称:astor,代码行数:26,代码来源:DormandPrince54IntegratorTest.java

示例4: testSinFunction

import org.apache.commons.math.util.FastMath; //导入方法依赖的package包/类
/**
 * Test of integrator for the sine function.
 */
public void testSinFunction() throws MathException {
    UnivariateRealFunction f = new SinFunction();
    UnivariateRealIntegrator integrator = new RombergIntegrator();
    double min, max, expected, result, tolerance;

    min = 0; max = FastMath.PI; expected = 2;
    tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
    result = integrator.integrate(f, min, max);
    assertEquals(expected, result, tolerance);

    min = -FastMath.PI/3; max = 0; expected = -0.5;
    tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
    result = integrator.integrate(f, min, max);
    assertEquals(expected, result, tolerance);
}
 
开发者ID:SpoonLabs,项目名称:astor,代码行数:19,代码来源:RombergIntegratorTest.java

示例5: testBackward

import org.apache.commons.math.util.FastMath; //导入方法依赖的package包/类
@Test
public void testBackward()
    throws MathUserException, IntegratorException {

    TestProblem5 pb = new TestProblem5();
    double step = FastMath.abs(pb.getFinalTime() - pb.getInitialTime()) * 0.001;

    FirstOrderIntegrator integ = new MidpointIntegrator(step);
    TestProblemHandler handler = new TestProblemHandler(pb, integ);
    integ.addStepHandler(handler);
    integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(),
                    pb.getFinalTime(), new double[pb.getDimension()]);

    Assert.assertTrue(handler.getLastError() < 6.0e-4);
    Assert.assertTrue(handler.getMaximalValueError() < 6.0e-4);
    Assert.assertEquals(0, handler.getMaximalTimeError(), 1.0e-12);
    Assert.assertEquals("midpoint", integ.getName());
}
 
开发者ID:SpoonLabs,项目名称:astor,代码行数:19,代码来源:MidpointIntegratorTest.java

示例6: handleStep

import org.apache.commons.math.util.FastMath; //导入方法依赖的package包/类
public void handleStep(StepInterpolator interpolator,
                       boolean isLast) {

  double step = FastMath.abs(interpolator.getCurrentTime()
                         - interpolator.getPreviousTime());
  if (firstTime) {
    minStep   = FastMath.abs(step);
    maxStep   = minStep;
    firstTime = false;
  } else {
    if (step < minStep) {
      minStep = step;
    }
    if (step > maxStep) {
      maxStep = step;
    }
  }

  if (isLast) {
    assertTrue(minStep < (1.0 / 100.0));
    assertTrue(maxStep > (1.0 / 2.0));
  }
}
 
开发者ID:SpoonLabs,项目名称:astor,代码行数:24,代码来源:DormandPrince853IntegratorTest.java

示例7: testQuinticFunction

import org.apache.commons.math.util.FastMath; //导入方法依赖的package包/类
/**
 * Test of integrator for the quintic function.
 */
public void testQuinticFunction() throws MathException {
    UnivariateRealFunction f = new QuinticFunction();
    UnivariateRealIntegrator integrator = new RombergIntegrator();
    double min, max, expected, result, tolerance;

    min = 0; max = 1; expected = -1.0/48;
    tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
    result = integrator.integrate(f, min, max);
    assertEquals(expected, result, tolerance);

    min = 0; max = 0.5; expected = 11.0/768;
    tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
    result = integrator.integrate(f, min, max);
    assertEquals(expected, result, tolerance);

    min = -1; max = 4; expected = 2048/3.0 - 78 + 1.0/48;
    tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
    result = integrator.integrate(f, min, max);
    assertEquals(expected, result, tolerance);
}
 
开发者ID:SpoonLabs,项目名称:astor,代码行数:24,代码来源:RombergIntegratorTest.java

示例8: testSinFunction

import org.apache.commons.math.util.FastMath; //导入方法依赖的package包/类
/**
 * Test of interpolator for the sine function.
 * <p>
 * |sin^(n)(zeta)| <= 1.0, zeta in [0, 2*PI]
 */
public void testSinFunction() {
    UnivariateRealFunction f = new SinFunction();
    UnivariateRealInterpolator interpolator = new DividedDifferenceInterpolator();
    double x[], y[], z, expected, result, tolerance;

    // 6 interpolating points on interval [0, 2*PI]
    int n = 6;
    double min = 0.0, max = 2 * FastMath.PI;
    x = new double[n];
    y = new double[n];
    for (int i = 0; i < n; i++) {
        x[i] = min + i * (max - min) / n;
        y[i] = f.value(x[i]);
    }
    double derivativebound = 1.0;
    UnivariateRealFunction p = interpolator.interpolate(x, y);

    z = FastMath.PI / 4; expected = f.value(z); result = p.value(z);
    tolerance = FastMath.abs(derivativebound * partialerror(x, z));
    assertEquals(expected, result, tolerance);

    z = FastMath.PI * 1.5; expected = f.value(z); result = p.value(z);
    tolerance = FastMath.abs(derivativebound * partialerror(x, z));
    assertEquals(expected, result, tolerance);
}
 
开发者ID:SpoonLabs,项目名称:astor,代码行数:31,代码来源:DividedDifferenceInterpolatorTest.java

示例9: testBackward

import org.apache.commons.math.util.FastMath; //导入方法依赖的package包/类
@Test
public void testBackward()
    throws MathUserException, IntegratorException {

    TestProblem5 pb = new TestProblem5();
    double step = FastMath.abs(pb.getFinalTime() - pb.getInitialTime()) * 0.001;

    FirstOrderIntegrator integ = new EulerIntegrator(step);
    TestProblemHandler handler = new TestProblemHandler(pb, integ);
    integ.addStepHandler(handler);
    integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(),
                    pb.getFinalTime(), new double[pb.getDimension()]);

    Assert.assertTrue(handler.getLastError() < 0.45);
    Assert.assertTrue(handler.getMaximalValueError() < 0.45);
    Assert.assertEquals(0, handler.getMaximalTimeError(), 1.0e-12);
    Assert.assertEquals("Euler", integ.getName());
}
 
开发者ID:SpoonLabs,项目名称:astor,代码行数:19,代码来源:EulerIntegratorTest.java

示例10: computeOmega

import org.apache.commons.math.util.FastMath; //导入方法依赖的package包/类
/** Computes the n<sup>th</sup> roots of unity.
 * <p>The computed omega[] = { 1, w, w<sup>2</sup>, ... w<sup>(n-1)</sup> } where
 * w = exp(-2 &pi; i / n), i = &sqrt;(-1).</p>
 * <p>Note that n is positive for
 * forward transform and negative for inverse transform.</p>
 * @param n number of roots of unity to compute,
 * positive for forward transform, negative for inverse transform
 * @throws IllegalArgumentException if n = 0
 */
public synchronized void computeOmega(int n) throws IllegalArgumentException {

  if (n == 0) {
    throw MathRuntimeException.createIllegalArgumentException(
            LocalizedFormats.CANNOT_COMPUTE_0TH_ROOT_OF_UNITY);
  }

  isForward = n > 0;

  // avoid repetitive calculations
  final int absN = FastMath.abs(n);

  if (absN == omegaCount) {
      return;
  }

  // calculate everything from scratch, for both forward and inverse versions
  final double t    = 2.0 * FastMath.PI / absN;
  final double cosT = FastMath.cos(t);
  final double sinT = FastMath.sin(t);
  omegaReal             = new double[absN];
  omegaImaginaryForward = new double[absN];
  omegaImaginaryInverse = new double[absN];
  omegaReal[0]             = 1.0;
  omegaImaginaryForward[0] = 0.0;
  omegaImaginaryInverse[0] = 0.0;
  for (int i = 1; i < absN; i++) {
    omegaReal[i] =
      omegaReal[i-1] * cosT + omegaImaginaryForward[i-1] * sinT;
    omegaImaginaryForward[i] =
       omegaImaginaryForward[i-1] * cosT - omegaReal[i-1] * sinT;
    omegaImaginaryInverse[i] = -omegaImaginaryForward[i];
  }
  omegaCount = absN;

}
 
开发者ID:SpoonLabs,项目名称:astor,代码行数:46,代码来源:FastFourierTransformer.java

示例11: RungeKuttaIntegrator

import org.apache.commons.math.util.FastMath; //导入方法依赖的package包/类
/** Simple constructor.
 * Build a Runge-Kutta integrator with the given
 * step. The default step handler does nothing.
 * @param name name of the method
 * @param c time steps from Butcher array (without the first zero)
 * @param a internal weights from Butcher array (without the first empty row)
 * @param b propagation weights for the high order method from Butcher array
 * @param prototype prototype of the step interpolator to use
 * @param step integration step
 */
protected RungeKuttaIntegrator(final String name,
                               final double[] c, final double[][] a, final double[] b,
                               final RungeKuttaStepInterpolator prototype,
                               final double step) {
  super(name);
  this.c          = c;
  this.a          = a;
  this.b          = b;
  this.prototype  = prototype;
  this.step       = FastMath.abs(step);
}
 
开发者ID:SpoonLabs,项目名称:astor,代码行数:22,代码来源:RungeKuttaIntegrator.java

示例12: computeTheoreticalState

import org.apache.commons.math.util.FastMath; //导入方法依赖的package包/类
@Override
public double[] computeTheoreticalState(double t) {

  // solve Kepler's equation
  double E = t;
  double d = 0;
  double corr = 999.0;
  for (int i = 0; (i < 50) && (FastMath.abs(corr) > 1.0e-12); ++i) {
    double f2  = e * FastMath.sin(E);
    double f0  = d - f2;
    double f1  = 1 - e * FastMath.cos(E);
    double f12 = f1 + f1;
    corr  = f0 * f12 / (f1 * f12 - f0 * f2);
    d -= corr;
    E = t + d;
  }

  double cosE = FastMath.cos(E);
  double sinE = FastMath.sin(E);

  y[0] = cosE - e;
  y[1] = FastMath.sqrt(1 - e * e) * sinE;
  y[2] = -sinE / (1 - e * cosE);
  y[3] = FastMath.sqrt(1 - e * e) * cosE / (1 - e * cosE);

  return y;
}
 
开发者ID:SpoonLabs,项目名称:astor,代码行数:28,代码来源:TestProblem3.java

示例13: doSolve

import org.apache.commons.math.util.FastMath; //导入方法依赖的package包/类
/**
 * {@inheritDoc}
 */
@Override
protected double doSolve() {
    final double min = getMin();
    final double max = getMax();
    final double initial = getStartValue();

    final double functionValueAccuracy = getFunctionValueAccuracy();

    verifySequence(min, initial, max);

    // check for zeros before verifying bracketing
    final double fMin = computeObjectiveValue(min);
    if (FastMath.abs(fMin) < functionValueAccuracy) {
        return min;
    }
    final double fMax = computeObjectiveValue(max);
    if (FastMath.abs(fMax) < functionValueAccuracy) {
        return max;
    }
    final double fInitial = computeObjectiveValue(initial);
    if (FastMath.abs(fInitial) <  functionValueAccuracy) {
        return initial;
    }

    verifyBracketing(min, max);

    if (isBracketing(min, initial)) {
        return solve(min, initial, fMin, fInitial);
    } else {
        return solve(initial, max, fInitial, fMax);
    }
}
 
开发者ID:SpoonLabs,项目名称:astor,代码行数:36,代码来源:MullerSolver.java

示例14: integrate

import org.apache.commons.math.util.FastMath; //导入方法依赖的package包/类
/** {@inheritDoc} */
public double integrate(final UnivariateRealFunction f, final double min, final double max)
    throws ConvergenceException,  MathUserException, IllegalArgumentException {

    clearResult();
    verifyInterval(min, max);
    verifyIterationCount();

    // compute first estimate with a single step
    double oldt = stage(f, min, max, 1);

    int n = 2;
    for (int i = 0; i < maximalIterationCount; ++i) {

        // improve integral with a larger number of steps
        final double t = stage(f, min, max, n);

        // estimate error
        final double delta = FastMath.abs(t - oldt);
        final double limit =
            FastMath.max(absoluteAccuracy,
                     relativeAccuracy * (FastMath.abs(oldt) + FastMath.abs(t)) * 0.5);

        // check convergence
        if ((i + 1 >= minimalIterationCount) && (delta <= limit)) {
            setResult(t, i);
            return result;
        }

        // prepare next iteration
        double ratio = FastMath.min(4, FastMath.pow(delta / limit, 0.5 / abscissas.length));
        n = FastMath.max((int) (ratio * n), n + 1);
        oldt = t;

    }

    throw new MaxCountExceededException(maximalIterationCount);

}
 
开发者ID:SpoonLabs,项目名称:astor,代码行数:40,代码来源:LegendreGaussIntegrator.java

示例15: doSolve

import org.apache.commons.math.util.FastMath; //导入方法依赖的package包/类
/**
 * {@inheritDoc}
 */
@Override
protected double doSolve() {
    double min = getMin();
    double max = getMax();
    // [x1, x2] is the bracketing interval in each iteration
    // x3 is the midpoint of [x1, x2]
    // x is the new root approximation and an endpoint of the new interval
    double x1 = min;
    double y1 = computeObjectiveValue(x1);
    double x2 = max;
    double y2 = computeObjectiveValue(x2);

    // check for zeros before verifying bracketing
    if (y1 == 0) {
        return min;
    }
    if (y2 == 0) {
        return max;
    }
    verifyBracketing(min, max);

    final double absoluteAccuracy = getAbsoluteAccuracy();
    final double functionValueAccuracy = getFunctionValueAccuracy();
    final double relativeAccuracy = getRelativeAccuracy();

    double oldx = Double.POSITIVE_INFINITY;
    while (true) {
        // calculate the new root approximation
        final double x3 = 0.5 * (x1 + x2);
        final double y3 = computeObjectiveValue(x3);
        if (FastMath.abs(y3) <= functionValueAccuracy) {
            return x3;
        }
        final double delta = 1 - (y1 * y2) / (y3 * y3);  // delta > 1 due to bracketing
        final double correction = (MathUtils.sign(y2) * MathUtils.sign(y3)) *
                                  (x3 - x1) / FastMath.sqrt(delta);
        final double x = x3 - correction;                // correction != 0
        final double y = computeObjectiveValue(x);

        // check for convergence
        final double tolerance = FastMath.max(relativeAccuracy * FastMath.abs(x), absoluteAccuracy);
        if (FastMath.abs(x - oldx) <= tolerance) {
            return x;
        }
        if (FastMath.abs(y) <= functionValueAccuracy) {
            return x;
        }

        // prepare the new interval for next iteration
        // Ridders' method guarantees x1 < x < x2
        if (correction > 0.0) {             // x1 < x < x3
            if (MathUtils.sign(y1) + MathUtils.sign(y) == 0.0) {
                x2 = x;
                y2 = y;
            } else {
                x1 = x;
                x2 = x3;
                y1 = y;
                y2 = y3;
            }
        } else {                            // x3 < x < x2
            if (MathUtils.sign(y2) + MathUtils.sign(y) == 0.0) {
                x1 = x;
                y1 = y;
            } else {
                x1 = x3;
                x2 = x;
                y1 = y3;
                y2 = y;
            }
        }
        oldx = x;
    }
}
 
开发者ID:SpoonLabs,项目名称:astor,代码行数:78,代码来源:RiddersSolver.java


注:本文中的org.apache.commons.math.util.FastMath.abs方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。