changeset 480:b70acc1d84d9

Fix to density (and test), add nonZeroValues
author Chris Cannam
date Fri, 01 Nov 2013 17:05:31 +0000
parents 493d7bfc58ca
children 9f87e8eb1e7e
files src/may/matrix/complex.yeti src/may/matrix/test/test_complexmatrix.yeti
diffstat 2 files changed, 54 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/src/may/matrix/complex.yeti	Fri Nov 01 12:58:37 2013 +0000
+++ b/src/may/matrix/complex.yeti	Fri Nov 01 17:05:31 2013 +0000
@@ -40,6 +40,26 @@
         esac;
     cpx.complex (partAt cm.real row col) (partAt cm.imaginary row col));
 
+enumerate cm =
+   (enhash h p proto ix =
+       (e = maybe [] mat.enumerate p;
+        for e do { i, j, v }:
+            key = { i, j };
+            if not key in h then h[key] := proto (); fi;
+            h[key][ix] := v;
+        done);
+    h = [:];
+    proto = \(array[0,0]);
+    enhash h cm.real proto 0;
+    enhash h cm.imaginary proto 1;
+    sortBy
+        do a b:
+            if a.i == b.i then a.j < b.j else a.i < b.i fi
+        done
+           (map do k: k with { v = cpx.complex h[k][0] h[k][1] } done
+               (keys h))
+    );
+
 maybe' lambda f = // like maybe but with lazy default
    \case of
     Some v: f v;
@@ -239,19 +259,12 @@
         imaginary = addParts (multiplyParts b c) (multiplyParts a d);
     });
 
+nonZeroValues cm =
+    fold do nnz { i, j, v }: if v != cpx.zero then nnz + 1 else nnz fi done
+        0 (enumerate cm);
+
 density cm =
-    case cm.real of
-    Some m1:
-        case cm.imaginary of
-        Some m2: (mat.density m1 + mat.density m2) / 2;
-        none: mat.density m1;
-        esac;
-    none:
-        case cm.imaginary of
-        Some m2: mat.density m2;
-        none: 0.0;
-        esac;
-    esac;
+    (nonZeroValues cm) / (width cm * height cm);
 
 newComplexMatrix type data =
    (newPart f =
@@ -267,31 +280,12 @@
    (newPart f = mat.newColumnVector (vec.fromList (map f data));
     complex (newPart cpx.real) (newPart cpx.imaginary));
 
-enumerate cm =
-   (enhash h p proto ix =
-       (e = maybe [] mat.enumerate p;
-        for e do { i, j, v }:
-            key = { i, j };
-            if not key in h then h[key] := proto (); fi;
-            h[key][ix] := v;
-        done);
-    h = [:];
-    proto = \(array[0,0]);
-    enhash h cm.real proto 0;
-    enhash h cm.imaginary proto 1;
-    sortBy
-        do a b:
-            if a.i == b.i then a.j < b.j else a.i < b.i fi
-        done
-           (map do k: k with { v = cpx.complex h[k][0] h[k][1] } done
-               (keys h))
-    );
-
 {
     size,
     width,
     height,
     density,
+    nonZeroValues,
     at,
     getColumn,
     getRow,
@@ -328,6 +322,7 @@
     width is complexmatrix -> number,
     height is complexmatrix -> number,
     density is complexmatrix -> number,
+    nonZeroValues is complexmatrix -> number,
     at is complexmatrix -> number -> number -> cplx,
     getColumn is number -> complexmatrix -> array<cplx>,
     getRow is number -> complexmatrix -> array<cplx>,
--- a/src/may/matrix/test/test_complexmatrix.yeti	Fri Nov 01 12:58:37 2013 +0000
+++ b/src/may/matrix/test/test_complexmatrix.yeti	Fri Nov 01 17:05:31 2013 +0000
@@ -78,6 +78,33 @@
     mags = cm.magnitudes m;
     compare (map vec.list (mat.asRows mags)) [[1,2],[0,5]];
 ),
+
+"magnitudes-real": \(
+    reals = mat.newMatrix (RowMajor ()) (map vec.fromList [[1,0],[0,4]]);
+    m = cm.fromReal reals;
+    mags = cm.magnitudes m;
+    compare (map vec.list (mat.asRows mags)) (map vec.list (mat.asRows reals));
+),
+
+"magnitudes-imag": \(
+    reals = mat.newMatrix (RowMajor ()) (map vec.fromList [[1,0],[0,4]]);
+    m = cm.fromImaginary reals;
+    mags = cm.magnitudes m;
+    compare (map vec.list (mat.asRows mags)) (map vec.list (mat.asRows reals));
+),
+
+"density": \(
+    // 1+0i 0-2i
+    // 0+0i 4-3i
+    reals = mat.newMatrix (RowMajor ()) (map vec.fromList [[1,0],[0,4]]);
+    imags = mat.newMatrix (RowMajor ()) (map vec.fromList [[0,-2],[0,-3]]);
+    compare (cm.density (cm.complex reals imags)) 0.75 and
+        compare (cm.nonZeroValues (cm.complex reals imags)) 3 and
+        compare (cm.density (cm.fromReal reals)) 0.5 and
+        compare (cm.nonZeroValues (cm.fromReal reals)) 2 and
+        compare (cm.density (cm.fromImaginary imags)) 0.5 and
+        compare (cm.nonZeroValues (cm.fromImaginary imags)) 2
+),
  
 "angles": \(
     // 1+0i 0-2i