From 90c4f1b50b5dc9a338417ef2757912ca994b65cf Mon Sep 17 00:00:00 2001 From: wirawan Date: Fri, 3 Jun 2011 18:24:00 +0000 Subject: [PATCH] * Added vector_str and matrix_str generic function to print a matrix/vector in space-seprated string format. The output is mainly for human consumption, not for machine re-reading (although it is possible to do so, in general). --- text_tools.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/text_tools.py b/text_tools.py index c2bcc2c..c0a9df9 100644 --- a/text_tools.py +++ b/text_tools.py @@ -1,4 +1,4 @@ -# $Id: text_tools.py,v 1.5 2010-09-27 19:54:26 wirawan Exp $ +# $Id: text_tools.py,v 1.6 2011-06-03 18:24:00 wirawan Exp $ # # wpylib.text_tools # Created: 20091204 @@ -34,6 +34,19 @@ def make_matrix(Str, debug=None): if debug: print rslt return numpy.array(rslt) +def vector_str(M, fmt="%22.15g", V=False): + if len(M.shape) != 1: + raise ValueError, "Wrong shape: expecting a one-dimensional array." + if V: + return "\n".join([ fmt % m for m in M ]) + else: + return " ".join([ fmt % m for m in M ]) + +def matrix_str(M, fmt="%22.15g"): + if len(M.shape) != 2: + raise ValueError, "Wrong shape: expecting a two-dimensional array." + return "\n".join([ " ".join([ fmt % c for c in R ] ) for R in M ]) + def str_unindent(S, amount=None): """Automatically unidents a string based on the first indentation found