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

Default impl? #58

Open
jgarvin opened this issue Jul 28, 2020 · 11 comments · May be fixed by #76
Open

Default impl? #58

jgarvin opened this issue Jul 28, 2020 · 11 comments · May be fixed by #76
Labels

Comments

@jgarvin
Copy link

jgarvin commented Jul 28, 2020

It would be convenient if mint::Vector2::default() worked (giving zeros) so my own structs could #[derive(Default)].

@kvark
Copy link
Owner

kvark commented Jul 28, 2020

See #38 (comment)

TL;DR: implementing this is possible for some but not the others. So we can't universally provide Default, and therefore we should consider not providing it at all.

@kvark kvark added the question label Jul 28, 2020
@jgarvin
Copy link
Author

jgarvin commented Jul 28, 2020 via email

@kvark
Copy link
Owner

kvark commented Jul 29, 2020

I disagree that impl Default for Matrix is obvious to be all-zeroes (filled with T::default()). If I see that a matrix type in a math library implements default, I'll first think about an identity matrix.

@jgarvin
Copy link
Author

jgarvin commented Jul 29, 2020 via email

@kvark
Copy link
Owner

kvark commented Jul 29, 2020

(thank you for bearing with me on this question!)

The standard library doesn't have math vectors and matrices, so it hardly sets up an example here. If we look at default implementations in the math libraries, they appear to be all over the place:

  • cgmath: no default
  • nalgebra: there is some default, but it's really hard to see what it does. Likely just fills with T::default() like you suggest
  • glam: identity default
  • ultraviolet: identity

And that's the reason why I'm hesitating to add anything to mint. The expectations are unclear.

@kvark
Copy link
Owner

kvark commented Dec 21, 2023

Having dog-fooded mint in my projects extensively, I find it unfortunate to not have defaults for at least vectors. It's just convenient when putting into larger types that can be defaulted. And for vectors, in most situations zeroes is a perfectly reasonable choice. As an example, in Blade config's positions and rotations.

Now, it would be good to have a consistent policy for implementing defaults. I'd argue that initializing quaternions and matrices with "identity" poses the minimal surprise factor for all the users. If the user code expects an identity, they get it. If they don't expect anything useful (e.g. all zeroes), the identity isn't going to screw them over.

To re-iterate on this line of thought:

  1. I believe having some defaults would improve the usability of the library.
  2. The "identity" default does imply a specific operation (like multiplication for matrices, and addition for vectors), but this is applicable in most cases.
  3. In the spirit of "perfect is the enemy of the good" we should add the defaults.

@Ralith
Copy link
Contributor

Ralith commented Dec 21, 2023

For comparison, nalgebra's default is zeroes for matrices, vectors, and general quaternions, and the identity rotation for UnitQuaternion. I agree that having some default, even a completely arbitrary one, is helpful.

There doesn't seem to be a drawback to going with identity for square matrixes, but what should non-square matrices have?

@jgarvin
Copy link
Author

jgarvin commented Dec 21, 2023

I still think zeroes is the most natural choice in a programming context:

  • It's consistent with numeric scalar types. If a 1x1 i32 matrix defaults to 1, but a scalar i32 defaults to 0, that feels inconsistent.
  • It's the behavior you get with container types like Vec<i32> if you resize to larger than the current size. Vectors and Matrices from programmer POV are container-like.
  • It's the default state of mmapped memory. From programmer POV matrix can also be thought of as a large memory block.

@kvark
Copy link
Owner

kvark commented Dec 23, 2023

I'm not keen on the argument that matrices should be naturally analogous to scalars or Vec<i32>. In terms of game development, matrices have very concrete applicability, and they imply multiplication. mint is meant for this - it's not exposing arbitrary matrix dimensions but only those that are practically used for games and visualizations.

Is there a concern with going for identity-ish non-square matrices? E.g.

1 0 0 0
0 1 0 0
0 0 1 0

and

1 0 0
0 1 0
0 0 1
0 0 0

Basically, treat them as parts of a bigger square matrix for cases where the remainder is well known and doesn't need to be stored.

@jgarvin
Copy link
Author

jgarvin commented Dec 23, 2023

Adding and subtracting matrices is also a typical matrix operation, and there 0 is a sane default. Also it avoids needing a separate solution for non-square.

@Ralith
Copy link
Contributor

Ralith commented Dec 24, 2023

Is there a concern with going for identity-ish non-square matrices?

I suppose the non-square matrixes you generally see in games/graphics are truncations of homogeneous matrices or their transpose, for which that makes perfect sense. Certainly holds for most of what I've done.

Adding and subtracting matrices is also a typical matrix operation

Not in linear algebra for computer graphics, I don't think.

@kvark kvark linked a pull request Dec 24, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants