On 10/25/2014 11:40 PM, [email protected] wrote:
> Thank you Michael, I loaded the file and succeeded to make a list of
> symbols. But now and after evaluating my expression in terms of its
> elements, how should I assign them different values I want the
> expression to be calculated for? (see a part of my code above, it may
> illustrate what I mean)
> Assigning X[1]=0, results to an attribute error:
>
> AttributeError: SymbolSequence instance has no attribute '__setitem__'
You have to substitute the values into the expression -- just setting
the value of the variable to something else won't actually affect the
expression. I know that sounds confusing. Here's a simpler example of
what I mean without the list of symbols:
sage: x = SR.var('x')
sage: f = x^2 + 1
At this point, "f" is a symbolic expression, in terms of some other
symbolic expression "x". We can substitute certain values of "x" into
"f" to see how it would evaluate:
sage: f(x = 3)
10
But setting the value of "x" doesn't change what "f" is:
sage: x = 3
sage: f
x^2 + 1
The sequences should work the same:
sage: x = SymbolSequence('x')
sage: f = x[1]^2 + x[2]
sage: f
x_1^2 + x_2
sage: f(x_1 = 3)
x_2 + 9
sage: f(x_1 = 3, x_2 = 1)
10
If you want to substitute a whole bunch of things at once
programmatically, you may need to use a dictionary. Suppose I want to
make a big matrix but I don't want to decide on the size or entries
beforehand (i.e. I need to do it at runtime).
sage: d = 5
sage: X = matrix(SR, d, d, x[0:d,0:d])
sage: X
[x_0_0 x_0_1 x_0_2 x_0_3 x_0_4]
[x_1_0 x_1_1 x_1_2 x_1_3 x_1_4]
[x_2_0 x_2_1 x_2_2 x_2_3 x_2_4]
[x_3_0 x_3_1 x_3_2 x_3_3 x_3_4]
[x_4_0 x_4_1 x_4_2 x_4_3 x_4_4]
Wheeeee. Now I want to substitute for those values. I can't hard-code
X(x_0_0 = 0, x_0_1 = 1, ...) since I don't know how many there are
beforehand. But I can make a list of them!
sage: substitutions=[ (x[i,j], i+j) for i in range(0,d)
....: for j in range(0,d) ]
sage: substitutions
[(x_0_0, 0),
(x_0_1, 1),
(x_0_2, 2),
(x_0_3, 3),
(x_0_4, 4),
(x_1_0, 1),
(x_1_1, 2),
(x_1_2, 3),
(x_1_3, 4),
(x_1_4, 5),
(x_2_0, 2),
(x_2_1, 3),
(x_2_2, 4),
(x_2_3, 5),
(x_2_4, 6),
(x_3_0, 3),
(x_3_1, 4),
(x_3_2, 5),
(x_3_3, 6),
(x_3_4, 7),
(x_4_0, 4),
(x_4_1, 5),
(x_4_2, 6),
(x_4_3, 7),
(x_4_4, 8)]
Wheeeeeeeeeeeeeeee. Now all I need to do is turn that into a dictionary.
One more time:
sage: subs_dict = dict(substitutions)
sage: subs_dict
{x_2_2: 4,
x_0_0: 0,
x_1_3: 4,
x_4_0: 4,
x_4_4: 8,
x_1_0: 1,
x_0_1: 1,
x_4_1: 5,
x_3_2: 5,
x_2_3: 5,
x_4_2: 6,
x_1_4: 5,
x_2_0: 2,
x_3_3: 6,
x_1_1: 2,
x_0_4: 4,
x_0_2: 2,
x_2_1: 3,
x_2_4: 6,
x_4_3: 7,
x_3_0: 3,
x_3_4: 7,
x_1_2: 3,
x_3_1: 4,
x_0_3: 3}
Finally, we can pass that dictionary as an argument to "subs" which
performs substitutions on expressions, matrices, and some other things.
sage: X.subs(subs_dict)
[0 1 2 3 4]
[1 2 3 4 5]
[2 3 4 5 6]
[3 4 5 6 7]
[4 5 6 7 8]
Cool, right? Did you ever realize that your example was way too big and
just keep going anyway? So basically,
* You have to use substitution to stick values into expressions.
* If you need to do programmatic substitution, a python dictionary
("dict") is often the way to go.
--
You received this message because you are subscribed to the Google Groups
"sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/d/optout.