changeset 529:39eba7d98ba1

Add complex matrix format and print
author Chris Cannam
date Fri, 14 Feb 2014 11:54:45 +0000
parents c2c1a874a2b5
children 0ebf2a6a83ee
files src/may/complex.yeti src/may/matrix/complex.yeti
diffstat 2 files changed, 40 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/may/complex.yeti	Thu Feb 13 18:27:00 2014 +0000
+++ b/src/may/complex.yeti	Fri Feb 14 11:54:45 2014 +0000
@@ -2,6 +2,7 @@
 module may.complex;
 
 vec = load may.vector;
+mm = load may.mathmisc;
 
 import java.lang: ClassCastException;
 
@@ -28,6 +29,20 @@
                 " \(real) + \(imag)i"
             fi
         fi,
+    String format()
+       (f v r =
+           (n = mm.round (v * 10000);
+            s = "\(n / 10000)";
+            if r then
+               (strPad ' ' (8 - strLength s) '') ^ s
+            else
+                strPad ' ' 7 "\(s)i";
+            fi);
+        if imag < 0 then
+            " \(f real true)-\(f (-imag) false)"
+        else 
+            " \(f real true)+\(f imag false)"
+        fi),
     int hashCode()
         Double#valueOf(real)#hashCode() + Double#valueOf(imag)#hashCode(),
     boolean equals(Object other)
@@ -90,6 +105,8 @@
 complexArray reals imags =
     array (map2 complex (vec.list reals) (vec.list imags));
 
+format c is ~Cplx -> string = c#format();
+
 typedef opaque complex_t = ~may.Cplx;
 
 {
@@ -108,7 +125,8 @@
    imaginaries,
    magnitudes,
    angles,
-   complexArray
+   complexArray,
+   format
 } as {
    real is complex_t -> number,
    imaginary is complex_t -> number,
@@ -125,6 +143,7 @@
    imaginaries is list?<complex_t> -> vec.vector_t,
    magnitudes is list?<complex_t> -> vec.vector_t,
    angles is list?<complex_t> -> vec.vector_t,
-   complexArray is vec.vector_t -> vec.vector_t -> array<complex_t>
+   complexArray is vec.vector_t -> vec.vector_t -> array<complex_t>,
+   format is complex_t -> string,
 }
 
--- a/src/may/matrix/complex.yeti	Thu Feb 13 18:27:00 2014 +0000
+++ b/src/may/matrix/complex.yeti	Fri Feb 14 11:54:45 2014 +0000
@@ -294,6 +294,21 @@
    (newPart f = mat.newColumnVector (vec.fromList (map f data));
     complex (newPart cpx.real) (newPart cpx.imaginary));
 
+format m =
+    strJoin "\n"
+       (chunk = 8;
+        map do b:
+            c0 = b * chunk;
+            c1 = b * chunk + chunk - 1;
+            c1 = if c1 > width m then width m else c1 fi;
+            [ "\nColumns \(c0) to \(c1)\n",
+              (map do row: map cpx.format row |> strJoin "";
+               done (asRows (columnSlice m c0 (c1 + 1))) |> strJoin "\n")
+            ];
+        done [0..width m / chunk - 1] |> concat);
+
+print' = print . format;
+
 {
     size,
     width,
@@ -333,6 +348,8 @@
     newComplexRowVector,
     newComplexColumnVector,
     enumerate,
+    format,
+    print = print',
 } as {
     size is complexmatrix_t -> { rows is number, columns is number },
     width is complexmatrix_t -> number,
@@ -375,5 +392,7 @@
     newComplexRowVector is list?<cpx.complex_t> -> complexmatrix_t,
     newComplexColumnVector is list?<cpx.complex_t> -> complexmatrix_t,
     enumerate is complexmatrix_t -> list<{ i is number, j is number, v is cpx.complex_t }>,
+    format is complexmatrix_t -> string,
+    print is complexmatrix_t -> (),
 }