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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:26:59+00:00 2026-06-04T15:26:59+00:00

I have some board numpy arrays like that: array([[0, 0, 0, 1, 0, 0,

  • 0

I have some board numpy arrays like that:

array([[0, 0, 0, 1, 0, 0, 0, 0],
       [1, 0, 0, 0, 0, 1, 0, 1],
       [0, 0, 0, 0, 0, 0, 0, 1],
       [0, 1, 0, 1, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 1],
       [0, 0, 0, 0, 1, 0, 0, 0],
       [0, 0, 1, 0, 0, 0, 1, 0],
       [1, 0, 0, 0, 0, 1, 0, 0]])

And I’m using the following code to find the sum of elements on each nth diagonal from -7 to 8 of the board (and the mirrored version of it).

n = 8
rate = [b.diagonal(i).sum()
        for b in (board, board[::-1])
        for i in range(-n+1, n)]

After some profiling, this operation is taking about 2/3 of overall running time and it seems to be because of 2 factors:

  • The .diagonal method builds a new array instead of a view (looks like numpy 1.7 will have a new .diag method to solve that)
  • The iteration is done in python inside the list comprehension

So, there are any methods to find these sums faster (possibly in the C layer of numpy)?


After some more tests, I could reduce 7.5x the total time by caching this operation… Maybe I was looking for the wrong bottleneck?


One more thing:

Just found the .trace method that replaces the diagonal(i).sum() thing and… There wasn’t much improvement in performance (about 2 to 4%).

So the problem should be the comprehension. Any ideas?

  • 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-04T15:27:01+00:00Added an answer on June 4, 2026 at 3:27 pm

    There’s a possible solution using stride_tricks. This is based in part on the plethora of information available in the answers to this question, but the problem is just different enough, I think, not to count as a duplicate. Here’s the basic idea, applied to a square matrix — see below for a function implementing the more general solution.

    >>> cols = 8
    >>> a = numpy.arange(cols * cols).reshape((cols, cols))
    >>> fill = numpy.zeros((cols - 1) * cols, dtype='i8').reshape((cols - 1, cols))
    >>> stacked = numpy.vstack((a, fill, a))
    >>> major_stride, minor_stride = stacked.strides
    >>> strides = major_stride, minor_stride * (cols + 1)
    >>> shape = (cols * 2 - 1, cols)
    >>> numpy.lib.stride_tricks.as_strided(stacked, shape, strides)
    array([[ 0,  9, 18, 27, 36, 45, 54, 63],
           [ 8, 17, 26, 35, 44, 53, 62,  0],
           [16, 25, 34, 43, 52, 61,  0,  0],
           [24, 33, 42, 51, 60,  0,  0,  0],
           [32, 41, 50, 59,  0,  0,  0,  0],
           [40, 49, 58,  0,  0,  0,  0,  0],
           [48, 57,  0,  0,  0,  0,  0,  0],
           [56,  0,  0,  0,  0,  0,  0,  0],
           [ 0,  0,  0,  0,  0,  0,  0,  7],
           [ 0,  0,  0,  0,  0,  0,  6, 15],
           [ 0,  0,  0,  0,  0,  5, 14, 23],
           [ 0,  0,  0,  0,  4, 13, 22, 31],
           [ 0,  0,  0,  3, 12, 21, 30, 39],
           [ 0,  0,  2, 11, 20, 29, 38, 47],
           [ 0,  1, 10, 19, 28, 37, 46, 55]])
    >>> diags = numpy.lib.stride_tricks.as_strided(stacked, shape, strides)
    >>> diags.sum(axis=1)
    array([252, 245, 231, 210, 182, 147, 105,  56,   7,  21,  42,  70, 105,
           147, 196])
    

    Of course, I have no idea how fast this will actually be. But I bet it will be faster than a Python list comprehension.

    For convenience, here’s a fully general diagonals function. It assumes you want to move the diagonal along the longest axis:

    def diagonals(a):
        rows, cols = a.shape
        if cols > rows:
            a = a.T
            rows, cols = a.shape
        fill = numpy.zeros(((cols - 1), cols), dtype=a.dtype)
        stacked = numpy.vstack((a, fill, a))
        major_stride, minor_stride = stacked.strides
        strides = major_stride, minor_stride * (cols + 1)
        shape = (rows + cols - 1, cols)
        return numpy.lib.stride_tricks.as_strided(stacked, shape, strides)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a message board, and I have some code that loads new messages
I have some Linq code, that works fine. It retrieves a list of board
I have some files that are uuencoded, and I need to decode them, using
I have some jquery code that is doing an ajax lookup and returning comma
I have the following code that is to be propperly converted to cython: from
Let's say I have a function called MyFunction(int myArray[][]) that does some array manipulations.
I'm working on a simple board game in qt, and i have some problem
I have some script in my default page that redirects users to language specific
I have some Java code which performs bitwise operations on a BitSet. I have
I have some XML which contains records and sub records, like this: <data> <record

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.