# Matrix set block

Hello,

i've a big Matrix and want to set a block of values using
http://fenicsproject.org/documentation/dolfin/1.0.beta/cpp/programmers-reference/la/Matrix.html

this function
void set(const double* block, uint m, const uint* rows, uint n, const uint* cols)

setrow etc. slows down by big size of matrix, so i wanted to try this function.

But i dont know how to define the block correct. I always get an memmory allocation.
Because of the structur "double * block" i first thought to allocate it like double block[size]....

some other trys failed, too.

Does anybody has a clue?
Would be great.
Best regards

Paul

## Question information

Language:
English Edit question
Status:
Solved
For:
FEniCS Project Edit question
Assignee:
No assignee Edit question
Solved by:
Anders Logg
Solved:
Last query:
 Revision history for this message Anders Logg (logg) said on 2013-02-12: #1

On Tue, Feb 12, 2013 at 04:11:15PM -0000, Paul Wimmer wrote:
> New question #221689 on FEniCS Project:
> i've a big Matrix and want to set a block of values using
> http://fenicsproject.org/documentation/dolfin/1.0.beta/cpp/programmers-reference/la/Matrix.html
> this function
> void set(const double* block, uint m, const uint* rows, uint n, const uint* cols)
> setrow etc. slows down by big size of matrix, so i wanted to try this function.
> But i dont know how to define the block correct. I always get an memmory allocation.
> Because of the structur "double * block" i first thought to allocate it like double block[size]....
> some other trys failed, too.
> Does anybody has a clue?
> Would be great.
> Best regards
> Paul

Quick recipe for inserting a unit block matrix at some random rows:

double block[9];
uint rows[3];
//uint cols[3]; same as rows here

block[0] = 1;
block[1] = 0;
block[2] = 0;
block[3] = 0;
block[4] = 1;
block[5] = 0;
block[6] = 0;
block[7] = 0;
block[8] = 1;

rows[0] = 100;
rows[1] = 200;
rows[2] = 300;

A.set(block, 3, rows, 3, rows);

--
Anders

 Revision history for this message Garth Wells (garth-wells) said on 2013-02-13: #2

On 12 February 2013 22:25, Anders Logg
> Question #221689 on FEniCS Project changed:
>
>
> Anders Logg proposed the following answer:
> On Tue, Feb 12, 2013 at 04:11:15PM -0000, Paul Wimmer wrote:
>> New question #221689 on FEniCS Project:
>> i've a big Matrix and want to set a block of values using
>> http://fenicsproject.org/documentation/dolfin/1.0.beta/cpp/programmers-reference/la/Matrix.html
>> this function
>> void set(const double* block, uint m, const uint* rows, uint n, const uint* cols)
>> setrow etc. slows down by big size of matrix, so i wanted to try this function.
>> But i dont know how to define the block correct. I always get an memmory allocation.
>> Because of the structur "double * block" i first thought to allocate it like double block[size]....
>> some other trys failed, too.
>> Does anybody has a clue?
>> Would be great.
>> Best regards
>> Paul
>
> Quick recipe for inserting a unit block matrix at some random rows:
>
> double block[9];
> uint rows[3];

It should be

dolfin::la_index rows[3];

Garth

> //uint cols[3]; same as rows here
>
> block[0] = 1;
> block[1] = 0;
> block[2] = 0;
> block[3] = 0;
> block[4] = 1;
> block[5] = 0;
> block[6] = 0;
> block[7] = 0;
> block[8] = 1;
>
> rows[0] = 100;
> rows[1] = 200;
> rows[2] = 300;
>
> A.set(block, 3, rows, 3, rows);
>
>
> --
> Anders
>
> --
> You received this question notification because you are a member of
> FEniCS Team, which is an answer contact for FEniCS Project.

 Revision history for this message Paul Wimmer (wimmerpauli) said on 2013-02-13: #3

So i was sure, that the error wasn't by setting/getting matrix entrys.

I found the causing segment violation error somewhere else...

best regards Paul

 Revision history for this message Paul Wimmer (wimmerpauli) said on 2013-02-13: #4

Thanks Anders Logg, that solved my question.

 Revision history for this message Anders Logg (logg) said on 2013-02-13: #5

On Wed, Feb 13, 2013 at 08:11:17AM -0000, Garth Wells wrote:
> Question #221689 on FEniCS Project changed:
> Garth Wells proposed the following answer:
> On 12 February 2013 22:25, Anders Logg

>> Question #221689 on FEniCS Project changed:
>>
>>
>> Anders Logg proposed the following answer:
>>
>> Quick recipe for inserting a unit block matrix at some random rows:
>>
>> double block[9];
>> uint rows[3];
> It should be
> dolfin::la_index rows[3];
> Garth

Not here, the function signature posted in the question indicates that
1.0 is being used.

--
Anders

>> //uint cols[3]; same as rows here
>>
>> block[0] = 1;
>> block[1] = 0;
>> block[2] = 0;
>> block[3] = 0;
>> block[4] = 1;
>> block[5] = 0;
>> block[6] = 0;
>> block[7] = 0;
>> block[8] = 1;
>>
>> rows[0] = 100;
>> rows[1] = 200;
>> rows[2] = 300;
>>
>> A.set(block, 3, rows, 3, rows);
>>