7
7
import mklab .JGNN .core .matrix .AccessRow ;
8
8
import mklab .JGNN .core .matrix .AccessCol ;
9
9
import mklab .JGNN .core .matrix .DenseMatrix ;
10
- import mklab .JGNN .core .matrix .SparseMatrix ;
11
10
import mklab .JGNN .core .matrix .TransposedMatrix ;
12
11
import mklab .JGNN .core .tensor .DenseTensor ;
13
- import mklab .JGNN .core .tensor .SparseTensor ;
14
12
15
13
import java .util .Map .Entry ;
16
14
@@ -27,38 +25,85 @@ public abstract class Matrix extends Tensor {
27
25
private long rows ;
28
26
private long cols ;
29
27
30
- public Matrix (long rows , long cols ) {
31
- super (rows *cols );
28
+ protected Matrix (long rows , long cols ) {
29
+ init (rows *cols );
32
30
this .rows = rows ;
33
31
this .cols = cols ;
34
32
}
35
33
34
+ /**
35
+ * Retrieves an iterable that traverses (row, col) entry pairs
36
+ * of non zero entries.
37
+ * @return An Entry iterable.
38
+ * @see #getNonZeroElements()
39
+ */
36
40
public abstract Iterable <Entry <Long , Long >> getNonZeroEntries ();
37
-
38
- protected Matrix () {
39
- }
40
-
41
+
42
+ /**
43
+ * Creates a Matrix with the same class and dimensions and all element set to zero.
44
+ * @return A Matrix with the same class and dimensions.
45
+ * @see #zeroCopy(long, long)
46
+ */
41
47
@ Override
42
48
public final Matrix zeroCopy () {
43
49
return zeroCopy (rows , cols );
44
50
}
45
-
51
+ /**
52
+ * Creates a Matrix with the same class and dimensions and all element set to zero. This
53
+ * checks that the copy has a total number of elements equal to the given size.
54
+ * @param size The desired size of the matrix.
55
+ * @return A Matrix with the same class and dimensions.
56
+ * @throws RuntimeException If the desired
57
+ * @see #zeroCopy(long, long)
58
+ */
59
+ @ Override
60
+ public final Tensor zeroCopy (long size ) {
61
+ if (size !=size ())
62
+ throw new RuntimeException ("Desired atrix size " +size +" can only be equal to rows " +rows +" * " +cols );
63
+ return zeroCopy (rows , cols );
64
+ }
65
+ /**
66
+ * Creates a matrix of the same class and all element set to zero, but with
67
+ * a given number of rows and columns.
68
+ * @param row The number of rows of the matrix.
69
+ * @param cols The number of columns of the matrix.
70
+ * @return A Matrix of the same class.
71
+ * @see #zeroCopy()
72
+ */
46
73
public abstract Matrix zeroCopy (long rows , long cols );
47
-
74
+ /**
75
+ * Retrieves the number of rows of a matrix.
76
+ * @return The number of rows.
77
+ */
48
78
public final long getRows () {
49
79
return rows ;
50
80
}
51
-
81
+ /**
82
+ * Retrieves the number of columns of a matrix.
83
+ * @return The number of columns.
84
+ */
52
85
public final long getCols () {
53
86
return cols ;
54
87
}
55
-
88
+ /**
89
+ * Retrieves the values stored at matrix elements.
90
+ * @param row The element's row.
91
+ * @param col The element's column.
92
+ * @return The value corresponding t element (row, col).
93
+ */
56
94
public final double get (long row , long col ) {
57
95
if (row <0 || col <0 || row >=rows || col >=cols )
58
96
throw new IllegalArgumentException ("Element out of range (" +row +"," +col +") for " +describe ());
59
97
return get (row +col *rows );
60
98
}
61
-
99
+
100
+ /**
101
+ * Stores values at matrix elements.
102
+ * @param row The element's row.
103
+ * @param col The element's column.
104
+ * @param value The value to store.
105
+ * @return <code>this</code> Matrix instance.
106
+ */
62
107
public final Matrix put (long row , long col , double value ) {
63
108
put (row +col *rows , value );
64
109
return this ;
@@ -210,11 +255,19 @@ public Matrix onesMask() {
210
255
}
211
256
return ones ;
212
257
}
213
-
258
+ /**
259
+ * Creates a copy of the Matrix that holds its normalized Laplacian transformation.
260
+ * @return A new Matrix of the same dimensions.
261
+ * @see #setToLaplacian()
262
+ */
214
263
public Matrix laplacian () {
215
264
return ((Matrix )copy ()).setToLaplacian ();
216
265
}
217
-
266
+ /**
267
+ * Sets the Matrix to its normalized Laplacian transformation by appropriately adjusting its element values.
268
+ * @return <code>this</code> Matrix instance.
269
+ * @see #laplacian()
270
+ */
218
271
public Matrix setToLaplacian () {
219
272
HashMap <Long , Double > outDegrees = new HashMap <Long , Double >();
220
273
HashMap <Long , Double > inDegrees = new HashMap <Long , Double >();
@@ -251,7 +304,8 @@ public Tensor getRow(long row) {
251
304
* also edits the original matrix.
252
305
* No new memory is allocated for matrix values.
253
306
* @param col The given column.
254
- * @return A {@link AccessCol} of the corresponding row.
307
+ * @return A {@link AccessCol} of the corresponding column.
308
+ * @see #accessColumns()
255
309
*/
256
310
public Tensor getCol (long col ) {
257
311
return new AccessCol (this , col );
@@ -272,15 +326,42 @@ public String toString() {
272
326
return res .toString ();
273
327
}
274
328
275
- public final static Matrix fromSparseColumns (List <Tensor > tensors ) {
329
+ /* public final static Matrix fromColumns (List<Tensor> tensors) {
276
330
Matrix ret = new SparseMatrix(tensors.get(0).size(), tensors.size());
277
331
for(int col=0;col<tensors.size();col++)
278
332
for(long row : tensors.get(col).getNonZeroElements())
279
333
ret.put(row, col, tensors.get(col).get(row));
280
334
return ret;
335
+ }*/
336
+ /**
337
+ * Organizes matrix rows to a list of tensors that share entries.
338
+ * This operation does not allocate memory for matrix elements and editing
339
+ * tensor elements edits the original matrix's elements.
340
+ * @return A list of {@link AccessRow}.
341
+ * @see #getCol(long)
342
+ * @see #accessColumns()
343
+ */
344
+ public final List <Tensor > accessRows () {
345
+ List <Tensor > ret = new ArrayList <Tensor >();
346
+ for (long row =0 ;row <getRows ();row ++)
347
+ ret .add (getRow (row ));
348
+ return ret ;
281
349
}
282
-
283
- public final List <Tensor > toSparseColumns () {
350
+ /**
351
+ * Organizes matrix columns to a list of tensors that share entries.
352
+ * This operation does not allocate memory for matrix elements and editing
353
+ * tensor elements edits the original matrix's elements.
354
+ * @return A list of {@link AccessCol}.
355
+ * @see #getCol(long)
356
+ * @see #acceRows()
357
+ */
358
+ public final List <Tensor > accessColumns () {
359
+ List <Tensor > ret = new ArrayList <Tensor >();
360
+ for (long col =0 ;col <getCols ();col ++)
361
+ ret .add (getCol (col ));
362
+ return ret ;
363
+ }
364
+ /*public final List<Tensor> toSparseColumns() {
284
365
List<Tensor> ret = new ArrayList<Tensor>();
285
366
for(long col=0;col<getCols();col++)
286
367
ret.add(new SparseTensor(getRows()));
@@ -290,7 +371,7 @@ public final List<Tensor> toSparseColumns() {
290
371
ret.get((int)col).put(row, get(row, col));
291
372
}
292
373
return ret;
293
- }
374
+ }*/
294
375
295
376
/**
296
377
* Converts a given value to a JGNN-compatible 1x1 matrix.
0 commit comments