Matematik funksiyalar. NumPy massiv elementlarida standart arifmetik amallarni amalga oshiradi.
3.3-jadval. Massivlar ustidagi arifmetik amallar
Funksiya
|
tavsifi
|
add(x, y)
|
elementlar bo'yicha qo'shish
|
subtract(x, y)
|
elementlar bo‘yicha ayirish
|
multiply(x, y)
|
elementlar bo'yicha ko'paytirish
|
divide(x, y)
|
elementlar bo'yicha bo'lish
|
power(x,y)
|
elementlar boʻyicha darajaga ko`tarish
|
negative(x)
|
massiv elementlarining ishorasini o'zgartiradi
|
Massivlarda elementar arifmetik amallardan foydalanishga misollar:
import numpy as np
a0 = np.linspace(1., 10., 10).reshape(2, 5)
print('a0:\n', a0)
a1 = np.ones(10).reshape(2, 5)
a1 = np.add(a1, 1.)
print('a1:\n', a1)
a2 = np.add(a0, a1)
print('a2:\n', a2)
a3 = np.multiply(a0, a1)
print('а3:\n', a3)
a4=np.power(a0, a1)
print('a4:\n',a4)
Natija:
a0:
[[ 1. 2. 3. 4. 5.]
[ 6. 7. 8. 9. 10.]]
a1:
[[2. 2. 2. 2. 2.]
[2. 2. 2. 2. 2.]]
a2:
[[ 3. 4. 5. 6. 7.]
[ 8. 9. 10. 11. 12.]]
a3:
[[ 2. 4. 6. 8. 10.]
[12. 14. 16. 18. 20.]]
a4:
[[ 1. 4. 9. 16. 25.]
[ 36. 49. 64. 81. 100.]]
Massivlarda element bo`yicha bajariladigan boshqa funksiyalar:
Trigonometrik: sin(x), cos(x), tan(x), arcsin(x), arccos(x), arctan(x), hypot(x, y), arctan2(x,y), degrees(x), radians(x);
Giperbolik: sinh(x), cosh(x), tanh(x), arcsinh(x), arccosh(x), arctanh(x);
Eksponensial va algoritmik: exp(x), expm1(x) (=exp(x)-1), log(x), logl0(x), log2(x), log1p(x)(=log(1+x)).
Boshqa funksiyalar Python standart kutubxonasining math modulidagi matematik funksiyalar yozuviga mos keladi (3.1-jadvalga qarang).
import numpy as np
a0 = np.linspace(0., np.pi, 9)
print('a0:\n', a0)
a1 = np.sin(a0)
print('a1:\n', a1)
a2 = np.cos(a0)
print('a2:\n', a2)
a3 = np.multiply(a1, a1) + np.multiply(a2, a2) - 1.
print('а3:\n', a3)
Natija:
a0:
[0. 0.39269908 0.78539816 1.17809725 1.57079633 1.96349541
2.35619449 2.74889357 3.14159265]
a1:
[0.00000000e+00 3.82683432e-01 7.07106781e-01 9.23879533e-01
1.00000000e+00 9.23879533e-01 7.07106781e-01 3.82683432e-01
1.22464680e-16]
a2:
[ 1.00000000e+00 9.23879533e-01 7.07106781e-01 3.82683432e-01
6.12323400e-17 -3.82683432e-01 -7.07106781e-01 -9.23879533e-01
-1.00000000e+00]
а3:
[0.00000000e+00 0.00000000e+00 2.22044605e-16 0.00000000e+00
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00]
Elementlari kompleks sonlardan iborat bo`lgan massivlar bilan ishlash uchun quyidagi funksiyalar mavjud: angle(x) (phase(x) funksiyasiga oʻxshash), rcal(x), imag(x) va conj(.x), bu kompleks-bog`langan elementli massivni qaytaradi.
import numpy as np
a0 = np.linspace(1., 10., 10).reshape(2, 5)
print('а0:\n', a0)
a1 = np.add(a0, 1.j)
print('a1:\n', a1)
a2 = np.conj(a1)
print('a2:\n', a2)
a3 = np.real(a1)
print('а3:\n', a3)
a4 = np.imag(a1)
print('a4:\n', a4)
Natija:
а0:
[[ 1. 2. 3. 4. 5.]
[ 6. 7. 8. 9. 10.]]
a1:
[[ 1.+1.j 2.+1.j 3.+1.j 4.+1.j 5.+1.j]
[ 6.+1.j 7.+1.j 8.+1.j 9.+1.j 10.+1.j]]
a2:
[[ 1.-1.j 2.-1.j 3.-1.j 4.-1.j 5.-1.j]
[ 6.-1.j 7.-1.j 8.-1.j 9.-1.j 10.-1.j]]
а3:
[[ 1. 2. 3. 4. 5.]
[ 6. 7. 8. 9. 10.]]
a4:
[[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]]
Do'stlaringiz bilan baham: |