<jupyter_start><jupyter_text>HEADING 1: Algebraic, trigonometric, and transcendental functions  Polynomial functionWe enocunter polynomial functions in everyday computations. For example, if we say that the cost (___C___) of an industrial product is proportional to the raw material (___M___) it consumes (plus a fixed cost ___F___), then we can write the total cost as,$$C = k.M+F\text{ where } k\text{ is the constant of proportionality}$$Here This cost function (and the type of the equation) is polynomial because it contains the   HEADING 2: Python libraries for numerical computing and visualization  `NumPy` library In the daily work of a data scientist, reading and manipulating arrays is one of the most important and frequently encountered jobs. These arrays could be a one-dimensional list or multi-dimensional table or matrix, full of numbers. An array could be filled with integers, floating point numbers, Booleans, strings, or even mixed type. However, in the majority of cases, numeric data types are predominant.In this regard, __NumPy arrays__ will be the most important object in Python that you need to know in depth. `NumPy` and `SciPy` are open-source libraries within the Python ecosystem that provide common mathematical and numerical routines in fast (and often pre-compiled) functions. One of the main objects of the `NumPy` module is to handle or create __single- or multi-dimensional arrays__. We will use this advanced data structure more extensively in the Part-III of this book when we discuss linear algebra. For this chapter, however, we will focus on the basic mathematical operations that can be performed using the NumPy library.<jupyter_code>import numpy as np<jupyter_output><empty_output><jupyter_text>Create an array from a Python list<jupyter_code>my_list = [2,4,5]
my_array = np.array(my_list)
print(my_array)
lst1 = [1,2,3] 
array1 = np.array(lst1)
lst2 = [10,11,12] 
array2 = np.array(lst2)
list_sum = lst1+lst2
array_sum = array1 + array2 
print("Sum of two lists:",list_sum)
print("Sum of two numpy arrays:",array_sum)<jupyter_output>Sum of two lists: [1, 2, 3, 10, 11, 12]
Sum of two numpy arrays: [11 13 15]<jupyter_text>Basic mathematical operations using arrays<jupyter_code>print("array1 multiplied by array2: ",array1*array2) 
print("array2 divided by array1: ",array2/array1) 
print("array2 raised to the power of array1: ",array2**array1)<jupyter_output>array1 multiplied by array2:  [10 22 36]
array2 divided by array1:  [10.   5.5  4. ]
array2 raised to the power of array1:  [  10  121 1728]<jupyter_text>More advanced mathematical operations on Numpy arrays<jupyter_code>print ("Sine of 0.5:", np.sin(0.5))
print("Exponential of 2.2:", np.exp(2.2))
print("Natural logarithm of 5:", np.log(5))
print("10-base logarithm of 100:", np.log10(100))
print("Inverse cosine of 0.25:", np.arccos(0.25))
print("Hyperbolic sine of 2.5:", np.sinh(2.5))<jupyter_output>Sine of 0.5: 0.479425538604203
Exponential of 2.2: 9.025013499434122
Natural logarithm of 5: 1.6094379124341003
10-base logarithm of 100: 2.0
Inverse cosine of 0.25: 1.318116071652818
Hyperbolic sine of 2.5: 6.0502044810397875<jupyter_text>Proving a mathematical identity using NumPy operations<jupyter_code>a = [i for i in range(1,21)]
arr = np.array(a)
x = [0.5]*20
x_arr = np.array(x)
log = ((-1)**(arr+1))*x_arr**arr/arr
print("Result using summation of the NumPy array:",np.sum(log))
print("Result using direct NumPy function:",np.log(1+0.5))<jupyter_output>Result using direct NumPy function: 0.4054651081081644<jupyter_text>Visualization using Matplotlib library  Creating a basic plotTo create a simple one-dimensional plot we need some data. Let us first generate the data using a NumPy function as we learned in the previous section.<jupyter_code>import matplotlib.pyplot as plt
x = np.arange(1,50.1,0.1)
print(x)
sinx = np.sin(x)
plt.plot(sinx)<jupyter_output><empty_output><jupyter_text>Advanced features of the plotting function<jupyter_code>plt.figure(figsize=(10,5)) 
plt.title("Plot of sin(x) vs. x\n",fontsize=20) 
plt.xlabel("x values",fontsize=16) 
plt.ylabel("Trigonometric function, sin(x)",fontsize=16) 
plt.grid (True) 
plt.ylim(-1.5,1.5) 
plt.xticks([i*5 for i in range(55)],fontsize=15) 
plt.yticks(fontsize=15) 
plt.scatter(x=x,y=sinx,c='orange',s=50)
plt.text(x=25,y=1.1,s="A vertical blue dashed line \nis drawn at x=21",fontsize=15) 
plt.vlines(x=21,ymin=-1.5,ymax=1.5,linestyles='dashed',color='blue',lw=3)
plt.legend(['Plot of sin(x)'],loc=2,fontsize=14) 
plt.show()<jupyter_output><empty_output><jupyter_text>Plots of few common functions<jupyter_code>x = np.arange(0,10,0.1)
def plotx(x,y, title):
    plt.figure(figsize=(6,4))
    plt.title (title, fontsize=20)
    plt.plot(x,y,lw=3,c='blue')
    plt.grid(True)
    plt.xticks(fontsize=15)
    plt.yticks(fontsize=15)
    plt.xlabel("x-values",fontsize=15)
    plt.ylabel("Function values",fontsize=15)
    plt.show()
y = x
title = "Linear function"
plotx(x,y,title)
y = x**2
title = "Quadratic function"
plotx(x,y,title)
y = np.exp(x)
title = "Exponential function"
plotx(x,y,title)
y = np.log(x)
title = "Natural logarithm function"
plotx(x,y,title)
y = np.cos(x)
title = "Cosine function"
plotx(x,y,title)
y = np.exp(-0.3*x)*np.sin(2*x)
title = "Exponentially decayed sine function"
plotx(x,y,title)<jupyter_output><empty_output><jupyter_text>Quadratic equation solve<jupyter_code>def solve_quad(a=1,b=2,c=1):
    """
    Solves a quadratic equation and returns the roots (real or complex) as a tuple
    """
    from math import sqrt
    z= b**2-4*a*c
    if z >= 0:
        x1 = (-b+sqrt(z))/(2*a)
        x2 = (-b-sqrt(z))/(2*a)
        return (x1,x2)
    else:
        x1 = complex((-b/2*a),sqrt(-z)/(2*a))
        x2 = complex((-b/2*a),-sqrt(-z)/(2*a))
        return (x1,x2)
solve_quad(1,-2,1)
solve_quad(2,-5,4)<jupyter_output><empty_output><jupyter_text>Solving $$2x^2-7x+4=0$$<jupyter_code>x1,x2=solve_quad(2,-7,4)
x1
x2<jupyter_output><empty_output><jupyter_text>Product of the roots<jupyter_code>x1*x2<jupyter_output><empty_output><jupyter_text>Sum of the roots<jupyter_code>x1+x2<jupyter_output><empty_output><jupyter_text>Growth of functions<jupyter_code>logx = []
linx = []
quadx = []
for x in range(1,21):
    logx.append(15*np.log10(x))
    linx.append(2*x-6)
    quadx.append(x**2-100)
logx=np.array(logx)
linx=np.array(linx)
quadx=np.array(quadx)
plt.figure(figsize=(8,6))
plt.plot(logx,c='r')
plt.plot(linx)
plt.plot(quadx)
plt.grid(True)
plt.xticks(fontsize=15)
plt.yticks(fontsize=15)
plt.legend(['Logarithmic function','Linear function','Quadratic function'],fontsize=15)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13157498116051244, 'fraction_numerical': 0.05516201959306707, 'mean_word_length': 5.602985074626866, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '501373', 'license': 'MIT', 'n_tokens_mistral': 2667, 'n_tokens_neox': 2428, 'n_words': 657, 'path': 'Packt-Maths-Data-Scientist/CHAPTER 2 - Functions, equations, plots, and limit.ipynb', 'repo_name': 'seyal84/Stats-Maths-with-Python', 'size': '6635'}