mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-05 01:09:40 +08:00
Enable ruff ICN001 rule (#11329)
* Enable ruff ICN001 rule * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
@ -25,8 +25,8 @@ import warnings
|
||||
from collections.abc import Callable
|
||||
from typing import Any
|
||||
|
||||
import numpy
|
||||
from matplotlib import pyplot
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
|
||||
c_cauliflower = 0.25 + 0.0j
|
||||
c_polynomial_1 = -0.4 + 0.6j
|
||||
@ -37,22 +37,20 @@ window_size = 2.0
|
||||
nb_pixels = 666
|
||||
|
||||
|
||||
def eval_exponential(c_parameter: complex, z_values: numpy.ndarray) -> numpy.ndarray:
|
||||
def eval_exponential(c_parameter: complex, z_values: np.ndarray) -> np.ndarray:
|
||||
"""
|
||||
Evaluate $e^z + c$.
|
||||
>>> eval_exponential(0, 0)
|
||||
1.0
|
||||
>>> abs(eval_exponential(1, numpy.pi*1.j)) < 1e-15
|
||||
>>> abs(eval_exponential(1, np.pi*1.j)) < 1e-15
|
||||
True
|
||||
>>> abs(eval_exponential(1.j, 0)-1-1.j) < 1e-15
|
||||
True
|
||||
"""
|
||||
return numpy.exp(z_values) + c_parameter
|
||||
return np.exp(z_values) + c_parameter
|
||||
|
||||
|
||||
def eval_quadratic_polynomial(
|
||||
c_parameter: complex, z_values: numpy.ndarray
|
||||
) -> numpy.ndarray:
|
||||
def eval_quadratic_polynomial(c_parameter: complex, z_values: np.ndarray) -> np.ndarray:
|
||||
"""
|
||||
>>> eval_quadratic_polynomial(0, 2)
|
||||
4
|
||||
@ -66,7 +64,7 @@ def eval_quadratic_polynomial(
|
||||
return z_values * z_values + c_parameter
|
||||
|
||||
|
||||
def prepare_grid(window_size: float, nb_pixels: int) -> numpy.ndarray:
|
||||
def prepare_grid(window_size: float, nb_pixels: int) -> np.ndarray:
|
||||
"""
|
||||
Create a grid of complex values of size nb_pixels*nb_pixels with real and
|
||||
imaginary parts ranging from -window_size to window_size (inclusive).
|
||||
@ -77,20 +75,20 @@ def prepare_grid(window_size: float, nb_pixels: int) -> numpy.ndarray:
|
||||
[ 0.-1.j, 0.+0.j, 0.+1.j],
|
||||
[ 1.-1.j, 1.+0.j, 1.+1.j]])
|
||||
"""
|
||||
x = numpy.linspace(-window_size, window_size, nb_pixels)
|
||||
x = np.linspace(-window_size, window_size, nb_pixels)
|
||||
x = x.reshape((nb_pixels, 1))
|
||||
y = numpy.linspace(-window_size, window_size, nb_pixels)
|
||||
y = np.linspace(-window_size, window_size, nb_pixels)
|
||||
y = y.reshape((1, nb_pixels))
|
||||
return x + 1.0j * y
|
||||
|
||||
|
||||
def iterate_function(
|
||||
eval_function: Callable[[Any, numpy.ndarray], numpy.ndarray],
|
||||
eval_function: Callable[[Any, np.ndarray], np.ndarray],
|
||||
function_params: Any,
|
||||
nb_iterations: int,
|
||||
z_0: numpy.ndarray,
|
||||
z_0: np.ndarray,
|
||||
infinity: float | None = None,
|
||||
) -> numpy.ndarray:
|
||||
) -> np.ndarray:
|
||||
"""
|
||||
Iterate the function "eval_function" exactly nb_iterations times.
|
||||
The first argument of the function is a parameter which is contained in
|
||||
@ -98,22 +96,22 @@ def iterate_function(
|
||||
values to iterate from.
|
||||
This function returns the final iterates.
|
||||
|
||||
>>> iterate_function(eval_quadratic_polynomial, 0, 3, numpy.array([0,1,2])).shape
|
||||
>>> iterate_function(eval_quadratic_polynomial, 0, 3, np.array([0,1,2])).shape
|
||||
(3,)
|
||||
>>> numpy.round(iterate_function(eval_quadratic_polynomial,
|
||||
>>> np.round(iterate_function(eval_quadratic_polynomial,
|
||||
... 0,
|
||||
... 3,
|
||||
... numpy.array([0,1,2]))[0])
|
||||
... np.array([0,1,2]))[0])
|
||||
0j
|
||||
>>> numpy.round(iterate_function(eval_quadratic_polynomial,
|
||||
>>> np.round(iterate_function(eval_quadratic_polynomial,
|
||||
... 0,
|
||||
... 3,
|
||||
... numpy.array([0,1,2]))[1])
|
||||
... np.array([0,1,2]))[1])
|
||||
(1+0j)
|
||||
>>> numpy.round(iterate_function(eval_quadratic_polynomial,
|
||||
>>> np.round(iterate_function(eval_quadratic_polynomial,
|
||||
... 0,
|
||||
... 3,
|
||||
... numpy.array([0,1,2]))[2])
|
||||
... np.array([0,1,2]))[2])
|
||||
(256+0j)
|
||||
"""
|
||||
|
||||
@ -121,8 +119,8 @@ def iterate_function(
|
||||
for _ in range(nb_iterations):
|
||||
z_n = eval_function(function_params, z_n)
|
||||
if infinity is not None:
|
||||
numpy.nan_to_num(z_n, copy=False, nan=infinity)
|
||||
z_n[abs(z_n) == numpy.inf] = infinity
|
||||
np.nan_to_num(z_n, copy=False, nan=infinity)
|
||||
z_n[abs(z_n) == np.inf] = infinity
|
||||
return z_n
|
||||
|
||||
|
||||
@ -130,21 +128,21 @@ def show_results(
|
||||
function_label: str,
|
||||
function_params: Any,
|
||||
escape_radius: float,
|
||||
z_final: numpy.ndarray,
|
||||
z_final: np.ndarray,
|
||||
) -> None:
|
||||
"""
|
||||
Plots of whether the absolute value of z_final is greater than
|
||||
the value of escape_radius. Adds the function_label and function_params to
|
||||
the title.
|
||||
|
||||
>>> show_results('80', 0, 1, numpy.array([[0,1,.5],[.4,2,1.1],[.2,1,1.3]]))
|
||||
>>> show_results('80', 0, 1, np.array([[0,1,.5],[.4,2,1.1],[.2,1,1.3]]))
|
||||
"""
|
||||
|
||||
abs_z_final = (abs(z_final)).transpose()
|
||||
abs_z_final[:, :] = abs_z_final[::-1, :]
|
||||
pyplot.matshow(abs_z_final < escape_radius)
|
||||
pyplot.title(f"Julia set of ${function_label}$, $c={function_params}$")
|
||||
pyplot.show()
|
||||
plt.matshow(abs_z_final < escape_radius)
|
||||
plt.title(f"Julia set of ${function_label}$, $c={function_params}$")
|
||||
plt.show()
|
||||
|
||||
|
||||
def ignore_overflow_warnings() -> None:
|
||||
|
@ -22,25 +22,25 @@ Requirements (pip):
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import matplotlib.pyplot as plt # type: ignore
|
||||
import numpy
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
|
||||
# initial triangle of Koch snowflake
|
||||
VECTOR_1 = numpy.array([0, 0])
|
||||
VECTOR_2 = numpy.array([0.5, 0.8660254])
|
||||
VECTOR_3 = numpy.array([1, 0])
|
||||
VECTOR_1 = np.array([0, 0])
|
||||
VECTOR_2 = np.array([0.5, 0.8660254])
|
||||
VECTOR_3 = np.array([1, 0])
|
||||
INITIAL_VECTORS = [VECTOR_1, VECTOR_2, VECTOR_3, VECTOR_1]
|
||||
|
||||
# uncomment for simple Koch curve instead of Koch snowflake
|
||||
# INITIAL_VECTORS = [VECTOR_1, VECTOR_3]
|
||||
|
||||
|
||||
def iterate(initial_vectors: list[numpy.ndarray], steps: int) -> list[numpy.ndarray]:
|
||||
def iterate(initial_vectors: list[np.ndarray], steps: int) -> list[np.ndarray]:
|
||||
"""
|
||||
Go through the number of iterations determined by the argument "steps".
|
||||
Be careful with high values (above 5) since the time to calculate increases
|
||||
exponentially.
|
||||
>>> iterate([numpy.array([0, 0]), numpy.array([1, 0])], 1)
|
||||
>>> iterate([np.array([0, 0]), np.array([1, 0])], 1)
|
||||
[array([0, 0]), array([0.33333333, 0. ]), array([0.5 , \
|
||||
0.28867513]), array([0.66666667, 0. ]), array([1, 0])]
|
||||
"""
|
||||
@ -50,13 +50,13 @@ def iterate(initial_vectors: list[numpy.ndarray], steps: int) -> list[numpy.ndar
|
||||
return vectors
|
||||
|
||||
|
||||
def iteration_step(vectors: list[numpy.ndarray]) -> list[numpy.ndarray]:
|
||||
def iteration_step(vectors: list[np.ndarray]) -> list[np.ndarray]:
|
||||
"""
|
||||
Loops through each pair of adjacent vectors. Each line between two adjacent
|
||||
vectors is divided into 4 segments by adding 3 additional vectors in-between
|
||||
the original two vectors. The vector in the middle is constructed through a
|
||||
60 degree rotation so it is bent outwards.
|
||||
>>> iteration_step([numpy.array([0, 0]), numpy.array([1, 0])])
|
||||
>>> iteration_step([np.array([0, 0]), np.array([1, 0])])
|
||||
[array([0, 0]), array([0.33333333, 0. ]), array([0.5 , \
|
||||
0.28867513]), array([0.66666667, 0. ]), array([1, 0])]
|
||||
"""
|
||||
@ -74,22 +74,22 @@ def iteration_step(vectors: list[numpy.ndarray]) -> list[numpy.ndarray]:
|
||||
return new_vectors
|
||||
|
||||
|
||||
def rotate(vector: numpy.ndarray, angle_in_degrees: float) -> numpy.ndarray:
|
||||
def rotate(vector: np.ndarray, angle_in_degrees: float) -> np.ndarray:
|
||||
"""
|
||||
Standard rotation of a 2D vector with a rotation matrix
|
||||
(see https://en.wikipedia.org/wiki/Rotation_matrix )
|
||||
>>> rotate(numpy.array([1, 0]), 60)
|
||||
>>> rotate(np.array([1, 0]), 60)
|
||||
array([0.5 , 0.8660254])
|
||||
>>> rotate(numpy.array([1, 0]), 90)
|
||||
>>> rotate(np.array([1, 0]), 90)
|
||||
array([6.123234e-17, 1.000000e+00])
|
||||
"""
|
||||
theta = numpy.radians(angle_in_degrees)
|
||||
c, s = numpy.cos(theta), numpy.sin(theta)
|
||||
rotation_matrix = numpy.array(((c, -s), (s, c)))
|
||||
return numpy.dot(rotation_matrix, vector)
|
||||
theta = np.radians(angle_in_degrees)
|
||||
c, s = np.cos(theta), np.sin(theta)
|
||||
rotation_matrix = np.array(((c, -s), (s, c)))
|
||||
return np.dot(rotation_matrix, vector)
|
||||
|
||||
|
||||
def plot(vectors: list[numpy.ndarray]) -> None:
|
||||
def plot(vectors: list[np.ndarray]) -> None:
|
||||
"""
|
||||
Utility function to plot the vectors using matplotlib.pyplot
|
||||
No doctest was implemented since this function does not have a return value
|
||||
|
Reference in New Issue
Block a user