List of usage examples for org.apache.commons.math3.complex Complex subtract
public Complex subtract(double subtrahend)
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:/*from w w w . ja v a 2 s .c om*/ * 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:gov.pnnl.goss.gridappsd.testmanager.CompareResults.java
public static boolean equals(Complex a, Complex b) { return a.equals(b) ? true : (a.subtract(b)).abs() < EPSILON; }
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 w w .j a v a2 s.c o m return output; }
From source file:com.wwidesigner.geometry.calculation.Tube.java
/** * Calculate the transfer matrix of a conical tube. * @param waveNumber - 2*pi*f/c, in radians per metre * @param length - length of the tube, in metres. * @param sourceRadius - radius of source end the tube, in metres. * @param loadRadius - radius of load end the tube, in metres. * @param params - physical parameters//from w ww.j av a 2s. co m * @return Transfer matrix */ public static TransferMatrix calcConeMatrix(double waveNumber, double length, double sourceRadius, double loadRadius, PhysicalParameters params) { // From: Antoine Lefebvre and Jean Kergomard. if (sourceRadius == loadRadius) { return calcCylinderMatrix(waveNumber, length, sourceRadius, params); } // Mean complex wave vector along the whole cone, from Lefebvre and Kergomard. double alpha_0 = params.getAlphaConstant() / FastMath.sqrt(waveNumber); double epsilon; if (FastMath.abs(loadRadius - sourceRadius) <= 0.00001 * sourceRadius) { // Use limiting value as loadRadius approaches sourceRadius. epsilon = alpha_0 / loadRadius; } else { epsilon = alpha_0 / (loadRadius - sourceRadius) * FastMath.log(loadRadius / sourceRadius); } Complex mean = new Complex(1.0 + epsilon, -epsilon); Complex kMeanL; if (length >= MINIMUM_CONE_LENGTH) { kMeanL = mean.multiply(waveNumber * length); } else { // Limit how short the cone can be. // Length of zero leads to a divide-by-zero below. kMeanL = mean.multiply(waveNumber * MINIMUM_CONE_LENGTH); } // Cotangents of theta_in and theta_out. Complex cot_in = new Complex((loadRadius - sourceRadius) / sourceRadius).divide(kMeanL); Complex cot_out = new Complex((loadRadius - sourceRadius) / loadRadius).divide(kMeanL); // sine and cosine of kMean * L. Complex sin_kL = kMeanL.sin(); Complex cos_kL = kMeanL.cos(); Complex A = cos_kL.multiply(loadRadius / sourceRadius).subtract(sin_kL.multiply(cot_in)); Complex B = Complex.I.multiply(sin_kL).multiply(params.calcZ0(loadRadius) * (loadRadius / sourceRadius)); Complex C = Complex.I.multiply(loadRadius / (sourceRadius * params.calcZ0(sourceRadius))).multiply( sin_kL.multiply(cot_out.multiply(cot_in).add(1.0)).add(cos_kL.multiply(cot_out.subtract(cot_in)))); Complex D = cos_kL.multiply(sourceRadius / loadRadius).add(sin_kL.multiply(cot_out)); TransferMatrix tm = new TransferMatrix(A, B, C, D); // assert tm.determinant() == Complex.valueOf(1.0,0.0); return tm; }
From source file:eu.itesla_project.iidm.ddb.eurostag.model.TransformerModel.java
public StateVariable toSv2(StateVariable sv1) { Complex s1 = new Complex(-sv1.p, -sv1.q); // s1=p1+jq1 Complex u1 = ComplexUtils.polar2Complex(sv1.u, Math.toRadians(sv1.theta)); Complex v1 = u1.divide(SQUARE_3); // v1=u1/sqrt(3) Complex v1p = v1.multiply(ratio); // v1p=v1*rho Complex i1 = s1.divide(v1.multiply(3)).conjugate(); // i1=conj(s1/(3*v1)) Complex i1p = i1.divide(ratio); // i1p=i1/rho Complex i2 = i1p.subtract(y.multiply(v1p)).negate(); // i2=-(i1p-y*v1p) Complex v2 = v1p.subtract(z.multiply(i2)); // v2=v1p-z*i2 Complex s2 = v2.multiply(3).multiply(i2.conjugate()); // s2=3*v2*conj(i2) Complex u2 = v2.multiply(SQUARE_3);//ww w .ja v a 2s .com return new StateVariable(-s2.getReal(), -s2.getImaginary(), u2.abs(), Math.toDegrees(u2.getArgument())); }
From source file:eu.itesla_project.iidm.network.util.SV.java
public SV otherSide(float r, float x, float g, float b, float ratio) { Complex z = new Complex(r, x); // z=r+jx Complex y = new Complex(g, b); // y=g+jb Complex s1 = new Complex(p, q); // s1=p1+jq1 Complex u1 = ComplexUtils.polar2Complex(u, Math.toRadians(a)); Complex v1 = u1.divide(Math.sqrt(3f)); // v1=u1/sqrt(3) Complex v1p = v1.multiply(ratio); // v1p=v1*rho Complex i1 = s1.divide(v1.multiply(3)).conjugate(); // i1=conj(s1/(3*v1)) Complex i1p = i1.divide(ratio); // i1p=i1/rho Complex i2 = i1p.subtract(y.multiply(v1p)); // i2=i1p-y*v1p Complex v2 = v1p.subtract(z.multiply(i2)); // v2=v1p-z*i2 Complex s2 = v2.multiply(3).multiply(i2.conjugate()); // s2=3*v2*conj(i2) Complex u2 = v2.multiply(Math.sqrt(3f)); return new SV((float) -s2.getReal(), (float) -s2.getImaginary(), (float) u2.abs(), (float) Math.toDegrees(u2.getArgument())); }
From source file:net.sf.dsp4j.octave.packages.signal_1_0_11.Bilinear.java
public Bilinear(Complex[] sZero, Complex[] sPole, double sGain, double T) { Complex _2 = new Complex(2); if (sZero.length > sPole.length || sPole.length == 0) { // error("bilinear: must have at least as many poles as zeros in s-plane"); }/*from ww w . ja va 2 s.co m*/ Complex[] bz = new Complex[sZero.length]; for (int i = 0; i < sZero.length; i++) { bz[i] = _2.subtract(sZero[i].multiply(T)).divide(T); } Complex[] bp = new Complex[sPole.length]; for (int i = 0; i < sPole.length; i++) { bp[i] = _2.subtract(sPole[i].multiply(T)).divide(T); } zGain = new Complex(sGain, 0).multiply(prod(bz).divide(prod(bp))).getReal(); zPole = new Complex[sPole.length]; for (int i = 0; i < sPole.length; i++) { zPole[i] = _2.add(sPole[i].multiply(T)).divide(_2.subtract(sPole[i].multiply(T))); } zZero = new Complex[sPole.length]; if (sZero.length == 0) { Arrays.fill(zZero, Complex.ONE.negate()); } else { } for (int i = 0; i < sZero.length; i++) { zZero[i] = _2.add(sZero[i].multiply(T)).divide(_2.subtract(sZero[i].multiply(T))); } if (sZero.length < zZero.length) { Arrays.fill(zZero, sZero.length, zZero.length, Complex.ONE.negate()); } }
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 w w w.j a v a2 s .co m*/ */ 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(); }
From source file:com.calc.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 w ww.j av a2 s . com*/ */ 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 "fat": stackAnswer.push(complexFormat.format(a.abs())); break; case "fib": 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(); }
From source file:org.briljantframework.array.AbstractComplexArray.java
@Override public ComplexArray reverseMinus(Complex scalar) { ComplexArray m = newEmptyArray(getShape()); for (int i = 0; i < size(); i++) { m.set(i, scalar.subtract(get(i))); }/*from w ww . j av a 2 s .c om*/ return m; }