1
# ifndef _RHEO_TINY_MATVEC_H
2
# define _RHEO_TINY_MATVEC_H
4
/// This file is part of Rheolef.
6
/// Copyright (C) 2000-2009 Pierre Saramito <Pierre.Saramito@imag.fr>
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.
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.
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
22
/// =========================================================================
24
// very small matrix - vector
26
// authors: Pierre.Saramito@imag.fr
30
#include "rheolef/compiler.h"
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
36
const unsigned int tiny_size_max = 32;
41
typedef typename std::vector<int>::size_type size_type;
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()); }
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()); }
79
T t_ [tiny_size_max][tiny_size_max];
85
// =====================================================================
87
// =====================================================================
91
tiny_vector<T>::tiny_vector (size_type n)
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
101
tiny_vector<T>::tiny_vector (size_type n, const T& value)
104
check_macro (n <= tiny_size_max, "invalid size");
105
fill (t_, t_+tiny_size_max, value);
110
tiny_vector<T>::resize(size_type 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
120
tiny_matrix<T>::tiny_matrix (size_type nr, size_type nc)
121
: nrow_(nr), ncol_(nc), i0_(0), j0_(0)
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
134
tiny_matrix<T>::resize(size_type nr, size_type 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
148
tiny_matrix<T>::fill(const T& val)
150
for (size_type i = i0_; i < i0_ + nrow_; i++)
151
for (size_type j = j0_; j < j0_ + ncol_; j++)
156
trans(const tiny_matrix<T>& a, tiny_matrix<T>& b)
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++)
166
operator* (const tiny_matrix<T>& a, const tiny_matrix<T>& b)
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());
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);
181
operator* (const tiny_matrix<T>& a, const tiny_vector<T>& u)
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());
188
for (size_type i=0; i<a.nrow(); i++)
189
for (size_type j=0; j<u.size(); j++)
193
}// namespace rheolef
194
# endif /* _RHEO_TINY_MATVEC_H */