1 package org.djunits.value.vdouble.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.vdouble.function.DoubleMathFunctions;
8 import org.djunits.value.vdouble.matrix.data.DoubleMatrixData;
9 import org.djunits.value.vdouble.scalar.base.DoubleScalarAbs;
10 import org.djunits.value.vdouble.scalar.base.DoubleScalarRelWithAbs;
11 import org.djunits.value.vdouble.vector.base.DoubleVectorAbs;
12 import org.djunits.value.vdouble.vector.base.DoubleVectorRelWithAbs;
13 import org.djunits.value.vdouble.vector.data.DoubleVectorData;
14
15 /**
16 * AbstractMutableDoubleMatrixRelWithAbs.java.
17 * <p>
18 * Copyright (c) 2019-2024 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 DoubleMatrixAbs<
33 AU extends AbsoluteLinearUnit<AU, RU>,
34 A extends DoubleScalarAbs<AU, A, RU, R>,
35 AV extends DoubleVectorAbs<AU, A, AV, RU, R, RV>,
36 AM extends DoubleMatrixAbs<AU, A, AV, AM, RU, R, RV, RM>,
37 RU extends Unit<RU>,
38 R extends DoubleScalarRelWithAbs<AU, A, RU, R>,
39 RV extends DoubleVectorRelWithAbs<AU, A, AV, RU, R, RV>,
40 RM extends DoubleMatrixRelWithAbs<AU, A, AV, AM, RU, R, RV, RM>>
41 extends DoubleMatrix<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 DoubleMatrix.
50 * @param data DoubleMatrixData; an internal data object
51 * @param unit AU; the unit
52 */
53 protected DoubleMatrixAbs(final DoubleMatrixData 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 R; the scalar by which to decrement all values
79 * @return AM; this modified vector
80 * @throws ValueRuntimeException in case this vector is immutable
81 */
82 public AM decrementBy(final R decrement)
83 {
84 checkCopyOnWrite();
85 return assign(DoubleMathFunctions.DEC(decrement.si));
86 }
87
88 /**
89 * Decrement all values of this matrix by the decrement on a value by value basis. This only works if this matrix is
90 * mutable.
91 * @param decrement RM; the matrix that contains the values by which to decrement the corresponding values
92 * @return AV; this modified matrix
93 * @throws ValueRuntimeException in case this matrix is immutable
94 * @throws ValueRuntimeException when the sizes of the matrices differ, or <code>decrement</code> is null
95 */
96 @SuppressWarnings("unchecked")
97 public AM decrementBy(final RM decrement)
98 {
99 checkCopyOnWrite();
100 this.data.decrementBy(decrement.getData());
101 return (AM) this;
102 }
103
104 /**
105 * Instantiate a new relative matrix of the class of this absolute matrix. This can be used instead of the
106 * DoubleMatrix.instiantiate() methods in case another matrix of this absolute matrix class is known. The method is faster
107 * than DoubleMatrix.instantiate, and it will also work if the matrix is user-defined.
108 * @param dmd DoubleMatrixData; the data used to instantiate the matrix
109 * @param displayUnit RU; the display unit of the relative matrix
110 * @return RM; a relative matrix of the correct type, belonging to this absolute matrix type
111 */
112 public abstract RM instantiateMatrixRel(DoubleMatrixData dmd, RU displayUnit);
113
114 /**
115 * Instantiate a new relative vector of the class of this absolute matrix. This can be used instead of the
116 * DoubleVector.instiantiate() methods in case another matrix of this absolute matrix class is known. The method is faster
117 * than DoubleVector.instantiate, and it will also work if the matrix or vector is user-defined.
118 * @param dvd DoubleVectorData; the data used to instantiate the vector
119 * @param displayUnit RU; the display unit of the relative vector
120 * @return RV; a relative vector of the correct type, belonging to this absolute matrix type
121 */
122 public abstract RV instantiateVectorRel(DoubleVectorData dvd, RU displayUnit);
123
124 /**
125 * Instantiate a new relative scalar for the class of this absolute matrix. This can be used instead of the
126 * DoubleScalar.instiantiate() methods in case a matrix of this class is known. The method is faster than
127 * DoubleScalar.instantiate, and it will also work if the matrix and/or scalar are user-defined.
128 * @param valueSI double; the SI value of the relative scalar
129 * @param displayUnit RU; the unit in which the relative value will be displayed
130 * @return R; a relative scalar of the correct type, belonging to this absolute matrix type
131 */
132 public abstract R instantiateScalarRelSI(double valueSI, RU displayUnit);
133
134 }