6626070
2997924

PL03-Topic03, maths

Back to the previous page
List of posts to read before reading this article


Contents


basic_maths





find

find_max

max(1,2,3)

3

Equivalent code
def find_max(nums):
    max = nums[0]
    for x in nums:
        if x > max:
            max = x
    print(max)





find_min

min(1,2,3)

1

Equivalent code
def find_min(x):
    min_num = x[0]
    for i in x:
        if min_num > i:
            min_num = i
    return min_num





find_lcm

def find_lcm(a,b):
    A = []
    B = []
    R = []
    for k in range(1,max(a,b)+1):
        A.append(k*a)
        B.append(k*b)
    for i in range(len(A)):
        for j in range(len(B)):
            if A[i] == B[j]:
                R.append(B[j])
    return min(R)
OUTPUT
>>> find_lcm(5,2)
10
>>> find_lcm(12,76)
228





find_gcd

def find_gcd(a,b):
    A = []
    B = []
    R = []
    for i in range(1,a+1):
        if a%i==0:
            A.append(i)
    for j in range(1,b+1):
        if b%j==0:
            B.append(j)
    for i in range(len(A)):
        for j in range(len(B)):
            if A[i] == B[j]:
                R.append(B[j])
    return max(R)
OUTPUT
>>> find_gcd(5,2)
1
>>> find_gcd(12,76)
4





abs

abs

def abs_val(num):
    return -num if num < 0 else num
OUTPUT
>>> abs_val(-5)
5
>>> abs_val(0)
0





abs_max

from typing import List

def abs_max(x: List[int]) -> int:
    j = x[0]
    for i in x:
        if abs(i) > abs(j):
            j = i
    return j
OUTPUT
>>> abs_max([0,5,1,11])
11
>>> abs_max([3,-10,-2])
-10


def abs_max_sort(x):
    return sorted(x, key=abs)[-1]
OUTPUT
>>> abs_max_sort([0,5,1,11])
11
>>> abs_max_sort([3,-10,-2])
-10





abs_min

from .abs import abs_val

def absMin(x):
    j = x[0]
    for i in x:
        if abs_val(i) < abs_val(j):
            j = i
    return j
OUTPUT
>>> absMin([0,5,1,11])
0
>>> absMin([3,-10,-2])
-2





aggregation_function

average_mean

def average(nums):
    avg = sum(nums) / len(nums)
    return avg





average_median

def median(nums):
    sorted_list = sorted(nums)
    med = None
    if len(sorted_list) % 2 == 0:
        mid_index_1 = len(sorted_list) // 2
        mid_index_2 = (len(sorted_list) // 2) - 1
        med = (sorted_list[mid_index_1] + sorted_list[mid_index_2]) / float(2)
    else:
        mid_index = (len(sorted_list) - 1) // 2
        med = sorted_list[mid_index]
    return med
OUTPUT
>>> median([0])
0
>>> median([4,1,3,2])
2.5





factorial_recursive

Factorial

def fact(n):
    return 1 if n <= 1 else n*fact(n-1)




Combination

def combi(n,r):
    def fact(n):
        return 1 if n <= 1 else n*fact(n-1)

    return fact(n)/(fact(n-r)*fact(r))





fibonacci





basic function

Gaussian

from numpy import pi, sqrt, exp

def gaussian(x, mu: float = 0.0, sigma: float = 1.0) -> int:
    return 1 / sqrt(2 * pi * sigma ** 2) * exp(-(x - mu) ** 2 / 2 * sigma ** 2)
OUTPUT
>>> gaussian(1)
0.24197072451914337
  
>>> gaussian(24)
3.342714441794458e-126
Supports NumPy Arrays
Use numpy.meshgrid with this to generate gaussian blur on images.
>>> import numpy as np
>>> x = np.arange(15)
>>> gaussian(x)
array([3.98942280e-01, 2.41970725e-01, 5.39909665e-02, 4.43184841e-03,
       1.33830226e-04, 1.48671951e-06, 6.07588285e-09, 9.13472041e-12,
       5.05227108e-15, 1.02797736e-18, 7.69459863e-23, 2.11881925e-27,
       2.14638374e-32, 7.99882776e-38, 1.09660656e-43])
           
>>> gaussian(15)
5.530709549844416e-50

>>> gaussian(10**234) # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
    ...
OverflowError: (34, 'Result too large')

>>> gaussian(10**-326)
0.3989422804014327

>>> gaussian(2523, mu=234234, sigma=3425)
0.0





softmax

def softmax(vector):
    exponentVector = np.exp(vector)
    sumOfExponents = np.sum(exponentVector)
    softmax_vector = exponentVector / sumOfExponents
    return softmax_vector
OUTPUT
>>> np.ceil(np.sum(softmax([1,2,3,4])))
1.0

>>> vec = np.array([5,5])
>>> softmax(vec)
array([0.5, 0.5])

>>> softmax([0])
array([1.])





integral

simpson_rule





trapezoidal_rule





List of posts followed by this article


Reference


OUTPUT