All Downloads are FREE. Search and download functionalities are using the official Maven repository.

edu.jhu.prim.vector.IntDoubleDenseVector Maven / Gradle / Ivy

The newest version!
package edu.jhu.prim.vector;

import java.util.Arrays;

import edu.jhu.prim.Primitives;
import edu.jhu.prim.arrays.DoubleArrays;
import edu.jhu.prim.util.Lambda;
import edu.jhu.prim.util.Lambda.FnIntDoubleToDouble;
import edu.jhu.prim.util.Lambda.FnIntDoubleToVoid;
import edu.jhu.prim.util.SafeCast;
import edu.jhu.prim.vector.IntDoubleHashVector.SparseBinaryOpApplier;

//TODO: Implement Iterable.
public class IntDoubleDenseVector extends AbstractIntDoubleVector implements IntDoubleVector {

    private static final long serialVersionUID = 1L;

    /** The value given to non-explicit entries in the vector. */
    private static final double missingEntries = 0;
    /** The internal array representing this vector. */
    private double[] elements;
    /** The index after the last explicit entry in the vector. */
    private int idxAfterLast;

    public IntDoubleDenseVector() {
        this(8);
    }
    
    public IntDoubleDenseVector(int initialCapacity) {
        elements = new double[initialCapacity];
        idxAfterLast = 0;
    }

    public IntDoubleDenseVector(double[] elements) {
        this.elements = elements;
        idxAfterLast = elements.length;
    }
    
    /** Gets a deep copy of this vector. */
    public IntDoubleDenseVector(IntDoubleDenseVector other) {
        this.elements = DoubleArrays.copyOf(other.elements);
        this.idxAfterLast = other.idxAfterLast;
    }

    /** Copy constructor. */
    public IntDoubleDenseVector(IntDoubleVector other) {
        // TODO: Exploit the number of non-zero entries in other.
        this();
        final IntDoubleDenseVector thisVec = this; 
        other.iterate(new FnIntDoubleToVoid() {
            @Override
            public void call(int idx, double val) {
                thisVec.set(idx, val);
            }
        });
    }
    
    /** Copy factory method. */
    @Override
    public IntDoubleVector copy() {
        return new IntDoubleDenseVector(this);
    }
    
    /**
     * Gets the i'th entry in the vector.
     * @param i The index of the element to get.
     * @return The value of the element to get.
     */
    public double get(int i) {
        if (i < 0 || i >= idxAfterLast) {
            return missingEntries;
        }
        return elements[i];
    }
    
    /**
     * Sets the i'th entry of the vector to the given value.
     * @param i The index to set.
     * @param value The value to set.
     */
    public double set(int idx, double value) {
        int i = idx;
        idxAfterLast = Math.max(idxAfterLast, i + 1);
        ensureCapacity(idxAfterLast);
        double old = elements[i];
        elements[i] = value;
        return old;
    }

    public void add(int idx, double value) {
        int i = idx;
        idxAfterLast = Math.max(idxAfterLast, i + 1);
        ensureCapacity(idxAfterLast);
        elements[i] += value;
    }

    public void scale(double multiplier) {
        for (int i=0; i=
     * this.getNumImplicitEntries(), this.get(j) will return 0.
     * 
     * @return The number of implicit entries.
     */
    public int getNumImplicitEntries() {
        return idxAfterLast;
    }
    
    /**
     * Trims the internal array to exactly the size of the list.
     */
    public void trimToSize() {
        if (idxAfterLast != elements.length) { 
            elements = Arrays.copyOf(elements, idxAfterLast);
        }
    }

    /**
     * Ensure that the internal array has space to contain the specified number of elements.
     * @param size The number of elements. 
     */
    private void ensureCapacity(int size) {
        elements = ensureCapacity(elements, size);
    }
    
    /**
     * Ensure that the array has space to contain the specified number of elements.
     * @param elements The array.
     * @param size The number of elements. 
     */
    public static double[] ensureCapacity(double[] elements, int size) {
        if (size > elements.length) {
            double[] tmp = new double[size*2];
            System.arraycopy(elements, 0, tmp, 0, elements.length);
            elements = tmp;
        }
        return elements;
    }
    
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("IntDoubleDenseVector [");
        for (int i=0; i maxAbs) {
                maxAbs = abs;
            }
        }
        return maxAbs;
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy