Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8690441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:54:09+00:00 2026-06-12T23:54:09+00:00

I have a simple function def square(x, a=1): return [x**2 + a, 2*x] I

  • 0

I have a simple function

def square(x, a=1):
    return [x**2 + a, 2*x]

I want to minimize it over x, for several parameters a. I currently have loops that, in spirit, do something like this:

In [89]: from scipy import optimize

In [90]: res = optimize.minimize(square, 25, method='BFGS', jac=True)

In [91]: [res.x, res.fun]
Out[91]: [array([ 0.]), 1.0]

In [92]: l = lambda x: square(x, 2)

In [93]: res = optimize.minimize(l, 25, method='BFGS', jac=True)

In [94]: [res.x, res.fun]
Out[94]: [array([ 0.]), 2.0]

Now, the function is already vectorized

In [98]: square(array([2,3]))
Out[98]: [array([ 5, 10]), array([4, 6])]

In [99]: square(array([2,3]), array([2,3]))
Out[99]: [array([ 6, 12]), array([4, 6])]

Which means it would probably be much faster to run all the optimizations in parallel rather than looping. Is that something that’s easily do-able with SciPy? Or any other 3rd party tool?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T23:54:10+00:00Added an answer on June 12, 2026 at 11:54 pm

    Here’s another try, based on my original answer and the discussion that followed.

    As far as I know, the scipy.optimize module is for functions with scalar or vector inputs and a scalar output, or “cost”.

    Since you’re treating each equation as independent of the others, my best idea is to use the multiprocessing module to do the work in parallel. If the functions you’re minimizing are as simple as the ones in your question, I’d say it’s not worth the effort.

    If the functions are more complex, and you’d like to divide the work up, try something like:

    import numpy as np
    from scipy import optimize
    from multiprocessing import Pool
    
    def square(x, a=1):
        return [np.sum(x**2 + a), 2*x]
    
    def minimize(args):
        f,x,a = args
        res = optimize.minimize(f, x, method = 'BFGS', jac = True, args = [a])
        return res.x
    
    # your a values
    a = np.arange(1,11)
    
    # initial guess for all the x values
    x = np.empty(len(a))
    x[:] = 25
    
    args = [(square,a[i],x[i]) for i in range(10)]
    p = Pool(4)
    print p.map(minimize,args)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a simple function in python: def func(a,x): return a+x*2 and then
I have a simple function that looks at an incoming mySQL data type and
I have a simple function that works in chrome, but in Internet explorer (9)
I have a simple function that returns an NSString after decoding it. I use
I have a simple function that sorts a dictionary: data = inputfile.readlines() lineData =
Let's say I have a function def odd_or_even n if n%2 == 0 return
I have a simple Delete function to drop scheduled events. Here's the function: def
I have a simple view function that's designed to allow the user to choose
Simple example of a nested function: def maker(N): def action(X): return X * N
I have simple function that act like a client . It sents an event

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.