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
11
12
13
14
15
16
17
18
19
20 public class DoubleMatrixDataDense extends DoubleMatrixData
21 {
22
23
24
25
26
27
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
44
45
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
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
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
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
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
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
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);
140 }
141 }
142
143
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
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
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
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 }