dolfin team mailing list archive
-
dolfin team
-
Mailing list archive
-
Message #18572
Re: Operator overloading for Vector
Florian!
I cannot really follow your implementation of the binary addition operator.
See
http://www.cs.caltech.edu/courses/cs11/material/cpp/donnie/cpp-ops.html
For an extensive discussion of the subject.
But yes, the binary linear algebra operators were not implemented in C++
because of of performance issues. "Forcing" the user to use the compound
assignment operators (+=, -+, *=) instead.
Another design decision that also kicks in here is to keep it simple. We have
many different Vector and Matrix classes, which intentionally should have
minimalistic interface.
Occasionally we have discussed adding them, as they are there in the Python
interface, but landed on not including them.
Johan
On Thursday June 17 2010 11:23:50 Florian Rathgeber wrote:
> Hi,
>
> I was wondering why +=, *=, etc. are overloaded in the Vector class, but
> not addition of Vectors and multiplication of Vectors by scalars.
>
> Hence I tried the following:
>
> namespace dolfin
> {
> Vector operator*( double a, const Vector& v )
> {
> Vector res( v );
> res *= a;
> return res;
> }
>
> Vector operator*( const Vector& v, double a )
> {
> Vector res( v );
> res *= a;
> return res;
> }
>
> Vector operator+( const Vector& v1, const Vector& v2 )
> {
> Vector res( v1 );
> res += v2;
> return res;
> }
> }
>
> but realized the construction of the return object is not possible since
> the copy constructor of Vector is explicit.
>
> Is that a design decision that this is not supposed to be possible? I
> realize that creating a temporary Vector can be quite expensive.
>
> Florian
>
> _______________________________________________
> Mailing list: https://launchpad.net/~dolfin
> Post to : dolfin@xxxxxxxxxxxxxxxxxxx
> Unsubscribe : https://launchpad.net/~dolfin
> More help : https://help.launchpad.net/ListHelp
References