Example usage for org.apache.commons.math3.complex Complex add

List of usage examples for org.apache.commons.math3.complex Complex add

Introduction

In this page you can find the example usage for org.apache.commons.math3.complex Complex add.

Prototype

public Complex add(double addend) 

Source Link

Document

Returns a Complex whose value is (this + addend) , with addend interpreted as a real number.

Usage

From source file:com.fpuna.preproceso.TestApacheMathLibDemo.java

/**
 * @param args//from   w  ww . j  av a  2s .  c  om
 */
public static void main(String[] args) {

    RandomGenerator randomGenerator = new JDKRandomGenerator();
    System.out.println(randomGenerator.nextInt());
    System.out.println(randomGenerator.nextDouble());

    /**
     * Descriptive Statistics like MEAN,GP,SD,MAX
    *
     */
    DescriptiveStatistics stats = new DescriptiveStatistics();
    stats.addValue(1);
    stats.addValue(2);
    stats.addValue(3);
    stats.addValue(4);
    stats.addValue(5);
    stats.addValue(6);
    stats.addValue(7);
    System.out.print("Mean : " + stats.getMean() + "\n");
    System.out.print("Standard deviation : " + stats.getStandardDeviation() + "\n");
    System.out.print("Max : " + stats.getMax() + "\n");

    /**
     * Complex number format a+bi
    *
     */
    Complex c1 = new Complex(1, 2);
    Complex c2 = new Complex(2, 3);
    System.out.print("Absolute of c1 " + c1.abs() + "\n");
    System.out.print("Addition : " + (c1.add(c2)) + "\n");
}

From source file:mandelbrot.MainDraw.java

public static Complex mandelFunction(Complex z, Complex c) {
    Complex zz = z.multiply(z);
    return (c.add(zz));
}

From source file:edu.ucsf.valelab.saim.calculations.SaimCalc.java

public static double fieldStrength(final double wavelength, final double angle, final double nSample,
        final double dOx, final double distance) {
    Complex rTE = fresnelTE(wavelength, angle, dOx, nSample);
    double phaseDiff = PhaseDiff(wavelength, angle, nSample, distance);
    Complex tmp = new Complex(Math.cos(phaseDiff), Math.sin(phaseDiff));
    Complex fieldStrength = rTE.multiply(tmp);
    fieldStrength = fieldStrength.add(1.0);

    return fieldStrength.getReal() * fieldStrength.getReal()
            + fieldStrength.getImaginary() * fieldStrength.getImaginary();
}

From source file:edu.ucsf.valelab.saim.calculations.SaimCalc.java

/**
 * Calculates the transverse electric (TE) component, perpendicular to the 
 * plane of incidence, of the Fresnel coefficient of reflection between the 
 * sample interface and the virtual silicon oxidesilicon layer, 
 * as described in://w  w  w. j  a  v a  2s .c o  m
 * Paszek, M.J., C.C. DuFort, M.G. Rubashkin, M.W. Davidson, K.S. Thorn, J.T. 
 * Liphardt, and V.M. Weaver. 2012. 
 * Scanning angle interference microscopy reveals cell dynamics at the nanoscale. 
 * Nat Meth. 9:825827. doi:10.1038/nmeth.2077.
 * 
 * 
 * 1/11/2016: Note that the above manuscript contains a mistake that is corrected
 * in a later publication: http://dx.doi.org/10.1016/B978-0-12-420138-5.00013-6
 * That correction is now applied.
 * 
 * 
 * @param wavelength of the excitation light source in nm
 * @param angle with respect to the normal in radiance
 * @param dOx Thickness of the Silicon Oxide layer in nm
 * @param nSample Refractive index of the sample's buffer
 * @return FresnelCoefficient for these conditions
 */
public static Complex fresnelTE(final double wavelength, final double angle, final double dOx,
        final double nSample) {

    double nSi = RI.getRI(RI.Compound.SILICON, wavelength);
    double nOx = RI.getRI(RI.Compound.SILICONOXIDE, wavelength);
    double kOx = k(wavelength, nOx);
    double angleOx = snell2(angle, nSample, RI.getRI(RI.Compound.SILICONOXIDE, wavelength));
    double cosOx = Math.cos(angleOx);
    double cosSi = Math.cos(snell2(angleOx, nOx, RI.getRI(RI.Compound.SILICON, wavelength)));
    double p0 = nSi * cosSi;
    double p1 = nOx * cosOx;
    double p2 = nSample * Math.cos(angle);
    double kOxdOxCosOx = kOx * dOx * cosOx;

    double cosOfkOxdOxCosOx = Math.cos(kOxdOxCosOx);
    double sinOfkOxdOxCosOx = Math.sin(kOxdOxCosOx);

    double m11TE = cosOfkOxdOxCosOx;
    Complex m12TE = Complex.I.multiply(-1 / p1 * sinOfkOxdOxCosOx);
    Complex m21TE = Complex.I.multiply(-p1 * sinOfkOxdOxCosOx);
    double m22TE = cosOfkOxdOxCosOx;

    Complex tmp1 = ((m12TE.multiply(p0)).add(m11TE)).multiply(p2);
    // this is the only line changed due to the error in the NM paper
    Complex tmp2 = tmp1.subtract(m21TE.add(m22TE * p0));
    //Complex tmp2 = tmp1.add( m21TE.subtract(m22TE * p0) );
    Complex tmp3 = tmp1.add(m21TE.add(m22TE * p0));
    Complex rTE = tmp2.divide(tmp3);

    return rTE;
}

From source file:com.analog.lyric.dimple.solvers.sumproduct.customFactors.CustomComplexGaussianPolynomial.java

public static double[][] buildJacobian(Complex[] coeffs, double[] powers, Complex x) {
    double a = x.getReal();
    double b = x.getImaginary();
    double a2pb2 = a * a + b * b;

    Complex dyda = new Complex(0, 0);
    Complex dydb = new Complex(0, 0);

    for (int k = 0; k < coeffs.length; k++) {
        double pow = powers[k];

        if (pow == 0) {
            dyda = dyda.add(coeffs[k]);
            dydb = dydb.add(coeffs[k].multiply(new Complex(0, 1)));
        } else {/*from  w w  w .j  av a  2s  . co m*/
            dyda = dyda.add(coeffs[k].multiply(x.multiply(new Complex(2 * a * k * Math.pow(a2pb2, k - 1), 0))
                    .add(new Complex(Math.pow(a2pb2, k), 0))));
            dydb = dydb.add(coeffs[k].multiply(x.multiply(new Complex(2 * b * k * Math.pow(a2pb2, k - 1), 0))
                    .add(new Complex(0, Math.pow(a2pb2, k)))));

        }
    }

    return new double[][] { new double[] { dyda.getReal(), dydb.getReal() },
            new double[] { dyda.getImaginary(), dydb.getImaginary() } };
}

From source file:com.analog.lyric.dimple.solvers.sumproduct.customFactors.CustomComplexGaussianPolynomial.java

private static Complex P(Complex input, double[] powers, Complex[] coeffs) {

    double a = input.getReal();
    double b = input.getImaginary();

    double a2pb2 = a * a + b * b;

    Complex retval = new Complex(0, 0);

    for (int i = 0; i < powers.length; i++) {

        Complex tmp = coeffs[i].multiply(input);
        double tmp2 = Math.pow(a2pb2, powers[i]);
        tmp = tmp.multiply(new Complex(tmp2, 0));

        retval = retval.add(tmp);
    }/*from  ww w.j  a  v a2 s. co  m*/

    return retval;
}

From source file:com.analog.lyric.dimple.solvers.sumproduct.customFactors.CustomComplexGaussianPolynomial.java

public static Complex newtonRaphson(Complex input, int numIterations, double[] powers, Complex[] coeffs) {
    //for some number of iterations
    //initialize x to y;
    Complex output = new Complex(input.getReal(), input.getImaginary());

    for (int i = 0; i < numIterations; i++) {
        double[][] J = buildJacobian(coeffs, powers, output);

        Complex pout = P(output, powers, coeffs);
        Complex y = input.subtract(pout);

        double[][] Jinv = invertMatrix(J);
        double[] tmp = matrixMultiply(Jinv, new double[] { y.getReal(), y.getImaginary() });

        output = output.add(new Complex(tmp[0], tmp[1]));
    }//from   w  ww.  ja  v  a  2s . c om

    return output;
}

From source file:edu.ucsf.valelab.saim.calculations.BenchmarkCalculations.java

/**
 * Compares two methods to calculate the Saim function
 * The implementation not using Complex numbers appears to be at least
 * 10 times faster//from  w  ww .  j a va  2 s. c  o m
 * @throws Exception 
 */
public void test() throws Exception {

    long nrRuns = 100000000;

    double wavelength = 488.0;
    double nSample = 1.36;
    double dOx = 500.0;
    double h = 16.0;

    double angle = Math.toRadians(0.0);
    SaimFunction sf = new SaimFunction(wavelength, dOx, nSample, false);
    Complex rTE = sf.getFresnelTE(0);
    double f = 4.0 * Math.PI * nSample * Math.cos(angle) / wavelength;
    double phaseDiff = f * h;

    // method 1
    long startTime = System.nanoTime();
    double c = rTE.getReal();
    double d = rTE.getImaginary();
    for (int i = 0; i < nrRuns; i++) {
        double val = 1 + 2 * c * Math.cos(phaseDiff) - 2 * d * Math.sin(phaseDiff) + c * c + d * d;
    }
    long endTime = System.nanoTime();
    long took = endTime - startTime;
    System.out.println("First method: " + nrRuns + " runs took: " + took / 1000000 + " milliseconds");

    // method 2
    startTime = System.nanoTime();
    for (int i = 0; i < nrRuns; i++) {
        Complex tmp = new Complex(Math.cos(phaseDiff), Math.sin(phaseDiff));
        Complex fieldStrength = rTE.multiply(tmp);
        fieldStrength = fieldStrength.add(1.0);
        // square of absolute 
        double val = fieldStrength.getReal() * fieldStrength.getReal()
                + fieldStrength.getImaginary() * fieldStrength.getImaginary();
    }
    endTime = System.nanoTime();
    took = endTime - startTime;
    System.out.println("Second method: " + nrRuns + " runs took: " + took / 1000000 + " milliseconds");

}

From source file:eu.itesla_project.iidm.ddb.eurostag.model.TransformerModel.java

public StateVariable toSv1(StateVariable sv2) {
    Complex s2 = new Complex(-sv2.p, -sv2.q); // s2=p2+jq2
    Complex u2 = ComplexUtils.polar2Complex(sv2.u, Math.toRadians(sv2.theta));
    Complex v2 = u2.divide(SQUARE_3); // v2=u2/sqrt(3)
    Complex i2 = s2.divide(v2.multiply(3)).conjugate(); // i2=conj(s2/(3*v2))
    Complex v1p = v2.add(z.multiply(i2)); // v1'=v2+z*i2
    Complex i1p = i2.negate().add(y.multiply(v1p)); // i1'=-i2+v1'*y
    Complex i1 = i1p.multiply(ratio); // i1=i1p*ration
    Complex v1 = v1p.divide(ratio); // v1=v1p/ration
    Complex s1 = v1.multiply(3).multiply(i1.conjugate()); // s1=3*v1*conj(i1)
    Complex u1 = v1.multiply(SQUARE_3);// w  ww  . j  av a2 s  .  com
    return new StateVariable(-s1.getReal(), -s1.getImaginary(), u1.abs(), Math.toDegrees(u1.getArgument()));
}

From source file:com.autsia.bracer.BracerParser.java

/**
 * Evaluates once parsed math expression with "var" variable included
 *
 * @param variableValue User-specified <code>Double</code> value
 * @return <code>String</code> representation of the result
 * @throws <code>ParseException</code> if the input expression is not
 *                                     correct
 * @since 3.0/*from  ww w.  j  a  v a 2  s  .c  om*/
 */
public String evaluate(double variableValue) throws ParseException {
    /* check if is there something to evaluate */
    if (stackRPN.empty()) {
        return "";
    }

    /* clean answer stack */
    stackAnswer.clear();

    /* get the clone of the RPN stack for further evaluating */
    @SuppressWarnings("unchecked")
    Stack<String> stackRPN = (Stack<String>) this.stackRPN.clone();

    /* enroll the variable value into expression */
    Collections.replaceAll(stackRPN, VARIABLE, Double.toString(variableValue));

    /* evaluating the RPN expression */
    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());
            boolean aBoolean = a.getReal() == 1.0;
            boolean bBoolean = b.getReal() == 1.0;
            switch (token) {
            case "+":
                stackAnswer.push(complexFormat.format(b.add(a)));
                break;
            case "-":
                stackAnswer.push(complexFormat.format(b.subtract(a)));
                break;
            case "*":
                stackAnswer.push(complexFormat.format(b.multiply(a)));
                break;
            case "/":
                stackAnswer.push(complexFormat.format(b.divide(a)));
                break;
            case "|":
                stackAnswer.push(String.valueOf(aBoolean || bBoolean ? "1" : "0"));
                break;
            case "&":
                stackAnswer.push(String.valueOf(aBoolean && bBoolean ? "1" : "0"));
                break;
            }
        } else if (isFunction(token)) {
            Complex a = complexFormat.parse(stackAnswer.pop());
            boolean aBoolean = a.getReal() == 1.0;
            switch (token) {
            case "abs":
                stackAnswer.push(complexFormat.format(a.abs()));
                break;
            case "acos":
                stackAnswer.push(complexFormat.format(a.acos()));
                break;
            case "arg":
                stackAnswer.push(complexFormat.format(a.getArgument()));
                break;
            case "asin":
                stackAnswer.push(complexFormat.format(a.asin()));
                break;
            case "atan":
                stackAnswer.push(complexFormat.format(a.atan()));
                break;
            case "conj":
                stackAnswer.push(complexFormat.format(a.conjugate()));
                break;
            case "cos":
                stackAnswer.push(complexFormat.format(a.cos()));
                break;
            case "cosh":
                stackAnswer.push(complexFormat.format(a.cosh()));
                break;
            case "exp":
                stackAnswer.push(complexFormat.format(a.exp()));
                break;
            case "imag":
                stackAnswer.push(complexFormat.format(a.getImaginary()));
                break;
            case "log":
                stackAnswer.push(complexFormat.format(a.log()));
                break;
            case "neg":
                stackAnswer.push(complexFormat.format(a.negate()));
                break;
            case "real":
                stackAnswer.push(complexFormat.format(a.getReal()));
                break;
            case "sin":
                stackAnswer.push(complexFormat.format(a.sin()));
                break;
            case "sinh":
                stackAnswer.push(complexFormat.format(a.sinh()));
                break;
            case "sqrt":
                stackAnswer.push(complexFormat.format(a.sqrt()));
                break;
            case "tan":
                stackAnswer.push(complexFormat.format(a.tan()));
                break;
            case "tanh":
                stackAnswer.push(complexFormat.format(a.tanh()));
                break;
            case "pow":
                Complex b = complexFormat.parse(stackAnswer.pop());
                stackAnswer.push(complexFormat.format(b.pow(a)));
                break;
            case "not":
                stackAnswer.push(String.valueOf(!aBoolean ? "1" : "0"));
                break;
            }
        }
    }

    if (stackAnswer.size() > 1) {
        throw new ParseException("Some operator is missing", 0);
    }

    return stackAnswer.pop();
}