Package org.apache.commons.math3.complex

Examples of org.apache.commons.math3.complex.Complex


    @Test
    public void test2DDataUnitary() {
        FastFourierTransformer transformer;
        transformer = new FastFourierTransformer(DftNormalization.UNITARY);
        double tolerance = 1E-12;
        Complex[][] input = new Complex[][] {new Complex[] {new Complex(1, 0),
                                                            new Complex(2, 0)},
                                             new Complex[] {new Complex(3, 1),
                                                            new Complex(4, 2)}};
        Complex[][] goodOutput = new Complex[][] {new Complex[] {new Complex(5,
                1.5), new Complex(-1, -.5)}, new Complex[] {new Complex(-2,
                -1.5), new Complex(0, .5)}};
        Complex[][] output = (Complex[][])transformer.mdfft(input, TransformType.FORWARD);
        Complex[][] output2 = (Complex[][])transformer.mdfft(output, TransformType.INVERSE);

        Assert.assertEquals(input.length, output.length);
        Assert.assertEquals(input.length, output2.length);
View Full Code Here


        // p(x) = x^5 + 4x^3 + x^2 + 4 = (x+1)(x^2-x+1)(x^2+4)
        final double[] coefficients = { 4.0, 0.0, 1.0, 4.0, 0.0, 1.0 };
        final LaguerreSolver solver = new LaguerreSolver();
        final Complex[] result = solver.solveAllComplex(coefficients, 0);

        for (Complex expected : new Complex[] { new Complex(0, -2),
                                                new Complex(0, 2),
                                                new Complex(0.5, 0.5 * FastMath.sqrt(3)),
                                                new Complex(-1, 0),
                                                new Complex(0.5, -0.5 * FastMath.sqrt(3.0)) }) {
            final double tolerance = FastMath.max(solver.getAbsoluteAccuracy(),
                                                  FastMath.abs(expected.abs() * solver.getRelativeAccuracy()));
            TestUtils.assertContains(result, expected, tolerance);
        }
    }
View Full Code Here

            && isFunction(stackOperations.lastElement())) {
          stackRPN.push(stackOperations.pop());
        }
      } else if (isNumber(token)) {
        if (token.equals(IMAGINARY)) {
          stackRPN.push(complexFormat.format(new Complex(0, 1)));
        } else if (token.contains(IMAGINARY)) {
          stackRPN.push(complexFormat.format(complexFormat.parse("0+"
              + token)));
        } else {
          stackRPN.push(token);
View Full Code Here

    while (!stackRPN.empty()) {
      String token = stackRPN.pop();
      if (isNumber(token)) {
        stackAnswer.push(token);
      } else if (isOperator(token)) {
        Complex a = complexFormat.parse(stackAnswer.pop());
        Complex b = complexFormat.parse(stackAnswer.pop());
        if (token.equals("+")) {
          stackAnswer.push(complexFormat.format(b.add(a)));
        } else if (token.equals("-")) {
          stackAnswer.push(complexFormat.format(b.subtract(a)));
        } else if (token.equals("*")) {
          stackAnswer.push(complexFormat.format(b.multiply(a)));
        } else if (token.equals("/")) {
          stackAnswer.push(complexFormat.format(b.divide(a)));
        }
      } else if (isFunction(token)) {
        Complex a = complexFormat.parse(stackAnswer.pop());
        if (token.equals("abs")) {
          stackAnswer.push(complexFormat.format(a.abs()));
        } else if (token.equals("acos")) {
          stackAnswer.push(complexFormat.format(a.acos()));
        } else if (token.equals("arg")) {
          stackAnswer.push(complexFormat.format(a.getArgument()));
        } else if (token.equals("asin")) {
          stackAnswer.push(complexFormat.format(a.asin()));
        } else if (token.equals("atan")) {
          stackAnswer.push(complexFormat.format(a.atan()));
        } else if (token.equals("conj")) {
          stackAnswer.push(complexFormat.format(a.conjugate()));
        } else if (token.equals("cos")) {
          stackAnswer.push(complexFormat.format(a.cos()));
        } else if (token.equals("cosh")) {
          stackAnswer.push(complexFormat.format(a.cosh()));
        } else if (token.equals("exp")) {
          stackAnswer.push(complexFormat.format(a.exp()));
        } else if (token.equals("imag")) {
          stackAnswer.push(complexFormat.format(a.getImaginary()));
        } else if (token.equals("log")) {
          stackAnswer.push(complexFormat.format(a.log()));
        } else if (token.equals("neg")) {
          stackAnswer.push(complexFormat.format(a.negate()));
        } else if (token.equals("real")) {
          stackAnswer.push(complexFormat.format(a.getReal()));
        } else if (token.equals("sin")) {
          stackAnswer.push(complexFormat.format(a.sin()));
        } else if (token.equals("sinh")) {
          stackAnswer.push(complexFormat.format(a.sinh()));
        } else if (token.equals("sqrt")) {
          stackAnswer.push(complexFormat.format(a.sqrt()));
        } else if (token.equals("tan")) {
          stackAnswer.push(complexFormat.format(a.tan()));
        } else if (token.equals("tanh")) {
          stackAnswer.push(complexFormat.format(a.tanh()));
        } else if (token.equals("pow")) {
          Complex b = complexFormat.parse(stackAnswer.pop());
          stackAnswer.push(complexFormat.format(b.pow(a)));
        }
      }
    }

    if (stackAnswer.size() > 1) {
View Full Code Here

     * @param yi imaginary part of the second number
     * @return result of the complex division
     */
    private Complex cdiv(final double xr, final double xi,
                         final double yr, final double yi) {
        return new Complex(xr, xi).divide(new Complex(yr, yi));
    }
View Full Code Here

                // Last vector component imaginary so matrix is triangular
                if (FastMath.abs(matrixT[idx][idx - 1]) > FastMath.abs(matrixT[idx - 1][idx])) {
                    matrixT[idx - 1][idx - 1] = q / matrixT[idx][idx - 1];
                    matrixT[idx - 1][idx]     = -(matrixT[idx][idx] - p) / matrixT[idx][idx - 1];
                } else {
                    final Complex result = cdiv(0.0, -matrixT[idx - 1][idx],
                                                matrixT[idx - 1][idx - 1] - p, q);
                    matrixT[idx - 1][idx - 1] = result.getReal();
                    matrixT[idx - 1][idx]     = result.getImaginary();
                }

                matrixT[idx][idx - 1] = 0.0;
                matrixT[idx][idx]     = 1.0;

                for (int i = idx - 2; i >= 0; i--) {
                    double ra = 0.0;
                    double sa = 0.0;
                    for (int j = l; j <= idx; j++) {
                        ra = ra + matrixT[i][j] * matrixT[j][idx - 1];
                        sa = sa + matrixT[i][j] * matrixT[j][idx];
                    }
                    double w = matrixT[i][i] - p;

                    if (Precision.compareTo(imagEigenvalues[i], 0.0, EPSILON) < 0.0) {
                        z = w;
                        r = ra;
                        s = sa;
                    } else {
                        l = i;
                        if (Precision.equals(imagEigenvalues[i], 0.0)) {
                            final Complex c = cdiv(-ra, -sa, w, q);
                            matrixT[i][idx - 1] = c.getReal();
                            matrixT[i][idx] = c.getImaginary();
                        } else {
                            // Solve complex equations
                            double x = matrixT[i][i + 1];
                            double y = matrixT[i + 1][i];
                            double vr = (realEigenvalues[i] - p) * (realEigenvalues[i] - p) +
                                        imagEigenvalues[i] * imagEigenvalues[i] - q * q;
                            final double vi = (realEigenvalues[i] - p) * 2.0 * q;
                            if (Precision.equals(vr, 0.0) && Precision.equals(vi, 0.0)) {
                                vr = Precision.EPSILON * norm *
                                     (FastMath.abs(w) + FastMath.abs(q) + FastMath.abs(x) +
                                      FastMath.abs(y) + FastMath.abs(z));
                            }
                            final Complex c     = cdiv(x * r - z * ra + q * sa,
                                                       x * s - z * sa - q * ra, vr, vi);
                            matrixT[i][idx - 1] = c.getReal();
                            matrixT[i][idx]     = c.getImaginary();

                            if (FastMath.abs(x) > (FastMath.abs(z) + FastMath.abs(q))) {
                                matrixT[i + 1][idx - 1] = (-ra - w * matrixT[i][idx - 1] +
                                                           q * matrixT[i][idx]) / x;
                                matrixT[i + 1][idx]     = (-sa - w * matrixT[i][idx] -
                                                           q * matrixT[i][idx - 1]) / x;
                            } else {
                                final Complex c2        = cdiv(-r - y * matrixT[i][idx - 1],
                                                               -s - y * matrixT[i][idx], z, q);
                                matrixT[i + 1][idx - 1] = c2.getReal();
                                matrixT[i + 1][idx]     = c2.getImaginary();
                            }
                        }

                        // Overflow control
                        double t = FastMath.max(FastMath.abs(matrixT[i][idx - 1]),
View Full Code Here

        final Random random = new Random(SEED);
        final Complex[] data = new Complex[n];
        for (int i = 0; i < n; i++) {
            final double re = 2.0 * random.nextDouble() - 1.0;
            final double im = 2.0 * random.nextDouble() - 1.0;
            data[i] = new Complex(re, im);
        }
        return data;
    }
View Full Code Here

                final double xr = x[j].getReal();
                final double xi = x[j].getImaginary();
                yr += c * xr - sgn * s * xi;
                yi += sgn * s * xr + c * xi;
            }
            y[i] = new Complex(yr, yi);
        }
        return y;
    }
View Full Code Here

        final FastFourierTransformer fft;
        fft = new FastFourierTransformer(normalization);
        final double[] x = createRealData(n);
        final Complex[] xc = new Complex[n];
        for (int i = 0; i < n; i++) {
            xc[i] = new Complex(x[i], 0.0);
        }
        final Complex[] expected;
        final double s;
        if (type == TransformType.FORWARD) {
            expected = dft(xc, -1);
View Full Code Here

        final FastFourierTransformer fft;
        fft = new FastFourierTransformer(normalization);
        final Complex[] x = new Complex[n];
        for (int i = 0; i < n; i++) {
            final double t = min + i * (max - min) / n;
            x[i] = new Complex(f.value(t));
        }
        final Complex[] expected;
        final double s;
        if (type == TransformType.FORWARD) {
            expected = dft(x, -1);
View Full Code Here

TOP

Related Classes of org.apache.commons.math3.complex.Complex

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.