~ubuntu-branches/ubuntu/maverick/dolfin/maverick

« back to all changes in this revision

Viewing changes to dolfin/la/Matrix.h

  • Committer: Bazaar Package Importer
  • Author(s): Johannes Ring
  • Date: 2008-09-16 08:41:20 UTC
  • Revision ID: james.westby@ubuntu.com-20080916084120-i8k3u6lhx3mw3py3
Tags: upstream-0.9.2
ImportĀ upstreamĀ versionĀ 0.9.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (C) 2006-2008 Anders Logg and Garth N. Wells.
 
2
// Licensed under the GNU LGPL Version 2.1.
 
3
//
 
4
// Modified by Ola Skavhaug, 2007-2008.
 
5
// Modified by Kent-Andre Mardal, 2008.
 
6
// Modified by Martin Sandve Alnes, 2008.
 
7
//
 
8
// First added:  2006-05-15
 
9
// Last changed: 2008-05-17
 
10
 
 
11
#ifndef __MATRIX_H
 
12
#define __MATRIX_H
 
13
 
 
14
#include <tr1/tuple>
 
15
#include <dolfin/common/Variable.h>
 
16
#include "DefaultFactory.h"
 
17
#include "GenericMatrix.h"
 
18
 
 
19
namespace dolfin
 
20
{
 
21
 
 
22
  /// This class provides the default DOLFIN matrix class,
 
23
  /// based on the default DOLFIN linear algebra backend.
 
24
 
 
25
  class Matrix : public GenericMatrix, public Variable
 
26
  {
 
27
  public:
 
28
 
 
29
    /// Create empty matrix
 
30
    Matrix() : Variable("A", "DOLFIN matrix"), matrix(0)
 
31
    { DefaultFactory factory; matrix = factory.create_matrix(); }
 
32
 
 
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); }
 
36
 
 
37
    /// Copy constructor
 
38
    explicit Matrix(const Matrix& A) : Variable("A", "DOLFIN matrix"),
 
39
                                       matrix(A.matrix->copy())
 
40
    {}
 
41
 
 
42
    /// Destructor
 
43
    virtual ~Matrix()
 
44
    { delete matrix; }
 
45
 
 
46
    //--- Implementation of the GenericTensor interface ---
 
47
 
 
48
    /// Initialize zero tensor using sparsity pattern
 
49
    virtual void init(const GenericSparsityPattern& sparsity_pattern)
 
50
    { matrix->init(sparsity_pattern); }
 
51
 
 
52
    /// Return copy of tensor
 
53
    virtual Matrix* copy() const
 
54
    { Matrix* A = new Matrix(); delete A->matrix; A->matrix = matrix->copy(); return A; }
 
55
 
 
56
    /// Return size of given dimension
 
57
    virtual uint size(uint dim) const
 
58
    { return matrix->size(dim); }
 
59
 
 
60
    /// Set all entries to zero and keep any sparse structure
 
61
    virtual void zero()
 
62
    { matrix->zero(); }
 
63
 
 
64
    /// Finalize assembly of tensor
 
65
    virtual void apply()
 
66
    { matrix->apply(); }
 
67
 
 
68
    /// Display tensor
 
69
    virtual void disp(uint precision=2) const
 
70
    { matrix->disp(precision); }
 
71
 
 
72
    //--- Implementation of the GenericMatrix interface ---
 
73
 
 
74
    /// Resize matrix to M x N
 
75
    virtual void resize(uint M, uint N)
 
76
    { matrix->resize(M, N); }
 
77
 
 
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); }
 
81
 
 
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); }
 
85
 
 
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); }
 
89
 
 
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); }
 
93
 
 
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); }
 
97
 
 
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); }
 
101
 
 
102
    /// Set given rows to zero
 
103
    virtual void zero(uint m, const uint* rows)
 
104
    { matrix->zero(m, rows); }
 
105
 
 
106
    /// Set given rows to identity matrix
 
107
    virtual void ident(uint m, const uint* rows)
 
108
    { matrix->ident(m, rows); }
 
109
 
 
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); }
 
113
 
 
114
    /// Multiply matrix by given number
 
115
    virtual const Matrix& operator*= (double a)
 
116
    { *matrix *= a; return *this; }
 
117
 
 
118
    /// Divide matrix by given number
 
119
    virtual const Matrix& operator/= (double a)
 
120
    { *matrix /= a; return *this; }
 
121
 
 
122
    /// Assignment operator
 
123
    virtual const GenericMatrix& operator= (const GenericMatrix& A)
 
124
    { *matrix = A; return *this; }
 
125
 
 
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(); }
 
130
 
 
131
    //--- Special functions ---
 
132
 
 
133
    /// Return linear algebra backend factory
 
134
    virtual LinearAlgebraFactory& factory() const
 
135
    { return matrix->factory(); }
 
136
 
 
137
    //--- Special functions, intended for library use only ---
 
138
 
 
139
    /// Return concrete instance / unwrap (const version)
 
140
    virtual const GenericMatrix* instance() const
 
141
    { return matrix; }
 
142
 
 
143
    /// Return concrete instance / unwrap (non-const version)
 
144
    virtual GenericMatrix* instance()
 
145
    { return matrix; }
 
146
 
 
147
    //--- Special Matrix functions ---
 
148
 
 
149
    /// Assignment operator
 
150
    const Matrix& operator= (const Matrix& A)
 
151
    { *matrix = *A.matrix; return *this; }
 
152
 
 
153
  private:
 
154
 
 
155
    // Pointer to concrete implementation
 
156
    GenericMatrix* matrix;
 
157
 
 
158
  };
 
159
 
 
160
}
 
161
 
 
162
#endif