← Back to team overview

dolfin team mailing list archive

Re: Identity matrix

 

Hi again,

I realize that assigning values blockwise is more efficient but if I try the following I get a segmentation fault (it works for one single row/column at a time though):

Matrix A;
A.init(n,n,1);
real *vals = new real[n];
int *cols = new int[n];
int *rows = new int[n];
for (int i=0; i<n; ++i)
 {
  vals[i] = 1.0;
  cols[i] = i;
  rows[i] = i;
 }
A->set(vals, cols, n, rows, n);
A->apply();

Regards, Jesper

Garth N. Wells skrev:


jesperc wrote:
Hi,

Does anyone have an example of using the ident() function in PETScMatrix. I cannot get it to work

Matrix::ident(. . . ) is not meant to do this, so it is unlikely to work.

and using a for loop with set() to
create an identity matrix is painfully slow...

This is how I do it now:

Matrix A;
A.init(n,n,1);
for (int i=0; i<n; ++i)
   A.set(i,i,1);


You shouldn't set Matrix values element-wise (Matrix::set(int,int,real) has been removed from the Matrix interface to stop users doing this). If you really want to access matrix entries element-wise, use a uBlasMatrix, otherwise set values in large blocks using

Matrix::set(const real* block, uint m, const uint* rows, uint n, const uint* cols)

Garth

and it kills me/my computer.

Kind regards, Jesper
_______________________________________________
DOLFIN-dev mailing list
DOLFIN-dev@xxxxxxxxxx
http://www.fenics.org/mailman/listinfo/dolfin-dev




Follow ups

References