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

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

Introduction

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

Prototype

int getDimension();

Source Link

Document

Returns the size of the vector.

Usage

From source file:fi.smaa.libror.MaximalVectorComputation.java

/**
 * Checks whether v1 dominates v2./*www. j a v  a 2 s .  com*/
 * 
 * @param v1
 * @param v2
 * @return true, if v1 \succ v2, false otherwise
 */
public static boolean dominates(RealVector v1, RealVector v2) {
    assert (v1.getDimension() == v2.getDimension());

    boolean largerFound = false;

    for (int i = 0; i < v1.getDimension(); i++) {
        if (v1.getEntry(i) > v2.getEntry(i)) {
            largerFound = true;
        } else if (v1.getEntry(i) < v2.getEntry(i)) {
            return false;
        }
    }
    return largerFound;
}

From source file:gda.spring.propertyeditors.RealVectorPropertyEditorTest.java

protected void assertVectorsEqual(double[] expected, RealVector actual) {
    assertEquals("Length does not match", expected.length, actual.getDimension());
    for (int i = 0; i < expected.length; i++) {
        assertEquals("Element " + i + " does not match", expected[i], actual.getData()[i]);
    }/*www  . java2 s.  co m*/
}

From source file:fi.smaa.libror.RejectionValueFunctionSampler.java

private FullValueFunction sampleValueFunction() throws SamplingException {
    int curIter = 1;

    do {//w ww .  jav a  2  s  .c  o  m
        FullValueFunction vf = new FullValueFunction();
        for (int i = 0; i < model.getNrCriteria(); i++) {
            RealVector lvls = model.getPerfMatrix().getLevels()[i];
            PartialValueFunction pvf = new PartialValueFunction(lvls.getDimension());
            vf.addValueFunction(pvf);
            sampleRandomPartialVF(pvf);
        }

        double[] w = new double[model.getNrCriteria()];
        RandomUtil.createSumToOneRand(w);
        vf.setWeights(w);
        if (acceptance.check(vf)) {
            return vf;
        }
        misses++;
        curIter++;
    } while (curIter <= maxTries);
    throw new SamplingException("Cannot sample a VF within " + maxTries + " iterations");
}

From source file:com.opengamma.analytics.math.linearalgebra.SVDecompositionCommonsResultTest.java

private void assertRealVectorEquals(final RealVector v1, final DoubleMatrix1D v2) {
    final int n = v1.getDimension();
    assertEquals(n, v2.getNumberOfElements());
    for (int i = 0; i < n; i++) {
        assertEquals(v1.getEntry(i), v2.getEntry(i), EPS);
    }/* ww  w  . ja va 2s  . c om*/
}

From source file:fi.smaa.libror.UTAGMSSolver.java

private int getNrLPVariables() {
    // utility of all alts + epsilon
    int sum = 0;/*  w  ww  . ja va  2s.c o m*/
    for (RealVector vec : model.getPerfMatrix().getLevels()) {
        sum += vec.getDimension();
    }
    return sum + 1;
}

From source file:fi.smaa.libror.UTAGMSSolver.java

private List<LinearConstraint> buildMonotonousConstraints(int critIndex) {
    List<LinearConstraint> constList = new ArrayList<LinearConstraint>();
    RealVector levels = model.getPerfMatrix().getLevels()[critIndex];
    for (int i = 0; i < levels.getDimension() - 1; i++) {
        double[] lhs = new double[getNrLPVariables()];
        double[] rhs = new double[getNrLPVariables()];
        lhs[getConstraintOffset(critIndex) + i] = 1.0;
        rhs[getConstraintOffset(critIndex) + i + 1] = 1.0;
        if (strictValueFunctions) {
            lhs[lhs.length - 1] = 1.0; // epsilon
        }/*  w w w .j  a v  a  2 s  .co  m*/
        constList.add(new LinearConstraint(lhs, 0.0, Relationship.LEQ, rhs, 0.0));
    }
    return constList;
}

From source file:eu.amidst.core.exponentialfamily.EF_Normal_NormalParents.java

/**
 * {@inheritDoc}//from  ww  w .j  a v  a  2  s  .  c o m
 */
@Override
public void updateNaturalFromMomentParameters() {

    /*
     * First step: means and convariances
     */
    CompoundVector globalMomentParam = (CompoundVector) this.momentParameters;
    double mean_X = globalMomentParam.getXYbaseMatrix().getEntry(0);
    RealVector mean_Y = globalMomentParam.getTheta_beta0BetaRV();

    double cov_XX = globalMomentParam.getcovbaseMatrix().getEntry(0, 0) - mean_X * mean_X;
    RealMatrix cov_YY = globalMomentParam.getcovbaseMatrix().getSubMatrix(1, nOfParents, 1, nOfParents)
            .subtract(mean_Y.outerProduct(mean_Y));
    RealVector cov_XY = globalMomentParam.getcovbaseMatrix().getSubMatrix(0, 0, 1, nOfParents).getRowVector(0)
            .subtract(mean_Y.mapMultiply(mean_X));
    //RealVector cov_YX = cov_XY; //outerProduct transposes the vector automatically

    /*
     * Second step: betas and variance
     */
    RealMatrix cov_YYInverse = new LUDecompositionImpl(cov_YY).getSolver().getInverse();
    RealVector beta = cov_YYInverse.preMultiply(cov_XY);

    this.betas = new double[beta.getDimension()];
    for (int i = 0; i < beta.getDimension(); i++) {
        this.betas[i] = beta.getEntry(i);
    }
    this.beta0 = mean_X - beta.dotProduct(mean_Y);
    this.variance = cov_XX - beta.dotProduct(cov_XY);

}

From source file:datafu.pig.hash.lsh.metric.MetricUDF.java

/**
 * This UDF expects a query vector as the first element, a threshold (double) as the second, and a bag of vectors.
 * Vectors are represented by tuples with doubles as elements or bags of tuples representing position and value
 * in the case of sparse vectors./*  w ww.  ja  v  a  2  s . c o  m*/
 * 
 * It returns one of the tuples of the bag of vectors.  For an example of its use, please see datafu.pig.hash.lsh.CosineDistanceHash.
 * 
 * @see datafu.pig.hash.lsh.CosineDistanceHash
 */
@Override
public Tuple exec(Tuple input) throws IOException {
    Object firstElement = input.get(0);
    double distanceRange = ((Number) input.get(1)).doubleValue();
    DataBag vectorBag = (DataBag) input.get(2);
    RealVector referenceVector = null;
    if (firstElement instanceof Tuple) {
        //in which case the first element is a non-sparse tuple
        referenceVector = DataTypeUtil.INSTANCE.convert((Tuple) firstElement, dim);
    } else {
        //in which case the first element is a bag, representing a sparse tuple
        referenceVector = DataTypeUtil.INSTANCE.convert(input, dim);
    }

    for (Tuple vecTuple : vectorBag) {
        Object vectorObj = vecTuple.get(0);
        RealVector v2 = null;
        if (vectorObj instanceof Tuple) {
            v2 = DataTypeUtil.INSTANCE.convert((Tuple) vecTuple.get(0), referenceVector.getDimension());
        } else {
            v2 = DataTypeUtil.INSTANCE.convert(vecTuple, referenceVector.getDimension());
        }
        double dist = dist(referenceVector, v2);
        if (dist < distanceRange) {
            return vecTuple;
        }
    }
    return null;
}