This article which is about ulab EP 3 discusses the ulab vector submodule used for arithmetic as well as the MicroPython math library. The content contains the functions that ulab supports on the ESP32 and ESP8266, along with the descriptions of functions.
vector
Functions in a group of vector submodule act like functions in a math library, differing in that functions in vectors are computations for every member of the 1D and 2D array and have the same result as a processing loop by applying the math library to each member. But the loop will be performed by vector submodule, and its performance is better than the loop by coding because the ulab library is written in C and is at a lower level than loop writing in Python.
The functions supported by vector submodule are:
- result = ulab.vector.acos( x ) Find the angle from the value calculated with cosine, which is the reciprocal of cosine.
- result = ulab.vector.acosh( x ) Find the angle from the values calculated with cosh, which is the reciprocal of cosh.
- result = ulab.vector.arctan2( x ) Find the angle from the calculated value with tan, which is the reciprocal of tan. Angle values are in the range [-pi,pi].
- result = ulab.vector.around( x ) Rounds a decimal number.
- result = ulab.vector.asin( x ) Find the angle from the value calculated with sine, which is the reciprocal of sine.
- result = ulab.vector.asinh( x ) Find the angle from the value calculated with sinh, which is the reciprocal of sinh.
- result = ulab.vector.atan( x ) หาค่ามุมจากค่าที่ได้จากการตำนวณด้วยค่า tan ซึ่งเป็นการหาส่วนกลับของ tan มีค่ามุมอยู่ในช่วง [-pi/2,pi/2]
- result = ulab.vector.atanh( x ) Find the angle from the value calculated with tanh, which is the reciprocal of tanh.
- result = ulab.vector.ceil( x ) Rounds the decimal to the next integer.
- result = ulab.vector.cos( x ) Finding radians with cosine
- result = ulab.vector.erf( x ) Calculate the error value of the statistical value.
- result = ulab.vector.erfc( x ) Compute the complement error of the statistical value.
- result = ulab.vector.exp( x ) Find the value of ex
- result = ulab.vector.expm1( x ) Finds e x-1. In some applications it is more accurate than exp(x).
- result = ulab.vector.floor( x ) Rounds the decimal to the previous integer.
- result = ulab.vector.gamma( x ) Calculate the gamma of x.
- result = ulab.vector.lgamma( x ) Calculate the log value of the gamma of x.
- result = ulab.vector.log( x ) Finds the natural logarithm.
- result = ulab.vector.log10( x ) Finds the common logarithm.
- result = ulab.vector.log2( x ) Finds the binary logarithm.
- result = ulab.vector.sin( x ) Find the sine of a radian angle.
- result = ulab.vector.sinh( x ) Finds the value of hyperbolic sine.
- result = ulab.vector.sqrt( x ) Find the square root.
- result = ulab.vector.tan( x ) Find the tangent of a radian angle.
- result = ulab.vector.tanh( x ) Find the value of hyperbolic tan.
The math.radians() and math.degrees() functions convert angles between radians and degrees.
Example code
ตัวอย่างโปรแกรม code18-7 เป็นการนำเวกเตอร์ pX และ pY จำนวน 4 สมาชิกซึ่งเป็นพิกัดของจุด 4 จุด เพื่อมาคำนวณขยายให้เป็น 2 เท่าในแกน X และ 4 เท่าในแกน Y กับนำมาหมุน 45 องศา และแสดงผลลัพธ์จากการคำนวณออกมาตามตัวอย่างภาพที่ 1 และเมื่อไปประยุกต์กับการแสดงผลด้วยการหมุนกล่องสี่เหลี่ยมตามเข็มนาฬิกา 360 องศาและทวนเข็มนาฬิกาจะเป็นดังคลิปที่1
# code18-7
import time
import ulab as np
import math
def scale(pX,pY,Sx=1.0,Sy=1.0):
return ((pX*Sx, pY*Sy))
def rotate(pX,pY,angle):
rad = math.radians(angle)
xCos = pX*np.vector.cos(rad)
ySin = pY*np.vector.sin(rad)
xSin = pX*np.vector.sin(rad)
yCos = pY*np.vector.cos(rad)
newX = xCos - ySin
newY = xSin + yCos
return (newX, newY)
pX = np.array([-1,1,1,-1],dtype=np.float)
pY = np.array([1,1,-1,-1],dtype=np.float)
Sx = 2.0
Sy = 4.0
newP = scale(pX,pY,Sx,Sy)
newP2 = rotate(pX, pY, 45.0)
print("Vector X={}".format(pX))
print("Vector Y={}".format(pY))
print("Vector X*Sx={}".format(newP[0]))
print("Vector Y*Sy={}".format(newP[1]))
print("Vector X rot 45 degrees={}".format(newP2[0]))
print("Vector Y rot 45 degrees={}".format(newP2[1]))
Conclusion
As you can see from this article, the vector submodule’s instruction set is functionally the same as in the math library, only that it is designed to compute with 1 and 2D arrays generated from ulab to provide better speed than direct loop commands from Python. The next article will be a linalg sub-module which is a sub-module on linear algebra.
Finally, have fun with programming.
(C) 2020, By Jarut Busadathid and Danai Jedsadathitikul
Updated 2021-08-14