On 04/02/2010 12:47 PM, Franco Saliola wrote:
I forgot to more the conversation below from sage-support to sage-devel.

---------- Forwarded message ----------
From: Franco Saliola<sali...@gmail.com>
Date: Fri, Apr 2, 2010 at 1:45 PM
Subject: Re: [sage-support] Is there an efficient method of producing
indexed variables?
To: sage-supp...@googlegroups.com


On Thu, Apr 1, 2010 at 10:50 PM, Minh Nguyen<nguyenmi...@gmail.com>  wrote:
Hi,

On Fri, Apr 2, 2010 at 12:36 PM, scott.h<scott.he...@gmail.com>  wrote:

<SNIP>

It seems like this should be simple but for the life of me I can't
figure out how to do it.

Here I'm taking a guess at what you really want to do. See the
following Sage session:

[mv...@sage ~]$ sage
----------------------------------------------------------------------
| Sage Version 4.3.5, Release Date: 2010-03-28                       |
| Type notebook() for the GUI, and license() for information.        |
----------------------------------------------------------------------
sage: n = 3
sage: M = random_matrix(ZZ, nrows=n); M
[ 2  2 -2]
[ 4  2 -7]
[ 2 -1  1]
sage: # create a list of unknown constants; these are actually
symbolic variables
sage: C = [var("C_%s" % i) for i in range(n)]; C
[C_0, C_1, C_2]
sage: X = [randint(1, 10) for i in range(n)]; X
[2, 3, 2]
sage: F = [C[i] * exp(M[i,i] * x) for i in range(n)]; F
[C_0*e^(2*x), C_1*e^(2*x), C_2*e^x]
sage: [F[i].substitute(x=X[i]) for i in range(n)]
[C_0*e^4, C_1*e^6, C_2*e^2]

It might be useful to have a constructor to simplify this.
One would be able to do something like this:

    sage: a = SymbolicVariables('a')
    sage: sum(a[i]*x^i for i in range(3))
    a2*x^2 + a1*x + a0

and Minh's session above would become:

    sage: n = 3
    sage: M = random_matrix(ZZ, nrows=n)
    sage: c = SymbolicVariables('c')
    sage: [c[i] * exp(M[i,i] * x) for i in range(n)]
    [c0*e^(-x), c1*e^(-5*x), c2*e^(13*x)]

Here is a very simple implementation of SymbolicVariables.

    class SymbolicVariables(SageObject):
        def __init__(self, prefix='x'):
            self._prefix = prefix
        def __getitem__(self, i):
            return var("%s%s"%(self._prefix, i))

Thoughts?


This has come up several times before on sage-devel, and I've always liked it. For example, Nathann Cohen brought this up when he was first integrating the linear programming stuff. I believe Robert Bradshaw suggested this "indexing creates a variable" approach a while ago too.

When/if we wrap pynac vectors/matrices, though, or have "symbolic vector variables", we might have to be careful about how we treat the __getitem__ function. I think in your case, where you explicitly are creating x with the understanding that indexing x creates new variables, things would be fine, though.

Thanks,

Jason

--
To post to this group, send an email to sage-devel@googlegroups.com
To unsubscribe from this group, send an email to 
sage-devel+unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-devel
URL: http://www.sagemath.org

To unsubscribe, reply using "remove me" as the subject.

Reply via email to