Using only a slice of meshgrid or replacing it for evaluting a function
<p>Edit: Solution</p>

<p>The answer was posted below, using list prod from itertools greatly reduces memory usage as it is a list object, something I overlooked in the thread I linked.</p>

<p>Code:</p>

<pre><code>n = 2 #dimension
r = np.arange(0.2,2.4,0.1) #range

grid = product(r,repeat=n)

for g in grid:
    y = np.array(g)
    if np.all(np.diff(g) &gt; 0) == True:
        print(f(y)) #or whatever you want to do
</code></pre>

<hr>

<p>I try to evalute a function with n parameters in a certain range. And I want to be able to change n in a certain range, so the user can determine it with his input. </p>

<p>I got a working code with help from <a href="http://stackoverflow.com/questions/39536288/variable-dimensionality-of-a-meshgrid-with-numpy">here</a> an it looks like this:</p>

<pre><code>import numpy as np

n = 2 #determine n
r = np.arange(0.2,2.4,0.1) #determine range
grid=np.array(np.meshgrid(*[r]*n)).T.reshape(-1,n) #creates a meshgrid for the range**n

#reduces the number of used points in the grid (the function is symmetrical in its input parameters)
for i in range(0,n-1):
    grid = np.array([g for g in grid if g[i]&lt;g[i+1]])

y = np.zeros((grid.shape[0]))
for i in range(grid.shape[0]):
    y[i] = f(grid[i,:]) #evaluating function for all lines in grid
</code></pre>

<p>This only works up to n equals 5 or 6 then the grid-array and the ouput array just gets to big for Spyder to handle it (~10 MB). </p>

<p>Is there a way to only create one line of the meshgrid (combination of parameters) at a time to evaluate the function with? Then I could save those values (grid,y) in a textfile and overwrite them in the next step.</p>

<p>Or is there a way to create all n-dimensional combinations in range without meshgrid but with a variable n and one at a time?</p>

<p>You may want to try iterator. It seems <a href="https://docs.python.org/3.6/library/itertools.html#itertools.product" rel="nofollow">itertools.product</a> fits your need.</p>

<pre><code>r = np.arange(0,3,1) #determine range
list(product(r,r))
#[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]
</code></pre>

<p>More detail:</p>

<pre><code>grid = product([r]*n)
y = np.zeros(... # you should know the exact size
i = 0
for g in grid:
    if ...:      # check if g in ascending order
        y[i] = f(g)
        i += 1
</code></pre>

<p>By the way the size of y should be <code>comb(len(r), 3)</code>, choose 3 from <code>len(r)</code></p>

<pre><code>from scipy.special import comb
comb(len(r), 3)
</code></pre>

<p>Or an one-liner</p>

<pre><code>y = [f(g) for g in grid if is_ascending(g)]
</code></pre>

<p>Where <code>is_ascending</code> is an user defined filter function.</p>

python arrays python-3.x numpy