views:

81

answers:

1

Hello!

I am trying to do a fit to a given function using Scipy. Scipy.optimize.leastsq needs a vectorized function as one of the input parameters. This all works fine, but now I have a more complicated function which is not vectorized automatically by Scipy/Numpy.

def f1(a, parameters):
    b, c = parameters
    result = scipy.integrate.quad(integrand, lower, upper, (a, b, c))
    return result

or to give a closed example numpy.vectorize also does not work with

def f2(a, parameters):
    b, c = parameters
    return a+b+c

Is there a possibility to vectorize these functions in Scipy/Numpy?

Thank you for any help! Alexander

A: 

Sorry, I'm not sure what the question is. Python *args collects any number of args, which a function can unpack as it pleases; see docs.python.org/tutorial/...

import numpy as np
from scipy.integrate import quad

def f2( a, *args ):
    print "args:", args
    return a + np.sum( args, axis=0 )

x = np.ones(3)
print f2( x, x*2, x*3 )


def quadf( *args ):
    print "quadf args:", args
    return 1

quad( quadf, 0, 1, (2,3) )
Denis
Thank you. That was almost what I was looking for. While numpy.vectorize does not work on my function with *args, numpy.frompyfunc accepts it if I tell it the number of input parameters of the function.
Alexander