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