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

  • SEARCH
  • Home
  • 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 6650131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:51:24+00:00 2026-05-26T00:51:24+00:00

I have a function in python which takes a vector and returns a real

  • 0

I have a function in python which takes a vector and returns a real number. I am using the scipy.optimize fmin and fmin_bfgs functions to find the argument which gives the function its approx minimum value. However, when I use fmin I get an alright answer (quite slowly) but when I switch to fmin_bfgs, I get an error saying “Matrices are not aligned”. Here’s my function:

    def norm(b_):
        b_ = b_.reshape(int(M),1)                   #M already given elsewhere
        Yb = np.dot(Y,b_)                           #Y already given elsewhere

        B = np.zeros((int(M),int(M))) 
        for j in xrange(int(M)):
            B[j][j] = -t[j+1]*np.exp(-t[j+1]*Yb[j]) #The t[j] are already known
        P = np.zeros((int(M),1))
        for j in xrange(int(M)):
            P[j][0] = np.exp(-t[j+1]*Yb[j])

        diff = np.zeros((int(M),1))                 #Functions d(i,b) are known
        for i in xrange(1,int(M)-1):
            diff[i][0] = d(i+1,b_) - d(i,b_)
        diff[0][0] = d(1,b_)
        diff[int(M)-1][0] = -d(int(M)-1,b_)

        term1_ = (1.0/N)*(np.dot((V - np.dot(c,P)).transpose(),W))
        term2_ = np.dot(W,V - np.dot(c,P))          #V,c,P,W already known
        term1_ = np.dot(term1_,term2_)
        term2_ = lambd*np.dot(Yb.transpose(),diff)

        return term1_ + term2_

Here’s how I call fmin_bfgs:

    fmin_bfgs(norm, b_guess,fprime=None,
              args=(),gtol=0.0001,norm=0.00000000001,
              epsilon=1.4901161193847656e-08,maxiter=None,
              full_output=0, disp=1, retall=0, callback=None)

When I call fmin it works fine, just too slowly to be useful (I need to optimise several times). But when I try fmin_bfgs I get this error:

Traceback (most recent call last):
File “C:\Program Files\Wing IDE 101 4.0\src\debug\tserver_sandbox.py”, line 287, in module
File “C:\Python27\Lib\site-packages\scipy\optimize\optimize.py”, line 491, in fmin_bfgs old_fval,old_old_fval)
File “C:\Python27\Lib\site-packages\scipy\optimize\linesearch.py”, line 239, in line_search_wolfe2 derphi0, c1, c2, amax)
File “C:\Python27\Lib\site-packages\scipy\optimize\linesearch.py”, line 339, in scalar_search_wolfe2 phi0, derphi0, c1, c2)
File “C:\Python27\Lib\site-packages\scipy\optimize\linesearch.py”, line 471, in _zoom derphi_aj = derphi(a_j)
File “C:\Python27\Lib\site-packages\scipy\optimize\linesearch.py”, line 233, in derphi return np.dot(gval[0], pk)
ValueError: matrices are not aligned

Any ideas why this might happen? All the matrices I have supplied the function are aligned correctly (and the function works since fmin worked). Help much appreciated!

  • 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-05-26T00:51:24+00:00Added an answer on May 26, 2026 at 12:51 am

    It seems that one of the programs just ended up dealing with numbers that were too large for it to handle. Shame it couldn’t tell me it was doing that properly. I worked around it though, so no more problem. Sorry if this wasted your time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two Python functions, both of which take variable arguments in their function
I'm using boost python. I've exported some function which takes class CL_DomElement in arguments.
I have a Python function in which I am doing some sanitisation of the
I have been wondering about the reload() function in python, which seems like it
I have a Python function that takes a numeric argument that must be an
Say I have a Python function that returns multiple values in a tuple: def
I have a python script which calls a lot of shell functions. The script
I have a function named OpenAccount() which takes in the details from the User
I have a function that takes a list of floats and returns the same
I've got a Perl function which takes a timestamp and returns either the unchanged

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.