Skip to content

Commit 61fec83

Browse files
QuantumNovicecclauss
authored andcommitted
Adds Gaussian Function in maths section (TheAlgorithms#1054)
* Create gaussian.py * Update gaussian.py * Update gaussian.py * Create gaussian.png * Add files via upload * Create prime_factors.py * Update prime_factors.py * Update prime_factors.py
1 parent f5e6d4e commit 61fec83

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed

maths/gaussian.py

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
"""
3+
Reference: https://en.wikipedia.org/wiki/Gaussian_function
4+
5+
python/black : True
6+
python : 3.7.3
7+
8+
"""
9+
from numpy import pi, sqrt, exp
10+
11+
12+
13+
def gaussian(x, mu: float = 0.0, sigma: float = 1.0) -> int:
14+
"""
15+
>>> gaussian(1)
16+
0.24197072451914337
17+
18+
>>> gaussian(24)
19+
3.342714441794458e-126
20+
21+
Supports NumPy Arrays
22+
Use numpy.meshgrid with this to generate gaussian blur on images.
23+
>>> import numpy as np
24+
>>> x = np.arange(15)
25+
>>> gaussian(x)
26+
array([3.98942280e-01, 2.41970725e-01, 5.39909665e-02, 4.43184841e-03,
27+
1.33830226e-04, 1.48671951e-06, 6.07588285e-09, 9.13472041e-12,
28+
5.05227108e-15, 1.02797736e-18, 7.69459863e-23, 2.11881925e-27,
29+
2.14638374e-32, 7.99882776e-38, 1.09660656e-43])
30+
31+
>>> gaussian(15)
32+
5.530709549844416e-50
33+
34+
>>> gaussian([1,2, 'string'])
35+
Traceback (most recent call last):
36+
...
37+
TypeError: unsupported operand type(s) for -: 'list' and 'float'
38+
39+
>>> gaussian('hello world')
40+
Traceback (most recent call last):
41+
...
42+
TypeError: unsupported operand type(s) for -: 'str' and 'float'
43+
44+
>>> gaussian(10**234) # doctest: +IGNORE_EXCEPTION_DETAIL
45+
Traceback (most recent call last):
46+
...
47+
OverflowError: (34, 'Result too large')
48+
49+
>>> gaussian(10**-326)
50+
0.3989422804014327
51+
52+
>>> gaussian(2523, mu=234234, sigma=3425)
53+
0.0
54+
"""
55+
return 1 / sqrt(2 * pi * sigma ** 2) * exp(-(x - mu) ** 2 / 2 * sigma ** 2)
56+
57+
58+
if __name__ == "__main__":
59+
import doctest
60+
61+
doctest.testmod()

maths/images/gaussian.png

52.3 KB
Loading

maths/prime_factors.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""
2+
python/black : True
3+
"""
4+
from typing import List
5+
6+
7+
def prime_factors(n: int) -> List[int]:
8+
"""
9+
Returns prime factors of n as a list.
10+
11+
>>> prime_factors(0)
12+
[]
13+
>>> prime_factors(100)
14+
[2, 2, 5, 5]
15+
>>> prime_factors(2560)
16+
[2, 2, 2, 2, 2, 2, 2, 2, 2, 5]
17+
>>> prime_factors(10**-2)
18+
[]
19+
>>> prime_factors(0.02)
20+
[]
21+
>>> x = prime_factors(10**241) # doctest: +NORMALIZE_WHITESPACE
22+
>>> x == [2]*241 + [5]*241
23+
True
24+
>>> prime_factors(10**-354)
25+
[]
26+
>>> prime_factors('hello')
27+
Traceback (most recent call last):
28+
...
29+
TypeError: '<=' not supported between instances of 'int' and 'str'
30+
>>> prime_factors([1,2,'hello'])
31+
Traceback (most recent call last):
32+
...
33+
TypeError: '<=' not supported between instances of 'int' and 'list'
34+
35+
"""
36+
i = 2
37+
factors = []
38+
while i * i <= n:
39+
if n % i:
40+
i += 1
41+
else:
42+
n //= i
43+
factors.append(i)
44+
if n > 1:
45+
factors.append(n)
46+
return factors
47+
48+
49+
if __name__ == "__main__":
50+
import doctest
51+
52+
doctest.testmod()

0 commit comments

Comments
 (0)