Arsalan Ahmad Ishaq
Arsalan Ahmad Ishaq

Reputation: 977

Creating mathematical equations using numpy in python

I want to create equations using numpy array multiplication ie I want to keep all variables in an array and its coefficients in other array and multiply both with each other to produce an expression so that I can use m.Equation() method of GEKKO. I tried the mentioned code but failed, please let me know how I can achieve my goal.

By "it failed" I meant that it just gave an error and did not let me use x*y==1 as equation in m.Equation() method available in GEKKO. My target is that I want to keep variables in one array and their coefficients in the other array and I multiply them to get mathematical equations to be used as input in m.Equation() method.

import numpy as np
from gekko import GEKKO


X = np.array([x,y,z])
y = np.array([4,5,6])
m = GEKKO(remote=False)
m.Equation(x*y==1)
# I wanted to get a result like 4x+5y+6z=1

The error I get is below

    Traceback (most recent call last):
  File "C:\Users\kk\AppData\Local\Programs\Python\Python37\MY WORK FILES\numpy practise.py", line 5, in <module>
    X = np.array([x,y,z])
NameError: name 'x' is not defined

Upvotes: 3

Views: 3076

Answers (1)

javidcf
javidcf

Reputation: 59731

You need to define variables and make the coefficients into a Gekko object. You can use an array to make the variables and a parameter for the coefficients:

from gekko import GEKKO

m = GEKKO(remote=False)
X = m.Array(m.Var, 3)
y = m.Param([4, 5, 6])
eq = m.Equation(X.dot(y) == 1)
print(eq.value)

Output:

((((v1)*(4))+((v2)*(5)))+((v3)*(6)))=1

Upvotes: 4

Related Questions