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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T04:25:07+00:00 2026-06-19T04:25:07+00:00

I have a code in which I need to handle some big numpy arrays.

  • 0

I have a code in which I need to handle some big numpy arrays. For example I have a 3D array A and I need to construct another 3d array B using the elements of A. However all the elements of B are independent of each other. Example:

for i in np.arange(Nx):
  for j in np.arange(Ny):
   for k in np.arange(Nz):
       B[i][j][k] = A[i+1][j][k]*np.sqrt(A[i][j-1][k-1])

So it will speed up immensely if I can construct the B array parallely. What is the simplest way to do this in python?

I also have similar matrix operations like normalizing each row of a 2D array. Example

for i in np.arange(Nx):
   f[i,:] = f[i,:]/np.linalg.norm(f[i,:])

This will also speed up if it runs parallely for each row. How can it be done?

  • 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-19T04:25:09+00:00Added an answer on June 19, 2026 at 4:25 am

    You should look into Numpy’s roll function. I think this is equivalent to your first block of code (though you need to decide what happens at the edges – roll “wraps around”):

    B = np.roll(A,1,axis=0) * np.sqrt(np.roll(np.roll(A,-1,axis=1),-1,axis=2))
    

    Another fairly horrible one-liner for your second case is:

    f /= np.sqrt(np.sum(f**2, axis=1))[...,np.newaxis]
    

    Explanation of this line:

    We are first going to calculate the norm of each row. Let’s

    f = np.random.rand(5,6)
    

    Square each element of f

    f**2
    

    Sum the squares along axis 1, which “flattens” out that axis.

    np.sum(f**2, axis=1)
    

    Take the square root of the sum of the squares.

    np.sqrt(np.sum(f**2, axis=1))
    

    We now have the norm of each row.

    To divide each original row of f by this correctly we need to make use of the Numpy broadcasting rules to effectively add a dimension:

    np.sqrt(np.sum(f**2, axis=1))[...,np.newaxis]
    

    And finally we calculate our result

    f /= np.sqrt(np.sum(f**2, axis=1))[...,np.newaxis]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to integrate some legacy 32-bit code - for which I don't have
We have some client code which is using the SqlConnection class in .NET to
i need to set image as background in android gridview. I have code which
I have the following FORTRAN code which I need to convert to C or
I have following code which works for radio buttons but need to be changed
I need to sort nodes in xml. I have the following code which successfully
Need assistance with the pexpect module I have written a simple code which would
I have a string which I need to verify if it's a Country code.
I have an installer for which I don't have any source code. I need
I need to have a checkbox which ajax-submits a form. The following code throws

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.