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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:38:54+00:00 2026-06-04T22:38:54+00:00

I am trying to improve numpy performance by applying operations on a 2d array,

  • 0

I am trying to improve numpy performance by applying operations on a 2d array, the problem is that the value at each element in the array depends on the i,j location of that element.

Obviously the easy way to do this is to use a nested for-loop, but I was wondering if there might be a better way by referencing np.indices or something along those lines? Here is my ‘stupid’ code:

for J in range(1025):
    for I in range(1025):
        PSI[I][J] = A*math.sin((float(I+1)-.5)*DI)*math.sin((float(J+1)-.5)*DJ)
        P[I][J] = PCF*(math.cos(2.*float(I)*DI)+math.cos(2.*float(J)*DJ))+50000.
  • 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-04T22:38:55+00:00Added an answer on June 4, 2026 at 10:38 pm

    Since you’re doing multiplication among your two arrays, you can use the outer function, after using arange to get arrays of your sin/cos.

    Something like this (use numpy’s trig functions, since they’re vectorized)

    PSI_i = numpy.sin((arange(1,1026)-0.5)*DI)
    PSI_j = numpy.sin((arange(1,1026)-0.5)*DJ)
    PSI = A*outer(PSI_i, PSI_j)
    
    P_i = numpy.cos(2.*arange(1,1026)*DI)
    P_j = numpy.cos(2.*arange(1,1026)*DJ)
    P = PCF*outer(P_i, P_j) + 50000
    

    If your environment is set up using from numpy import * or from pylab import *, then you don’t need those numpy. prefixes before your trig functions. I kept them in to distinguish them from the math ones, which won’t work for this approach.

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

Sidebar

Related Questions

I'm trying to improve the performance on a query that is running very slowly.
I'm trying to improve performance for an application that does a lot of bit
I'm trying to improve a legacy database. One problem is that it's missing a
I'm trying to improve the performance of a website written in classic ASP. It
I'm trying to improve performance in our app. I've got performance information in the
I'm trying to improve performance in a torrent tracker site . But I've been
I'm trying to improve the performance of my web app where a page does
I am trying to improve performance by limiting my objects in memory by using
I trying to improve a textbox that filters a gridview, by implementing an enhanced
I'm trying to improve my current algorithm for the 8 Queens problem, and this

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.