compas icon indicating copy to clipboard operation
compas copied to clipboard

Vector operators

Open ZacZhangzhuo opened this issue 1 year ago • 4 comments

Describe the bug

for Vector peration *, like:

>>> u = Vector(1.000, 1.000, 0.000)
>>> u * 2
Vector(2.000, 2.000, 0.000)

if we do: Vector * float: It is fine. float * Vector: TypeError: unsupported operand type(s) for *: 'float' and 'Vector'.

Expected behavior I think this could be fixed with minimal effort just for a better user experience. :)

ZacZhangzhuo avatar Jan 04 '24 17:01 ZacZhangzhuo

Can it really be solved easily? I have the feeling it's not possible to define the binding of the star operator to take right hand operand as precedence...

gonzalocasas avatar Jan 04 '24 18:01 gonzalocasas

yes, we could implement the "r" versions of the magic methods, but i don't know if there would be any unwanted side-effects...

def __rmul__(self, factor):
    ...

tomvanmele avatar Jan 04 '24 18:01 tomvanmele

Ah true 👍

gonzalocasas avatar Jan 04 '24 18:01 gonzalocasas

yes, it's not a big issue. mathmatically the floating number is preferred in the front. like a = k b, a, b are vectors.

ZacZhangzhuo avatar Jan 05 '24 09:01 ZacZhangzhuo

@tomvanmele you're right that rmul would address the issue, and @ZacZhangzhuo is right in that operators in Vectors should be complete -- but float * Vector is quite counter intuitive?

jf--- avatar Apr 08 '24 19:04 jf---

i agree, that is why never implemented the "r" versions. i think it is easy enough for the user to be explicit about their intent by writing the compas object first...

tomvanmele avatar Apr 08 '24 19:04 tomvanmele

@tomvanmele I know I'm being frantic, but seems like this issue can be closed? The broader concept of vec ops isn't discussed.

jf--- avatar Apr 08 '24 21:04 jf---

@tomvanmele you're right that rmul would address the issue, and @ZacZhangzhuo is right in that operators in Vectors should be complete -- but float * Vector is quite counter intuitive?

It is actually quite common and intuitive, especially when you compute a "WEIGHTED * long-expression"....

xarthurx avatar Apr 29 '24 11:04 xarthurx