# [SciPy-User] Avoiding inner for loops??

Issa Karambal issa@aims.ac...
Mon Aug 20 20:27:00 CDT 2012

```perhaps you can consider 'np.ogrid' for the product.
Ps: Is this code not helpful?
param1 = np.arange(5)
param2 = param1
params1 = param1*param2[:, None]
fake_model = data1+data2*params1.reshape(-
1)[:, None]
ans = np.reshape( fake_model, (5,5,5) )
print ans[0]
array([[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4]])

print ans[1]
array([[ 0,  1,  2,  3,  4],
[ 0,  2,  4,  6,  8],
[ 0,  3,  6,  9, 12],
[ 0,  4,  8, 12, 16],
[ 0,  5, 10, 15, 20]])

print ans[-1]
array([[ 0,  1,  2,  3,  4],
[ 0,  5, 10, 15, 20],
[ 0,  9, 18, 27, 36],
[ 0, 13, 26, 39, 52],
[ 0, 17, 34, 51, 68]])

On 21 August 2012 02:11, Martin De Kauwe <mdekauwe@gmail.com> wrote:

> Or perhaps an easier solution would be to build the sampling grid first
> and then use a single for loop to run the model?
>
> import itertools
> import numpy as np
>
> grid_size = 500
> a = np.linspace(5.0, 350, grid_size)
> b = np.linspace(5.0, 550, grid_size)
> c = np.linspace(1E-8, 10.5, grid_size)
>
> x = []
> for (i,j,k) in itertools.product(a, b, c):
>     x.extend((i,j,k))
>
> This would achieve what I want but is again very slow, so is there a way
> to jump over the need for the two inner loops?
>
> thanks
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20120821/4d124cfc/attachment-0001.html
```