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:
using PyCall
using Plots
Importing the NumPy computational library:
py"""
import numpy as np # Фундаментальный пакет для научных вычислений
"""
Graphing¶
Plotting graphs by points:
plot(py"""[1, 2]""",py"""[3, 4]""")# Отрисовка возрастающего графика
plot!(py"""[1, 2]""",py"""[4, 3]""") # Отрисовка убывающего графика
Constructing multiple graphs¶
Declaring a function for graphing.
py"""
def f(t):
return np.exp(-t) * np.cos(2*np.pi*t) # Математическая формула
"""
Declaring arrays.
py"""
t1 = np.arange(0.0, 5.0, 0.1)
t2 = np.arange(0.0, 5.0, 0.02)
"""
Building graphs.
plot(py"""t2""", py"""np.cos(2*np.pi*t2)""")
plot(py"""t1""", py"""f(t1)""")
plot!(py"""t2""", py"""f(t2)""")
It is also possible to import Python libraries directly into Engee. Let's consider such an example by connecting Math and Numpy libraries.
math = pyimport("math")
To demonstrate the performance of this approach, let's use the sin functions for constants and vectors.
math.sin(math.radians(90))
a = math.pi/4;
math.sin(a)
As we can see from the example above, it is possible to set values in Engee and then use them using Python functions.
a = [1,2,3]
numpy = pyimport("numpy")
a = numpy.sin(a)
using Plots
plot(a)
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.