[EN] ulab EP3 vector

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:

  1. result = ulab.vector.acos( x ) Find the angle from the value calculated with cosine, which is the reciprocal of cosine.
  2. result = ulab.vector.acosh( x ) Find the angle from the values calculated with cosh, which is the reciprocal of cosh.
  3. 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].
  4. result = ulab.vector.around( x ) Rounds a decimal number.
  5. result = ulab.vector.asin( x ) Find the angle from the value calculated with sine, which is the reciprocal of sine.
  6. result = ulab.vector.asinh( x ) Find the angle from the value calculated with sinh, which is the reciprocal of sinh.
  7. result = ulab.vector.atan( x ) หาค่ามุมจากค่าที่ได้จากการตำนวณด้วยค่า tan ซึ่งเป็นการหาส่วนกลับของ tan มีค่ามุมอยู่ในช่วง [-pi/2,pi/2]
  8. result = ulab.vector.atanh( x ) Find the angle from the value calculated with tanh, which is the reciprocal of tanh.
  9. result = ulab.vector.ceil( x ) Rounds the decimal to the next integer.
  10. result = ulab.vector.cos( x ) Finding radians with cosine
  11. result = ulab.vector.erf( x ) Calculate the error value of the statistical value.
  12. result = ulab.vector.erfc( x ) Compute the complement error of the statistical value.
  13. result = ulab.vector.exp( x ) Find the value of ex
  14. result = ulab.vector.expm1( x ) Finds e x-1. In some applications it is more accurate than exp(x).
  15. result = ulab.vector.floor( x ) Rounds the decimal to the previous integer.
  16. result = ulab.vector.gamma( x ) Calculate the gamma of x.
  17. result = ulab.vector.lgamma( x ) Calculate the log value of the gamma of x.
  18. result = ulab.vector.log( x ) Finds the natural logarithm.
  19. result = ulab.vector.log10( x ) Finds the common logarithm.
  20. result = ulab.vector.log2( x ) Finds the binary logarithm.
  21. result = ulab.vector.sin( x ) Find the sine of a radian angle.
  22. result = ulab.vector.sinh( x ) Finds the value of hyperbolic sine.
  23. result = ulab.vector.sqrt( x ) Find the square root.
  24. result = ulab.vector.tan( x ) Find the tangent of a radian angle.
  25. 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]))

(Figure. 1 result of code18-7)
(Video 1 An example of implementing the code18-7.)

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