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

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

Introduction

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

Prototype

public double walkInOptimizedOrder(final RealVectorChangingVisitor visitor) 

Source Link

Document

Visits (and possibly alters) all entries of this vector in optimized order.

Usage

From source file:edu.byu.nlp.math.RealVectors.java

public static double sum(RealVector v) {
    if (v == null) {
        return 0;
    }//w ww  . j  a  v a  2s.  c om
    if (v.getDimension() == 0) {
        return 0;
    }
    return v.walkInOptimizedOrder(new RealVectorPreservingVisitor() {
        private double total = 0;

        @Override
        public void visit(int index, double value) {
            total += value;
        }

        @Override
        public void start(int dimension, int start, int end) {
        }

        @Override
        public double end() {
            return total;
        }
    });
}

From source file:com.github.thorbenlindhauer.factor.CanonicalGaussianFactor.java

/**
 * mapping must have the size of the new vector; maps a vector to a new size by applying the mapping of the positions
 * and fills the remaining places with 0 values
 *///from  w  w  w  . j  a  v  a2 s .  c om
protected static RealVector padVector(final RealVector vector, int newSize, final int[] mapping) {
    final RealVector newVector = new ArrayRealVector(newSize);

    newVector.walkInOptimizedOrder(new RealVectorChangingVisitor() {

        @Override
        public double visit(int index, double value) {
            if (mapping[index] >= 0) {
                return vector.getEntry(mapping[index]);
            } else {
                return 0;
            }
        }

        @Override
        public void start(int dimension, int start, int end) {
        }

        @Override
        public double end() {
            return 0;
        }
    });
    return newVector;
}

From source file:com.cloudera.oryx.common.math.SparseIteratorTest.java

@Test
public void testEmptyOptimizedSparseIterator() {
    RealVector vector = Vectors.sparse(0);
    DummyVisitor visitor = new DummyVisitor();
    vector.walkInOptimizedOrder(visitor);
    assertTrue(visitor.getSeenValues().isEmpty());
}

From source file:com.cloudera.oryx.common.math.SparseIteratorTest.java

@Test
public void testSingleOptimizedSparseIterator() {
    RealVector vector = Vectors.sparse(1);
    vector.setEntry(0, 3);//w  w  w  .jav  a2  s . co m
    DummyVisitor visitor = new DummyVisitor();
    vector.walkInOptimizedOrder(visitor);
    assertEquals(1, visitor.getSeenValues().size());
    Pair<Integer, Double> first = visitor.getSeenValues().get(0);
    assertEquals(0, first.getFirst().intValue());
    assertEquals(3.0, first.getSecond().doubleValue());
}

From source file:com.cloudera.oryx.common.math.SparseIteratorTest.java

@Test
public void testManyOptimizedSparseIterator() {
    RealVector vector = Vectors.sparse(10, 3);
    vector.setEntry(3, 4.5);// w  w w  .j a v a2  s.c o  m
    vector.setEntry(8, 0.0); // Won't show up
    vector.setEntry(9, 3.0);
    vector.setEntry(2, -7.0);
    DummyVisitor visitor = new DummyVisitor();
    vector.walkInOptimizedOrder(visitor);
    List<Pair<Integer, Double>> seen = visitor.getSeenValues();
    assertEquals(3, seen.size());
    assertEquals(9, seen.get(0).getFirst().intValue());
    assertEquals(3.0, seen.get(0).getSecond().doubleValue());
    assertEquals(2, seen.get(1).getFirst().intValue());
    assertEquals(-7.0, seen.get(1).getSecond().doubleValue());
    assertEquals(3, seen.get(2).getFirst().intValue());
    assertEquals(4.5, seen.get(2).getSecond().doubleValue());
}