#!/usr/bin/env python # coding: utf-8 # In[16]: # 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)) # In[18]: # 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)) # In[22]: # 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)) # In[25]: # 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)) # In[29]: # 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)) # In[31]: # 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) # In[35]: # 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))