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
11
12
13
14
15
16
17
18
19
20 public class FloatMatrixDataDense extends FloatMatrixData
21 {
22
23 private static final long serialVersionUID = 1L;
24
25
26
27
28
29
30
31
32 public FloatMatrixDataDense(final float[] matrixSI, final int rows, final int cols) throws ValueException
33 {
34 super(StorageType.DENSE);
35 if (rows * cols != matrixSI.length)
36 {
37 throw new ValueException("FloatMatrixDataDense constructor, rows * cols != matrixSI.length");
38 }
39 this.matrixSI = new float[matrixSI.length];
40 System.arraycopy(matrixSI, 0, this.matrixSI, 0, matrixSI.length);
41 this.rows = rows;
42 this.cols = cols;
43 }
44
45
46
47
48
49
50 public FloatMatrixDataDense(final float[][] matrixSI) throws ValueException
51 {
52 super(StorageType.DENSE);
53 if (matrixSI == null || matrixSI.length == 0)
54 {
55 throw new ValueException("FloatMatrixDataDense constructor, matrixSI == null || matrixSI.length == 0");
56 }
57 this.rows = matrixSI.length;
58 this.cols = matrixSI[0].length;
59 this.matrixSI = new float[this.rows * this.cols];
60 for (int r = 0; r < this.rows; r++)
61 {
62 float[] row = matrixSI[r];
63 if (row.length != this.cols)
64 {
65 throw new ValueException("FloatMatrixDataDense constructor, ragged matrix");
66 }
67 System.arraycopy(row, 0, this.matrixSI, r * this.cols, row.length);
68 }
69 }
70
71
72
73
74 public final void assign(final FloatFunction floatFunction)
75 {
76 IntStream.range(0, this.rows() * this.cols()).parallel()
77 .forEach(i -> this.matrixSI[i] = floatFunction.apply(this.matrixSI[i]));
78 }
79
80
81 @Override
82 public final FloatMatrixDataSparse toSparse()
83 {
84 int length = cardinality();
85 float[] sparseSI = new float[length];
86 long[] indices = new long[length];
87 int count = 0;
88 for (int r = 0; r < this.rows; r++)
89 {
90 for (int c = 0; c < this.cols; c++)
91 {
92 int index = r * this.cols + c;
93 if (this.matrixSI[index] != 0.0)
94 {
95 sparseSI[count] = this.matrixSI[index];
96 indices[count] = index;
97 count++;
98 }
99 }
100 }
101 return new FloatMatrixDataSparse(sparseSI, indices, this.matrixSI.length, this.rows, this.cols);
102 }
103
104
105 @Override
106 public final float getSI(final int row, final int col)
107 {
108 return this.matrixSI[row * this.cols + col];
109 }
110
111
112 @Override
113 public final void setSI(final int row, final int col, final float valueSI)
114 {
115 this.matrixSI[row * this.cols + col] = valueSI;
116 }
117
118
119 @Override
120 public final float[][] getDenseMatrixSI()
121 {
122 float[][] matrix = new float[this.rows][];
123 for (int r = 0; r < this.rows; r++)
124 {
125 float[] row = new float[this.cols];
126 System.arraycopy(this.matrixSI, r * this.cols, row, 0, row.length);
127 matrix[r] = row;
128 }
129 return matrix;
130 }
131
132
133 @Override
134 public final double[][] getDoubleDenseMatrixSI()
135 {
136 double[][] matrix = new double[this.rows][];
137 for (int r = 0; r < this.rows; r++)
138 {
139 double[] row = new double[this.cols];
140 int offset = r * this.cols;
141 for (int c = 0; c < this.cols; c++)
142 {
143 row[c] = this.matrixSI[offset++];
144 }
145 matrix[r] = row;
146 }
147 return matrix;
148 }
149
150
151 @Override
152 public final FloatMatrixDataDense copy()
153 {
154 try
155 {
156 return new FloatMatrixDataDense(getDenseMatrixSI());
157 }
158 catch (ValueException exception)
159 {
160 throw new RuntimeException(exception);
161 }
162 }
163
164
165 @Override
166 public final void incrementBy(final FloatMatrixData right) throws ValueException
167 {
168 IntStream.range(0, this.rows).parallel().forEach(
169 r -> IntStream.range(0, this.cols).forEach(c -> this.matrixSI[r * this.cols + c] += right.getSI(r, c)));
170 }
171
172
173 @Override
174 public final void decrementBy(final FloatMatrixData right) throws ValueException
175 {
176 IntStream.range(0, this.rows).parallel().forEach(
177 r -> IntStream.range(0, this.cols).forEach(c -> this.matrixSI[r * this.cols + c] -= right.getSI(r, c)));
178 }
179
180
181 @Override
182 public final void multiplyBy(final FloatMatrixData right) throws ValueException
183 {
184 IntStream.range(0, this.rows).parallel().forEach(
185 r -> IntStream.range(0, this.cols).forEach(c -> this.matrixSI[r * this.cols + c] *= right.getSI(r, c)));
186 }
187
188
189 @Override
190 public final void divideBy(final FloatMatrixData right) throws ValueException
191 {
192 IntStream.range(0, this.rows).parallel().forEach(
193 r -> IntStream.range(0, this.cols).forEach(c -> this.matrixSI[r * this.cols + c] /= right.getSI(r, c)));
194 }
195 }