FloatVector.java
package org.opentrafficsim.core.value.vfloat.vector;
import java.io.Serializable;
import org.opentrafficsim.core.unit.SICoefficients;
import org.opentrafficsim.core.unit.SIUnit;
import org.opentrafficsim.core.unit.Unit;
import org.opentrafficsim.core.value.Absolute;
import org.opentrafficsim.core.value.AbstractValue;
import org.opentrafficsim.core.value.DenseData;
import org.opentrafficsim.core.value.Format;
import org.opentrafficsim.core.value.Relative;
import org.opentrafficsim.core.value.SparseData;
import org.opentrafficsim.core.value.ValueException;
import org.opentrafficsim.core.value.ValueUtil;
import org.opentrafficsim.core.value.vfloat.scalar.FloatScalar;
import cern.colt.matrix.tfloat.FloatMatrix1D;
import cern.colt.matrix.tfloat.impl.DenseFloatMatrix1D;
import cern.colt.matrix.tfloat.impl.SparseFloatMatrix1D;
/**
* Immutable FloatVector.
* <p>
* This file was generated by the OpenTrafficSim value classes generator, 09 mrt, 2015
* <p>
* Copyright (c) 2014 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
* BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
* <p>
* @version 09 mrt, 2015 <br>
* @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
* @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
* @param <U> Unit; the unit of this FloatVector
*/
public abstract class FloatVector<U extends Unit<U>> extends AbstractValue<U> implements
Serializable,
ReadOnlyFloatVectorFunctions<U>
{
/** */
private static final long serialVersionUID = 20150309L;
/**
* The internal storage for the vector; internally the values are stored in standard SI unit; storage can be dense
* or sparse.
*/
private FloatMatrix1D vectorSI;
/**
* Construct a new Immutable FloatVector.
* @param unit U; the unit of the new FloatVector
*/
protected FloatVector(final U unit)
{
super(unit);
// System.out.println("Created FloatVector");
}
/**
* @param <U> Unit
*/
public abstract static class Abs<U extends Unit<U>> extends FloatVector<U> implements Absolute
{
/** */
private static final long serialVersionUID = 20150309L;
/**
* Construct a new Absolute Immutable FloatVector.
* @param unit U; the unit of the new Absolute Immutable FloatVector
*/
protected Abs(final U unit)
{
super(unit);
// System.out.println("Created Abs");
}
/**
* @param <U> Unit
*/
public static class Dense<U extends Unit<U>> extends Abs<U> implements DenseData
{
/** */
private static final long serialVersionUID = 20150309L;
/**
* Construct a new Absolute Dense Immutable FloatVector.
* @param values float[]; the values of the entries in the new Absolute Dense Immutable FloatVector
* @param unit U; the unit of the new Absolute Dense Immutable FloatVector
* @throws ValueException when values is null
*/
public Dense(final float[] values, final U unit) throws ValueException
{
super(unit);
// System.out.println("Created Dense");
initialize(values);
}
/**
* Construct a new Absolute Dense Immutable FloatVector.
* @param values FloatScalar.Abs<U>[]; the values of the entries in the new Absolute Dense Immutable
* FloatVector
* @throws ValueException when values has zero entries
*/
public Dense(final FloatScalar.Abs<U>[] values) throws ValueException
{
super(checkNonEmpty(values)[0].getUnit());
// System.out.println("Created Dense");
initialize(values);
}
/**
* For package internal use only.
* @param values FloatMatrix1D; the values of the entries in the new Absolute Dense Immutable FloatVector
* @param unit U; the unit of the new Absolute Dense Immutable FloatVector
*/
protected Dense(final FloatMatrix1D values, final U unit)
{
super(unit);
// System.out.println("Created Dense");
initialize(values); // shallow copy
}
/** {@inheritDoc} */
@Override
public final MutableFloatVector.Abs.Dense<U> mutable()
{
return new MutableFloatVector.Abs.Dense<U>(getVectorSI(), getUnit());
}
/** {@inheritDoc} */
@Override
protected final FloatMatrix1D createMatrix1D(final int size)
{
return new DenseFloatMatrix1D(size);
}
/** {@inheritDoc} */
@Override
public final FloatVector.Abs.Dense<U> copy()
{
return this; // That was easy...
}
}
/**
* @param <U> Unit
*/
public static class Sparse<U extends Unit<U>> extends Abs<U> implements SparseData
{
/** */
private static final long serialVersionUID = 20150309L;
/**
* Construct a new Absolute Sparse Immutable FloatVector.
* @param values float[]; the values of the entries in the new Absolute Sparse Immutable FloatVector
* @param unit U; the unit of the new Absolute Sparse Immutable FloatVector
* @throws ValueException when values is null
*/
public Sparse(final float[] values, final U unit) throws ValueException
{
super(unit);
// System.out.println("Created Sparse");
initialize(values);
}
/**
* Construct a new Absolute Sparse Immutable FloatVector.
* @param values FloatScalar.Abs<U>[]; the values of the entries in the new Absolute Sparse Immutable
* FloatVector
* @throws ValueException when values has zero entries
*/
public Sparse(final FloatScalar.Abs<U>[] values) throws ValueException
{
super(checkNonEmpty(values)[0].getUnit());
// System.out.println("Created Sparse");
initialize(values);
}
/**
* For package internal use only.
* @param values FloatMatrix1D; the values of the entries in the new Absolute Sparse Immutable FloatVector
* @param unit U; the unit of the new Absolute Sparse Immutable FloatVector
*/
protected Sparse(final FloatMatrix1D values, final U unit)
{
super(unit);
// System.out.println("Created Sparse");
initialize(values); // shallow copy
}
/** {@inheritDoc} */
@Override
public final MutableFloatVector.Abs.Sparse<U> mutable()
{
return new MutableFloatVector.Abs.Sparse<U>(getVectorSI(), getUnit());
}
/** {@inheritDoc} */
@Override
protected final FloatMatrix1D createMatrix1D(final int size)
{
return new SparseFloatMatrix1D(size);
}
/** {@inheritDoc} */
@Override
public final FloatVector.Abs.Sparse<U> copy()
{
return this; // That was easy...
}
}
/** {@inheritDoc} */
@Override
public final FloatScalar.Abs<U> get(final int index) throws ValueException
{
return new FloatScalar.Abs<U>(getInUnit(index, getUnit()), getUnit());
}
}
/**
* @param <U> Unit
*/
public abstract static class Rel<U extends Unit<U>> extends FloatVector<U> implements Relative
{
/** */
private static final long serialVersionUID = 20150309L;
/**
* Construct a new Relative Immutable FloatVector.
* @param unit U; the unit of the new Relative Immutable FloatVector
*/
protected Rel(final U unit)
{
super(unit);
// System.out.println("Created Rel");
}
/**
* @param <U> Unit
*/
public static class Dense<U extends Unit<U>> extends Rel<U> implements DenseData
{
/** */
private static final long serialVersionUID = 20150309L;
/**
* Construct a new Relative Dense Immutable FloatVector.
* @param values float[]; the values of the entries in the new Relative Dense Immutable FloatVector
* @param unit U; the unit of the new Relative Dense Immutable FloatVector
* @throws ValueException when values is null
*/
public Dense(final float[] values, final U unit) throws ValueException
{
super(unit);
// System.out.println("Created Dense");
initialize(values);
}
/**
* Construct a new Relative Dense Immutable FloatVector.
* @param values FloatScalar.Rel<U>[]; the values of the entries in the new Relative Dense Immutable
* FloatVector
* @throws ValueException when values has zero entries
*/
public Dense(final FloatScalar.Rel<U>[] values) throws ValueException
{
super(checkNonEmpty(values)[0].getUnit());
// System.out.println("Created Dense");
initialize(values);
}
/**
* For package internal use only.
* @param values FloatMatrix1D; the values of the entries in the new Relative Dense Immutable FloatVector
* @param unit U; the unit of the new Relative Dense Immutable FloatVector
*/
protected Dense(final FloatMatrix1D values, final U unit)
{
super(unit);
// System.out.println("Created Dense");
initialize(values); // shallow copy
}
/** {@inheritDoc} */
@Override
public final MutableFloatVector.Rel.Dense<U> mutable()
{
return new MutableFloatVector.Rel.Dense<U>(getVectorSI(), getUnit());
}
/** {@inheritDoc} */
@Override
protected final FloatMatrix1D createMatrix1D(final int size)
{
return new DenseFloatMatrix1D(size);
}
/** {@inheritDoc} */
@Override
public final FloatVector.Rel.Dense<U> copy()
{
return this; // That was easy...
}
}
/**
* @param <U> Unit
*/
public static class Sparse<U extends Unit<U>> extends Rel<U> implements SparseData
{
/** */
private static final long serialVersionUID = 20150309L;
/**
* Construct a new Relative Sparse Immutable FloatVector.
* @param values float[]; the values of the entries in the new Relative Sparse Immutable FloatVector
* @param unit U; the unit of the new Relative Sparse Immutable FloatVector
* @throws ValueException when values is null
*/
public Sparse(final float[] values, final U unit) throws ValueException
{
super(unit);
// System.out.println("Created Sparse");
initialize(values);
}
/**
* Construct a new Relative Sparse Immutable FloatVector.
* @param values FloatScalar.Rel<U>[]; the values of the entries in the new Relative Sparse Immutable
* FloatVector
* @throws ValueException when values has zero entries
*/
public Sparse(final FloatScalar.Rel<U>[] values) throws ValueException
{
super(checkNonEmpty(values)[0].getUnit());
// System.out.println("Created Sparse");
initialize(values);
}
/**
* For package internal use only.
* @param values FloatMatrix1D; the values of the entries in the new Relative Sparse Immutable FloatVector
* @param unit U; the unit of the new Relative Sparse Immutable FloatVector
*/
protected Sparse(final FloatMatrix1D values, final U unit)
{
super(unit);
// System.out.println("Created Sparse");
initialize(values); // shallow copy
}
/** {@inheritDoc} */
@Override
public final MutableFloatVector.Rel.Sparse<U> mutable()
{
return new MutableFloatVector.Rel.Sparse<U>(getVectorSI(), getUnit());
}
/** {@inheritDoc} */
@Override
protected final FloatMatrix1D createMatrix1D(final int size)
{
return new SparseFloatMatrix1D(size);
}
/** {@inheritDoc} */
@Override
public final FloatVector.Rel.Sparse<U> copy()
{
return this; // That was easy...
}
}
/** {@inheritDoc} */
@Override
public final FloatScalar.Rel<U> get(final int index) throws ValueException
{
return new FloatScalar.Rel<U>(getInUnit(index, getUnit()), getUnit());
}
}
/**
* Retrieve the internal data.
* @return FloatMatrix1D; the data in the internal format
*/
protected final FloatMatrix1D getVectorSI()
{
return this.vectorSI;
}
/**
* Make a deep copy of the data (used ONLY in the MutableFloatVector sub class).
*/
protected final void deepCopyData()
{
this.vectorSI = getVectorSI().copy(); // makes a deep copy, using multithreading
}
/**
* Create a mutable version of this FloatVector. <br>
* The mutable version is created with a shallow copy of the data and the internal copyOnWrite flag set. The first
* operation in the mutable version that modifies the data shall trigger a deep copy of the data.
* @return MutableFloatVector<U>; mutable version of this FloatVector
*/
public abstract MutableFloatVector<U> mutable();
/**
* Import the values and convert them into the SI standard unit.
* @param values float[]; an array of values
* @throws ValueException when values is null
*/
protected final void initialize(final float[] values) throws ValueException
{
if (null == values)
{
throw new ValueException("values is null");
}
this.vectorSI = createMatrix1D(values.length);
if (getUnit().equals(getUnit().getStandardUnit()))
{
this.vectorSI.assign(values);
}
else
{
for (int index = values.length; --index >= 0;)
{
safeSet(index, (float) expressAsSIUnit(values[index]));
}
}
}
/**
* Import the values from an existing FloatMatrix1D. This makes a shallow copy.
* @param values FloatMatrix1D; the values
*/
protected final void initialize(final FloatMatrix1D values)
{
this.vectorSI = values;
}
/**
* Construct the vector and store the values in the standard SI unit.
* @param values FloatScalar<U>[]; an array of values
* @throws ValueException when values is null, or empty
*/
protected final void initialize(final FloatScalar<U>[] values) throws ValueException
{
if (null == values)
{
throw new ValueException("values is null");
}
this.vectorSI = createMatrix1D(values.length);
for (int index = 0; index < values.length; index++)
{
safeSet(index, values[index].getSI());
}
}
/**
* Create storage for the data. <br/>
* This method must be implemented by each leaf class.
* @param size int; the number of cells in the vector
* @return FloatMatrix1D; an instance of the right type of FloatMatrix1D (absolute/relative, dense/sparse, etc.)
*/
protected abstract FloatMatrix1D createMatrix1D(final int size);
/**
* Create a float[] array filled with the values in the standard SI unit.
* @return float[]; array of values in the standard SI unit
*/
public final float[] getValuesSI()
{
return this.vectorSI.toArray(); // this makes a deep copy
}
/**
* Create a float[] array filled with the values in the original unit.
* @return float[]; the values in the original unit
*/
public final float[] getValuesInUnit()
{
return getValuesInUnit(getUnit());
}
/**
* Create a float[] array filled with the values converted into a specified unit.
* @param targetUnit U; the unit into which the values are converted for use
* @return float[]; the values converted into the specified unit
*/
public final float[] getValuesInUnit(final U targetUnit)
{
float[] values = this.vectorSI.toArray();
for (int i = values.length; --i >= 0;)
{
values[i] = (float) ValueUtil.expressAsUnit(values[i], targetUnit);
}
return values;
}
/** {@inheritDoc} */
@Override
public final int size()
{
return (int) this.vectorSI.size();
}
/** {@inheritDoc} */
@Override
public final float getSI(final int index) throws ValueException
{
checkIndex(index);
return safeGet(index);
}
/** {@inheritDoc} */
@Override
public final float getInUnit(final int index) throws ValueException
{
return (float) expressAsSpecifiedUnit(getSI(index));
}
/** {@inheritDoc} */
@Override
public final float getInUnit(final int index, final U targetUnit) throws ValueException
{
return (float) ValueUtil.expressAsUnit(getSI(index), targetUnit);
}
/** {@inheritDoc} */
@Override
public final float zSum()
{
return this.vectorSI.zSum();
}
/** {@inheritDoc} */
@Override
public final int cardinality()
{
return this.vectorSI.cardinality();
}
/** {@inheritDoc} */
@Override
public final String toString()
{
return toString(getUnit(), false, true);
}
/**
* Print this FloatVector with the values expressed in the specified unit.
* @param displayUnit U; the unit into which the values are converted for display
* @return String; printable string with the vector contents expressed in the specified unit
*/
public final String toString(final U displayUnit)
{
return toString(displayUnit, false, true);
}
/**
* Print this FloatVector with optional type and unit information.
* @param verbose boolean; if true; include type info; if false; exclude type info
* @param withUnit boolean; if true; include the unit; of false; exclude the unit
* @return String; printable string with the vector contents
*/
public final String toString(final boolean verbose, final boolean withUnit)
{
return toString(getUnit(), verbose, withUnit);
}
/**
* Print this FloatVector with the values expressed in the specified unit.
* @param displayUnit U; the unit into which the values are converted for display
* @param verbose boolean; if true; include type info; if false; exclude type info
* @param withUnit boolean; if true; include the unit; of false; exclude the unit
* @return String; printable string with the vector contents
*/
public final String toString(final U displayUnit, final boolean verbose, final boolean withUnit)
{
StringBuffer buf = new StringBuffer();
if (verbose)
{
if (this instanceof MutableFloatVector)
{
buf.append("Mutable ");
if (this instanceof MutableFloatVector.Abs.Dense)
{
buf.append("Abs Dense ");
}
else if (this instanceof MutableFloatVector.Rel.Dense)
{
buf.append("Rel Dense ");
}
else if (this instanceof MutableFloatVector.Abs.Sparse)
{
buf.append("Abs Sparse ");
}
else if (this instanceof MutableFloatVector.Rel.Sparse)
{
buf.append("Rel Sparse ");
}
else
{
buf.append("??? ");
}
}
else
{
buf.append("Immutable ");
if (this instanceof FloatVector.Abs.Dense)
{
buf.append("Abs Dense ");
}
else if (this instanceof FloatVector.Rel.Dense)
{
buf.append("Rel Dense ");
}
else if (this instanceof FloatVector.Abs.Sparse)
{
buf.append("Abs Sparse ");
}
else if (this instanceof FloatVector.Rel.Sparse)
{
buf.append("Rel Sparse ");
}
else
{
buf.append("??? ");
}
}
}
for (int i = 0; i < size(); i++)
{
float f = (float) ValueUtil.expressAsUnit(safeGet(i), displayUnit);
buf.append(" " + Format.format(f));
}
if (withUnit)
{
buf.append(displayUnit.getAbbreviation());
}
return buf.toString();
}
/**
* Centralized size equality check.
* @param other FloatVector<?>; other FloatVector
* @throws ValueException when other is null, or vectors have unequal size
*/
protected final void checkSize(final FloatVector<?> other) throws ValueException
{
if (null == other)
{
throw new ValueException("other is null");
}
if (size() != other.size())
{
throw new ValueException("The vectors have different sizes: " + size() + " != " + other.size());
}
}
/**
* Centralized size equality check.
* @param other float[]; array of float
* @throws ValueException when vectors have unequal size
*/
protected final void checkSize(final float[] other) throws ValueException
{
if (size() != other.length)
{
throw new ValueException("The vector and the array have different sizes: " + size() + " != " + other.length);
}
}
/**
* Check that a provided index is valid.
* @param index int; the value to check
* @throws ValueException when index is invalid
*/
protected final void checkIndex(final int index) throws ValueException
{
if (index < 0 || index >= size())
{
throw new ValueException("index out of range (valid range is 0.." + (size() - 1) + ", got " + index + ")");
}
}
/**
* Retrieve a value in vectorSI without checking validity of the index.
* @param index int; the index
* @return float; the value stored at that index
*/
protected final float safeGet(final int index)
{
return this.vectorSI.getQuick(index);
}
/**
* Modify a value in vectorSI without checking validity of the index.
* @param index int; the index
* @param valueSI float; the new value for the entry in vectorSI
*/
protected final void safeSet(final int index, final float valueSI)
{
this.vectorSI.setQuick(index, valueSI);
}
/**
* Create a deep copy of the data.
* @return FloatMatrix1D; deep copy of the data
*/
protected final FloatMatrix1D deepCopyOfData()
{
return this.vectorSI.copy();
}
/**
* Check that a provided array can be used to create some descendant of a FloatVector.
* @param fsArray FloatScalar<U>[]; the provided array
* @param <U> Unit; the unit of the FloatScalar array
* @return FloatScalar<U>[]; the provided array
* @throws ValueException when the array has length equal to 0
*/
protected static <U extends Unit<U>> FloatScalar<U>[] checkNonEmpty(final FloatScalar<U>[] fsArray)
throws ValueException
{
if (0 == fsArray.length)
{
throw new ValueException(
"Cannot create a FloatVector or MutableFloatVector from an empty array of FloatScalar");
}
return fsArray;
}
/** {@inheritDoc} */
@Override
public final int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + this.vectorSI.hashCode();
return result;
}
/** {@inheritDoc} */
@Override
public final boolean equals(final Object obj)
{
if (this == obj)
{
return true;
}
if (obj == null)
{
return false;
}
if (!(obj instanceof FloatVector))
{
return false;
}
FloatVector<?> other = (FloatVector<?>) obj;
// unequal if not both Absolute or both Relative
if (this.isAbsolute() != other.isAbsolute() || this.isRelative() != other.isRelative())
{
return false;
}
// unequal if the standard SI units differ
if (!this.getUnit().getStandardUnit().equals(other.getUnit().getStandardUnit()))
{
return false;
}
// Colt's equals also tests the size of the vector
if (!getVectorSI().equals(other.getVectorSI()))
{
return false;
}
return true;
}
/**********************************************************************************/
/********************************* STATIC METHODS *********************************/
/**********************************************************************************/
/**
* Add two FloatVectors value by value and store the result in a new MutableFloatVector.Abs.Dense<U>.
* @param left FloatVector.Abs.Dense<U>; the left operand
* @param right FloatVector.Rel<U>; the right operand
* @param <U> Unit; the unit of the parameters and the result
* @return MutableFloatVector.Abs.Dense<U>
* @throws ValueException when the vectors do not have the same size
*/
public static <U extends Unit<U>> MutableFloatVector.Abs.Dense<U> plus(final FloatVector.Abs.Dense<U> left,
final FloatVector.Rel<U> right) throws ValueException
{
return (MutableFloatVector.Abs.Dense<U>) left.mutable().incrementBy(right);
}
/**
* Add two FloatVectors value by value and store the result in a new MutableFloatVector.Abs.Dense<U>.
* @param left FloatVector.Abs.Sparse<U>; the left operand
* @param right FloatVector.Rel.Dense<U>; the right operand
* @param <U> Unit; the unit of the parameters and the result
* @return MutableFloatVector.Abs.Dense<U>
* @throws ValueException when the vectors do not have the same size
*/
public static <U extends Unit<U>> MutableFloatVector.Abs.Dense<U> plus(final FloatVector.Abs.Sparse<U> left,
final FloatVector.Rel.Dense<U> right) throws ValueException
{
return (MutableFloatVector.Abs.Dense<U>) sparseToDense(left).incrementBy(right);
}
/**
* Add two FloatVectors value by value and store the result in a new MutableFloatVector.Abs.Sparse<U>.
* @param left FloatVector.Abs.Sparse<U>; the left operand
* @param right FloatVector.Rel.Sparse<U>; the right operand
* @param <U> Unit; the unit of the parameters and the result
* @return MutableFloatVector.Abs.Sparse<U>
* @throws ValueException when the vectors do not have the same size
*/
public static <U extends Unit<U>> MutableFloatVector.Abs.Sparse<U> plus(final FloatVector.Abs.Sparse<U> left,
final FloatVector.Rel.Sparse<U> right) throws ValueException
{
return (MutableFloatVector.Abs.Sparse<U>) left.mutable().incrementBy(right);
}
/**
* Add two FloatVectors value by value and store the result in a new MutableFloatVector.Rel.Dense<U>.
* @param left FloatVector.Rel.Dense<U>; the left operand
* @param right FloatVector.Rel<U>; the right operand
* @param <U> Unit; the unit of the parameters and the result
* @return MutableFloatVector.Rel.Dense<U>
* @throws ValueException when the vectors do not have the same size
*/
public static <U extends Unit<U>> MutableFloatVector.Rel.Dense<U> plus(final FloatVector.Rel.Dense<U> left,
final FloatVector.Rel<U> right) throws ValueException
{
return (MutableFloatVector.Rel.Dense<U>) left.mutable().incrementBy(right);
}
/**
* Add two FloatVectors value by value and store the result in a new MutableFloatVector.Rel.Dense<U>.
* @param left FloatVector.Rel.Sparse<U>; the left operand
* @param right FloatVector.Rel.Dense<U>; the right operand
* @param <U> Unit; the unit of the parameters and the result
* @return MutableFloatVector.Rel.Dense<U>
* @throws ValueException when the vectors do not have the same size
*/
public static <U extends Unit<U>> MutableFloatVector.Rel.Dense<U> plus(final FloatVector.Rel.Sparse<U> left,
final FloatVector.Rel.Dense<U> right) throws ValueException
{
return (MutableFloatVector.Rel.Dense<U>) sparseToDense(left).incrementBy(right);
}
/**
* Add two FloatVectors value by value and store the result in a new MutableFloatVector.Rel.Sparse<U>.
* @param left FloatVector.Rel.Sparse<U>; the left operand
* @param right FloatVector.Rel.Sparse<U>; the right operand
* @param <U> Unit; the unit of the parameters and the result
* @return MutableFloatVector.Rel.Sparse<U>
* @throws ValueException when the vectors do not have the same size
*/
public static <U extends Unit<U>> MutableFloatVector.Rel.Sparse<U> plus(final FloatVector.Rel.Sparse<U> left,
final FloatVector.Rel.Sparse<U> right) throws ValueException
{
return (MutableFloatVector.Rel.Sparse<U>) left.mutable().incrementBy(right);
}
/**
* Subtract two FloatVectors value by value and store the result in a new MutableFloatVector.Rel.Dense<U>.
* @param left FloatVector.Abs.Dense<U>; the left operand
* @param right FloatVector.Abs<U>; the right operand
* @param <U> Unit; the unit of the parameters and the result
* @return MutableFloatVector.Rel.Dense<U>
* @throws ValueException when the vectors do not have the same size
*/
public static <U extends Unit<U>> MutableFloatVector.Rel.Dense<U> minus(final FloatVector.Abs.Dense<U> left,
final FloatVector.Abs<U> right) throws ValueException
{
return (MutableFloatVector.Rel.Dense<U>) new MutableFloatVector.Rel.Dense<U>(left.deepCopyOfData(),
left.getUnit()).decrementBy(right);
}
/**
* Subtract two FloatVectors value by value and store the result in a new MutableFloatVector.Rel.Sparse<U>.
* @param left FloatVector.Abs.Sparse<U>; the left operand
* @param right FloatVector.Abs.Sparse<U>; the right operand
* @param <U> Unit; the unit of the parameters and the result
* @return MutableFloatVector.Rel.Sparse<U>
* @throws ValueException when the vectors do not have the same size
*/
public static <U extends Unit<U>> MutableFloatVector.Rel.Sparse<U> minus(final FloatVector.Abs.Sparse<U> left,
final FloatVector.Abs.Sparse<U> right) throws ValueException
{
return (MutableFloatVector.Rel.Sparse<U>) new MutableFloatVector.Rel.Sparse<U>(left.deepCopyOfData(),
left.getUnit()).decrementBy(right);
}
/**
* Subtract two FloatVectors value by value and store the result in a new MutableFloatVector.Rel.Dense<U>.
* @param left FloatVector.Abs.Sparse<U>; the left operand
* @param right FloatVector.Abs.Dense<U>; the right operand
* @param <U> Unit; the unit of the parameters and the result
* @return MutableFloatVector.Rel.Dense<U>
* @throws ValueException when the vectors do not have the same size
*/
public static <U extends Unit<U>> MutableFloatVector.Rel.Dense<U> minus(final FloatVector.Abs.Sparse<U> left,
final FloatVector.Abs.Dense<U> right) throws ValueException
{
return (MutableFloatVector.Rel.Dense<U>) new MutableFloatVector.Rel.Dense<U>(left.deepCopyOfData(),
left.getUnit()).decrementBy(right);
}
/**
* Subtract two FloatVectors value by value and store the result in a new MutableFloatVector.Abs.Dense<U>.
* @param left FloatVector.Abs.Dense<U>; the left operand
* @param right FloatVector.Rel<U>; the right operand
* @param <U> Unit; the unit of the parameters and the result
* @return MutableFloatVector.Abs.Dense<U>
* @throws ValueException when the vectors do not have the same size
*/
public static <U extends Unit<U>> MutableFloatVector.Abs.Dense<U> minus(final FloatVector.Abs.Dense<U> left,
final FloatVector.Rel<U> right) throws ValueException
{
return (MutableFloatVector.Abs.Dense<U>) left.mutable().decrementBy(right);
}
/**
* Subtract two FloatVectors value by value and store the result in a new MutableFloatVector.Abs.Dense<U>.
* @param left FloatVector.Abs.Sparse<U>; the left operand
* @param right FloatVector.Rel.Dense<U>; the right operand
* @param <U> Unit; the unit of the parameters and the result
* @return MutableFloatVector.Abs.Dense<U>
* @throws ValueException when the vectors do not have the same size
*/
public static <U extends Unit<U>> MutableFloatVector.Abs.Dense<U> minus(final FloatVector.Abs.Sparse<U> left,
final FloatVector.Rel.Dense<U> right) throws ValueException
{
return (MutableFloatVector.Abs.Dense<U>) sparseToDense(left).decrementBy(right);
}
/**
* Subtract two FloatVectors value by value and store the result in a new MutableFloatVector.Abs.Sparse<U>.
* @param left FloatVector.Abs.Sparse<U>; the left operand
* @param right FloatVector.Rel.Sparse<U>; the right operand
* @param <U> Unit; the unit of the parameters and the result
* @return MutableFloatVector.Abs.Sparse<U>
* @throws ValueException when the vectors do not have the same size
*/
public static <U extends Unit<U>> MutableFloatVector.Abs.Sparse<U> minus(final FloatVector.Abs.Sparse<U> left,
final FloatVector.Rel.Sparse<U> right) throws ValueException
{
return (MutableFloatVector.Abs.Sparse<U>) left.mutable().decrementBy(right);
}
/**
* Subtract two FloatVectors value by value and store the result in a new MutableFloatVector.Rel.Dense<U>.
* @param left FloatVector.Rel.Dense<U>; the left operand
* @param right FloatVector.Rel<U>; the right operand
* @param <U> Unit; the unit of the parameters and the result
* @return MutableFloatVector.Rel.Dense<U>
* @throws ValueException when the vectors do not have the same size
*/
public static <U extends Unit<U>> MutableFloatVector.Rel.Dense<U> minus(final FloatVector.Rel.Dense<U> left,
final FloatVector.Rel<U> right) throws ValueException
{
return (MutableFloatVector.Rel.Dense<U>) left.mutable().decrementBy(right);
}
/**
* Subtract two FloatVectors value by value and store the result in a new MutableFloatVector.Rel.Dense<U>.
* @param left FloatVector.Rel.Sparse<U>; the left operand
* @param right FloatVector.Rel.Dense<U>; the right operand
* @param <U> Unit; the unit of the parameters and the result
* @return MutableFloatVector.Rel.Dense<U>
* @throws ValueException when the vectors do not have the same size
*/
public static <U extends Unit<U>> MutableFloatVector.Rel.Dense<U> minus(final FloatVector.Rel.Sparse<U> left,
final FloatVector.Rel.Dense<U> right) throws ValueException
{
return (MutableFloatVector.Rel.Dense<U>) sparseToDense(left).decrementBy(right);
}
/**
* Subtract two FloatVectors value by value and store the result in a new MutableFloatVector.Rel.Sparse<U>.
* @param left FloatVector.Rel.Sparse<U>; the left operand
* @param right FloatVector.Rel.Sparse<U>; the right operand
* @param <U> Unit; the unit of the parameters and the result
* @return MutableFloatVector.Rel.Sparse<U>
* @throws ValueException when the vectors do not have the same size
*/
public static <U extends Unit<U>> MutableFloatVector.Rel.Sparse<U> minus(final FloatVector.Rel.Sparse<U> left,
final FloatVector.Rel.Sparse<U> right) throws ValueException
{
return (MutableFloatVector.Rel.Sparse<U>) left.mutable().decrementBy(right);
}
// TODO Decide if you ever need multiply an Absolute with anything
/**
* Multiply two FloatVectors value by value and store the result in a new
* MutableFloatVector.Abs.Dense<SIUnit>.
* @param left FloatVector.Abs.Dense<?>; the left operand
* @param right FloatVector.Abs.Dense<?>; the right operand
* @return MutableFloatVector.Abs.Dense<SIUnit>
* @throws ValueException when the vectors do not have the same size
*/
public static MutableFloatVector.Abs.Dense<SIUnit> times(final FloatVector.Abs.Dense<?> left,
final FloatVector.Abs.Dense<?> right) throws ValueException
{
SIUnit targetUnit =
Unit.lookupOrCreateSIUnitWithSICoefficients(SICoefficients.multiply(left.getUnit().getSICoefficients(),
right.getUnit().getSICoefficients()).toString());
MutableFloatVector.Abs.Dense<SIUnit> work =
new MutableFloatVector.Abs.Dense<SIUnit>(left.deepCopyOfData(), targetUnit);
work.scaleValueByValue(right);
return work;
}
/**
* Multiply two FloatVectors value by value and store the result in a new
* MutableFloatVector.Abs.Sparse<SIUnit>.
* @param left FloatVector.Abs.Dense<?>; the left operand
* @param right FloatVector.Abs.Sparse<?>; the right operand
* @return MutableFloatVector.Abs.Sparse<SIUnit>
* @throws ValueException when the vectors do not have the same size
*/
public static MutableFloatVector.Abs.Sparse<SIUnit> times(final FloatVector.Abs.Dense<?> left,
final FloatVector.Abs.Sparse<?> right) throws ValueException
{
SIUnit targetUnit =
Unit.lookupOrCreateSIUnitWithSICoefficients(SICoefficients.multiply(left.getUnit().getSICoefficients(),
right.getUnit().getSICoefficients()).toString());
MutableFloatVector.Abs.Sparse<SIUnit> work =
new MutableFloatVector.Abs.Sparse<SIUnit>(left.deepCopyOfData(), targetUnit);
work.scaleValueByValue(right);
return work;
}
/**
* Multiply two FloatVectors value by value and store the result in a new
* MutableFloatVector.Abs.Sparse<SIUnit>.
* @param left FloatVector.Abs.Sparse<?>; the left operand
* @param right FloatVector.Abs<?>; the right operand
* @return MutableFloatVector.Abs.Sparse<SIUnit>
* @throws ValueException when the vectors do not have the same size
*/
public static MutableFloatVector.Abs.Sparse<SIUnit> times(final FloatVector.Abs.Sparse<?> left,
final FloatVector.Abs<?> right) throws ValueException
{
SIUnit targetUnit =
Unit.lookupOrCreateSIUnitWithSICoefficients(SICoefficients.multiply(left.getUnit().getSICoefficients(),
right.getUnit().getSICoefficients()).toString());
MutableFloatVector.Abs.Sparse<SIUnit> work =
new MutableFloatVector.Abs.Sparse<SIUnit>(left.deepCopyOfData(), targetUnit);
work.scaleValueByValue(right);
return work;
}
/**
* Multiply two FloatVectors value by value and store the result in a new
* MutableFloatVector.Rel.Dense<SIUnit>.
* @param left FloatVector.Rel.Dense<?>; the left operand
* @param right FloatVector.Rel.Dense<?>; the right operand
* @return MutableFloatVector.Rel.Dense<SIUnit>
* @throws ValueException when the vectors do not have the same size
*/
public static MutableFloatVector.Rel.Dense<SIUnit> times(final FloatVector.Rel.Dense<?> left,
final FloatVector.Rel.Dense<?> right) throws ValueException
{
SIUnit targetUnit =
Unit.lookupOrCreateSIUnitWithSICoefficients(SICoefficients.multiply(left.getUnit().getSICoefficients(),
right.getUnit().getSICoefficients()).toString());
MutableFloatVector.Rel.Dense<SIUnit> work =
new MutableFloatVector.Rel.Dense<SIUnit>(left.deepCopyOfData(), targetUnit);
work.scaleValueByValue(right);
return work;
}
/**
* Multiply two FloatVectors value by value and store the result in a new
* MutableFloatVector.Rel.Sparse<SIUnit>.
* @param left FloatVector.Rel.Dense<?>; the left operand
* @param right FloatVector.Rel.Sparse<?>; the right operand
* @return MutableFloatVector.Rel.Sparse<SIUnit>
* @throws ValueException when the vectors do not have the same size
*/
public static MutableFloatVector.Rel.Sparse<SIUnit> times(final FloatVector.Rel.Dense<?> left,
final FloatVector.Rel.Sparse<?> right) throws ValueException
{
SIUnit targetUnit =
Unit.lookupOrCreateSIUnitWithSICoefficients(SICoefficients.multiply(left.getUnit().getSICoefficients(),
right.getUnit().getSICoefficients()).toString());
MutableFloatVector.Rel.Sparse<SIUnit> work =
new MutableFloatVector.Rel.Sparse<SIUnit>(left.deepCopyOfData(), targetUnit);
work.scaleValueByValue(right);
return work;
}
/**
* Multiply two FloatVectors value by value and store the result in a new
* MutableFloatVector.Rel.Sparse<SIUnit>.
* @param left FloatVector.Rel.Sparse<?>; the left operand
* @param right FloatVector.Rel<?>; the right operand
* @return MutableFloatVector.Rel.Sparse<SIUnit>
* @throws ValueException when the vectors do not have the same size
*/
public static MutableFloatVector.Rel.Sparse<SIUnit> times(final FloatVector.Rel.Sparse<?> left,
final FloatVector.Rel<?> right) throws ValueException
{
SIUnit targetUnit =
Unit.lookupOrCreateSIUnitWithSICoefficients(SICoefficients.multiply(left.getUnit().getSICoefficients(),
right.getUnit().getSICoefficients()).toString());
MutableFloatVector.Rel.Sparse<SIUnit> work =
new MutableFloatVector.Rel.Sparse<SIUnit>(left.deepCopyOfData(), targetUnit);
work.scaleValueByValue(right);
return work;
}
/**
* Multiply the values in a FloatVector and a float array value by value and store the result in a new
* MutableFloatVector.Abs.Dense<U>.
* @param left FloatVector.Abs.Dense<U>; the FloatVector
* @param right float[]; the float array
* @param <U> Unit; the unit of the left parameter and the result
* @return MutableFloatVector.Abs.Dense<U>
* @throws ValueException when the FloatVector and the array do not have the same size
*/
public static <U extends Unit<U>> MutableFloatVector.Abs.Dense<U> times(final FloatVector.Abs.Dense<U> left,
final float[] right) throws ValueException
{
return (MutableFloatVector.Abs.Dense<U>) left.mutable().scaleValueByValue(right);
}
/**
* Multiply the values in a FloatVector and a float array value by value and store the result in a new
* MutableFloatVector.Abs.Sparse<U>.
* @param left FloatVector.Abs.Sparse<U>; the FloatVector
* @param right float[]; the float array
* @param <U> Unit; the unit of the left parameter and the result
* @return MutableFloatVector.Abs.Sparse<U>
* @throws ValueException when the FloatVector and the array do not have the same size
*/
public static <U extends Unit<U>> MutableFloatVector.Abs.Sparse<U> times(final FloatVector.Abs.Sparse<U> left,
final float[] right) throws ValueException
{
return (MutableFloatVector.Abs.Sparse<U>) left.mutable().scaleValueByValue(right);
}
/**
* Multiply the values in a FloatVector and a float array value by value and store the result in a new
* MutableFloatVector.Rel.Dense<U>.
* @param left FloatVector.Rel.Dense<U>; the FloatVector
* @param right float[]; the float array
* @param <U> Unit; the unit of the left parameter and the result
* @return MutableFloatVector.Rel.Dense<U>
* @throws ValueException when the FloatVector and the array do not have the same size
*/
public static <U extends Unit<U>> MutableFloatVector.Rel.Dense<U> times(final FloatVector.Rel.Dense<U> left,
final float[] right) throws ValueException
{
return (MutableFloatVector.Rel.Dense<U>) left.mutable().scaleValueByValue(right);
}
/**
* Multiply the values in a FloatVector and a float array value by value and store the result in a new
* MutableFloatVector.Rel.Sparse<U>.
* @param left FloatVector.Rel.Sparse<U>; the FloatVector
* @param right float[]; the float array
* @param <U> Unit; the unit of the left parameter and the result
* @return MutableFloatVector.Rel.Sparse<U>
* @throws ValueException when the FloatVector and the array do not have the same size
*/
public static <U extends Unit<U>> MutableFloatVector.Rel.Sparse<U> times(final FloatVector.Rel.Sparse<U> left,
final float[] right) throws ValueException
{
return (MutableFloatVector.Rel.Sparse<U>) left.mutable().scaleValueByValue(right);
}
/**
* Make the Sparse equivalent of a DenseFloatMatrix1D.
* @param dense FloatMatrix1D; the Dense FloatMatrix1D
* @return SparseFloatMatrix1D
*/
private static SparseFloatMatrix1D makeSparse(final FloatMatrix1D dense)
{
SparseFloatMatrix1D result = new SparseFloatMatrix1D((int) dense.size());
result.assign(dense);
return result;
}
/**
* Create a Sparse version of a Dense FloatVector.
* @param in FloatVector.Abs.Dense<U>; the Dense FloatVector
* @param <U> Unit; the unit of the parameter and the result
* @return MutableFloatVector.Abs.Sparse<U>
*/
public static <U extends Unit<U>> MutableFloatVector.Abs.Sparse<U> denseToSparse(final FloatVector.Abs.Dense<U> in)
{
return new MutableFloatVector.Abs.Sparse<U>(makeSparse(in.getVectorSI()), in.getUnit());
}
/**
* Create a Sparse version of a Dense FloatVector.
* @param in FloatVector.Rel.Dense<U>; the Dense FloatVector
* @param <U> Unit; the unit of the parameter and the result
* @return MutableFloatVector.Rel.Sparse<U>
*/
public static <U extends Unit<U>> MutableFloatVector.Rel.Sparse<U> denseToSparse(final FloatVector.Rel.Dense<U> in)
{
return new MutableFloatVector.Rel.Sparse<U>(makeSparse(in.getVectorSI()), in.getUnit());
}
/**
* Make the Dense equivalent of a SparseFloatMatrix1D.
* @param sparse FloatMatrix1D; the Sparse FloatMatrix1D
* @return DenseFloatMatrix1D
*/
private static DenseFloatMatrix1D makeDense(final FloatMatrix1D sparse)
{
DenseFloatMatrix1D result = new DenseFloatMatrix1D((int) sparse.size());
result.assign(sparse);
return result;
}
/**
* Create a Dense version of a Sparse FloatVector.
* @param in FloatVector.Abs.Sparse<U>; the Sparse FloatVector
* @param <U> Unit; the unit of the parameter and the result
* @return MutableFloatVector.Abs.Dense<U>
*/
public static <U extends Unit<U>> MutableFloatVector.Abs.Dense<U> sparseToDense(final FloatVector.Abs.Sparse<U> in)
{
return new MutableFloatVector.Abs.Dense<U>(makeDense(in.getVectorSI()), in.getUnit());
}
/**
* Create a Dense version of a Sparse FloatVector.
* @param in FloatVector.Rel.Sparse<U>; the Sparse FloatVector
* @param <U> Unit; the unit of the parameter and the result
* @return MutableFloatVector.Rel.Dense<U>
*/
public static <U extends Unit<U>> MutableFloatVector.Rel.Dense<U> sparseToDense(final FloatVector.Rel.Sparse<U> in)
{
return new MutableFloatVector.Rel.Dense<U>(makeDense(in.getVectorSI()), in.getUnit());
}
/**
* Interpolate between or extrapolate over two values.
* @param zero FloatVector.Abs.Dense<U>; zero reference (returned when ratio == 0)
* @param one FloatVector.Abs.Dense<U>; one reference (returned when ratio == 1)
* @param ratio float; the ratio that determines where between (or outside) zero and one the result lies
* @param <U> Unit; the unit of the parameters and the result
* @return MutableFloatVector.Abs.Dense<U>
* @throws ValueException when zero and one do not have the same size
*/
public static <U extends Unit<U>> MutableFloatVector.Abs.Dense<U> interpolate(final FloatVector.Abs.Dense<U> zero,
final FloatVector.Abs.Dense<U> one, final float ratio) throws ValueException
{
MutableFloatVector.Abs.Dense<U> result = zero.mutable();
for (int index = result.size(); --index >= 0;)
{
result.setSI(index, result.getSI(index) * (1 - ratio) + one.getSI(index) * ratio);
}
return result;
}
/**
* Interpolate between or extrapolate over two values.
* @param zero FloatVector.Rel.Dense<U>; zero reference (returned when ratio == 0)
* @param one FloatVector.Rel.Dense<U>; one reference (returned when ratio == 1)
* @param ratio float; the ratio that determines where between (or outside) zero and one the result lies
* @param <U> Unit; the unit of the parameters and the result
* @return MutableFloatVector.Rel.Dense<U>
* @throws ValueException when zero and one do not have the same size
*/
public static <U extends Unit<U>> MutableFloatVector.Rel.Dense<U> interpolate(final FloatVector.Rel.Dense<U> zero,
final FloatVector.Rel.Dense<U> one, final float ratio) throws ValueException
{
MutableFloatVector.Rel.Dense<U> result = zero.mutable();
for (int index = result.size(); --index >= 0;)
{
result.setSI(index, result.getSI(index) * (1 - ratio) + one.getSI(index) * ratio);
}
return result;
}
/**
* Interpolate between or extrapolate over two values.
* @param zero FloatVector.Abs.Sparse<U>; zero reference (returned when ratio == 0)
* @param one FloatVector.Abs.Sparse<U>; one reference (returned when ratio == 1)
* @param ratio float; the ratio that determines where between (or outside) zero and one the result lies
* @param <U> Unit; the unit of the parameters and the result
* @return MutableFloatVector.Abs.Sparse<U>
* @throws ValueException when zero and one do not have the same size
*/
public static <U extends Unit<U>> MutableFloatVector.Abs.Sparse<U> interpolate(final FloatVector.Abs.Sparse<U> zero,
final FloatVector.Abs.Sparse<U> one, final float ratio) throws ValueException
{
MutableFloatVector.Abs.Sparse<U> result = zero.mutable();
for (int index = result.size(); --index >= 0;)
{
result.setSI(index, result.getSI(index) * (1 - ratio) + one.getSI(index) * ratio);
}
return result;
}
/**
* Interpolate between or extrapolate over two values.
* @param zero FloatVector.Rel.Sparse<U>; zero reference (returned when ratio == 0)
* @param one FloatVector.Rel.Sparse<U>; one reference (returned when ratio == 1)
* @param ratio float; the ratio that determines where between (or outside) zero and one the result lies
* @param <U> Unit; the unit of the parameters and the result
* @return MutableFloatVector.Rel.Sparse<U>
* @throws ValueException when zero and one do not have the same size
*/
public static <U extends Unit<U>> MutableFloatVector.Rel.Sparse<U> interpolate(final FloatVector.Rel.Sparse<U> zero,
final FloatVector.Rel.Sparse<U> one, final float ratio) throws ValueException
{
MutableFloatVector.Rel.Sparse<U> result = zero.mutable();
for (int index = result.size(); --index >= 0;)
{
result.setSI(index, result.getSI(index) * (1 - ratio) + one.getSI(index) * ratio);
}
return result;
}
}