0

Suppose you have few logical tables as follows:

import numpy as np
table1 = np.zeros((4,3)); table2 = np.zeros((4,3)); table3 = np.zeros((4,3));
table1[0] = 1; table2[1] = 1; table3[1] = table3[3] = 1;

Now suppose you have a set of arrays that change in shape during a simulation depending on certain conditions, let's call them equations:

eq1 =[[a,b,c], [d,e,f]]; eq2 = [[g,h,i],[j,k,l]]; eq3 = [[m,n,o],[p,q,r]]; 
#result array:
result = np.zeros((4,3))

The result array is a combination of the different equations assigned to different rows, using the logical tables as follows:

result = table1*eq1 + table2*eq2 + table3*eq3 

In a way that the end result is something like this:

result = [[a,b,c], [g +m,h+n,i+o], [0,0,0], [p,q,r]];

I know the code above has issues because of the different shapes. And I also know how to solve this particular problem to multiply element wise two arrays. But my question is in the general case. The array result is changing dynamically in a simulation loop because the equations are changing. So is there a way to have a general solution for this? In a sense if you have the array table1 of shape (4,3) for example and another array say eq1 it could be (2,3), it could be (2,2), etc.. but always less than or equal the dimensions of table1. So how to do this multiplication of the two, such that the elements of table1 that are outside the range of eq1 stays the same. Think about the simulation this way:

for time_simulation:
   Update(eq1,eq2,eq3)
   result = table1*eq1 + table2*eq2 + table3*eq3
   Use(result)

I hope my question is clear.

zizou
  • 67
  • 1
  • 5
  • It isn't cear to me how a (2,3) or (2,2) 'fits' in (4,3) result. So you have to clearly spell that out in your code. I'd focus on mapping/padding (n,m) table-n to the target (4,3) shape. – hpaulj May 22 '23 at 14:59
  • So the idea is not to fit the (2,3) in (4,3), but only to take (2,3) into the (4,3), and the remaining of the (4,3) stays the same. In a second iteration say, the (2,3) becomes (2,2), so now the job is to fit this (2,2) into the (4,3) and keep the rest of (4,3) the same. Hope this is clear! – zizou May 22 '23 at 16:07

1 Answers1

0

You could extract an array with fewer dimension through slicing. For example, if you know that eq1 has a Len (a2 - a1,b2 - b1):

working_array = table1[a1:a2,b1:b2]

However, you have to know which column and row of the table1 array you have to use for your multiplication of array. More context of the problem would be useful in order to get a more accurate answer.

Nils
  • 11
  • 4