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.
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
One place to look would be
http://diveintopython.org/native_data_types/formatting_strings.html .
--Mike
> 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.
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
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
--
> 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