Package org.djunits.vecmat.def
Class VectorMatrix<Q extends Quantity<Q>,VM extends VectorMatrix<Q,VM,SI,H,VMT>,SI extends VectorMatrix<SIQuantity,SI,?,?,?>,H extends VectorMatrix<?,?,?,?,?>,VMT extends VectorMatrix<Q,VMT,?,?,VM>>
java.lang.Object
org.djunits.vecmat.def.VectorMatrix<Q,VM,SI,H,VMT>
- Type Parameters:
Q- the quantity typeVM- the 'SELF' vector or matrix typeSI- the vector or matrix type with generics <SIQuantity, SIUnit<H- the generic vector or matrix type with generics <?, ?< for Hadamard operationsVMT- the type of the transposed version of the vector or matrix
- All Implemented Interfaces:
Serializable,Additive<VM>,Scalable<VM>,Value<VM,,Q> Hadamard<H,SI>
public abstract class VectorMatrix<Q extends Quantity<Q>,VM extends VectorMatrix<Q,VM,SI,H,VMT>,SI extends VectorMatrix<SIQuantity,SI,?,?,?>,H extends VectorMatrix<?,?,?,?,?>,VMT extends VectorMatrix<Q,VMT,?,?,VM>>
extends Object
implements Value<VM,Q>, Scalable<VM>, Additive<VM>, Hadamard<H,SI>
VectorMatrix contains a number of standard operations on vectors and matrices of relative quantities.
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 Summary
ConstructorsConstructorDescriptionVectorMatrix(Unit<?, Q> displayUnit) Create a new vector or matrix with a unit. -
Method Summary
Modifier and TypeMethodDescriptionabs()Make the SI-values of this element absolute.Return the a vector or matrix with entries that contain the sum of the element and the increment.Add an element to this element, based on the SI-values.Convert this vector, matrix or table to aMatrix1x1.Convert this vector, matrix or table to aMatrix2x2.Convert this vector, matrix or table to aMatrix3x3.Convert this vector or matrix to aMatrixNxM.Convert this vector, matrix or table to aMatrixNxN.Convert this vector or matrix to aQuantityTable.Return this vector, matrix or table as a 1-element column vector.Return this vector, matrix or table as a 2-element column vector.Return this vector, matrix or table as a 2-element row vector.Return this vector, matrix or table as a 3-element column vector.Return this vector, matrix or table as a 3-element row vector.Convert this vector, matrix or table to an N-element column vector.Convert this vector, matrix or table to an N-element row vector.protected voidcheckMultiply(Matrix<?, ?, ?, ?, ?> matrix) Check if the multiplication with the other matrix is valid.protected voidcheckMultiply(Vector<?, ?, ?, ?, ?> vector) Check if the multiplication with the other matrix is valid.abstract intcols()Return the number of columns.divideElements(Dimensionless quantity) Multiply the entries of this vector, matrix or table by the given quantity.divideEntries(H other) Divide the entries of this vector, matrix or table on an entry-by-entry basis with those of another collection of the same type (but possibly representing another quantity).Retrieve the unit of this Value.abstract double[]Return a row-major array of SI-values for this matrix or vector.abstract VMinstantiateSi(double[] siNew) Return a new vector or matrix with the given SI or BASE values.abstract SIinstantiateSi(double[] siNew, SIUnit siUnit) Return a new vector or matrix in SI-units with the given SI or BASE values.Invert the vector, matrix or table on an entry-by-entry basis.booleanIndicate whether this is a Relative Value.max()Return the maximum value of the entries of the vector or matrix.mean()Return the mean value of the entries of the vector or matrix.median()Return the median value of the entries of the vector or matrix.min()Return the minimum value of the entries of the vector or matrix.multiplyElements(Dimensionless quantity) Multiply the entries of this vector, matrix or table by the given quantity.multiplyEntries(H other) Multiply the entries of this vector, matrix or table on an entry-by-entry basis with those of another collection of the same type (but possibly representing another quantity).multiplyEntries(Quantity<?> quantity) Multiply the entries of this vector, matrix or table by the given quantity.negate()Negate this element, based on the SI-values.intnnz()Return the number of non-zero entries in the vector, matrix or table.abstract intReturn the number of non-zero entries in the vector, matrix or table.abstract introws()Return the number of rows.scaleBy(double factor) Scale the element by a dimensionless factor, based on the SI-values.setDisplayUnit(Unit<?, Q> newUnit) Set a new display unit for the value.Return the a vector or matrix with entries that contain the value minus the decrement.Subtract an element from this element, based on the SI-values.sum()Return the sum of the values of the entries of the vector or matrix.toString()abstract VMTReturn a transposed vector or matrix, where rows and columns have been swapped.abstract double[]Return a row-major possibly UNSAFE array of SI-values for this matrix or vector.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.djunits.vecmat.operations.Hadamard
divideEntriesMethods inherited from interface org.djunits.value.Value
format, format, isAbsolute, setDisplayUnit
-
Constructor Details
-
VectorMatrix
Create a new vector or matrix with a unit.- Parameters:
displayUnit- the display unit to use
-
-
Method Details
-
getDisplayUnit
Description copied from interface:ValueRetrieve the unit of this Value. -
setDisplayUnit
Description copied from interface:ValueSet a new display unit for the value. Internally, the value will not changed since it is stored in a base unit. -
rows
public abstract int rows()Return the number of rows.- Returns:
- the number of rows
-
cols
public abstract int cols()Return the number of columns.- Returns:
- the number of columns
-
instantiateSi
Return a new vector or matrix with the given SI or BASE values.- 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
-
instantiateSi
Return a new vector or matrix in SI-units with the given SI or BASE values.- Parameters:
siNew- the values for the new vector or matrix in row-major formatsiUnit- the new unit for the new vector or matrix- Returns:
- a new matrix with the provided SI or BASE values
-
getSiArray
public abstract double[] getSiArray()Return a row-major array of SI-values for this matrix or vector. This is guaranteed to be a safe copy.- Returns:
- the row-major array of SI-values (safe copy)
-
unsafeSiArray
public abstract double[] unsafeSiArray()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.- Returns:
- the row-major array of SI-values (safe copy)
-
transpose
Return a transposed vector or matrix, where rows and columns have been swapped.- Returns:
- a transposed vector or matrix, where rows and columns have been swapped
-
isRelative
public boolean isRelative()Description copied from interface:ValueIndicate whether this is a Relative Value. -
mean
Return the mean value of the entries of the vector or matrix.- Returns:
- the mean value of the entries of the vector or matrix
-
min
Return the minimum value of the entries of the vector or matrix.- Returns:
- the minimum value of the entries of the vector or matrix
-
max
Return the maximum value of the entries of the vector or matrix.- Returns:
- the maximum value of the entries of the vector or matrix
-
median
Return the median value of the entries of the vector or matrix.- Returns:
- the median value of the entries of the vector or matrix
-
sum
Return the sum of the values of the entries of the vector or matrix.- Returns:
- the sum of the values of the entries of the vector or matrix
-
add
Return the a vector or matrix with entries that contain the sum of the element and the increment.- 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
Return the a vector or matrix with entries that contain the value minus the decrement.- 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
-
add
Description copied from interface:AdditiveAdd 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. -
subtract
Description copied from interface:AdditiveSubtract an element from this element, based on the SI-values. -
negate
Description copied from interface:AdditiveNegate 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. -
abs
Description copied from interface:AdditiveMake 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. -
scaleBy
Description copied from interface:ScalableScale 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. -
nonZeroCount
public abstract int nonZeroCount()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.- Returns:
- the number of non-zero entries in the vector, matrix or table
-
nnz
public int nnz()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. the acronym 'nnz' stands for 'number of non-zero entries'.- Returns:
- the number of non-zero entries in the vector, matrix or table
-
multiplyElements
Multiply the entries of this vector, matrix or table by the given quantity. This is actually a Hadamard operation, but since it is equivalent to a scaleBy operation, it is included in this interface.- Parameters:
quantity- the scalar quantity to multiply by- Returns:
- a vector, matrix or table where the entries have been multiplied by the given quantity
-
divideElements
Multiply the entries of this vector, matrix or table by the given quantity. This is actually a Hadamard operation, but since it is equivalent to a scaleBy operation, it is included in this interface.- Parameters:
quantity- the scalar quantity to multiply by- Returns:
- a vector, matrix or table where the entries have been multiplied by the given quantity
-
invertEntries
Description copied from interface:HadamardInvert the vector, matrix or table on an entry-by-entry basis. -
multiplyEntries
Description copied from interface:HadamardMultiply the entries of this vector, matrix or table on an entry-by-entry basis with those of another collection of the same type (but possibly representing another quantity). -
divideEntries
Description copied from interface:HadamardDivide the entries of this vector, matrix or table on an entry-by-entry basis with those of another collection of the same type (but possibly representing another quantity). -
multiplyEntries
Description copied from interface:HadamardMultiply the entries of this vector, matrix or table by the given quantity. -
asMatrixNxM
Convert this vector or matrix to aMatrixNxM. The underlying data MIGHT be shared between this object and the MatrixNxM.- Returns:
- a
MatrixNxNwith identical SI data and display unit
-
asQuantityTable
Convert this vector or matrix to aQuantityTable. The underlying data MIGHT be shared between this object and the quantity table.- Returns:
- a
QuantityTablewith identical SI data and display unit
-
asVector1
Return this vector, matrix or table as a 1-element column vector. Shape must be 1 x 1.- Returns:
- a
Vector1with identical SI data and display unit - Throws:
IllegalStateException- if shape is not 1 x 1
-
asVector2Col
Return this vector, matrix or table as a 2-element column vector. Shape must be 2 x 1.- Returns:
- a
Vector2.Colwith identical SI data and display unit - Throws:
IllegalStateException- if shape is not 2 x 1
-
asVector3Col
Return this vector, matrix or table as a 3-element column vector. Shape must be 3 x 1.- Returns:
- a
Vector3.Colwith identical SI data and display unit - Throws:
IllegalStateException- if shape is not 3 x 1
-
asVectorNCol
Convert this vector, matrix or table to an N-element column vector. Shape must be N x 1. The underlying data MIGHT be shared between this object and the VectorN.Col.- Returns:
- a
VectorN.Colwith identical SI data and display unit - Throws:
IllegalStateException- ifcols() != 1
-
asVector2Row
Return this vector, matrix or table as a 2-element row vector. Shape must be 1 x 2.- Returns:
- a
Vector2.Rowwith identical SI data and display unit - Throws:
IllegalStateException- if shape is not 1 x 2
-
asVector3Row
Return this vector, matrix or table as a 3-element row vector. Shape must be 1 x 3.- Returns:
- a
Vector3.Rowwith identical SI data and display unit - Throws:
IllegalStateException- if shape is not 1 x 3
-
asVectorNRow
Convert this vector, matrix or table to an N-element row vector. Shape must be 1 x N. The underlying data MIGHT be shared between this object and the VectorN.Row.- Returns:
- a
VectorN.Rowwith identical SI data and display unit - Throws:
IllegalStateException- ifrows() != 1
-
asMatrix1x1
Convert this vector, matrix or table to aMatrix1x1. The shape must be 1 x 1.- Returns:
- a
Matrix1x1with identical SI data and display unit - Throws:
IllegalStateException- if this matrix is not 1 x 1
-
asMatrix2x2
Convert this vector, matrix or table to aMatrix2x2. The shape must be 2 x 2.- Returns:
- a
Matrix2x2with identical SI data and display unit - Throws:
IllegalStateException- if this matrix is not 2 x 2
-
asMatrix3x3
Convert this vector, matrix or table to aMatrix3x3. The shape must be 3 x 3.- Returns:
- a
Matrix3x3with identical SI data and display unit - Throws:
IllegalStateException- if this matrix is not 3 x 3
-
asMatrixNxN
Convert this vector, matrix or table to aMatrixNxN. The shape must be square. The underlying data MIGHT be shared between this object and the MatrixNxN.- Returns:
- a
MatrixNxNwith identical SI data and display unit - Throws:
IllegalStateException- if this matrix is not square
-
checkMultiply
Check if the multiplication with the other matrix is valid. A valid matrix multiplication is (M x N) x (N x P).- Parameters:
matrix- the other matrix- Throws:
IllegalArgumentException- when this.cols() != other.rows()
-
checkMultiply
Check if the multiplication with the other matrix is valid. A valid matrix multiplication is (M x N) x (N x P).- Parameters:
vector- the other matrix- Throws:
IllegalArgumentException- when this.cols() != other.rows()
-
toString
-