1 package org.djunits.value.vfloat.matrix.base;
2
3 import org.djunits.unit.AbsoluteLinearUnit;
4 import org.djunits.unit.Unit;
5 import org.djunits.value.Absolute;
6 import org.djunits.value.ValueRuntimeException;
7 import org.djunits.value.vfloat.function.FloatMathFunctions;
8 import org.djunits.value.vfloat.matrix.data.FloatMatrixData;
9 import org.djunits.value.vfloat.scalar.base.FloatScalarAbs;
10 import org.djunits.value.vfloat.scalar.base.FloatScalarRelWithAbs;
11 import org.djunits.value.vfloat.vector.base.FloatVectorAbs;
12 import org.djunits.value.vfloat.vector.base.FloatVectorRelWithAbs;
13 import org.djunits.value.vfloat.vector.data.FloatVectorData;
14
15 /**
16 * AbstractMutableFloatMatrixRelWithAbs.java.
17 * <p>
18 * Copyright (c) 2019-2025 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
19 * BSD-style license. See <a href="https://djunits.org/docs/license.html">DJUNITS License</a>.
20 * </p>
21 * @author <a href="https://www.tudelft.nl/averbraeck" target="_blank">Alexander Verbraeck</a>
22 * @param <AU> the absolute unit belonging to the relative unit
23 * @param <A> the absolute scalar type belonging to the absolute matrix type
24 * @param <AV> the absolute vector type belonging to the absolute matrix type
25 * @param <AM> the (immutable or mutable) absolute matrix type
26 * @param <RU> the relative unit belonging to the absolute unit
27 * @param <R> the relative scalar type belonging to the relative matrix type
28 * @param <RV> the relative vector type belonging to the relative matrix type
29 * @param <RM> the relative (immutable or mutable) matrix type with this unit
30 */
31 // @formatter:off
32 public abstract class FloatMatrixAbs<
33 AU extends AbsoluteLinearUnit<AU, RU>,
34 A extends FloatScalarAbs<AU, A, RU, R>,
35 AV extends FloatVectorAbs<AU, A, AV, RU, R, RV>,
36 AM extends FloatMatrixAbs<AU, A, AV, AM, RU, R, RV, RM>,
37 RU extends Unit<RU>,
38 R extends FloatScalarRelWithAbs<AU, A, RU, R>,
39 RV extends FloatVectorRelWithAbs<AU, A, AV, RU, R, RV>,
40 RM extends FloatMatrixRelWithAbs<AU, A, AV, AM, RU, R, RV, RM>>
41 extends FloatMatrix<AU, A, AV, AM>
42 implements Absolute<AU, AM, RU, RM>
43 // @formatter:on
44 {
45 /** */
46 private static final long serialVersionUID = 20190908L;
47
48 /**
49 * Construct a new Relative Mutable FloatMatrix.
50 * @param data an internal data object
51 * @param unit the unit
52 */
53 protected FloatMatrixAbs(final FloatMatrixData data, final AU unit)
54 {
55 super(data.copy(), unit);
56 }
57
58 @Override
59 public AM plus(final RM increment) throws ValueRuntimeException
60 {
61 return instantiateMatrix(this.getData().plus(increment.getData()), getDisplayUnit());
62 }
63
64 @Override
65 public AM minus(final RM decrement) throws ValueRuntimeException
66 {
67 return instantiateMatrix(this.getData().minus(decrement.getData()), getDisplayUnit());
68 }
69
70 @Override
71 public RM minus(final AM decrement) throws ValueRuntimeException
72 {
73 return instantiateMatrixRel(this.getData().minus(decrement.getData()), decrement.getDisplayUnit().getRelativeUnit());
74 }
75
76 /**
77 * Decrement all values of this matrix by the decrement. This only works if this matrix is mutable.
78 * @param decrement the scalar by which to decrement all values
79 * @return this modified vector
80 * @throws ValueRuntimeException in case this vector is immutable
81 */
82 @SuppressWarnings("unchecked")
83 public AM decrementBy(final R decrement)
84 {
85 checkCopyOnWrite();
86 assign(FloatMathFunctions.DEC(decrement.si));
87 return (AM) this;
88 }
89
90 /**
91 * Decrement all values of this matrix by the decrement on a value by value basis. This only works if this matrix is
92 * mutable.
93 * @param decrement the matrix that contains the values by which to decrement the corresponding values
94 * @return this modified matrix
95 * @throws ValueRuntimeException in case this matrix is immutable or when the sizes of the matrices differ
96 */
97 @SuppressWarnings("unchecked")
98 public AM decrementBy(final RM decrement)
99 {
100 checkCopyOnWrite();
101 this.data.decrementBy(decrement.getData());
102 return (AM) this;
103 }
104
105 /**
106 * Instantiate a new relative matrix of the class of this absolute matrix. This can be used instead of the
107 * FloatMatrix.instiantiate() methods in case another matrix of this absolute matrix class is known. The method is faster
108 * than FloatMatrix.instantiate, and it will also work if the matrix is user-defined.
109 * @param dmd the data used to instantiate the matrix
110 * @param displayUnit the display unit of the relative matrix
111 * @return a relative matrix of the correct type, belonging to this absolute matrix type
112 */
113 public abstract RM instantiateMatrixRel(FloatMatrixData dmd, RU displayUnit);
114
115 /**
116 * Instantiate a new relative vector of the class of this absolute matrix. This can be used instead of the
117 * FloatVector.instiantiate() methods in case another matrix of this absolute matrix class is known. The method is faster
118 * than FloatVector.instantiate, and it will also work if the matrix or vector is user-defined.
119 * @param dvd the data used to instantiate the vector
120 * @param displayUnit the display unit of the relative vector
121 * @return a relative vector of the correct type, belonging to this absolute matrix type
122 */
123 public abstract RV instantiateVectorRel(FloatVectorData dvd, RU displayUnit);
124
125 /**
126 * Instantiate a new relative scalar for the class of this absolute matrix. This can be used instead of the
127 * FloatScalar.instiantiate() methods in case a matrix of this class is known. The method is faster than
128 * FloatScalar.instantiate, and it will also work if the matrix and/or scalar are user-defined.
129 * @param valueSI the SI value of the relative scalar
130 * @param displayUnit the unit in which the relative value will be displayed
131 * @return a relative scalar of the correct type, belonging to this absolute matrix type
132 */
133 public abstract R instantiateScalarRelSI(float valueSI, RU displayUnit);
134
135 }