Suppose I have defined a function
and a list
Now I can calculate the product
But: If I use the command
I will get the message:
How can I realize such a summation?
Here are some ways to sum the squares of the numbers from 0 to 1000, with timings:
So if speed matters to you, you should learn a little python!
For example, the syntax [EXPR for VAR in LIST] is very handy - it evaluates EXPR with VAR ranging over the list (or any iterable object) LIST. You can even use it with a filtering:
will sum the squares of primes in this range.
To understand the last two summation examples above you should read about lambda, reduce, and xrange in a python tutorial. It will pay off!
Given the syntax of the
Also, be careful defining a variable,
The first syntax is symbolic summation:
It will always try to evaluate the sum symbolically. If you use something that doesn't understand symbolic variables (like GF(8), which is implemented by polybori) then you get the "unable to convert to integer" error. Because polybori can only exponentiate by integers, and not by formal variables.
The second syntax is Python's list creation and summation of lists:
Note that the variable inside the list comprehension (which I called j here) is automatically created and will overwrite other variables. In your second example, calling the inner variable i overwrites the previously-defined symbolic variable var('i')
posted May 16 '11Volker Braun
2238 ● 5 ● 22 ● 52
I met a similar problem. Take the following example.
Let us try another syntax.
Now it is okay thanks to the previous answer. I wonder if it's possible to make it work with the first syntax.
To add to the diversity of answers here, another valid approach is to add a single line to what you already have:
This will return 100. The point is that you can use this syntax to sum the values of a proper function, but not arbitrary Python expressions.
posted May 18 '11Jason Bandlow
381 ● 1 ● 8 ● 18
Asked: Mar 26 '11
Seen: 2,477 times
Last updated: May 18 '11
powered by ASKBOT version 0.7.22
Copyright Sage, 2010. Some rights reserved under creative commons license.