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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:52:43+00:00 2026-06-17T15:52:43+00:00

I am trying to use NumPy and vectorization operations to make a section of

  • 0

I am trying to use NumPy and vectorization operations to make a section of code run faster. I appear to have a misunderstanding of how to vectorize this code, however (probably due to an incomplete understanding of vectorization).

Here’s the working code with loops (A and B are 2D arrays of a set size, already initialized):

for k in range(num_v):
    B[:] = A[:]
    for i in range(num_v):
        for j in range(num_v):
            A[i][j] = min(B[i][j], B[i][k] + B[k][j])
return A

And here is my attempt at vectorizing the above code:

for k in range(num_v):
    B = numpy.copy(A)
    A = numpy.minimum(B, B[:,k] + B[k,:])
return A

For testing these, I used the following, with the code above wrapped in a function called ‘algorithm’:

def setup_array(edges, num_v):
    r = range(1, num_v + 1)
    A = [[None for x in r] for y in r]  # or (numpy.ones((num_v, num_v)) * 1e10) for numpy
    for i in r:
        for j in r:
            val = 1e10
            if i == j:
                val = 0 
            elif (i,j) in edges:
                val = edges[(i,j)]
            A[i-1][j-1] = val 
    return A

A = setup_array({(1, 2): 2, (6, 4): 1, (3, 2): -3, (1, 3): 5, (3, 6): 5, (4, 5): 2, (3, 1): 4, (4, 3): 8, (3, 4): 6, (2, 4): -4, (6, 5): -5}, 6) 
B = []
algorithm(A, B, 6)

The expected outcome, and what I get with the first code is:

[[0, 2, 5, -2, 0, 10] 
 [8, 0, 4, -4, -2, 9]
 [4, -3, 0, -7, -5, 5]
 [12, 5, 8, 0, 2, 13]
 [10000000000.0, 9999999997.0, 10000000000.0, 9999999993.0, 0, 10000000000.0]
 [13, 6, 9, 1, -5, 0]]

The second (vectorized) function instead returns:

[[ 0. -4.  0.  0.  0.  0.]
 [ 0. -4.  0. -4.  0.  0.]
 [ 0. -4.  0.  0.  0.  0.]
 [ 0. -4.  0.  0.  0.  0.]
 [ 0. -4.  0.  0.  0.  0.]
 [ 0. -4.  0.  0. -5.  0.]]

What am I missing?

  • 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-17T15:52:43+00:00Added an answer on June 17, 2026 at 3:52 pm

    The problem is caused by array broadcasting in the line:

    A = numpy.minimum(B, B[:,k] + B[k,:])
    

    B is size 6 by 6, B[:,k] is an array with 6 elements, B[k,:] is an array with 6 elements.

    (Because you are using the numpy array type, both B[:,k] and B[k,:] return a rank-1 array of shape N)

    Numpy automatically changes the sizes to match:

    1. First B[:,k] is added to B[k,:] to make an intermediate array result with 6 elements. (This is not what you intended)
    2. Second this 6 element array is broadcast to a 6 by 6 matrix by repeating the rows
    3. Third the minimum of the original matrix and this broadcast matrix is computed.

    This means that your numpy code is equivalent to:

    for k in range(num_v):
       B[:] = A[:]
       C=[B[i][k]+B[k][i] for i in range(num_v)]
       for i in range(num_v):
          for j in range(num_v):
             A[i][j] = min(B[i][j], C[j])
    

    The easiest way to fix your code is to use the matrix type instead of the array type:

    A = numpy.matrix(A)
    for k in range(num_v):
        A = numpy.minimum(A, A[:,k] + A[k,:])
    

    The matrix type uses stricter broadcasting rules so in this case:

    1. A[:,k] is extended to a 6 by 6 matrix by repeating columns
    2. A[k,:] is extended to a 6 by 6 matrix by repeating rows
    3. The broadcasted matrices are added together to make a 6 by 6 matrix
    4. The minimum is applied
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make some code run faster by using pypy . However, whenever
I'm trying to pass the values that I want numpy.arange to use. The code
I'm trying to subclass numpy.complex64 in order to make use of the way numpy
This is a pretty basic question. I'm trying to use SciPy/NumPy to perform fft
I was trying to make a contour plot using numpy.meshgrid and pylab.imshow(); this worked
I'm trying to use a matrix to compute stuff. The code is this import
I am trying use gem tire to search in my application. I have tables
I was trying use a set of filter functions to run the appropriate routine,
Hi I'm trying use a datepicker on a field I have. I'm trying to
I am need paint my image. I'm trying use JQuery in here this link:

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.