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

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

Introduction

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

Prototype

public static Complex valueOf(double realPart, double imaginaryPart) 

Source Link

Document

Create a complex number given the real and imaginary parts.

Usage

From source file:com.wwidesigner.geometry.calculation.DefaultHoleCalculator.java

public TransferMatrix calcTransferMatrix_2010(Hole hole, boolean isOpen, double waveNumber,
        PhysicalParameters parameters) {
    double radius = mFudgeFactor * hole.getDiameter() / 2;
    double boreRadius = hole.getBoreDiameter() / 2;
    Complex Zs = null;//w  ww.  j a v a  2  s .  co m
    Complex Za = null;

    // double Z0 = parameters.calcZ0(boreRadius);
    double Z0h = parameters.calcZ0(radius);

    double delta = radius / boreRadius;

    double tm = (radius * delta / 8.) * (1. + 0.207 * delta * delta * delta);
    double te = hole.getHeight() + tm;

    double ta = 0.;

    // Complex Gamma = Complex.I.multiply(wave_number);

    if (isOpen) // open
    {
        double kb = waveNumber * radius;
        double ka = waveNumber * boreRadius;
        double xhi = 0.25 * kb * kb;

        ta = (-0.35 + 0.06 * Math.tanh(2.7 * hole.getHeight() / radius)) * radius * delta * delta * delta
                * delta;

        Complex Zr = Complex.I.multiply(waveNumber * 0.61 * radius).add(xhi);

        Complex Zo = (Zr.multiply(Math.cos(waveNumber * te)).add(Complex.I.multiply(Math.sin(waveNumber * te))))
                .divide(Complex.I.multiply(Zr).multiply(Math.sin(waveNumber * te))
                        .add(Math.cos(waveNumber * te)));

        double ti = radius
                * (0.822 - 0.10 * delta - 1.57 * delta * delta + 2.14 * delta * delta * delta
                        - 1.6 * delta * delta * delta * delta + 0.50 * delta * delta * delta * delta * delta)
                * (1. + (1. - 4.56 * delta + 6.55 * delta * delta)
                        * (0.17 * ka + 0.92 * ka * ka + 0.16 * ka * ka * ka - 0.29 * ka * ka * ka * ka));

        Zs = Complex.I.multiply(waveNumber * ti).add(Zo).multiply(Z0h);

    } else {
        ta = (-0.12 - 0.17 * Math.tanh(2.4 * hole.getHeight() / radius)) * radius * delta * delta * delta
                * delta;
        Zs = Complex.valueOf(0, -Z0h / Math.tan(waveNumber * te));
    }

    Za = Complex.I.multiply(Z0h * waveNumber * ta);
    Complex Za_Zs = Za.divide(Zs);

    TransferMatrix result = new TransferMatrix(Za_Zs.divide(2.).add(1.), Za.multiply(Za_Zs.divide(4.).add(1.)),
            Complex.ONE.divide(Zs), Za_Zs.divide(2.0).add(1.));

    assert result.determinant() == Complex.ONE;

    return result;
}

From source file:net.sf.dsp4j.octave.packages.signal_1_2_0.NCauer.java

public NCauer(double Rp, double Rs, int n) {

    // Cutoff frequency = 1:
    double wp = 1;

    // Stop band edge ws:
    double ws = __ellip_ws(n, Rp, Rs);

    double k = wp / ws;
    double k1 = Math.sqrt(1.0 - Math.pow(k, 2));
    double q0 = (1.0 / 2.0) * ((1.0 - Math.sqrt(k1)) / (1 + Math.sqrt(k1)));
    double q = q0 + 2.0 * Math.pow(q0, 5) + 15.0 * Math.pow(q0, 9) + 150.0 * Math.pow(q0, 13); //%(....)
    double D = (Math.pow(10, 0.1 * Rs) - 1.0) / (Math.pow(10, (0.1 * Rp)) - 1.0);

    //Filter order maybe this, but not used now:
    //n=ceil(log10(16*D)/log10(1/q))

    double l = (1.0 / (2.0 * n)) * Math.log((Math.pow(10, 0.05 * Rp) + 1.0) / (Math.pow(10, 0.05 * Rp) - 1.0));
    double sig01 = 0;
    double sig02 = 0;
    for (int m = 0; m <= 30; m++) {
        sig01 = sig01 + Math.pow((-1), m) * Math.pow(q, (m * (m + 1))) * Math.sinh((2 * m + 1) * l);
    }//  w  w w  .ja  v  a  2 s  . c  om
    for (int m = 1; m <= 30; m++) {
        sig02 = sig02 + Math.pow(-1.0, m) * Math.pow(q, (Math.pow(m, 2))) * Math.cosh(2 * m * l);
    }
    double sig0 = Math.abs((2.0 * Math.pow(q, (1.0 / 4.0)) * sig01) / (1.0 + 2.0 * sig02));

    double w = Math.sqrt((1.0 + k * Math.pow(sig0, 2)) * (1.0 + Math.pow(sig0, 2) / k));

    int r;
    if (n % 2 != 0) {
        r = (n - 1) / 2;
    } else {
        r = n / 2;
    }

    double[] wi = new double[r];
    for (int ii = 1; ii <= r; ii++) {
        double mu;
        if (n % 2 != 0) {
            mu = ii;
        } else {
            mu = (double) ii - 0.5;
        }
        double soma1 = 0;
        for (int m = 0; m <= 30; m++) {
            soma1 = soma1 + 2.0 * Math.pow(q, (1.0 / 4.0)) * (Math.pow(-1.0, m) * Math.pow(q, (m * (m + 1)))
                    * Math.sin(((2.0 * m + 1.0) * Math.PI * mu) / n));
        }
        double soma2 = 0;
        for (int m = 1; m <= 30; m++) {
            soma2 = soma2 + 2.0 * (Math.pow(-1.0, m) * Math.pow(q, (Math.pow(m, 2)))
                    * Math.cos((2.0 * m * Math.PI * mu) / n));
        }
        wi[ii - 1] = (soma1 / (1.0 + soma2));
    }

    double[] Vi = new double[wi.length];
    for (int i = 0; i < wi.length; i++) {
        Vi[i] = Math.sqrt((1.0 - (k * (Math.pow(wi[i], 2)))) * (1.0 - (Math.pow(wi[i], 2)) / k));
    }
    double[] A0i = new double[wi.length];
    for (int i = 0; i < wi.length; i++) {
        A0i[i] = 1.0 / (Math.pow(wi[i], 2));
    }
    double[] sqrA0i = new double[wi.length];
    for (int i = 0; i < wi.length; i++) {
        sqrA0i[i] = 1.0 / wi[i];
    }

    double[] B0i = new double[wi.length];
    for (int i = 0; i < wi.length; i++) {
        B0i[i] = (Math.pow((sig0 * Vi[i]), 2) + Math.pow((w * wi[i]), 2))
                / Math.pow((1.0 + Math.pow(sig0, 2) * Math.pow(wi[i], 2)), 2);
    }

    double C01;
    if (wi.length == 0) {
        C01 = 1.0;
    } else {
        C01 = B0i[0] / A0i[0];
        for (int i = 1; i < wi.length; i++) {
            C01 *= B0i[i] / A0i[i];
        }
    }

    //Gain T0:
    if (n % 2 != 0) {
        T0 = sig0 * C01 * Math.sqrt(ws);
    } else {
        T0 = Math.pow(10, (-0.05 * Rp)) * C01;
    }

    //zeros:
    zer = new Complex[sqrA0i.length * 2];
    for (int i = 0; i < sqrA0i.length; i++) {
        zer[i] = Complex.valueOf(0.0, sqrA0i[i]);
        zer[i + sqrA0i.length] = Complex.valueOf(0.0, -sqrA0i[i]);
    }

    //poles:
    pol = new Complex[Vi.length * 2];
    for (int i = 0; i < Vi.length; i++) {
        pol[i] = new Complex(-2.0 * sig0 * Vi[i], 2.0 * wi[i] * w)
                .divide(2.0 * (1 + Math.pow(sig0, 2) * Math.pow(wi[i], 2)));
        pol[i + Vi.length] = new Complex(-2.0 * sig0 * Vi[i], -2.0 * wi[i] * w)
                .divide(2.0 * (1 + Math.pow(sig0, 2) * Math.pow(wi[i], 2)));
    }

    //If n odd, there is a real pole  -sig0:
    if (n % 2 != 0) {
        pol = Arrays.copyOf(pol, pol.length + 1);
        pol[pol.length - 1] = new Complex(-sig0);
    }

    for (int i = 0; i < pol.length; i++) {
        pol[i] = pol[i].multiply(Math.sqrt(ws));
    }
    for (int i = 0; i < zer.length; i++) {
        zer[i] = zer[i].multiply(Math.sqrt(ws));
    }

}

From source file:com.wwidesigner.geometry.calculation.DefaultHoleCalculator.java

@Override
public TransferMatrix calcTransferMatrix(Hole hole, boolean isOpen, double waveNumber,
        PhysicalParameters parameters) {
    double radius = mFudgeFactor * hole.getDiameter() / 2;
    double boreRadius = hole.getBoreDiameter() / 2;
    Complex Ys = Complex.ZERO; // Shunt admittance == 1/Zs
    Complex Za = Complex.ZERO; // Series impedance

    double Z0h = parameters.calcZ0(radius);
    double delta = radius / boreRadius;
    double delta2 = delta * delta;
    // double Z0 = parameters.calcZ0(boreRadius);
    // Z0 == Z0h * delta*delta

    double tm = (radius * delta / 8.) * (1. + 0.207 * delta * delta2);
    double te = hole.getHeight() + tm;

    double ta = 0.;

    // Complex Gamma = Complex.I.multiply(wave_number);

    if (isOpen) {
        double kb = waveNumber * radius;
        double ka = waveNumber * boreRadius;

        ta = (-0.35 + 0.06 * FastMath.tanh(2.7 * hole.getHeight() / radius)) * radius * delta2;

        Complex Zr = new Complex(0.25 * kb * kb,
                (0.822 - 0.47 * FastMath.pow(radius / (boreRadius + hole.getHeight()), 0.8)) * waveNumber
                        * radius);/*from  w w  w  .  java2 s .  com*/
        double cos = FastMath.cos(waveNumber * te);
        Complex jsin = new Complex(0, FastMath.sin(waveNumber * te));

        Complex Zo = (Zr.multiply(cos).add(jsin)).divide(Zr.multiply(jsin).add(cos));

        double ti = radius
                * (0.822 + delta
                        * (-0.095 + delta * (-1.566 + delta * (2.138 + delta * (-1.640 + delta * 0.502)))))
                * (1. + (1. - 4.56 * delta + 6.55 * delta2) * ka
                        * (0.17 + ka * (0.92 + ka * (0.16 - 0.29 * ka))));

        Ys = Complex.ONE.divide(Complex.I.multiply(waveNumber * ti).add(Zo).multiply(Z0h));

    } else if (hole.getKey() == null) {
        // Tonehole closed by player's finger.
        if (hole.getHeight() <= AssumedFingerSize) {
            // Finger is likely to fill the hole. Ignore the hole entirely.
            ta = 0.;
            Ys = Complex.ZERO;
        } else {
            ta = (-0.12 - 0.17 * FastMath.tanh(2.4 * (hole.getHeight() - AssumedFingerSize) / radius)) * radius
                    * delta2;
            Ys = Complex.valueOf(0, FastMath.tan(waveNumber * (te - AssumedFingerSize)) / Z0h);
        }
    } else {
        // Tonehole closed by key.
        ta = (-0.12 - 0.17 * FastMath.tanh(2.4 * hole.getHeight() / radius)) * radius * delta2;
        Ys = Complex.valueOf(0, FastMath.tan(waveNumber * te) / Z0h);
    }

    Za = Complex.I.multiply(Z0h * delta2 * waveNumber * ta);
    Complex Za_Zs = Za.multiply(Ys);

    Complex A = Za_Zs.divide(2.).add(1.);
    Complex B = Za.multiply(Za_Zs.divide(4.).add(1.));
    Complex C = Ys;
    // Choose A and D to make the determinant = 1.
    // Complex A = Complex.ONE.add(B.multiply(C)).sqrt();
    TransferMatrix result = new TransferMatrix(A, B, C, A);

    // assert result.determinant() == Complex.ONE;

    return result;
}

From source file:com.wwidesigner.util.PhysicalParameters.java

/**
 * Compute the complex wave vector, allowing for losses.
 * @param waveNumber : non-lossy wave number, in radians/meter
 * @param radius : tube radius, in m//from   w ww.  jav  a 2  s  .  co  m
 * @return omega/v - j * alpha
 */
public Complex getComplexWaveNumber(double waveNumber, double radius) {
    double alpha = (1 / radius) * Math.sqrt(waveNumber) * mAlphaConstant;
    return Complex.I.multiply(waveNumber).add(Complex.valueOf(1, 1).multiply(alpha));
}

From source file:org.briljantframework.array.AbstractComplexArray.java

@Override
public void assign(double[] value) {
    Check.argument(value.length == size() * 2);
    int j = 0;//from  www.java  2  s. co  m
    for (int i = 0; i < size(); i++) {
        Complex c = Complex.valueOf(value[j], value[j + 1]);
        j += 2;
    }
}

From source file:org.briljantframework.array.netlib.NetlibLinearAlgebraRoutinesTest.java

@Test
public void testGeev() throws Exception {
    final int n = 5;
    DoubleArray a = bj.newDoubleVector(-1.01, 3.98, 3.30, 4.43, 7.31, 0.86, 0.53, 8.26, 4.96, -6.43, -4.60,
            -7.04, -3.89, -7.66, -6.16, 3.31, 5.29, 8.20, -7.33, 2.47, -4.81, 3.55, -1.51, 6.18, 5.58)
            .reshape(n, n);//from   ww  w  . j  av  a  2s .  com
    DoubleArray wr = bj.newDoubleArray(n);
    DoubleArray wi = bj.newDoubleArray(n);
    DoubleArray vl = bj.newDoubleArray(n, n);
    DoubleArray vr = bj.newDoubleArray(n, n);
    linalg.geev('v', 'v', a, wr, wi, vl, vr);

    ArrayAssert.assertArrayEquals(
            bj.newComplexVector(Complex.valueOf(2.858132878, 10.7627498307),
                    Complex.valueOf(2.858132878, -10.7627498307), Complex.valueOf(-0.6866745133, 4.7042613406),
                    Complex.valueOf(-0.6866745133, -4.7042613406), Complex.valueOf(-10.4629167295)),
            toComplex(wr, wi));
}

From source file:org.briljantframework.array.netlib.NetlibLinearAlgebraRoutinesTest.java

ComplexArray toComplex(DoubleArray r, DoubleArray i) {
    ComplexArray c = bj.newComplexArray(r.getShape());
    for (int j = 0; j < r.size(); j++) {
        c.set(j, Complex.valueOf(r.get(j), i.get(j)));
    }/*from  w  ww .j  a va 2  s . c  om*/
    return c;
}

From source file:org.briljantframework.complex.MutableComplex.java

public Complex toComplex() {
    return Double.isNaN(real) || Double.isNaN(imag) ? Complex.NaN : Complex.valueOf(real, imag);
}

From source file:org.briljantframework.math.transform.DiscreteFourierTransform.java

private static void transformBluestein(ComplexArray a) {
    // Find a power-of-2 convolution length m such that m >= n * 2 + 1
    int n = a.size();
    if (n >= 0x20000000) { // n >= 536870912
        throw new IllegalArgumentException("");
    }// w  w w . j a v  a  2 s . com
    int m = Integer.highestOneBit(n * 2 + 1) << 1;

    // Trigonometric tables
    DoubleArray cosTable = DoubleArray.zeros(n);
    DoubleArray sinTable = DoubleArray.zeros(n);
    for (int i = 0; i < n; i++) {
        int j = (int) ((long) i * i % (n * 2));
        cosTable.set(i, Math.cos(Math.PI * j / n));
        sinTable.set(i, Math.sin(Math.PI * j / n));
    }

    ComplexArray an = ComplexArray.zeros(m);
    ComplexArray bn = ComplexArray.zeros(m);

    bn.set(0, new Complex(cosTable.get(0), sinTable.get(0)));
    for (int i = 0; i < n; i++) {
        double cos = cosTable.get(i);
        double sin = sinTable.get(i);
        Complex complex = a.get(i);
        double real = complex.getReal() * cos + complex.getImaginary() * sin;
        double imag = -complex.getReal() * sin + complex.getImaginary() * cos;
        an.set(i, new Complex(real, imag));

        int j = i + 1;
        if (j < n) {
            Complex bcVal = Complex.valueOf(cosTable.get(j), sinTable.get(j));
            bn.set(j, bcVal);
            bn.set(m - j, bcVal);
        }
    }

    // Convolution
    ComplexArray cc = convolve(an, bn);
    for (int i = 0; i < n; i++) {
        double cos = cosTable.get(i);
        double sin = sinTable.get(i);

        Complex cv = cc.get(i);
        double real = cv.getReal() * cos + cv.getImaginary() * sin;
        double imag = -cv.getReal() * sin + cv.getImaginary() * cos;
        a.set(i, new Complex(real, imag));
    }
}

From source file:org.briljantframework.math.transform.DiscreteFourierTransform.java

private static ComplexArray convolve(ComplexArray x, ComplexArray y) {
    int n = x.size();
    ComplexArray xt = fft(x);/*from  ww  w. j a  v a 2 s  .  co m*/
    ComplexArray yt = fft(y);

    for (int i = 0; i < n; i++) {
        xt.set(i, xt.get(i).multiply(yt.get(i)));
    }

    // TODO: 02/12/15 performance
    for (int i = 0; i < n; i++) {
        Complex complex = xt.get(i);
        xt.set(i, new Complex(complex.getImaginary(), complex.getReal()));
    }

    fftInplace(xt); // inverse transform, since xt is reversed above
    for (int i = 0; i < n; i++) {
        Complex c = xt.get(i);
        xt.set(i, Complex.valueOf(c.getImaginary() / n, c.getReal() / n));
    }
    return xt;
}