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