1

I have the following Python code.

for i,j,k in itertools.product(range(theta),range(gamma),range(N)):
    L = (gamma[j]*math.pi)*((theta[i])**2) + x[n]

Since I have three for loops, the variable L is essentially a 3-dimensional matrix. Is it possible to index the variable L? Something like, L[i,j,k].

6
  • can you give us an example of a 3D matrix, and the output you want? Commented Jul 23, 2020 at 11:43
  • I think you should use numpy array for this Commented Jul 23, 2020 at 11:47
  • I am just seeking a way to index the variable L. Currently, the output appears as a vector, but as you can see, L is essentially 3-dimensional. Commented Jul 23, 2020 at 11:51
  • I like your first answer, if you added some code you would be able to reference L[i][j][k], but before storing the ref you would have to validate you had the base array, the arrays of arrays and then the arrays of arrays of arrays that support the 3 dimensions. the hash using the tuple of the indices as the key is a very elegant solution Commented Jul 23, 2020 at 11:56
  • I think you need to show more of your code, as regards what you want L to do. Are you trying to construct a 3-d array of values or simply fetch an L value on each iteration to use in some other expression? Commented Jul 23, 2020 at 12:10

2 Answers 2

2

If what you want to get is the value of L seen as a function of i,j,k (get a specific value of L for each triplet i,j,k), the simplest way is probably to make it a hash and use a three dimensions tuple as index.

Your code would become something like that:

L = {}
for i,j,k in itertools.product(range(theta),range(gamma),range(N)):
    L[(i,j,k)] = (gamma[j]*math.pi)*((theta[i])**2) + x[k]

Beware, when done this way not all cells of "matrix" are defined, only the cells actually containing something, and you will have to manage the case when the tuple index match nothing.

Of course you can also use a numpy matrix as suggested by Mike67, the tuple trick would be most useful for cases when you don't actually loop over all possible values of theta, gamma and range but only specific interesting cases.

PS: I changed n to k in the formula as I believe it's a typo

Sign up to request clarification or add additional context in comments.

2 Comments

Hi Kriss, as it happens, Mike67's reply makes more sense to my work. So let me please pick his answer over yours. Nonetheless, it was useful to learn something new from your response.
@Nanda: sure, pick the best answer, using numpy methods is usually much more efficient that calling standard python. I should have completed my answer with a simlar proposal as mike67's. Glad he did it.
1

If using numpy with a multi-dimensional array, you can use itertools.product to iterate across all dimensions of the array.

import numpy as np

L = np.zeros((theta, gamma, N))
for i,j,k in itertools.product(range(theta),range(gamma),range(N)):
    L[i,j,k] = (gamma[j]*math.pi)*((theta[i])**2) + x[n]

1 Comment

Thanks, Mike67. Your suggestion worked better for my application.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.