 # Glas :

## Re: [glas] arithmetic operations

From: Guntram Berti (berti_at_[hidden])
Date: 2005-11-04 07:48:28

On Fri, Oct 28, 2005 at 08:14:46AM -0500, Andrew Lumsdaine wrote:
>
> On Oct 28, 2005, at 2:23 AM, Karl Meerbergen wrote:

> > To make the choice easier, we could assume that a vector is a column
> > (as
> > is always the case in linear algebra). In this case
> > trans(x)*y = dot product
> > x*trans(y) = outer product
> > herm(x)*y = hermitian inner product
> >
> > This is probably the closest we can get to linear algebra notation.
>
> Right. I think these all make perfect sense mathematically. I wasn't
> suggesting to leave out the use of * altogether, but rather to use it
> only in the ways that make sense mathematically. And to have it always
> mean the same thing.
>

Another point to note is that if we use * on unqualified vectors like u*v
for the dot (inner) product, we loose associativity:
(u*v)*w != u*(v*w)
(if we assume * also is used for the multiplication with a scalar).
Therefore I personally never use * for the dot product.

So if we use operators like *, we must make sure (in addition to corresponding
to mathemtical convention) that its use is 100% unambigous and can be deduced from the types.
So Karl's suggestion, using trans(x)*y etc. seems to make sense,
because then any multiplication involving non-scalar quantities
is formally a matrix multiplication, and errors can be detected easily (at least at
run time).

If we want an operator for element-wise multiplication, we must then
use something like elem(v) * elem(w) (to keep with Karl's notation)
or elemwise_mul(v,w), as has been suggested before.

Concerning the dot product: If we overload the comma operator,
we can probably use the notation (v,w) common in mathemetics ;-)
(just kidding ...)

Another remark/idea concerning the inner product (also concerning norms):
Sometimes it may be useful to make this a parameter of an algorithm,
for instance, using different norms to compute stopping criteria,
or orthonormalization using different inner products.
This is perhaps an argument for preferring inner_prod(v,w)
over a notation using operator *, because I feel that it is easier
to influence the former using template parameters.
Perhaps something like space::inner_product(v,w) ...

--guntram