-2

I need to calculate the sum of the sequence in Python but I can't use the built-in functions for exponentiation.

It means I can't use ** and pow(). I have to create my own function for that.

So I created the function for exponentiation but it works only for numbers. I need to calculate my formula to the nth.

My function for exponentiation:

def exponentiation(a,b):
    result = 1
    for index in range(b):
        result = result * a
    return result

For numbers, it works. But when I want to do this to the nth (I defined 'n' as a Symbol) I get:

'Symbol' object cannot be interpreted as an integer

So I don't know how to fix that.

And if I want to calculate the sum of the sequence, I use and it works:

sy.summation((-1/2)**n, (n,1, oo))

But as I said before I need to change ** to my own exponentiation function, but it still shows that 'Symbol' object cannot be interpreted as an integer.

sy.summation(exponentiation((-1/2),n), (n,1, oo))

Do you have any advice?

martineau
  • 119,623
  • 25
  • 170
  • 301
Minimalist
  • 79
  • 1
  • 6
  • 2
    What exactly is the result of exponentiation with a symbol? – martineau Oct 27 '20 at 09:55
  • @eugenhu yes, I use sympy – Minimalist Oct 27 '20 at 09:56
  • @martineau I get an error: TypeError: 'Symbol' object cannot be interpreted as an integer. – Minimalist Oct 27 '20 at 09:56
  • 1
    _It means I can't use `**` and `pow()`._ - Why? – Patrick Artner Oct 27 '20 at 09:57
  • @PatrickArtner it's a part of the task. It means calculate the sum of the sequence without using built-in functions for exponentiation. – Minimalist Oct 27 '20 at 09:59
  • Minimalist: Yeah, I know — you mentioned that already. The misunderstanding was because there was no mention that you were using sympy. I just added the tag to your question. – martineau Oct 27 '20 at 09:59
  • 3
    So you are allowed to use SymPy but not **, pow() ? This seems to be a XY-Problem. You seem to want to calculate the limes of the function (-1/2)**n for n in 0 ... infinity. - you can not "calculate" infinite things with a loop, your code would never finish, you need some break condition for a OR need to find a closed solution. – Patrick Artner Oct 27 '20 at 10:00
  • @Mr.T for exponentiation(2,3) I get 8 - it works. When I do for example exponentiation(2,n) I get an error that symbol object cannot be interpreted as an integer - I did only for the test. When I do: sy.summation((-1/2)**n, (n,1, oo)) I receive 1/3. So I need to calcucate this sum without built-in functions for exponentiation but I want to recive 1/3 too. – Minimalist Oct 27 '20 at 10:03
  • 1
    Doesn't make any sense, since in this case ** is not a python build-in, but a function (__pow__) that is implemented by SymPy. – AnnoSiedler Oct 27 '20 at 10:05
  • @PatrickArtner I use SymPy to calculate the sum of the sequence (sy.summation). So do you think that my function for exponentiation is wrongly defined? – Minimalist Oct 27 '20 at 10:08
  • @AnnoSiedler I can't use any function to calculate the exponentiation (**, pow(), etc) I need to create my own one. – Minimalist Oct 27 '20 at 10:10
  • Check with your teacher. How high are the chances you are allowed to use SymPy if you are forbidden to use **? – Patrick Artner Oct 27 '20 at 10:13
  • @PatrickArtner I only got the instruction that I have to calculate the sum of the sequence (-1/2)^n but I can't use prepared commands in Python for raising the number to integer power. and I need to create my own function for that. – Minimalist Oct 27 '20 at 10:20

3 Answers3

2

You cannot raise 'n' to a power. I am pretty sure if you are forbidden to use ** and pow() using SymPy won't fly either.

To calculate what limes n formula results in you can simply assume a "big" n and check if you can still detect any difference between the earlier result and the next result - you will not see any more changes very fast due to floating math limitations (Is floating point math broken?):

def exponentiation(a,b):
    result = 1
    for index in range(b):
        result = result * a
    return result
s = 0
a = -1/2
for n in range(1, 10000000):
    old_s = s
    s += exponentiation(a,n)

    # do not compare floats with small differences with ==
    # see link below for better ways to do that
    if s == old_s:
        print("\nThe sum no longer changes due to floating math limitations.")
        print(f"Result before: {old_s} and after {s} for n={n}")
        break
    else:
        print(f"nResult before: {old_s} and after {s} for n={n}")

Output:

Result before: 0 and after -0.5 for n=1
Result before: -0.5 and after -0.25 for n=2
Result before: -0.25 and after -0.375 for n=3
[...snipp...]
Result before: -0.33333333333333326 and after -0.33333333333333337 for n=53
Result before: -0.33333333333333337 and after -0.3333333333333333 for n=54
Result before: -0.3333333333333333 and after -0.33333333333333337 for n=55

The sum no longer changes due to floating math limitations.
Result before: -0.33333333333333337 and after -0.33333333333333337 for n=56

See What is the best way to compare floats for almost-equality in Python? for more on float comparisons.

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
0

The 'nth' means any given number. So you don't need to (and I can't think how you would) exponentiate any symbols. I think you can maybe simplify things a bit, if you return a list instead of just the nth value:

def exponentiation(a, n):
    result = 1
    exponents_list = []
    for i in range(n):
        result *= a
        exponents_list.append(result)
    return exponents_list

then work with the list with a for loop to get your sum

if you have to work with sympy, check this answer: Summation over a sympy Array

nihilok
  • 1,325
  • 8
  • 12
  • it doesn't work. I need to change that: summation((-1/2)**n, (n,1, oo)). - I have to replace ** with my own function. The function you wrote doesn't work because it gets "Sum(n**(-0.5), (n, 1, oo))" and the result should be 1/3. – Minimalist Oct 27 '20 at 10:30
  • `print(sum(exponentiation(-.5, 64)))` returns roughly -1/3 (-0.33333333333333337) – nihilok Oct 27 '20 at 10:36
  • That's assuming you're allowed to use the builtin `sum()` function. Otherwise, use a for loop as in the exponentiation function. – nihilok Oct 27 '20 at 10:42
  • it's assumed you will take https://docs.python.org/3/tutorial/floatingpoint.html into account – nihilok Oct 27 '20 at 11:03
0

I'd say, the only meaningful solution to replace ** with Python-code is something like that:

def exponentiation(a,b):
    if isinstance(a, Symbol):
        return a.__pow__(b)
    if isinstance(b, Symbol):
        return b.__pow__(a)
    result = 1
    for index in range(b):
        result = result * a
    return result

If you want to re-implement SymPys pow function - that'd be certainly far too difficult for a Stackoverflow-answer ;).

But you can find SymPys source code here: https://github.com/sympy/sympy/blob/master/sympy/core/power.py

AnnoSiedler
  • 273
  • 1
  • 6