Rheolef  7.2
an efficient C++ finite element environment
 
Loading...
Searching...
No Matches
tiny_matvec.h
Go to the documentation of this file.
1# ifndef _RHEO_TINY_MATVEC_H
2# define _RHEO_TINY_MATVEC_H
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
32namespace 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
36const unsigned int tiny_size_max = 32;
37
38template <class T>
40public:
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()); }
56protected:
57 T t_ [tiny_size_max];
60};
61template <class T>
63public:
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()); }
78private:
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
89template <class T>
90inline
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}
99template <class T>
100inline
102 : size_(n), i0_(0)
103{
104 check_macro (n <= tiny_size_max, "invalid size");
105 fill (t_, t_+tiny_size_max, value);
106}
107template <class T>
108inline
109void
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}
118template <class T>
119inline
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}
131template <class T>
132inline
133void
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}
145template <class T>
146inline
147void
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}
154template <class T>
155void
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}
164template<class T>
165tiny_matrix<T>
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 }
179template<class T>
180tiny_vector<T>
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 */
field::size_type size_type
Definition branch.cc:430
size_type get_col_origin() const
Definition tiny_matvec.h:75
tiny_matrix(size_type nr=0, size_type nc=0)
T & operator()(size_type i, size_type j)
Definition tiny_matvec.h:68
void fill(const T &val)
void resize(size_type nr, size_type nc)
void set_origin(size_type i, size_type j)
Definition tiny_matvec.h:72
size_type nrow() const
Definition tiny_matvec.h:66
size_type get_row_origin() const
Definition tiny_matvec.h:74
size_type ncol() const
Definition tiny_matvec.h:67
tiny_vector< T >::size_type size_type
Definition tiny_matvec.h:64
tiny_vector(size_type n=0)
Definition tiny_matvec.h:91
void fill(const T &val)
Definition tiny_matvec.h:53
T t_[tiny_size_max]
Definition tiny_matvec.h:57
size_type size() const
Definition tiny_matvec.h:45
const T & operator()(size_type i) const
Definition tiny_matvec.h:47
void resize(size_type n)
void set_origin(size_type i)
Definition tiny_matvec.h:51
size_type get_origin() const
Definition tiny_matvec.h:52
const T & operator[](size_type i) const
Definition tiny_matvec.h:48
std::vector< int >::size_type size_type
Definition tiny_matvec.h:41
const unsigned int tiny_size_max
Definition tiny_matvec.h:36
Expr1::float_type T
Definition field_expr.h:230
check_macro(expr1.have_homogeneous_space(Xh1), "dual(expr1,expr2); expr1 should have homogeneous space. HINT: use dual(interpolate(Xh, expr1),expr2)")
This file is part of Rheolef.
csr< T, sequential > trans(const csr< T, sequential > &a)
trans(a): see the form page for the full documentation
Definition csr.h:455
csr< T, sequential > operator*(const T &lambda, const csr< T, sequential > &a)
Definition csr.h:437
Definition leveque.h:25