-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatvec.cpp
110 lines (104 loc) · 3.14 KB
/
matvec.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Produit matrice-vecteur
# include <cassert>
# include <vector>
# include <iostream>
// ---------------------------------------------------------------------
class Matrix : public std::vector<double>
{
public:
Matrix (int dim);
Matrix( int nrows, int ncols );
Matrix( const Matrix& A ) = delete;
Matrix( Matrix&& A ) = default;
~Matrix() = default;
Matrix& operator = ( const Matrix& A ) = delete;
Matrix& operator = ( Matrix&& A ) = default;
double& operator () ( int i, int j ) {
return m_arr_coefs[i + j*m_nrows];
}
double operator () ( int i, int j ) const {
return m_arr_coefs[i + j*m_nrows];
}
std::vector<double> operator * ( const std::vector<double>& u ) const;
std::ostream& print( std::ostream& out ) const
{
const Matrix& A = *this;
out << "[\n";
for ( int i = 0; i < m_nrows; ++i ) {
out << " [ ";
for ( int j = 0; j < m_ncols; ++j ) {
out << A(i,j) << " ";
}
out << " ]\n";
}
out << "]";
return out;
}
private:
int m_nrows, m_ncols;
std::vector<double> m_arr_coefs;
};
// ---------------------------------------------------------------------
inline std::ostream&
operator << ( std::ostream& out, const Matrix& A )
{
return A.print(out);
}
// ---------------------------------------------------------------------
inline std::ostream&
operator << ( std::ostream& out, const std::vector<double>& u )
{
out << "[ ";
for ( const auto& x : u )
out << x << " ";
out << " ]";
return out;
}
// ---------------------------------------------------------------------
std::vector<double>
Matrix::operator * ( const std::vector<double>& u ) const
{
const Matrix& A = *this;
assert( u.size() == unsigned(m_ncols) );
std::vector<double> v(m_nrows, 0.);
for ( int i = 0; i < m_nrows; ++i ) {
for ( int j = 0; j < m_ncols; ++j ) {
v[i] += A(i,j)*u[j];
}
}
return v;
}
// =====================================================================
Matrix::Matrix (int dim) : m_nrows(dim), m_ncols(dim),
m_arr_coefs(dim*dim)
{
for ( int i = 0; i < dim; ++ i ) {
for ( int j = 0; j < dim; ++j ) {
(*this)(i,j) = (i+j)%dim;
}
}
}
// ---------------------------------------------------------------------
Matrix::Matrix( int nrows, int ncols ) : m_nrows(nrows), m_ncols(ncols),
m_arr_coefs(nrows*ncols)
{
int dim = (nrows > ncols ? nrows : ncols );
for ( int i = 0; i < nrows; ++ i ) {
for ( int j = 0; j < ncols; ++j ) {
(*this)(i,j) = (i+j)%dim;
}
}
}
// =====================================================================
int main( int nargs, char* argv[] )
{
const int N = 120;
Matrix A(N);
std::cout << "A : " << A << std::endl;
std::vector<double> u( N );
for ( int i = 0; i < N; ++i ) u[i] = i+1;
std::cout << " u : " << u << std::endl;
std::vector<double> v = A*u;
std::cout << "A.u = " << v << std::endl;
return EXIT_SUCCESS;
}