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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T12:36:00+00:00 2026-06-02T12:36:00+00:00

I would like to implement a diagonal matrix apply function that is created by

  • 0

I would like to implement a diagonal matrix apply function that is created by providing the diagonal d first, and then doing a bunch of matrix-vector multiplications with x. Of course I wouldn’t want to create an actual matrix because all that’s needed is a elementwise vector multiplication.

Now, some users are going to provide a diagonal d of shape (k,), some of shape (k,1). Also, x can have shapes (k,) and (k,1). I would like the apply() method to behave just like the * for numpy matrices in that the result has the same shape as the input x.

Hence the question: In Python/Numpy, is there a non-iffy way to elementwise-multiply two np.arrays x and y of shapes (k,) or (k,1) (in any combination) such that the resulting array has the shape of x?

I experimented a little with [:,None],

x = np.empty((4,1))
y = np.empty(4)
(x * y).shape         # (4,4)  -- nope
(y * y).shape         # (4,)   -- yes
(x * y[:,None]).shape # (4, 1) -- yes
(y * y[:,None]).shape # (4,4)  -- nope

and I could certainly wrap my code in if len(x.shape)==...:, but that doesn’t feel very pythonic.

Suggestions?

  • 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-02T12:36:02+00:00Added an answer on June 2, 2026 at 12:36 pm

    Now that I understand your question, my suggestion would be simply to reshape. Calling reshape returns a view, so it doesn’t incur any big copying costs or anything like that. Simply reshape the arrays, multiply, and reshape again:

    >>> def shape_preserving_mult(x, y):
    ...     return (x.reshape((-1,)) * y.reshape((-1))).reshape(x.shape)
    ... 
    

    Or more concisely, as you and rroowwllaanndd pointed out:

    >>> def shape_preserving_mult(x, y):
    ...     return x * y.reshape(x.shape)
    ... 
    >>> shape_preserving_mult(x, y)
    array([[ 0],
           [ 1],
           [ 4],
           [ 9],
           [16]])
    >>> shape_preserving_mult(x, y.T)
    array([[ 0],
           [ 1],
           [ 4],
           [ 9],
           [16]])
    >>> shape_preserving_mult(x.T, y)
    array([[ 0,  1,  4,  9, 16]])
    >>> shape_preserving_mult(x.T, y.T)
    array([[ 0,  1,  4,  9, 16]])
    

    The substance of my previous suggestion remains below.

    It’s worth noting that if you multiply a numpy array of shape (1, 4) with an array of shape (4,) you get something close to what you want.

    >>> x = numpy.arange(5).reshape((5, 1))
    >>> y = numpy.arange(5)
    >>> x.shape
    (5, 1)
    >>> x.T.shape
    (1, 5)
    >>> y * x.T
    array([[ 0,  1,  4,  9, 16]])
    

    This doesn’t have the shape of a, but it does have the shape of a.T. You could always call T on the result again. This will work on arrays of shape (5,) too, because the transpose operation on a 1-d array causes no change. So perhaps you could do this:

    >>> def transposed_mult(x, y):
    ...     return (x.T * y).T
    ... 
    >>> transposed_mult(x, y)
    array([[ 0],
           [ 1],
           [ 4],
           [ 9],
           [16]])
    

    But of course this causes the opposite problem if you pass an array of shape (1, 5):

    >>> transposed_mult(x.T, y)
    array([[ 0,  0,  0,  0,  0],
           [ 0,  1,  2,  3,  4],
           [ 0,  2,  4,  6,  8],
           [ 0,  3,  6,  9, 12],
           [ 0,  4,  8, 12, 16]])
    

    So transposed_mult does the exact thing you asked for in your original post, but if you need any further flexibility, it won’t work as expected. And indeed, it seems you need additional flexibility.

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

Sidebar

Related Questions

I would like to implement a function that takes as input a size n
We would like to implement a web form that automatically saves content at regular
i would like to implement 'drawing modes' (in my own graphics library). That is
I would like to implement operations that is undoable in a short time after
I would like to implement an apps that allow users save the webpage including
I would like to implement the effect that a short line segment revolving around
I would like to implement a post build event that performs the following actions
I would like to implement a ORM-style system that can save updates to POJOs
I would like to implement a producer/consumer scenario that obeys interfaces that are roughly:
I would like to implement the following feature using jQuery scrollTo function or some

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.