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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:47:25+00:00 2026-05-25T17:47:25+00:00

I want to multiply a sparse matrix A, with a matrix B which has

  • 0

I want to multiply a sparse matrix A, with a matrix B which has 0, -1, or 1 as elements. To reduce the complexity of the matrix multiplication, I can ignore items if they are 0, or go ahead and add the column without multiplication if the item is 1, or subs. if it’s -1. The discussion about this is here:

Random projection algorithm pseudo code

Now I can go ahead and implement this trick but I wonder if I use Numpy’s multiplication functions it’ll be faster.

Does anyone knows if they optimised matrix multiplication for such matrices? Or can you suggest something to speed this process up since I have a matrix 300000×1000.

  • 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-25T17:47:25+00:00Added an answer on May 25, 2026 at 5:47 pm

    Have you looked at scipy.sparse? There’s no point in re-inventing the wheel, here. Sparse matricies are a fairly standard thing.

    (In the example, I’m using a 300000x4 matrix for easier printing after the multiplication. A 300000x1000 matrix shouldn’t be any problem, though. This will be much faster than multiplying two dense arrays, assuming you have a majority of 0 elements.)

    import scipy.sparse
    import numpy as np
    
    # Make the result reproducible...
    np.random.seed(1977)
    
    def generate_random_sparse_array(nrows, ncols, numdense):
        """Generate a random sparse array with -1 or 1 in the non-zero portions"""
        i = np.random.randint(0, nrows-1, numdense)
        j = np.random.randint(0, ncols-1, numdense)
        data = np.random.random(numdense)
        data[data <= 0.5] = -1
        data[data > 0.5] = 1
        ij = np.vstack((i,j))
        return scipy.sparse.coo_matrix((data, ij), shape=(nrows, ncols))
    
    A = generate_random_sparse_array(4, 300000, 1000)
    B = generate_random_sparse_array(300000, 5, 1000)
    
    C = A * B
    
    print C.todense()
    

    This yields:

    [[ 0.  1.  0.  0.  0.]
     [ 0.  2. -1.  0.  0.]
     [ 1. -1.  0.  0.  0.]
     [ 0.  0.  0.  0.  0.]]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a variable which contain the textfield value. I want to multiply that
I have a question which might be for beginners. I want to multiply a
Hey! I have matrix class and i want to add, multiply and take transpose
I want to multiply two matrices but the triple loop has O(n 3 )
I want to multiply 2 matrix using Jama library but it returns: A col:
When I pull the values I want to multiply, they're strings. So I pull
I want to multiply long numbers which are given in a 2^32 basis. I
i want to multiply two 512 bit integers in java and store the result.suggest
I want to find a text * 50 (multiply sign space 50). How ?
I want to multiply with SSE4 a __m128i object with 16 unsigned 8 bit

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.