lundi 27 avril 2020

Python 2D array with different equation for each element

I'm relatively new to Python and designing scripts/simulations solo. I'm working on a simulation that involves a complex of parameters. There are four sets that exist in 200x50 matrices and other than the first row and first column, every other element in each matrix uses a different (allbeit related) equation to calculate its value. The general equation is: lambda_*((dfA[i-1,j-1]-(2*dfA[i-1,j])+dfA[i-1,j+1])+[dfA[i-1,j]]), where dfA is shorthand for a pandas DataFrame using .iloc, e.g. dfA = df_c_ox_A.iloc df_c_ox_A = pd.DataFrame(data). Naming convention seems weird but there are a lot of parameters.

As there are 10000x4 equations to write is there a faster way I can do this using loops. I know this code is wrong but this will hopefully show what I'm looking for:

for dfA[i,j] in range(200):
    if i == 0:
        dfA[i,j] = c_ox_b
    elif j == 0:
        dfA[i,j] = c_ox_0[i]
    for i <= 1 & j <= 1:
        dfA[i,j] = lambda_*((dfA[i-1,j-1]-(2*dfA[i-1,j])+dfA[i-1,j+1])+[dfA[i-1,j]])

For extra context, c_ox_b is a single value variable and c_ox_0 is an array of values (200). It's chemistry based so I won't bore with the details.

Aucun commentaire:

Enregistrer un commentaire