1 package org.djunits.vecmat.def;
2
3 import org.djunits.formatter.MatrixFormat;
4 import org.djunits.formatter.MatrixFormatter;
5 import org.djunits.quantity.SIQuantity;
6 import org.djunits.quantity.def.Quantity;
7 import org.djunits.unit.Unit;
8 import org.djunits.unit.si.SIUnit;
9 import org.djunits.util.MatrixMath;
10 import org.djunits.vecmat.dnxm.MatrixNxM;
11 import org.djunits.vecmat.storage.DenseDoubleDataSi;
12 import org.djunits.vecmat.storage.DenseFloatDataSi;
13
14 /**
15 * Matrix contains a number of standard operations on matrices of relative quantities.
16 * <p>
17 * Copyright (c) 2025-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
18 * for project information <a href="https://djunits.org" target="_blank">https://djunits.org</a>. The DJUNITS project is
19 * distributed under a <a href="https://djunits.org/docs/license.html" target="_blank">three-clause BSD-style license</a>.
20 * @author Alexander Verbraeck
21 * @param <Q> the quantity type
22 * @param <M> the 'SELF' matrix type
23 * @param <SI> the matrix type with generics <SIQuantity, SIUnit<
24 * @param <H> the generic matrix type with generics <?, ?< for Hadamard operations
25 * @param <MT> the type of the transposed version of the matrix
26 */
27 public abstract class Matrix<Q extends Quantity<Q>, M extends Matrix<Q, M, SI, H, MT>,
28 SI extends Matrix<SIQuantity, SI, ?, ?, ?>, H extends Matrix<?, ?, ?, ?, ?>, MT extends Matrix<Q, MT, ?, ?, M>>
29 extends Table<Q, M, SI, H, MT>
30 {
31 /** */
32 private static final long serialVersionUID = 600L;
33
34 /**
35 * Create a new matrix with a unit.
36 * @param displayUnit the display unit to use
37 */
38 public Matrix(final Unit<?, Q> displayUnit)
39 {
40 super(displayUnit);
41 }
42
43 /**
44 * Multiply this matrix with a MatrixNxM, resulting in a MatrixNxM. The multiplication is a (NxM) x (MxP) matrix
45 * multiplication resulting in an (NxP) matrix.
46 * @param matrix the matrix to multiply with
47 * @return a MatrixNxM of an SIQuantity as the result of the matrix multiplication
48 * @throws IllegalArgumentException when the number of columns of this matrix does not equal the number of rows of the
49 * matrix to multiply with
50 */
51 public MatrixNxM<SIQuantity> multiply(final MatrixNxM<?> matrix)
52 {
53 checkMultiply(matrix);
54 double[] result = MatrixMath.multiply(unsafeSiArray(), matrix.unsafeSiArray(), rows(), cols(), matrix.cols());
55 SIUnit siUnit = getDisplayUnit().siUnit().plus(matrix.getDisplayUnit().siUnit());
56 if (matrix.getDataGrid().isDouble())
57 {
58 return new MatrixNxM<SIQuantity>(new DenseDoubleDataSi(result, rows(), matrix.cols()), siUnit);
59 }
60 return new MatrixNxM<SIQuantity>(new DenseFloatDataSi(result, rows(), matrix.cols()), siUnit);
61 }
62
63 /* *********************************************************************************/
64 /* ************************** STRING AND FORMATTING METHODS ************************/
65 /* *********************************************************************************/
66
67 /**
68 * Concise description of this matrix.
69 * @return a String with the matrix, with the unit attached.
70 */
71 @Override
72 public String format()
73 {
74 return format(MatrixFormat.defaults());
75 }
76
77 /**
78 * String representation of this matrix after applying the format.
79 * @param format the format to apply for the matrix
80 * @return a String representation of this matrix, formatted according to the given format
81 */
82 public String format(final MatrixFormat format)
83 {
84 return MatrixFormatter.format(this, format);
85 }
86
87 /**
88 * String representation of this matrix, expressed in the specified unit.
89 * @param targetUnit the unit into which the values of the matrix are converted for display
90 * @return printable string with the matrix's values expressed in the specified unit
91 */
92 @Override
93 public String format(final Unit<?, Q> targetUnit)
94 {
95 return format(MatrixFormat.defaults().setDisplayUnit(targetUnit));
96 }
97
98 }