← Back to team overview

dolfin team mailing list archive

Re: Matrix assignment

 

On Jun 23 2008, Anders Logg wrote:

On Mon, Jun 23, 2008 at 01:18:29PM +0200, Martin Sandve Alnæs wrote:
2008/6/23 Garth N. Wells <gnw20@xxxxxxxxx>:
>
>
> Anders Logg wrote:
>> On Sat, Jun 21, 2008 at 05:29:14AM -0400, Hatef Monajemi wrote:
>>> Hi Isn't this code supposed to work? It gives me "invalid lvalue in >>> assignment" error while compiling. How should I assign the elements >>> of a Matrix???
>>>
>>> //-------------------------------
>>> int main()
>>> {
>>>
>>>
>>> for(i=0; i<nens; i++)
>>> .
>>> .
>>> .
>>> real* values = new real[nstat];
>>> u1.interpolate(values);
>>>
>>> Matrix S(nstat,nens);
>>>
>>> for(int j=0;j<nstat;j++)
>>>   {
>>>     S(j,i)=values[j];
>>>   }
>>>
>>> }
>>
>> Sorry, this doesn't work yet.
>>
>> We can easily fix this in the GenericMatrix interface by adding an
>> auxiliary class MatrixEntry (which just holds (i, j) and a reference
>> to the matrix) and have operator() return that, then overload
>> operator= in MatrixEntry to assign to the matrix.
>>
>> We had this in DOLFIN 2-3 years back but the linear algebra has gone
>> through several reimplementations since then...
>>
>> Until this has been implemented, you can use the setitem() function.
>>
>
> I don't know if we want this because the operation is ill-suited to > some linear algebra back-ends and adding it encourages its use. If > element-wise access is needed, the uBlas matrix class should be used.
>
> Garth

I agree with Garth here. There's also no chance at all that this will
be efficient. It's just not within the scope of the dolfin linear
algebra classes.

It's very natural to be able to set values in a matrix. I'm not sure
we must always make it impossible to do operations which are not
efficient. We can easily discourage it by issuing a warning when
matrices are accessed item by item.

It's not impossible to set values - a user just needs to choose the appropriate class, which in this case is the uBlas matrix.

Garth






Follow ups

References