Natasha
Natasha

Reputation: 1521

Evaluating math expressions in a string in python

I've a bunch of equations that are loaded from a JSON file and I'd like to evaluate these expressions. These expressions have some constants and values of the constants (K1, K2, ...) are also saved in the json file. The values corresponding to the variables (var1, var2 ...) present in the expressions are defined in a dictionary vars. I want to plugin the values of the constants and the variables in the expressions and compute the numerical values.

import json
vars = {"var1": 1, "var2": 2, "var3": 3, "var4": 4, "var5": 5, "var6": 5}
with open("tst.json", 'r') as f:
     r = json.load(f)
     print(r)

json content:

{
  "EQ1": {
    "parameters": {
      "EQ1_coeff1": {
        "value": 1.0e-06
      },
      "P2": {
        "value": 3.0
      },
      "P3": {
        "value": 3.0
      }
    },
    "expression": "(EQ1_coeff1 * ((1 dimensionless)/(1 dimensionless)) / (1 dimensionless))*(var1^P3/(var1^P3 + P2^P3) )"
  },
  "EQ2": {
    "parameters": {
      "EQ2_coeff2": {
        "value": 5253
      },
      "K2": {
        "value": 3
      },
      "K5": {
        "value": 1
      },
      "K3": {
        "value": 525
      },
      "K4": {
        "value": 3
      },
      "K6": {
        "value": 2
      },
      "K7": {
        "value": 0.01
      }
    },
    "expression": "(EQ2_coeff2 *((var2*var3)/(K1*K2))* ((1 dimensionless - ((var3*var4)/(var2))/K6) / ((1 dimensionless + var1/K1)*(1 dimensionless + var2)+ 1 dimensionless)))*(1 dimensionless/(1 dimensionless + var5/K7))"
  }
}

I'd like to ask for suggestions on how these expressions (stored in expression) can be computed.

Side note:

1 dimensionless is nothing but 1.

Upvotes: -1

Views: 136

Answers (1)

quest
quest

Reputation: 3936

I will simply update the globals() with the dict and then use eval, as done below:

import json
vars = {"var1": 1, "var2": 2, "var3": 3, "var4": 4, "var5": 5, "var6": 5}
r = {
  "EQ1": {
    "parameters": {
      "EQ1_coeff1": {
        "value": 1.0e-06
      },
      "P2": {
        "value": 3.0
      },
      "P3": {
        "value": 3.0
      }
    },
    "expression": "(EQ1_coeff1 * ((1 dimensionless)/(1 dimensionless)) / (1 dimensionless))*(var1^P3/(var1^P3 + P2^P3) )"
  },
  "EQ2": {
    "parameters": {
      "EQ2_coeff2": {
        "value": 5253
      },
      "K2": {
        "value": 3
      },
      "K5": {
        "value": 1
      },
      "K3": {
        "value": 525
      },
      "K4": {
        "value": 3
      },
      "K6": {
        "value": 2
      },
      "K7": {
        "value": 0.01
      }
    },
    "expression": "(EQ2_coeff2 *((var2*var3)/(K1*K2))* ((1 dimensionless - ((var3*var4)/(var2))/K6) / ((1 dimensionless + var1/K1)*(1 dimensionless + var2)+ 1 dimensionless)))*(1 dimensionless/(1 dimensionless + var5/K7))"
  }
}

globals().update( **{k:v['value'] for k,v in r["EQ1"]["parameters"].items()})
globals().update( **{k:v['value'] for k,v in r["EQ2"]["parameters"].items()})
globals().update(**vars)

eval(r['EQ1']['expression'].replace("dimensionless", "").replace("^", "**"))

This gives: 3.571428571428571e-08

For EQ2 you do not have variable K1 defined. This is more of a hack, I am not aware of a more elegant way though. Also keep in mind eval is a not very safe function.

Upvotes: 1

Related Questions