MutableFloatVector.java

package org.djunits.value.vfloat.vector;

import java.util.List;
import java.util.SortedMap;

import org.djunits.unit.Unit;
import org.djunits.value.Absolute;
import org.djunits.value.FunctionsAbs;
import org.djunits.value.FunctionsRel;
import org.djunits.value.MathFunctionsAbs;
import org.djunits.value.MathFunctionsRel;
import org.djunits.value.Relative;
import org.djunits.value.StorageType;
import org.djunits.value.ValueException;
import org.djunits.value.ValueUtil;
import org.djunits.value.vfloat.FloatFunction;
import org.djunits.value.vfloat.FloatMathFunctions;
import org.djunits.value.vfloat.scalar.FloatScalar;

/**
 * MutableFloatVector.
 * <p>
 * This file was generated by the djunits value classes generator, 26 jun, 2015
 * <p>
 * Copyright (c) 2015-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
 * BSD-style license. See <a href="http://djunits.org/docs/license.html">DJUNITS License</a>.
 * <p>
 * $LastChangedDate: 2016-05-28 14:25:52 +0200 (Sat, 28 May 2016) $, @version $Revision: 202 $, by $Author: averbraeck $,
 * initial version 30 Oct, 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 MutableFloatVector
 */
public abstract class MutableFloatVector<U extends Unit<U>> extends FloatVector<U> implements MutableFloatVectorInterface<U>
{
    /**  */
    private static final long serialVersionUID = 20151003L;

    /**
     * Construct a new MutableFloatVector.
     * @param unit U; the unit of the new MutableFloatVector
     */
    protected MutableFloatVector(final U unit)
    {
        super(unit);
    }

    /** If set, any modification of the data must be preceded by replacing the data with a local copy. */
    private boolean copyOnWrite = false;

    /**
     * Retrieve the value of the copyOnWrite flag.
     * @return boolean
     */
    private boolean isCopyOnWrite()
    {
        return this.copyOnWrite;
    }

    /**
     * Change the copyOnWrite flag.
     * @param copyOnWrite boolean; the new value for the copyOnWrite flag
     */
    final void setCopyOnWrite(final boolean copyOnWrite)
    {
        this.copyOnWrite = copyOnWrite;
    }

    /** ============================================================================================ */
    /** ================================= ABSOLUTE IMPLEMENTATION ================================== */
    /** ============================================================================================ */

    /**
     * ABSOLUTE implementation of MutableFloatVector.
     * @param <U> Unit the unit for which this Vector will be created
     */
    public static class Abs<U extends Unit<U>> extends MutableFloatVector<U> implements Absolute,
            MathFunctionsAbs<MutableFloatVector.Abs<U>>, FunctionsAbs<U, FloatVector.Abs<U>, FloatVector.Rel<U>>,
            FloatMathFunctions<MutableFloatVector.Abs<U>>
    {
        /**  */
        private static final long serialVersionUID = 20151003L;

        /**
         * Construct a new Absolute Mutable FloatVector.
         * @param values float[]; the values of the entries in the new Absolute Mutable FloatVector
         * @param unit U; the unit of the new Absolute Mutable FloatVector
         * @param storageType the data type to use (e.g., DENSE or SPARSE)
         * @throws ValueException when values is null
         */
        public Abs(final float[] values, final U unit, final StorageType storageType) throws ValueException
        {
            super(unit);
            this.data = FloatVectorData.instantiate(values, unit.getScale(), storageType);
        }

        /**
         * Construct a new Absolute Mutable FloatVector.
         * @param values List; the values of the entries in the new Absolute Mutable FloatVector
         * @param unit U; the unit of the new Absolute Mutable FloatVector
         * @param storageType the data type to use (e.g., DENSE or SPARSE)
         * @throws ValueException when values is null
         */
        public Abs(final List<Float> values, final U unit, final StorageType storageType) throws ValueException
        {
            super(unit);
            this.data = FloatVectorData.instantiate(values, unit.getScale(), storageType);
        }

        /**
         * Construct a new Absolute Mutable FloatVector.
         * @param values FloatScalar.Abs&lt;U&gt;[]; the values of the entries in the new Absolute Mutable FloatVector
         * @param storageType the data type to use (e.g., DENSE or SPARSE)
         * @throws ValueException when values has zero entries
         */
        public Abs(final FloatScalar.Abs<U>[] values, final StorageType storageType) throws ValueException
        {
            super(checkUnit(values));
            this.data = FloatVectorData.instantiate(values, storageType);
        }

        /**
         * Construct a new Absolute Mutable FloatVector.
         * @param values List; the values of the entries in the new Absolute Mutable FloatVector
         * @param storageType the data type to use (e.g., DENSE or SPARSE)
         * @param <S> the Scalar type used
         * @throws ValueException when values has zero entries
         */
        public <S extends FloatScalar.Abs<U>> Abs(final List<S> values, final StorageType storageType) throws ValueException
        {
            super(checkUnit(values));
            this.data = FloatVectorData.instantiateLD(values, storageType);
        }

        /**
         * Construct a new Relative Mutable FloatVector.
         * @param values FloatScalar.Rel&lt;U&gt;[]; the values of the entries in the new Relative Sparse Mutable FloatVector
         * @param length the size of the vector
         * @param storageType the data type to use (e.g., DENSE or SPARSE)
         * @param <S> the Scalar type used
         * @throws ValueException when values has zero entries
         */
        public <S extends FloatScalar.Abs<U>> Abs(final SortedMap<Integer, S> values, final int length,
                final StorageType storageType) throws ValueException
        {
            super(checkUnit(values));
            this.data = FloatVectorData.instantiateMD(values, length, storageType);
        }

        /**
         * Construct a new Relative Mutable FloatVector.
         * @param values Map; the map of indexes to values of the Relative Sparse Mutable FloatVector
         * @param unit U; the unit of the new Relative Sparse Mutable FloatVector
         * @param length the size of the vector
         * @param storageType the data type to use (e.g., DENSE or SPARSE)
         * @throws ValueException when values is null
         */
        public Abs(final SortedMap<Integer, Float> values, final U unit, final int length, final StorageType storageType)
                throws ValueException
        {
            super(unit);
            this.data = FloatVectorData.instantiate(values, length, unit.getScale(), storageType);
        }

        /**
         * Construct a new Absolute Mutable FloatVector.
         * @param data an internal data object
         * @param unit the unit
         */
        Abs(final FloatVectorData data, final U unit)
        {
            super(unit);
            this.data = data.copy();
        }

        /** {@inheritDoc} */
        @Override
        @SuppressWarnings("designforextension")
        public FloatVector.Abs<U> immutable()
        {
            setCopyOnWrite(true);
            return instantiateAbs(getData(), getUnit());
        }

        /** {@inheritDoc} */
        @Override
        @SuppressWarnings("designforextension")
        public MutableFloatVector.Abs<U> mutable()
        {
            setCopyOnWrite(true);
            final MutableFloatVector.Abs<U> result = MutableFloatVector.instantiateMutableAbs(getData(), getUnit());
            result.setCopyOnWrite(true);
            return result;
        }

        /** {@inheritDoc} */
        @Override
        @SuppressWarnings("checkstyle:designforextension")
        public MutableFloatVector.Abs<U> copy()
        {
            return mutable();
        }

        /** {@inheritDoc} */
        @Override
        @SuppressWarnings("designforextension")
        public MutableFloatVector.Abs<U> toDense()
        {
            return this.data.isDense() ? this : instantiateMutableAbs(this.data.toDense(), getUnit());
        }

        /** {@inheritDoc} */
        @Override
        @SuppressWarnings("designforextension")
        public MutableFloatVector.Abs<U> toSparse()
        {
            return this.data.isSparse() ? this : instantiateMutableAbs(this.data.toSparse(), getUnit());
        }

        /** {@inheritDoc} */
        @Override
        @SuppressWarnings("checkstyle:designforextension")
        public FloatScalar.Abs<U> get(final int index) throws ValueException
        {
            return new FloatScalar.Abs<U>(getInUnit(index, getUnit()), getUnit());
        }

        /**
         * Increment the value by the supplied value and return the changed vector.
         * @param increment FloatVector.Rel&lt;U&gt;; amount by which the value is incremented
         * @return the changed MutableFloatVector.Abs&lt;U&gt;
         * @throws ValueException when the size of increment is not identical to the size of this
         */
        public final MutableFloatVector.Abs<U> incrementBy(final FloatVector.Rel<U> increment) throws ValueException
        {
            checkCopyOnWrite();
            this.data.incrementBy(increment.getData());
            return this;
        }

        /**
         * Increment the value by the supplied value and return the changed vector.
         * @param increment FloatScalar.Rel&lt;U&gt;; amount by which the value is incremented
         * @return the changed MutableFloatVector.Abs&lt;U&gt;
         */
        public final MutableFloatVector.Abs<U> incrementBy(final FloatScalar.Rel<U> increment)
        {
            return incrementBy(increment.si);
        }

        /**
         * Increment the value by the supplied constant and return the changed vector.
         * @param increment amount by which the value is incremented
         * @return the changed MutableFloatVector.Abs&lt;U&gt;
         */
        public final MutableFloatVector.Abs<U> incrementBy(final float increment)
        {
            checkCopyOnWrite();
            this.data.incrementBy(increment);
            return this;
        }

        /**
         * Decrement the value by the supplied value and return the changed vector.
         * @param decrement FloatVector.Rel&lt;U&gt;; amount by which the value is decremented
         * @return the changed MutableFloatVector.Abs&lt;U&gt;
         * @throws ValueException when the size of increment is not identical to the size of this
         */
        public final MutableFloatVector.Abs<U> decrementBy(final FloatVector.Rel<U> decrement) throws ValueException
        {
            checkCopyOnWrite();
            this.data.decrementBy(decrement.getData());
            return this;
        }

        /**
         * Decrement the value by the supplied value and return the changed vector.
         * @param decrement FloatScalar.Rel&lt;U&gt;; amount by which the value is decremented
         * @return the changed MutableFloatVector.Abs&lt;U&gt;
         */
        public final MutableFloatVector.Abs<U> decrementBy(final FloatScalar.Rel<U> decrement)
        {
            return decrementBy(decrement.si);
        }

        /**
         * Decrement the value by the supplied constant and return the changed vector.
         * @param decrement amount by which the value is decremented
         * @return the changed MutableFloatVector.Abs&lt;U&gt;
         */
        public final MutableFloatVector.Abs<U> decrementBy(final float decrement)
        {
            checkCopyOnWrite();
            this.data.decrementBy(decrement);
            return this;
        }

        /**
         * Multiply the values in the vector by the supplied values and return the changed vector.
         * @param factors FloatVector.Rel&lt;U&gt;; amounts by which the value is multiplied
         * @return the changed MutableFloatVector.Abs&lt;U&gt;
         * @throws ValueException when the size of the factors is not identical to the size of this
         */
        public final MutableFloatVector.Abs<U> multiplyBy(final FloatVector.Rel<U> factors) throws ValueException
        {
            checkCopyOnWrite();
            this.data.multiplyBy(factors.getData());
            return this;
        }

        /**
         * Multiply the values in the vector by the supplied value and return the changed vector.
         * @param factor FloatScalar.Rel&lt;U&gt;; amount by which the values in the vector are multiplied
         * @return the changed MutableFloatVector.Abs&lt;U&gt;
         */
        public final MutableFloatVector.Abs<U> multiplyBy(final FloatScalar.Rel<U> factor)
        {
            return multiplyBy(factor.si);
        }

        /** {@inheritDoc} */
        @Override
        @SuppressWarnings("checkstyle:designforextension")
        public MutableFloatVector.Abs<U> multiplyBy(final float factor)
        {
            checkCopyOnWrite();
            this.data.multiplyBy(factor);
            return this;
        }

        /**
         * Divide the values in the vector by the supplied values and return the changed vector.
         * @param factors FloatVector.Rel&lt;U&gt;; amounts by which the value is divided
         * @return the changed MutableFloatVector.Abs&lt;U&gt;
         * @throws ValueException when the size of the factors is not identical to the size of this
         */
        public final MutableFloatVector.Abs<U> divideBy(final FloatVector.Rel<U> factors) throws ValueException
        {
            checkCopyOnWrite();
            this.data.divideBy(factors.getData());
            return this;
        }

        /** {@inheritDoc} */
        @Override
        @SuppressWarnings("checkstyle:designforextension")
        public MutableFloatVector.Abs<U> divideBy(final float factor)
        {
            this.data.divideBy(factor);
            return this;
        }

        /**
         * Divide the values in the vector by the supplied value and return the changed vector.
         * @param factor FloatScalar.Rel&lt;U&gt;; amount by which the values in the vector are divided
         * @return the changed MutableFloatVector.Abs&lt;U&gt;
         */
        public final MutableFloatVector.Abs<U> divideBy(final FloatScalar.Rel<U> factor)
        {
            return divideBy(factor.si);
        }

        /** {@inheritDoc} */
        @Override
        @SuppressWarnings("designforextension")
        public FloatVector.Abs<U> plus(final FloatVector.Rel<U> rel) throws ValueException
        {
            return instantiateAbs(this.getData().plus(rel.getData()), getUnit());
        }

        /** {@inheritDoc} */
        @Override
        @SuppressWarnings("designforextension")
        public FloatVector.Abs<U> minus(final FloatVector.Rel<U> rel) throws ValueException
        {
            return instantiateAbs(this.getData().minus(rel.getData()), getUnit());
        }

        /** {@inheritDoc} */
        @Override
        @SuppressWarnings("designforextension")
        public FloatVector.Rel<U> minus(final FloatVector.Abs<U> abs) throws ValueException
        {
            return instantiateRel(this.getData().minus(abs.getData()), getUnit());
        }

        /**********************************************************************************/
        /********************************** MATH METHODS **********************************/
        /**********************************************************************************/

        /** {@inheritDoc} */
        @Override
        @SuppressWarnings("checkstyle:designforextension")
        public MutableFloatVector.Abs<U> ceil()
        {
            assign(FloatMathFunctions.CEIL);
            return this;
        }

        /** {@inheritDoc} */
        @Override
        @SuppressWarnings("checkstyle:designforextension")
        public MutableFloatVector.Abs<U> floor()
        {
            assign(FloatMathFunctions.FLOOR);
            return this;
        }

        /** {@inheritDoc} */
        @Override
        @SuppressWarnings("checkstyle:designforextension")
        public MutableFloatVector.Abs<U> rint()
        {
            assign(FloatMathFunctions.RINT);
            return this;
        }

        /** {@inheritDoc} */
        @Override
        @SuppressWarnings("checkstyle:designforextension")
        public MutableFloatVector.Abs<U> round()
        {
            assign(FloatMathFunctions.ROUND);
            return this;
        }

    }

    /** ============================================================================================ */
    /** ================================= RELATIVE IMPLEMENTATION ================================== */
    /** ============================================================================================ */

    /**
     * RELATIVE implementation of MutableFloatVector.
     * @param <U> Unit the unit for which this Vector will be created
     */
    public static class Rel<U extends Unit<U>> extends MutableFloatVector<U> implements Relative,
            MathFunctionsRel<MutableFloatVector.Rel<U>>, FunctionsRel<U, FloatVector.Abs<U>, FloatVector.Rel<U>>,
            FloatMathFunctions<MutableFloatVector.Rel<U>>
    {
        /**  */
        private static final long serialVersionUID = 20151003L;

        /**
         * Construct a new Relative Mutable FloatVector.
         * @param values float[]; the values of the entries in the new Relative Mutable FloatVector
         * @param unit U; the unit of the new Relative Mutable FloatVector
         * @param storageType the data type to use (e.g., DENSE or SPARSE)
         * @throws ValueException when values is null
         */
        public Rel(final float[] values, final U unit, final StorageType storageType) throws ValueException
        {
            super(unit);
            this.data = FloatVectorData.instantiate(values, unit.getScale(), storageType);
        }

        /**
         * Construct a new Relative Mutable FloatVector.
         * @param values List; the values of the entries in the new Relative Mutable FloatVector
         * @param unit U; the unit of the new Relative Mutable FloatVector
         * @param storageType the data type to use (e.g., DENSE or SPARSE)
         * @throws ValueException when values is null
         */
        public Rel(final List<Float> values, final U unit, final StorageType storageType) throws ValueException
        {
            super(unit);
            this.data = FloatVectorData.instantiate(values, unit.getScale(), storageType);
        }

        /**
         * Construct a new Relative Mutable FloatVector.
         * @param values FloatScalar.Rel&lt;U&gt;[]; the values of the entries in the new Relative Mutable FloatVector
         * @param storageType the data type to use (e.g., DENSE or SPARSE)
         * @throws ValueException when values has zero entries
         */
        public Rel(final FloatScalar.Rel<U>[] values, final StorageType storageType) throws ValueException
        {
            super(checkUnit(values));
            this.data = FloatVectorData.instantiate(values, storageType);
        }

        /**
         * Construct a new Relative Mutable FloatVector.
         * @param values List; the values of the entries in the new Relative Mutable FloatVector
         * @param storageType the data type to use (e.g., DENSE or SPARSE)
         * @param <S> the Scalar type used
         * @throws ValueException when values has zero entries
         */
        public <S extends FloatScalar.Rel<U>> Rel(final List<S> values, final StorageType storageType) throws ValueException
        {
            super(checkUnit(values));
            this.data = FloatVectorData.instantiateLD(values, storageType);
        }

        /**
         * Construct a new Relative Mutable FloatVector.
         * @param values FloatScalar.Rel&lt;U&gt;[]; the values of the entries in the new Relative Sparse Mutable FloatVector
         * @param length the size of the vector
         * @param storageType the data type to use (e.g., DENSE or SPARSE)
         * @param <S> the Scalar type used
         * @throws ValueException when values has zero entries
         */
        public <S extends FloatScalar.Rel<U>> Rel(final SortedMap<Integer, S> values, final int length,
                final StorageType storageType) throws ValueException
        {
            super(checkUnit(values));
            this.data = FloatVectorData.instantiateMD(values, length, storageType);
        }

        /**
         * Construct a new Relative Mutable FloatVector.
         * @param values Map; the map of indexes to values of the Relative Sparse Mutable FloatVector
         * @param unit U; the unit of the new Relative Sparse Mutable FloatVector
         * @param length the size of the vector
         * @param storageType the data type to use (e.g., DENSE or SPARSE)
         * @throws ValueException when values is null
         */
        public Rel(final SortedMap<Integer, Float> values, final U unit, final int length, final StorageType storageType)
                throws ValueException
        {
            super(unit);
            this.data = FloatVectorData.instantiate(values, length, unit.getScale(), storageType);
        }

        /**
         * Construct a new Relative Mutable FloatVector.
         * @param data an internal data object
         * @param unit the unit
         */
        Rel(final FloatVectorData data, final U unit)
        {
            super(unit);
            this.data = data.copy();
        }

        /** {@inheritDoc} */
        @Override
        @SuppressWarnings("designforextension")
        public FloatVector.Rel<U> immutable()
        {
            setCopyOnWrite(true);
            return instantiateRel(getData(), getUnit());
        }

        /** {@inheritDoc} */
        @Override
        @SuppressWarnings("designforextension")
        public MutableFloatVector.Rel<U> mutable()
        {
            setCopyOnWrite(true);
            final MutableFloatVector.Rel<U> result = new MutableFloatVector.Rel<U>(getData(), getUnit());
            result.setCopyOnWrite(true);
            return result;
        }

        /** {@inheritDoc} */
        @Override
        @SuppressWarnings("checkstyle:designforextension")
        public MutableFloatVector.Rel<U> copy()
        {
            return mutable();
        }

        /** {@inheritDoc} */
        @Override
        @SuppressWarnings("designforextension")
        public MutableFloatVector.Rel<U> toDense()
        {
            return this.data.isDense() ? this : new MutableFloatVector.Rel<U>(this.data.toDense(), getUnit());
        }

        /** {@inheritDoc} */
        @Override
        @SuppressWarnings("designforextension")
        public MutableFloatVector.Rel<U> toSparse()
        {
            return this.data.isSparse() ? this : new MutableFloatVector.Rel<U>(this.data.toSparse(), getUnit());
        }

        /** {@inheritDoc} */
        @Override
        @SuppressWarnings("checkstyle:designforextension")
        public FloatScalar.Rel<U> get(final int index) throws ValueException
        {
            return new FloatScalar.Rel<U>(getInUnit(index, getUnit()), getUnit());
        }

        /**
         * Increment the value by the supplied value and return the changed vector.
         * @param increment FloatVector.Rel&lt;U&gt;; amount by which the value is incremented
         * @return the changed MutableFloatVector.Rel&lt;U&gt;
         * @throws ValueException when the size of increment is not identical to the size of this
         */
        public final MutableFloatVector.Rel<U> incrementBy(final FloatVector.Rel<U> increment) throws ValueException
        {
            checkCopyOnWrite();
            this.data.incrementBy(increment.getData());
            return this;
        }

        /**
         * Increment the value by the supplied value and return the changed vector.
         * @param increment FloatScalar.Rel&lt;U&gt;; amount by which the value is incremented
         * @return the changed MutableFloatVector.Rel&lt;U&gt;
         */
        public final MutableFloatVector.Rel<U> incrementBy(final FloatScalar.Rel<U> increment)
        {
            return incrementBy(increment.si);
        }

        /**
         * Increment the value by the supplied constant and return the changed vector.
         * @param increment amount by which the value is incremented
         * @return the changed MutableFloatVector.Rel&lt;U&gt;
         */
        public final MutableFloatVector.Rel<U> incrementBy(final float increment)
        {
            checkCopyOnWrite();
            this.data.incrementBy(increment);
            return this;
        }

        /**
         * Decrement the value by the supplied value and return the changed vector.
         * @param decrement FloatVector.Rel&lt;U&gt;; amount by which the value is decremented
         * @return the changed MutableFloatVector.Rel&lt;U&gt;
         * @throws ValueException when the size of increment is not identical to the size of this
         */
        public final MutableFloatVector.Rel<U> decrementBy(final FloatVector.Rel<U> decrement) throws ValueException
        {
            checkCopyOnWrite();
            this.data.decrementBy(decrement.getData());
            return this;
        }

        /**
         * Decrement the value by the supplied value and return the changed vector.
         * @param decrement FloatScalar.Rel&lt;U&gt;; amount by which the value is decremented
         * @return the changed MutableFloatVector.Rel&lt;U&gt;
         */
        public final MutableFloatVector.Rel<U> decrementBy(final FloatScalar.Rel<U> decrement)
        {
            return decrementBy(decrement.si);
        }

        /**
         * Decrement the value by the supplied constant and return the changed vector.
         * @param decrement amount by which the value is decremented
         * @return the changed MutableFloatVector.Rel&lt;U&gt;
         */
        public final MutableFloatVector.Rel<U> decrementBy(final float decrement)
        {
            checkCopyOnWrite();
            this.data.decrementBy(decrement);
            return this;
        }

        /**
         * Multiply the values in the vector by the supplied values and return the changed vector.
         * @param factors FloatVector.Rel&lt;U&gt;; amounts by which the value is multiplied
         * @return the changed MutableFloatVector.Rel&lt;U&gt;
         * @throws ValueException when the size of the factors is not identical to the size of this
         */
        public final MutableFloatVector.Rel<U> multiplyBy(final FloatVector.Rel<U> factors) throws ValueException
        {
            checkCopyOnWrite();
            this.data.multiplyBy(factors.getData());
            return this;
        }

        /**
         * Multiply the values in the vector by the supplied value and return the changed vector.
         * @param factor FloatScalar.Rel&lt;U&gt;; amount by which the values in the vector are multiplied
         * @return the changed MutableFloatVector.Rel&lt;U&gt;
         */
        public final MutableFloatVector.Rel<U> multiplyBy(final FloatScalar.Rel<U> factor)
        {
            return multiplyBy(factor.si);
        }

        /** {@inheritDoc} */
        @Override
        @SuppressWarnings("checkstyle:designforextension")
        public MutableFloatVector.Rel<U> multiplyBy(final float factor)
        {
            checkCopyOnWrite();
            this.data.multiplyBy(factor);
            return this;
        }

        /**
         * Divide the values in the vector by the supplied values and return the changed vector.
         * @param factors FloatVector.Rel&lt;U&gt;; amounts by which the value is divided
         * @return the changed MutableFloatVector.Rel&lt;U&gt;
         * @throws ValueException when the size of the factors is not identical to the size of this
         */
        public final MutableFloatVector.Rel<U> divideBy(final FloatVector.Rel<U> factors) throws ValueException
        {
            checkCopyOnWrite();
            this.data.divideBy(factors.getData());
            return this;
        }

        /** {@inheritDoc} */
        @Override
        @SuppressWarnings("checkstyle:designforextension")
        public MutableFloatVector.Rel<U> divideBy(final float factor)
        {
            this.data.divideBy(factor);
            return this;
        }

        /**
         * Divide the values in the vector by the supplied value and return the changed vector.
         * @param factor FloatScalar.Rel&lt;U&gt;; amount by which the values in the vector are divided
         * @return the changed MutableFloatVector.Rel&lt;U&gt;
         */
        public final MutableFloatVector.Rel<U> divideBy(final FloatScalar.Rel<U> factor)
        {
            return divideBy(factor.si);
        }

        /** {@inheritDoc} */
        @Override
        @SuppressWarnings("designforextension")
        public FloatVector.Rel<U> plus(final FloatVector.Rel<U> rel) throws ValueException
        {
            return instantiateRel(this.getData().plus(rel.getData()), getUnit());
        }

        /** {@inheritDoc} */
        @Override
        @SuppressWarnings("designforextension")
        public FloatVector.Abs<U> plus(final FloatVector.Abs<U> abs) throws ValueException
        {
            return instantiateAbs(this.getData().plus(abs.getData()), getUnit());
        }

        /** {@inheritDoc} */
        @Override
        @SuppressWarnings("designforextension")
        public FloatVector.Rel<U> minus(final FloatVector.Rel<U> rel) throws ValueException
        {
            return instantiateRel(this.getData().minus(rel.getData()), getUnit());
        }

        /** {@inheritDoc} */
        @Override
        @SuppressWarnings("designforextension")
        public FloatVector.Rel<U> times(final FloatVector.Rel<U> rel) throws ValueException
        {
            return instantiateRel(this.getData().times(rel.getData()), getUnit());
        }

        /** {@inheritDoc} */
        @Override
        @SuppressWarnings("designforextension")
        public FloatVector.Rel<U> divide(final FloatVector.Rel<U> rel) throws ValueException
        {
            return instantiateRel(this.getData().divide(rel.getData()), getUnit());
        }

        /**********************************************************************************/
        /********************************** MATH METHODS **********************************/
        /**********************************************************************************/

        /** {@inheritDoc} */
        @Override
        @SuppressWarnings("checkstyle:designforextension")
        public MutableFloatVector.Rel<U> abs()
        {
            assign(FloatMathFunctions.ABS);
            return this;
        }

        /** {@inheritDoc} */
        @Override
        @SuppressWarnings("checkstyle:designforextension")
        public MutableFloatVector.Rel<U> ceil()
        {
            assign(FloatMathFunctions.CEIL);
            return this;
        }

        /** {@inheritDoc} */
        @Override
        @SuppressWarnings("checkstyle:designforextension")
        public MutableFloatVector.Rel<U> floor()
        {
            assign(FloatMathFunctions.FLOOR);
            return this;
        }

        /** {@inheritDoc} */
        @Override
        @SuppressWarnings("checkstyle:designforextension")
        public MutableFloatVector.Rel<U> rint()
        {
            assign(FloatMathFunctions.RINT);
            return this;
        }

        /** {@inheritDoc} */
        @Override
        @SuppressWarnings("checkstyle:designforextension")
        public MutableFloatVector.Rel<U> round()
        {
            assign(FloatMathFunctions.ROUND);
            return this;
        }

    }

    /**********************************************************************************/
    /******************************** ABS + REL METHODS *******************************/
    /**********************************************************************************/

    /**
     * Instantiate a vector based on the type of data.
     * @param dvData the FloatVectorData
     * @param unit the unit to use
     * @param <U> the unit
     * @return an instantiated vector
     */
    static <U extends Unit<U>> MutableFloatVector.Abs<U> instantiateMutableAbs(final FloatVectorData dvData, final U unit)
    {
        return new MutableFloatVector.Abs<U>(dvData, unit);
    }

    /**
     * Check the copyOnWrite flag and, if it is set, make a deep copy of the data and clear the flag.
     */
    protected final void checkCopyOnWrite()
    {
        if (isCopyOnWrite())
        {
            this.data = this.data.copy();
            setCopyOnWrite(false);
        }
    }

    /** {@inheritDoc} */
    @Override
    public final void setSI(final int index, final float valueSI) throws ValueException
    {
        checkIndex(index);
        checkCopyOnWrite();
        this.data.setSI(index, valueSI);
    }

    /** {@inheritDoc} */
    @Override
    public final void set(final int index, final FloatScalar<U> value) throws ValueException
    {
        setSI(index, value.getSI());
    }

    /** {@inheritDoc} */
    @Override
    public final void setInUnit(final int index, final float value, final U valueUnit) throws ValueException
    {
        setSI(index, (float) ValueUtil.expressAsSIUnit(value, valueUnit));
    }

    /**
     * Execute a function on a cell by cell basis. Note: because many functions have to act on zero cells or can generate cells
     * with a zero value, the functions have to be applied on a dense dataset which has to be transformed back to a sparse
     * dataset if necessary.
     * @param floatFunction the function to apply
     */
    public final void assign(final FloatFunction floatFunction)
    {
        checkCopyOnWrite();
        if (this.data instanceof FloatVectorDataDense)
        {
            ((FloatVectorDataDense) this.data).assign(floatFunction);
        }
        else
        {
            FloatVectorDataDense dvdd = ((FloatVectorDataSparse) this.data).toDense();
            dvdd.assign(floatFunction);
            this.data = dvdd.toSparse();
        }
    }

    /** {@inheritDoc} */
    @Override
    public final void normalize() throws ValueException
    {
        float sum = zSum();
        if (0 == sum)
        {
            throw new ValueException("zSum is 0; cannot normalize");
        }
        checkCopyOnWrite();
        this.data.divideBy(sum);
    }
}