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

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

Introduction

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

Prototype

public double getImaginary() 

Source Link

Document

Access the imaginary part.

Usage

From source file:uk.ac.diamond.scisoft.analysis.fitting.functions.FanoGaussian.java

@Override
public void fillWithValues(DoubleDataset data, CoordinatesIterator it) {
    if (isDirty()) {
        calcCachedParameters();//  ww w  .  jav a2 s  . c  o  m
    }

    it.reset();
    double[] coords = it.getCoordinates();
    int i = 0;
    double[] buffer = data.getData();
    while (it.hasNext()) {
        Complex z = new Complex(fr * (coords[0] - r), zi);
        Complex w = Faddeeva.w(z, 0);

        buffer[i++] = ft * (w.getReal() + fq * w.getImaginary());
    }

}

From source file:uk.ac.diamond.scisoft.analysis.fitting.functions.Polynomial.java

private static Complex[] sort(Complex[] values) {
    // reorder to NumPy's roots output
    List<Complex> rts = Arrays.asList(values);
    Collections.sort(rts, new Comparator<Complex>() {
        @Override/* w w  w .  j  a va  2 s .  co  m*/
        public int compare(Complex o1, Complex o2) {
            double a = o1.getReal();
            double b = o2.getReal();

            double u = 10 * Math.ulp(Math.max(Math.abs(a), Math.abs(b)));
            if (Math.abs(a - b) > u)
                return a < b ? -1 : 1;

            a = o1.getImaginary();
            b = o2.getImaginary();
            if (a == b)
                return 0;
            return a < b ? 1 : -1;
        }
    });

    return rts.toArray(new Complex[0]);
}

From source file:uk.ac.diamond.scisoft.analysis.fitting.functions.PolynomialTest.java

private static void checkRoot(Complex root, double... coeffs) {
    double[] reals = new double[coeffs.length];
    double[] imags = new double[coeffs.length];
    Complex z = new Complex(1, 0);
    for (int i = coeffs.length - 1; i >= 0; i--) {
        double c = coeffs[i];
        reals[i] = c * z.getReal();//  www.j a va2s  .  c om
        imags[i] = c * z.getImaginary();
        z = z.multiply(root);
    }

    double tol = 1e-7 * findAbsMax(coeffs);
    Assert.assertEquals("R " + root + " @t " + tol, 0, sumC(reals), tol);
    Assert.assertEquals("I " + root, 0, sumC(imags), tol);
}

From source file:uk.ac.diamond.scisoft.analysis.fitting.functions.PolynomialTest.java

private static void checkComplex(Complex[] expected, Complex[] actual, double err) {
    Assert.assertEquals(expected.length, actual.length);
    for (int i = 0; i < expected.length; i++) {
        Complex e = expected[i];
        Complex a = actual[i];//from w  w w .j av  a  2 s .c om
        Assert.assertEquals("Real " + i, e.getReal(), a.getReal(), err);
        Assert.assertEquals("Imag " + i, e.getImaginary(), a.getImaginary(), err);
    }
}

From source file:uk.ac.diamond.scisoft.analysis.io.HDF5LoaderTest.java

@Test
public void testLoadingCompoundDatatype() throws ScanFileHolderException {
    String n = TestFileFolder + "h5py_complex.h5";
    HDF5Loader l = new HDF5Loader(n);
    DataHolder dh = l.loadFile();/*from   w  w  w .j  av  a  2 s  . c om*/
    assertEquals("File does not have the correct number of datasets", 1, dh.getNames().length);
    assertTrue(dh.contains("/complex_example"));
    ILazyDataset data = dh.getLazyDataset("/complex_example");
    int[] shape = data.getShape();
    assertEquals("Dataset is not the right shape", 2, shape.length);
    assertEquals("Dataset dimension 0 is not of the correct shape", 50, shape[0]);
    assertEquals("Dataset dimension 1 is not of the correct shape", 50, shape[1]);
    IDataset s = data.getSlice(null, new int[] { 1, 1 }, null);
    assertTrue(s instanceof ComplexDoubleDataset);
    ComplexDoubleDataset cs = (ComplexDoubleDataset) s;
    Complex cx = cs.getComplexAbs(0);
    assertEquals(2.18634188175992, cx.getReal(), 1e-15);
    assertEquals(-0.01617389291212438, cx.getImaginary(), 1e-15);

    s = data.getSlice(new int[] { 1, 3 }, new int[] { 10, 10 }, new int[] { 2, 3 });
    assertTrue(s instanceof ComplexDoubleDataset);
    cs = (ComplexDoubleDataset) s;
    assertEquals("Dataset dimension 0 is not of the correct shape", 5, cs.getShapeRef()[0]);
    assertEquals("Dataset dimension 1 is not of the correct shape", 3, cs.getShapeRef()[1]);
    cx = cs.getComplex(0, 0);
    assertEquals(0.22884877031898887, cx.getReal(), 1e-15);
    assertEquals(0.19673784135439948, cx.getImaginary(), 1e-15);
    cx = cs.getComplex(4, 2);
    assertEquals(0.6922704317579508, cx.getReal(), 1e-15);
    assertEquals(-1.8087566023531674, cx.getImaginary(), 1e-15);
}

From source file:uk.ac.diamond.scisoft.analysis.rcp.views.PlotViewStatsAndMaths.java

private String formatObject(Object o) {
    if (o instanceof Number) {
        return formatNumber((Number) o);
    } else if (o instanceof double[]) {
        return formatDoubleArray((double[]) o);
    } else if (o instanceof Complex) {
        Complex z = (Complex) o;
        double i = z.getImaginary();
        if (i < 0)
            return String.format(doubleFormat + " - " + doubleFormat + "j", z.getReal(), i);
        return String.format(doubleFormat + " + " + doubleFormat + "j", z.getReal(), i);
    }//from w  w  w. j  ava2  s  . c om

    return o.toString();
}

From source file:uk.ac.diamond.scisoft.analysis.utils.Faddeeva.java

/**
 * Compute the scaled complementary error function for a complex argument
 * @param z/*from w  w w .j  a  v  a  2s .c o m*/
 * @param relerr
 * @return erfcx(z) = exp(z^2) erfc(z)
 */
public static Complex erfcx(Complex z, double relerr) {
    return C(erfcx_i(C(z.getReal(), z.getImaginary()), relerr));
}

From source file:uk.ac.diamond.scisoft.analysis.utils.Faddeeva.java

/**
 * Compute the error function for a complex argument
 * @param z/* w  ww. j  a va  2  s.com*/
 * @param relerr
 * @return erf(z)
 */
public static Complex erf(Complex z, double relerr) {
    return C(erf_i(C(z.getReal(), z.getImaginary()), relerr));
}

From source file:uk.ac.diamond.scisoft.analysis.utils.Faddeeva.java

/**
 * Compute the imaginary error function for a complex argument
 * //from   w w w  . ja  v  a  2s .c om
 * @param z
 * @param relerr
 * @return erfi(z) = -i erf(iz)
 */
public static Complex erfi(Complex z, double relerr) {
    return C(erfi_i(C(z.getReal(), z.getImaginary()), relerr));
}

From source file:uk.ac.diamond.scisoft.analysis.utils.Faddeeva.java

/**
 * Compute the complementary error function for a complex argument
 *
 * @param z//from  w  w w .j  av a2  s. c o  m
 * @param relerr
 * @return erfc(z) = 1 - erfc(z)
 */
public static Complex erfc(Complex z, double relerr) {
    return C(erfc_i(C(z.getReal(), z.getImaginary()), relerr));
}