package nxm.sys.libm;

import nxm.sys.inc.Constants;

/**
   Performs the Power 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 Power extends Operator implements Constants {

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

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

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

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

  public final int validate (char type, int n, int mode) {
    switch (mode) {
      case SSS: return n;
      case CSC: 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 CSC: CSC(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 CSC: CSC(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]=(double)Math.pow((double)a[i],(double)b[0]);
      }
    } else if (a.length == 1) {
      for (int i=0; i<n; i++) {
        c[i]=(double)Math.pow((double)a[0],(double)b[i]);
      }
    } else {
      for (int i=0; i<n; i++) {
        c[i]=(double)Math.pow((double)a[i],(double)b[i]);
      }
    }
  }

  /** Apply this operator to mode {@link Operator#CSC} 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 CSC (double[] a, double[] b, double[] c, int n) {
    if (b.length == 1) {
      for (int i=0,j=0; i<n; i++,j+=2) {
        double mag=Math.pow((double)(a[j]*a[j]+a[j+1]*a[j+1]),(double)b[0]*.5);
        double pha=Math.atan2((double)a[j+1],(double)a[j])*b[0];
        c[j]=(double)(mag*Math.cos(pha)); c[j+1]=(double)(mag*Math.sin(pha));
      }
    } else if (a.length == 2) {
      for (int i=0,j=0; i<n; i++,j+=2) {
        double mag=Math.pow((double)(a[0]*a[0]+a[1]*a[1]),(double)b[i]*.5);
        double pha=Math.atan2((double)a[1],(double)a[0])*b[i];
        c[j]=(double)(mag*Math.cos(pha)); c[j+1]=(double)(mag*Math.sin(pha));
      }
    } else {
      for (int i=0,j=0; i<n; i++,j+=2) {
        double mag=Math.pow((double)(a[j]*a[j]+a[j+1]*a[j+1]),(double)b[i]*.5);
        double pha=Math.atan2((double)a[j+1],(double)a[j])*b[i];
        c[j]=(double)(mag*Math.cos(pha)); c[j+1]=(double)(mag*Math.sin(pha));
      }
    }
  }

  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 CSC: CSC(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 CSC: CSC(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]=(float)Math.pow((double)a[i],(double)b[0]);
      }
    } else if (a.length == 1) {
      for (int i=0; i<n; i++) {
        c[i]=(float)Math.pow((double)a[0],(double)b[i]);
      }
    } else {
      for (int i=0; i<n; i++) {
        c[i]=(float)Math.pow((double)a[i],(double)b[i]);
      }
    }
  }

  /** Apply this operator to mode {@link Operator#CSC} 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 CSC (float[] a, float[] b, float[] c, int n) {
    if (b.length == 1) {
      for (int i=0,j=0; i<n; i++,j+=2) {
        double mag=Math.pow((double)(a[j]*a[j]+a[j+1]*a[j+1]),(double)b[0]*.5);
        double pha=Math.atan2((double)a[j+1],(double)a[j])*b[0];
        c[j]=(float)(mag*Math.cos(pha)); c[j+1]=(float)(mag*Math.sin(pha));
      }
    } else if (a.length == 2) {
      for (int i=0,j=0; i<n; i++,j+=2) {
        double mag=Math.pow((double)(a[0]*a[0]+a[1]*a[1]),(double)b[i]*.5);
        double pha=Math.atan2((double)a[1],(double)a[0])*b[i];
        c[j]=(float)(mag*Math.cos(pha)); c[j+1]=(float)(mag*Math.sin(pha));
      }
    } else {
      for (int i=0,j=0; i<n; i++,j+=2) {
        double mag=Math.pow((double)(a[j]*a[j]+a[j+1]*a[j+1]),(double)b[i]*.5);
        double pha=Math.atan2((double)a[j+1],(double)a[j])*b[i];
        c[j]=(float)(mag*Math.cos(pha)); c[j+1]=(float)(mag*Math.sin(pha));
      }
    }
  }

  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 CSC: CSC(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 CSC: CSC(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]=(int)Math.pow((double)a[i],(double)b[0]);
      }
    } else if (a.length == 1) {
      for (int i=0; i<n; i++) {
        c[i]=(int)Math.pow((double)a[0],(double)b[i]);
      }
    } else {
      for (int i=0; i<n; i++) {
        c[i]=(int)Math.pow((double)a[i],(double)b[i]);
      }
    }
  }

  /** Apply this operator to mode {@link Operator#CSC} 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 CSC (int[] a, int[] b, int[] c, int n) {
    if (b.length == 1) {
      for (int i=0,j=0; i<n; i++,j+=2) {
        double mag=Math.pow((double)(a[j]*a[j]+a[j+1]*a[j+1]),(double)b[0]*.5);
        double pha=Math.atan2((double)a[j+1],(double)a[j])*b[0];
        c[j]=(int)(mag*Math.cos(pha)); c[j+1]=(int)(mag*Math.sin(pha));
      }
    } else if (a.length == 2) {
      for (int i=0,j=0; i<n; i++,j+=2) {
        double mag=Math.pow((double)(a[0]*a[0]+a[1]*a[1]),(double)b[i]*.5);
        double pha=Math.atan2((double)a[1],(double)a[0])*b[i];
        c[j]=(int)(mag*Math.cos(pha)); c[j+1]=(int)(mag*Math.sin(pha));
      }
    } else {
      for (int i=0,j=0; i<n; i++,j+=2) {
        double mag=Math.pow((double)(a[j]*a[j]+a[j+1]*a[j+1]),(double)b[i]*.5);
        double pha=Math.atan2((double)a[j+1],(double)a[j])*b[i];
        c[j]=(int)(mag*Math.cos(pha)); c[j+1]=(int)(mag*Math.sin(pha));
      }
    }
  }

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