Class Vector2<Q extends Quantity<Q>,V extends Vector2<Q,V,SI,H,VT>,SI extends Vector2<SIQuantity,SI,?,?,?>,H extends Vector2<?,?,?,?,?>,VT extends Vector2<Q,VT,?,?,V>>

java.lang.Object
org.djunits.vecmat.def.VectorMatrix<Q,V,SI,H,VT>
org.djunits.vecmat.def.Vector<Q,V,SI,H,VT>
org.djunits.vecmat.d2.Vector2<Q,V,SI,H,VT>
Type Parameters:
Q - the quantity type
V - the vector type (Row or Col)
SI - the vector type with generics <SIQuantity, SIUnit<
H - the generic vector type with generics <?, ?< for Hadamard operations
VT - the type of the transposed version of the vector
All Implemented Interfaces:
Serializable, Iterable<Q>, Additive<V>, Scalable<V>, Value<V,Q>, Hadamard<H,SI>, Normed<Q>
Direct Known Subclasses:
Vector2.Col, Vector2.Row

public abstract class Vector2<Q extends Quantity<Q>,V extends Vector2<Q,V,SI,H,VT>,SI extends Vector2<SIQuantity,SI,?,?,?>,H extends Vector2<?,?,?,?,?>,VT extends Vector2<Q,VT,?,?,V>> extends Vector<Q,V,SI,H,VT>
Vector2 implements a vector with two real-valued entries. The vector is immutable, except for the display unit, which can be changed. Many of the method that have been defined already for a generic vector have been re-implemented for efficiency.

Copyright (c) 2025-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See for project information https://djunits.org. The DJUNITS project is distributed under a three-clause BSD-style license.

Author:
Alexander Verbraeck
See Also:
  • Constructor Details

    • Vector2

      protected Vector2(double xSi, double ySi, Unit<?,Q> displayUnit)
      Create a new Vector2 with a display unit.
      Parameters:
      xSi - the x-value expressed in the SI unit
      ySi - the y-value expressed in the SI unit
      displayUnit - the display unit to use
  • Method Details

    • instantiateSi

      protected abstract V instantiateSi(double xSiNew, double ySiNew)
      Return a column or row vector with x and y values in SI or BASE units.
      Parameters:
      xSiNew - the x value in SI or BASE units
      ySiNew - the y value in SI or BASE units
      Returns:
      a new column or row vector with adapted x and y values
    • size

      public int size()
      Description copied from class: Vector
      Retrieve the size of the vector.
      Specified by:
      size in class Vector<Q extends Quantity<Q>,V extends Vector2<Q,V,SI,H,VT>,SI extends Vector2<SIQuantity,SI,?,?,?>,H extends Vector2<?,?,?,?,?>,VT extends Vector2<Q,VT,?,?,V>>
      Returns:
      the size of the vector
    • si

      public double si(int index) throws IndexOutOfBoundsException
      Description copied from class: Vector
      Retrieve an si-value from the vector.
      Specified by:
      si in class Vector<Q extends Quantity<Q>,V extends Vector2<Q,V,SI,H,VT>,SI extends Vector2<SIQuantity,SI,?,?,?>,H extends Vector2<?,?,?,?,?>,VT extends Vector2<Q,VT,?,?,V>>
      Parameters:
      index - the index (0-based) to retrieve the value from
      Returns:
      the value as a Scalar
      Throws:
      IndexOutOfBoundsException - in case index is out of bounds
    • iterator

      public Iterator<Q> iterator()
      Description copied from class: Vector
      Create and return an iterator over the scalars in this vector in proper sequence.
      Specified by:
      iterator in interface Iterable<Q extends Quantity<Q>>
      Specified by:
      iterator in class Vector<Q extends Quantity<Q>,V extends Vector2<Q,V,SI,H,VT>,SI extends Vector2<SIQuantity,SI,?,?,?>,H extends Vector2<?,?,?,?,?>,VT extends Vector2<Q,VT,?,?,V>>
      Returns:
      an iterator over the scalars in this vector in proper sequence
    • getScalarArray

      public Q[] getScalarArray()
      Description copied from class: Vector
      Return the vector as an array of scalars.
      Specified by:
      getScalarArray in class Vector<Q extends Quantity<Q>,V extends Vector2<Q,V,SI,H,VT>,SI extends Vector2<SIQuantity,SI,?,?,?>,H extends Vector2<?,?,?,?,?>,VT extends Vector2<Q,VT,?,?,V>>
      Returns:
      the vector as an array of scalars
    • instantiateSi

      public V instantiateSi(double[] siNew)
      Description copied from class: VectorMatrix
      Return a new vector or matrix with the given SI or BASE values.
      Specified by:
      instantiateSi in class VectorMatrix<Q extends Quantity<Q>,V extends Vector2<Q,V,SI,H,VT>,SI extends Vector2<SIQuantity,SI,?,?,?>,H extends Vector2<?,?,?,?,?>,VT extends Vector2<Q,VT,?,?,V>>
      Parameters:
      siNew - the values for the new vector or matrix in row-major format
      Returns:
      a new matrix with the provided SI or BASE values
    • xSi

      public double xSi()
      Return the x-value of the vector in SI or BASE units.
      Returns:
      the x-value of the vector in SI or BASE units
    • ySi

      public double ySi()
      Return the y-value of the vector in SI or BASE units.
      Returns:
      the y-value of the vector in SI or BASE units
    • nonZeroCount

      public int nonZeroCount()
      Description copied from class: VectorMatrix
      Return the number of non-zero entries in the vector, matrix or table. Note that NaN and Infinity count as a non-zero element. The value -0.0 counts as 0.0.
      Specified by:
      nonZeroCount in class VectorMatrix<Q extends Quantity<Q>,V extends Vector2<Q,V,SI,H,VT>,SI extends Vector2<SIQuantity,SI,?,?,?>,H extends Vector2<?,?,?,?,?>,VT extends Vector2<Q,VT,?,?,V>>
      Returns:
      the number of non-zero entries in the vector, matrix or table
    • unsafeSiArray

      public double[] unsafeSiArray()
      Description copied from class: VectorMatrix
      Return a row-major possibly UNSAFE array of SI-values for this matrix or vector. The method might give access to the underlying data structure, so treat the data carefully.
      Specified by:
      unsafeSiArray in class VectorMatrix<Q extends Quantity<Q>,V extends Vector2<Q,V,SI,H,VT>,SI extends Vector2<SIQuantity,SI,?,?,?>,H extends Vector2<?,?,?,?,?>,VT extends Vector2<Q,VT,?,?,V>>
      Returns:
      the row-major array of SI-values (safe copy)
    • getSiArray

      public double[] getSiArray()
      Description copied from class: VectorMatrix
      Return a row-major array of SI-values for this matrix or vector. This is guaranteed to be a safe copy.
      Specified by:
      getSiArray in class VectorMatrix<Q extends Quantity<Q>,V extends Vector2<Q,V,SI,H,VT>,SI extends Vector2<SIQuantity,SI,?,?,?>,H extends Vector2<?,?,?,?,?>,VT extends Vector2<Q,VT,?,?,V>>
      Returns:
      the row-major array of SI-values (safe copy)
    • x

      public Q x()
      Return the x-value of the vector as a quantity with the correct unit.
      Returns:
      the x-value of the vector as a quantity with the correct unit
    • y

      public Q y()
      Return the y-value of the vector as a quantity with the correct unit.
      Returns:
      the y-value of the vector as a quantity with the correct unit
    • scaleBy

      public V scaleBy(double factor)
      Description copied from interface: Scalable
      Scale the element by a dimensionless factor, based on the SI-values. Note that this can lead to unwanted effects for elements with Offset scales. 2 * 10 °C = 293.15 °C and not 20 °C.
      Specified by:
      scaleBy in interface Scalable<Q extends Quantity<Q>>
      Overrides:
      scaleBy in class VectorMatrix<Q extends Quantity<Q>,V extends Vector2<Q,V,SI,H,VT>,SI extends Vector2<SIQuantity,SI,?,?,?>,H extends Vector2<?,?,?,?,?>,VT extends Vector2<Q,VT,?,?,V>>
      Parameters:
      factor - the dimensionless scale factor
      Returns:
      a new element with all of its SI-values scaled by the dimensionless factor
    • add

      public V add(V other)
      Description copied from interface: Additive
      Add an element to this element, based on the SI-values. Note that this can lead to unwanted effects for elements with Offset scales: 10 °C + 5 °C = 288.15 °C and not 15 °C.
      Specified by:
      add in interface Additive<Q extends Quantity<Q>>
      Overrides:
      add in class VectorMatrix<Q extends Quantity<Q>,V extends Vector2<Q,V,SI,H,VT>,SI extends Vector2<SIQuantity,SI,?,?,?>,H extends Vector2<?,?,?,?,?>,VT extends Vector2<Q,VT,?,?,V>>
      Parameters:
      other - the element to add to this element
      Returns:
      the sum of the SI-values of this element and the SI-values of the other element
    • subtract

      public V subtract(V other)
      Description copied from interface: Additive
      Subtract an element from this element, based on the SI-values.
      Specified by:
      subtract in interface Additive<Q extends Quantity<Q>>
      Overrides:
      subtract in class VectorMatrix<Q extends Quantity<Q>,V extends Vector2<Q,V,SI,H,VT>,SI extends Vector2<SIQuantity,SI,?,?,?>,H extends Vector2<?,?,?,?,?>,VT extends Vector2<Q,VT,?,?,V>>
      Parameters:
      other - the element to subtract from this element
      Returns:
      a new element with the SI-values of this element minus the SI-values of the other element
    • negate

      public V negate()
      Description copied from interface: Additive
      Negate this element, based on the SI-values. Note that this can lead to unwanted effects for elements with Offset scales. A temperature in Celsius that is negated, will have its Kelvin temperatures negated.
      Specified by:
      negate in interface Additive<Q extends Quantity<Q>>
      Overrides:
      negate in class VectorMatrix<Q extends Quantity<Q>,V extends Vector2<Q,V,SI,H,VT>,SI extends Vector2<SIQuantity,SI,?,?,?>,H extends Vector2<?,?,?,?,?>,VT extends Vector2<Q,VT,?,?,V>>
      Returns:
      a new element with all of its SI-values negated
    • abs

      public V abs()
      Description copied from interface: Additive
      Make the SI-values of this element absolute. A new element wil be returned. Note that this can lead to unwanted effects for elements with Offset scales. A temperature of -10 degrees Celsius will have its Kelvin temperatures negated.
      Specified by:
      abs in interface Additive<Q extends Quantity<Q>>
      Overrides:
      abs in class VectorMatrix<Q extends Quantity<Q>,V extends Vector2<Q,V,SI,H,VT>,SI extends Vector2<SIQuantity,SI,?,?,?>,H extends Vector2<?,?,?,?,?>,VT extends Vector2<Q,VT,?,?,V>>
      Returns:
      a new element with the absolute value for all of its SI-values
    • normL1

      public Q normL1()
      Description copied from interface: Normed
      Return the L1-norm of this element, expressed as a quantity. The L1-norm is |x1| + |x2| + ... + |xn|.
      Returns:
      the L1-norm of this element, expressed as a quantity
    • normL2

      public Q normL2()
      Description copied from interface: Normed
      Return the L2-norm of this element, expressed as a quantity. The L2-norm is sqrt(x1^2 + x2^2 + ... + xn^2).
      Returns:
      the L2-norm of this element, expressed as a quantity
    • normLp

      public Q normLp(int p)
      Description copied from interface: Normed
      Return the Lp-norm of this element, expressed as a quantity. The Ln-norm is (x1^p + x2^p + ... + xn^p)^(1/p).
      Parameters:
      p - the rank of the norm
      Returns:
      the Lp-norm of this element, expressed as a quantity
    • normLinf

      public Q normLinf()
      Description copied from interface: Normed
      Return the L∞-norm of this element, expressed as a quantity. The L∞-norm is max(|x1|, |x2|, ..., |xn|).
      Returns:
      the L∞-norm of this element, expressed as a quantity
    • mean

      public Q mean()
      Description copied from class: VectorMatrix
      Return the mean value of the entries of the vector or matrix.
      Overrides:
      mean in class VectorMatrix<Q extends Quantity<Q>,V extends Vector2<Q,V,SI,H,VT>,SI extends Vector2<SIQuantity,SI,?,?,?>,H extends Vector2<?,?,?,?,?>,VT extends Vector2<Q,VT,?,?,V>>
      Returns:
      the mean value of the entries of the vector or matrix
    • min

      public Q min()
      Description copied from class: VectorMatrix
      Return the minimum value of the entries of the vector or matrix.
      Overrides:
      min in class VectorMatrix<Q extends Quantity<Q>,V extends Vector2<Q,V,SI,H,VT>,SI extends Vector2<SIQuantity,SI,?,?,?>,H extends Vector2<?,?,?,?,?>,VT extends Vector2<Q,VT,?,?,V>>
      Returns:
      the minimum value of the entries of the vector or matrix
    • max

      public Q max()
      Description copied from class: VectorMatrix
      Return the maximum value of the entries of the vector or matrix.
      Overrides:
      max in class VectorMatrix<Q extends Quantity<Q>,V extends Vector2<Q,V,SI,H,VT>,SI extends Vector2<SIQuantity,SI,?,?,?>,H extends Vector2<?,?,?,?,?>,VT extends Vector2<Q,VT,?,?,V>>
      Returns:
      the maximum value of the entries of the vector or matrix
    • median

      public Q median()
      Description copied from class: VectorMatrix
      Return the median value of the entries of the vector or matrix.
      Overrides:
      median in class VectorMatrix<Q extends Quantity<Q>,V extends Vector2<Q,V,SI,H,VT>,SI extends Vector2<SIQuantity,SI,?,?,?>,H extends Vector2<?,?,?,?,?>,VT extends Vector2<Q,VT,?,?,V>>
      Returns:
      the median value of the entries of the vector or matrix
    • sum

      public Q sum()
      Description copied from class: VectorMatrix
      Return the sum of the values of the entries of the vector or matrix.
      Overrides:
      sum in class VectorMatrix<Q extends Quantity<Q>,V extends Vector2<Q,V,SI,H,VT>,SI extends Vector2<SIQuantity,SI,?,?,?>,H extends Vector2<?,?,?,?,?>,VT extends Vector2<Q,VT,?,?,V>>
      Returns:
      the sum of the values of the entries of the vector or matrix
    • add

      public V add(Q increment)
      Description copied from class: VectorMatrix
      Return the a vector or matrix with entries that contain the sum of the element and the increment.
      Overrides:
      add in class VectorMatrix<Q extends Quantity<Q>,V extends Vector2<Q,V,SI,H,VT>,SI extends Vector2<SIQuantity,SI,?,?,?>,H extends Vector2<?,?,?,?,?>,VT extends Vector2<Q,VT,?,?,V>>
      Parameters:
      increment - the quantity by which to increase the values of the vector or matrix
      Returns:
      a vector or matrix with entries that are incremented by the given quantity
    • subtract

      public V subtract(Q decrement)
      Description copied from class: VectorMatrix
      Return the a vector or matrix with entries that contain the value minus the decrement.
      Overrides:
      subtract in class VectorMatrix<Q extends Quantity<Q>,V extends Vector2<Q,V,SI,H,VT>,SI extends Vector2<SIQuantity,SI,?,?,?>,H extends Vector2<?,?,?,?,?>,VT extends Vector2<Q,VT,?,?,V>>
      Parameters:
      decrement - the quantity by which to decrease the values of the vector or matrix
      Returns:
      a vector or matrix with entries that are decremented by the given quantity
    • isRelative

      public boolean isRelative()
      Description copied from interface: Value
      Indicate whether this is a Relative Value.
      Specified by:
      isRelative in interface Value<Q extends Quantity<Q>,V extends Vector2<Q,V,SI,H,VT>>
      Overrides:
      isRelative in class VectorMatrix<Q extends Quantity<Q>,V extends Vector2<Q,V,SI,H,VT>,SI extends Vector2<SIQuantity,SI,?,?,?>,H extends Vector2<?,?,?,?,?>,VT extends Vector2<Q,VT,?,?,V>>
      Returns:
      whether this is a Relative Value
    • isColumnVector

      public abstract boolean isColumnVector()
      Return whether this vector is a column vector.
      Specified by:
      isColumnVector in class Vector<Q extends Quantity<Q>,V extends Vector2<Q,V,SI,H,VT>,SI extends Vector2<SIQuantity,SI,?,?,?>,H extends Vector2<?,?,?,?,?>,VT extends Vector2<Q,VT,?,?,V>>
      Returns:
      whether this vector is a column vector
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object