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

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

Introduction

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

Prototype

public abstract double getEntry(int index) throws OutOfRangeException;

Source Link

Document

Return the entry at the specified index.

Usage

From source file:org.grouplens.samantha.modeler.tree.RegressionTree.java

private int predictLeaf(LearningInstance instance) {
    int predNode = -1;
    if (variableSpace.getVectorVarSizeByName(treeName) > 0) {
        StandardLearningInstance ins = (StandardLearningInstance) instance;
        int node = 0;
        do {//  ww w.ja  v a 2 s . c o m
            predNode = node;
            RealVector nodeVec = variableSpace.getVectorVarByNameIndex(treeName, node);
            int splitIdx = (int) nodeVec.getEntry(0);
            if (splitIdx == -1) {
                return predNode;
            }
            double splitVal = nodeVec.getEntry(1);
            double feaVal = 0.0;
            if (ins.getFeatures().containsKey(splitIdx)) {
                feaVal = ins.getFeatures().get(splitIdx);
            }
            if (feaVal <= splitVal) {
                node = (int) nodeVec.getEntry(2);
            } else {
                node = (int) nodeVec.getEntry(3);
            }
            if (node == -1) {
                return predNode;
            }
        } while (node != -1);
    }
    return predNode;
}

From source file:org.grouplens.samantha.server.retriever.FeatureSupportRetrieverConfig.java

public Retriever getRetriever(RequestContext requestContext) {
    List<EntityExpander> entityExpanders = ExpanderUtilities.getEntityExpanders(requestContext, expandersConfig,
            injector);//from   w w w  . j av a2 s .  co m
    FeatureSupportModelManager manager = new FeatureSupportModelManager(modelName, modelFile, injector);
    IndexedVectorModel model = (IndexedVectorModel) manager.manage(requestContext);
    List<ObjectNode> results = new ArrayList<>();
    int size = model.getIndexSize();
    for (int i = 0; i < size; i++) {
        ObjectNode one = Json.newObject();
        Map<String, String> keys = FeatureExtractorUtilities.decomposeKey(model.getKeyByIndex(i));
        RealVector vector = model.getIndexVector(i);
        one.put(supportAttr, vector.getEntry(0));
        for (String attr : itemAttrs) {
            one.put(attr, keys.get(attr));
        }
        results.add(one);
    }
    return new PrecomputedRetriever(results, entityExpanders, config);
}

From source file:org.grouplens.samantha.server.space.RedisVariableSpace.java

public void setScalarVarByName(String name, RealVector vars) {
    for (int i = 0; i < vars.getDimension(); i++) {
        setScalarVarByNameIndex(name, i, vars.getEntry(i));
    }// w ww . ja  va2s.  co  m
}

From source file:org.grouplens.samantha.server.space.RedisVariableSpace.java

public void setVectorVarByNameIndex(String name, int index, RealVector var) {
    String varIdxName = "IDX_V_" + name + "_" + Integer.valueOf(index).toString();
    ArrayNode values = Json.newArray();/*from  w ww.  j a  v a 2 s.  c  o m*/
    values.add(index);
    for (int i = 0; i < var.getDimension(); i++) {
        values.add(var.getEntry(i));
    }
    redisService.setValue(spaceIdentifier, varIdxName, values);
}

From source file:org.knime.al.util.noveltydetection.knfst.MatrixFunctions.java

public static RealVector sqrt(final RealVector vector) {
    final RealVector result = vector.copy();
    for (int e = 0; e < result.getDimension(); e++) {
        result.setEntry(e, Math.sqrt(result.getEntry(e)));
    }/* ww  w .jav  a2  s  .  c o m*/
    return result;
}

From source file:org.knime.knip.core.algorithm.convolvers.KernelTools.java

/**
 * Creates a numDims dimensions image where all dimensions d<numDims are of size 1 and the last dimensions contains
 * the vector./*from www .j  a v  a 2  s.com*/
 * 
 * @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) {
    long[] dims = new long[numDims];

    for (int i = 0; i < dims.length - 1; i++) {
        dims[i] = 1;
    }
    dims[dims.length - 1] = ar.getDimension();
    Img<R> res = fac.create(dims, type);
    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.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 w  ww .  j  a  va2 s  .co m
 * 
 * @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.knowrob.vis.model.util.algorithm.ACCUM.java

/**
 * Calculates curvature for the vertices of a triangle. Based on
 * the trimesh2 (2.12) (Szymon Rusinkiewicz Princeton University)
 * curvature calculation algorithm.//from  w ww.j  a va 2 s.  c  o  m
 * 
 * @see <a href="https://github.com/fcole/qrtsc/tree/master/trimesh2">trimesh2</a>
 * 
 * @param curvatures
 *            vertex curvature mapping
 * @param tri
 *            triangle to calculate curvature for
 */
private static void calculateCurvatureForTriangle(HashMap<Vertex, Curvature> curvatures, Triangle tri) {
    // Edges
    Edge e[] = tri.getEdges();

    // N-T-B coordinate system per face
    Vector3f t = new Vector3f(e[0].getEdgeValue());
    t.normalize();
    Vector3f n = new Vector3f();
    n.cross(e[0].getEdgeValue(), e[1].getEdgeValue());
    //n.normalize();
    Vector3f b = new Vector3f();
    b.cross(n, t);
    b.normalize();

    // Estimate curvature based on variation of normals
    // along edges
    float m[] = { 0.0f, 0.0f, 0.0f };
    double w[][] = { { 0.0d, 0.0d, 0.0d }, { 0.0d, 0.0d, 0.0d }, { 0.0d, 0.0d, 0.0d } };
    for (int j = 0; j < 3; j++) {

        float u = e[j].getEdgeValue().dot(t);
        float v = e[j].getEdgeValue().dot(b);
        w[0][0] += u * u;
        w[0][1] += u * v;
        // w[1][1] += v*v + u*u;
        // w[1][2] += u*v;
        w[2][2] += v * v;
        Vector3f dn = new Vector3f(tri.getPosition()[(j + 2) % 3].getNormalVector());
        Vector3f dnn = new Vector3f(tri.getPosition()[(j + 1) % 3].getNormalVector());
        dn.sub(dnn);
        float dnu = dn.dot(t);
        float dnv = dn.dot(b);
        m[0] += dnu * u;
        m[1] += dnu * v + dnv * u;
        m[2] += dnv * v;
    }
    w[1][1] = w[0][0] + w[2][2];
    w[1][2] = w[0][1];

    RealMatrix coefficients = new Array2DRowRealMatrix(w, false);
    DecompositionSolver solver = new LUDecomposition(coefficients).getSolver();
    if (!solver.isNonSingular()) {
        return;
    }

    RealVector constants = new ArrayRealVector(new double[] { m[0], m[1], m[2] }, false);
    RealVector solution = solver.solve(constants);

    m[0] = (float) solution.getEntry(0);
    m[1] = (float) solution.getEntry(1);
    m[2] = (float) solution.getEntry(2);

    // flag for ill-solutioned system (NaN = no valid info on the curvatures => then consider 
    // the curvatures to be the ones of a simple plane)
    boolean okSolution = true;
    if (isNaN(m[0]) || isNaN(m[1]) || isNaN(m[2])) {
        okSolution = false;
    }

    // Push it back out to the vertices
    for (int j = 0; j < tri.getPosition().length; j++) {
        Vertex vj = tri.getPosition()[j];
        float c1, c12, c2;
        float[] ret;

        // guard against ill-conditioned systems (NaN curvatures are viewed as a simple planar
        // corresponding curves      
        if (okSolution) {
            ret = proj_curv(t, b, m[0], m[1], m[2], curvatures.get(vj).getPrincipleDirectionMax(),
                    curvatures.get(vj).getPrincipleDirectionMin());
            if (isNaN(ret[0]) || isNaN(ret[1]) || isNaN(ret[2])) {
                c1 = 0.0f;
                c12 = 0.0f;
                c2 = 0.0f;
            } else {
                c1 = ret[0];
                c12 = ret[1];
                c2 = ret[2];
            }
        } else {
            c1 = 0.0f;
            c12 = 0.0f;
            c2 = 0.0f;
        }

        Curvature c = curvatures.get(vj);

        double wt = 0.0d;
        if (vj.getPointarea() != 0.0f) {
            if (j == 0)
                wt = tri.getCornerarea().x / vj.getPointarea();
            else if (j == 1)
                wt = tri.getCornerarea().y / vj.getPointarea();
            else
                wt = tri.getCornerarea().z / vj.getPointarea();
        }

        synchronized (c) {
            c.setCurvatureMax((float) (c.getCurvatureMax() + wt * c1));
            c.setCurvatureMinMax((float) (c.getCurvatureMinMax() + wt * c12));
            c.setCurvatureMin((float) (c.getCurvatureMin() + wt * c2));
        }
    }
}

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 w  w.  j  ava2 s . co m

    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  www.  jav  a 2s .  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);
}