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 7594635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:28:09+00:00 2026-05-30T21:28:09+00:00

Problem I would like to compute the following using numpy or scipy: Y =

  • 0

Problem

I would like to compute the following using numpy or scipy:

Y = A**T * Q * A

where A is a m x n matrix, A**T is the transpose of A and Q is an m x m diagonal matrix.

Since Q is a diagonal matrix I store only its diagonal elements as a vector.

Ways of solving for Y

Currently I can think of two ways of how to calculate Y:

  1. Y = np.dot(np.dot(A.T, np.diag(Q)), A) and
  2. Y = np.dot(A.T * Q, A).

Clearly option 2 is better than option 1 since no real matrix has to be created with diag(Q) (if this is what numpy really does…)
However, both methods suffer from the defect of having to allocate more memory than there really is necessary since A.T * Q and np.dot(A.T, np.diag(Q)) have to be stored along with A in order to calculate Y.

Question

Is there a method in numpy/scipy that would eliminate the unnecessary allocation of extra memory where you would only pass two matrices A and B (in my case B is A.T) and a weighting vector Q along with it?

  • 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-30T21:28:10+00:00Added an answer on May 30, 2026 at 9:28 pm

    (w/r/t the last sentence of the OP: i am not aware of such a numpy/scipy method but w/r/t the Question in the OP Title (i.e., improving NumPy dot performance) what’s below should be of some help. In other words, my answer is directed to improving performance of most of the steps comprising your function for Y).

    First, this should give you a noticeable boost over the vanilla NumPy dot method:

    >>> from scipy.linalg import blas as FB
    >>> vx = FB.dgemm(alpha=1., a=v1, b=v2, trans_b=True)
    

    Note that the two arrays, v1, v2 are both in C_FORTRAN order

    You can access the byte order of a NumPy array through an array’s flags attribute like so:

    >>> c = NP.ones((4, 3))
    >>> c.flags
          C_CONTIGUOUS : True          # refers to C-contiguous order
          F_CONTIGUOUS : False         # fortran-contiguous
          OWNDATA : True
          MASKNA : False
          OWNMASKNA : False
          WRITEABLE : True
          ALIGNED : True
          UPDATEIFCOPY : False
    

    to change the order of one of the arrays so both are aligned, just call the NumPy array constructor, pass in the array and set the appropriate order flag to True

    >>> c = NP.array(c, order="F")
    
    >>> c.flags
          C_CONTIGUOUS : False
          F_CONTIGUOUS : True
          OWNDATA : True
          MASKNA : False
          OWNMASKNA : False
          WRITEABLE : True
          ALIGNED : True
          UPDATEIFCOPY : False
    

    You can further optimize by exploiting array-order alignment to reduce excess memory consumption caused by copying the original arrays.

    But why are the arrays copied before being passed to dot?

    The dot product relies on BLAS operations. These operations require arrays stored in C-contiguous order–it’s this constraint that causes the arrays to be copied.

    On the other hand, the transpose does not effect a copy, though unfortunately returns the result in Fortran order:

    Therefore, to remove the performance bottleneck, you need to eliminate the predicate array-copying step; to do that just requires passing both arrays to dot in C-contiguous order*.

    So to calculate dot(A.T., A) without making an extra copy:

    >>> import scipy.linalg.blas as FB
    >>> vx = FB.dgemm(alpha=1.0, a=A.T, b=A.T, trans_b=True)
    

    In sum, the expression just above (along with the predicate import statement) can substitute for dot, to supply the same functionality but better performance

    you can bind that expression to a function like so:

    >>> super_dot = lambda v, w: FB.dgemm(alpha=1., a=v.T, b=w.T, trans_b=True)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

while designing my user control, i encountered the following problem: i would like to
Problem: I would like to share code between multiple assemblies. This shared code will
I have a reasonably complex layout problem: I would like to have a main
I have a problem and i would like to learn the correct way to
I have a problem that I would like have solved via a SQL query.
I have a little problem where I would like to insert a svn diff
I have an interesting genetics problem that I would like to solve in native
I need to solve a job affectation problem and I would like to find
I would like solve the problem (now hypothetical but propably real in future) of
I would like your advice about how best to solve my problem. In a

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.