So, if I want to add another row, what I should do is that:<br><br>rowindices[size]=rowindices[size-1];<br><br>rowindices[size-1]=size+1;<br>is this last line right?<br>When a CSR matrix has a line &#39;i&#39; which contains only 0s, how should I set rowindices[i]?<br>

<br>Is there any place where the number of columns is taken into account in  <br><pre><i>PetscInt* rowindices;
</i><i>PetscInt* colindices;
</i><i>PetscScalar* values;</i></pre>reading the documentation about CSR, it does not look like that.<br><br>Thank you again<br>Isaia<br><br><br><div class="gmail_quote">On Mon, Nov 28, 2011 at 11:30 PM, Nisoli Isaia <span dir="ltr">&lt;<a href="mailto:isaia.nisoli@gmail.com">isaia.nisoli@gmail.com</a>&gt;</span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><pre><div class="im">&gt;&gt;<i> int convertPETSc(srmatrix &amp;sA, Mat * const Aaddr)
</i>&gt;&gt;<i> {
</i>&gt;&gt;<i>     PetscErrorCode ierr;
</i>&gt;&gt;<i>     PetscInt size=(sA.A).size1();
</i>&gt;&gt;<i>     unsigned int totnnz=(sA.A).nnz();
</i>&gt;&gt;<i>
</i>&gt;&gt;<i>     (sA.A).complete_index1_data();
</i>&gt;&gt;<i>     long unsigned int *row_ptr =(sA.A).index1_data().begin();
</i>&gt;&gt;<i>     long unsigned int *col_ptr =(sA.A).index2_data().begin();
</i>&gt;&gt;<i>     double * value_ptr = (sA.A).value_data().begin();
</i>&gt;&gt;<i>     unsigned int sizerow_ptr=(sA.A).index1_data().size();
</i>&gt;&gt;<i>     unsigned int sizecol_ptr=(sA.A).index2_data().size();
</i>&gt;&gt;<i>     PetscInt* rowindices;
</i>&gt;&gt;<i>     PetscInt* colindices;
</i>&gt;&gt;<i>     PetscScalar* values;
</i>&gt;&gt;<i>     rowindices=new PetscInt[sizerow_ptr];
</i>&gt;&gt;<i>
</i></div>
&gt;This array is probably one too short. It needs to have length nrows+1 (so
&gt;that the length of the last row is known).
<br>I tried <br><i>rowindices=new PetscInt[sizerow_ptr+1];<br></i>allocating some more memory is not a problem...<br>but I get the same error.<div class="im"><br><br>&gt;&gt;<i>     colindices=new PetscInt[sizecol_ptr];
</i>&gt;&gt;<i>     values=new PetscScalar[totnnz];
</i>&gt;&gt;<i>     for (unsigned int i=0;i&lt;sizerow_ptr;i++)
</i>&gt;&gt;<i> rowindices[i]=PetscInt(row_ptr[i]);
</i>&gt;&gt;<i>     for (unsigned int i=0;i&lt;sizecol_ptr;i++)
</i>&gt;&gt;<i> colindices[i]=PetscInt(col_ptr[i]);
</i>&gt;&gt;<i>     for (unsigned int i=0;i&lt;totnnz;i++)
</i>&gt;&gt;<i> values[i]=PetscScalar(value_ptr[i]);
</i>&gt;&gt;<i>
</i>&gt;&gt;<i> ierr=MatCreateSeqAIJWithArrays(PETSC_COMM_WORLD,size+1,size+1,rowindices,colindices,values,Aaddr);CHKERRQ(ierr);
</i>&gt;&gt;<i>
</i></div>
&gt;You haven&#39;t explained your API, but you have a lot of &quot;size&quot; things running
&gt;around. You should probably pass in &quot;size&quot; here instead of &quot;size+1&quot;.
<br>I&#39;m sorry, I didn&#39;t say that in the first mail, but I want the function to add one last row and one last column of zeros to the matrix. <br>So that the output matrix is (size+1)*(size+1) if the input matrix is size*size.

&gt;<i>     ierr=MatAssemblyBegin(*Aaddr,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
</i>&gt;<i>     ierr=MatAssemblyEnd(*Aaddr,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
</i>&gt;<i>     return 0;
</i>&gt;<i> }
</i>&gt;</pre>
</blockquote></div><br>