package nxm.sys.libm;

import nxm.sys.inc.Constants;

/**
   Performs the DotProduct operation on arrays. <br>
   See {@link Operator} for a description of the available I/O Modes.
   @author  Auto-Generated
   @see     Operator
*/
@SuppressWarnings("cast")
public final class DotProduct extends Operator implements Constants {

  /** Create a new instance. */
  public DotProduct() {
    super();
  }

  /** Create a new instance.
      @param flags The processing flags to use.
   */
  public DotProduct (int flags) {
    super(flags);
  }

  /** Create a new instance.
      @param scale The scale factor to use.
   */
  public DotProduct (double scale) {
    super(scale);
  }

  /** Create a new instance.
      @param flags The processing flags to use.
      @param scale The scale factor to use.
   */
  public DotProduct (int flags, double scale) {
    super(flags,scale);
  }

  public final int validate (char type, int n, int mode) {
    switch (mode) {
      case SSS: return n;
      case CCS: return n;
      case VVS: return n;
      default: return NOSUPPORT;
    }
  }

  public final int process (double[][] v, int n, int mode) {
    switch (mode) {
      case SSS: SSS(v[0],v[1],v[2],n); return n;
      case CCS: CCS(v[0],v[1],v[2],n); return n;
      case VVS: VVS(v[0],v[1],v[2],n); return n;
      default:  return NOSUPPORT;
    }
  }

  public final int process (double[][] v, int n, int mode, double scale) {
    switch (mode) {
      case SSS: SSS(v[0],v[1],v[2],n); return n;
      case CCS: CCS(v[0],v[1],v[2],n); return n;
      case VVS: VVS(v[0],v[1],v[2],n); return n;
      default:  return NOSUPPORT;
    }
  }

  /** Apply this operator to mode {@link Operator#SSS} data.
      @param a     The first  operand.
      @param b     The second  operand.
      @param c     The third  operand.
      @param n     The number of elements to operate on.
      @see #process(double[][],int,int)
   */
  public static final void SSS (double[] a, double[] b, double[] c, int n) {
    if (b.length == 1) {
      for (int i=0; i<n; i++) {
        c[i]=a[i]*b[0];
      }
    } else if (a.length == 1) {
      for (int i=0; i<n; i++) {
        c[i]=a[0]*b[i];
      }
    } else {
      for (int i=0; i<n; i++) {
        c[i]=a[i]*b[i];
      }
    }
  }

  /** Apply this operator to mode {@link Operator#CCS} data.
      @param a     The first  operand.
      @param b     The second  operand.
      @param c     The third  operand.
      @param n     The number of elements to operate on.
      @see #process(double[][],int,int)
   */
  public static final void CCS (double[] a, double[] b, double[] c, int n) {
    if (b.length == 2) {
      for (int i=0,j=0; i<n; i++,j+=2) {
        c[i]=a[j]*b[0]+a[j+1]*b[1];
      }
    } else if (a.length == 2) {
      for (int i=0,j=0; i<n; i++,j+=2) {
        c[i]=a[0]*b[j]+a[1]*b[j+1];
      }
    } else {
      for (int i=0,j=0; i<n; i++,j+=2) {
        c[i]=a[j]*b[j]+a[j+1]*b[j+1];
      }
    }
  }

  /** Apply this operator to mode {@link Operator#VVS} data.
      @param a     The first  vector
      @param b     The second  vector
      @param c     vector to hold the result
      @param n     The number of elements to operate on.
      @see #process(double[][],int,int)
   */
  public static final void VVS (double[] a, double[] b, double[] c, int n) {
    if (b.length == 3) {
      for (int i=0,k=0; i<n; i++,k+=3) {
        c[i]=a[k]*b[0]+a[k+1]*b[1]+a[k+2]*b[2];
      }
    } else if (a.length == 3) {
      for (int i=0,k=0; i<n; i++,k+=3) {
        c[i]=a[0]*b[k]+a[1]*b[k+1]+a[2]*b[k+2];
      }
    } else {
      for (int i=0,k=0; i<n; i++,k+=3) {
        c[i]=a[k]*b[k]+a[k+1]*b[k+1]+a[k+2]*b[k+2];
      }
    }
  }

  public final int process (float[][] v, int n, int mode) {
    switch (mode) {
      case SSS: SSS(v[0],v[1],v[2],n); return n;
      case CCS: CCS(v[0],v[1],v[2],n); return n;
      case VVS: VVS(v[0],v[1],v[2],n); return n;
      default:  return NOSUPPORT;
    }
  }

  public final int process (float[][] v, int n, int mode, double scale) {
    switch (mode) {
      case SSS: SSS(v[0],v[1],v[2],n); return n;
      case CCS: CCS(v[0],v[1],v[2],n); return n;
      case VVS: VVS(v[0],v[1],v[2],n); return n;
      default:  return NOSUPPORT;
    }
  }

  /** Apply this operator to mode {@link Operator#SSS} data.
      @param a     The first  operand.
      @param b     The second  operand.
      @param c     The third  operand.
      @param n     The number of elements to operate on.
      @see #process(float[][],int,int)
   */
  public static final void SSS (float[] a, float[] b, float[] c, int n) {
    if (b.length == 1) {
      for (int i=0; i<n; i++) {
        c[i]=a[i]*b[0];
      }
    } else if (a.length == 1) {
      for (int i=0; i<n; i++) {
        c[i]=a[0]*b[i];
      }
    } else {
      for (int i=0; i<n; i++) {
        c[i]=a[i]*b[i];
      }
    }
  }

  /** Apply this operator to mode {@link Operator#CCS} data.
      @param a     The first  operand.
      @param b     The second  operand.
      @param c     The third  operand.
      @param n     The number of elements to operate on.
      @see #process(float[][],int,int)
   */
  public static final void CCS (float[] a, float[] b, float[] c, int n) {
    if (b.length == 2) {
      for (int i=0,j=0; i<n; i++,j+=2) {
        c[i]=a[j]*b[0]+a[j+1]*b[1];
      }
    } else if (a.length == 2) {
      for (int i=0,j=0; i<n; i++,j+=2) {
        c[i]=a[0]*b[j]+a[1]*b[j+1];
      }
    } else {
      for (int i=0,j=0; i<n; i++,j+=2) {
        c[i]=a[j]*b[j]+a[j+1]*b[j+1];
      }
    }
  }

  /** Apply this operator to mode {@link Operator#VVS} data.
      @param a     The first  operand.
      @param b     The second  operand.
      @param c     The third  operand.
      @param n     The number of elements to operate on.
      @see #process(float[][],int,int)
   */
  public static final void VVS (float[] a, float[] b, float[] c, int n) {
    if (b.length == 3) {
      for (int i=0,k=0; i<n; i++,k+=3) {
        c[i]=a[k]*b[0]+a[k+1]*b[1]+a[k+2]*b[2];
      }
    } else if (a.length == 3) {
      for (int i=0,k=0; i<n; i++,k+=3) {
        c[i]=a[0]*b[k]+a[1]*b[k+1]+a[2]*b[k+2];
      }
    } else {
      for (int i=0,k=0; i<n; i++,k+=3) {
        c[i]=a[k]*b[k]+a[k+1]*b[k+1]+a[k+2]*b[k+2];
      }
    }
  }

  public final int process (int[][] v, int n, int mode) {
    switch (mode) {
      case SSS: SSS(v[0],v[1],v[2],n); return n;
      case CCS: CCS(v[0],v[1],v[2],n); return n;
      case VVS: VVS(v[0],v[1],v[2],n); return n;
      default:  return NOSUPPORT;
    }
  }

  public final int process (int[][] v, int n, int mode, double scale) {
    switch (mode) {
      case SSS: SSS(v[0],v[1],v[2],n); return n;
      case CCS: CCS(v[0],v[1],v[2],n); return n;
      case VVS: VVS(v[0],v[1],v[2],n); return n;
      default:  return NOSUPPORT;
    }
  }

  /** Apply this operator to mode {@link Operator#SSS} data.
      @param a     The first  operand.
      @param b     The second  operand.
      @param c     The third  operand.
      @param n     The number of elements to operate on.
      @see #process(int[][],int,int)
   */
  public static final void SSS (int[] a, int[] b, int[] c, int n) {
    if (b.length == 1) {
      for (int i=0; i<n; i++) {
        c[i]=a[i]*b[0];
      }
    } else if (a.length == 1) {
      for (int i=0; i<n; i++) {
        c[i]=a[0]*b[i];
      }
    } else {
      for (int i=0; i<n; i++) {
        c[i]=a[i]*b[i];
      }
    }
  }

  /** Apply this operator to mode {@link Operator#CCS} data.
      @param a     The first  operand.
      @param b     The second  operand.
      @param c     The third  operand.
      @param n     The number of elements to operate on.
      @see #process(int[][],int,int)
   */
  public static final void CCS (int[] a, int[] b, int[] c, int n) {
    if (b.length == 2) {
      for (int i=0,j=0; i<n; i++,j+=2) {
        c[i]=a[j]*b[0]+a[j+1]*b[1];
      }
    } else if (a.length == 2) {
      for (int i=0,j=0; i<n; i++,j+=2) {
        c[i]=a[0]*b[j]+a[1]*b[j+1];
      }
    } else {
      for (int i=0,j=0; i<n; i++,j+=2) {
        c[i]=a[j]*b[j]+a[j+1]*b[j+1];
      }
    }
  }

  /** Apply this operator to mode {@link Operator#VVS} data.
      @param a     The first  operand.
      @param b     The second  operand.
      @param c     The third  operand.
      @param n     The number of elements to operate on.
      @see #process(int[][],int,int)
   */
  public static final void VVS (int[] a, int[] b, int[] c, int n) {
    if (b.length == 3) {
      for (int i=0,k=0; i<n; i++,k+=3) {
        c[i]=a[k]*b[0]+a[k+1]*b[1]+a[k+2]*b[2];
      }
    } else if (a.length == 3) {
      for (int i=0,k=0; i<n; i++,k+=3) {
        c[i]=a[0]*b[k]+a[1]*b[k+1]+a[2]*b[k+2];
      }
    } else {
      for (int i=0,k=0; i<n; i++,k+=3) {
        c[i]=a[k]*b[k]+a[k+1]*b[k+1]+a[k+2]*b[k+2];
      }
    }
  }

  public int getInputs() { return 2; }
  public int getOutputs() { return 1; }
}
