mirror of
https://github.com/TheAlgorithms/Python.git
synced 2026-03-13 09:50:19 +08:00
Upgrade to Python 3.13 (#11588)
This commit is contained in:
@@ -13,13 +13,13 @@ def euclidean_distance(vector_1: Vector, vector_2: Vector) -> VectorOut:
|
||||
"""
|
||||
Calculate the distance between the two endpoints of two vectors.
|
||||
A vector is defined as a list, tuple, or numpy 1D array.
|
||||
>>> euclidean_distance((0, 0), (2, 2))
|
||||
>>> float(euclidean_distance((0, 0), (2, 2)))
|
||||
2.8284271247461903
|
||||
>>> euclidean_distance(np.array([0, 0, 0]), np.array([2, 2, 2]))
|
||||
>>> float(euclidean_distance(np.array([0, 0, 0]), np.array([2, 2, 2])))
|
||||
3.4641016151377544
|
||||
>>> euclidean_distance(np.array([1, 2, 3, 4]), np.array([5, 6, 7, 8]))
|
||||
>>> float(euclidean_distance(np.array([1, 2, 3, 4]), np.array([5, 6, 7, 8])))
|
||||
8.0
|
||||
>>> euclidean_distance([1, 2, 3, 4], [5, 6, 7, 8])
|
||||
>>> float(euclidean_distance([1, 2, 3, 4], [5, 6, 7, 8]))
|
||||
8.0
|
||||
"""
|
||||
return np.sqrt(np.sum((np.asarray(vector_1) - np.asarray(vector_2)) ** 2))
|
||||
|
||||
@@ -26,7 +26,7 @@ def explicit_euler(
|
||||
... return y
|
||||
>>> y0 = 1
|
||||
>>> y = explicit_euler(f, y0, 0.0, 0.01, 5)
|
||||
>>> y[-1]
|
||||
>>> float(y[-1])
|
||||
144.77277243257308
|
||||
"""
|
||||
n = int(np.ceil((x_end - x0) / step_size))
|
||||
|
||||
@@ -24,13 +24,13 @@ def euler_modified(
|
||||
>>> def f1(x, y):
|
||||
... return -2*x*(y**2)
|
||||
>>> y = euler_modified(f1, 1.0, 0.0, 0.2, 1.0)
|
||||
>>> y[-1]
|
||||
>>> float(y[-1])
|
||||
0.503338255442106
|
||||
>>> import math
|
||||
>>> def f2(x, y):
|
||||
... return -2*y + (x**3)*math.exp(-2*x)
|
||||
>>> y = euler_modified(f2, 1.0, 0.0, 0.1, 0.3)
|
||||
>>> y[-1]
|
||||
>>> float(y[-1])
|
||||
0.5525976431951775
|
||||
"""
|
||||
n = int(np.ceil((x_end - x0) / step_size))
|
||||
|
||||
@@ -5,18 +5,18 @@ Reference: https://en.wikipedia.org/wiki/Gaussian_function
|
||||
from numpy import exp, pi, sqrt
|
||||
|
||||
|
||||
def gaussian(x, mu: float = 0.0, sigma: float = 1.0) -> int:
|
||||
def gaussian(x, mu: float = 0.0, sigma: float = 1.0) -> float:
|
||||
"""
|
||||
>>> gaussian(1)
|
||||
>>> float(gaussian(1))
|
||||
0.24197072451914337
|
||||
|
||||
>>> gaussian(24)
|
||||
>>> float(gaussian(24))
|
||||
3.342714441794458e-126
|
||||
|
||||
>>> gaussian(1, 4, 2)
|
||||
>>> float(gaussian(1, 4, 2))
|
||||
0.06475879783294587
|
||||
|
||||
>>> gaussian(1, 5, 3)
|
||||
>>> float(gaussian(1, 5, 3))
|
||||
0.05467002489199788
|
||||
|
||||
Supports NumPy Arrays
|
||||
@@ -29,7 +29,7 @@ def gaussian(x, mu: float = 0.0, sigma: float = 1.0) -> int:
|
||||
5.05227108e-15, 1.02797736e-18, 7.69459863e-23, 2.11881925e-27,
|
||||
2.14638374e-32, 7.99882776e-38, 1.09660656e-43])
|
||||
|
||||
>>> gaussian(15)
|
||||
>>> float(gaussian(15))
|
||||
5.530709549844416e-50
|
||||
|
||||
>>> gaussian([1,2, 'string'])
|
||||
@@ -47,10 +47,10 @@ def gaussian(x, mu: float = 0.0, sigma: float = 1.0) -> int:
|
||||
...
|
||||
OverflowError: (34, 'Result too large')
|
||||
|
||||
>>> gaussian(10**-326)
|
||||
>>> float(gaussian(10**-326))
|
||||
0.3989422804014327
|
||||
|
||||
>>> gaussian(2523, mu=234234, sigma=3425)
|
||||
>>> float(gaussian(2523, mu=234234, sigma=3425))
|
||||
0.0
|
||||
"""
|
||||
return 1 / sqrt(2 * pi * sigma**2) * exp(-((x - mu) ** 2) / (2 * sigma**2))
|
||||
|
||||
@@ -19,7 +19,7 @@ def minkowski_distance(
|
||||
>>> minkowski_distance([1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0], 2)
|
||||
8.0
|
||||
>>> import numpy as np
|
||||
>>> np.isclose(5.0, minkowski_distance([5.0], [0.0], 3))
|
||||
>>> bool(np.isclose(5.0, minkowski_distance([5.0], [0.0], 3)))
|
||||
True
|
||||
>>> minkowski_distance([1.0], [2.0], -1)
|
||||
Traceback (most recent call last):
|
||||
|
||||
@@ -102,7 +102,7 @@ class AdamsBashforth:
|
||||
>>> def f(x, y):
|
||||
... return x + y
|
||||
>>> y = AdamsBashforth(f, [0, 0.2, 0.4], [0, 0, 0.04], 0.2, 1).step_3()
|
||||
>>> y[3]
|
||||
>>> float(y[3])
|
||||
0.15533333333333332
|
||||
|
||||
>>> AdamsBashforth(f, [0, 0.2], [0, 0], 0.2, 1).step_3()
|
||||
@@ -140,9 +140,9 @@ class AdamsBashforth:
|
||||
... return x + y
|
||||
>>> y = AdamsBashforth(
|
||||
... f, [0, 0.2, 0.4, 0.6], [0, 0, 0.04, 0.128], 0.2, 1).step_4()
|
||||
>>> y[4]
|
||||
>>> float(y[4])
|
||||
0.30699999999999994
|
||||
>>> y[5]
|
||||
>>> float(y[5])
|
||||
0.5771083333333333
|
||||
|
||||
>>> AdamsBashforth(f, [0, 0.2, 0.4], [0, 0, 0.04], 0.2, 1).step_4()
|
||||
@@ -185,7 +185,7 @@ class AdamsBashforth:
|
||||
>>> y = AdamsBashforth(
|
||||
... f, [0, 0.2, 0.4, 0.6, 0.8], [0, 0.02140, 0.02140, 0.22211, 0.42536],
|
||||
... 0.2, 1).step_5()
|
||||
>>> y[-1]
|
||||
>>> float(y[-1])
|
||||
0.05436839444444452
|
||||
|
||||
>>> AdamsBashforth(f, [0, 0.2, 0.4], [0, 0, 0.04], 0.2, 1).step_5()
|
||||
|
||||
@@ -19,7 +19,7 @@ def runge_kutta(f, y0, x0, h, x_end):
|
||||
... return y
|
||||
>>> y0 = 1
|
||||
>>> y = runge_kutta(f, y0, 0.0, 0.01, 5)
|
||||
>>> y[-1]
|
||||
>>> float(y[-1])
|
||||
148.41315904125113
|
||||
"""
|
||||
n = int(np.ceil((x_end - x0) / h))
|
||||
|
||||
@@ -34,12 +34,12 @@ def runge_kutta_fehlberg_45(
|
||||
>>> def f(x, y):
|
||||
... return 1 + y**2
|
||||
>>> y = runge_kutta_fehlberg_45(f, 0, 0, 0.2, 1)
|
||||
>>> y[1]
|
||||
>>> float(y[1])
|
||||
0.2027100937470787
|
||||
>>> def f(x,y):
|
||||
... return x
|
||||
>>> y = runge_kutta_fehlberg_45(f, -1, 0, 0.2, 0)
|
||||
>>> y[1]
|
||||
>>> float(y[1])
|
||||
-0.18000000000000002
|
||||
>>> y = runge_kutta_fehlberg_45(5, 0, 0, 0.1, 1)
|
||||
Traceback (most recent call last):
|
||||
|
||||
@@ -34,7 +34,7 @@ def runge_kutta_gills(
|
||||
>>> def f(x, y):
|
||||
... return (x-y)/2
|
||||
>>> y = runge_kutta_gills(f, 0, 3, 0.2, 5)
|
||||
>>> y[-1]
|
||||
>>> float(y[-1])
|
||||
3.4104259225717537
|
||||
|
||||
>>> def f(x,y):
|
||||
|
||||
@@ -28,7 +28,7 @@ def softmax(vector):
|
||||
|
||||
The softmax vector adds up to one. We need to ceil to mitigate for
|
||||
precision
|
||||
>>> np.ceil(np.sum(softmax([1,2,3,4])))
|
||||
>>> float(np.ceil(np.sum(softmax([1,2,3,4]))))
|
||||
1.0
|
||||
|
||||
>>> vec = np.array([5,5])
|
||||
|
||||
Reference in New Issue
Block a user