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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:54:05+00:00 2026-05-26T21:54:05+00:00

This is a simplification of my question. I have a numpy array: x =

  • 0

This is a simplification of my question. I have a numpy array:

x = np.array([0,1,2,3])

and I have a function:

def f(y): return y**2

I can compute f(x).

Now suppose I really want to compute f(x) for a repeated x:

x = np.array([0,1,2,3,0,1,2,3,0,1,2,3])

Is there a way to do this without creating a repeated version of x and in a way that is transparent to f?

In my particular case, f is an involved function and one of the arguments is x. I would like to be able to calculate f when x is repeated without actually repeating it as it wont fit into memory.

Rewriting f to handle repeated x would be work and I was hoping for a clever way possibly to subclass a numpy array to do this.

Any tips appreciated.

  • 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-26T21:54:06+00:00Added an answer on May 26, 2026 at 9:54 pm

    You can (almost) do this by using a few tricks with strides.

    However, there are some major caveats…

    import numpy as np
    x = np.arange(4)
    numrepeats = 3
    
    y = np.lib.stride_tricks.as_strided(x, (numrepeats,)+x.shape, (0,)+x.strides)
    
    print y
    x[0] = 9
    print y
    

    So, y is now a view into x where each row is x. No new memory is used, and we can make y as large as we like.

    For example, I can do this:

    import numpy as np
    x = np.arange(4)
    numrepeats = 1e15
    
    y = np.lib.stride_tricks.as_strided(x, (numrepeats,)+x.shape, (0,)+x.strides)
    

    …and not use any more memory than the 32 bytes required for x. (y would use ~8 Petabytes of ram, otherwise)

    However, if we reshape y so that it only has one dimension, we’ll get a copy which will use the full amount of memory. There’s no way to describe a “horizontally” tiled view of x using strides and shape, so any shape with less than 2 dimensions will return a copy.

    Additionally, if we operate on y in a way that would return a copy (e.g. the y**2 in your example), we’ll get a full copy.

    For that reason, it makes more sense to operate on things in-place. (e.g. y **= 2, or equivalently x **= 2. Both will accomplish the same thing.)

    Even for a generic function, you can pass in x and place the result back in x.

    E.g.

    def f(x):
        return x**3
    
    x[...] = f(x)
    print y
    

    y will be updated, as well, as it’s just a view into x.

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

Sidebar

Related Questions

I have this (simplification): $(li).click(function{alert(test);}); <li> <input> </li> What's the best way to bind
Update: What I really wanted all along were greenlets . Note: This question mutated
This is a simplification of the issue (there are lots of ways of doing
This is a bit of a long shot, but if anyone can figure it
This might seem like a stupid question I admit. But I'm in a small
This is a difficult and open-ended question I know, but I thought I'd throw
Here is an initial specification for a simple Address class. This is a simplification
I want to pass a Python function to another function with some of its
While looking at this code (most of which has been removed for simplification of
I need to run the following query (this is a simplification of my process):

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.