View Javadoc
1   package org.djunits.vecmat.def;
2   
3   import java.util.Arrays;
4   
5   import org.djunits.quantity.SIQuantity;
6   import org.djunits.quantity.def.Quantity;
7   import org.djunits.unit.UnitInterface;
8   import org.djunits.unit.si.SIUnit;
9   import org.djutils.exceptions.Throw;
10  
11  /**
12   * SquareDenseMatrix implements the core functions for a matrix with n x n real-valued entries. The data is stored in a dense
13   * double[] field. The matrix is immutable, except for the display unit, which can be changed.
14   * <p>
15   * Copyright (c) 2025-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 <Q> the quantity type
20   * @param <U> the unit type
21   * @param <M> the 'SELF' square dense matrix type
22   * @param <SI> the square dense matrix type with generics &lt;SIQuantity, SIUnit&lt;
23   * @param <H> the generic square dense matrix type with generics &lt;?, ?&lt; for Hadamard operations
24   */
25  public abstract class SquareDenseMatrix<Q extends Quantity<Q, U>, U extends UnitInterface<U, Q>,
26          M extends SquareDenseMatrix<Q, U, M, SI, H>, SI extends SquareDenseMatrix<SIQuantity, SIUnit, SI, ?, ?>,
27          H extends SquareDenseMatrix<?, ?, ?, ?, ?>> extends SquareMatrix<Q, U, M, SI, H>
28  {
29      /** */
30      private static final long serialVersionUID = 600L;
31  
32      /** The n x n values in si-units. */
33      private final double[] dataSi;
34  
35      /** the order (n in n x n) of the matrix. */
36      private final int order;
37  
38      /**
39       * Create a new SquareDenseMatrix with a unit.
40       * @param dataInUnit the matrix values {a11, a12, ..., a21, a22, ...} expressed in the display unit
41       * @param displayUnit the display unit to use
42       * @param order the order of the square matrix (number of rows/columns)
43       */
44      protected SquareDenseMatrix(final double[] dataInUnit, final U displayUnit, final int order)
45      {
46          super(displayUnit);
47          Throw.when(dataInUnit.length != order * order, IllegalArgumentException.class,
48                  "SquareDenseMatrix initialized with %d values instead of %d", dataInUnit.length, order * order);
49          this.dataSi = new double[dataInUnit.length];
50          for (int i = 0; i < this.dataSi.length; i++)
51          {
52              this.dataSi[i] = displayUnit.toBaseValue(dataInUnit[i]);
53          }
54          this.order = order;
55      }
56  
57      @Override
58      public double[] si()
59      {
60          return this.dataSi;
61      }
62  
63      @Override
64      public double si(final int row, final int col)
65      {
66          checkRow(row);
67          checkCol(col);
68          return this.dataSi[this.order * row + col];
69      }
70  
71      @Override
72      public int rows()
73      {
74          return this.order;
75      }
76  
77      @Override
78      public int cols()
79      {
80          return this.order;
81      }
82  
83      @Override
84      public double[] getRowSi(final int row)
85      {
86          checkRow(row);
87          double[] vSi = new double[this.order];
88          for (int col = 0; col < this.order; col++)
89          {
90              vSi[col] = this.dataSi[this.order * row + col];
91          }
92          return vSi;
93      }
94  
95      @Override
96      public double[] getColumnSi(final int col)
97      {
98          checkCol(col);
99          double[] vSi = new double[this.order];
100         for (int row = 0; row < this.order; row++)
101         {
102             vSi[row] = this.dataSi[this.order * row + col];
103         }
104         return vSi;
105     }
106 
107     @Override
108     public int hashCode()
109     {
110         final int prime = 31;
111         int result = 1;
112         result = prime * result + Arrays.hashCode(this.dataSi);
113         return result;
114     }
115 
116     @SuppressWarnings("checkstyle:needbraces")
117     @Override
118     public boolean equals(final Object obj)
119     {
120         if (this == obj)
121             return true;
122         if (obj == null)
123             return false;
124         if (getClass() != obj.getClass())
125             return false;
126         SquareDenseMatrix<?, ?, ?, ?, ?> other = (SquareDenseMatrix<?, ?, ?, ?, ?>) obj;
127         return Arrays.equals(this.dataSi, other.dataSi);
128     }
129 
130 }