# HG changeset patch # User Chris Cannam # Date 1392378885 0 # Node ID 39eba7d98ba13fa4c153921778e2fbeae7bce4bc # Parent c2c1a874a2b54cfe79c073e7f2fed6c9fa820a4f Add complex matrix format and print diff -r c2c1a874a2b5 -r 39eba7d98ba1 src/may/complex.yeti --- 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? -> vec.vector_t, magnitudes is list? -> vec.vector_t, angles is list? -> vec.vector_t, - complexArray is vec.vector_t -> vec.vector_t -> array + complexArray is vec.vector_t -> vec.vector_t -> array, + format is complex_t -> string, } diff -r c2c1a874a2b5 -r 39eba7d98ba1 src/may/matrix/complex.yeti --- 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? -> complexmatrix_t, newComplexColumnVector is list? -> 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 -> (), }