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

[misc.arrayTools] move Vector to its own submodule, and rewrite as a tuple subclass #2201

Merged
merged 2 commits into from
Mar 2, 2021

Conversation

justvanrossum
Copy link
Collaborator

I felt our n-dimensional Vector class could be improved by making it a subclass of tuple, and deserves its own module.

This change makes Vector instances immutable. It appears this was the intention, as it did support __getitem__ but not __setitem__.

The internal values attribute is gone. I added a values read-only property for backward compatibility. Setting .values is no longer supported (due to Vector's new immutablity).

I added a two methods:

  • vector.length(): an alias for abs(vector)
  • vector.normalized(): returns the norm vector

I added some DeprecationWarnings.

Copy link
Member

@anthrotype anthrotype left a comment

Choose a reason for hiding this comment

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

Thanks!

When I implement Vector class, I like to call what you namedlength() => norm() and what you named normalized() => unit().

But it probably doesn't really matter.

I wonder about the use of abs() as a shortcut to computing the norm/magnitude/length/whathaveyou of the vector. Is that common? I guess it can be handy, but also may surprise a bit (it did surprise me).

@justvanrossum
Copy link
Collaborator Author

When I implement Vector class, I like to call what you named length() => norm()

It seems that "length", "norm" and "magnitude" are all synonyms here. I prefer "length" as it's a word that anyone understands. I just had to google "norm", as I wasn't even sure...

and what you named normalized() => unit().

Right, that makes sense, too. I prefer "normalized" as it implies an action.

I wonder about the use of abs() as a shortcut to computing the norm/magnitude/length/whathaveyou of the vector. Is that common? I guess it can be handy, but also may surprise a bit (it did surprise me).

It surprised me, too, but I took it from the original Vector, so it better stay for b/w compatibility... It makes sense given the |vec| mathematical notation, but this was the first time I saw it used in Python via __abs__.

@anthrotype
Copy link
Member

ok, sgtm! thanks

@justvanrossum justvanrossum merged commit 07a5e2d into fonttools:main Mar 2, 2021
@justvanrossum justvanrossum deleted the revamp-vector branch March 2, 2021 09:11
@justvanrossum
Copy link
Collaborator Author

Now it would be nice to use it more in fonttools itself. Many things in bezierTools could benefit from it, like calcCubicParameters().

(Back in the day fonttools used numpy to make these kinds of operations easier to write. I'm very glad Behdad got rid of that dependency, but I've occationally missed the convenience.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants