Java Double Number mod modifiedGramSchmidt(double[][] s)

Here you can find the source of modifiedGramSchmidt(double[][] s)

Description

Orthonormalizes a set of vectors.

License

Open Source License

Declaration

public static double[][] modifiedGramSchmidt(double[][] s) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**// w  w  w. ja  v a  2 s  .c o  m
     * Orthonormalizes a set of vectors.
     */
    public static double[][] modifiedGramSchmidt(double[][] s) {
        // TODO: use Householder reflections for numerical stability
        double[][] u = new double[s.length][s[0].length];
        // orthogonalize
        for (int i = 0; i < s.length; i++) {
            u[i] = s[i];
            for (int j = 0; j < i; j++) {
                u[i] = sumArrays(u[i], scalarProduct(-1, project(u[j], u[i])));
            }
        }
        // normalize
        for (int i = 0; i < u.length; i++) {
            u[i] = normalize(u[i]);
        }
        return u;
    }

    public static double[] sumArrays(double[] xs1, double[] xs2) {
        double[] sum = new double[xs1.length];
        for (int i = 0; i < xs1.length; i++) {
            sum[i] = xs1[i] + xs2[i];
        }
        return sum;
    }

    public static double[] scalarProduct(double x, double[] xs) {
        double[] product = new double[xs.length];
        for (int i = 0; i < product.length; i++) {
            product[i] = x * xs[i];
        }
        return product;
    }

    /**
     * Returns the projection of v in the u direction.
     */
    public static double[] project(double[] u, double[] v) {
        return scalarProduct(dotProduct(u, v) / dotProduct(u, u), u);
    }

    public static double[] normalize(double[] xs) {
        double l2norm = Math.sqrt(dotProduct(xs, xs));
        return scalarProduct(1.0 / l2norm, xs);
    }

    public static double dotProduct(double[] xs1, double[] xs2) {
        double product = 0;
        for (int i = 0; i < xs1.length; i++) {
            product += xs1[i] * xs2[i];
        }
        return product;
    }
}

Related

  1. mod(double x, double y)
  2. mod(final double a, final double b)
  3. mod2pi(double vin)
  4. mod2pi_pos(double vin)
  5. modArray(double value, int mod, int div)
  6. modifiedJulianToJulian(double modifiedJulian)
  7. modifySize(double[] x, int newLen)
  8. modPI2(double angle)
  9. Modulo(double x, double y)