# Iam very sorry because I had to copy the problems given below directly from the textbook, because it is a redundant process.
# Moreover the concept of numpy library is a never ending process, because there are a ton of different methods and functions.
# I think this is more than enough to learn about numpy.
import numpy as np
x = np.arange(4)
print("x =", x)
print("x + 5 =", x + 5)
print("x - 5 =", x - 5)
print("x * 2 =", x * 2)
print("x / 2 =", x / 2)
print("x // 2 =", x // 2) # floor division
print("-x = ", -x)
print("x ** 2 = ", x ** 2)
print("x % 2 = ", x % 2)
print(np.add(x, 2))
x = [0 1 2 3] x + 5 = [5 6 7 8] x - 5 = [-5 -4 -3 -2] x * 2 = [0 2 4 6] x / 2 = [0. 0.5 1. 1.5] x // 2 = [0 0 1 1] -x = [ 0 -1 -2 -3] x ** 2 = [0 1 4 9] x % 2 = [0 1 0 1] [2 3 4 5]
# Absolute value
x = np.array([-2, -1, 0, 1, 2])
print(abs(x))
print("---------------------------")
x = np.array([3 - 4j, 4 - 3j, 2 + 0j, 0 + 1j])
print(np.abs(x))
[2 1 0 1 2] --------------------------- [5. 5. 2. 1.]
# Trigonometric functions
theta = np.linspace(0, np.pi, 3)
print("---------------------------")
print("theta = ", theta)
print("sin(theta) = ", np.sin(theta))
print("cos(theta) = ", np.cos(theta))
print("tan(theta) = ", np.tan(theta))
print("---------------------------")
x = [-1, 0, 1]
print("x = ", x)
print("arcsin(x) = ", np.arcsin(x))
print("arccos(x) = ", np.arccos(x))
print("arctan(x) = ", np.arctan(x))
--------------------------- theta = [0. 1.57079633 3.14159265] sin(theta) = [0.0000000e+00 1.0000000e+00 1.2246468e-16] cos(theta) = [ 1.000000e+00 6.123234e-17 -1.000000e+00] tan(theta) = [ 0.00000000e+00 1.63312394e+16 -1.22464680e-16] --------------------------- x = [-1, 0, 1] arcsin(x) = [-1.57079633 0. 1.57079633] arccos(x) = [3.14159265 1.57079633 0. ] arctan(x) = [-0.78539816 0. 0.78539816]
# Exponents and logarithms
x = [1, 2, 3]
print("x =", x)
print("e^x =", np.exp(x))
print("2^x =", np.exp2(x))
print("3^x =", np.power(3, x))
print("---------------------------")
x = [1, 2, 4, 10]
print("x =", x)
print("ln(x) =", np.log(x))
print("log2(x) =", np.log2(x))
print("log10(x) =", np.log10(x))
print("---------------------------")
x = [0, 0.001, 0.01, 0.1]
print("exp(x) - 1 =", np.expm1(x))
print("log(1 + x) =", np.log1p(x))
x = [1, 2, 3] e^x = [ 2.71828183 7.3890561 20.08553692] 2^x = [2. 4. 8.] 3^x = [ 3 9 27] --------------------------- x = [1, 2, 4, 10] ln(x) = [0. 0.69314718 1.38629436 2.30258509] log2(x) = [0. 1. 2. 3.32192809] log10(x) = [0. 0.30103 0.60205999 1. ] --------------------------- exp(x) - 1 = [0. 0.0010005 0.01005017 0.10517092] log(1 + x) = [0. 0.0009995 0.00995033 0.09531018]
# Another excellent source for more specialized and obscure ufuncs is the submodule
# scipy.special. If you want to compute some obscure mathematical function on
# your data, chances are it is implemented in scipy.special.
from scipy import special
x = [1, 5, 10]
print("gamma(x) =", special.gamma(x))
print("ln|gamma(x)| =", special.gammaln(x))
print("beta(x, 2) =", special.beta(x, 2))
print("---------------------------")
# Error function (integral of Gaussian)
# its complement, and its inverse
x = np.array([0, 0.3, 0.7, 1.0])
print("erf(x) =", special.erf(x))
print("erfc(x) =", special.erfc(x))
print("erfinv(x) =", special.erfinv(x))
gamma(x) = [1.0000e+00 2.4000e+01 3.6288e+05] ln|gamma(x)| = [ 0. 3.17805383 12.80182748] beta(x, 2) = [0.5 0.03333333 0.00909091] --------------------------- erf(x) = [0. 0.32862676 0.67780119 0.84270079] erfc(x) = [1. 0.67137324 0.32219881 0.15729921] erfinv(x) = [0. 0.27246271 0.73286908 inf]
# Advanced Ufunc Features
# Specifying output
x = np.arange(5)
y = np.empty(5)
np.multiply(x, 10, out=y)
print(y)
print("---------------------------")
y = np.zeros(10)
np.power(2, x, out=y[::2])
print(y)
[ 0. 10. 20. 30. 40.] --------------------------- [ 1. 0. 2. 0. 4. 0. 8. 0. 16. 0.]
# Aggregates
x = np.arange(1, 6)
print(np.add.reduce(x))
print("---------------------------")
print(np.multiply.reduce(x))
print("---------------------------")
print(np.add.accumulate(x))
print("---------------------------")
print(np.multiply.accumulate(x))
15 --------------------------- 120 --------------------------- [ 1 3 6 10 15] --------------------------- [ 1 2 6 24 120]