Add multiple arrays in Python

≯℡__Kan透↙ 提交于 2021-01-29 11:40:40

问题


What I have created so far

I have created an 18x18 square matrix of zeros called ‘master_matrix’.

I have created an array called ingreso_datos, whose column 0 [col 0] indicates the data label.

I have created a for loop where:

For each data label I will have a little_matrix whose values will be assigned to master_matrix in their corresponding rows and columns. As this happens inside a for loop, for this example I get 6 master_matrix (in my variable list only the last one is read, that is, the sixth master_matrix).

What i need to do

What I'm looking for now is to add the 6 master_matrix that come from the for loop.

I have tried to do it one by one but the problem is that the data could change and I think it would not be efficient, I would greatly appreciate your help, regards.

import numpy as np

I have created an 18x18 square matrix of zeros called ‘master_matrix’

 master_matrix = np.zeros((18, 18))

I have created an array called 'ingreso_datos', whose column 0 [col 0] indicates the data label.

#              Data label  |-------1(i)       2(i)       3(i)        1(j)      2(j)        3(j) --|
#               [Col0]     |------[Col1]     [Col2]     [Col3]      [Col4]    [Col5]      [Col6] --|

ingreso_datos = [[ 1,              13,       14,          15,          7,        8,          9],
                 [ 2,              16,       17,          18,         10,       11,       12],
                 [ 3,               7,        8,           9,          1,        2,        3],
                 [ 4,              10,       11,          12,          4,        5,        6],
                 [ 5,               7,        8,           9,         10,       11,         12],
                 [ 6,               1,        2,           3,          4,        5,        6]]

For each data label I will have a 'little_matrix' whose values will be assigned to 'master_matrix' in their corresponding rows and columns. As this happens inside a for loop, for this example I get 6 'master_matrix' (in my variable list only the last one is read, that is, the sixth 'master_matrix').

indices = []     # moved outside of the loop 
for i in range(len(ingreso_datos)):
    indices.append([ingreso_datos[i][0], ingreso_datos[i][1], ingreso_datos[i][2], ingreso_datos[i][3],
                    ingreso_datos[i][4], ingreso_datos[i][5], ingreso_datos[i][6]])   

for row in indices:
    indices = np.array(row[1:])
    indices -= 1

    d = 5
    s = 0.2          
    e = 0.05            
    y = 5000000           

    little_matrix = np.array([[ s*y/d,           0,           0,    -s*y/d,            0,           0],
                              [     0,    y*e/d**3,    y*e/d**2,         0,    -y*e/d**3,    y*e/d**2],
                              [     0,    y*e/d**2,       y*e/d,         0,    -y*e/d**2,       y*e/d],
                              [-s*y/d,           0,           0,     s*y/d,            0,           0],
                              [     0,    y*e/d**3,   -y*e/d**2,         0,     y*e/d**3,   -y*e/d**2],
                              [     0,    y*e/d**2,       y*e/d,         0,    -y*e/d**2,       y*e/d]])

    master_matrix[np.ix_(indices, indices)] = little_matrix

What I'm looking for now is to add the 6 master_matrix that come from the for loop.

    master_matrix.sum()

回答1:


In [68]: ingreso_datos = np.array([[ 1,              13,       14,          15,          7,    
    ...:     8,          9], 
    ...:                  [ 2,              16,       17,          18,         10,       11,   
    ...:     12], 
    ...:                  [ 3,               7,        8,           9,          1,        2,   
    ...:      3], 
    ...:                  [ 4,              10,       11,          12,          4,        5,   
    ...:      6], 
    ...:                  [ 5,               7,        8,           9,         10,       11,   
    ...:       12], 
    ...:                  [ 6,               1,        2,           3,          4,        5,   
    ...:      6]])                                                                             
In [69]: ingreso_datos.shape                                                                   
Out[69]: (6, 7)

That indices creation doesn't do anything significant:

In [70]: indices = []     # moved outside of the loop  
    ...: for i in range(len(ingreso_datos)): 
    ...:     indices.append([ingreso_datos[i][0], ingreso_datos[i][1], ingreso_datos[i][2], ing
    ...: reso_datos[i][3], 
    ...:                     ingreso_datos[i][4], ingreso_datos[i][5], ingreso_datos[i][6]])   
    ...:  
    ...:                                                                                       
In [71]: np.array(indices).shape                                                               
Out[71]: (6, 7)
In [72]: np.allclose(ingreso_datos, np.array(indices))                                         
Out[72]: True

little_matrix doesn't need to be in the loop, since it doesn't change:

In [74]: little_matrix                                                                         
Out[74]: 
array([[ 200000.,       0.,       0., -200000.,       0.,       0.],
       [      0.,    2000.,   10000.,       0.,   -2000.,   10000.],
       [      0.,   10000.,   50000.,       0.,  -10000.,   50000.],
       [-200000.,       0.,       0.,  200000.,       0.,       0.],
       [      0.,    2000.,  -10000.,       0.,    2000.,  -10000.],
       [      0.,   10000.,   50000.,       0.,  -10000.,   50000.]])

As for your iteration on row:

In [75]: np.array(indices[0][1:])-1                                                            
Out[75]: array([12, 13, 14,  6,  7,  8])
In [77]: ingreso_datos[0,1:]-1                                                                 
Out[77]: array([12, 13, 14,  6,  7,  8])

So your iterative assignment to master_matrix becomes:

In [78]: master_matrix=np.zeros((18,18)) 
In [80]: for row in ingreso_datos: 
    ...:     indices = row[1:]-1 
    ...:     master_matrix[np.ix_(indices,indices)]=little_matrix 
    ...:                                                                                       
In [81]: master_matrix                                                                         
Out[81]: 
array([[ 200000.,       0.,       0., -200000.,       0.,       0.,
        -200000.,       0.,       0.,       0.,       0.,       0.,
              0.,       0.,       0.,       0.,       0.,       0.],
       [      0.,    2000.,   10000.,       0.,   -2000.,   10000.,
              0.,    2000.,  -10000.,       0.,       0.,       0.,
              0.,       0.,       0.,       0.,       0.,       0.],
       ...

I suspect we can eliminate that iteration as well, but that'll take some more work.

Looking at the indices as a whole:

In [88]: x = ingreso_datos[:,1:]-1                                                             
In [89]: x                                                                                     
Out[89]: 
array([[12, 13, 14,  6,  7,  8],
       [15, 16, 17,  9, 10, 11],
       [ 6,  7,  8,  0,  1,  2],
       [ 9, 10, 11,  3,  4,  5],
       [ 6,  7,  8,  9, 10, 11],
       [ 0,  1,  2,  3,  4,  5]])

I see some overlap. For example the last row over writes some of the values written by row 2:

In [90]: master_matrix[:6,:6]                                                                  
Out[90]: 
array([[ 200000.,       0.,       0., -200000.,       0.,       0.],
       [      0.,    2000.,   10000.,       0.,   -2000.,   10000.],
       [      0.,   10000.,   50000.,       0.,  -10000.,   50000.],
       [-200000.,       0.,       0.,  200000.,       0.,       0.],
       [      0.,    2000.,  -10000.,       0.,    2000.,  -10000.],
       [      0.,   10000.,   50000.,       0.,  -10000.,   50000.]])
In [91]: master_matrix[np.ix_(x[2],x[2])]                                                      
Out[91]: 
array([[ 200000.,       0.,       0., -200000.,       0.,       0.],
       [      0.,    2000.,   10000.,       0.,   -2000.,   10000.],
       [      0.,   10000.,   50000.,       0.,  -10000.,   50000.],
       [-200000.,       0.,       0.,  200000.,       0.,       0.],
       [      0.,    2000.,  -10000.,       0.,    2000.,   10000.],
       [      0.,   10000.,   50000.,       0.,   10000.,   50000.]])

(notice the sign change in the bottom right corner).

Given that over lap it's impossible to replicate the iterative assignment with one call.



来源:https://stackoverflow.com/questions/60936223/add-multiple-arrays-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!