2

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.

rhanson
  • 31
  • 2
  • A numpy matrix might be more natural than a pandas dataframe. See [this question](https://stackoverflow.com/q/6254713/4996248) for how to construct a matrix where the elements are functions of its index. You could construct the first row and first column separately, and then glue it all together (or, wrap the logic into a single function which conditionally decided what to return) – John Coleman Apr 27 '20 at 11:03
  • Thanks, while testing if I'd put the DataFrame together properly I set the first row and first column so I know that works well. I'll look at the numpy matrix and try that. – rhanson Apr 27 '20 at 11:10

0 Answers0