C++ Logo


Advanced search

Re: [std-proposals] Add operator for element wise multiplication.

From: Patrik Tegelberg <patrik_at_[hidden]>
Date: Tue, 10 May 2022 17:12:07 +0200

Thank you Edward, the named operator idiom is interesting. I am a little concerned about how it performs. If it has any cost then I’ll stick to my current work around, which is to use a.scale(b); for element wise multiplication. My preferred code formatting would also make the named operators look weird, a minor issue indeed. And there is also that boiler plate.


My work around isn’t that bad. I thought that I would lift the issue to see what resistance it would meet. I do agree that if it cannot be implementing without polluting then it might not be worth it. I thought it could maybe be a rather small implementation if the added operator was for overloading only.


Sent from Mail for Windows


From: Edward Catmur
Sent: Tuesday, 10 May 2022 16:32
To: std-proposals
Cc: Patrik Tegelberg
Subject: Re: [std-proposals] Add operator for element wise multiplication.


On Tue, 10 May 2022 at 14:13, Patrik Tegelberg via Std-Proposals <std-proposals_at_[hidden]> wrote:

I often use vector math and usually make a small vector class overloading the math operators. I always lack a good option for element wise multiplication (Hadamard product). Divide becomes element wise divide, multiplication becomes the dot product and % becomes the cross product. There are no more operators with the correct precedence to overload. I suggest making another symbol, with multiplication precedence, available for overloading.


Matlab uses .* for their elementwise operations. They also use ./ and .^ so it is an established convention. But both dot and asterisk have other meanings in C++, maybe ¤ would be pretty, if we are allowed to use it.

The symbol is not that important, the important thing is to get another operator with multiplication precedence.


Have you considered implementing the named operator idiom? https://github.com/klmr/named-operator 


Received on 2022-05-10 15:12:12