Home > Net >  Calculating the sum of array-based terms in SymPy [ValueError: Invalid limits given]
Calculating the sum of array-based terms in SymPy [ValueError: Invalid limits given]

Time:12-29

I have been trying to obtain some numerical outputs from the sum below. It needs to give numerical outputs normally. However, I receive an error related to the limits.

I have a sum with a low boundary of i = 0 and an upper boundary of i = k-1 mathematically. I took (i, 0, k) because the last term is excluded by SymPy, Sum. Besides, I need to get the result within this nested for loop. Could there be a mismatch between for loops and sum? Even so, I cannot change for loops for k and Nt. Here, k depends on the Nt.

The code:

import numpy as np
from sympy import *
from sympy import Sum

Nx = 31          
Nt = 17
tau = .85 / Nt   
u = np.ones((Nt, Nx)) * np.sin(np.pi)
Sigma = np.zeros((Nt, Nx))   

for k in range(1, Nt): 
  for i in range(1, k):
    for j in range(1, Nx-1):   
      #define sum
      Sigma[i, j] = Sum( ((u[i 1][j] - u[i][j]) / tau * 97.1), (i, 0, k)).doit()    
      print(Sigma[i, j]) 

Error:

---> 16       Sigma[i, j] = Sum( ((u[i 1][j] - u[i][j]) / tau * 97.1), (i, 0, k) ).doit()

ValueError: Invalid limits given: ((1, 0, 2),)

Also, I am confused about sum, Sum or summation. None of these gave me a numerical result, or most likely I am not using these methods correctly. How can I get the numerical outputs? Again, I've tried np.sum() below.

for k in range(1, Nt): 
  for i in range(1, k):
    for j in range(1, Nx-1):   
      #define sum
      Sigma[i, j] = np.sum( ((u[i 1][j] - u[i][j]) / tau * 97.1), 0, k-1)    
      print(Sigma[i, j]) 

Output:

0.0
0.0
0.0...

I think I cannot properly write the limits of the sum in np.sum(). How can I correct this? How can I avoid 0 result?

EDIT: I used sum():

for k in range(1, Nt): 
  for i in range(1, k):
    for j in range(1, Nx-1):   
      #define sum
      Sigma[i, j] = sum(u[i 1][j] - u[i][j])
      print(Sigma[i, j])

Error:

TypeError: 'numpy.float64' object is not iterable

Thank you for any help!

CodePudding user response:

Reread the sympy docs. I think they specify that sum should be used with:

 Sum(expr, (var, a, b)

(I probably shouldn't try to work from memory here, but you can check).

In your:

 Sum( ((u[i 1][j] - u[i][j]) / tau * 97.1), (i, 0, k))

((u[i 1][j] - u[i][j]) / tau * 97.1) is a number, derived from numpy u. It isn't a sympy expression.

and i is a number, not sympy symbol. The error tells us that "Invalid limits given: ((1, 0, 2),)".

For someone who is new to Python, trying to use sympy will be difficult.

The problem with

np.sum( ((u[i 1][j] - u[i][j]) / tau * 97.1), 0, k-1)

is that np.sum does not take limits like the sympy Sum. Don't assume the documentation for one function applies to a similarly named one in another package. np.sum, if you read its docs, takes an array, with optional parameters like axis.

As for your last attempt:

sum(u[i 1][j] - u[i][j])

the python sum takes an "iterable", something like a list. u[i 1][j] - u[i][j] is a single number.

u is a 2d numpy array. u[i] is a 1d array, a "row";, u[i,j] is a single element of the array.

What exactly are you trying to sum?

I suspect you have some sort of mathematical summation in mind, and have tried to express that with sympy algebra. But your u is a 2d numpy array. So u[i 1,j]-u[i,j] is a single number, the difference between two elements. u[1:,j]-u[:-1,j] takes the difference between all such pairs of rows.

I haven't tried to figure out what your nested loops are doing, especially since i is a subset of possible rows.

CodePudding user response:

Is this what you are looking for?

for k in range(1, Nt): 
  for i in range(1, k):
    for j in range(1, Nx-1):   
      #define sum
      Sigma[i, j] = sum(u[_ 1][j] - u[_][j] for _ in range(0, k))
      print(Sigma[i, j])

Although you could write Sum(u[_ 1[j] - u[_], (_, 0, k)).doit(), the built-in sum is really what you are trying to do: an elementwise summation of literal values, not a summation of symbolic terms like Sum(1/x, (x, 0, 5)) -- there does not need to be an x array for SymPy to figure out that sum since the limits indicate what the values of x are going to be. In your case you have the values in an array already.

  • Related