Dlassq.java

package org.mklab.sdpj.gpack.lapackwrap;

import org.mklab.nfc.matrix.ComplexNumericalMatrix;
import org.mklab.nfc.matrix.RealNumericalMatrix;
import org.mklab.nfc.scalar.ComplexNumericalScalar;
import org.mklab.nfc.scalar.RealNumericalScalar;


/**
 * @author koga
 * @version $Revision$, 2009/04/25
   * @param <RS> type of real scalar
   * @param <RM> type of real matrix
   * @param <CS> type of complex scalar
   * @param <CM> type of complex Matrix
 */
public class Dlassq<RS extends RealNumericalScalar<RS, RM, CS, CM>, RM extends RealNumericalMatrix<RS, RM, CS, CM>, CS extends ComplexNumericalScalar<RS, RM, CS, CM>, CM extends ComplexNumericalMatrix<RS, RM, CS, CM>> {

  /*  -- LAPACK auxiliary routine (version 3.0) --   
  Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd.,   
  Courant Institute, Argonne National Lab, and Rice University   
  June 30, 1999   


  Purpose   
  =======   

  DLASSQ  returns the values  scl  and  smsq  such that   

  ( scl**2 )*smsq = x( 1 )**2 +...+ x( n )**2 + ( scale**2 )*sumsq,   

  where  x( i ) = X( 1 + ( i - 1 )*INCX ). The value of  sumsq  is   
  assumed to be non-negative and  scl  returns the value   

  scl = max( scale, abs( x( i ) ) ).   

  scale and sumsq must be supplied in SCALE and SUMSQ and   
  scl and smsq are overwritten on SCALE and SUMSQ respectively.   

  The routine makes only one pass through the vector x.   

  Arguments   
  =========   

  N       (input) INTEGER   
       The number of elements to be used from the vector X.   

  X       (input) DOUBLE PRECISION array, dimension (N)   
       The vector for which a scaled sum of squares is computed.   
          x( i )  = X( 1 + ( i - 1 )*INCX ), 1 <= i <= n.   

  INCX    (input) INTEGER   
       The increment between successive values of the vector X.   
       INCX > 0.   

  SCALE   (input/output) DOUBLE PRECISION   
       On entry, the value  scale  in the equation above.   
       On exit, SCALE is overwritten with  scl , the scaling factor   
       for the sum of squares.   

  SUMSQ   (input/output) DOUBLE PRECISION   
       On entry, the value  sumsq  in the equation above.   
       On exit, SUMSQ is overwritten with  smsq , the basic sum of   
       squares from which  scl  has been factored out.   

  =====================================================================   
   */
  /**
   * @param n n
   * @param x x
   * @param incx incx
   * @param scale scale
   * @param sumsq sumsq
   * @return result
   */
  RS[] execute(int n, RS[] x, int incx, RS scale, RS sumsq) {
    RS scaleTemp = scale;
    RS sumsqTemp = sumsq;

    int pointer_x = -1;

    if (n > 0) {
      int i1 = (n - 1) * incx + 1;
      int i2 = incx;
      for (int ix = 1; i2 < 0 ? ix >= i1 : ix <= i1; ix += i2) {
        if (!x[pointer_x + ix].isZero()) {
          RS d1 = x[pointer_x + ix];
          RS absxi = d1.abs();

          if (scaleTemp.isLessThan(absxi)) {
            RS d2 = scaleTemp.divide(absxi);
            sumsqTemp = (sumsqTemp.multiply(d2.multiply(d2))).add(1);
            scaleTemp = absxi;
          } else {
            RS d2 = absxi.divide(scaleTemp);
            sumsqTemp = sumsqTemp.add(d2.multiply(d2));
          }
        }
      }
    }

    RS[] ans = scale.createArray(2);
    ans[0] = scaleTemp;
    ans[1] = sumsqTemp;
    return ans;
  }
}