-
Notifications
You must be signed in to change notification settings - Fork 51
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
Overloading operators for MVariable, MArray and MArrayPointer classes. #18
Comments
Met some problems when trying to implement the class MyScene(Scene):
def construct(self):
arr = MArray(self, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
arr.shift(LEFT * 4 + UP * 2)
tar_val = MVariable(self, 3, label="target value")
tar_val.shift(DOWN * 2)
l = MArrayPointer(self, arr, 0, label="left")
r = MArrayPointer(self, arr, 9, label="right")
self.play(Create(arr))
self.play(Create(tar_val))
self.play(Create(l))
self.play(Create(r))
mid = MArrayPointer(self, arr, (l + r) // 2, label="mid")
self.play(Create(mid))
# find first larger or equal to target value
while (l <= r):
anims = []
if (tar_val > mid):
anims.append(l.shift_to_elem(mid + 1, play_anim=False))
else:
anims.append(r.shift_to_elem(mid - 1, play_anim=False))
anims.append(mid.shift_to_elem((l + r) // 2, play_anim=False))
self.play(AnimationGroup(*anims))
self.wait(1) The following error is shown:
So it seems like somewhere in the Manim library, the hash function is used. Then I tried to implement the hash function. In my idea, the hash function should return the same value if all the attributes are the same. So I use the following code as my implementation: def __key(self) -> tuple:
# return the tuple of all attributues, used for hashing
return (self.__scene, self.__arr, self.__index, self.__label, self.__arrow_len, self.__arrow_gap, self.__label_gap,
self.__pointer_pos, self.__updater_pos
)
def __hash__(self) -> int:
return hash(self.__key()) And the following error is shown after run the initial code:
Basically it says there is no attributes named |
Now I used this implementation in my forked repo: def __hash__(self) -> int:
return id(self) But the problem is that it will not give the same value even if all the attributes are the same since the address of the object in memory is not the same. |
So I'm wondering what the correct way of implementing this. if I used |
Discussed in #17
Originally posted by ttzytt February 22, 2023
Found some places to improve when using the package to implement the animation of a simple binary search algorithm:
For every comparison between
MArrayPointer
, you have to callfetch_index()
to get the position where the array is pointing. I think it will be better to overload comparators in MArrayPointer so that the implementation will be easier.The same works for other operators like add and subtract: the addition or subtraction between
MArrayPointer
an integer or anotherMArrayPointer
should give the addition and subtraction between the integer and the index that the pointer is pointing to in aMArray
. That way, the operation of pointers will be much easier, and code like the following will be simplified:r.shift_to_elem(mid.fetch_index() - 1, play_anim=False
mid.shift_to_elem((l.fetch_index() + r.fetch_index()) // 2
Another possible improvement from overloading operators is to overload
__getitem__
and__setitem__
forMArray
. Using these two methods, we could directly access theMArrayElement
inside theMArray
through the bracket operator, thus making animations onMArrayElement
in an easier way.I'm not sure about your ideas for these modifications, but if you think they are helpful, I can make a PR on that.
@ttzytt Please provide details for which classes and operators you think should be overloaded.
The text was updated successfully, but these errors were encountered: