1
// Copyright (C) 2006-2008 Anders Logg and Garth N. Wells.
2
// Licensed under the GNU LGPL Version 2.1.
4
// Modified by Ola Skavhaug, 2007-2008.
5
// Modified by Kent-Andre Mardal, 2008.
6
// Modified by Martin Sandve Alnes, 2008.
8
// First added: 2006-05-15
9
// Last changed: 2008-05-17
15
#include <dolfin/common/Variable.h>
16
#include "DefaultFactory.h"
17
#include "GenericMatrix.h"
22
/// This class provides the default DOLFIN matrix class,
23
/// based on the default DOLFIN linear algebra backend.
25
class Matrix : public GenericMatrix, public Variable
29
/// Create empty matrix
30
Matrix() : Variable("A", "DOLFIN matrix"), matrix(0)
31
{ DefaultFactory factory; matrix = factory.create_matrix(); }
33
/// Create M x N matrix
34
Matrix(uint M, uint N) : Variable("A", "DOLFIN matrix"), matrix(0)
35
{ DefaultFactory factory; matrix = factory.create_matrix(); matrix->resize(M, N); }
38
explicit Matrix(const Matrix& A) : Variable("A", "DOLFIN matrix"),
39
matrix(A.matrix->copy())
46
//--- Implementation of the GenericTensor interface ---
48
/// Initialize zero tensor using sparsity pattern
49
virtual void init(const GenericSparsityPattern& sparsity_pattern)
50
{ matrix->init(sparsity_pattern); }
52
/// Return copy of tensor
53
virtual Matrix* copy() const
54
{ Matrix* A = new Matrix(); delete A->matrix; A->matrix = matrix->copy(); return A; }
56
/// Return size of given dimension
57
virtual uint size(uint dim) const
58
{ return matrix->size(dim); }
60
/// Set all entries to zero and keep any sparse structure
64
/// Finalize assembly of tensor
69
virtual void disp(uint precision=2) const
70
{ matrix->disp(precision); }
72
//--- Implementation of the GenericMatrix interface ---
74
/// Resize matrix to M x N
75
virtual void resize(uint M, uint N)
76
{ matrix->resize(M, N); }
78
/// Get block of values
79
virtual void get(double* block, uint m, const uint* rows, uint n, const uint* cols) const
80
{ matrix->get(block, m, rows, n, cols); }
82
/// Set block of values
83
virtual void set(const double* block, uint m, const uint* rows, uint n, const uint* cols)
84
{ matrix->set(block, m, rows, n, cols); }
86
/// Add block of values
87
virtual void add(const double* block, uint m, const uint* rows, uint n, const uint* cols)
88
{ matrix->add(block, m, rows, n, cols); }
90
/// Add multiple of given matrix (AXPY operation)
91
virtual void axpy(double a, const GenericMatrix& A, bool same_nonzero_pattern = false)
92
{ matrix->axpy(a, A, same_nonzero_pattern); }
94
/// Get non-zero values of given row
95
virtual void getrow(uint row, std::vector<uint>& columns, std::vector<double>& values) const
96
{ matrix->getrow(row, columns, values); }
98
/// Set values for given row
99
virtual void setrow(uint row, const std::vector<uint>& columns, const std::vector<double>& values)
100
{ matrix->setrow(row, columns, values); }
102
/// Set given rows to zero
103
virtual void zero(uint m, const uint* rows)
104
{ matrix->zero(m, rows); }
106
/// Set given rows to identity matrix
107
virtual void ident(uint m, const uint* rows)
108
{ matrix->ident(m, rows); }
110
// Matrix-vector product, y = Ax
111
virtual void mult(const GenericVector& x, GenericVector& y, bool transposed=false) const
112
{ matrix->mult(x, y, transposed); }
114
/// Multiply matrix by given number
115
virtual const Matrix& operator*= (double a)
116
{ *matrix *= a; return *this; }
118
/// Divide matrix by given number
119
virtual const Matrix& operator/= (double a)
120
{ *matrix /= a; return *this; }
122
/// Assignment operator
123
virtual const GenericMatrix& operator= (const GenericMatrix& A)
124
{ *matrix = A; return *this; }
126
/// Return pointers to underlying compressed storage data.
127
/// See GenericMatrix for documentation.
128
virtual std::tr1::tuple<const std::size_t*, const std::size_t*, const double*, int> data() const
129
{ return matrix->data(); }
131
//--- Special functions ---
133
/// Return linear algebra backend factory
134
virtual LinearAlgebraFactory& factory() const
135
{ return matrix->factory(); }
137
//--- Special functions, intended for library use only ---
139
/// Return concrete instance / unwrap (const version)
140
virtual const GenericMatrix* instance() const
143
/// Return concrete instance / unwrap (non-const version)
144
virtual GenericMatrix* instance()
147
//--- Special Matrix functions ---
149
/// Assignment operator
150
const Matrix& operator= (const Matrix& A)
151
{ *matrix = *A.matrix; return *this; }
155
// Pointer to concrete implementation
156
GenericMatrix* matrix;