Set entries of a Dolfin matrix object

Asked by Alejandro Ortiz-Bernardin

Hello everyone,

I just started to explore Dolfin capabilities. I am reading the Fenics book to this end. I am really impressed about Fenics project in general. For now, I would like to use the solver function, which looks quite simple. For this a matrix object is needed. I am interested in the C++ version. Say, the following is the matrix:

A= [ 1 4 7 4;
         4 2 6 8;
         7 6 4 2;
         4 8 2 6]

How can I set all these entries in a matrix object? Can somebody provide a small C++ code example for this?

Also, it seems Dolfin only works with sparse matrices. Is there any way to work with a dense matrix like A previously defined?

I would appreciate your reply.

Thanks,
Alejandro.

Question information

Language:
English Edit question
Status:
Solved
For:
DOLFIN Edit question
Assignee:
No assignee Edit question
Solved by:
Johan Hake
Solved:
Last query:
Last reply:
Revision history for this message
Johan Hake (johan-hake) said :
#1

DOLFIN uses different LA backends. Because not all of then support
initialization like A(m,n), we have dropped support for this at the
GenericMatrix level.

To initialize a general matrix with A(m,n) you need to look at the
different LA backends. And for a dense matrix have look at uBLASDenseMatrix

Johan

On 02/11/2013 02:20 AM, Alejandro Ortiz-Bernardin wrote:
> New question #221558 on DOLFIN:
> https://answers.launchpad.net/dolfin/+question/221558
>
> Hello everyone,
>
> I just started to explore Dolfin capabilities. I am reading the Fenics book to this end. I am really impressed about Fenics project in general. For now, I would like to use the solver function, which looks quite simple. For this a matrix object is needed. I am interested in the C++ version. Say, the following is the matrix:
>
> A= [ 1 4 7 4;
> 4 2 6 8;
> 7 6 4 2;
> 4 8 2 6]
>
> How can I set all these entries in a matrix object? Can somebody provide a small C++ code example for this?
>
> Also, it seems Dolfin only works with sparse matrices. Is there any way to work with a dense matrix like A previously defined?
>
> I would appreciate your reply.
>
> Thanks,
> Alejandro.
>
>
>

Revision history for this message
Alejandro Ortiz-Bernardin (aortizb) said :
#2

Hi Johan,

Thanks for the answer. However, I still have a doubt. As stated in the Fenics book, If one declares

Matrix A;

How do you insert the entries? I was looking at the website and found the Matrix class to have a function called "set". It seems to me that this function can be used for inserting the entries. However, I don't understand how to use "block" argument and "la_index" argument. Could you explain on that?

Thanks,
Alejandro.

Revision history for this message
Anders Logg (logg) said :
#3

On Mon, Feb 11, 2013 at 11:35:52AM -0000, Alejandro Ortiz-Bernardin wrote:
> Question #221558 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/221558
> Status: Answered => Open
> Alejandro Ortiz-Bernardin is still having a problem:
> Hi Johan,
> Thanks for the answer. However, I still have a doubt. As stated in the
> Fenics book, If one declares
> Matrix A;
> How do you insert the entries? I was looking at the website and found
> the Matrix class to have a function called "set". It seems to me that
> this function can be used for inserting the entries. However, I don't
> understand how to use "block" argument and "la_index" argument. Could
> you explain on that?

Setting matrix entries is a low-level operation for sparse matrices in
DOLFIN. You need to (1) compute the sparsity pattern, (2) initialize
the matrix with that sparsity pattern, then use (3) A.add to set
blocks of values. The last operation works similarly to MatSetValues
in PETSc (paste that into Google to get more information).

Use a dense matrix to be able to set entries. Otherwise, rely on the
DOLFIN assembler functions to assemble matrices from variational forms.

--
Anders

Revision history for this message
Alejandro Ortiz-Bernardin (aortizb) said :
#4

Thanks for the answer. I am not getting this. I will be more clear:

This is the matrix

A= [ 1 4 7 4;
         4 2 6 8;
         7 6 4 2;
         4 8 2 6]

I want to do: Matrix A;

How do I fill A using the Matrix class such that all the entries are those indicated above?

Thank you.

Alejandro.

Revision history for this message
Best Johan Hake (johan-hake) said :
#5

Short answer:

  You cannot.

Long answer:

  re-read the answers from me and Logg.

That said, some of the la-backends do support this. Have alook at the
uBLASDenseMatrix.

Johan

On 02/11/2013 02:50 PM, Alejandro Ortiz-Bernardin wrote:
> Question #221558 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/221558
>
> Status: Answered => Open
>
> Alejandro Ortiz-Bernardin is still having a problem:
> Thanks for the answer. I am not getting this. I will be more clear:
>
> This is the matrix
>
> A= [ 1 4 7 4;
> 4 2 6 8;
> 7 6 4 2;
> 4 8 2 6]
>
> I want to do: Matrix A;
>
> How do I fill A using the Matrix class such that all the entries are
> those indicated above?
>
> Thank you.
>
> Alejandro.
>

Revision history for this message
Alejandro Ortiz-Bernardin (aortizb) said :
#8

Thanks Johan Hake, that solved my question.