Ask Your Question

# Revision history [back]

### 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.

Thank you for your time.

### 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.

Thank you for your time.

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

This made the trick:

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