1 package org.djunits.value.vfloat.vector.base; 2 3 import org.djunits.unit.SIUnit; 4 import org.djunits.unit.Unit; 5 import org.djunits.unit.util.UnitException; 6 import org.djunits.value.Relative; 7 import org.djunits.value.ValueRuntimeException; 8 import org.djunits.value.vfloat.function.FloatMathFunctions; 9 import org.djunits.value.vfloat.scalar.base.FloatScalarRel; 10 import org.djunits.value.vfloat.vector.FloatSIVector; 11 import org.djunits.value.vfloat.vector.data.FloatVectorData; 12 13 /** 14 * AbstractMutableFloatVectorRel.java. 15 * <p> 16 * Copyright (c) 2019-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br> 17 * BSD-style license. See <a href="https://djunits.org/docs/license.html">DJUNITS License</a>. 18 * </p> 19 * @author <a href="https://www.tudelft.nl/averbraeck" target="_blank">Alexander Verbraeck</a> 20 * @param <U> the unit 21 * @param <S> the scalar type belonging to the vector type 22 * @param <RV> the relative vector type with this unit 23 */ 24 public abstract class FloatVectorRel<U extends Unit<U>, S extends FloatScalarRel<U, S>, 25 RV extends FloatVectorRel<U, S, RV>> extends FloatVector<U, S, RV> implements Relative<U, RV> 26 { 27 /** */ 28 private static final long serialVersionUID = 20190908L; 29 30 /** 31 * Construct a new Relative Mutable FloatVector. 32 * @param data FloatVectorData; an internal data object 33 * @param unit U; the unit 34 */ 35 protected FloatVectorRel(final FloatVectorData data, final U unit) 36 { 37 super(data.copy(), unit); 38 } 39 40 /** 41 * Compute the sum of all SI values of this vector. 42 * @return S; the sum of all values of this vector with the same display unit as this vector 43 */ 44 public final S zSum() 45 { 46 return instantiateScalarSI(getData().zSum(), getDisplayUnit()); 47 } 48 49 @Override 50 public final RV plus(final RV rel) throws ValueRuntimeException 51 { 52 return instantiateVector(this.getData().plus(rel.getData()), getDisplayUnit()); 53 } 54 55 @Override 56 public final RV minus(final RV rel) throws ValueRuntimeException 57 { 58 return instantiateVector(this.getData().minus(rel.getData()), getDisplayUnit()); 59 } 60 61 /** 62 * Increment all values of this vector by the increment. This only works if the vector is mutable. 63 * @param increment S; the scalar by which to increment all values 64 * @return RV; this modified vector 65 * @throws ValueRuntimeException in case this vector is immutable 66 */ 67 @SuppressWarnings("unchecked") 68 public RV incrementBy(final S increment) 69 { 70 checkCopyOnWrite(); 71 assign(FloatMathFunctions.INC(increment.si)); 72 return (RV) this; 73 } 74 75 /** 76 * Increment all values of this vector by the increment on a value by value basis. This only works if this vector is 77 * mutable. 78 * @param increment RV; the vector that contains the values by which to increment the corresponding values 79 * @return RV; this modified vector 80 * @throws ValueRuntimeException in case this vector is immutable or when the sizes of the vectors differ 81 */ 82 @SuppressWarnings("unchecked") 83 public RV incrementBy(final RV increment) 84 { 85 checkCopyOnWrite(); 86 getData().incrementBy(increment.getData()); 87 return (RV) this; 88 } 89 90 /** 91 * Decrement all values of this vector by the decrement. This only works if the vector is mutable. 92 * @param decrement S; the scalar by which to decrement all values 93 * @return RV; this modified vector 94 * @throws ValueRuntimeException in case this vector is immutable 95 */ 96 @SuppressWarnings("unchecked") 97 public RV decrementBy(final S decrement) 98 { 99 checkCopyOnWrite(); 100 assign(FloatMathFunctions.DEC(decrement.si)); 101 return (RV) this; 102 } 103 104 /** 105 * Decrement all values of this vector by the decrement on a value by value basis. This only works if this vector is 106 * mutable. 107 * @param decrement RV; the vector that contains the values by which to decrement the corresponding values 108 * @return RV; this modified vector 109 * @throws ValueRuntimeException in case this vector is immutable or when the sizes of the vectors differ 110 */ 111 @SuppressWarnings("unchecked") 112 public RV decrementBy(final RV decrement) 113 { 114 checkCopyOnWrite(); 115 getData().decrementBy(decrement.getData()); 116 return (RV) this; 117 } 118 119 /** 120 * Multiply all values of this vector by the multiplier. This only works if the vector is mutable. 121 * @param multiplier float; the factor by which to multiply all values 122 * @return RV; this modified vector 123 * @throws ValueRuntimeException in case the vector is immutable 124 */ 125 public RV multiplyBy(final float multiplier) 126 { 127 return assign(FloatMathFunctions.MULT(multiplier)); 128 } 129 130 /** 131 * Divide all values of this vector by the divisor. This only works if the vector is mutable. 132 * @param divisor float; the value by which to divide all values 133 * @return RV; this modified vector 134 * @throws ValueRuntimeException in case the vector is immutable 135 */ 136 public RV divideBy(final float divisor) 137 { 138 return assign(FloatMathFunctions.DIV(divisor)); 139 } 140 141 /** 142 * Multiply all values of this vector by the multiplier. This only works if the vector is mutable. 143 * @param multiplier double; the factor by which to multiply all values 144 * @return RV; this modified vector 145 * @throws ValueRuntimeException in case the vector is immutable 146 */ 147 public RV multiplyBy(final double multiplier) 148 { 149 return assign(FloatMathFunctions.MULT((float) multiplier)); 150 } 151 152 /** 153 * Divide all values of this vector by the divisor. This only works if the vector is mutable. 154 * @param divisor double; the value by which to divide all values 155 * @return RV; this modified vector 156 * @throws ValueRuntimeException in case the vector is immutable 157 */ 158 public RV divideBy(final double divisor) 159 { 160 return assign(FloatMathFunctions.DIV((float) divisor)); 161 } 162 163 /** 164 * Multiply a Relative value with this Relative value for a vector or matrix. The multiplication is done value by value and 165 * store the result in a new Relative value. If both operands are dense, the result is a dense vector or matrix, otherwise 166 * the result is a sparse vector or matrix. 167 * @param rel VT; the right operand, which can be any vector type 168 * @return FloatSIVector; the multiplication of this vector and the operand 169 * @throws ValueRuntimeException in case this vector or matrix and the operand have a different size 170 * @throws UnitException on unit error 171 * @param <UT> the unit type of the multiplier 172 * @param <ST> the scalar type of the multiplier 173 * @param <VT> the vector type of the multiplier 174 */ 175 public final <UT extends Unit<UT>, ST extends FloatScalarRel<UT, ST>, 176 VT extends FloatVectorRel<UT, ST, VT> & Relative<UT, VT>> FloatSIVector times(final VT rel) 177 throws ValueRuntimeException, UnitException 178 { 179 return new FloatSIVector(this.getData().times(rel.getData()), SIUnit.of( 180 getDisplayUnit().getQuantity().getSiDimensions().plus(rel.getDisplayUnit().getQuantity().getSiDimensions()))); 181 } 182 183 /** 184 * Divide this Relative value by a Relative value for a vector or matrix. The division is done value by value and store the 185 * result in a new Relative value. If both operands are dense, the result is a dense vector or matrix, otherwise the result 186 * is a sparse vector or matrix. 187 * @param rel VT; the right operand, which can be any vector type 188 * @return FloatSIVector; the division of this vector and the operand 189 * @throws ValueRuntimeException in case this vector or matrix and the operand have a different size 190 * @throws UnitException on unit error 191 * @param <UT> the unit type of the multiplier 192 * @param <ST> the scalar type of the multiplier 193 * @param <VT> the vector type of the multiplier 194 */ 195 public final <UT extends Unit<UT>, ST extends FloatScalarRel<UT, ST>, 196 VT extends FloatVectorRel<UT, ST, VT> & Relative<UT, VT>> FloatSIVector divide(final VT rel) 197 throws ValueRuntimeException, UnitException 198 { 199 return new FloatSIVector(this.getData().divide(rel.getData()), SIUnit.of( 200 getDisplayUnit().getQuantity().getSiDimensions().minus(rel.getDisplayUnit().getQuantity().getSiDimensions()))); 201 } 202 203 @Override 204 public RV times(final double multiplier) 205 { 206 return clone().mutable().assign(FloatMathFunctions.MULT((float) multiplier)).immutable(); 207 } 208 209 @Override 210 public RV divide(final double divisor) 211 { 212 return clone().mutable().assign(FloatMathFunctions.DIV((float) divisor)).immutable(); 213 } 214 215 @Override 216 public RV times(final float multiplier) 217 { 218 return times((double) multiplier); 219 } 220 221 @Override 222 public RV divide(final float divisor) 223 { 224 return divide((double) divisor); 225 } 226 227 }