Example usage for org.apache.commons.math3.linear RealVector getDimension

List of usage examples for org.apache.commons.math3.linear RealVector getDimension

Introduction

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

Prototype

public abstract int getDimension();

Source Link

Document

Returns the size of the vector.

Usage

From source file:org.knime.knip.core.util.ApacheMathTools.java

/**
 * Creates a numDims dimensions image where all dimensions d<numDims are of size 1 and the last dimensions contains
 * the vector./*from   ww w .  j a v a2 s  . c om*/
 * 
 * @param <R>
 * @param ar
 * @param type
 * @param numDims number of dimensions
 * @return
 */
public static <R extends RealType<R>> Img<R> vectorToImage(final RealVector ar, final R type, final int numDims,
        final ImgFactory<R> fac) {
    final long[] dims = new long[numDims];

    for (int i = 0; i < (dims.length - 1); i++) {
        dims[i] = 1;
    }
    dims[dims.length - 1] = ar.getDimension();
    final Img<R> res = fac.create(dims, type);
    final Cursor<R> c = res.cursor();
    while (c.hasNext()) {
        c.fwd();
        c.get().setReal(ar.getEntry(c.getIntPosition(numDims - 1)));
    }

    return res;
}

From source file:org.lambda3.indra.core.function.AlphaSkewRelatednessFunction.java

@Override
public double sim(RealVector r1, RealVector r2, boolean sparse) {
    if (r1.getDimension() != r2.getDimension()) {
        return 0;
    }//from w ww .ja v a2  s .  c  om

    double alpha = 0.99;
    double divergence = 0.0;

    for (int i = 0; i < r1.getDimension(); ++i) {
        if (r1.getEntry(i) > 0.0 && r2.getEntry(i) > 0.0) {
            divergence += r1.getEntry(i)
                    * Math.log(r1.getEntry(i) / ((1 - alpha) * r1.getEntry(i) + alpha * r2.getEntry(i)));
        }
    }

    double result = (1 - (divergence / Math.sqrt(2 * Math.log(2))));
    return Math.abs(result);
}

From source file:org.lambda3.indra.core.function.ChebyshevRelatednessFunction.java

@Override
public double sim(RealVector r1, RealVector r2, boolean sparse) {
    if (r1.getDimension() != r2.getDimension()) {
        return 0;
    }//from w  w  w.  j  a v a2s .  c o  m

    double max = 0;
    double tmp;

    for (int i = 0; i < r1.getDimension(); ++i) {
        tmp = Math.abs((r1.getEntry(i) - r2.getEntry(i)));
        max = (tmp > max ? tmp : max);
    }

    double result = 1 / (1 + (max == Double.NaN ? 0 : max));
    return Math.abs(result);
}

From source file:org.lambda3.indra.core.function.CityBlockRelatednessFunction.java

@Override
public double sim(RealVector r1, RealVector r2, boolean sparse) {
    if (r1.getDimension() != r2.getDimension()) {
        return 0;
    }/*from  w ww . j av  a 2 s .c  o m*/

    double sum = 0.0;

    for (int i = 0; i < r1.getDimension(); ++i) {
        sum += Math.abs((r1.getEntry(i) - r2.getEntry(i)));
    }

    double result = 1 / (1 + (sum == Double.NaN ? 0 : sum));
    return Math.abs(result);
}

From source file:org.lambda3.indra.core.function.DiceRelatednessFunction.java

@Override
public double sim(RealVector r1, RealVector r2, boolean sparse) {
    if (r1.getDimension() != r2.getDimension()) {
        return 0;
    }/*from   w w w .ja va 2 s  .c om*/

    double min = 0.0;
    double sum = 0.0;

    for (int i = 0; i < r1.getDimension(); ++i) {
        if (r1.getEntry(i) > r2.getEntry(i)) {
            min += r2.getEntry(i);
        } else {
            min += r1.getEntry(i);
        }
        sum += r1.getEntry(i) + r2.getEntry(i);
    }

    if (sum == 0) {
        return 0;
    }

    double result = 2 * min / sum;
    return Math.abs(result);
}

From source file:org.lambda3.indra.core.function.Jaccard2RelatednessFunction.java

@Override
public double sim(RealVector r1, RealVector r2, boolean sparse) {
    if (r1.getDimension() != r2.getDimension()) {
        return 0;
    }/*from   ww  w . j av a 2  s  .  com*/

    double min = 0.0;
    double max = 0.0;

    for (int i = 0; i < r1.getDimension(); ++i) {
        if (r1.getEntry(i) > r2.getEntry(i)) {
            min += r2.getEntry(i);
            max += r1.getEntry(i);
        } else {
            min += r1.getEntry(i);
            max += r2.getEntry(i);
        }
    }

    if (max == 0) {
        return 0;
    }

    return Math.abs(min / max);
}

From source file:org.lambda3.indra.core.function.JensenShannonRelatednessFunction.java

@Override
public double sim(RealVector r1, RealVector r2, boolean sparse) {
    if (r1.getDimension() != r2.getDimension()) {
        return 0;
    }//from  w ww  .j av  a 2 s.c  o m

    double divergence = 0.0;
    double avr = 0.0;

    for (int i = 0; i < r1.getDimension(); ++i) {
        avr = (r1.getEntry(i) + r2.getEntry(i)) / 2;

        if (r1.getEntry(i) > 0.0 && avr > 0.0) {
            divergence += r1.getEntry(i) * Math.log(r1.getEntry(i) / avr);
        }
    }
    for (int i = 0; i < r2.getDimension(); ++i) {
        avr = (r1.getEntry(i) + r2.getEntry(i)) / 2;

        if (r2.getEntry(i) > 0.0 && avr > 0.0) {
            divergence += r1.getEntry(i) * Math.log(r2.getEntry(i) / avr);
        }
    }

    double result = 1 - (divergence / (2 * Math.sqrt(2 * Math.log(2))));
    return Math.abs(result);
}

From source file:org.lambda3.indra.core.impl.AlphaSkewClient.java

@Override
protected double sim(RealVector r1, RealVector r2, boolean sparse) {
    if (r1.getDimension() != r2.getDimension()) {
        return 0;
    }/*from  w ww.ja  v a2  s. com*/

    double alpha = 0.99;
    double divergence = 0.0;

    for (int i = 0; i < r1.getDimension(); ++i) {
        if (r1.getEntry(i) > 0.0 && r2.getEntry(i) > 0.0) {
            divergence += r1.getEntry(i)
                    * Math.log(r1.getEntry(i) / ((1 - alpha) * r1.getEntry(i) + alpha * r2.getEntry(i)));
        }
    }

    double result = (1 - (divergence / Math.sqrt(2 * Math.log(2))));
    return Math.abs(result);
}

From source file:org.lambda3.indra.core.impl.ChebyshevClient.java

@Override
protected double sim(RealVector r1, RealVector r2, boolean sparse) {
    if (r1.getDimension() != r2.getDimension()) {
        return 0;
    }//from   w  w w .j  a va 2  s.com

    double max = 0;
    double tmp;

    for (int i = 0; i < r1.getDimension(); ++i) {
        tmp = Math.abs((r1.getEntry(i) - r2.getEntry(i)));
        max = (tmp > max ? tmp : max);
    }

    double result = 1 / (1 + (max == Double.NaN ? 0 : max));
    return Math.abs(result);
}

From source file:org.lambda3.indra.core.impl.CityBlockClient.java

@Override
protected double sim(RealVector r1, RealVector r2, boolean sparse) {
    if (r1.getDimension() != r2.getDimension()) {
        return 0;
    }/*from  w w w .j a v  a2  s .c  o m*/

    double sum = 0.0;

    for (int i = 0; i < r1.getDimension(); ++i) {
        sum += Math.abs((r1.getEntry(i) - r2.getEntry(i)));
    }

    double result = 1 / (1 + (sum == Double.NaN ? 0 : sum));
    return Math.abs(result);
}