package nxm.sys.libm;

import nxm.sys.inc.Constants;

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

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

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

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

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

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

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

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

  /** Apply this operator to mode {@link Operator#CC} data.
      @param a     The first  operand.
      @param b     The second  operand.
      @param n     The number of elements to operate on.
      @param scale The scale factor.
      @see #process(double[][],int,int,double)
   */
  public static final void CC (double[] a, double[] b, int n, double scale) {
      for (int i=0,j=0; i<n; i++,j+=2) {
        double t=(double)Math.sqrt((double)(a[j]*a[j]+a[j+1]*a[j+1])); b[j+1]=(double)(scale*Math.atan2((double)a[j+1],(double)a[j])); b[j]=t;
      }
  }

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

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

  /** Apply this operator to mode {@link Operator#CC} data.
      @param a     The first  operand.
      @param b     The second  operand.
      @param n     The number of elements to operate on.
      @param scale The scale factor.
      @see #process(float[][],int,int,double)
   */
  public static final void CC (float[] a, float[] b, int n, double scale) {
      for (int i=0,j=0; i<n; i++,j+=2) {
        float t=(float)Math.sqrt((double)(a[j]*a[j]+a[j+1]*a[j+1])); b[j+1]=(float)(scale*Math.atan2((double)a[j+1],(double)a[j])); b[j]=t;
      }
  }

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

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

  /** Apply this operator to mode {@link Operator#CC} data.
      @param a     The first  operand.
      @param b     The second  operand.
      @param n     The number of elements to operate on.
      @param scale The scale factor.
      @see #process(int[][],int,int,double)
   */
  public static final void CC (int[] a, int[] b, int n, double scale) {
      for (int i=0,j=0; i<n; i++,j+=2) {
        int t=(int)Math.sqrt((double)(a[j]*a[j]+a[j+1]*a[j+1])); b[j+1]=(int)(scale*Math.atan2((double)a[j+1],(double)a[j])); b[j]=t;
      }
  }

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