Cleanup simple_functions.py (#2437)

* Remove fdiv

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* actually remove fdiv

* Use lru cache and scipy's func

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* set maxsize

should be enough for how it's used

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Remove get_num_args

* Remove one instance of clip_in_place

* Readd clip_in_place, it has a use

* rm unnecessary line

* Properly clip color

* Revert "Properly clip color"

This reverts commit 0591c78334.

* remove clip in place

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* actually remove

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Darylgolden 2022-01-09 21:16:19 +08:00 committed by GitHub
commit d8dc0b462d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 21 additions and 76 deletions

View file

@ -26,7 +26,6 @@ from ..utils.color import color_to_int_rgba
from ..utils.family import extract_mobject_family_members
from ..utils.images import get_full_raster_image_path
from ..utils.iterables import list_difference_update
from ..utils.simple_functions import fdiv
from ..utils.space_ops import angle_of_vector
@ -223,7 +222,7 @@ class Camera:
pixel_width = self.pixel_width
frame_height = self.frame_height
frame_width = self.frame_width
aspect_ratio = fdiv(pixel_width, pixel_height)
aspect_ratio = pixel_width / pixel_height
if fixed_dimension == 0:
frame_height = frame_width / aspect_ratio
else:
@ -574,12 +573,12 @@ class Camera:
ctx.scale(pw, ph)
ctx.set_matrix(
cairo.Matrix(
fdiv(pw, fw),
(pw / fw),
0,
0,
-fdiv(ph, fh),
(pw / 2) - fc[0] * fdiv(pw, fw),
(ph / 2) + fc[1] * fdiv(ph, fh),
-(ph / fh),
(pw / 2) - fc[0] * (pw / fw),
(ph / 2) + fc[1] * (ph / fh),
),
)
self.cache_cairo_context(pixel_array, ctx)
@ -1105,7 +1104,7 @@ class Camera:
# TODO: This seems...unsystematic
big_sum = op.add(config["pixel_height"], config["pixel_width"])
this_sum = op.add(self.pixel_height, self.pixel_width)
factor = fdiv(big_sum, this_sum)
factor = big_sum / this_sum
return 1 + (thickness - 1) * factor
def get_thickening_nudges(self, thickness):
@ -1163,16 +1162,15 @@ class Camera:
uncentered_pixel_coords = np.indices([self.pixel_height, self.pixel_width])[
::-1
].transpose(1, 2, 0)
uncentered_space_coords = fdiv(
uncentered_pixel_coords * full_space_dims,
full_pixel_dims,
)
uncentered_space_coords = (
uncentered_pixel_coords * full_space_dims
) / full_pixel_dims
# Could structure above line's computation slightly differently, but figured (without much
# thought) multiplying by frame_shape first, THEN dividing by pixel_shape, is probably
# better than the other order, for avoiding underflow quantization in the division (whereas
# overflow is unlikely to be a problem)
centered_space_coords = uncentered_space_coords - fdiv(full_space_dims, 2)
centered_space_coords = uncentered_space_coords - (full_space_dims / 2)
# Have to also flip the y coordinates to account for pixel array being listed in
# top-to-bottom order, opposite of screen coordinate convention

View file

@ -16,7 +16,6 @@ from ..mobject.numbers import DecimalNumber
from ..mobject.types.vectorized_mobject import VGroup, VMobject
from ..utils.bezier import interpolate
from ..utils.config_ops import merge_dicts_recursively
from ..utils.simple_functions import fdiv
from ..utils.space_ops import normalize
@ -358,10 +357,7 @@ class NumberLine(Line):
"""
start, end = self.get_start_and_end()
unit_vect = normalize(end - start)
proportion = fdiv(
np.dot(point - start, unit_vect),
np.dot(end - start, unit_vect),
)
proportion = np.dot(point - start, unit_vect) / np.dot(end - start, unit_vect)
return interpolate(self.x_min, self.x_max, proportion)
def n2p(self, number: float) -> np.ndarray:

View file

@ -10,7 +10,7 @@ from ..mobject.types.opengl_vectorized_mobject import (
)
from ..utils.color import *
from ..utils.iterables import adjacent_n_tuples, adjacent_pairs
from ..utils.simple_functions import clip, fdiv
from ..utils.simple_functions import clip
from ..utils.space_ops import (
angle_between_vectors,
angle_of_vector,
@ -616,13 +616,13 @@ class OpenGLArrow(OpenGLLine):
vect = end - start
length = max(np.linalg.norm(vect), 1e-8)
thickness = self.thickness
w_ratio = fdiv(self.max_width_to_length_ratio, fdiv(thickness, length))
w_ratio = self.max_width_to_length_ratio / (thickness / length)
if w_ratio < 1:
thickness *= w_ratio
tip_width = self.tip_width_ratio * thickness
tip_length = tip_width / (2 * np.tan(self.tip_angle / 2))
t_ratio = fdiv(self.max_tip_length_to_length_ratio, fdiv(tip_length, length))
t_ratio = self.max_tip_length_to_length_ratio / (tip_length / length)
if t_ratio < 1:
tip_length *= t_ratio
tip_width *= t_ratio

View file

@ -36,7 +36,6 @@ from ...utils.bezier import (
from ...utils.color import BLACK, WHITE, color_to_rgba
from ...utils.deprecation import deprecated
from ...utils.iterables import make_even, stretch_array_to_length, tuplify
from ...utils.simple_functions import clip_in_place
from ...utils.space_ops import rotate_vector, shoelace_direction
from ..opengl_compatibility import ConvertToOpenGL
from .opengl_vectorized_mobject import OpenGLVMobject
@ -166,7 +165,7 @@ class VMobject(Mobject):
if sheen_factor != 0 and len(rgbas) == 1:
light_rgbas = np.array(rgbas)
light_rgbas[:, :3] += sheen_factor
clip_in_place(light_rgbas, 0, 1)
np.clip(light_rgbas, 0, 1, out=light_rgbas)
rgbas = np.append(rgbas, light_rgbas, axis=0)
return rgbas

View file

@ -54,7 +54,6 @@ from ..camera.multi_camera import MultiCamera
from ..constants import *
from ..mobject.types.image_mobject import ImageMobjectFromCamera
from ..scene.moving_camera_scene import MovingCameraScene
from ..utils.simple_functions import fdiv
# Note, any scenes from old videos using ZoomedScene will almost certainly
# break, as it was restructured.
@ -205,4 +204,4 @@ class ZoomedScene(MovingCameraScene):
float
The zoom factor.
"""
return fdiv(self.zoomed_camera.frame.height, self.zoomed_display.height)
return self.zoomed_camera.frame.height / self.zoomed_display.height

View file

@ -26,7 +26,6 @@ import numpy as np
from colour import Color
from ..utils.bezier import interpolate
from ..utils.simple_functions import clip_in_place
from ..utils.space_ops import normalize
@ -548,5 +547,4 @@ def get_shaded_rgb(
if factor < 0:
factor *= 0.5
result = rgb + factor
clip_in_place(rgb + factor, 0, 1)
return result

View file

@ -2,52 +2,26 @@
__all__ = [
"sigmoid",
"choose_using_cache",
"choose",
"get_num_args",
"get_parameters",
"clip_in_place",
"fdiv",
"binary_search",
]
import inspect
import operator as op
from functools import reduce
from functools import lru_cache
import numpy as np
from scipy import special
def sigmoid(x):
return 1.0 / (1 + np.exp(-x))
CHOOSE_CACHE = {}
def choose_using_cache(n, r):
if n not in CHOOSE_CACHE:
CHOOSE_CACHE[n] = {}
if r not in CHOOSE_CACHE[n]:
CHOOSE_CACHE[n][r] = choose(n, r, use_cache=False)
return CHOOSE_CACHE[n][r]
def choose(n, r, use_cache=True):
if use_cache:
return choose_using_cache(n, r)
if n < r:
return 0
if r == 0:
return 1
denominator = reduce(op.mul, range(1, r + 1), 1)
numerator = reduce(op.mul, range(n, n - r, -1), 1)
return numerator // denominator
def get_num_args(function):
return len(get_parameters(function))
@lru_cache(maxsize=10)
def choose(n, k):
return special.comb(n, k, exact=True)
def get_parameters(function):
@ -69,25 +43,6 @@ def clip(a, min_a, max_a):
return a
def clip_in_place(array, min_val=None, max_val=None):
if max_val is not None:
array[array > max_val] = max_val
if min_val is not None:
array[array < min_val] = min_val
return array
def fdiv(a, b, zero_over_zero_value=None):
if zero_over_zero_value is not None:
out = np.full_like(a, zero_over_zero_value)
where = np.logical_or(a != 0, b != 0)
else:
out = None
where = True
return np.true_divide(a, b, out=out, where=where)
def binary_search(function, target, lower_bound, upper_bound, tolerance=1e-4):
lh = lower_bound
rh = upper_bound