View Javadoc
1   package org.djunits.value.vdouble.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.vdouble.function.DoubleMathFunctions;
9   import org.djunits.value.vdouble.matrix.SIMatrix;
10  import org.djunits.value.vdouble.matrix.data.DoubleMatrixData;
11  import org.djunits.value.vdouble.scalar.base.DoubleScalar;
12  import org.djunits.value.vdouble.scalar.base.DoubleScalarRel;
13  import org.djunits.value.vdouble.vector.base.DoubleVector;
14  import org.djunits.value.vdouble.vector.base.DoubleVectorRel;
15  
16  /**
17   * DoubleMatrixRel.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 DoubleMatrixRel<U extends Unit<U>, S extends DoubleScalarRel<U, S>,
29          RV extends DoubleVectorRel<U, S, RV>, RM extends DoubleMatrixRel<U, S, RV, RM>>
30          extends DoubleMatrix<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 DoubleMatrix.
37       * @param data DoubleMatrixData; an internal data object
38       * @param unit U; the unit
39       */
40      protected DoubleMatrixRel(final DoubleMatrixData 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 SI 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 this 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(DoubleMathFunctions.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
86       * @throws ValueRuntimeException when the sizes of the matrices differ, or <code>increment</code> is null
87       */
88      @SuppressWarnings("unchecked")
89      public RM incrementBy(final RM increment)
90      {
91          checkCopyOnWrite();
92          this.data.incrementBy(increment.getData());
93          return (RM) this;
94      }
95  
96      /**
97       * Decrement all values of this matrix by the decrement. This only works if this matrix is mutable.
98       * @param decrement S; the scalar by which to decrement all values
99       * @return RM; this modified matrix
100      * @throws ValueRuntimeException in case this matrix is immutable
101      */
102     @SuppressWarnings("unchecked")
103     public RM decrementBy(final S decrement)
104     {
105         checkCopyOnWrite();
106         assign(DoubleMathFunctions.DEC(decrement.si));
107         return (RM) this;
108     }
109 
110     /**
111      * Decrement this Relative matrix by another Relative matrix. The operation is done value by value. This is only allowed if
112      * this matrix is mutable.
113      * @param decrement RM; the matrix that contains the values by which to decrement the corresponding values
114      * @return RM; this modified matrix
115      * @throws ValueRuntimeException in case this matrix is immutable
116      * @throws ValueRuntimeException when the sizes of the matrices differ, or <code>decrement</code> is null
117      */
118     @SuppressWarnings("unchecked")
119     public final RM decrementBy(final RM decrement)
120     {
121         checkCopyOnWrite();
122         this.data.decrementBy(decrement.getData());
123         return (RM) this;
124     }
125 
126     /**
127      * Multiply all values of this matrix by the multiplier. This only works if the matrix is mutable.
128      * @param multiplier double; the factor by which to multiply all values
129      * @return V; this modified matrix
130      * @throws ValueRuntimeException in case the matrix is immutable
131      */
132     public RM multiplyBy(final double multiplier)
133     {
134         return assign(DoubleMathFunctions.MULT(multiplier));
135     }
136 
137     /**
138      * Divide all values of this matrix by the divisor. This only works if the matrix is mutable.
139      * @param divisor double; the value by which to divide all values
140      * @return RM; this modified matrix
141      * @throws ValueRuntimeException in case the matrix is immutable
142      */
143     public RM divideBy(final double divisor)
144     {
145         return assign(DoubleMathFunctions.DIV(divisor));
146     }
147 
148     /**
149      * Multiply a Relative value with this Relative value for a matrix or matrix. The multiplication is done value by value and
150      * store the result in a new Relative value. If both operands are dense, the result is a dense matrix or matrix, otherwise
151      * the result is a sparse matrix or matrix.
152      * @param rel MT; the right operand, which can be any matrix type
153      * @return SIMatrix; the multiplication of this matrix and the operand
154      * @throws ValueRuntimeException in case this matrix or matrix and the operand have a different size
155      * @throws UnitException on unit error
156      * @param <UT> the unit type of the multiplier
157      * @param <ST> the scalar type of the multiplier
158      * @param <VT> the vector type of the multiplier
159      * @param <MT> the matrix type of the multiplier
160      */
161     public final <UT extends Unit<UT>, ST extends DoubleScalar<UT, ST>, VT extends DoubleVector<UT, ST, VT>,
162             MT extends DoubleMatrix<UT, ST, VT, MT> & Relative<UT, MT>> SIMatrix times(final MT rel)
163                     throws ValueRuntimeException, UnitException
164     {
165         return new SIMatrix(this.getData().times(rel.getData()), SIUnit.of(
166                 getDisplayUnit().getQuantity().getSiDimensions().plus(rel.getDisplayUnit().getQuantity().getSiDimensions())));
167     }
168 
169     @Override
170     public final RM times(final double multiplier)
171     {
172         RM result = clone().mutable();
173         result.assign(DoubleMathFunctions.MULT(multiplier));
174         return result.immutable();
175     }
176 
177     @Override
178     public final RM times(final float multiplier)
179     {
180         return times((double) multiplier);
181     }
182 
183     /**
184      * Divide this Relative matrix by another Relative matrix. The operation is done value by value and store the result is
185      * stored in a new Relative matrix. If both operands are dense, the result is a dense matrix, otherwise the result is a
186      * sparse matrix. TODO discuss dense or sparseness of result.
187      * @param rel MT; the right operand, which can be any matrix type
188      * @return SIMatrix; the division of this matrix and the operand
189      * @throws ValueRuntimeException in case this matrix 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      * @param <MT> the matrix type of the multiplier
195      */
196     public final <UT extends Unit<UT>, ST extends DoubleScalar<UT, ST>, VT extends DoubleVector<UT, ST, VT>,
197             MT extends DoubleMatrix<UT, ST, VT, MT> & Relative<UT, MT>> SIMatrix divide(final MT rel)
198                     throws ValueRuntimeException, UnitException
199     {
200         return new SIMatrix(this.getData().divide(rel.getData()), SIUnit.of(
201                 getDisplayUnit().getQuantity().getSiDimensions().minus(rel.getDisplayUnit().getQuantity().getSiDimensions())));
202     }
203 
204     @Override
205     public final RM divide(final float divisor)
206     {
207         return divide((double) divisor);
208     }
209 
210     @Override
211     public final RM divide(final double divisor)
212     {
213         RM result = clone().mutable();
214         result.assign(DoubleMathFunctions.DIV(divisor));
215         return result.immutable();
216     }
217 
218 }