Engee documentation
Notebook

How to use Python in Engee scripts

In this example, we'll look at the principles of interacting with Python from within the Engee environment and do some graphing exercises.

Getting started

Select the Python language:

In [ ]:
using PyCall
using Plots

Importing the NumPy computational library:

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

Graphing

Plotting graphs by points:

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

Constructing multiple graphs

Declaring a function for graphing.

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)
"""

Building 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 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 performance of this approach, let's 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, it is possible to set values in Engee and then use them using 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]:

This example shows that we can also work with vector values and process them not only with Engee, julia, but also with Python.

Conclusion

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