MFEM v4.9.0
Finite element discretization library
Loading...
Searching...
No Matches
symmat.hpp
Go to the documentation of this file.
1// Copyright (c) 2010-2025, Lawrence Livermore National Security, LLC. Produced
2// at the Lawrence Livermore National Laboratory. All Rights reserved. See files
3// LICENSE and NOTICE for details. LLNL-CODE-806117.
4//
5// This file is part of the MFEM library. For more information and source code
6// availability visit https://mfem.org.
7//
8// MFEM is free software; you can redistribute it and/or modify it under the
9// terms of the BSD-3 license. We welcome feedback and contributions, see file
10// CONTRIBUTING.md for details.
11
12#ifndef MFEM_SYMMETRICMAT
13#define MFEM_SYMMETRICMAT
14
15#include "../config/config.hpp"
17#include "matrix.hpp"
18
19namespace mfem
20{
21
22/// Dense symmetric matrix storing the upper triangular part. This class so far
23/// has little functionality beyond storage.
25{
26private:
27 Array<real_t> data;
28
29public:
30
31 /** Default constructor for DenseSymmetricMatrix.
32 Sets data = NULL and height = width = 0. */
34
35 /// Creates square matrix of size s.
36 explicit DenseSymmetricMatrix(int s);
37
38 /// Copy constructor: default
40
41 /// Move constructor: default
43
44 /// Construct a DenseSymmetricMatrix using an existing data array.
45 /** The DenseSymmetricMatrix does not assume ownership of the data array,
46 i.e. it will not delete the array. */
48 : Matrix(s, s) { UseExternalData(d, s); }
49
50 /// Change the data array and the size of the DenseSymmetricMatrix.
51 /** The DenseSymmetricMatrix does not assume ownership of the data array,
52 i.e. it will not delete the data array @a d. This method should not be
53 used with DenseSymmetricMatrix that owns its current data array. */
54 void UseExternalData(real_t *d, int s)
55 {
56 data.MakeRef(d, (s*(s+1))/2);
57 height = s; width = s;
58 }
59
60 /// Change the data array and the size of the DenseSymmetricMatrix.
61 /** The DenseSymmetricMatrix does not assume ownership of the data array,
62 i.e. it will not delete the new array @a d. This method will delete the
63 current data array, if owned. */
64 void Reset(real_t *d, int s)
65 { UseExternalData(d, s); }
66
67 /** Clear the data array and the dimensions of the DenseSymmetricMatrix. This
68 method should not be used with DenseSymmetricMatrix that owns its current
69 data array. */
70 void ClearExternalData() { data.LoseData(); height = width = 0; }
71
72 /// Delete the matrix data array (if owned) and reset the matrix state.
73 void Clear() { data.DeleteAll(); height = width = 0; }
74
75 /// Change the size of the DenseSymmetricMatrix to s x s.
76 void SetSize(int s);
77
78 /// Return the number of stored nonzeros in the matrix.
79 int GetStoredSize() const { return Height()*(Height()+1)/2; }
80
81 /// Returns the matrix data array.
82 inline real_t *Data() const
83 { return const_cast<real_t*>((const real_t*)data);}
84
85 /// Returns the matrix data array.
86 inline real_t *GetData() const { return Data(); }
87
88 Memory<real_t> &GetMemory() { return data.GetMemory(); }
89 const Memory<real_t> &GetMemory() const { return data.GetMemory(); }
90
91 /// Return the DenseSymmetricMatrix data (host pointer) ownership flag.
92 inline bool OwnsData() const { return data.OwnsData(); }
93
94 /// Returns reference to a_{ij}.
95 inline real_t &operator()(int i, int j);
96
97 /// Returns constant reference to a_{ij}.
98 inline const real_t &operator()(int i, int j) const;
99
100 /// Returns reference to a_{ij}.
101 real_t &Elem(int i, int j) override;
102
103 /// Returns constant reference to a_{ij}.
104 const real_t &Elem(int i, int j) const override;
105
106 /// Sets the matrix elements equal to constant c
108
110
111 /** @brief Copy assignment: default.
112 Sets the matrix size and elements equal to those of @a m. */
114
115 /// Move assignment: default
117
118 std::size_t MemoryUsage() const { return data.Capacity() * sizeof(real_t); }
119
120 /// Shortcut for mfem::Read(GetMemory(), GetStoredSize(), on_dev).
121 const real_t *Read(bool on_dev = true) const { return data.Read(on_dev); }
122
123 /// Shortcut for mfem::Read(GetMemory(), GetStoredSize(), false).
124 const real_t *HostRead() const { return data.Read(false); }
125
126 /// Shortcut for mfem::Write(GetMemory(), GetStoredSize(), on_dev).
127 real_t *Write(bool on_dev = true) { return data.Write(on_dev); }
128
129 /// Shortcut for mfem::Write(GetMemory(), GetStoredSize(), false).
130 real_t *HostWrite() { return data.Write(false); }
131
132 /// Shortcut for mfem::ReadWrite(GetMemory(), GetStoredSize(), on_dev).
133 real_t *ReadWrite(bool on_dev = true) { return data.ReadWrite(on_dev); }
134
135 /// Shortcut for mfem::ReadWrite(GetMemory(), GetStoredSize(), false).
136 real_t *HostReadWrite() { return data.ReadWrite(false); }
137
138 /// Matrix vector multiplication.
139 void Mult(const Vector &x, Vector &y) const override;
140
141 /// Returns a pointer to (an approximation) of the matrix inverse.
142 MatrixInverse *Inverse() const override;
143};
144
145// Inline methods
146
147// The number of entries stored in rows 1,...,k is
148// n + n-1 + n-2 + ... + n-k+1, where there are k terms. This equals
149// kn - sum_{i=1}^{k-1} i = kn - (k-1)k/2
150// This formula is used for the offset for each row.
152{
153 MFEM_ASSERT(data && i >= 0 && i < height && j >= 0 && j < width, "");
154 if (i > j) // reverse i and j
155 {
156 return data[(j*height) - (((j-1)*j)/2) + i - j];
157 }
158 else
159 {
160 return data[(i*height) - (((i-1)*i)/2) + j - i];
161 }
162}
163
164inline const real_t &DenseSymmetricMatrix::operator()(int i, int j) const
165{
166 MFEM_ASSERT(data && i >= 0 && i < height && j >= 0 && j < width, "");
167 if (i > j) // reverse i and j
168 {
169 return data[(j*height) - (((j-1)*j)/2) + i - j];
170 }
171 else
172 {
173 return data[(i*height) - (((i-1)*i)/2) + j - i];
174 }
175}
176
177} // namespace mfem
178
179#endif
Memory< T > & GetMemory()
Return a reference to the Memory object used by the Array.
Definition array.hpp:145
T * ReadWrite(bool on_dev=true)
Shortcut for mfem::ReadWrite(a.GetMemory(), a.Size(), on_dev).
Definition array.hpp:397
void LoseData()
NULL-ifies the data.
Definition array.hpp:160
void MakeRef(T *data_, int size_, bool own_data=false)
Make this Array a reference to a pointer.
Definition array.hpp:1053
void DeleteAll()
Delete the whole array.
Definition array.hpp:1033
T * Write(bool on_dev=true)
Shortcut for mfem::Write(a.GetMemory(), a.Size(), on_dev).
Definition array.hpp:389
bool OwnsData() const
Return true if the data will be deleted by the Array.
Definition array.hpp:154
const T * Read(bool on_dev=true) const
Shortcut for mfem::Read(a.GetMemory(), a.Size(), on_dev).
Definition array.hpp:381
int Capacity() const
Definition array.hpp:181
DenseSymmetricMatrix(real_t *d, int s)
Construct a DenseSymmetricMatrix using an existing data array.
Definition symmat.hpp:47
const Memory< real_t > & GetMemory() const
Definition symmat.hpp:89
real_t * HostWrite()
Shortcut for mfem::Write(GetMemory(), GetStoredSize(), false).
Definition symmat.hpp:130
void SetSize(int s)
Change the size of the DenseSymmetricMatrix to s x s.
Definition symmat.cpp:32
const real_t * HostRead() const
Shortcut for mfem::Read(GetMemory(), GetStoredSize(), false).
Definition symmat.hpp:124
void UseExternalData(real_t *d, int s)
Change the data array and the size of the DenseSymmetricMatrix.
Definition symmat.hpp:54
real_t * ReadWrite(bool on_dev=true)
Shortcut for mfem::ReadWrite(GetMemory(), GetStoredSize(), on_dev).
Definition symmat.hpp:133
DenseSymmetricMatrix & operator*=(real_t c)
Definition symmat.cpp:66
int GetStoredSize() const
Return the number of stored nonzeros in the matrix.
Definition symmat.hpp:79
real_t & operator()(int i, int j)
Returns reference to a_{ij}.
Definition symmat.hpp:151
real_t * HostReadWrite()
Shortcut for mfem::ReadWrite(GetMemory(), GetStoredSize(), false).
Definition symmat.hpp:136
std::size_t MemoryUsage() const
Definition symmat.hpp:118
DenseSymmetricMatrix(DenseSymmetricMatrix &&)=default
Move constructor: default.
void Clear()
Delete the matrix data array (if owned) and reset the matrix state.
Definition symmat.hpp:73
real_t * Data() const
Returns the matrix data array.
Definition symmat.hpp:82
Memory< real_t > & GetMemory()
Definition symmat.hpp:88
DenseSymmetricMatrix & operator=(const DenseSymmetricMatrix &m)=default
Copy assignment: default. Sets the matrix size and elements equal to those of m.
DenseSymmetricMatrix & operator=(DenseSymmetricMatrix &&)=default
Move assignment: default.
bool OwnsData() const
Return the DenseSymmetricMatrix data (host pointer) ownership flag.
Definition symmat.hpp:92
MatrixInverse * Inverse() const override
Returns a pointer to (an approximation) of the matrix inverse.
Definition symmat.cpp:81
real_t * Write(bool on_dev=true)
Shortcut for mfem::Write(GetMemory(), GetStoredSize(), on_dev).
Definition symmat.hpp:127
void Mult(const Vector &x, Vector &y) const override
Matrix vector multiplication.
Definition symmat.cpp:76
DenseSymmetricMatrix(const DenseSymmetricMatrix &)=default
Copy constructor: default.
const real_t * Read(bool on_dev=true) const
Shortcut for mfem::Read(GetMemory(), GetStoredSize(), on_dev).
Definition symmat.hpp:121
real_t * GetData() const
Returns the matrix data array.
Definition symmat.hpp:86
DenseSymmetricMatrix & operator=(real_t c)
Sets the matrix elements equal to constant c.
Definition symmat.cpp:46
real_t & Elem(int i, int j) override
Returns reference to a_{ij}.
Definition symmat.cpp:56
void Reset(real_t *d, int s)
Change the data array and the size of the DenseSymmetricMatrix.
Definition symmat.hpp:64
Abstract data type for matrix inverse.
Definition matrix.hpp:63
Abstract data type matrix.
Definition matrix.hpp:28
Class used by MFEM to store pointers to host and/or device memory.
int width
Dimension of the input / number of columns in the matrix.
Definition operator.hpp:28
int Height() const
Get the height (size of output) of the Operator. Synonym with NumRows().
Definition operator.hpp:66
int height
Dimension of the output / number of rows in the matrix.
Definition operator.hpp:27
Vector data type.
Definition vector.hpp:82
float real_t
Definition config.hpp:46