On 15 October 2012 22:09, someone <span dir="ltr"><<a href="mailto:newsboost@gmail.com" target="_blank">newsboost@gmail.com</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
See this:<br>
<br>
==============================<u></u>============================<br>
In [5]: Dx = numpy.matrix('1 0 0; 0 0.5 -0.5; 0 -0.5 1.5')<br>
<br>
In [6]: Dx<br>
Out[6]:<br>
matrix([[ 1. , 0. , 0. ],<br>
[ 0. , 0.5, -0.5],<br>
[ 0. , -0.5, 1.5]])<br>
==============================<u></u>============================<br></blockquote><div><br></div><div><snip></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Without having to manually change all the individual places using my variables (test is actually many variables, not just one but I think you should understand the problem now).<br>
<br>
<br>
How to initialize my array directly using variables ?<br>
<br>
It could also be that I wanted:<br>
<br>
test11 = 1<br>
test12 = 1.5<br>
test13 = 2<br>
test21 = 0<br>
test22 = 5<br>
<br>
Dx = numpy.matrix('test11 test12 test13; test21 test22 -0.5; 0 -0.5 1.5')<br>
<br>
Etc... for many variables...<br>
<br>
Appreciate ANY help, thank you very much!</blockquote><div><br></div><div> Well, I don't know that much on Numpy (I've only ever used the basic stuff), but Ian Kelly's probably right. If you can't "just use" a list, why don't you convert it?</div>
<div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span style="font-family:'courier new',monospace">def matrixstring2lists(string, lookup_chain):<br>
</span><span style="font-family:'courier new',monospace"> stringmatrix = [substr.split() for substr in string.split(";")]</span> </blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
</blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span style="font-family:'courier new',monospace"></span><span style="font-family:'courier new',monospace"> return [<br>
</span><span style="font-family:'courier new',monospace"> [<br></span><span style="font-family:'courier new',monospace"> lookup_chain[item] if item.isidentifier() else eval(item)<br></span><span style="font-family:'courier new',monospace"> for item in items<br>
</span><span style="font-family:'courier new',monospace"> ]<br></span><span style="font-family:'courier new',monospace"> for items in stringmatrix<br></span><span style="font-family:'courier new',monospace"> ]</span> </blockquote>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"> </blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<span style="font-family:'courier new',monospace"></span><span style="font-family:'courier new',monospace">##### TESTS ######<br></span><span style="font-family:'courier new',monospace">import builtins</span> </blockquote>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"> </blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<span style="font-family:'courier new',monospace"></span><span style="font-family:'courier new',monospace">## {{{ <a href="http://code.activestate.com/recipes/305268/">http://code.activestate.com/recipes/305268/</a> (r2)<br>
</span><span style="font-family:'courier new',monospace">class Chainmap(dict):<br></span><span style="font-family:'courier new',monospace"> """Combine multiple mappings for sequential lookup.</span> </blockquote>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"> </blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<span style="font-family:'courier new',monospace"></span><span style="font-family:'courier new',monospace"> For example, to emulate Python's normal lookup sequence:<br></span><span style="font-family:'courier new',monospace"> import __builtin__<br>
</span><span style="font-family:'courier new',monospace"> pylookup = Chainmap(locals(), globals(), vars(__builtin__)) <br></span><span style="font-family:'courier new',monospace"> """<br>
</span><span style="font-family:'courier new',monospace"> def __init__(self, *maps):<br></span><span style="font-family:'courier new',monospace"> self._maps = maps<br></span><span style="font-family:'courier new',monospace"> def __getitem__(self, key):<br>
</span><span style="font-family:'courier new',monospace"> for mapping in self._maps:<br></span><span style="font-family:'courier new',monospace"> try:<br></span><span style="font-family:'courier new',monospace"> return mapping[key]<br>
</span><span style="font-family:'courier new',monospace"> except KeyError:<br></span><span style="font-family:'courier new',monospace"> pass<br></span><span style="font-family:'courier new',monospace"> raise KeyError(key)<br>
</span><span style="font-family:'courier new',monospace">## end of <a href="http://code.activestate.com/recipes/305268/">http://code.activestate.com/recipes/305268/</a> }}}</span> </blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<font face="courier new, monospace"><br></font><span style="font-family:'courier new',monospace">test11 = 1<br></span><span style="font-family:'courier new',monospace">test12 = 1.5<br></span><span style="font-family:'courier new',monospace">test13 = 2<br>
</span><span style="font-family:'courier new',monospace">test21 = 0<br></span><span style="font-family:'courier new',monospace">test22 = 5</span> </blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
</blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span style="font-family:'courier new',monospace"></span><span style="font-family:'courier new',monospace">print(matrixstring2lists(<br>
</span><span style="font-family:'courier new',monospace"> 'test11 test12 test13; test21 test22 -0.5; 0 -0.5 1.5',<br></span><span style="font-family:'courier new',monospace"> Chainmap(locals(), globals(), vars(__builtins__))<br>
</span><font face="courier new, monospace">))</font></blockquote><div><br></div><div>That said, calling "matrixstring2lists" requires inputting a mapping from the local scope, so I don't see how it could get much simpler to call (minus removing "vars(__builtins__)" unless you <i>really</i> want that for some reason. </div>
<div><blockquote></blockquote><blockquote></blockquote><blockquote></blockquote><blockquote></blockquote><blockquote></blockquote><blockquote></blockquote><blockquote></blockquote><blockquote></blockquote><blockquote></blockquote>
<blockquote></blockquote><blockquote></blockquote><blockquote></blockquote><blockquote></blockquote><blockquote></blockquote><blockquote></blockquote><blockquote></blockquote><blockquote></blockquote><blockquote></blockquote>
<blockquote></blockquote><blockquote></blockquote><blockquote></blockquote><blockquote></blockquote><blockquote></blockquote><blockquote></blockquote><blockquote></blockquote><blockquote></blockquote><blockquote></blockquote>
<blockquote></blockquote><blockquote></blockquote><blockquote></blockquote><blockquote></blockquote><blockquote></blockquote><blockquote></blockquote><blockquote></blockquote><blockquote></blockquote><blockquote></blockquote>
<blockquote></blockquote></div></div>