Skip to content
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

Improve @ operator to work on different coordinate systems #3879

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion manim/mobject/geometry/shape_matchers.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def set_style(self, fill_opacity: float, **kwargs: Any) -> Self: # type: ignore
def get_fill_color(self) -> ManimColor:
# The type of the color property is set to Any using the property decorator
# vectorized_mobject.py#L571
temp_color: ManimColor = self.color
temp_color: ManimColor = self.color # type: ignore[has-type]
return temp_color


Expand Down
23 changes: 21 additions & 2 deletions manim/mobject/graphing/coordinate_systems.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def __init__(
self.y_length = y_length
self.num_sampled_graph_points_per_tick = 10

def coords_to_point(self, *coords: ManimFloat):
def coords_to_point(self, *coords: float):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def coords_to_point(self, *coords: float):
def coords_to_point(self, *coords: float) -> Point3D:

raise NotImplementedError()

def point_to_coords(self, point: Point3D):
Expand Down Expand Up @@ -1836,12 +1836,19 @@ def construct(self):

return T_label_group

def __matmul__(self, coord: Point3D | Mobject):
def __matmul__(self, coord: Iterable[float] | Mobject):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left a suggestion regarding the return type.

About the parameter type, I'm not sure. The Liskov substitution principle says that, when overriding methods, parameter types are contravariant and return types are covariant, which means that the parameter type cannot get more specific when overriding a method. It can only get broader or stay the same. The reverse is true for return types.

Maybe the parameter type could be Mobject, because all of the coordinate systems accept a Mobject as an argument (except for ComplexPlane, but that should be changed in another PR).

Suggested change
def __matmul__(self, coord: Iterable[float] | Mobject):
def __matmul__(self, coord: Iterable[float] | Mobject) -> Point3D:

if isinstance(coord, Mobject):
coord = coord.get_center()
return self.coords_to_point(*coord)

def __rmatmul__(self, point: Point3D):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the return type should be something like this?

Suggested change
def __rmatmul__(self, point: Point3D):
def __rmatmul__(self, point: Point3D) -> Point2D | Point3D | complex:

"""Perform a point-to-coords action for a coordinate scene.

.. warning::

This will not work with NumPy arrays or other objects that
implement ``__matmul__``.
"""
return self.point_to_coords(point)


Expand Down Expand Up @@ -3327,6 +3334,12 @@ def get_radian_label(self, number, font_size: float = 24, **kwargs: Any) -> Math

return MathTex(string, font_size=font_size, **kwargs)

def __matmul__(self, coord: Point2D):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def __matmul__(self, coord: Point2D):
def __matmul__(self, coord: Point2D) -> Point3D:

return self.polar_to_point(*coord)

def __rmatmul__(self, point: Point2D):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def __rmatmul__(self, point: Point2D):
def __rmatmul__(self, point: Point3D) -> Point2D:

return self.point_to_polar(point)


class ComplexPlane(NumberPlane):
"""A :class:`~.NumberPlane` specialized for use with complex numbers.
Expand Down Expand Up @@ -3399,6 +3412,12 @@ def p2n(self, point: Point3D) -> complex:
"""Abbreviation for :meth:`point_to_number`."""
return self.point_to_number(point)

def __matmul__(self, coord: float | complex):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just noticed that ComplexPlane.number_to_point(), unlike the rest of coordinate systems, does not accept a Mobject as its parameter. It should be changed, but let's do that in another PR.

Suggested change
def __matmul__(self, coord: float | complex):
def __matmul__(self, coord: float | complex) -> Point3D:

return self.number_to_point(coord)

def __rmatmul__(self, point: Point3D):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def __rmatmul__(self, point: Point3D):
def __rmatmul__(self, point: Point3D) -> complex:

return self.point_to_number(point)

def _get_default_coordinate_values(self) -> list[float | complex]:
"""Generate a list containing the numerical values of the plane's labels.

Expand Down
45 changes: 45 additions & 0 deletions tests/module/mobject/graphing/test_coordinate_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import math

import numpy as np
import numpy.testing as nt
import pytest

from manim import (
Expand All @@ -14,6 +15,7 @@
Circle,
ComplexPlane,
Dot,
NumberLine,
NumberPlane,
PolarPlane,
ThreeDAxes,
Expand Down Expand Up @@ -192,3 +194,46 @@ def test_input_to_graph_point():
# test the line_graph implementation
position = np.around(ax.input_to_graph_point(x=PI, graph=line_graph), decimals=4)
np.testing.assert_array_equal(position, (2.6928, 1.2876, 0))


def test_matmul_operations():
ax = Axes()
nt.assert_equal(ax @ (1, 2), ax.coords_to_point(1, 2))
# should work with mobjects too, using their center
mob = Dot().move_to((1, 2, 0))
nt.assert_equal(ax @ mob, ax.coords_to_point(1, 2))

# other coordinate systems like PolarPlane and ComplexPlane should override __matmul__ indirectly
polar = PolarPlane()
nt.assert_equal(polar @ (1, 2), polar.polar_to_point(1, 2))

complx = ComplexPlane()
nt.assert_equal(complx @ (1 + 2j), complx.number_to_point(1 + 2j))

# Numberline doesn't inherit from CoordinateSystem, but it should still work
n = NumberLine()
nt.assert_equal(n @ 3, n.number_to_point(3))


def test_rmatmul_operations():
point = (1, 2, 0)

ax = Axes()
nt.assert_equal(point @ ax, ax.point_to_coords(point))

polar = PolarPlane()
assert point @ polar == polar.point_to_polar(point)

complx = ComplexPlane()
nt.assert_equal(point @ complx, complx.point_to_number(point))

n = NumberLine()
point = n @ 4

nt.assert_equal(
tuple(point) @ n, # ndarray overrides __matmul__
n.point_to_number(point),
)

mob = Dot().move_to(point)
nt.assert_equal(mob @ n, n.point_to_number(mob.get_center()))
16 changes: 15 additions & 1 deletion tests/test_graphical_units/test_coordinate_systems.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
from __future__ import annotations

from manim import *
from manim import (
BLUE,
GREEN,
ORANGE,
RED,
UL,
YELLOW,
Axes,
LogBase,
NumberPlane,
ThreeDAxes,
ThreeDScene,
VGroup,
np,
)
from manim.utils.testing.frames_comparison import frames_comparison

__module_test__ = "coordinate_system"
Expand Down
Loading