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

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

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

import java.util.Arrays;
import java.util.Iterator;

import edu.jhu.prim.iter.LongIter;
import edu.jhu.prim.map.LongDoubleEntry;
import edu.jhu.prim.sort.LongDoubleSort;
import edu.jhu.prim.util.Lambda.FnLongDoubleToDouble;
import edu.jhu.prim.util.Lambda.FnLongDoubleToVoid;
import edu.jhu.prim.util.SafeCast;
import edu.jhu.prim.util.math.FastMath;

/**
 * Lazily-sorted vector.
 * 
 * @author Travis Wolfe 
 */
public class LongDoubleUnsortedVector extends AbstractLongDoubleVector implements LongDoubleVector, Iterable {

    private static final long serialVersionUID = 1L;

    public static final int defaultSparseInitCapacity = 16;
    
    public boolean printWarnings = true;
    
    protected long[] idx;
    protected double[] vals;
    protected int top;          	// indices less than this are valid
    protected boolean compacted;    // are elements of idx sorted and unique?

    public LongDoubleUnsortedVector() {
        this(defaultSparseInitCapacity);
    }
    
    public LongDoubleUnsortedVector(int initCapacity) {
        idx = new long[initCapacity];
        vals = new double[initCapacity];
        top = 0;
        compacted = true;
    }

    /** Copy constructor. */
    public LongDoubleUnsortedVector(LongDoubleUnsortedVector other) {
        this(other.idx.length);
        for (int i=0; i=top)
        LongDoubleSort.sortIndexAsc(idx, vals, top);

        // let add() remove duplicate entries
        int oldTop = top;
        top = 0;
        for(int i=0; i 0 ? idx[top-1] : -1;
        if(index == prevIdx) {
            //System.out.printf("[add] prevIndex=%d top=%d prevVal=%.2f index=%d value=%.2f\n", prevIdx, top, vals[top-1], index, value);
            vals[top-1] += value;
            if(vals[top-1] == 0)
                top--;
        }
        else {
            //System.out.printf("[add] top=%d index=%d value=%.2f\n" , top, index, value);
            if(top == capacity()) grow();
            idx[top] = index;
            vals[top] = value;
            top++;
            compacted &= (index > prevIdx);
        }
    }

    private void grow() {
        int newSize = (int)(capacity() * 1.3d + 8d);
        idx = Arrays.copyOf(idx, newSize);
        vals = Arrays.copyOf(vals, newSize);
    }

    /* START EXCLUDE ILV norms */
    
    public int l0Norm() {
        compact();
        return top;
    }

    public double l1Norm() {
        compact();
        double sum = 0;
        for(int i=0; i= 0) sum += v;
            else sum -= v;
        }
        return sum;
    }

    public double l2Norm() {
        compact();
        double sum = 0;
        for(int i=0; i 0 && v > biggest)
                biggest = v;
        }
        return biggest >= 0 ? biggest : -biggest;
    }

    public void makeUnitVector() { scale(1 / l2Norm()); }

    /**
     * returns true if any values are NaN or Inf
     */
    public boolean hasBadValues() {
        for(int i=0; i iterator() {
        compact();
        return new LongDoubleIterator();
    }
    
    public void applyNoCompact(FnLongDoubleToDouble function) {
        for(int i=0; i iteratorNoCompact() {
        return new LongDoubleIterator();
    }

    @Override
    public void subtract(LongDoubleVector other) {
        final LongDoubleUnsortedVector me = this;
        other.iterate(new FnLongDoubleToVoid() {
            @Override
            public void call(long idx, double val) {
                me.add(idx, - val);
            }
        });
    }

    @Override
    public void product(LongDoubleVector other) {
        throw new RuntimeException("not supported");
    }
    
    @Override
    public double dot(double[] other) {
        double sum = 0;
        for(int i=0; i oth.top) {
                smaller = oth; bigger = this;
            }
            smaller.compact();
            bigger.compact();
            double dot = 0;
            int j = 0;
            long attempt = bigger.idx[j];
            for(int i=0; i {

        // The current entry.
        private LongDoubleEntryImpl entry = new LongDoubleEntryImpl(-1);

        @Override
        public boolean hasNext() {
            return entry.i + 1 < top;
        }

        @Override
        public LongDoubleEntry next() {
            entry.i++;
            return entry;
        }

        @Override
        public void remove() {
            throw new RuntimeException("operation not supported");
        }

    }
    
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("{");
        for(int i=0; i= 0) {
            return idx[top-1] + 1;
        } else {
            return 0;
        }
    }

    @Override
    public double[] toNativeArray() {
        compact();
        final double[] arr = new double[SafeCast.safeLongToInt(getNumImplicitEntries())];
        iterate(new FnLongDoubleToVoid() {
            @Override
            public void call(long idx, double val) {
                arr[SafeCast.safeLongToInt(idx)] = val;
            }
        });
        return arr;
    }

    /**
     * Gets the INTERNAL representation of the indices. Great care should be
     * taken to avoid touching the values beyond the used indices.
     */
    public long[] getInternalIndices() {
        return idx;
    }

    /**
     * Gets the INTERNAL representation of the values. Great care should be
     * taken to avoid touching the values beyond the used values.
     */
    public double[] getInternalValues() {
        return vals;
    }
    
    /** Gets the INTERNAL number of used entries in this vector. */
    public int getUsed() {
        return top;
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy