package net.heartshield.signal;

import org.apache.commons.math3.complex.Complex;

/**
 * Numerical utilities on Complex[] -- compare numpy functions.
 *
 * @author David Madl (git@abanbytes.eu)
 * @date 2017-03-28
 */
public class CVec {
    /** concatenate two or more arrays */
    public static Complex[] stack(Complex[]... arrs) {
        int len = 0;
        for(Complex[] x : arrs)
            len += x.length;

        Complex[] a = new Complex[len];

        int i = 0;
        for(Complex[] x : arrs) {
            System.arraycopy(x, 0, a, i, x.length);
            i += x.length;
        }

        return a;
    }

    private static void throwIfLengthMismatch(Complex[] x, Complex[] y) {
        if(x.length != y.length)
            throw new IllegalArgumentException("x.length=" + x.length + " != y.length=" + y.length);
    }

    /** element-wise multiplication */
    public static Complex[] mul(Complex[] x, Complex[] y) {
        throwIfLengthMismatch(x, y);
        Complex[] a = new Complex[x.length];
        for(int i = 0; i < x.length; i++)
            a[i] = x[i].multiply(y[i]);
        return a;
    }
}
