3

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

0

1 Answer 1

2

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) )
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.