View Javadoc
1   package org.djunits.value.vdouble.matrix;
2   
3   import java.util.stream.IntStream;
4   
5   import org.djunits.value.StorageType;
6   import org.djunits.value.ValueException;
7   import org.djunits.value.vdouble.DoubleFunction;
8   
9   /**
10   * Stores dense data for a DoubleMatrix and carries out basic operations.
11   * <p>
12   * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
13   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
14   * </p>
15   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
16   * initial version Oct 3, 2015 <br>
17   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
18   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
19   */
20  public class DoubleMatrixDataDense extends DoubleMatrixData
21  {
22      /**
23       * Create a matrix with dense data.
24       * @param matrixSI the data to store
25       * @param rows the number of rows
26       * @param cols the number of columns
27       * @throws ValueException in case <tt>rows * cols != matrixSI.length</tt>
28       */
29      public DoubleMatrixDataDense(final double[] matrixSI, final int rows, final int cols) throws ValueException
30      {
31          super(StorageType.DENSE);
32          if (rows * cols != matrixSI.length)
33          {
34              throw new ValueException("DoubleMatrixDataDense constructor, rows * cols != matrixSI.length");
35          }
36          this.matrixSI = new double[matrixSI.length];
37          System.arraycopy(matrixSI, 0, this.matrixSI, 0, matrixSI.length);
38          this.rows = rows;
39          this.cols = cols;
40      }
41  
42      /**
43       * Create a matrix with dense data.
44       * @param matrixSI the data to store
45       * @throws ValueException in case matrix is ragged
46       */
47      public DoubleMatrixDataDense(final double[][] matrixSI) throws ValueException
48      {
49          super(StorageType.DENSE);
50          if (matrixSI == null || matrixSI.length == 0)
51          {
52              throw new ValueException("DoubleMatrixDataDense constructor, matrixSI == null || matrixSI.length == 0");
53          }
54          this.rows = matrixSI.length;
55          this.cols = matrixSI[0].length;
56          this.matrixSI = new double[this.rows * this.cols];
57          for (int r = 0; r < this.rows; r++)
58          {
59              double[] row = matrixSI[r];
60              if (row.length != this.cols)
61              {
62                  throw new ValueException("DoubleMatrixDataDense constructor, ragged matrix");
63              }
64              System.arraycopy(row, 0, this.matrixSI, r * this.cols, row.length);
65          }
66      }
67  
68      /**
69       * @param doubleFunction the function to apply on the (mutable) data elements
70       */
71      public final void assign(final DoubleFunction doubleFunction)
72      {
73          IntStream.range(0, this.rows() * this.cols()).parallel()
74                  .forEach(i -> this.matrixSI[i] = doubleFunction.apply(this.matrixSI[i]));
75      }
76  
77      /** {@inheritDoc} */
78      @Override
79      public final DoubleMatrixDataSparse toSparse()
80      {
81          int length = cardinality();
82          double[] sparseSI = new double[length];
83          long[] indices = new long[length];
84          int count = 0;
85          for (int r = 0; r < this.rows; r++)
86          {
87              for (int c = 0; c < this.cols; c++)
88              {
89                  int index = r * this.cols + c;
90                  if (this.matrixSI[index] != 0.0)
91                  {
92                      sparseSI[count] = this.matrixSI[index];
93                      indices[count] = index;
94                      count++;
95                  }
96              }
97          }
98          return new DoubleMatrixDataSparse(sparseSI, indices, this.matrixSI.length, this.rows, this.cols);
99      }
100 
101     /** {@inheritDoc} */
102     @Override
103     public final double getSI(final int row, final int col)
104     {
105         return this.matrixSI[row * this.cols + col];
106     }
107 
108     /** {@inheritDoc} */
109     @Override
110     public final void setSI(final int row, final int col, final double valueSI)
111     {
112         this.matrixSI[row * this.cols + col] = valueSI;
113     }
114 
115     /** {@inheritDoc} */
116     @Override
117     public final double[][] getDenseMatrixSI()
118     {
119         double[][] matrix = new double[this.rows][];
120         for (int r = 0; r < this.rows; r++)
121         {
122             double[] row = new double[this.cols];
123             System.arraycopy(this.matrixSI, r * this.cols, row, 0, row.length);
124             matrix[r] = row;
125         }
126         return matrix;
127     }
128 
129     /** {@inheritDoc} */
130     @Override
131     public final DoubleMatrixDataDense copy()
132     {
133         try
134         {
135             return new DoubleMatrixDataDense(getDenseMatrixSI());
136         }
137         catch (ValueException exception)
138         {
139             throw new RuntimeException(exception); // should not happen -- original is not ragged...
140         }
141     }
142 
143     /** {@inheritDoc} */
144     @Override
145     public final void incrementBy(final DoubleMatrixData right) throws ValueException
146     {
147         IntStream.range(0, this.rows).parallel().forEach(
148                 r -> IntStream.range(0, this.cols).forEach(c -> this.matrixSI[r * this.cols + c] += right.getSI(r, c)));
149     }
150 
151     /** {@inheritDoc} */
152     @Override
153     public final void decrementBy(final DoubleMatrixData right) throws ValueException
154     {
155         IntStream.range(0, this.rows).parallel().forEach(
156                 r -> IntStream.range(0, this.cols).forEach(c -> this.matrixSI[r * this.cols + c] -= right.getSI(r, c)));
157     }
158 
159     /** {@inheritDoc} */
160     @Override
161     public final void multiplyBy(final DoubleMatrixData right) throws ValueException
162     {
163         IntStream.range(0, this.rows).parallel().forEach(
164                 r -> IntStream.range(0, this.cols).forEach(c -> this.matrixSI[r * this.cols + c] *= right.getSI(r, c)));
165     }
166 
167     /** {@inheritDoc} */
168     @Override
169     public final void divideBy(final DoubleMatrixData right) throws ValueException
170     {
171         IntStream.range(0, this.rows).parallel().forEach(
172                 r -> IntStream.range(0, this.cols).forEach(c -> this.matrixSI[r * this.cols + c] /= right.getSI(r, c)));
173     }
174 }