0

I'm trying to get the Newton polynomial through interpolation. Thus, I need "x" as a symbol - that is, I expect a polynomial formula at the output.

Encountered an error:

TypeError: reduce() arg 2 must support iteration

What could be the problem?

Code:

from scipy.special import factorial
from sympy import symbols, prod

x = symbols('x')
string_one = [0.3091,-0.0304,-0.0271,0.0054,0.0025,-0.0016,0.0019,-0.0046,0.0099,-0.019]
x_col_list = [0.0, 0.3142, 0.6283, 0.9425, 1.2566, 1.5708, 1.885, 2.1991, 2.5133]

 # we go along the deltas of the first line:
num=1
for delta in string_one:
    # we go along the column of x's: 
    for args in x_col_list:
        polynom = round( ( delta/(factorial(num)*h**num) ), 4 )  *prod(x - args)
        num+=1
    print(polynom)
2
  • If you look at the traceback of the error, it should tell you what the problem is. Commented Jun 12, 2022 at 9:36
  • 2
    prod takes an iterable as argument (like a list). x-args is not an iterable. [x - args for args in x_col_list] would be an iterable. Commented Jun 12, 2022 at 10:06

1 Answer 1

1

Before even running your code, and before even arriving to the prod, you should quickly recognize that if you run your code, you will get an error about h and not prod! And if you do run your code, you will get the following error message:

Traceback (most recent call last):
  File "...", line 13, in <module>
    polynom = round( ( delta/(factorial(num)*h**num) ), 4 )  *prod(x - args)
NameError: name 'h' is not defined

Indeed the issue is that you are using h before defining it! Let's assume you forgot to put a line h = 0.3141 somehwere maybe after num = 1.

Now you will get the error that you are talking about.

Traceback (most recent call last):
  File "...", line 14, in <module>
    polynom = round( ( delta/(factorial(num)*h**num) ), 4 )  *prod(x - args)
  File "...\Python\lib\site-packages\sympy\core\mul.py", line 2072, in prod
    return reduce(operator.mul, a, start)
TypeError: reduce() arg 2 must support iteration

Now what is the problem? Why not looking at the help text for the command prod that developers of it have written and is available in sympy package itself? For seeing a short description of a Python function, just use help(). So you can do the following.

import sympy
help( sympy.prod )

I'm loading sympy, then asking Python to show me any description written by the developers about the function prod in the module sympy. This is what you will see.

Help on function prod in module sympy.core.mul:

prod(a, start=1)
    Return product of elements of a. Start with int 1 so if only
       ints are included then an int result is returned.
    
    Examples
    ========
    
    >>> from sympy import prod, S
    >>> prod(range(3))
    0
    >>> type(_) is int
    True
    >>> prod([S(2), 3])
    6
    >>> _.is_Integer
    True
    
    You can start the product at something other than 1:
    
    >>> prod([1, 2], 3)
    6

So it gets something called a and returns product of elements of a! So a is a collection! Not a single value! Look at examples, for example range(3) is not a single number 3, but it is 0, 1, 2, no? But what did you give to prod in your code? You gave x - args, x is a single symbol x, args is a single value, look at the place you defined it, it is the counter of your for-loop, you asked Python to go through elements of the list x_col_list one by one, at each step of your for-loop, args contains only one of these elements, what are the elemnts in x_col_list, single float numbers. Check it with the following code.

from sympy import symbols

x = symbols('x')
x_col_list = [0.0, 0.3142, 0.6283, 0.9425, 1.2566, 1.5708, 1.885, 2.1991, 2.5133]

number = 1
for args in x_col_list:
    print( f"At step {number} of the for-loop, x - args = {x - args}.\n" )
    number += 1

You see? At step 2 for example, x - args is just one expression x - 0.3142. So you are asking prod to give you product of this single element. Mathematically you may think that product of one thing, is itself, but then why to use prod at all?

Now let's assume you wanted product of x - xi where xi runs through xcol_list, then it may help you to remember the Sigma and Pi notation of sum and product in math, you write a Big Sigma/Pi, in front of it x - xi, but then below the big Sigma/Pi you write xi in x_col_list right? Or you might have seen i=0 below and n-1 above. Here you can do the same. Why didn't I use args? Well, args is the counter of your for-loop, and in this line you are still inside this loop, so it is not free yet to be re-used for a different task. Even if you don't get an error, it is not a good practice.

So in the end your prod( x - args ) will be replaced by prod( x - xi for xi in x_col_list ). Your code will be syntax/runtime error free, but then you should think if your code is really computing the polynomial that you wanted? That I leave to yourself.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.