On Sun, Feb 5, 2012 at 11:54 AM, Comer <comer.dun...@gmail.com> wrote:
> I am working on a project which requires that I create a matrix of
> symbols. For discussion purposes say the matrix is 3 x 3. The matrix
> is to contain 9 unique symbols and my preferred format is the usual
> matrix format:
>
> a11, ..., a33 = symbols('a_11 ....a_33')
>
> Now obviously I could easily just type in the above for all nine
> symbols.  However, what I want to see how to do is to create a 3 x 3
> array as above but do it in a couple of for loops.  I looked at the
> symbols() function but it only seems to work with a linear sequence of
> symbol names such as x0, x1, x2,  .... xN. But I can not see how to do
> the same for a 2d array of symbol names such as x00, x01, x02, ... .
> It would be convenient in my project to be able to quickly create the
> 2d array of symbols directly by calling  symbols(). It would save some
> typing.
>
> Can someone please let me know how best to do this?

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

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To post to this group, send email to sympy@googlegroups.com.
To unsubscribe from this group, send email to 
sympy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sympy?hl=en.

Reply via email to