List of usage examples for org.apache.commons.math.complex Complex divide
public Complex divide(Complex rhs)
From source file:com.discursive.jccook.math.ComplexNumbers.java
public static void main(String[] args) { Complex a = new Complex(2, 3); Complex b = new Complex(4, 5); Complex c = new Complex(0.3, 2); Complex e = new Complex(4, 4); Complex sum = a.add(b);/* w w w .j a v a 2s . c o m*/ Complex d = c.divide(sum); Complex f = e.multiply(d.conjugate()); System.out.println("D is: " + ComplexFormat.formatComplex(d)); System.out.println("F is: " + ComplexFormat.formatComplex(f)); double realF = f.getReal(); double imD = d.getImaginary(); double answer = realF / imD; System.out.println("Answer: " + NumberFormat.getInstance().format(answer)); }
From source file:geogebra.kernel.GeoVec2D.java
/** c = a / b Michael Borcherds 2007-12-09 * //from w ww .j av a 2 s . co m * */ final public static void complexDivide(GeoVec2D a, GeoVec2D b, GeoVec2D c) { // NB temporary variables *crucial*: a and c can be the same variable //double x1=a.x,y1=a.y,x2=b.x,y2=b.y; // complex division //c.x = (x1 * x2 + y1 * y2)/(x2 * x2 + y2 * b.y); //c.y = (y1 * x2 - x1 * y2)/(x2 * x2 + y2 * b.y); Complex out = new Complex(a.x, a.y); out = out.divide(new Complex(b.x, b.y)); c.x = out.getReal(); c.y = out.getImaginary(); c.setMode(Kernel.COORD_COMPLEX); }
From source file:geogebra.kernel.GeoVec2D.java
/** c = a / b Michael Borcherds 2008-08-12 * // w w w. ja v a2 s. c o m * */ final public static void complexDivide(NumberValue a, GeoVec2D b, GeoVec2D c) { // NB temporary variables *crucial*: a and c can be the same variable //double x1=a.getDouble(), x2 = b.x, y2 = b.y; // complex division //c.x = (x1 * x2 )/(x2 * x2 + y2 * b.y); //c.y = ( - x1 * y2)/(x2 * x2 + y2 * b.y); Complex out = new Complex(a.getDouble(), 0); out = out.divide(new Complex(b.x, b.y)); c.x = out.getReal(); c.y = out.getImaginary(); c.setMode(Kernel.COORD_COMPLEX); }
From source file:geogebra.common.kernel.geos.GeoVec2D.java
/** * c = a / b Michael Borcherds 2007-12-09 * /*from w ww . j ava 2 s .com*/ * @param a * dividend * @param b * divisor * @param c * result * * */ final public static void complexDivide(GeoVec2D a, GeoVec2D b, GeoVec2D c) { Complex out = new Complex(a.x, a.y); out = out.divide(new Complex(b.x, b.y)); c.x = out.getReal(); c.y = out.getImaginary(); c.setMode(Kernel.COORD_COMPLEX); }
From source file:geogebra.common.kernel.geos.GeoVec2D.java
/** * c = a / b Michael Borcherds 2008-08-12 * /* w ww . j av a2s. c om*/ * @param a * dividend * @param b * divisor * @param c * result * * */ final public static void complexDivide(NumberValue a, GeoVec2D b, GeoVec2D c) { // NB temporary variables *crucial*: a and c can be the same variable // double x1=a.getDouble(), x2 = b.x, y2 = b.y; // complex division // c.x = (x1 * x2 )/(x2 * x2 + y2 * b.y); // c.y = ( - x1 * y2)/(x2 * x2 + y2 * b.y); Complex out = new Complex(a.getDouble(), 0); out = out.divide(new Complex(b.x, b.y)); c.x = out.getReal(); c.y = out.getImaginary(); c.setMode(Kernel.COORD_COMPLEX); }
From source file:com.QMTunnelling.GaussianPotential.java
public Complex getA() {//finds A coef Complex psi1 = this.psi[0]; Complex psi2 = this.psi[1]; double x1 = this.xvals[0]; double x2 = this.xvals[1]; Complex A = (psi2.multiply(Exp(Complex.I.multiply(Math.sqrt(kappa) * x2)))); A = A.subtract(psi1.multiply(Exp(Complex.I.multiply(Math.sqrt(kappa) * x1)))); Complex divisor = Exp(Complex.I.multiply(2 * Math.sqrt(kappa) * x2)) .subtract(Exp(Complex.I.multiply(2 * Math.sqrt(kappa) * x1))); A = A.divide(divisor); return A;/*www.j a va 2 s . c o m*/ }
From source file:org.eclipse.ice.reflectivity.ReflectivityCalculator.java
/** * This operation returns the value of the squared modulus of the specular * reflectivity for a single wave vector Q. * * @param waveVectorQ//from w ww. j av a 2 s.c o m * the value of the wave vector * @param wavelength * the wavelength of the incident neutrons * @param tiles * the list of Tiles that contains the physical parameters needed * for the calculation, including the scattering densities, * absorption parameters and thicknesses. * @return the squared modulus of the specular reflectivity */ public double getModSqrdSpecRef(double waveVectorQ, double wavelength, Tile[] tiles) { double modSqrdSpecRef = 0.0; if (wavelength > 0.0) { // Variables only needed if we are going to do the work, i.e. - // wavelength > 0.0. Tile tile; Complex aNm1Sq, fNm1N, rNm1N = new Complex(0.0, 0.0), one = new Complex(1.0, 0.0), qN = new Complex(0.0, 0.0), rNNp1 = new Complex(0.0, 0.0); // Get the bottom tile int nLayers = tiles.length; tile = tiles[nLayers - 1]; // Starting point--no reflected beam in bottom-most (bulk) layer double qCSq = 16.0 * Math.PI * tile.scatteringLength; double muLAbs = tile.trueAbsLength; double mulInc = tile.incAbsLength; double thickness = tile.thickness; // Setup other values for the problem double betaNm1 = 4.0 * Math.PI * (muLAbs + mulInc / wavelength); Complex qNm1 = new Complex(waveVectorQ * waveVectorQ - qCSq, -2.0 * betaNm1); qNm1 = qNm1.sqrt(); // Loop through to calculate recursion formula described in Parratt. // Start at the bottom and work up. for (int i = nLayers - 1; i > 0; i--) { // Get the tile above tile[i] (started at the bottom tile = tiles[i - 1]; // Calculate the normal component of Q for layer and layer-1 qN = qNm1; qCSq = 16.0 * Math.PI * tile.scatteringLength; muLAbs = tile.trueAbsLength; mulInc = tile.incAbsLength; thickness = tile.thickness; betaNm1 = 4.0 * Math.PI * (muLAbs + mulInc / wavelength); qNm1 = new Complex(waveVectorQ * waveVectorQ - qCSq, -2.0 * betaNm1); qNm1 = qNm1.sqrt(); // Calculate phase factor, e^(-0.5*d*qNm1) aNm1Sq = (new Complex(qNm1.getImaginary(), qNm1.getReal()).multiply(-0.5 * thickness)).exp(); // CDiv(qNm1-qN,qNm1+qN) fNm1N = qNm1.subtract(qN).divide(qNm1.add(qN)); // Calculate the reflectivity amplitude. // CMult(aNm1Sq, CMult(aNm1Sq, CDiv(CAdd(rNNp1, fNm1N), // CAdd(CMult(rNNp1, fNm1N), CReal(1))))) Complex y = rNNp1.multiply(fNm1N).add(one); Complex z = rNNp1.add(fNm1N); rNm1N = aNm1Sq.multiply(aNm1Sq).multiply(z.divide((y))); // Carry over to the next iteration rNNp1 = rNm1N; } modSqrdSpecRef = rNm1N.getReal() * rNm1N.getReal() + rNm1N.getImaginary() * rNm1N.getImaginary(); } return modSqrdSpecRef; }
From source file:r.base.Ops.java
@Primitive("/") @Recycle// w w w. ja va 2 s. c om @PreserveAttributes(PreserveAttributeStyle.ALL) public static Complex divide(Complex x, Complex y) { return x.divide(y); }
From source file:uk.ac.diamond.scisoft.analysis.dataset.MathsTest.java
@Test public void testDivision() { AbstractDataset a, b, c = null, d = null; Complex zv = new Complex(-3.5, 0); final double dv = zv.getReal(); long start;// ww w . j a va 2s .c o m int n; for (String dn : classes.keySet()) { final int dtype = classes.get(dn); Random.seed(12735L); for (String en : classes.keySet()) { final int etype = classes.get(en); System.out.println("Dividing " + dn + " by " + en); n = 32; for (int i = 0; i < SITER; i++) { if (dtype < AbstractDataset.ARRAYINT8) { a = Random.randn(n).imultiply(100); a = a.cast(dtype); } else { AbstractDataset[] aa = new AbstractDataset[ISIZEA]; for (int j = 0; j < ISIZEA; j++) { aa[j] = Random.randn(n).imultiply(100); } a = DatasetUtils.cast(aa, dtype); } if (etype < AbstractDataset.ARRAYINT8) { b = Random.randn(n).imultiply(100); b = b.cast(etype); } else { AbstractDataset[] ab = new AbstractDataset[ISIZEB]; for (int j = 0; j < ISIZEB; j++) { ab[j] = Random.randn(n).imultiply(100); } b = DatasetUtils.cast(ab, etype); } start = -System.nanoTime(); try { c = Maths.divide(a, b); } catch (IllegalArgumentException e) { System.out.println("Could not perform this operation: " + e.getMessage()); continue; } start += System.nanoTime(); double ntime = ((double) start) / c.getSize(); d = AbstractDataset.zeros(c); start = -System.nanoTime(); IndexIterator ita = a.getIterator(); IndexIterator itb = b.getIterator(); int j = 0; if ((dtype == AbstractDataset.COMPLEX64 || dtype == AbstractDataset.COMPLEX128) && (etype == AbstractDataset.COMPLEX64 || etype == AbstractDataset.COMPLEX128)) { final int is = d.getElementsPerItem(); while (ita.hasNext() && itb.hasNext()) { d.setObjectAbs(j, ((Complex) a.getObjectAbs(ita.index)) .divide((Complex) b.getObjectAbs(itb.index))); j += is; } } else if ((dtype == AbstractDataset.COMPLEX64 || dtype == AbstractDataset.COMPLEX128) && !(etype == AbstractDataset.COMPLEX64 || etype == AbstractDataset.COMPLEX128)) { final int is = d.getElementsPerItem(); while (ita.hasNext() && itb.hasNext()) { d.setObjectAbs(j, ((Complex) a.getObjectAbs(ita.index)) .divide(new Complex(b.getElementDoubleAbs(itb.index), 0))); j += is; } } else if (!(dtype == AbstractDataset.COMPLEX64 || dtype == AbstractDataset.COMPLEX128) && (etype == AbstractDataset.COMPLEX64 || etype == AbstractDataset.COMPLEX128)) { final int is = d.getElementsPerItem(); while (ita.hasNext() && itb.hasNext()) { d.setObjectAbs(j, new Complex(a.getElementDoubleAbs(ita.index), 0) .divide((Complex) b.getObjectAbs(itb.index))); j += is; } } else { if (dtype < AbstractDataset.ARRAYINT8 && etype < AbstractDataset.ARRAYINT8) { while (ita.hasNext() && itb.hasNext()) { d.setObjectAbs(j++, ((Number) a.getObjectAbs(ita.index)).doubleValue() / ((Number) b.getObjectAbs(itb.index)).doubleValue()); } } else { final double[] answer = new double[MAXISIZE]; final int is = d.getElementsPerItem(); final int rtype = d.getDtype(); if (a.getElementsPerItem() < is) { while (ita.hasNext() && itb.hasNext()) { final double xa = a.getElementDoubleAbs(ita.index); if (rtype == AbstractDataset.ARRAYFLOAT32 || rtype == AbstractDataset.ARRAYFLOAT64) { for (int k = 0; k < ISIZEB; k++) { answer[k] = xa / b.getElementDoubleAbs(itb.index + k); } } else { for (int k = 0; k < ISIZEB; k++) { final double v = xa / b.getElementDoubleAbs(itb.index + k); answer[k] = Double.isInfinite(v) || Double.isNaN(v) ? 0 : v; } } d.setObjectAbs(j, answer); j += is; } } else if (b.getElementsPerItem() < is) { while (ita.hasNext() && itb.hasNext()) { final double xb = b.getElementDoubleAbs(itb.index); if (rtype == AbstractDataset.ARRAYFLOAT32 || rtype == AbstractDataset.ARRAYFLOAT64) { for (int k = 0; k < ISIZEA; k++) { answer[k] = a.getElementDoubleAbs(ita.index + k) / xb; } } else { if (xb == 0) { for (int k = 0; k < ISIZEA; k++) { answer[k] = 0; } } else { for (int k = 0; k < ISIZEA; k++) { answer[k] = a.getElementDoubleAbs(ita.index + k) / xb; } } } d.setObjectAbs(j, answer); j += is; } } else { while (ita.hasNext() && itb.hasNext()) { if (rtype == AbstractDataset.ARRAYFLOAT32 || rtype == AbstractDataset.ARRAYFLOAT64) { double v; for (int k = 0; k < is; k++) { v = a.getElementDoubleAbs(ita.index + k) / b.getElementDoubleAbs(itb.index + k); answer[k] = Double.isInfinite(v) || Double.isNaN(v) ? 0 : v; } } else { double v; for (int k = 0; k < is; k++) { v = a.getElementDoubleAbs(ita.index + k) / b.getElementDoubleAbs(itb.index + k); answer[k] = Double.isInfinite(v) || Double.isNaN(v) ? 0 : v; } } d.setObjectAbs(j, answer); j += is; } } } } start += System.nanoTime(); double otime = ((double) start) / d.getSize(); System.out.printf("Time taken by div for %s: %s; %s (%.1f%%)\n", n, otime, ntime, 100. * (otime - ntime) / otime); checkDatasets(a, b, c, d); n *= SSTEP; } } Random.seed(12735L); n = 32; System.out.println("Dividing " + dn + " by constant"); for (int i = 0; i < SITER; i++) { if (dtype < AbstractDataset.ARRAYINT8) { a = Random.randn(n); a.imultiply(100); a = a.cast(dtype); } else { AbstractDataset[] aa = new AbstractDataset[ISIZEA]; for (int j = 0; j < ISIZEA; j++) { aa[j] = Random.randn(n).imultiply(100); } a = DatasetUtils.cast(aa, dtype); } start = -System.nanoTime(); c = Maths.divide(a, dv); start += System.nanoTime(); double ntime = ((double) start) / c.getSize(); d = AbstractDataset.zeros(c); start = -System.nanoTime(); IndexIterator ita = a.getIterator(); int j = 0; if (dtype == AbstractDataset.COMPLEX64 || dtype == AbstractDataset.COMPLEX128) { final int is = d.getElementsPerItem(); while (ita.hasNext()) { d.setObjectAbs(j, ((Complex) a.getObjectAbs(ita.index)).divide(zv)); j += is; } } else { if (dtype < AbstractDataset.ARRAYINT8) { while (ita.hasNext()) { d.setObjectAbs(j++, ((Number) a.getObjectAbs(ita.index)).doubleValue() / dv); } } else { final double[] answer = new double[ISIZEA]; while (ita.hasNext()) { for (int k = 0; k < ISIZEA; k++) { answer[k] = a.getElementDoubleAbs(ita.index + k) / dv; } d.setObjectAbs(j, answer); j += ISIZEA; } } } start += System.nanoTime(); double otime = ((double) start) / d.getSize(); System.out.printf("Time taken by div for %s: %s; %s (%.1f%%)\n", n, otime, ntime, 100. * (otime - ntime) / otime); checkDatasets(a, dv, c, d); n *= SSTEP; } Random.seed(12735L); n = 32; System.out.println("Dividing constant by " + dn); for (int i = 0; i < SITER; i++) { if (dtype < AbstractDataset.ARRAYINT8) { a = Random.randn(n); a.imultiply(100); a = a.cast(dtype); } else { AbstractDataset[] aa = new AbstractDataset[ISIZEA]; for (int j = 0; j < ISIZEA; j++) { aa[j] = Random.randn(n).imultiply(100); } a = DatasetUtils.cast(aa, dtype); } start = -System.nanoTime(); c = Maths.divide(dv, a); start += System.nanoTime(); double ntime = ((double) start) / c.getSize(); d = AbstractDataset.zeros(c); start = -System.nanoTime(); IndexIterator ita = a.getIterator(); int j = 0; if (dtype == AbstractDataset.COMPLEX64 || dtype == AbstractDataset.COMPLEX128) { final int is = d.getElementsPerItem(); while (ita.hasNext()) { d.setObjectAbs(j, zv.divide((Complex) a.getObjectAbs(ita.index))); j += is; } } else { if (dtype < AbstractDataset.ARRAYINT8) { while (ita.hasNext()) { d.setObjectAbs(j++, dv / ((Number) a.getObjectAbs(ita.index)).doubleValue()); } } else { final double[] answer = new double[ISIZEA]; while (ita.hasNext()) { for (int k = 0; k < ISIZEA; k++) { answer[k] = dv / a.getElementDoubleAbs(ita.index + k); } d.setObjectAbs(j, answer); j += ISIZEA; } } } start += System.nanoTime(); double otime = ((double) start) / d.getSize(); System.out.printf("Time taken by div for %s: %s; %s (%.1f%%)\n", n, otime, ntime, 100. * (otime - ntime) / otime); checkDatasets(dv, a, c, d); n *= SSTEP; } } }