[petsc-users] how to use MatSetValues?

Gianluca Meneghello gianmail at gmail.com
Tue Mar 8 03:23:34 CST 2011


You should do a loop over the values

for (int i = 0; i < 5; i++)
MatSetValues(xx,1,row[i],1,col[i],val[i],INSERT_VALUES);


On 8 March 2011 10:16, Tomasz Jankowski <tomjan at jay.au.poznan.pl> wrote:
>> I think m and n are the number of rows and columns you are inserting
>> in the matrix. If you insert one value at a time, that would be
>>
>> MatSetValues(xx,1,row,1,col,val,INSERT_VALUES);
>>
>> I hope it helps
>>
>> Gianluca
>
> hello Gianluca, thanks for re.
>
> your sugestion doesn't work.
>
> my code
>
>  Mat xx;
>  PetscInitialize(&argc,&argv,(char *)0,help);
>
>  MatCreateSeqAIJ(PETSC_COMM_SELF,5,5,PETSC_DECIDE,PETSC_NULL,&xx);
>
>  PetscInt row [] ={0, 1, 2, 3, 4};
>  PetscInt col [] = {1, 0, 4, 2, 3};
>  PetscScalar val[] = {1.1,2.2,3.3,4.4,5.5};
>
>  MatSetValues(xx,5,row,5,col,val,INSERT_VALUES);
>
>  MatAssemblyBegin(xx,MAT_FINAL_ASSEMBLY);
>  MatAssemblyEnd(xx,MAT_FINAL_ASSEMBLY);
>
>  MatView(xx,PETSC_VIEWER_STDOUT_WORLD);
>  PetscFinalize();
>
> and here few examples of results
>
>  MatSetValues(xx,1,row,1,col,val,INSERT_VALUES);
>
> row 0: (1, 1.1)
> row 1:
> row 2:
> row 3:
> row 4:
>
>
>  MatSetValues(xx,5,row,1,col,val,INSERT_VALUES);
>
> row 0: (1, 1.1)
> row 1: (1, 2.2)
> row 2: (1, 3.3)
> row 3: (1, 4.4)
> row 4: (1, 5.5)
>
>  MatSetValues(xx,5,row,5,col,val,INSERT_VALUES);
>
> row 0: (0, 2.2)  (1, 1.1)  (2, 4.4)  (3, 5.5)  (4, 3.3)
> row 1: (0, 4.94066e-324)  (1, 2.07411e-317)  (2, 1.4822e-323)  (3,
> 2.07356e-317)  (4, 4.24399e-314)
> row 2: (0, 6.36599e-314)  (1, 2.122e-314)  (2, 2.07402e-317)  (3,
> 1.5957e-316)  (4, 1.18832e-312)
> row 3: (0, 6.95332e-310)  (1, 2.07367e-317)  (2, 0)  (3, 1.20407e-312)  (4,
> 0)
> row 4: (0, 6.95332e-310)  (1, 0)  (2, 2.07378e-317)  (3, 0)  (4, 2.122e-314)
>
> tom
>
> ########################################################
> #               tomjan at jay.au.poznan.pl                #
> #              jay.au.poznan.pl/~tomjan/               #
> ########################################################
>
> On Tue, 8 Mar 2011, Gianluca Meneghello wrote:
>
>> I think m and n are the number of rows and columns you are inserting
>> in the matrix. If you insert one value at a time, that would be
>>
>> MatSetValues(xx,1,row,1,col,val,INSERT_VALUES);
>>
>> I hope it helps
>>
>> Gianluca
>>
>> On 8 March 2011 09:39, Tomasz Jankowski <tomjan at jay.au.poznan.pl> wrote:
>>>
>>> hello,
>>>
>>> Could someone give me a hand with MatSetValues function?
>>>
>>> suppose such example sparse matrix
>>>
>>> 0   1.1 0   0   0
>>> 2.2 0   0   0   0
>>> 0   0   0   0   3.3
>>> 0   0   4.4 0   0
>>> 0   0   0   5.5 0
>>>
>>> after storing it in row, col and val arrays we have
>>>
>>> row[0] = 0;col[0]=1;val[0]=1.1;
>>> row[1] = 1;col[1]=0;val[1]=2.2;
>>> row[2] = 2;col[2]=4;val[2]=3.3;
>>> row[3] = 3;col[3]=2;val[3]=4.4;
>>> row[3] = 4;col[3]=3;val[4]=5.5;
>>>
>>> question is: how to use MatSetValues with it?
>>>
>>> I thought that 'MatSetValues(xx,5,row,1,col,val,INSERT_VALUES);' would be
>>> good but it doesn't .(I suppose I don't fully anderstand how it works...)
>>>
>>> thanks,
>>>
>>> tom
>>>
>>> ########################################################
>>> #               tomjan at jay.au.poznan.pl                #
>>> #              jay.au.poznan.pl/~tomjan/               #
>>> ########################################################
>>>
>>
>>
>>
>> --
>> "[Je pense que] l'homme est un monde qui vaut des fois les mondes et
>> que les plus ardentes ambitions sont celles qui ont eu l'orgueil de
>> l'Anonymat" -- Non omnibus, sed mihi et tibi
>> Amedeo Modigliani
>



-- 
"[Je pense que] l'homme est un monde qui vaut des fois les mondes et
que les plus ardentes ambitions sont celles qui ont eu l'orgueil de
l'Anonymat" -- Non omnibus, sed mihi et tibi
Amedeo Modigliani


More information about the petsc-users mailing list