View Javadoc
1   package org.djunits.value.vfloat.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.vfloat.FloatFunction;
8   
9   /**
10   * Stores dense data for a FloatMatrix 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 FloatMatrixDataDense extends FloatMatrixData
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 FloatMatrixDataDense(final float[] 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("FloatMatrixDataDense constructor, rows * cols != matrixSI.length");
35          }
36          this.matrixSI = new float[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 FloatMatrixDataDense(final float[][] matrixSI) throws ValueException
48      {
49          super(StorageType.DENSE);
50          if (matrixSI == null || matrixSI.length == 0)
51          {
52              throw new ValueException("FloatMatrixDataDense constructor, matrixSI == null || matrixSI.length == 0");
53          }
54          this.rows = matrixSI.length;
55          this.cols = matrixSI[0].length;
56          this.matrixSI = new float[this.rows * this.cols];
57          for (int r = 0; r < this.rows; r++)
58          {
59              float[] row = matrixSI[r];
60              if (row.length != this.cols)
61              {
62                  throw new ValueException("FloatMatrixDataDense constructor, ragged matrix");
63              }
64              System.arraycopy(row, 0, this.matrixSI, r * this.cols, row.length);
65          }
66      }
67  
68      /**
69       * @param floatFunction the function to apply on the (mutable) data elements
70       */
71      public final void assign(final FloatFunction floatFunction)
72      {
73          IntStream.range(0, this.rows() * this.cols()).parallel()
74                  .forEach(i -> this.matrixSI[i] = floatFunction.apply(this.matrixSI[i]));
75      }
76  
77      /** {@inheritDoc} */
78      @Override
79      public final FloatMatrixDataSparse toSparse()
80      {
81          int length = cardinality();
82          float[] sparseSI = new float[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 FloatMatrixDataSparse(sparseSI, indices, this.matrixSI.length, this.rows, this.cols);
99      }
100 
101     /** {@inheritDoc} */
102     @Override
103     public final float 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 float valueSI)
111     {
112         this.matrixSI[row * this.cols + col] = valueSI;
113     }
114 
115     /** {@inheritDoc} */
116     @Override
117     public final float[][] getDenseMatrixSI()
118     {
119         float[][] matrix = new float[this.rows][];
120         for (int r = 0; r < this.rows; r++)
121         {
122             float[] row = new float[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 double[][] getDoubleDenseMatrixSI()
132     {
133         double[][] matrix = new double[this.rows][];
134         for (int r = 0; r < this.rows; r++)
135         {
136             double[] row = new double[this.cols];
137             int offset = r * this.cols;
138             for (int c = 0; c < this.cols; c++)
139             {
140                 row[c] = this.matrixSI[offset++];
141             }
142             matrix[r] = row;
143         }
144         return matrix;
145     }
146 
147     /** {@inheritDoc} */
148     @Override
149     public final FloatMatrixDataDense copy()
150     {
151         try
152         {
153             return new FloatMatrixDataDense(getDenseMatrixSI());
154         }
155         catch (ValueException exception)
156         {
157             throw new RuntimeException(exception); // should not happen -- original is not ragged...
158         }
159     }
160 
161     /** {@inheritDoc} */
162     @Override
163     public final void incrementBy(final FloatMatrixData right) throws ValueException
164     {
165         IntStream.range(0, this.rows).parallel().forEach(
166                 r -> IntStream.range(0, this.cols).forEach(c -> this.matrixSI[r * this.cols + c] += right.getSI(r, c)));
167     }
168 
169     /** {@inheritDoc} */
170     @Override
171     public final void decrementBy(final FloatMatrixData right) throws ValueException
172     {
173         IntStream.range(0, this.rows).parallel().forEach(
174                 r -> IntStream.range(0, this.cols).forEach(c -> this.matrixSI[r * this.cols + c] -= right.getSI(r, c)));
175     }
176 
177     /** {@inheritDoc} */
178     @Override
179     public final void multiplyBy(final FloatMatrixData right) throws ValueException
180     {
181         IntStream.range(0, this.rows).parallel().forEach(
182                 r -> IntStream.range(0, this.cols).forEach(c -> this.matrixSI[r * this.cols + c] *= right.getSI(r, c)));
183     }
184 
185     /** {@inheritDoc} */
186     @Override
187     public final void divideBy(final FloatMatrixData right) throws ValueException
188     {
189         IntStream.range(0, this.rows).parallel().forEach(
190                 r -> IntStream.range(0, this.cols).forEach(c -> this.matrixSI[r * this.cols + c] /= right.getSI(r, c)));
191     }
192 }