cgmath icon indicating copy to clipboard operation
cgmath copied to clipboard

Restore operators for component wise vector mul and div

Open bitshifter opened this issue 9 years ago • 2 comments

I just ran into this when upgrading from cgmath 0.7 to cgmath 0.10 (I put it off because a lot of my code broke). While it may not be arithmetically valid to do element wise multiply and divisions it is quite common and convenient to do so, which is why glsl and hlsl vector types support this, as does glm.

Some examples of where I was using this:

// normalise mouse velocity by screen_size - mouse_delta and screen_size are vec2
mouse_delta * 100.0 / screen_size; // was this
(mouse_delta * 100.0).div_element_wise(screen_size); // now this
// initialise particle properties with random variation - min_offset and offset_range are vec3
let offset = min_offset + offset_range * rng.gen::<Vector3<f32>>(); // was this
let offset = min_offset + offset_range.mul_element_wise(rng.gen::<Vector3<f32>>()); // now this

bitshifter avatar Jun 13 '16 04:06 bitshifter

On 0.15.0, the old way should work since mouse_delta and offset_range both implement the VectorSpace trait.

linkmauve avatar Jan 02 '18 02:01 linkmauve

I agree, and along with component-wise multiplication, it would be nice to have a One impl for Vector* for the multiplicative identity.

On 0.15.0, the old way should work since mouse_delta and offset_range both implement the VectorSpace trait.

AFAICT, this doesn't solve the problem, as VectorSpace doesn't guarantee an implementation of Mul<Self>

agausmann avatar Apr 20 '21 02:04 agausmann