~ubuntu-branches/ubuntu/trusty/rheolef/trusty

« back to all changes in this revision

Viewing changes to nfem/geo_element/tiny_matvec.h

  • Committer: Package Import Robot
  • Author(s): Pierre Saramito
  • Date: 2012-04-06 09:12:21 UTC
  • mfrom: (1.1.5)
  • Revision ID: package-import@ubuntu.com-20120406091221-m58me99p1nxqui49
Tags: 6.0-1
* New upstream release 6.0 (major changes):
  - massively distributed and parallel support
  - full FEM characteristic method (Lagrange-Gakerkin method) support
  - enhanced users documentation 
  - source code supports g++-4.7 (closes: #667356)
* debian/control: dependencies for MPI distributed solvers added
* debian/rules: build commands simplified
* debian/librheolef-dev.install: man1/* to man9/* added
* debian/changelog: package description rewritted (closes: #661689)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# ifndef _RHEO_TINY_MATVEC_H
 
2
# define _RHEO_TINY_MATVEC_H
 
3
///
 
4
/// This file is part of Rheolef.
 
5
///
 
6
/// Copyright (C) 2000-2009 Pierre Saramito <Pierre.Saramito@imag.fr>
 
7
///
 
8
/// Rheolef is free software; you can redistribute it and/or modify
 
9
/// it under the terms of the GNU General Public License as published by
 
10
/// the Free Software Foundation; either version 2 of the License, or
 
11
/// (at your option) any later version.
 
12
///
 
13
/// Rheolef is distributed in the hope that it will be useful,
 
14
/// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
/// GNU General Public License for more details.
 
17
///
 
18
/// You should have received a copy of the GNU General Public License
 
19
/// along with Rheolef; if not, write to the Free Software
 
20
/// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
21
/// 
 
22
/// =========================================================================
 
23
//
 
24
// very small matrix - vector
 
25
//
 
26
// authors: Pierre.Saramito@imag.fr
 
27
//
 
28
// date: 7 july 1997
 
29
//
 
30
#include "rheolef/compiler.h"
 
31
 
 
32
namespace rheolef { 
 
33
// take a 2^n since a(i,j) -> table (tiny_size_max*j + i)
 
34
// and tiny_size_max*j == j << log2(tiny_size_max*j) is fast
 
35
 
 
36
const unsigned int tiny_size_max = 32;
 
37
 
 
38
template <class T>
 
39
class tiny_vector {
 
40
public:
 
41
    typedef typename std::vector<int>::size_type size_type;
 
42
 
 
43
    tiny_vector (size_type n = 0);
 
44
    tiny_vector (size_type n, const T& value);
 
45
    size_type size() const { return size_; }
 
46
    void resize(size_type n);
 
47
    const T& operator() (size_type i) const { return t_[i+i0_]; }
 
48
    const T& operator[] (size_type i) const { return t_[i+i0_]; }
 
49
    T& operator() (size_type i) { return t_[i+i0_]; }
 
50
    T& operator[] (size_type i) { return t_[i+i0_]; }
 
51
    void set_origin(size_type i) { i0_ = i; }
 
52
    size_type get_origin() const { return i0_; }
 
53
    void fill(const T& val) {
 
54
        for (size_type i = i0_; i < i0_ + size_; i++) t_ [i] = val; }
 
55
    void reset() { fill(T()); }
 
56
protected:
 
57
    T t_ [tiny_size_max];
 
58
    size_type size_;
 
59
    size_type i0_;
 
60
};
 
61
template <class T>
 
62
class tiny_matrix {
 
63
public:
 
64
    typedef typename tiny_vector<T>::size_type size_type;
 
65
    tiny_matrix (size_type nr = 0, size_type nc = 0);
 
66
    size_type nrow() const { return nrow_; }
 
67
    size_type ncol() const { return ncol_; }
 
68
    T& operator() (size_type i, size_type j) { return t_[i+i0_][j+j0_]; }
 
69
    const T& operator() (size_type i, size_type j) const { return t_[i+i0_][j+j0_]; }
 
70
    T& operator() (size_type i) { return t_[i+i0_][i+j0_]; }
 
71
    const T& operator() (size_type i) const { return t_[i+i0_][i+j0_]; }
 
72
    void set_origin(size_type i, size_type j) { i0_ = i; j0_ = j; }
 
73
    void resize(size_type nr, size_type nc);
 
74
    size_type get_row_origin() const { return i0_; }
 
75
    size_type get_col_origin() const { return j0_; }
 
76
    void fill(const T& val);
 
77
    void reset() { fill(T()); }
 
78
private:
 
79
    T t_ [tiny_size_max][tiny_size_max];
 
80
    size_type nrow_;
 
81
    size_type ncol_;
 
82
    size_type i0_;
 
83
    size_type j0_;
 
84
};
 
85
// =====================================================================
 
86
// inlined
 
87
// =====================================================================
 
88
 
 
89
template <class T>
 
90
inline
 
91
tiny_vector<T>::tiny_vector (size_type n)
 
92
  : size_(n), i0_(0)
 
93
 
94
    check_macro (n <= tiny_size_max, "invalid size"); 
 
95
#ifdef _RHEOLEF_PARANO
 
96
    std::fill (t_, t_+tiny_size_max, std::numeric_limits<T>::max());
 
97
#endif // _RHEOLEF_PARANO
 
98
}
 
99
template <class T>
 
100
inline
 
101
tiny_vector<T>::tiny_vector (size_type n, const T& value)
 
102
  : size_(n), i0_(0)
 
103
 
104
    check_macro (n <= tiny_size_max, "invalid size"); 
 
105
    fill (t_, t_+tiny_size_max, value);
 
106
}
 
107
template <class T>
 
108
inline
 
109
void
 
110
tiny_vector<T>::resize(size_type n)
 
111
{
 
112
    size_ = n;
 
113
    check_macro (n <= tiny_size_max, "invalid size"); 
 
114
#ifdef _RHEOLEF_PARANO
 
115
    std::fill (t_, t_+tiny_size_max, std::numeric_limits<T>::max());
 
116
#endif // _RHEOLEF_PARANO
 
117
}
 
118
template <class T>
 
119
inline
 
120
tiny_matrix<T>::tiny_matrix (size_type nr, size_type nc)
 
121
  : nrow_(nr), ncol_(nc), i0_(0), j0_(0)
 
122
{
 
123
    check_macro (nr <= tiny_size_max && nc <= tiny_size_max, "invalid sizes");
 
124
#ifdef _RHEOLEF_PARANO
 
125
    for (size_type i = 0; i < tiny_size_max; i++) 
 
126
      for (size_type j = 0; j < tiny_size_max; j++) 
 
127
        t_[i][j] = std::numeric_limits<T>::max();
 
128
#endif // _RHEOLEF_PARANO
 
129
 
 
130
}
 
131
template <class T>
 
132
inline
 
133
void
 
134
tiny_matrix<T>::resize(size_type nr, size_type nc)
 
135
{
 
136
    nrow_ = nr;
 
137
    ncol_ = nc;
 
138
    check_macro (nr <= tiny_size_max && nc <= tiny_size_max, "invalid sizes");
 
139
#ifdef _RHEOLEF_PARANO
 
140
    for (size_type i = 0; i < tiny_size_max; i++) 
 
141
      for (size_type j = 0; j < tiny_size_max; j++) 
 
142
        t_[i][j] = std::numeric_limits<T>::max();
 
143
#endif // _RHEOLEF_PARANO
 
144
}
 
145
template <class T>
 
146
inline
 
147
void
 
148
tiny_matrix<T>::fill(const T& val)
 
149
{
 
150
    for (size_type i = i0_; i < i0_ + nrow_; i++) 
 
151
      for (size_type j = j0_; j < j0_ + ncol_; j++) 
 
152
        t_ [i][j] = val;
 
153
}
 
154
template <class T>
 
155
void
 
156
trans(const tiny_matrix<T>& a, tiny_matrix<T>& b)
 
157
{
 
158
    typedef typename tiny_matrix<T>::size_type size_type;
 
159
    b.resize (a.ncol(), a.nrow());
 
160
    for (size_type i = 0; i < a.nrow(); i++) 
 
161
      for (size_type j = 0; j < a.ncol(); j++)
 
162
        b(j,i) = a(i,j);
 
163
}
 
164
template<class T>
 
165
tiny_matrix<T>
 
166
operator* (const tiny_matrix<T>& a, const tiny_matrix<T>& b)
 
167
 {
 
168
    check_macro(a.ncol()==b.nrow(),"Error in matrices sizes for multiplication, "
 
169
        << a.nrow()<<"x"<<a.ncol() <<" and "<< b.nrow()<<"x"<<b.ncol());
 
170
    typedef typename tiny_matrix<T>::size_type size_type;
 
171
    tiny_matrix<T> c(a.nrow(),b.ncol());
 
172
    c.fill(0);
 
173
    for (size_type i=0; i<a.nrow(); i++)
 
174
     for (size_type j=0; j<b.ncol(); j++)
 
175
      for (size_type k=0; k<b.nrow(); k++)
 
176
       c(i,j)+=a(i,k)*b(k,j);
 
177
    return c;
 
178
 }
 
179
template<class T>
 
180
tiny_vector<T>
 
181
operator* (const tiny_matrix<T>& a, const tiny_vector<T>& u)
 
182
 {
 
183
    check_macro(a.ncol()==u.size(),"Error in matrice-vector sizes for multiplication, "
 
184
        << a.nrow()<<"x"<<a.ncol() <<" and "<< u.size());
 
185
    typedef typename tiny_matrix<T>::size_type size_type;
 
186
    tiny_vector<T> v(a.nrow());
 
187
    v.fill(0);
 
188
    for (size_type i=0; i<a.nrow(); i++)
 
189
     for (size_type j=0; j<u.size(); j++)
 
190
      v(i)+=a(i,j)*u(j);
 
191
    return v;
 
192
 }
 
193
}// namespace rheolef
 
194
# endif /* _RHEO_TINY_MATVEC_H */