-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Adds Gaussian Function in maths section #1054
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f5498e3
Create gaussian.py
QuantumNovice 024d5fd
Update gaussian.py
QuantumNovice 3993f96
Update gaussian.py
QuantumNovice 6a4748f
Create gaussian.png
QuantumNovice f3b4401
Add files via upload
QuantumNovice dd6b25a
Create prime_factors.py
QuantumNovice ae99fff
Update prime_factors.py
QuantumNovice 0382832
Update prime_factors.py
QuantumNovice File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
|
||
""" | ||
Reference: https://en.wikipedia.org/wiki/Gaussian_function | ||
|
||
python/black : True | ||
python : 3.7.3 | ||
|
||
""" | ||
from numpy import pi, sqrt, exp | ||
|
||
|
||
|
||
def gaussian(x, mu: float = 0.0, sigma: float = 1.0) -> int: | ||
""" | ||
>>> 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([1,2, 'string']) | ||
Traceback (most recent call last): | ||
... | ||
TypeError: unsupported operand type(s) for -: 'list' and 'float' | ||
|
||
>>> gaussian('hello world') | ||
Traceback (most recent call last): | ||
... | ||
TypeError: unsupported operand type(s) for -: 'str' and 'float' | ||
|
||
>>> 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 | ||
""" | ||
return 1 / sqrt(2 * pi * sigma ** 2) * exp(-(x - mu) ** 2 / 2 * sigma ** 2) | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
""" | ||
python/black : True | ||
""" | ||
|
||
|
||
def prime_factors(n: int) -> list: | ||
""" | ||
Returns prime factors of n as a list. | ||
|
||
>>> prime_factors(0) | ||
[] | ||
>>> prime_factors(100) | ||
[2, 2, 5, 5] | ||
>>> prime_factors(2560) | ||
[2, 2, 2, 2, 2, 2, 2, 2, 2, 5] | ||
>>> prime_factors(10**-2) | ||
[] | ||
>>> prime_factors(0.02) | ||
[] | ||
>>> prime_factors(10**241) | ||
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 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, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5] | ||
>>> prime_factors(10**-354) | ||
[] | ||
>>> prime_factors('hello') | ||
Traceback (most recent call last): | ||
... | ||
TypeError: '<=' not supported between instances of 'int' and 'str' | ||
>>> prime_factors([1,2,'hello']) | ||
Traceback (most recent call last): | ||
... | ||
TypeError: '<=' not supported between instances of 'int' and 'list' | ||
|
||
""" | ||
i = 2 | ||
factors = [] | ||
while i * i <= n: | ||
if n % i: | ||
i += 1 | ||
else: | ||
n //= i | ||
factors.append(i) | ||
if n > 1: | ||
factors.append(n) | ||
return factors | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My last request on this PR... I promise.
https://docs.python.org/3/library/doctest.html#doctest.NORMALIZE_WHITESPACE
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Thank you for your time. This is educational for me.