예제 #1
0
 public static List<Double> cbrt(List<Double> data) {
   List<Double> t_list = new ArrayList<Double>(data.size());
   for (double i : data) {
     t_list.add(Math.cbrt(i));
   }
   return t_list;
 }
예제 #2
0
  /**
   * Solve the cubic whose coefficients are in the <code>eqn</code> array and place the non-complex
   * roots into the <code>res</code> array, returning the number of roots. The cubic solved is
   * represented by the equation: eqn = {c, b, a, d} dx^3 + ax^2 + bx + c = 0 A return value of -1
   * is used to distinguish a constant equation, which may be always 0 or never 0, from an equation
   * which has no zeroes.
   *
   * @param eqn the specified array of coefficients to use to solve the cubic equation
   * @param res the array that contains the non-complex roots resulting from the solution of the
   *     cubic equation
   * @return the number of roots, or -1 if the equation is a constant
   * @since 1.3
   */
  public static int solveCubic(double eqn[], double res[]) {
    // From Graphics Gems:
    // http://tog.acm.org/resources/GraphicsGems/gems/Roots3And4.c
    final double d = eqn[3];
    if (d == 0) {
      return QuadCurve2D.solveQuadratic(eqn, res);
    }

    /* normal form: x^3 + Ax^2 + Bx + C = 0 */
    final double A = eqn[2] / d;
    final double B = eqn[1] / d;
    final double C = eqn[0] / d;

    //  substitute x = y - A/3 to eliminate quadratic term:
    //     x^3 +Px + Q = 0
    //
    // Since we actually need P/3 and Q/2 for all of the
    // calculations that follow, we will calculate
    // p = P/3
    // q = Q/2
    // instead and use those values for simplicity of the code.
    double sq_A = A * A;
    double p = 1.0 / 3 * (-1.0 / 3 * sq_A + B);
    double q = 1.0 / 2 * (2.0 / 27 * A * sq_A - 1.0 / 3 * A * B + C);

    /* use Cardano's formula */

    double cb_p = p * p * p;
    double D = q * q + cb_p;

    final double sub = 1.0 / 3 * A;

    int num;
    if (D < 0) {
      /* Casus irreducibilis: three real solutions */
      // see: http://en.wikipedia.org/wiki/Cubic_function#Trigonometric_.28and_hyperbolic.29_method
      double phi = 1.0 / 3 * Math.acos(-q / Math.sqrt(-cb_p));
      double t = 2 * Math.sqrt(-p);

      if (res == eqn) {
        eqn = Arrays.copyOf(eqn, 4);
      }

      res[0] = (t * Math.cos(phi));
      res[1] = (-t * Math.cos(phi + Math.PI / 3));
      res[2] = (-t * Math.cos(phi - Math.PI / 3));
      num = 3;

      for (int i = 0; i < num; ++i) {
        res[i] -= sub;
      }

    } else {
      // Please see the comment in fixRoots marked 'XXX' before changing
      // any of the code in this case.
      double sqrt_D = Math.sqrt(D);
      double u = Math.cbrt(sqrt_D - q);
      double v = -Math.cbrt(sqrt_D + q);
      double uv = u + v;

      num = 1;

      double err = 1200000000 * ulp(abs(uv) + abs(sub));
      if (iszero(D, err) || within(u, v, err)) {
        if (res == eqn) {
          eqn = Arrays.copyOf(eqn, 4);
        }
        res[1] = -(uv / 2) - sub;
        num = 2;
      }
      // this must be done after the potential Arrays.copyOf
      res[0] = uv - sub;
    }

    if (num > 1) { // num == 3 || num == 2
      num = fixRoots(eqn, res, num);
    }
    if (num > 2 && (res[2] == res[1] || res[2] == res[0])) {
      num--;
    }
    if (num > 1 && res[1] == res[0]) {
      res[1] = res[--num]; // Copies res[2] to res[1] if needed
    }
    return num;
  }