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