Engee documentation
Notebook

How to use Python in Engee scripts

In this example, we will look at the principles of interacting with Python from the Engee environment and do some exercises with graphs.

Getting started

Language selection Python:

In [ ]:
using PyCall
using Plots

Importing a computing library NumPy:

In [ ]:
py"""
import numpy as np # Фундаментальный пакет для научных вычислений
"""

Plotting graphs

Plotting graphs by points:

In [ ]:
plot(py"""[1, 2]""",py"""[3, 4]""")# Отрисовка возрастающего графика
plot!(py"""[1, 2]""",py"""[4, 3]""") # Отрисовка убывающего графика
Out[0]:

Building multiple graphs

Declaring a function for plotting graphs.

In [ ]:
py"""
def f(t):
    return np.exp(-t) * np.cos(2*np.pi*t) # Математическая формула
"""    

Declaring arrays.

In [ ]:
py"""
t1 = np.arange(0.0, 5.0, 0.1)
t2 = np.arange(0.0, 5.0, 0.02)
"""

Plotting graphs.

In [ ]:
plot(py"""t2""", py"""np.cos(2*np.pi*t2)""") 
Out[0]:
In [ ]:
plot(py"""t1""", py"""f(t1)""")
plot!(py"""t2""", py"""f(t2)""") 
Out[0]:

It is also possible to import Python libraries directly into Engee. Let's consider such an example by connecting the Math and Numpy libraries.

In [ ]:
math = pyimport("math")
Out[0]:
PyObject <module 'math' from '/usr/local/julia-1.9.3/conda/3/x86_64/lib/python3.10/lib-dynload/math.cpython-310-x86_64-linux-gnu.so'>

To demonstrate the efficiency of this approach, we will use the sin functions for constants and vectors.

In [ ]:
math.sin(math.radians(90))
Out[0]:
1.0
In [ ]:
a = math.pi/4;
math.sin(a)
Out[0]:
0.7071067811865475

As we can see from the example above, you can set values in Engee and then use them using the Python functions.

In [ ]:
a = [1,2,3]
numpy = pyimport("numpy")
a = numpy.sin(a)
Out[0]:
3-element Vector{Float64}:
 0.8414709848078965
 0.9092974268256817
 0.1411200080598672
In [ ]:
using Plots
plot(a)
Out[0]:

From this example, it can be seen that we can also work with vector values and process them not only using the Engee, julia functionality, but also using Python.

Conclusion

In this example, we have demonstrated the capabilities of Engee in writing and implementing algorithms in Python. In the modern world, this option is in high demand, as Python is very popular, and a huge amount of functionality is implemented in this language.