I am working through an assignment and keep getting the following error:
ValueError: operands could not be broadcast together with shapes (20,) (6,)
The code is below
import numpy as np
import scipy as sp
def fun2(x):
serval=7
# This is just the size of the taylor series
# eventually I will make the user enter this
z=np.arange(1,serval,1)
# Here I am creating a range for the taylor series
v= (x**z)/(sp.special.factorial(z))
# If I remove the x^z part of this equation
# It seems to work. For some reason it gets stuck here
u=np.sum(v)
y=(3*x-10)*(1+u)
# These two lines dont pose any problems
return y
I have done some troubleshooting and it looks like the statement x**z is tripping it up. Can I get some guidance please. For the record I am running a taylor series so the formula I am going for is (3x-10)(1+x/1!+x^2/2!....)
The 20 as I can tell is the size of the x array I am feeding the function. the 6 is the array inside the function, ie z.
Thanks in advance for any advice.
I am working through an assignment and keep getting the following error:
ValueError: operands could not be broadcast together with shapes (20,) (6,)
The code is below
import numpy as np
import scipy as sp
def fun2(x):
serval=7
# This is just the size of the taylor series
# eventually I will make the user enter this
z=np.arange(1,serval,1)
# Here I am creating a range for the taylor series
v= (x**z)/(sp.special.factorial(z))
# If I remove the x^z part of this equation
# It seems to work. For some reason it gets stuck here
u=np.sum(v)
y=(3*x-10)*(1+u)
# These two lines dont pose any problems
return y
I have done some troubleshooting and it looks like the statement x**z is tripping it up. Can I get some guidance please. For the record I am running a taylor series so the formula I am going for is (3x-10)(1+x/1!+x^2/2!....)
The 20 as I can tell is the size of the x array I am feeding the function. the 6 is the array inside the function, ie z.
Thanks in advance for any advice.
Share Improve this question edited 2 days ago simon 5,4311 gold badge16 silver badges29 bronze badges asked Feb 28 at 7:23 Kourosh KeshavarzKourosh Keshavarz 1 New contributor Kourosh Keshavarz is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 3 |1 Answer
Reset to default 0In context you probably meant to use an outer
(an equivalent is possible with newaxis
); but you can't simply mash two arrays together with different sizes.
import numpy as np
import scipy as sp
def fun2(x: np.ndarray, serval: int = 7):
# Range for the taylor series
z = np.arange(1, serval)
v = np.pow.outer(x, z)/sp.special.factorial(z)
u = np.sum(v, axis=-1)
y = (3*x - 10)*(1 + u)
return y
def demo() -> None:
rand = np.random.default_rng(seed=0)
print(fun2(x=rand.random(20)))
if __name__ == '__main__':
demo()
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1740998535a4288977.html
v
for every combination ofx
andz
you can replace that line withv= (x[:, None]**z)/(sp.special.factorial(z))
, which will create an array of shape (20, 6) – Nin17 Commented Feb 28 at 8:33axis=1
. – Matt Haberland Commented 2 days ago