View Javadoc
1   package org.djunits.vecmat.d2;
2   
3   import org.djunits.quantity.def.AbsQuantity;
4   import org.djunits.quantity.def.Quantity;
5   import org.djunits.quantity.def.Reference;
6   import org.djunits.unit.UnitInterface;
7   import org.djunits.vecmat.def.AbsVector;
8   import org.djutils.exceptions.Throw;
9   
10  /**
11   * AbsVector2 implements a vector with two real-valued entries representing an absolute quantity. The vector is immutable,
12   * except for the display unit, which can be changed. Some of the method that have been defined already for a generic vector can
13   * be re-implemented for efficiency.
14   * <p>
15   * Copyright (c) 2026-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
16   * for project information <a href="https://djunits.org" target="_blank">https://djunits.org</a>. The DJUNITS project is
17   * distributed under a <a href="https://djunits.org/docs/license.html" target="_blank">three-clause BSD-style license</a>.
18   * @author Alexander Verbraeck
19   * @param <A> the absolute quantity type
20   * @param <Q> the corresponding relative quantity type
21   * @param <VA> the absolute vector or matrix type
22   * @param <VQ> the relative vector or matrix type
23   * @param <VAT> the type of the transposed version of the absolute vector
24   */
25  public abstract class AbsVector2<A extends AbsQuantity<A, Q, ?>, Q extends Quantity<Q>,
26          VA extends AbsVector2<A, Q, VA, VQ, VAT>, VQ extends Vector2<Q, VQ, ?, ?, ?>, VAT extends AbsVector2<A, Q, VAT, ?, VA>>
27          extends AbsVector<A, Q, VA, VQ, VAT>
28  {
29      /** */
30      private static final long serialVersionUID = 600L;
31  
32      /**
33       * Instantiate an AbsVector2.
34       * @param relativeVector the vector with values relative to the reference point
35       * @param reference the reference point for the absolute values
36       */
37      public AbsVector2(final VQ relativeVector, final Reference<?, A, Q> reference)
38      {
39          super(relativeVector, reference);
40      }
41  
42      /**
43       * Column vector for AbsVector2 with absolute quantities.
44       * <p>
45       * Copyright (c) 2026-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved.
46       * See for project information <a href="https://djunits.org" target="_blank">https://djunits.org</a>. The DJUNITS project is
47       * distributed under a <a href="https://djunits.org/docs/license.html" target="_blank">three-clause BSD-style license</a>.
48       * @author Alexander Verbraeck
49       * @param <A> the absolute quantity type
50       * @param <Q> the corresponding relative quantity type
51       */
52      public static class Col<A extends AbsQuantity<A, Q, ?>, Q extends Quantity<Q>>
53              extends AbsVector2<A, Q, AbsVector2.Col<A, Q>, Vector2.Col<Q>, AbsVector2.Row<A, Q>>
54              implements AbsVector.Col<AbsVector2.Col<A, Q>, Q>
55      {
56          /** */
57          private static final long serialVersionUID = 600L;
58  
59          /**
60           * Create a new AbsVector2 with absolute quantities, with a display unit and a reference point.
61           * @param relativeVector the vector with values relative to the reference point
62           * @param reference the reference point for the absolute values
63           */
64          public Col(final Vector2.Col<Q> relativeVector, final Reference<?, A, Q> reference)
65          {
66              super(relativeVector, reference);
67          }
68  
69          @Override
70          public AbsVector2.Col<A, Q> instantiate(final Vector2.Col<Q> relativeVector, final Reference<?, A, Q> reference)
71          {
72              return new AbsVector2.Col<>(relativeVector, reference);
73          }
74  
75          @Override
76          public AbsVector2.Row<A, Q> transpose()
77          {
78              return new AbsVector2.Row<>(getRelativeVecMat().transpose(), getReference());
79          }
80  
81          // ------------------------------------------ OF METHODS ------------------------------------------
82  
83          /**
84           * Create a AbsVector2.Col without needing generics.
85           * @param xInUnit the x-value expressed in the unit
86           * @param yInUnit the y-value expressed in the unit
87           * @param unit the unit of the data, which will also be used as the display unit
88           * @param reference the reference point for the absolute quantities
89           * @return a new AbsVector2.Col with a unit
90           * @param <A> the absolute quantity type
91           * @param <Q> the quantity type
92           * @param <R> the reference type
93           */
94          public static <A extends AbsQuantity<A, Q, R>, Q extends Quantity<Q>,
95                  R extends Reference<R, A, Q>> AbsVector2.Col<A, Q> of(final double xInUnit, final double yInUnit,
96                          final UnitInterface<Q> unit, final R reference)
97          {
98              return new AbsVector2.Col<>(Vector2.Col.of(xInUnit, yInUnit, unit), reference);
99          }
100 
101         /**
102          * Create a AbsVector2.Col without needing generics. The display unit will be taken from the x-quantity.
103          * @param x the x-value expressed as a quantity
104          * @param y the y-value expressed as a quantity
105          * @param reference the reference point for the absolute quantities
106          * @return a new AbsVector2.Col with a unit
107          * @param <A> the absolute quantity type
108          * @param <Q> the quantity type
109          * @param <R> the reference type
110          */
111         public static <A extends AbsQuantity<A, Q, R>, Q extends Quantity<Q>,
112                 R extends Reference<R, A, Q>> AbsVector2.Col<A, Q> of(final Q x, final Q y, final R reference)
113         {
114             return new AbsVector2.Col<>(Vector2.Col.of(x, y), reference);
115         }
116 
117         /**
118          * Create an AbsVector2.Col without needing generics.
119          * @param absX the v1-value expressed as an absolute quantity
120          * @param absY the v2-value expressed as an absolute quantity
121          * @return a new AbsVector2.Col with a unit
122          * @param <A> the absolute quantity type
123          * @param <Q> the quantity type
124          * @param <R> the reference type
125          */
126         public static <A extends AbsQuantity<A, Q, R>, Q extends Quantity<Q>,
127                 R extends Reference<R, A, Q>> AbsVector2.Col<A, Q> of(final A absX, final A absY)
128         {
129             Throw.whenNull(absX, "absX");
130             Throw.whenNull(absY, "absY");
131             Throw.when(!absX.getReference().equals(absY.getReference()), IllegalArgumentException.class,
132                     "absX.reference != absY.reference");
133             return new AbsVector2.Col<>(Vector2.Col.of(absX.getQuantity(), absY.getQuantity()), absX.getReference());
134         }
135 
136         /**
137          * Create a AbsVector2.Col without needing generics.
138          * @param dataInUnit the vector entries expressed as an array in the unit
139          * @param unit the unit of the data, which will also be used as the display unit
140          * @param reference the reference point for the absolute quantities
141          * @return a new AbsVector2.Col with a unit
142          * @param <A> the absolute quantity type
143          * @param <Q> the quantity type
144          * @param <R> the reference type
145          */
146         public static <A extends AbsQuantity<A, Q, R>, Q extends Quantity<Q>,
147                 R extends Reference<R, A, Q>> AbsVector2.Col<A, Q> of(final double[] dataInUnit, final UnitInterface<Q> unit,
148                         final R reference)
149         {
150             return new AbsVector2.Col<>(Vector2.Col.of(dataInUnit, unit), reference);
151         }
152 
153         /**
154          * Create a AbsVector2.Col without needing generics.
155          * @param dataSi the vector entries expressed as an array in the SI units
156          * @param displayUnit the display unit to use
157          * @param reference the reference point for the absolute quantities
158          * @return a new AbsVector2.Col with a unit
159          * @param <A> the absolute quantity type
160          * @param <Q> the quantity type
161          * @param <R> the reference type
162          */
163         public static <A extends AbsQuantity<A, Q, R>, Q extends Quantity<Q>,
164                 R extends Reference<R, A, Q>> AbsVector2.Col<A, Q> ofSi(final double[] dataSi, final UnitInterface<Q> displayUnit,
165                         final R reference)
166         {
167             return new AbsVector2.Col<>(Vector2.Col.ofSi(dataSi, displayUnit), reference);
168         }
169 
170         /**
171          * Create a AbsVector2.Col without needing generics.
172          * @param xSi the x vector entry expressed in the SI unit
173          * @param ySi the y vector entry expressed in the SI unit
174          * @param displayUnit the display unit to use
175          * @param reference the reference point for the absolute quantities
176          * @return a new AbsVector2.Col with a unit
177          * @param <A> the absolute quantity type
178          * @param <Q> the quantity type
179          * @param <R> the reference type
180          */
181         public static <A extends AbsQuantity<A, Q, R>, Q extends Quantity<Q>,
182                 R extends Reference<R, A, Q>> AbsVector2.Col<A, Q> ofSi(final double xSi, final double ySi,
183                         final UnitInterface<Q> displayUnit, final R reference)
184         {
185             return new AbsVector2.Col<>(Vector2.Col.ofSi(xSi, ySi, displayUnit), reference);
186         }
187 
188         /**
189          * Create a AbsVector2.Col without needing generics. The display unit will be taken from the first quantity in the
190          * array.
191          * @param data the vector entries expressed as an array of quantities
192          * @param reference the reference point for the absolute quantities
193          * @return a new AbsVector2.Col with a unit
194          * @param <A> the absolute quantity type
195          * @param <Q> the quantity type
196          * @param <R> the reference type
197          */
198         public static <A extends AbsQuantity<A, Q, R>, Q extends Quantity<Q>,
199                 R extends Reference<R, A, Q>> AbsVector2.Col<A, Q> of(final Q[] data, final R reference)
200         {
201             return new AbsVector2.Col<>(Vector2.Col.of(data), reference);
202         }
203 
204         /**
205          * Create an AbsVector2.Col without needing generics.
206          * @param absData the {x, y} value expressed as an array of absolute quantities
207          * @return a new AbsVector2.Col with a unit
208          * @param <A> the absolute quantity type
209          * @param <Q> the quantity type
210          * @param <R> the reference type
211          */
212         public static <A extends AbsQuantity<A, Q, R>, Q extends Quantity<Q>,
213                 R extends Reference<R, A, Q>> AbsVector2.Col<A, Q> of(final A[] absData)
214         {
215             Throw.whenNull(absData, "absData");
216             Throw.when(absData.length != 2, IllegalArgumentException.class, "absData.length != 2");
217             return AbsVector2.Col.of(absData[0], absData[1]);
218         }
219 
220         /**
221          * Create an AbsVector2.Col without needing generics.
222          * @param relativeVector the relative vector
223          * @param reference the reference point for the absolute quantities
224          * @return a new AbsVector2.Col with a unit
225          * @param <A> the absolute quantity type
226          * @param <Q> the quantity type
227          * @param <R> the reference type
228          */
229         public static <A extends AbsQuantity<A, Q, R>, Q extends Quantity<Q>,
230                 R extends Reference<R, A, Q>> AbsVector2.Col<A, Q> of(final Vector2.Col<Q> relativeVector, final R reference)
231         {
232             Throw.whenNull(relativeVector, "relativeVector");
233             return new AbsVector2.Col<>(relativeVector, reference);
234         }
235 
236     }
237 
238     /**
239      * Row vector for AbsVector2 with absolute quantities.
240      * <p>
241      * Copyright (c) 2026-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved.
242      * See for project information <a href="https://djunits.org" target="_blank">https://djunits.org</a>. The DJUNITS project is
243      * distributed under a <a href="https://djunits.org/docs/license.html" target="_blank">three-clause BSD-style license</a>.
244      * @author Alexander Verbraeck
245      * @param <A> the absolute quantity type
246      * @param <Q> the corresponding relative quantity type
247      */
248     public static class Row<A extends AbsQuantity<A, Q, ?>, Q extends Quantity<Q>>
249             extends AbsVector2<A, Q, AbsVector2.Row<A, Q>, Vector2.Row<Q>, AbsVector2.Col<A, Q>>
250             implements AbsVector.Row<AbsVector2.Row<A, Q>, Q>
251     {
252         /** */
253         private static final long serialVersionUID = 600L;
254 
255         /**
256          * Create a new AbsVector2 with absolute quantities, with a display unit and a reference point.
257          * @param relativeVector the vector with values relative to the reference point
258          * @param reference the reference point for the absolute values
259          */
260         public Row(final Vector2.Row<Q> relativeVector, final Reference<?, A, Q> reference)
261         {
262             super(relativeVector, reference);
263         }
264 
265         @Override
266         public AbsVector2.Row<A, Q> instantiate(final Vector2.Row<Q> relativeVector, final Reference<?, A, Q> reference)
267         {
268             return new AbsVector2.Row<>(relativeVector, reference);
269         }
270 
271         @Override
272         public AbsVector2.Col<A, Q> transpose()
273         {
274             return new AbsVector2.Col<>(getRelativeVecMat().transpose(), getReference());
275         }
276 
277         // ------------------------------------------ OF METHODS ------------------------------------------
278 
279         /**
280          * Create a AbsVector2.Row without needing generics.
281          * @param xInUnit the x-value expressed in the unit
282          * @param yInUnit the y-value expressed in the unit
283          * @param unit the unit of the data, which will also be used as the display unit
284          * @param reference the reference point for the absolute quantities
285          * @return a new AbsVector2.Row with a unit
286          * @param <A> the absolute quantity type
287          * @param <Q> the quantity type
288          * @param <R> the reference type
289          */
290         public static <A extends AbsQuantity<A, Q, R>, Q extends Quantity<Q>,
291                 R extends Reference<R, A, Q>> AbsVector2.Row<A, Q> of(final double xInUnit, final double yInUnit,
292                         final UnitInterface<Q> unit, final R reference)
293         {
294             return new AbsVector2.Row<>(Vector2.Row.of(xInUnit, yInUnit, unit), reference);
295         }
296 
297         /**
298          * Create a AbsVector2.Row without needing generics. The display unit will be taken from the x-quantity.
299          * @param x the x-value expressed as a quantity
300          * @param y the y-value expressed as a quantity
301          * @param reference the reference point for the absolute quantities
302          * @return a new AbsVector2.Row with a unit
303          * @param <A> the absolute quantity type
304          * @param <Q> the quantity type
305          * @param <R> the reference type
306          */
307         public static <A extends AbsQuantity<A, Q, R>, Q extends Quantity<Q>,
308                 R extends Reference<R, A, Q>> AbsVector2.Row<A, Q> of(final Q x, final Q y, final R reference)
309         {
310             return new AbsVector2.Row<>(Vector2.Row.of(x, y), reference);
311         }
312 
313         /**
314          * Create an AbsVector2.Row without needing generics.
315          * @param absX the v1-value expressed as an absolute quantity
316          * @param absY the v2-value expressed as an absolute quantity
317          * @return a new AbsVector2.Row with a unit
318          * @param <A> the absolute quantity type
319          * @param <Q> the quantity type
320          * @param <R> the reference type
321          */
322         public static <A extends AbsQuantity<A, Q, R>, Q extends Quantity<Q>,
323                 R extends Reference<R, A, Q>> AbsVector2.Row<A, Q> of(final A absX, final A absY)
324         {
325             Throw.whenNull(absX, "absX");
326             Throw.whenNull(absY, "absY");
327             Throw.when(!absX.getReference().equals(absY.getReference()), IllegalArgumentException.class,
328                     "absX.reference != absY.reference");
329             return new AbsVector2.Row<>(Vector2.Row.of(absX.getQuantity(), absY.getQuantity()), absX.getReference());
330         }
331 
332         /**
333          * Create a AbsVector2.Row without needing generics.
334          * @param dataInUnit the vector entries expressed as an array in the unit
335          * @param unit the unit of the data, which will also be used as the display unit
336          * @param reference the reference point for the absolute quantities
337          * @return a new AbsVector2.Row with a unit
338          * @param <A> the absolute quantity type
339          * @param <Q> the quantity type
340          * @param <R> the reference type
341          */
342         public static <A extends AbsQuantity<A, Q, R>, Q extends Quantity<Q>,
343                 R extends Reference<R, A, Q>> AbsVector2.Row<A, Q> of(final double[] dataInUnit, final UnitInterface<Q> unit,
344                         final R reference)
345         {
346             return new AbsVector2.Row<>(Vector2.Row.of(dataInUnit, unit), reference);
347         }
348 
349         /**
350          * Create a AbsVector2.Row without needing generics.
351          * @param dataSi the vector entries expressed as an array in the SI units
352          * @param displayUnit the display unit to use
353          * @param reference the reference point for the absolute quantities
354          * @return a new AbsVector2.Row with a unit
355          * @param <A> the absolute quantity type
356          * @param <Q> the quantity type
357          * @param <R> the reference type
358          */
359         public static <A extends AbsQuantity<A, Q, R>, Q extends Quantity<Q>,
360                 R extends Reference<R, A, Q>> AbsVector2.Row<A, Q> ofSi(final double[] dataSi, final UnitInterface<Q> displayUnit,
361                         final R reference)
362         {
363             return new AbsVector2.Row<>(Vector2.Row.ofSi(dataSi, displayUnit), reference);
364         }
365 
366         /**
367          * Create a AbsVector2.Row without needing generics.
368          * @param xSi the x vector entry expressed in the SI unit
369          * @param ySi the y vector entry expressed in the SI unit
370          * @param displayUnit the display unit to use
371          * @param reference the reference point for the absolute quantities
372          * @return a new AbsVector2.Row with a unit
373          * @param <A> the absolute quantity type
374          * @param <Q> the quantity type
375          * @param <R> the reference type
376          */
377         public static <A extends AbsQuantity<A, Q, R>, Q extends Quantity<Q>,
378                 R extends Reference<R, A, Q>> AbsVector2.Row<A, Q> ofSi(final double xSi, final double ySi,
379                         final UnitInterface<Q> displayUnit, final R reference)
380         {
381             return new AbsVector2.Row<>(Vector2.Row.ofSi(xSi, ySi, displayUnit), reference);
382         }
383 
384         /**
385          * Create a AbsVector2.Row without needing generics. The display unit will be taken from the first quantity in the
386          * array.
387          * @param data the vector entries expressed as an array of quantities
388          * @param reference the reference point for the absolute quantities
389          * @return a new AbsVector2.Row with a unit
390          * @param <A> the absolute quantity type
391          * @param <Q> the quantity type
392          * @param <R> the reference type
393          */
394         public static <A extends AbsQuantity<A, Q, R>, Q extends Quantity<Q>,
395                 R extends Reference<R, A, Q>> AbsVector2.Row<A, Q> of(final Q[] data, final R reference)
396         {
397             return new AbsVector2.Row<>(Vector2.Row.of(data), reference);
398         }
399 
400         /**
401          * Create an AbsVector2.Row without needing generics.
402          * @param absData the {x, y} value expressed as an array of absolute quantities
403          * @return a new AbsVector2.Row with a unit
404          * @param <A> the absolute quantity type
405          * @param <Q> the quantity type
406          * @param <R> the reference type
407          */
408         public static <A extends AbsQuantity<A, Q, R>, Q extends Quantity<Q>,
409                 R extends Reference<R, A, Q>> AbsVector2.Row<A, Q> of(final A[] absData)
410         {
411             Throw.whenNull(absData, "absData");
412             Throw.when(absData.length != 2, IllegalArgumentException.class, "absData.length != 2");
413             return AbsVector2.Row.of(absData[0], absData[1]);
414         }
415 
416         /**
417          * Create an AbsVector2.Row without needing generics.
418          * @param relativeVector the relative vector
419          * @param reference the reference point for the absolute quantities
420          * @return a new AbsVector2.Row with a unit
421          * @param <A> the absolute quantity type
422          * @param <Q> the quantity type
423          * @param <R> the reference type
424          */
425         public static <A extends AbsQuantity<A, Q, R>, Q extends Quantity<Q>,
426                 R extends Reference<R, A, Q>> AbsVector2.Row<A, Q> of(final Vector2.Row<Q> relativeVector, final R reference)
427         {
428             Throw.whenNull(relativeVector, "relativeVector");
429             return new AbsVector2.Row<>(relativeVector, reference);
430         }
431     }
432 
433 }