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
24
25
26
27
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
44
45
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
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
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
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
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
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
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
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);
158 }
159 }
160
161
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
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
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
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 }