Example usage for org.apache.commons.math3.linear ArrayRealVector unitize

List of usage examples for org.apache.commons.math3.linear ArrayRealVector unitize

Introduction

In this page you can find the example usage for org.apache.commons.math3.linear ArrayRealVector unitize.

Prototype

public void unitize() throws MathArithmeticException 

Source Link

Document

Converts this vector into a unit vector.

Usage

From source file:EndmemberExtraction.java

public static ArrayList<Integer> ORASIS(double[][] data, int nData, int nDim, double threshold,
        int[] exemplarLabel) {
    ArrayRealVector vec;
    ArrayList<ArrayRealVector> X = new ArrayList<>();
    ArrayList<ArrayRealVector> E = new ArrayList<>();
    ArrayList<Integer> exemplarIndex = new ArrayList<>();

    for (int i = 0; i < nData; i++) {
        vec = new ArrayRealVector(data[i]);
        vec.unitize();
        X.add(vec);/*  w  w  w.jav  a 2 s .  com*/
    }

    E.add(X.get(0));
    exemplarIndex.add(0);
    double t = Math.sqrt(2 * (1 - threshold));

    //Add first element of test spectra to set of exemplar spectra
    exemplarLabel[0] = 0;

    boolean flag;
    double maxCos, sigmaMin, sigmaMax, dotXR, dotER, cosTheta;

    double[] vecR = new double[nDim];
    for (int i = 0; i < nDim; i++) {
        vecR[i] = 1 / Math.sqrt(nDim);
    }
    ArrayRealVector R = new ArrayRealVector(vecR);

    ArrayRealVector exemplarSpec, testSpec;

    for (int i = 0; i < X.size(); i++) {
        if (i == 0 || exemplarLabel[i] == -1) {
            continue;
        }

        flag = false;
        maxCos = 0;
        testSpec = X.get(i);
        dotXR = testSpec.dotProduct(R);
        sigmaMin = dotXR - t;
        sigmaMax = dotXR + t;

        for (int j = 0; j < E.size(); j++) {
            exemplarSpec = E.get(j);
            dotER = exemplarSpec.dotProduct(R);

            if (dotER < sigmaMax && dotER > sigmaMin) {
                cosTheta = testSpec.dotProduct(exemplarSpec);

                if (cosTheta > threshold) {
                    //Test spectra is similar to one of the exemplar spectra
                    if (cosTheta > maxCos) {
                        maxCos = cosTheta;
                        exemplarLabel[i] = j;
                        //System.out.println("Count: "+i+"\texemplarLabel: "+exemplarLabel[i]);
                        flag = true;
                    }
                }
            }
        }

        if (!flag) {
            //Test spectra is unique, add it to set of exemplars
            E.add(testSpec);
            exemplarIndex.add(i);
            exemplarLabel[i] = E.size() - 1;
            //System.out.println("Count: "+i+"\texemplarLabel: "+exemplarLabel[i]);
        }

    }
    return exemplarIndex;
}