|Re: [eigen] Commenting issue #110, disabling operator for matrices|
[ Thread Index |
| More lists.tuxfamily.org/eigen Archives
- To: eigen@xxxxxxxxxxxxxxxxxxx
- Subject: Re: [eigen] Commenting issue #110, disabling operator for matrices
- From: Benoit Jacob <jacob.benoit.1@xxxxxxxxx>
- Date: Thu, 22 Apr 2010 08:50:46 -0400
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:received:message-id:subject:from:to:content-type :content-transfer-encoding; bh=j97Z57sxy4Lap7yezqyqGM1BY4t6Ekxl0HDTwkLy5ps=; b=bN3No+gRwMw04m03RzVINgBuujls7/1xEaBD/jR/XmdT0i3TRCMw4bAjcN1rqsSz95 2fNOab/5a1+T62dxNwt73Bcb3UIhlxT7mKsdZuO0Lz/cQSw/ueLaxH4lO0DRMZq1DjZ8 X1FysOeeaalUuVCUAxUXYi78Wv4ZLSBKWl1zc=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; b=hX/wrVzM4RcmT1+2KxMZMl8Q4P57WRNiDOoPQRrncfJkpJaPzirq0JLXQ3BJmJO/MC edvGwF7jfyPr4RFKh7UEHQEbCiPHUjZ8Gftvg3vQlFTbpXHQqpn9BBdt9OLDoqs0g/3s CagLzIIoGllHEutNAynOZY4RMhS+cIkpRTlE0=
There is a real, very important use case for linear index-based access
of matrices, i.e. matrix(index).
Take an operation like:
matrix3 = matrix1 + matrix2;
Naively one can implement it like
matrix3(i,j) = matrix1(i,j) + matrix2(i,j);
But one can do much better:
matrix3(i) = matrix1(i) + matrix2(i);
This is much better because:
1) it's just faster (if only because it avoids nontrivial offset computations)
2) it's even faster as it can vectorize a larger part of the
computation, since we don't have to deal with unaligned boundaries in
every column (or row).
3) it generates smaller binary code
Eigen is able to understand automatically when an operation like that
can be performed using such a linear index-based traversal. The
accessor it uses internally is the coeff(int index) method.
So in any case, we keep coeff(int index).
Then, it would be slightly (no big deal) inconsistent to remove
operator()(int index) (the difference being that operator()() does
assertions, while coeff() doesn't). If we allow the unsafe coeff()
version, why not allow the safe operator()() version? And the fact
that we are using it shows that there is a use case.
Another argument is that "the user mistakenly believes the matrix to
be a vector" is a rather uncommon kind of mistake, no?
2010/4/22 Márton Danóczy <marton78@xxxxxxxxx>:
> In my opinion matrix(i) should be disallowed as well, I'd guess in
> most of the cases matrix(i) means that the user mistakenly believes
> the matrix to be a vector. I'd go for the more verbose but
> self-documenting .asVector(i).
> Just my two cents.
> On 22 April 2010 14:34, Benoit Jacob <jacob.benoit.1@xxxxxxxxx> wrote:
>> here we are discussing disabling operator on matrices.
>> We can perfectly disable it on matrices while keeping it on vectors
>> (e.g. with a static assertion).
>> The motivation for disabling it on matrices is to guard against this
>> dreadful c++ code:
>> which in c++ compiles as
>> which addresses the i-th entry in the storage order of the matrix.
>> If you want that functionality, there still is the synonym
>> And the method coeff(int) if you want to avoid the assertion.
>> People have already proposed adding an expression to view a matrix as
>> a vector, following its storage order. But you can already do that
>> with Map. If you really want, we can add a conveniene method
>> asVector() to return such a Map object. But given the possibility of
>> doing matrix(index), i don't see too much motivation for that...