Take the Sigma of a factorial with unknown variable (using sympy?)

Question:

Good day,

I am trying to write a function for the following equation:

Eurlang B

Where B and N are given and I am solving for A.

I was reading up and it seemed like sympy was the way to go so I started to declare the known variables, but when it came to the sigma notation with the factorial, I had no idea of how to approach it since A is an unknown.

Here is what I came up with:

from sympy import Eq, var, solve
from math import *

A = var('A')
channels = raw_input("Enter the number of channels: ")
#GOS = raw_input("Enter GOS: ")

Sigma = A
for i in range(0,channels+1):
  Sigma += (A**i / factorial(i))

# equation = Eq((A**channels / factorial(channels)) / Sigma) 
# print solve(equation)

which gives me the error TypeError: cannot concatenate 'str' and 'int' objects

This makes sense to me, but my lack of knowledge with sympy makes me unable to figure out how to fix it.

EDIT: Looking around a bit more, I edited my code to this:

  from sympy import *

  A = symbols('A')
  channels = raw_input("Enter the number of channels: ")
  GOS = raw_input("Enter GOS: ")

  Sigma = summation(A**i / factorial(i), (i, 0,channels))
  print Sigma



  # equation = Eq((A**channels / factorial(channels)) / Sigma) 

Now I get NameError: name 'i' is not defined

Thanks in advance.

Asked By: Trever Wagenhals

||

Answers:

First of all, the error ( name ‘i’ not defined) is because you havent defined it. so you need to give an initial value for i.

secondly, I have tried to make your program run. got an error free solution with this code:

from sympy import *

A = symbols('A')
channels = raw_input("Enter the number of channels: ")
GOS = raw_input("Enter GOS: ")

# note that I convert the string 'channel' to an int
# convert to float if channel could also be a floating number
channels = int(channels) 
Sigma = A
for i in range(0,channels+1):
    Sigma += (A**i / factorial(i))
print Sigma

The result,

inputs: channels = 3, GOS = 1

output: A**3/6 + A**2/2 + 2*A + 1

EDIT: Out of interest I started looking further into your problem (also because I could realize this question would not stop just by a datatype issue).
The Solve function has 2 inputs, the equation and the symbol to calculate.
it solves the equation == 0. so the variable B has to be subtracted from the equation. (I supposed the input GOS is the B variable in the function)

equation = (A**channels / factorial(channels)) / Sigma
print(solve(equation-int(GOS), A))

running the code with the lines above (add them under the code) gave these outputs:

A**3/6 + A**2/2 + 2*A + 1
[-2 - sqrt(2), -2 + sqrt(2)]

I must notice that if the GOS does not intersect the function it gives large results with additional parameter I (capital i, might indicate imaginary i).
I hoped this helped solving your problem.

Answered By: Petrus1904

You can also perform the summation in SymPy using the summation function

i = symbols('i')
summation(A**i/factorial(i), (i, 0, N)

Another note: you are starting with Sigma = A, meaning your final result is A + ΣA^i/i! instead of just ΣA^i/i! (you can see in the output from @Petrus1904’s answer there is a 2*A instead of A). If you want to use a loop to compute a summation you should initialize the variable to 0.

Answered By: asmeurer

To describe a similar sum as at the top of the page, when I use the asmeurer’s recommendation summation, I get the error -TypeError: ‘Symbol’ object is not subscriptable." What could be the possible cause of this error? I imported the libraries below. There is a continuation of the code, but I did not add it to avoid confusion.

import sympy as sympy 
from sympy import *       
from sympy import summation, symbols

class FQ(): 
def integrate(self):
   for k in range(1, self.Nt):
      i = symbols('i', integer=True)
      self.Sigma = summation(self.u[i+1][j], (i, 0, k - 1))

#second attempt

def integrate(self, alpha, Nt, Nx, L):
    
    for k in range(1, self.Nt):   
      for j in range(1, self.Nx-1):  
        #define sum
        for i in range(1, self.Nt):
          Sigma = summation(u[i+1][j], (i, 0, k-1))
Answered By: Nurdan
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.