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

modified MArray class to exclude MArrayDirection Enum #23

Open
wants to merge 5 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
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ PASTE HERE

<details><summary>LaTeX details</summary>

Output of `latex --version`:
+ LaTeX distribution (e.g. TeX Live 2020):
+ Installed LaTeX packages:
<!-- output of `tlmgr list --only-installed` for TeX Live or a screenshot of the Packages page for MikTeX -->
Expand Down
3 changes: 2 additions & 1 deletion .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!-- Thank you for contributing to Manim Data Structures! Learn more about the process in our contributing guidelines: https://github.com/drageelr/manim-data-structures/blob/main/CONTRIBUTING.md -->
<!-- Thank you for contributing to Manim Data Structures! Learn more about the process in our contributing guidelines: https://github.com/ufosc/manim-data-structures/blob/main/CONTRIBUTING.md -->

## Overview: What does this pull request change?
<!-- If there is more information than the PR title that should be added to our release changelog, add it in the following changelog section. This is optional, but recommended for larger pull requests. -->
Expand All @@ -18,4 +18,5 @@
<!-- Thank you again for contributing! Do not modify the lines below, they are for reviewers. -->
## Reviewer Checklist
- [ ] The PR title is descriptive enough for the changelog, and the PR is labeled correctly
- [ ] If applicable: newly added code segments are adequately covered by tests
- [ ] If applicable: newly added non-private functions and classes have a docstring including a short summary and a PARAMETERS section
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ The general workflow for contributing to this project is as follows:
To interact with the project, you will need to install some dependencies and configure git.

1. [Install git](https://git-scm.com/)
2. [Install Manim dependencies](https://docs.manim.community/en/stable/installation.html)
2. [Install Manim dependencies](https://docs.manim.community/en/stable/installation.html#installing-manim-locally)
- Note: You do not need to install Manim itself (Poetry will handle that).
3. [Install Poetry](https://python-poetry.org/docs/master/#installing-with-the-official-installer)
4. Fork the [project](https://github.com/ufosc/manim-data-structures).
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ authors = ["Nikhil Iyer <[email protected]>", "Hammad Nasir <hammadn99@gma
readme = "README.md"
packages = [{include = "manim_data_structures", from = "src"}]
repository = "https://github.com/ufosc/manim-data-structures"
documentation = ""
documentation = "https://docs.manim.community/en/stable/"

[tool.poetry.dependencies]
python = ">=3.8.0,<3.11"
Expand Down
78 changes: 37 additions & 41 deletions src/manim_data_structures/m_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,13 +580,15 @@ class MArray(VGroup):
Represents the array label.
"""

__dir_map = [
{"arr": UP, "index": RIGHT},
{"arr": DOWN, "index": RIGHT},
{"arr": RIGHT, "index": UP},
{"arr": LEFT, "index": UP},
]
"""Maps :class:`~.m_enum.MArrayDirection` to :class:`np.ndarray`."""
__coor_alias_val = {
UP: {"index": RIGHT},
DOWN: {"index": RIGHT},
RIGHT: {"index": UP},
LEFT: {"index": UP},
}
'''
"""Maps :class:`np.ndarray` to :class:`np.ndarray`."""
'''

def __sum_elem_len(self, index_start: int, index_end: int) -> int:
"""Sums the side_length of all elements' square mobject present in the array between the specified range.
Expand Down Expand Up @@ -630,20 +632,21 @@ def __calc_label_pos_and_mob(self) -> typing.Tuple[Square, np.ndarray]:

# Label position is parallel to array growth direction
if np.array_equal(
self.__dir_map[self.__arr_label_pos.value]["arr"],
self.__dir_map[self.__arr_dir.value]["arr"],
# Compares two np.ndarrays
self.__arr_label_pos,
self.__arr_dir,
):
return (
self.__mob_arr[-1].fetch_mob_square(),
self.__dir_map[self.__arr_label_pos.value]["arr"],
self.__arr_label_pos,
)
elif np.array_equal(
self.__dir_map[self.__arr_label_pos.value]["arr"],
-self.__dir_map[self.__arr_dir.value]["arr"],
self.__arr_label_pos,
-self.__arr_dir,
):
return (
self.__mob_arr[0].fetch_mob_square(),
self.__dir_map[self.__arr_label_pos.value]["arr"],
self.__arr_label_pos,
)

# Label position is perpendicular to array growth direction
Expand All @@ -659,9 +662,7 @@ def __calc_label_pos_and_mob(self) -> typing.Tuple[Square, np.ndarray]:
)
return (
self.__mob_arr[middle_index].fetch_mob_square(),
self.__dir_map[self.__arr_label_pos.value]["arr"]
+ self.__dir_map[self.__arr_dir.value]["arr"]
* ((len_after - len_before) / 2),
self.__arr_label_pos + self.__arr_dir * ((len_after - len_before) / 2),
)

def __calc_index(self, index: int) -> typing.Union[int, str]:
Expand Down Expand Up @@ -698,9 +699,9 @@ def __calc_index_pos(self) -> np.ndarray:
"""

return (
self.__dir_map[self.__arr_dir.value]["index"]
self.__coor_alias_val[self.__arr_dir]["index"]
if not self.__switch_index_pos
else self.__dir_map[self.__arr_dir.value]["index"] * -1
else self.__coor_alias_val[self.__arr_dir]["index"] * -1
)

def __calc_label_shift_factor(self, mob: MArrayElement) -> float:
Expand All @@ -718,13 +719,13 @@ def __calc_label_shift_factor(self, mob: MArrayElement) -> float:
"""

if np.array_equal(
self.__dir_map[self.__arr_label_pos.value]["arr"],
self.__dir_map[self.__arr_dir.value]["arr"],
self.__arr_label_pos,
self.__arr_dir,
):
return mob.fetch_mob_square().side_length
elif not np.array_equal(
self.__dir_map[self.__arr_label_pos.value]["arr"],
-self.__dir_map[self.__arr_dir.value]["arr"],
self.__arr_label_pos,
-self.__arr_dir,
):
return mob.fetch_mob_square().side_length / 2
return 0
Expand Down Expand Up @@ -777,7 +778,7 @@ def __append_elem(
mob_index_args=mob_index_args,
index_pos=self.__calc_index_pos(),
next_to_mob=self.__mob_arr[-1] if len(self.__mob_arr) else None,
next_to_dir=self.__dir_map[self.__arr_dir.value]["arr"],
next_to_dir=self.__arr_dir,
)
)
self.add(self.__mob_arr[-1])
Expand All @@ -793,7 +794,7 @@ def __append_elem(
anim_list.append(
ApplyMethod(
self.__mob_arr_label.shift,
self.__dir_map[self.__arr_dir.value]["arr"] * label_shift_factor,
self.__arr_dir * label_shift_factor,
)
)

Expand Down Expand Up @@ -848,10 +849,7 @@ def __remove_elem(
anims_shift.append(
ApplyMethod(
self.__mob_arr[i].shift,
-(
self.__dir_map[self.__arr_dir.value]["arr"]
* removed_mob.fetch_mob_square().side_length
),
-(self.__arr_dir * removed_mob.fetch_mob_square().side_length),
)
)

Expand All @@ -861,7 +859,7 @@ def __remove_elem(
anims_shift.append(
ApplyMethod(
self.__mob_arr_label.shift,
-self.__dir_map[self.__arr_dir.value]["arr"] * label_shift_factor,
-self.__arr_dir * label_shift_factor,
)
)

Expand Down Expand Up @@ -919,9 +917,9 @@ def __init_props(
index_start: int,
index_hex_display: bool,
hide_index: bool,
arr_dir: MArrayDirection,
arr_dir: np.ndarray,
switch_index_pos: bool,
arr_label_pos: MArrayDirection,
arr_label_pos: np.ndarray,
arr_label_gap: float,
) -> None:
"""Initializes the attributes for the class.
Expand Down Expand Up @@ -963,9 +961,9 @@ def __init_props(
self.__index_start: int = index_start
self.__index_hex_display: bool = index_hex_display
self.__hide_index: int = hide_index
self.__arr_dir: MArrayDirection = arr_dir
self.__arr_dir: np.ndarray = arr_dir
self.__switch_index_pos: bool = switch_index_pos
self.__arr_label_pos: MArrayDirection = arr_label_pos
self.__arr_label_pos: np.ndarray = arr_label_pos
self.__arr_label_gap: float = arr_label_gap

def __update_props(
Expand Down Expand Up @@ -1007,8 +1005,7 @@ def __init_mobs(
)
if len(self.__mob_arr) % 2 == 0:
self.__mob_arr_label.shift(
-self.__dir_map[self.__arr_dir.value]["arr"]
* (next_to_mob.side_length / 2)
-self.__arr_dir * (next_to_mob.side_length / 2)
)
self.add(self.__mob_arr_label)

Expand All @@ -1021,9 +1018,9 @@ def __init__(
index_start: int = 0,
index_hex_display: bool = False,
hide_index: bool = False,
arr_dir: MArrayDirection = MArrayDirection.RIGHT,
arr_dir: np.ndarray = RIGHT,
switch_index_pos: bool = False,
arr_label_pos: MArrayDirection = MArrayDirection.LEFT,
arr_label_pos: np.ndarray = LEFT,
arr_label_gap: float = 0.5,
mob_arr_label_args: dict = {},
mob_square_args: dict = {},
Expand Down Expand Up @@ -1133,13 +1130,12 @@ def fetch_mob_arr_label(self) -> Text:

return self.__mob_arr_label

def fetch_arr_dir(self) -> MArrayDirection:
"""Fetches the growth direction enum of the array.
def fetch_arr_dir(self) -> np.ndarray:
"""Fetches the growth direction.

Returns
-------
:class:`~.m_enum.MArrayDirection`
:attr:`__arr_dir`.

"""

return self.__arr_dir
Expand Down