Is there an efficient method of producing indexed variables?

21 views
Skip to first unread message

scott.h

unread,
Apr 1, 2010, 9:36:28 PM4/1/10
to sage-support
Hello, I'm new at Sage. Having come from a Maple background, my
largest issue is with how Sage handles variables. Hopefully, my
question is somewhat well formed.

My specific issue is with the following: from a diagonal matrix I
want to produce a vector of exponential functions of the respective
diagonal entries where each exponential function is multiplied by an
unknown constant. The following, although it is likely poorly
implemented, does the trick without the constants for a 3x3 diagonal
matrix D:

f=lambda i:e^(D[i,i]*x)
sol=vector(map(f,range(3)))

I want to do the following without initializing each c[i] manually:

f=lambda i:c[i]*e^(D[i,i]*x)

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

Message has been deleted

scott.h

unread,
Apr 2, 2010, 12:21:11 AM4/2/10
to sage-support
That did exactly what I wanted to do! Thank you very much for taking
the time to reply. The command C = [var("C_%s" % i) for i in
range(n)] in particular is what I was looking for. I think I can
glean how %s works from how you've used it and will experiment a
little. However if you, or anyone else, could point me in the
direction of some documentation for it that would be much
appreciated. It's sort of hard to google/search.


On Apr 1, 7: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:
>
> [mvngu@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]
>
> --
> Regards
> Minh Van Nguyen

Mike Hansen

unread,
Apr 2, 2010, 12:27:32 AM4/2/10
to sage-s...@googlegroups.com
On Thu, Apr 1, 2010 at 9:21 PM, scott.h <scott...@gmail.com> wrote:
> That did exactly what I wanted to do!  Thank you very much for taking
> the time to reply.  The command C = [var("C_%s" % i) for i in
> range(n)] in particular is what I was looking for.  I think I can
> glean how %s works from how you've used it and will experiment a
> little.  However if you, or anyone else, could point me in the
> direction of some documentation for it that would be much
> appreciated.  It's sort of hard to google/search.

One place to look would be
http://diveintopython.org/native_data_types/formatting_strings.html .

--Mike

Robert Bradshaw

unread,
Apr 2, 2010, 12:24:28 AM4/2/10
to sage-s...@googlegroups.com
On Apr 1, 2010, at 9:21 PM, scott.h wrote:

> That did exactly what I wanted to do! Thank you very much for taking
> the time to reply. The command C = [var("C_%s" % i) for i in
> range(n)] in particular is what I was looking for. I think I can
> glean how %s works from how you've used it and will experiment a
> little. However if you, or anyone else, could point me in the
> direction of some documentation for it that would be much
> appreciated. It's sort of hard to google/search.

How about http://diveintopython.org/native_data_types/formatting_strings.html

> --
> To post to this group, send email to sage-s...@googlegroups.com
> To unsubscribe from this group, send email to sage-support...@googlegroups.com
> For more options, visit this group at http://groups.google.com/group/sage-support
> URL: http://www.sagemath.org
>
> To unsubscribe, reply using "remove me" as the subject.

scott.h

unread,
Apr 2, 2010, 12:31:30 AM4/2/10
to sage-support
Perfect, thanks again!

On Apr 1, 9:24 pm, Robert Bradshaw <rober...@math.washington.edu>
wrote:


> On Apr 1, 2010, at 9:21 PM, scott.h wrote:
>
> > That did exactly what I wanted to do!  Thank you very much for taking
> > the time to reply.  The command C = [var("C_%s" % i) for i in
> > range(n)] in particular is what I was looking for.  I think I can
> > glean how %s works from how you've used it and will experiment a
> > little.  However if you, or anyone else, could point me in the
> > direction of some documentation for it that would be much
> > appreciated.  It's sort of hard to google/search.
>

> How abouthttp://diveintopython.org/native_data_types/formatting_strings.html

> > For more options, visit this group athttp://groups.google.com/group/sage-support

Franco Saliola

unread,
Apr 2, 2010, 1:45:03 PM4/2/10
to sage-s...@googlegroups.com
On Thu, Apr 1, 2010 at 10:50 PM, Minh Nguyen <nguye...@gmail.com> wrote:
> Hi,
>
> On Fri, Apr 2, 2010 at 12:36 PM, scott.h <scott...@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:
>
> [mvngu@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?

Franco

--

Alec Mihailovs

unread,
Apr 2, 2010, 5:59:15 PM4/2/10
to sage-support
On Apr 2, 1:45 pm, Franco Saliola <sali...@gmail.com> wrote:

> sage: a = SymbolicVariables('a')

> ...


> 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?

It would be conveniet if that would work with more than 1 index, too.
In this particular example, a[1,2] produces something strange if
typeset is turned on in the notebook.

Also, in Maple matrices and vectors with symbolic entries can be
defined as

Matrix(2, symbol=a);

[a[1, 1] a[1, 2]]
[ ]
[a[2, 1] a[2, 2]]

Vector(3, symbol=b);

[b[1]]
[ ]
[b[2]]
[ ]
[b[3]]


Alec

Reply all
Reply to author
Forward
0 new messages