# Summation of simbolic variables

Hi guys!

I have this function on theta:

$R_{x}(\theta) = R_{tx} + \cos \theta \cdot \left( f_{vp} + \sum_{i=1}^{n} \left(p S_i \sin^2 \alpha_i \right) \right) - \sin \theta \cdot \frac{1}{2} \sum_{i=1}^{n} \left(p S_i \sin 2\alpha_i \right)$

Since I cannot define an n-dimensional vector, I reduced n to 5, and wrote the following code down in SAGE:

var('alpha1, alpha2, alpha3, alpha4, alpha5');    # some angles
var('area1, area2, area3, area4, area5');         # some areas
var('Rtx, Rty');                                  # components of some force
var('fvp');                                       # modulus of yet another force
var('pv');                                        # wind pressure
var('theta');                                     # wind direction
var('i');                                         # variable to iterate over the angles and areas

# this vector contains all the angles
alpha = vector([alpha1, alpha2, alpha3, alpha4, alpha5]);

# this vector contains all the areas
area = vector([area1, area2, area3, area4, area5]);


Upto this point everything seems to go fine.

Then I try to define my function like this:

# The function I am trying to define, depending on the wind direction
Rx(theta) = Rtx + cos(theta) * (fvp + sum(pv * area[i] * sin(alpha[i])^2, i, 1, 5)) - sin(theta) * 1/2 * sum(area[i] * sin(2*alpha[i])^2, i, 1, 5)


And sage complains:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-18-4889d5aee183> in <module>()
----> 1 __tmp__=var("theta"); Rx = symbolic_expression(Rtx + cos(theta) * (fvp + sum(pv * area[i] * sin(alpha[i])**Integer(2), i, Integer(1), Integer(5))) - sin(theta) * Integer(1)/Integer(2) * sum(area[i] * sin(Integer(2)*alpha[i])**Integer(2), i, Integer(1), Integer(5))).function(theta)

sage/modules/free_module_element.pyx in sage.modules.free_module_element.FreeModuleElement.__getitem__ (/build/sagemath-x4mQwo/sagemath-7.4/sage/src/build/cythonized/sage/modules/free_module_element.c:12935)()

sage/symbolic/expression.pyx in sage.symbolic.expression.Expression.__index__ (/build/sagemath-x4mQwo/sagemath-7.4/sage/src/build/cythonized/sage/symbolic/expression.cpp:31916)()

sage/symbolic/expression.pyx in sage.symbolic.expression.Expression._integer_ (/build/sagemath-x4mQwo/sagemath-7.4/sage/src/build/cythonized/sage/symbolic/expression.cpp:8623)()

TypeError: unable to convert i to an integer


Any ideas on what may be happening? And, Is there a way to make it work?

Apparently, my question is similar to: Symbolic function that sums over variable sequence (sorry: my karma is insufficient to post links).

Still, it is different in that I have two vectors of variables and need to define a computation using variables from both vectors. And I was trying to get it done in a different way.

============== EDIT:

alpha = function("alpha")(i);
area = function("area")(i);
Rx=function("Rx")(theta);
sum1(i) = pv * area(i) * sin(alpha(i)^2)
sum2(i) = area(i) * sin(2*alpha(i))/2

Rx(theta) = Rtx + cos(theta) * (fvp + sum(sum1(i), i, 1, n)) - sin(theta) * sum(sum2(i), i, 1, n)


Thanks to @emmanuel-charpentier

edit retag close merge delete

Sort by » oldest newest most voted

You have two different beasts here:

The Python function sum, that returns the sum of a list (or other sequence, IRC :

sage: z=[var("z_{}".format(u)) for u in (1..5)];z
[z_1, z_2, z_3, z_4, z_5]
sage: sum(z)
z_1 + z_2 + z_3 + z_4 + z_5


The Sage function sum, that returns the summation of a symbolic function of a symbolic variable taking integer values varying between the bounds:

sage: var("j")
j
sage: y=function("y")(j)
sage: sum(y(j),j,1,5)
y(5) + y(4) + y(3) + y(2) + y(1)


But the first one has a list argument, whereas the second has four arguments belonging in SR. And, no, you can't mix n' match:

sage: sum(z[j],j,2,3)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-24-8e9069a4812d> in <module>()
----> 1 sum(z[j],j,Integer(2),Integer(3))

/usr/local/sage-8/local/lib/python2.7/site-packages/sage/symbolic/expression.pyx in sage.symbolic.expression.Expression.__index__ (build/cythonized/sage/symbolic/expression.cpp:32735)()
5753             [0, 1, 2, 3, 4]
5754         """
-> 5755         return int(self._integer_())
5756
5757     def iterator(self):

/usr/local/sage-8/local/lib/python2.7/site-packages/sage/symbolic/expression.pyx in sage.symbolic.expression.Expression._integer_ (build/cythonized/sage/symbolic/expression.cpp:8663)()
1085             n = self.pyobject()
1086         except TypeError:
-> 1087             raise TypeError("unable to convert %r to an integer" % self)
1088         if isinstance(n, sage.rings.integer.Integer):
1089             return n

TypeError: unable to convert j to an integer


What happens is that, whereas z is a legal Python expression (whose value is z_2, a Symbolic Expression) because it designates a Python list indexed by something that has an integer value, z[j] is illegal : you can't index a Python list by a Symbolic Expression. Since Python evaluates all the arguments of a function call before evaluating the function call itself, it has no way to "know" that j will eventually be assigned an integer value. Therefore, it barfs.

[ The lack of any kind of "special forms", "macroes" or "lazy evaluation" in Python is, IMNSHO, one of the major design weaknesses of Python. It turns out that it may well be one of its major strengths, according to some... Not being a computer scientist, I'll refrain from commenting further. I'll just user Lisp (or R) (or even C) when I'll need such a construct.. ]

[ Note also that the evaluation of the arguments before the function call allows for choosing between the built-in Python function and the Sage function based on the type of the arguments... ]

HTH,

more

Thank you. Changing alpha and area from vectors into functions did the trick. Sorry I can't upvote yet.

1

If a question was good enough for you to answer it, consider upvoting it.

Especially if it is a question from a new user, so the new user gets karma. As part of the symbolic summation you are trying to access e.g. alpha[i] where i is an indeterminate variable; that doesn't work. Instead you should use the ordinary summation. Also, indices start from 0 in Python and SageMath.

sum(area[i] * sin(2*alpha[i])^2, i, 1, 5)


it should be e.g.

sum(area[i] * sin(2*alpha[i])^2 for i in range(5))


Note also that you can do things like

n = 5
alpha = vector([var('alpha_%d' % (k+1)) for k in range(n)])


to save yourself some typing.

more