replaced with:
a=[2.0, 3.0]
b=[4.0, 5.0]
c = gamminc(a,b)
print c
Thanks,
Ben
No, this is not currently possible. However, a very easy way to do it
for individual functions is to wrap them with numpy.frompyfunc:
>>> from mpmath import gammainc
>>> from numpy import frompyfunc
>>> g = frompyfunc(gammainc, 2, 1)
>>> a = [2, 3]
>>> b = [4, 5]
>>> g(a, b)
array([0.0915781944436709, 0.249304038966162], dtype=object)
Fredrik
v/r
Scott
import numpy
import sympy, mpmath
sympy.var('x1,x2,a,b')
global a,b
f1=list([a*x1**2 + x2, b*5*x1**2 - 3*x1 + 2*x2 - 3])
x=list([x1,x2])
x0=numpy.array([.5,.5])
f=sympy.lambdify((x+[a,b]),f1)
def F(x):
global a,b #passing as args=a,b preferred
para=list([a,b])
args=x+para
return f(*args)# len(f)=len(x),type float
a=3
b=2
x01=numpy.copy(x0)
mpmath.findroot(F,x0)
How to best relate f1,x and para list is the question?
Thanks for the tips.
import sympy as sy
import numpy
sy.var('x1,x2,a,b')
global a,b
# assume f_list is a "long" list of "functions" generated by another
sympy script
# f_list may contain more than 100 terms
f_list=list([a*x1**2 + x2, b*5*x1**2 - 3*x1 + 2*x2 - 3])
x=list([x1,x2])#degrees of freedom or unknowns
parameters=list([a,b])#knowns
x0=numpy.array([1,1])#
args=x+parameters
b=1
a=2
# make the symbolic functions more 'numerical'
F1=sy.lambdify(args,f_list)
#reshuffle F1 as an fsolve freindly function with global parameters
def F2(x):
global a,b
parameters=list([a,b])
args=tuple(x)+tuple(parameters)
return F1(*args)
#F2(x)-> F3(x1,x2) for mpmath.findroot
f3='def F3(%s):\n\treturn F2(tuple(%s))'%(str(x)[1:-1],str(x))
exec f3
print(sy.mpmath.findroot(F3,list(x0)))
Sorry for the late reply.
This is because findroot() does not support functions with vectors as
arguments, you need to "flatten" the arguments.
For example:
from mpmath import *
f = lambda x1, x2, a, b: [a*x1**2 + x2, b*5*x1**2 - 3*x1 + 2*x2 - 3]
myf = lambda x1, x2: f(x1, x2, 3, 2)
x0 = (0.5, 0.5)
print findroot(myf, x0)
Or in your case:
import sympy
import sympy.mpmath as mpmath
sympy.var('x1,x2,a,b')
f1 = [a*x1**2 + x2, b*5*x1**2 - 3*x1 + 2*x2 - 3]
x = [x1, x2]
x0 = [.5, .5]
f = sympy.lambdify((x + [a, b]),f1)
def F(x1, x2):
global a, b # passing as args=a,b preferred
return f(x1, x2, a, b)
a = 3
b = 2
print mpmath.findroot(F, x0)
I don't understand how 'args=a,b' could work, but I agree that using
globals is ugly. Alternatives are:
1. hardcode the parameters in a new function (like in my first
code listing)
2. create a simple class with a __call__ method that use the
parameters as attributes (so you can change them later without
redefining the function)
Hope this helps.
Vinzent
Sorry, I missed this post for some reason, you already got the
solution. :)
Vinzent