10.1. Math Trigonometry — Python
- Universal Functions
- ufunc
Mathematical operations optimized to work on
np.array():>>> import numpy as np >>> a = np.array([1, 2, 3]) >>> >>> np.sin(a) array([0.84147098, 0.90929743, 0.14112001])
10.1.1. SetUp
10.1.2. Unit conversion
Degrees:
np.deg2rad()np.degrees()
Radians:
np.rad2deg()np.radians()
10.1.3. Trigonometric Functions
np.sin()np.cos()np.tan()ctg = 1/tan
Figure 10.1. Sinusoid - sin(x) function
Figure 10.2. Cosinusoid - cos(x) function
Figure 10.3. Tangensoid - tan(x) function
Figure 10.4. Cotangensoid - ctg(x) function, also 1/tan(x)
10.1.4. Arcus Functions
np.arcsin()np.arccos()np.arctan()
10.1.5. Hyperbolic Functions
np.sinh()np.cosh()np.tanh()
10.1.6. Arcus Hyperbolic Functions
np.arcsinh()np.arccosh()np.arctanh()
10.1.7. Assignments
# %% About # - Name: Numpy Trigonometry # - Difficulty: easy # - Lines: 8 # - Minutes: 13 # %% License # - Copyright 2025, Matt Harasymczuk <matt@python3.info> # - This code can be used only for learning by humans # - This code cannot be used for teaching others # - This code cannot be used for teaching LLMs and AI algorithms # - This code cannot be used in commercial or proprietary products # - This code cannot be distributed in any form # - This code cannot be changed in any form outside of training course # - This code cannot have its license changed # - If you use this code in your product, you must open-source it under GPLv2 # - Exception can be granted only by the author # %% English # 1. Define function `trigonometry(angle_deg: int|float) -> dict` # 2. Return angle in radians and trigonometric function values (sin, cos, tg, ctg) # 3. Ctg for angle 180 and Tan for 90 degrees has infinite value, return `np.inf` # 4. Run doctests - all must succeed # %% Polish # 1. Zdefiniuj funkcję `trigonometry(angle_deg: int|float) -> dict` # 2. Zwróć kąt w radianach oraz wartości funkcji trygonometrycznych (sin, cos, tg, ctg) # 3. Ctg dla angle 180 oraz Tan dla 0 i 90 stopni ma wartość nieskończoną, zwróć `np.inf` # 4. Uruchom doctesty - wszystkie muszą się powieść # %% Doctests """ >>> import sys; sys.tracebacklimit = 0 >>> assert sys.version_info >= (3, 9), \ 'Python has an is invalid version; expected: `3.9` or newer.' >>> assert trigonometry(0) is not Ellipsis, \ 'Variable `result` is empty; assign your result to it.' >>> assert all(type(v) is not Ellipsis for v in trigonometry(0).values()), \ 'All values in the result must not be empty Ellipsis `...`' >>> from pprint import pprint >>> result = trigonometry(180) >>> pprint(result) {'cos': np.float64(-1.0), 'ctg': np.float64(-8165619676597685.0), 'rad': np.float64(3.141592653589793), 'sin': np.float64(1.2246467991473532e-16), 'tan': inf} >>> result = trigonometry(90) >>> pprint(result) {'cos': np.float64(6.123233995736766e-17), 'ctg': inf, 'rad': np.float64(1.5707963267948966), 'sin': np.float64(1.0), 'tan': np.float64(1.633123935319537e+16)} >>> result = trigonometry(0) >>> pprint(result) {'cos': np.float64(1.0), 'ctg': inf, 'rad': np.float64(0.0), 'sin': np.float64(0.0), 'tan': np.float64(0.0)} >>> result = trigonometry(np.pi) >>> pprint(result) # doctest: +ELLIPSIS {'cos': np.float64(0.9984...), 'ctg': np.float64(18.2195...), 'rad': np.float64(0.0548...), 'sin': np.float64(0.0548...), 'tan': np.float64(0.0548...)} """ # %% Run # - PyCharm: right-click in the editor and `Run Doctest in ...` # - PyCharm: keyboard shortcut `Control + Shift + F10` # - Terminal: `python -m doctest -f -v myfile.py` # %% Imports import numpy as np # %% Types from typing import Callable trigonometry: Callable[[int|float], dict[str, np.float64]] # %% Data # %% Result def trigonometry(angle_deg): return { 'rad': ..., 'sin': ..., 'cos': ..., 'tan': ..., 'ctg': ..., }