Example usage for org.apache.mahout.math Vector isSequentialAccess

List of usage examples for org.apache.mahout.math Vector isSequentialAccess

Introduction

In this page you can find the example usage for org.apache.mahout.math Vector isSequentialAccess.

Prototype

boolean isSequentialAccess();

Source Link

Document

true if this implementation should be considered to be iterable in index order in an efficient way.

Usage

From source file:com.ydy.cf.solver.impl.AlternatingLeastSquaresSolver.java

License:Apache License

private Matrix createRiIiMaybeTransposed(Vector ratingVector) {
    Preconditions.checkArgument(ratingVector.isSequentialAccess());
    Matrix RiIiMaybeTransposed = new DenseMatrix(ratingVector.getNumNondefaultElements(), 1);
    Iterator<Vector.Element> ratingsIterator = ratingVector.iterateNonZero();
    int index = 0;
    while (ratingsIterator.hasNext()) {
        Vector.Element elem = ratingsIterator.next();
        RiIiMaybeTransposed.setQuick(index++, 0, elem.get());
    }//w  w  w.ja v  a  2  s . co  m
    return RiIiMaybeTransposed;
}

From source file:edu.rosehulman.mahout.math.VectorWritable.java

License:Apache License

public static void writeVector(DataOutput out, Vector vector, boolean laxPrecision) throws IOException {
    boolean dense = vector.isDense();
    boolean sequential = vector.isSequentialAccess();
    boolean named = vector instanceof NamedVector;

    out.writeByte((dense ? FLAG_DENSE : 0) | (sequential ? FLAG_SEQUENTIAL : 0) | (named ? FLAG_NAMED : 0)
            | (laxPrecision ? FLAG_LAX_PRECISION : 0));

    Varint.writeUnsignedVarInt(vector.size(), out);
    if (dense) {//from  w w  w .  j ava2s  .  co  m
        for (Vector.Element element : vector.all()) {
            if (laxPrecision) {
                out.writeFloat((float) element.get());
            } else {
                out.writeDouble(element.get());
            }
        }
    } else {
        Varint.writeUnsignedVarInt(vector.getNumNonZeroElements(), out);
        Iterator<Element> iter = vector.nonZeroes().iterator();
        if (sequential) {
            int lastIndex = 0;
            while (iter.hasNext()) {
                Vector.Element element = iter.next();
                if (element.get() == 0) {
                    continue;
                }
                int thisIndex = element.index();
                // Delta-code indices:
                Varint.writeUnsignedVarInt(thisIndex - lastIndex, out);
                lastIndex = thisIndex;
                if (laxPrecision) {
                    out.writeFloat((float) element.get());
                } else {
                    out.writeDouble(element.get());
                }
            }
        } else {
            while (iter.hasNext()) {
                Vector.Element element = iter.next();
                if (element.get() == 0) {
                    // TODO(robinanil): Fix the damn iterator for the zero element.
                    continue;
                }
                Varint.writeUnsignedVarInt(element.index(), out);
                if (laxPrecision) {
                    out.writeFloat((float) element.get());
                } else {
                    out.writeDouble(element.get());
                }
            }
        }
    }
    if (named) {
        String name = ((NamedVector) vector).getName();
        out.writeUTF(name == null ? "" : name);
    }
}