dimforge / nalgebra

Linear algebra library for Rust.
https://nalgebra.org
Apache License 2.0
3.98k stars 476 forks source link

Component wise multiplication with nalgebra-glm #716

Open ciechowoj opened 4 years ago

ciechowoj commented 4 years ago

Cannot multiply two vectors component wise (glm 0.7.0):

    use glm::*;
    let brick_size = vec2(64f32, 32f32);
    let brick_spacing = vec2(8f32, 8f32);
    let xxx = brick_size * brick_spacing;
ciechowoj commented 4 years ago
error[E0277]: the trait bound `nalgebra::base::constraint::ShapeConstraint: nalgebra::base::constraint::DimEq<glm::U1, glm::U2>` is not satisfied
  --> src/game.rs:72:26
   |
72 |     let xxx = brick_size * brick_spacing;
   |                          ^ the trait `nalgebra::base::constraint::DimEq<glm::U1, glm::U2>` is not implemented for `nalgebra::base::constraint::ShapeConstraint`
   |
   = help: the following implementations were found:
             <nalgebra::base::constraint::ShapeConstraint as nalgebra::base::constraint::DimEq<D, D>>
             <nalgebra::base::constraint::ShapeConstraint as nalgebra::base::constraint::DimEq<D, nalgebra::base::dimension::Dynamic>>
             <nalgebra::base::constraint::ShapeConstraint as nalgebra::base::constraint::DimEq<nalgebra::base::dimension::Dynamic, D>>
   = note: required because of the requirements on the impl of `nalgebra::base::constraint::AreMultipliable<glm::U2, glm::U1, glm::U2, glm::U1>` for `nalgebra::base::constraint::ShapeConstraint`
   = note: required because of the requirements on the impl of `std::ops::Mul` for `nalgebra::base::matrix::Matrix<f32, glm::U2, glm::U1, nalgebra::base::array_storage::ArrayStorage<f32, glm::U2, glm::U1>>`
sebcrozet commented 4 years ago

Hi! Yes, it is not possible because the multiplication operator is already used for matrix multiplication. For componentwise multiplication you can do let xxx = brick_size.component_mul(&brick_spacing).

therocode commented 4 years ago

I tripped over this one as well. It became extra confusing when glm::convert(brick_size) * brick_spacing works, which I added because one was IVec and the other Vec. I assume the glm::convert is capable of flipping rows -> columns?

Anyway, maybe it can be added to https://www.nalgebra.org/rustdoc_glm/nalgebra_glm/index.html#main-differences-compared-to-glm seems like it'll trip up a lot of users.

Tastaturtaste commented 3 years ago

I also tripped over this issue. Finding this thread was also not easy, because the title "nalgebra-glm" is pretty generic when you are searching for a issue with elementwise multiplication.

rokonio commented 2 years ago

Is glm::convert or component_mut the best approach ?

sebcrozet commented 2 years ago

@rokonio The recommended approach is component_mut.