简体   繁体   中英

Generalised hypergeometric function for numpy

I'm looking to fit some data to the hypergeometric function. I'm using the generalised hypergeometric function given in mpmath , hyper . I'm trying to convert it to work with curve_fit using np.frompyfunc . When I do

np_hyp = np.frompyfunc(hyper,3,1)
np_hyp([-1/3],[-2/3,2/3],x**2/4)

where x is some numpy array. The error I get is len(a_s): 'float' object has no length , or something to that effect (I'll be more accurate when I can get back to my PC to replicate the error). I suspect it's something to do with the inputs being lists and converting weirdly when numpy tries to convert the function.

Does anyone know a way to fix this error? Any help would be greatly appreciated.

Turns out my comment from above is true, ie the first and second list is also decomposed and passed as single element. This is not supposed to happen. The solution is, hence,

from mpmath import hyper
import numpy as np

print( hyper( [ -1 / 3 ],[ -2 / 3, 2 / 3 ], 0.255 ) )

nphyper = np.vectorize( hyper )
nphyper.excluded.add(0)
nphyper.excluded.add(1)

print( 
    nphyper(
        [ -1 / 3 ],
        [ -2 / 3, 2 / 3 ],
        np.array( [ 0.255, 0.257 ] )
    )
)

It is not clear from the docs so thanks to this post, I figured how to exclude positional arguments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM