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