1 package org.djunits.value.vfloat.matrix.data;
2
3 import java.io.Serializable;
4 import java.util.Arrays;
5 import java.util.Collection;
6 import java.util.stream.IntStream;
7
8 import org.djunits.unit.Unit;
9 import org.djunits.unit.scale.Scale;
10 import org.djunits.value.ValueRuntimeException;
11 import org.djunits.value.storage.Storage;
12 import org.djunits.value.storage.StorageType;
13 import org.djunits.value.vfloat.function.FloatFunction;
14 import org.djunits.value.vfloat.function.FloatFunction2;
15 import org.djunits.value.vfloat.matrix.base.FloatSparseValue;
16 import org.djunits.value.vfloat.scalar.base.FloatScalar;
17 import org.djutils.exceptions.Throw;
18
19
20
21
22
23
24
25
26
27
28 public abstract class FloatMatrixData extends Storage<FloatMatrixData> implements Serializable
29 {
30
31 private static final long serialVersionUID = 1L;
32
33
34 @SuppressWarnings("checkstyle:visibilitymodifier")
35 protected float[] matrixSI;
36
37
38 @SuppressWarnings("checkstyle:visibilitymodifier")
39 protected int rows;
40
41
42 @SuppressWarnings("checkstyle:visibilitymodifier")
43 protected int cols;
44
45
46
47
48
49 public FloatMatrixData(final StorageType storageType)
50 {
51 super(storageType);
52 }
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68 public static FloatMatrixData instantiate(final float[][] values, final Scale scale, final StorageType storageType)
69 throws ValueRuntimeException
70 {
71 Throw.whenNull(scale, "FloatMatrixData.instantiate: scale is null");
72 Throw.whenNull(storageType, "FloatMatrixData.instantiate: storageType is null");
73 checkRectangularAndNonNull(values);
74
75 int rows = values.length;
76 final int cols = rows == 0 ? 0 : values[0].length;
77 if (cols == 0)
78 {
79 rows = 0;
80 }
81
82 switch (storageType)
83 {
84 case DENSE:
85 float[] valuesSI = new float[rows * cols];
86 IntStream.range(0, values.length).parallel().forEach(r -> IntStream.range(0, cols)
87 .forEach(c -> valuesSI[r * cols + c] = (float) scale.toStandardUnit(values[r][c])));
88 return new FloatMatrixDataDense(valuesSI, rows, cols);
89
90 case SPARSE:
91 return FloatMatrixDataSparse.instantiate(values, scale);
92
93 default:
94 throw new ValueRuntimeException("Unknown storage type in FloatMatrixData.instantiate: " + storageType);
95 }
96 }
97
98
99
100
101
102
103
104
105
106
107
108
109
110 public static <U extends Unit<U>, S extends FloatScalar<U, S>> FloatMatrixData instantiate(
111 final Collection<FloatSparseValue<U, S>> values, final int rows, final int cols, final StorageType storageType)
112 throws NullPointerException
113 {
114 Throw.whenNull(values, "FloatMatrixData.instantiate: values is null");
115 Throw.whenNull(storageType, "FloatMatrixData.instantiate: storageType is null");
116 Throw.when(cols < 0, ValueRuntimeException.class, "cols must be >= 0");
117 Throw.when(rows < 0, ValueRuntimeException.class, "rows must be >= 0");
118 for (FloatSparseValue<U, S> fsp : values)
119 {
120 Throw.whenNull(fsp, "null value in values");
121 Throw.when(fsp.getRow() < 0 || fsp.getRow() >= rows, ValueRuntimeException.class, "row out of range");
122 Throw.when(fsp.getColumn() < 0 || fsp.getColumn() >= cols, ValueRuntimeException.class, "column out of range");
123 }
124
125 switch (storageType)
126 {
127 case DENSE:
128 float[] valuesSI = new float[rows * cols];
129 values.stream().parallel().forEach(v -> valuesSI[v.getRow() * cols + v.getColumn()] = v.getValueSI());
130 return new FloatMatrixDataDense(valuesSI, rows, cols);
131
132 case SPARSE:
133 return new FloatMatrixDataSparse(values, rows, cols);
134
135 default:
136 throw new ValueRuntimeException("Unknown storage type in FloatMatrixData.instantiate: " + storageType);
137 }
138 }
139
140
141
142
143
144
145
146
147
148
149
150
151 public static <U extends Unit<U>, S extends FloatScalar<U, S>> FloatMatrixData instantiate(final S[][] values,
152 final StorageType storageType) throws ValueRuntimeException
153 {
154 Throw.whenNull(storageType, "FloatMatrixData.instantiate: storageType is null");
155 checkRectangularAndNonNull(values);
156
157 int rows = values.length;
158 final int cols = rows == 0 ? 0 : values[0].length;
159 if (cols == 0)
160 {
161 rows = 0;
162 }
163
164 switch (storageType)
165 {
166 case DENSE:
167 float[] valuesSI = new float[rows * cols];
168 IntStream.range(0, rows).parallel()
169 .forEach(r -> IntStream.range(0, cols).forEach(c -> valuesSI[r * cols + c] = values[r][c].getSI()));
170 return new FloatMatrixDataDense(valuesSI, rows, cols);
171
172 case SPARSE:
173 float[][] matrixSI = new float[rows][cols];
174 IntStream.range(0, values.length).parallel()
175 .forEach(r -> IntStream.range(0, cols).forEach(c -> matrixSI[r][c] = values[r][c].getSI()));
176 return FloatMatrixDataSparse.instantiate(matrixSI);
177
178 default:
179 throw new ValueRuntimeException("Unknown storage type in FloatMatrixData.instantiate: " + storageType);
180 }
181 }
182
183
184
185
186
187
188
189
190
191 public int rows()
192 {
193 return this.rows;
194 }
195
196
197
198
199
200 public int cols()
201 {
202 return this.cols;
203 }
204
205
206
207
208
209 public abstract FloatMatrixDataDense toDense();
210
211
212
213
214
215 public abstract FloatMatrixDataSparse toSparse();
216
217
218
219
220
221
222
223 public abstract float getSI(int row, int col);
224
225
226
227
228
229
230
231 public abstract void setSI(int row, int col, float valueSI);
232
233
234
235
236
237 public final float zSum()
238 {
239
240 return (float) IntStream.range(0, this.matrixSI.length).parallel().mapToDouble(i -> this.matrixSI[i]).sum();
241 }
242
243
244
245
246
247
248 public abstract float[][] getDenseMatrixSI();
249
250
251
252
253
254
255 public abstract double[][] getDoubleDenseMatrixSI();
256
257
258
259
260
261
262
263
264 protected static float[][] checkRectangularAndNonNull(final float[][] values) throws ValueRuntimeException
265 {
266 Throw.when(null == values, NullPointerException.class, "Cannot create a matrix from a null float[][]");
267 for (int row = 0; row < values.length; row++)
268 {
269 Throw.when(null == values[row], ValueRuntimeException.class,
270 "Cannot create a matrix from float[][] containing null row(s)");
271 Throw.when(values[row].length != values[0].length, ValueRuntimeException.class,
272 "Cannot create a matrix from a jagged float[][]");
273 }
274 return values;
275 }
276
277
278
279
280
281
282
283
284
285
286 protected static <U extends Unit<U>, S extends FloatScalar<U, S>> S[][] checkRectangularAndNonNull(
287 final S[][] values) throws ValueRuntimeException
288 {
289 Throw.when(null == values, NullPointerException.class, "Cannot create a matrix from a null Scalar[][]");
290 for (int row = 0; row < values.length; row++)
291 {
292 Throw.when(null == values[row], ValueRuntimeException.class,
293 "Cannot create a matrix from Scalar[][] containing null row(s)");
294 Throw.when(values[row].length != values[0].length, ValueRuntimeException.class,
295 "Cannot create a matrix from a jagged Scalar[][]");
296 for (int col = 0; col < values[row].length; col++)
297 {
298 Throw.whenNull(values[row][col], "Cannot create a matrix from Scalar[][] containing null(s)");
299 }
300 }
301 return values;
302 }
303
304
305
306
307
308
309 protected void checkSizes(final FloatMatrixData other) throws ValueRuntimeException
310 {
311 if (this.rows() != other.rows() || this.cols() != other.cols())
312 {
313 throw new ValueRuntimeException("Two data objects used in a FloatMatrix operation do not have the same size");
314 }
315 }
316
317
318
319
320
321
322
323
324
325
326 public abstract FloatMatrixData assign(FloatFunction doubleFunction);
327
328
329
330
331
332
333
334
335 abstract FloatMatrixData assign(FloatFunction2 floatFunction, FloatMatrixData right) throws ValueRuntimeException;
336
337
338
339
340
341
342
343
344 public abstract FloatMatrixData plus(FloatMatrixData right) throws ValueRuntimeException;
345
346
347
348
349
350
351
352 public final FloatMatrixData incrementBy(final FloatMatrixData right) throws ValueRuntimeException
353 {
354 return assign(new FloatFunction2()
355 {
356 @Override
357 public float apply(final float leftValue, final float rightValue)
358 {
359 return leftValue + rightValue;
360 }
361 }, right);
362 }
363
364
365
366
367
368
369
370
371 public abstract FloatMatrixData minus(FloatMatrixData right) throws ValueRuntimeException;
372
373
374
375
376
377
378
379 public final FloatMatrixData decrementBy(final FloatMatrixData decrement) throws ValueRuntimeException
380 {
381 return assign(new FloatFunction2()
382 {
383 @Override
384 public float apply(final float leftValue, final float rightValue)
385 {
386 return leftValue - rightValue;
387 }
388 }, decrement);
389 }
390
391
392
393
394
395
396
397
398 public abstract FloatMatrixData times(FloatMatrixData right) throws ValueRuntimeException;
399
400
401
402
403
404
405
406
407 public final FloatMatrixData multiplyBy(final FloatMatrixData right) throws ValueRuntimeException
408 {
409 return assign(new FloatFunction2()
410 {
411 @Override
412 public float apply(final float leftValue, final float rightValue)
413 {
414 return leftValue * rightValue;
415 }
416 }, right);
417 }
418
419
420
421
422
423
424
425
426 public abstract FloatMatrixData divide(FloatMatrixData right) throws ValueRuntimeException;
427
428
429
430
431
432
433
434
435 public final FloatMatrixData divideBy(final FloatMatrixData right) throws ValueRuntimeException
436 {
437 return assign(new FloatFunction2()
438 {
439 @Override
440 public float apply(final float leftValue, final float rightValue)
441 {
442 return leftValue / rightValue;
443 }
444 }, right);
445 }
446
447
448
449
450
451 @Override
452 public int hashCode()
453 {
454 final int prime = 31;
455 int result = 1;
456 result = prime * result + this.rows;
457 result = prime * result + this.cols;
458 for (int row = 0; row < this.rows; row++)
459 {
460 for (int col = 0; col < this.cols; col++)
461 {
462 result = 31 * result + Float.floatToIntBits(getSI(row, col));
463 }
464 }
465 return result;
466 }
467
468
469
470
471
472
473
474 protected boolean compareDenseMatrixWithSparseMatrix(final FloatMatrixDataDense dm, final FloatMatrixDataSparse sm)
475 {
476 for (int row = 0; row < dm.rows; row++)
477 {
478 for (int col = 0; col < dm.cols; col++)
479 {
480 if (dm.getSI(row, col) != sm.getSI(row, col))
481 {
482 return false;
483 }
484 }
485 }
486 return true;
487 }
488
489 @Override
490 @SuppressWarnings("checkstyle:needbraces")
491 public boolean equals(final Object obj)
492 {
493 if (this == obj)
494 return true;
495 if (obj == null)
496 return false;
497 if (!(obj instanceof FloatMatrixData))
498 return false;
499 FloatMatrixData other = (FloatMatrixData) obj;
500 if (this.rows != other.rows)
501 return false;
502 if (this.cols != other.cols)
503 return false;
504 if (other instanceof FloatMatrixDataSparse && this instanceof FloatMatrixDataDense)
505 {
506 return compareDenseMatrixWithSparseMatrix((FloatMatrixDataDense) this, (FloatMatrixDataSparse) other);
507 }
508 else if (other instanceof FloatMatrixDataDense && this instanceof FloatMatrixDataSparse)
509 {
510 return compareDenseMatrixWithSparseMatrix((FloatMatrixDataDense) other, (FloatMatrixDataSparse) this);
511 }
512
513 return Arrays.equals(this.matrixSI, other.matrixSI);
514 }
515
516 @Override
517 public String toString()
518 {
519 return "FloatMatrixData [storageType=" + getStorageType() + ", matrixSI=" + Arrays.toString(this.matrixSI) + "]";
520 }
521
522 }