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.base.Matrix;
8 import org.djunits.value.vdouble.function.DoubleMathFunctions;
9 import org.djunits.value.vdouble.matrix.data.DoubleMatrixData;
10 import org.djunits.value.vdouble.scalar.base.AbstractDoubleScalarAbs;
11 import org.djunits.value.vdouble.scalar.base.AbstractDoubleScalarRelWithAbs;
12 import org.djunits.value.vdouble.vector.base.AbstractDoubleVectorAbs;
13 import org.djunits.value.vdouble.vector.base.AbstractDoubleVectorRelWithAbs;
14 import org.djunits.value.vdouble.vector.data.DoubleVectorData;
15
16 /**
17 * AbstractMutableDoubleMatrixRelWithAbs.java.
18 * <p>
19 * Copyright (c) 2019-2019 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 <AU> the absolute unit belonging to the relative unit
24 * @param <A> the absolute scalar type belonging to the absolute matrix type
25 * @param <AV> the absolute vector type belonging to the absolute matrix type
26 * @param <AM> the (immutable or mutable) absolute matrix type
27 * @param <RU> the relative unit belonging to the absolute unit
28 * @param <R> the relative scalar type belonging to the relative matrix type
29 * @param <RV> the relative vector type belonging to the relative matrix type
30 * @param <RM> the relative (immutable or mutable) matrix type with this unit
31 */
32 // @formatter:off
33 public abstract class AbstractDoubleMatrixAbs<
34 AU extends AbsoluteLinearUnit<AU, RU>,
35 A extends AbstractDoubleScalarAbs<AU, A, RU, R>,
36 AV extends AbstractDoubleVectorAbs<AU, A, AV, RU, R, RV>,
37 AM extends AbstractDoubleMatrixAbs<AU, A, AV, AM, RU, R, RV, RM>,
38 RU extends Unit<RU>,
39 R extends AbstractDoubleScalarRelWithAbs<AU, A, RU, R>,
40 RV extends AbstractDoubleVectorRelWithAbs<AU, A, AV, RU, R, RV>,
41 RM extends AbstractDoubleMatrixRelWithAbs<AU, A, AV, AM, RU, R, RV, RM>>
42 extends AbstractDoubleMatrix<AU, A, AV, AM>
43 implements Matrix.Abs<AU, A, AV, AM, RU, R, RV, RM>, Absolute
44 // @formatter:on
45 {
46 /** */
47 private static final long serialVersionUID = 20190908L;
48
49 /**
50 * Construct a new Relative Mutable DoubleMatrix.
51 * @param data DoubleMatrixData; an internal data object
52 * @param unit U; the unit
53 */
54 protected AbstractDoubleMatrixAbs(final DoubleMatrixData data, final AU unit)
55 {
56 super(data.copy(), unit);
57 }
58
59 /** {@inheritDoc} */
60 @Override
61 public AM plus(final RM increment) throws ValueRuntimeException
62 {
63 return instantiateMatrix(this.getData().plus(increment.getData()), getDisplayUnit());
64 }
65
66 /** {@inheritDoc} */
67 @Override
68 public AM minus(final RM decrement) throws ValueRuntimeException
69 {
70 return instantiateMatrix(this.getData().minus(decrement.getData()), getDisplayUnit());
71 }
72
73 /** {@inheritDoc} */
74 @Override
75 public RM minus(final AM decrement) throws ValueRuntimeException
76 {
77 return instantiateMatrixRel(this.getData().minus(decrement.getData()), decrement.getDisplayUnit().getRelativeUnit());
78 }
79
80 /**
81 * Decrement all values of this matrix by the decrement. This only works if this matrix is mutable.
82 * @param decrement R; the scalar by which to decrement all values
83 * @return AM; this modified vector
84 * @throws ValueRuntimeException in case this vector is immutable
85 */
86 public AM decrementBy(final R decrement)
87 {
88 checkCopyOnWrite();
89 return assign(DoubleMathFunctions.DEC(decrement.si));
90 }
91
92 /**
93 * Decrement all values of this matrix by the decrement on a value by value basis. This only works if this matrix is
94 * mutable.
95 * @param decrement RV; the matrix that contains the values by which to decrement the corresponding values
96 * @return AV; this modified matrix
97 * @throws ValueRuntimeException in case this matrix is immutable
98 * @throws ValueRuntimeException when the sizes of the matrices differ, or <code>decrement</code> is null
99 */
100 @SuppressWarnings("unchecked")
101 public AM decrementBy(final RM decrement)
102 {
103 checkCopyOnWrite();
104 this.data.decrementBy(decrement.getData());
105 return (AM) this;
106 }
107
108 /**
109 * Instantiate a new relative matrix of the class of this absolute matrix. This can be used instead of the
110 * DoubleMatrix.instiantiate() methods in case another matrix of this absolute matrix class is known. The method is faster
111 * than DoubleMatrix.instantiate, and it will also work if the matrix is user-defined.
112 * @param dmd DoubleMatrixData; the data used to instantiate the matrix
113 * @param displayUnit RU; the display unit of the relative matrix
114 * @return RM; a relative matrix of the correct type, belonging to this absolute matrix type
115 */
116 public abstract RM instantiateMatrixRel(DoubleMatrixData dmd, RU displayUnit);
117
118 /**
119 * Instantiate a new relative vector of the class of this absolute matrix. This can be used instead of the
120 * DoubleVector.instiantiate() methods in case another matrix of this absolute matrix class is known. The method is faster
121 * than DoubleVector.instantiate, and it will also work if the matrix or vector is user-defined.
122 * @param dvd DoubleVectorData; the data used to instantiate the vector
123 * @param displayUnit RU; the display unit of the relative vector
124 * @return RV; a relative vector of the correct type, belonging to this absolute matrix type
125 */
126 public abstract RV instantiateVectorRel(DoubleVectorData dvd, RU displayUnit);
127
128 /**
129 * Instantiate a new relative scalar for the class of this absolute matrix. This can be used instead of the
130 * DoubleScalar.instiantiate() methods in case a matrix of this class is known. The method is faster than
131 * DoubleScalar.instantiate, and it will also work if the matrix and/or scalar are user-defined.
132 * @param valueSI double; the SI value of the relative scalar
133 * @param displayUnit RU; the unit in which the relative value will be displayed
134 * @return R; a relative scalar of the correct type, belonging to this absolute matrix type
135 */
136 public abstract R instantiateScalarRelSI(double valueSI, RU displayUnit);
137
138 }