Giter Site home page Giter Site logo

How to copy a SparseMatrix about mfem HOT 4 CLOSED

martemyev avatar martemyev commented on May 16, 2024
How to copy a SparseMatrix

from mfem.

Comments (4)

tzanio avatar tzanio commented on May 16, 2024

Hi Mikhail,

If you add two BilinerFormIntegrators the corresponding BilinearForm will assemble their sum, see for example

https://github.com/mfem/mfem/blob/master/examples/ex3.cpp#L126

Does this address your use case?

You should be able to write

M += D

for sparse matrices M and D, as long as the sparsity patter on D is a subset of the sparsity patter of M (which very likely is true), see

https://github.com/mfem/mfem/blob/master/linalg/sparsemat.hpp#L279

Hope this helps,
Tzanio

from mfem.

martemyev avatar martemyev commented on May 16, 2024

Hi Tzanio,

If you add two BilinerFormIntegrators the corresponding BilinearForm will assemble their sum

I think this is indeed possible in my case, but I thought that since I already computed M and D as separate matrices there should be any easy way to add them up, and creating a new bilinear form didn't look as the one.

You should be able to write M += D

I can't because in this case I change M, but I need the original one for the right hand side (the same is true for D).

for sparse matrices M and D, as long as the sparsity patter on D is a subset of the sparsity patter of M

I think I can add them up even if the patterns are completely different, but the height and width are the same, and both are not finalized. Please, let me know if I'm wrong.

As for the copying sparse matrices (just to close the topic) - there is no way to do that as far as I understood. Correct?

Best regards,
Mikhail

from mfem.

v-dobrev avatar v-dobrev commented on May 16, 2024

Hi Mikhail,

Copying a SparseMatrix can be achieved with something like this:

// Assume we have a SparseMatrix A that is finalized, i.e. in csr format.
int nnz = A.NumNonZeroElems();
SparseMatrix B(A.GetI(), A.GetJ(), new double[nnz], A.Height(), A.Width(),
               false, true, A.areColumnsSorted());
B = 0.0;
B += A;

The new SparseMatrix B has the same sparsity pattern as A without duplicating the "I" and "J" arrays of the csr format, only the "data" double array is different and it will be owned by the newly created SparseMatrix B, i.e. B will delete its "data" array when it is destroyed.

An actual copy constructor for SparseMatrix will be available in the next release of MFEM.

Veselin

from mfem.

martemyev avatar martemyev commented on May 16, 2024

Hi Veselin,

That looks like a solution! Thank you!

Best,
Mikhail

from mfem.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.