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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:44:30+00:00 2026-05-22T22:44:30+00:00

Given a 2D numpy array, I need to compute the dot product of every

  • 0

Given a 2D numpy array, I need to compute the dot product of every column with itself, and store the result in a 1D array. The following works:

In [45]: A = np.array([[1,2,3,4],[5,6,7,8]])

In [46]: np.array([np.dot(A[:,i], A[:,i]) for i in xrange(A.shape[1])])
Out[46]: array([26, 40, 58, 80])

Is there a simple way to avoid the Python loop? The above is hardly the end of the world, but if there’s a numpy primitive for this, I’d like to use it.

edit In practice the matrix has many rows and relatively few columns. I am therefore not overly keen on creating temporary arrays larger than O(A.shape[1]). I also can’t modify A in place.

  • 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-22T22:44:31+00:00Added an answer on May 22, 2026 at 10:44 pm

    How about:

    >>> A = np.array([[1,2,3,4],[5,6,7,8]])
    >>> (A*A).sum(axis=0)
    array([26, 40, 58, 80])
    

    EDIT: Hmm, okay, you don’t want intermediate large objects. Maybe:

    >>> from numpy.core.umath_tests import inner1d
    >>> A = np.array([[1,2,3,4],[5,6,7,8]])
    >>> inner1d(A.T, A.T)
    array([26, 40, 58, 80])
    

    which seems a little faster anyway. This should do what you want behind the scenes, as A.T is a view (which doesn’t make its own copy, IIUC), and inner1d seems to loop the way it needs to.

    VERY BELATED UPDATE: Another alternative would be to use np.einsum:

    >>> A = np.array([[1,2,3,4],[5,6,7,8]])
    >>> np.einsum('ij,ij->j', A, A)
    array([26, 40, 58, 80])
    >>> timeit np.einsum('ij,ij->j', A, A)
    100000 loops, best of 3: 3.65 us per loop
    >>> timeit inner1d(A.T, A.T)
    100000 loops, best of 3: 5.02 us per loop
    >>> A = np.random.randint(0, 100, (2, 100000))
    >>> timeit np.einsum('ij,ij->j', A, A)
    1000 loops, best of 3: 363 us per loop
    >>> timeit inner1d(A.T, A.T)
    1000 loops, best of 3: 848 us per loop
    >>> (np.einsum('ij,ij->j', A, A) == inner1d(A.T, A.T)).all()
    True
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I sort a NumPy array by its nth column? For example, given:
Given the following NumPy array, > a = array([[1, 2, 3, 4, 5], [1,
I have a multidimensional numpy array, and I need to iterate across a given
Given a 3 times 3 numpy array a = numpy.arange(0,27,3).reshape(3,3) # array([[ 0, 3,
I have a 1D numpy array containing complex numbers (eg. numpy.complex64). I need to
Is there a numpy function that gives for a given numpy array its maximum
Given a NumPy array of int32 , how do I convert it to float32
So I have a need to pass around a numpy array in my PyQt
Given a numpy array and a __getitem__ -type index, is there an idiomatic way
Is it possible to map a NumPy array in place? If yes, how? Given

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.