This is an automated email from the ASF dual-hosted git repository.

baunsgaard pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/systemds.git


The following commit(s) were added to refs/heads/main by this push:
     new ce07e37  [MINOR] Improve sparse matrix printing
ce07e37 is described below

commit ce07e37231cecc2979e5e28c714fc1e19b612ef2
Author: baunsgaard <baunsga...@tugraz.at>
AuthorDate: Tue Nov 16 17:29:07 2021 +0100

    [MINOR] Improve sparse matrix printing
    
    This commit change the sparse printing to only print rows, that actually
    contain values. Also included is a small test of unary operations on
    dense, CSR, and MCSR.
    
    Closes #1458
---
 .../apache/sysds/runtime/data/SparseBlockCSR.java  | 28 +++++----
 .../apache/sysds/runtime/data/SparseBlockMCSR.java |  2 +
 .../sysds/test/component/matrix/UnaryOpTest.java   | 68 ++++++++++++++++++++++
 3 files changed, 86 insertions(+), 12 deletions(-)

diff --git a/src/main/java/org/apache/sysds/runtime/data/SparseBlockCSR.java 
b/src/main/java/org/apache/sysds/runtime/data/SparseBlockCSR.java
index dd04e4c..d4921e9 100644
--- a/src/main/java/org/apache/sysds/runtime/data/SparseBlockCSR.java
+++ b/src/main/java/org/apache/sysds/runtime/data/SparseBlockCSR.java
@@ -864,20 +864,24 @@ public class SparseBlockCSR extends SparseBlock
                sb.append(", nnz=");
                sb.append(size());
                sb.append("\n");
-               for( int i=0; i<numRows(); i++ ) {
-                       sb.append("row +");
-                       sb.append(i);
-                       sb.append(": ");
-                       //append row
-                       int pos = pos(i);
-                       int len = size(i);
-                       for(int j=pos; j<pos+len; j++) {
-                               sb.append(_indexes[j]);
+               for(int i = 0; i < numRows(); i++) {
+                       // append row
+                       final int pos = pos(i);
+                       final int len = size(i);
+                       if(pos < pos + len) {
+
+                               sb.append("row +");
+                               sb.append(i);
                                sb.append(": ");
-                               sb.append(_values[j]);
-                               sb.append("\t");
+
+                               for(int j = pos; j < pos + len; j++) {
+                                       sb.append(_indexes[j]);
+                                       sb.append(": ");
+                                       sb.append(_values[j]);
+                                       sb.append("\t");
+                               }
+                               sb.append("\n");
                        }
-                       sb.append("\n");
                }
                
                return sb.toString();
diff --git a/src/main/java/org/apache/sysds/runtime/data/SparseBlockMCSR.java 
b/src/main/java/org/apache/sysds/runtime/data/SparseBlockMCSR.java
index 66f63a4..9687dea 100644
--- a/src/main/java/org/apache/sysds/runtime/data/SparseBlockMCSR.java
+++ b/src/main/java/org/apache/sysds/runtime/data/SparseBlockMCSR.java
@@ -420,6 +420,8 @@ public class SparseBlockMCSR extends SparseBlock
                sb.append(size());
                sb.append("\n");
                for( int i=0; i<numRows(); i++ ) {
+                       if(isEmpty(i))
+                               continue;
                        sb.append("row +");
                        sb.append(i);
                        sb.append(": ");
diff --git 
a/src/test/java/org/apache/sysds/test/component/matrix/UnaryOpTest.java 
b/src/test/java/org/apache/sysds/test/component/matrix/UnaryOpTest.java
new file mode 100644
index 0000000..b9c4284
--- /dev/null
+++ b/src/test/java/org/apache/sysds/test/component/matrix/UnaryOpTest.java
@@ -0,0 +1,68 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sysds.test.component.matrix;
+
+import static org.junit.Assert.assertTrue;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.sysds.runtime.data.SparseBlockCSR;
+import org.apache.sysds.runtime.functionobjects.Builtin;
+import org.apache.sysds.runtime.functionobjects.Builtin.BuiltinCode;
+import org.apache.sysds.runtime.matrix.data.MatrixBlock;
+import org.apache.sysds.runtime.matrix.operators.UnaryOperator;
+import org.junit.Test;
+
+public class UnaryOpTest {
+       protected static final Log LOG = 
LogFactory.getLog(UnaryOpTest.class.getName());
+       static final MatrixBlock m = new MatrixBlock(100, 100, false);
+       static final UnaryOperator op = new 
UnaryOperator(Builtin.getBuiltinFnObject(BuiltinCode.ROUND));
+       
+       static {
+               m.setValue(3, 3, 4.2);
+       }
+
+       @Test
+       public void testBasic() {
+               assertTrue(m.getValue(3, 3) == 4.2);
+       }
+
+       @Test
+       public void testDirectUnaryOp() {
+               MatrixBlock mr = m.unaryOperations(op, null);
+               assertTrue(mr.getValue(3, 3) == 4);
+       }
+
+       @Test
+       public void testFromSparseCSRUnaryOp() {
+               MatrixBlock sb = new MatrixBlock(1, 1, false);
+               sb.copy(m);
+               sb.setSparseBlock(new SparseBlockCSR(sb.getSparseBlock()));
+               MatrixBlock mr = sb.unaryOperations(op, null);
+               assertTrue(mr.getValue(3, 3) == 4);
+       }
+
+       @Test
+       public void testFromSparseMCSRUnaryOp() {
+               MatrixBlock sb = new MatrixBlock(1, 1, false);
+               sb.copy(m);
+               MatrixBlock mr = sb.unaryOperations(op, null);
+               assertTrue(mr.getValue(3, 3) == 4);
+       }
+}

Reply via email to