Java Array Multiply multiply(final float[] complexA, final float[] complexB, final boolean overwriteA)

Here you can find the source of multiply(final float[] complexA, final float[] complexB, final boolean overwriteA)

Description

multiply

License

Open Source License

Declaration

public static float[] multiply(final float[] complexA, final float[] complexB, final boolean overwriteA) 

Method Source Code

//package com.java2s;
/**// w w  w  . ja v a  2 s . c  om
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License 2
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * An execption is the FFT implementation of Dave Hale which we use as a library,
 * wich is released under the terms of the Common Public License - v1.0, which is
 * available at http://www.eclipse.org/legal/cpl-v10.html
 *
 * @author Stephan Preibisch
 */

public class Main {
    public static float[] multiply(final float[] complexA, final float[] complexB, final boolean overwriteA) {
        if (complexA.length != complexB.length) {
            return null;
        }

        float[] complexResult = null;

        if (!overwriteA) {
            complexResult = new float[complexA.length];
        }

        // this is the amount of complex numbers
        // the actual array size is twice as high
        final int wComplex = complexA.length / 2;

        // we compute: (a + bi) * (c + di)
        float a, b, c, d;

        if (!overwriteA) {
            for (int pos = 0; pos < wComplex; pos++) {
                a = complexA[pos * 2];
                b = complexA[pos * 2 + 1];
                c = complexB[pos * 2];
                d = complexB[pos * 2 + 1];

                // compute new real part
                complexResult[pos * 2] = multiplyComplexReal(a, b, c, d);

                // compute new imaginary part
                complexResult[pos * 2 + 1] = multiplyComplexImg(a, b, c, d);
            }
        } else {
            for (int pos = 0; pos < wComplex; pos++) {
                a = complexA[pos * 2];
                b = complexA[pos * 2 + 1];
                c = complexB[pos * 2];
                d = complexB[pos * 2 + 1];

                // compute new real part
                complexA[pos * 2] = multiplyComplexReal(a, b, c, d);

                // compute new imaginary part
                complexA[pos * 2 + 1] = multiplyComplexImg(a, b, c, d);
            }
        }

        if (overwriteA) {
            return complexA;
        }
        return complexResult;
    }

    public static float multiplyComplexReal(final float a, final float b, final float c, final float d) {
        return a * c - b * d;
    }

    public static float multiplyComplexImg(final float a, final float b, final float c, final float d) {
        return a * d + b * c;
    }
}

Related

  1. multiply(final double[] a, double b, final double[] dest, int n)
  2. multiply(final double[] inout, final double[] in)
  3. multiply(final double[] lhs, final double[] rhs)
  4. multiply(final double[] v1, final double[] v2)
  5. multiply(final double[] x, final double[] y)
  6. multiply(final int[] numbers)
  7. multiply(float[] array, float factor)
  8. multiply(float[] array, int multiplier)
  9. multiply(float[] in, float f)