Boost logo

Ublas :

From: Nizar Khalifa Sallem (nksallem_at_[hidden])
Date: 2008-08-13 14:40:41


James Sutherland wrote:
>
> On Aug 13, 2008, at 11:26 AM, Nizar Khalifa Sallem wrote:
>>
>> Second, the easy dirty way is to do something like :
>> ublas::vector tmpRow = matrix_row<MatType> row( m, 1 );
>> matrix_row<MatType> row( m, 1 ) = matrix_row<MatType> row( m, 3 );
>> matrix_row<MatType> row( m, 3 ) = tmpRow;
>>
>> Finally, you may use iterators (you need to check for consistency):
>> ublas::compressed_matrix<double>::iterator1 srcRowIt, dstRowIt
>> ublas::compressed_matrix<double>::iterator2 src_ij, dst_ij, tmp_ij;
>> dstRowIt = srcRowIt = m.begin1();//iterator over row 0
>> ++srcRowIt;//here will iterate over row 1;
>> dstRowIt+=3;//iterate over row 3;
>> for (src_ij = srcRowIt.begin(), dst_ij = dstRowIt.begin();
>> src_ij != srcRowIt.end();
>> ++src_ij,++dst_ij) {
>> tmp_ij = src_ij;
>> src_ij = dst_ij;
>> dst_ij = tmp_ij;
>> // if you do something like src_ij = dst_ij; you will loose
>> your row 1 elements'
>> }
>>
>> I didn't check this code, if you want columns just invert iterator1
>> and iterator 2
>
> Thanks for the suggestions.
>
> It seems to me that the examples you provide copy column 3 to column
> 1. However, I want to *move* it. In other words, I want move
> coefficients within a given matrix_row from one column to another.
>
> I don't see how the examples you provide do this. Perhaps I am just
> confused???
>
> _______________________________________________
> ublas mailing list
> ublas_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/ublas

in the first suggestion, I did copy content of row 3 into row 1
in the second one, I move the iterator location (memory address), I
guess you understand functioning of iterators, if not you can assume
iterators as augmented pointers.
Here is a simple example using pointers:
int a = 5;
int b = 6;
int *p_a = a;
int *p_b = b;
int *tmp;
cout <<*p_a<<endl;
//returns 5
cout <<*p_b<<andl;
//returns 6
tmp = p_a;
p_a = p_b;
p_b = tmp;
cout <<*p_a<<endl;
//returns 6
cout <<*p_b<<andl;
//returns 5
In this example I didn't modify neither a nor b I just asked p_a to
point to memory address of b and p_b to memory address of a.
My second suggestion works the same but as I said you need to check
that, at least, row 1 and 3 contain same numbers of elements else you
need to work around with extra elements.

Regards,