Comer
--
You received this message because you are subscribed to the Google Groups "sympy" group.
To post to this group, send email to sy...@googlegroups.com.
To unsubscribe from this group, send email to sympy+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sympy?hl=en.
Very nice!
I think you are looking for the symarray() function in sympy:
Type: function
Base Class: <type 'function'>
String Form:<function symarray at 0x2aa3488>
Namespace: Interactive
File: /home/ondrej/repos/sympy/sympy/matrices/matrices.py
Definition: symarray(prefix, shape)
Docstring:
Create a numpy ndarray of symbols (as an object array).
The created symbols are named `prefix_i1_i2_`... You should thus provide a
non-empty prefix if you want your symbols to be unique for different output
arrays, as Sympy symbols with identical names are the same object.
Parameters
----------
prefix : string
A prefix prepended to the name of every symbol.
shape : int or tuple
Shape of the created array. If an int, the array is one-dimensional; for
more than one dimension the shape must be a tuple.
Examples
--------
These doctests require numpy.
>>> from sympy import symarray
>>> symarray('', 3) #doctest: +SKIP
[_0, _1, _2]
If you want multiple symarrays to contain distinct symbols, you *must*
provide unique prefixes:
>>> a = symarray('', 3) #doctest: +SKIP
>>> b = symarray('', 3) #doctest: +SKIP
>>> a[0] is b[0] #doctest: +SKIP
True
>>> a = symarray('a', 3) #doctest: +SKIP
>>> b = symarray('b', 3) #doctest: +SKIP
>>> a[0] is b[0] #doctest: +SKIP
False
Creating symarrays with a prefix:
>>> symarray('a', 3) #doctest: +SKIP
[a_0, a_1, a_2]
For more than one dimension, the shape must be given as a tuple:
>>> symarray('a', (2, 3)) #doctest: +SKIP
[[a_0_0, a_0_1, a_0_2],
[a_1_0, a_1_1, a_1_2]]
>>> symarray('a', (2, 3, 2)) #doctest: +SKIP
[[[a_0_0_0, a_0_0_1],
[a_0_1_0, a_0_1_1],
[a_0_2_0, a_0_2_1]],
<BLANKLINE>
[[a_1_0_0, a_1_0_1],
[a_1_1_0, a_1_1_1],
[a_1_2_0, a_1_2_1]]]
Ondrej
The best is to improve the function symarray():
https://github.com/sympy/sympy/blob/master/sympy/matrices/matrices.py#L4377
You can see that the implementation is only 4 lines. You can add a few options
to it, controlling this underscore behavior.
Ondrej
Yes, this is the 'gotcha' about symbols: the objects exist but aren't assigned
to any python variables. When you do Symbol('x') this doesn't create a python
variable x, only a sympy Symbol object.
As Matt (IIRC) said, you can use var to create this. Here are some steps showing
how else you might do this:
get the array and store in a
>>> csymarray('A_','',3,3)
array([[A_00, A_01, A_02],
[A_10, A_11, A_12],
[A_20, A_21, A_22]], dtype=object)
>>> a=_
get a string of the *objects* in it; flatten undoes the nesting:
>>> str(flatten(a))[1:-1]
'A_00, A_01, A_02, A_10, A_11, A_12, A_20, A_21, A_22'
>>> s=_
make a string to execute in the local environment
>>> "%s=symbols('%s')" % (s,s)
"A_00, A_01, A_02, A_10, A_11, A_12, A_20, A_21, A_22=symbols('A_00, A_01, A_02,
A_10, A_11, A_12, A_20, A_21, A_22')"
execute it and confirm that the python variables (which happen to have
the same sympy symbol name) have been created
>>> exec(_)
>>> A_00
A_00
or do it all at once with
>>> var(s)
(A_00, A_01, A_02, A_10, A_11, A_12, A_20, A_21, A_22)
>>> A_11
A_11
You don't need to do any of that to define and manipulate a Matrix, however:
>>> Matrix(a)
[A_00, A_01, A_02]
[A_10, A_11, A_12]
[A_20, A_21, A_22]
or the alternate instantiation which requires the flat list of elements
>>> Matrix(3, 3, flatten(a))
[A_00, A_01, A_02]
[A_10, A_11, A_12]
[A_20, A_21, A_22]
>>>
HTH,
/c
I appreciated how helpful the python community was when I was learning
(Danny Yoo was the tutor-list monitor at the time and a *great* help
as were many others). Glad to be able to return in kind.
/c