# Question about dolfin.cpp.la.Matrix

**URL:** <https://fenicsproject.discourse.group/t/question-about-dolfin-cpp-la-matrix/13687>\
**Category:** Legacy DOLFIN\
**Tags:** linear-algebra\
**Created:** [February 23, 2024, 7:26am UTC](https://fenicsproject.discourse.group/t/question-about-dolfin-cpp-la-matrix/13687 "2024-02-23T07:26:22Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![xiaohe](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/xiaohe/32/7777_2.png) [@xiaohe](https://fenicsproject.discourse.group/u/xiaohe)\
**Post date:** [February 23, 2024, 7:26am UTC](https://fenicsproject.discourse.group/t/question-about-dolfin-cpp-la-matrix/13687/1 "2024-02-23T07:26:22Z")

</div>

Hello, everyone  
I am working on fenics 2019.1, I have some question about the class **dolfin.cpp.la.Matrix**

1. Whether fenics supports basic matrix operations.(for example add,subract, multiply, divide, inv…)
2. Whether fenics supports advanced matrix operations.For example, Creating a specified size fenics matrix and updating the value of the element. Calculating the fenics matrix eigenvalues and eigenvectors…
3. If fenics can not complete some matrix operations. Maybe i need extra package(numpy and …). I can convert fenics matrix to numpy array using **M01.array()**, but this approach will fail when the size of matrix is very large (error: memory leak). Is there any package to recommend?

Thank you to anyone for your recovery!

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [February 23, 2024, 10:52am UTC](https://fenicsproject.discourse.group/t/question-about-dolfin-cpp-la-matrix/13687/2 "2024-02-23T10:52:05Z")

</div>

See: [Bitbucket](https://bitbucket.org/fenics-project/dolfin/src/1c52e837eb54cc34627f90bde254be4aa8a2ae17/python/src/la.cpp#lines-255) for all functions associated with a matrix.

Note that if you use `as\_backend\_type(A).mat() you most likely get a petsc4py matrix and can use all their operations

---

<div class="post-metadata">

**Author:** ![xiaohe](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/xiaohe/32/7777_2.png) [@xiaohe](https://fenicsproject.discourse.group/u/xiaohe)\
**Post date:** [February 24, 2024, 6:54am UTC](https://fenicsproject.discourse.group/t/question-about-dolfin-cpp-la-matrix/13687/3 "2024-02-24T06:54:58Z")

</div>

Thank you for your reply！  
I’m already learning PETSC basic the fenics 2019.1, but i am getting an error as follows:

```auto
import sys
import petsc4py
from fenics import *

petsc4py.init(sys.argv)
from petsc4py import PETSc

A = PETSc.Mat()
A.create(comm=PETSc.COMM_WORLD)
A.setSizes((5, 5))

A.setType(PETSc.Mat.Type.AIJ)

A[1, 1] = 999

A.assemblyBegin()
A.assemblyEnd()

print(A.getValues(1, 1))

error:
[0]PETSC ERROR: ------------------------------------------------------------------------
[0]PETSC ERROR: Caught signal number 11 SEGV: Segmentation Violation, probably memory access out of range
[0]PETSC ERROR: Try option -start_in_debugger or -on_error_attach_debugger
[0]PETSC ERROR: or see https://www.mcs.anl.gov/petsc/documentation/faq.html#valgrind
[0]PETSC ERROR: or try http://valgrind.org on GNU/linux and Apple Mac OS X to find memory corruption errors
[0]PETSC ERROR: configure using --with-debugging=yes, recompile, link, and run 
[0]PETSC ERROR: to get more information on the crash.
application called MPI_Abort(MPI_COMM_WORLD, 59) - process 0
[unset]: write_line error; fd=-1 buf=:cmd=abort exitcode=59
:
system msg for write_line failure : Bad file descriptor
Error in system call pthread_mutex_destroy: Device or resource busy
    src/mpi/init/initthread.c:284

```

When I run the same code in another python environment(no fenics), there is no error.

---

<div class="post-metadata">

**Author:** ![francesco-ballarin](https://avatars.discourse-cdn.com/v4/letter/f/f05b48/32.png) [@francesco-ballarin](https://fenicsproject.discourse.group/u/francesco-ballarin)\
**Post date:** [February 24, 2024, 7:44am UTC](https://fenicsproject.discourse.group/t/question-about-dolfin-cpp-la-matrix/13687/4 "2024-02-24T07:44:15Z")

</div>

Your code is running correctly on my installation, and prints `999.0`.

The issue is likely in your installation. Without any indication on how you installed dolfin it’s impossible to be of further help.

---

<div class="post-metadata">

**Author:** ![xiaohe](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/xiaohe/32/7777_2.png) [@xiaohe](https://fenicsproject.discourse.group/u/xiaohe)\
**Post date:** [February 24, 2024, 8:19am UTC](https://fenicsproject.discourse.group/t/question-about-dolfin-cpp-la-matrix/13687/5 "2024-02-24T08:19:10Z")

</div>

Thank you for reply!  
I installed miniconda under linux system, then install fenics as follows

```auto
conda create -n fenicsproject -c conda-forge fenics

```

---

<div class="post-metadata">

**Author:** ![xiaohe](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/xiaohe/32/7777_2.png) [@xiaohe](https://fenicsproject.discourse.group/u/xiaohe)\
**Post date:** [February 24, 2024, 8:23am UTC](https://fenicsproject.discourse.group/t/question-about-dolfin-cpp-la-matrix/13687/6 "2024-02-24T08:23:54Z")

</div>

This is the version information  
dolfin. **version** 2019.1.0

 ![6696c62c843853b4e8e109d8dfc1d71](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/2X/8/86807c11c6a22100247d5ad5b30f8805185f0686.png)

---

<div class="post-metadata">

**Author:** ![francesco-ballarin](https://avatars.discourse-cdn.com/v4/letter/f/f05b48/32.png) [@francesco-ballarin](https://fenicsproject.discourse.group/u/francesco-ballarin)\
**Post date:** [February 24, 2024, 10:09am UTC](https://fenicsproject.discourse.group/t/question-about-dolfin-cpp-la-matrix/13687/7 "2024-02-24T10:09:42Z")

</div>

Did you add other libraries after creating the conda environment? If so, please create a further one and keep it clean of anything else, and confirm if the code works or not.

---

<div class="post-metadata">

**Author:** ![xiaohe](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/xiaohe/32/7777_2.png) [@xiaohe](https://fenicsproject.discourse.group/u/xiaohe)\
**Post date:** [February 24, 2024, 11:18am UTC](https://fenicsproject.discourse.group/t/question-about-dolfin-cpp-la-matrix/13687/8 "2024-02-24T11:18:32Z")

</div>

Thank you for your reply!  
The code can work after i installed feics in a new conda environment. The above error may be caused by me installing some extra python package. Now I wonder what I should pay attention to when I install the extra package (eg gmsh, meshio, and etc …)

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [February 24, 2024, 12:03pm UTC](https://fenicsproject.discourse.group/t/question-about-dolfin-cpp-la-matrix/13687/9 "2024-02-24T12:03:02Z")

</div>

If you use conda, i would highly recommend to install all other required packages with conda. Mixing pip and conda can cause tons of issues
