Java Code Examples for org.apache.commons.math.util.FastMath#atan()

The following examples show how to use org.apache.commons.math.util.FastMath#atan() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MinpackTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
public double[] value(double[] variables) {
  double x1 = variables[0];
  double x2 = variables[1];
  double x3 = variables[2];
  double tmp1;
  if (x1 == 0) {
    tmp1 = (x2 >= 0) ? 0.25 : -0.25;
  } else {
    tmp1 = FastMath.atan(x2 / x1) / twoPi;
    if (x1 < 0) {
      tmp1 += 0.5;
    }
  }
  double tmp2 = FastMath.sqrt(x1 * x1 + x2 * x2);
  return new double[] {
    10.0 * (x3 - 10 * tmp1),
    10.0 * (tmp2 - 1),
    x3
  };
}
 
Example 2
Source File: MinpackTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
public double[] value(double[] variables) {
  double x1 = variables[0];
  double x2 = variables[1];
  double x3 = variables[2];
  double tmp1;
  if (x1 == 0) {
    tmp1 = (x2 >= 0) ? 0.25 : -0.25;
  } else {
    tmp1 = FastMath.atan(x2 / x1) / twoPi;
    if (x1 < 0) {
      tmp1 += 0.5;
    }
  }
  double tmp2 = FastMath.sqrt(x1 * x1 + x2 * x2);
  return new double[] {
    10.0 * (x3 - 10 * tmp1),
    10.0 * (tmp2 - 1),
    x3
  };
}
 
Example 3
Source File: MinpackTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
public double[] value(double[] variables) {
  double x1 = variables[0];
  double x2 = variables[1];
  double x3 = variables[2];
  double tmp1;
  if (x1 == 0) {
    tmp1 = (x2 >= 0) ? 0.25 : -0.25;
  } else {
    tmp1 = FastMath.atan(x2 / x1) / twoPi;
    if (x1 < 0) {
      tmp1 += 0.5;
    }
  }
  double tmp2 = FastMath.sqrt(x1 * x1 + x2 * x2);
  return new double[] {
    10.0 * (x3 - 10 * tmp1),
    10.0 * (tmp2 - 1),
    x3
  };
}
 
Example 4
Source File: MinpackTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
public double[] value(double[] variables) {
  double x1 = variables[0];
  double x2 = variables[1];
  double x3 = variables[2];
  double tmp1;
  if (x1 == 0) {
    tmp1 = (x2 >= 0) ? 0.25 : -0.25;
  } else {
    tmp1 = FastMath.atan(x2 / x1) / twoPi;
    if (x1 < 0) {
      tmp1 += 0.5;
    }
  }
  double tmp2 = FastMath.sqrt(x1 * x1 + x2 * x2);
  return new double[] {
    10.0 * (x3 - 10 * tmp1),
    10.0 * (tmp2 - 1),
    x3
  };
}
 
Example 5
Source File: MinpackTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected double[] getResiduals() {
  double x1 = parameters[0].getEstimate();
  double x2 = parameters[1].getEstimate();
  double x3 = parameters[2].getEstimate();
  double tmp1;
  if (x1 == 0) {
    tmp1 = (x2 >= 0) ? 0.25 : -0.25;
  } else {
    tmp1 = FastMath.atan(x2 / x1) / twoPi;
    if (x1 < 0) {
      tmp1 += 0.5;
    }
  }
  double tmp2 = FastMath.sqrt(x1 * x1 + x2 * x2);
  return new double[] {
    10.0 * (x3 - 10 * tmp1),
    10.0 * (tmp2 - 1),
    x3
  };
}
 
Example 6
Source File: ArrayRealVector.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public RealVector mapAtanToSelf() {
    for (int i = 0; i < data.length; i++) {
        data[i] = FastMath.atan(data[i]);
    }
    return this;
}
 
Example 7
Source File: SimplexOptimizerMultiDirectionalTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testMinimizeMaximize() {
    // the following function has 4 local extrema:
    final double xM        = -3.841947088256863675365;
    final double yM        = -1.391745200270734924416;
    final double xP        =  0.2286682237349059125691;
    final double yP        = -yM;
    final double valueXmYm =  0.2373295333134216789769; // local  maximum
    final double valueXmYp = -valueXmYm;                // local  minimum
    final double valueXpYm = -0.7290400707055187115322; // global minimum
    final double valueXpYp = -valueXpYm;                // global maximum
    MultivariateRealFunction fourExtrema = new MultivariateRealFunction() {
            public double value(double[] variables) {
                final double x = variables[0];
                final double y = variables[1];
                return ((x == 0) || (y == 0)) ? 0 :
                    (FastMath.atan(x) * FastMath.atan(x + 2) * FastMath.atan(y) * FastMath.atan(y) / (x * y));
            }
        };

    SimplexOptimizer optimizer = new SimplexOptimizer(1e-11, 1e-30);
    optimizer.setSimplex(new MultiDirectionalSimplex(new double[] { 0.2, 0.2 }));
    RealPointValuePair optimum;

    // minimization
    optimum = optimizer.optimize(200, fourExtrema, GoalType.MINIMIZE, new double[] { -3, 0 });
    Assert.assertEquals(xM,        optimum.getPoint()[0], 4e-6);
    Assert.assertEquals(yP,        optimum.getPoint()[1], 3e-6);
    Assert.assertEquals(valueXmYp, optimum.getValue(),    8e-13);
    Assert.assertTrue(optimizer.getEvaluations() > 120);
    Assert.assertTrue(optimizer.getEvaluations() < 150);

    optimum = optimizer.optimize(200, fourExtrema, GoalType.MINIMIZE, new double[] { 1, 0 });
    Assert.assertEquals(xP,        optimum.getPoint()[0], 2e-8);
    Assert.assertEquals(yM,        optimum.getPoint()[1], 3e-6);
    Assert.assertEquals(valueXpYm, optimum.getValue(),    2e-12);
    Assert.assertTrue(optimizer.getEvaluations() > 120);
    Assert.assertTrue(optimizer.getEvaluations() < 150);

    // maximization
    optimum = optimizer.optimize(200, fourExtrema, GoalType.MAXIMIZE, new double[] { -3.0, 0.0 });
    Assert.assertEquals(xM,        optimum.getPoint()[0], 7e-7);
    Assert.assertEquals(yM,        optimum.getPoint()[1], 3e-7);
    Assert.assertEquals(valueXmYm, optimum.getValue(),    2e-14);
    Assert.assertTrue(optimizer.getEvaluations() > 120);
    Assert.assertTrue(optimizer.getEvaluations() < 150);

    optimizer.setConvergenceChecker(new SimpleScalarValueChecker(1e-15, 1e-30));
    optimum = optimizer.optimize(200, fourExtrema, GoalType.MAXIMIZE, new double[] { 1, 0 });
    Assert.assertEquals(xP,        optimum.getPoint()[0], 2e-8);
    Assert.assertEquals(yP,        optimum.getPoint()[1], 3e-6);
    Assert.assertEquals(valueXpYp, optimum.getValue(),    2e-12);
    Assert.assertTrue(optimizer.getEvaluations() > 180);
    Assert.assertTrue(optimizer.getEvaluations() < 220);
}
 
Example 8
Source File: Atan.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc} */
public double value(double x) {
    return FastMath.atan(x);
}
 
Example 9
Source File: SimplexOptimizerMultiDirectionalTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testMinimizeMaximize() {
    // the following function has 4 local extrema:
    final double xM        = -3.841947088256863675365;
    final double yM        = -1.391745200270734924416;
    final double xP        =  0.2286682237349059125691;
    final double yP        = -yM;
    final double valueXmYm =  0.2373295333134216789769; // local  maximum
    final double valueXmYp = -valueXmYm;                // local  minimum
    final double valueXpYm = -0.7290400707055187115322; // global minimum
    final double valueXpYp = -valueXpYm;                // global maximum
    MultivariateRealFunction fourExtrema = new MultivariateRealFunction() {
            public double value(double[] variables) {
                final double x = variables[0];
                final double y = variables[1];
                return ((x == 0) || (y == 0)) ? 0 :
                    (FastMath.atan(x) * FastMath.atan(x + 2) * FastMath.atan(y) * FastMath.atan(y) / (x * y));
            }
        };

    SimplexOptimizer optimizer = new SimplexOptimizer(1e-11, 1e-30);
    optimizer.setSimplex(new MultiDirectionalSimplex(new double[] { 0.2, 0.2 }));
    RealPointValuePair optimum;

    // minimization
    optimum = optimizer.optimize(200, fourExtrema, GoalType.MINIMIZE, new double[] { -3, 0 });
    Assert.assertEquals(xM,        optimum.getPoint()[0], 4e-6);
    Assert.assertEquals(yP,        optimum.getPoint()[1], 3e-6);
    Assert.assertEquals(valueXmYp, optimum.getValue(),    8e-13);
    Assert.assertTrue(optimizer.getEvaluations() > 120);
    Assert.assertTrue(optimizer.getEvaluations() < 150);

    optimum = optimizer.optimize(200, fourExtrema, GoalType.MINIMIZE, new double[] { 1, 0 });
    Assert.assertEquals(xP,        optimum.getPoint()[0], 2e-8);
    Assert.assertEquals(yM,        optimum.getPoint()[1], 3e-6);
    Assert.assertEquals(valueXpYm, optimum.getValue(),    2e-12);
    Assert.assertTrue(optimizer.getEvaluations() > 120);
    Assert.assertTrue(optimizer.getEvaluations() < 150);

    // maximization
    optimum = optimizer.optimize(200, fourExtrema, GoalType.MAXIMIZE, new double[] { -3.0, 0.0 });
    Assert.assertEquals(xM,        optimum.getPoint()[0], 7e-7);
    Assert.assertEquals(yM,        optimum.getPoint()[1], 3e-7);
    Assert.assertEquals(valueXmYm, optimum.getValue(),    2e-14);
    Assert.assertTrue(optimizer.getEvaluations() > 120);
    Assert.assertTrue(optimizer.getEvaluations() < 150);

    optimizer.setConvergenceChecker(new SimpleScalarValueChecker(1e-15, 1e-30));
    optimum = optimizer.optimize(200, fourExtrema, GoalType.MAXIMIZE, new double[] { 1, 0 });
    Assert.assertEquals(xP,        optimum.getPoint()[0], 2e-8);
    Assert.assertEquals(yP,        optimum.getPoint()[1], 3e-6);
    Assert.assertEquals(valueXpYp, optimum.getValue(),    2e-12);
    Assert.assertTrue(optimizer.getEvaluations() > 180);
    Assert.assertTrue(optimizer.getEvaluations() < 220);
}
 
Example 10
Source File: Atan.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc} */
public double value(double x) {
    return FastMath.atan(x);
}
 
Example 11
Source File: MultiDirectionalTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testMinimizeMaximize()
    throws FunctionEvaluationException, ConvergenceException {

    // the following function has 4 local extrema:
    final double xM        = -3.841947088256863675365;
    final double yM        = -1.391745200270734924416;
    final double xP        =  0.2286682237349059125691;
    final double yP        = -yM;
    final double valueXmYm =  0.2373295333134216789769; // local  maximum
    final double valueXmYp = -valueXmYm;                // local  minimum
    final double valueXpYm = -0.7290400707055187115322; // global minimum
    final double valueXpYp = -valueXpYm;                // global maximum
    MultivariateRealFunction fourExtrema = new MultivariateRealFunction() {
        private static final long serialVersionUID = -7039124064449091152L;
        public double value(double[] variables) throws FunctionEvaluationException {
            final double x = variables[0];
            final double y = variables[1];
            return ((x == 0) || (y == 0)) ? 0 : (FastMath.atan(x) * FastMath.atan(x + 2) * FastMath.atan(y) * FastMath.atan(y) / (x * y));
        }
    };

    MultiDirectional optimizer = new MultiDirectional();
    optimizer.setConvergenceChecker(new SimpleScalarValueChecker(1.0e-11, 1.0e-30));
    optimizer.setMaxEvaluations(200);
    optimizer.setStartConfiguration(new double[] { 0.2, 0.2 });
    RealPointValuePair optimum;

    // minimization
    optimum = optimizer.optimize(fourExtrema, GoalType.MINIMIZE, new double[] { -3.0, 0 });
    Assert.assertEquals(xM,        optimum.getPoint()[0], 4.0e-6);
    Assert.assertEquals(yP,        optimum.getPoint()[1], 3.0e-6);
    Assert.assertEquals(valueXmYp, optimum.getValue(),    8.0e-13);
    Assert.assertTrue(optimizer.getEvaluations() > 120);
    Assert.assertTrue(optimizer.getEvaluations() < 150);

    optimum = optimizer.optimize(fourExtrema, GoalType.MINIMIZE, new double[] { +1, 0 });
    Assert.assertEquals(xP,        optimum.getPoint()[0], 2.0e-8);
    Assert.assertEquals(yM,        optimum.getPoint()[1], 3.0e-6);
    Assert.assertEquals(valueXpYm, optimum.getValue(),    2.0e-12);
    Assert.assertTrue(optimizer.getEvaluations() > 120);
    Assert.assertTrue(optimizer.getEvaluations() < 150);

    // maximization
    optimum = optimizer.optimize(fourExtrema, GoalType.MAXIMIZE, new double[] { -3.0, 0.0 });
    Assert.assertEquals(xM,        optimum.getPoint()[0], 7.0e-7);
    Assert.assertEquals(yM,        optimum.getPoint()[1], 3.0e-7);
    Assert.assertEquals(valueXmYm, optimum.getValue(),    2.0e-14);
    Assert.assertTrue(optimizer.getEvaluations() > 120);
    Assert.assertTrue(optimizer.getEvaluations() < 150);

    optimizer.setConvergenceChecker(new SimpleScalarValueChecker(1.0e-15, 1.0e-30));
    optimum = optimizer.optimize(fourExtrema, GoalType.MAXIMIZE, new double[] { +1, 0 });
    Assert.assertEquals(xP,        optimum.getPoint()[0], 2.0e-8);
    Assert.assertEquals(yP,        optimum.getPoint()[1], 3.0e-6);
    Assert.assertEquals(valueXpYp, optimum.getValue(),    2.0e-12);
    Assert.assertTrue(optimizer.getEvaluations() > 180);
    Assert.assertTrue(optimizer.getEvaluations() < 220);

}
 
Example 12
Source File: ComposableFunction.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public double value(double d) {
    return FastMath.atan(d);
}
 
Example 13
Source File: SimplexOptimizerMultiDirectionalTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testMinimizeMaximize() {
    // the following function has 4 local extrema:
    final double xM        = -3.841947088256863675365;
    final double yM        = -1.391745200270734924416;
    final double xP        =  0.2286682237349059125691;
    final double yP        = -yM;
    final double valueXmYm =  0.2373295333134216789769; // local  maximum
    final double valueXmYp = -valueXmYm;                // local  minimum
    final double valueXpYm = -0.7290400707055187115322; // global minimum
    final double valueXpYp = -valueXpYm;                // global maximum
    MultivariateRealFunction fourExtrema = new MultivariateRealFunction() {
            private static final long serialVersionUID = -7039124064449091152L;
            public double value(double[] variables) {
                final double x = variables[0];
                final double y = variables[1];
                return ((x == 0) || (y == 0)) ? 0 :
                    (FastMath.atan(x) * FastMath.atan(x + 2) * FastMath.atan(y) * FastMath.atan(y) / (x * y));
            }
        };

    SimplexOptimizer optimizer = new SimplexOptimizer(1e-11, 1e-30);
    optimizer.setSimplex(new MultiDirectionalSimplex(new double[] { 0.2, 0.2 }));
    RealPointValuePair optimum;

    // minimization
    optimum = optimizer.optimize(200, fourExtrema, GoalType.MINIMIZE, new double[] { -3, 0 });
    Assert.assertEquals(xM,        optimum.getPoint()[0], 4e-6);
    Assert.assertEquals(yP,        optimum.getPoint()[1], 3e-6);
    Assert.assertEquals(valueXmYp, optimum.getValue(),    8e-13);
    Assert.assertTrue(optimizer.getEvaluations() > 120);
    Assert.assertTrue(optimizer.getEvaluations() < 150);

    optimum = optimizer.optimize(200, fourExtrema, GoalType.MINIMIZE, new double[] { 1, 0 });
    Assert.assertEquals(xP,        optimum.getPoint()[0], 2e-8);
    Assert.assertEquals(yM,        optimum.getPoint()[1], 3e-6);
    Assert.assertEquals(valueXpYm, optimum.getValue(),    2e-12);
    Assert.assertTrue(optimizer.getEvaluations() > 120);
    Assert.assertTrue(optimizer.getEvaluations() < 150);

    // maximization
    optimum = optimizer.optimize(200, fourExtrema, GoalType.MAXIMIZE, new double[] { -3.0, 0.0 });
    Assert.assertEquals(xM,        optimum.getPoint()[0], 7e-7);
    Assert.assertEquals(yM,        optimum.getPoint()[1], 3e-7);
    Assert.assertEquals(valueXmYm, optimum.getValue(),    2e-14);
    Assert.assertTrue(optimizer.getEvaluations() > 120);
    Assert.assertTrue(optimizer.getEvaluations() < 150);

    optimizer.setConvergenceChecker(new SimpleScalarValueChecker(1e-15, 1e-30));
    optimum = optimizer.optimize(200, fourExtrema, GoalType.MAXIMIZE, new double[] { 1, 0 });
    Assert.assertEquals(xP,        optimum.getPoint()[0], 2e-8);
    Assert.assertEquals(yP,        optimum.getPoint()[1], 3e-6);
    Assert.assertEquals(valueXpYp, optimum.getValue(),    2e-12);
    Assert.assertTrue(optimizer.getEvaluations() > 180);
    Assert.assertTrue(optimizer.getEvaluations() < 220);
}
 
Example 14
Source File: Atan.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc} */
public double value(double x) {
    return FastMath.atan(x);
}
 
Example 15
Source File: CauchyDistributionImpl.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * For this distribution, {@code X}, this method returns {@code P(X < x)}.
 *
 * @param x Value at which the CDF is evaluated.
 * @return CDF evaluated at {@code x}.
 */
public double cumulativeProbability(double x) {
    return 0.5 + (FastMath.atan((x - median) / scale) / FastMath.PI);
}
 
Example 16
Source File: CauchyDistributionImpl.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * For this distribution, X, this method returns P(X &lt; <code>x</code>).
 * @param x the value at which the CDF is evaluated.
 * @return CDF evaluted at <code>x</code>.
 */
public double cumulativeProbability(double x) {
    return 0.5 + (FastMath.atan((x - median) / scale) / FastMath.PI);
}
 
Example 17
Source File: CauchyDistributionImpl.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * For this distribution, {@code X}, this method returns {@code P(X < x)}.
 *
 * @param x Value at which the CDF is evaluated.
 * @return CDF evaluated at {@code x}.
 */
public double cumulativeProbability(double x) {
    return 0.5 + (FastMath.atan((x - median) / scale) / FastMath.PI);
}
 
Example 18
Source File: CauchyDistributionImpl.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * For this distribution, {@code X}, this method returns {@code P(X < x)}.
 *
 * @param x Value at which the CDF is evaluated.
 * @return CDF evaluated at {@code x}.
 */
public double cumulativeProbability(double x) {
    return 0.5 + (FastMath.atan((x - median) / scale) / FastMath.PI);
}