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