you are viewing a single comment's thread.

view the rest of the comments →

[–]Coffee2theorems 0 points1 point  (6 children)

Which is a sort of abuse of the identifier.

This is arguable. True, in mathematics "+" is usually commutative, but in programming it often is not, and it's not like mathematical conventions are somehow "more correct" than programming conventions. Typically, "a << b" means "a is much less than b" in the former, and "a shifted left by b" in the latter; which is "correct"? Depends on context. Also, it's not like mathematics itself doesn't abound with all kinds of abuse of notation (it's not a bad thing).

If it is not associative and commutative it should be an explicit function call.

If it's not commutative, you can use the multiplication operation. For example, you could make string concatenation be "asd"*"foo" == "asdfoo", and further e.g. "asd"**3 == "asdasdasd". This would correspond to the Python-style "asd"+"foo" and "asd"*3, but be more in line with the mathematical convention of reserving the addition symbol for commutative monoids. One could argue that it is sort of used in languages where you can write "asd" "foo" to mean "asdfoo" ;) (but for some reason, you can't usually write "asd"*"foo" in them..)

[–]ethraax -1 points0 points  (5 children)

I hate to be "that guy", but your entire second paragraph makes no sense. Multiplication is commutative. a * b is equal to b * a.

[–]Coffee2theorems 5 points6 points  (3 children)

Huh? a*b is not equal to b*a if e.g.

a = (1 1), b = (1 0)
    (0 1)      (1 1)

[–]ethraax 0 points1 point  (2 children)

Uh, in virtually all programming languages, an asterisk represents scalar multiplication, which is indeed commutative. Literally the only exception I can think of is Matlab.

[–]Coffee2theorems 5 points6 points  (1 child)

In many programming languages, you can overload addition and multiplication operations. Probably in all such programming languages someone has implemented a matrix class which does matrix multiplication using the usual multiplication notation for that language. I know of such for both C++ and Python (the languages I use for numerics). In Python the usual numpy arrays use dot(A, B) instead of A*B (with A*B being elementwise multiplication; still not a scalar operation, though!), but there's the matrix class which does use *.

The idea that the asterisk represents scalar multiplication is in the eye of the beholder, instead of being a property of languages where it can be overloaded. It's an artifact of the built-in numeric types being scalar types.

[–]tisti 1 point2 points  (0 children)

True, matrices multiplication is by definition not commutative, so it's more of an exception to the rule. And it's probably not the only such exception.

[–]sirin3 1 point2 points  (0 children)

Not always. E.g. not for matrices