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