7 for (int i
=0, j
=0; dat
->mult_ok(i
,j
); dat
->mult_next(i
,j
))
8 r
+= sqr(dat
->elem(i
,j
));
15 for (int i
=0, j
=0; dat
->mult_ok(i
,j
); dat
->mult_next(i
,j
))
20 Matrix::set_diag(Real r
)
22 for (int i
=0, j
=0; dat
->mult_ok(i
,j
); dat
->mult_next(i
,j
))
23 dat
->elem(i
,j
)=(i
==j
) ? r
: 0.0;
27 Matrix::set_diag(Vector d
)
29 for (int i
=0, j
=0; dat
->mult_ok(i
,j
); dat
->mult_next(i
,j
))
30 dat
->elem(i
,j
)=(i
==j
) ? d(i
): 0.0;
34 Matrix::operator+=(const Matrix
&m
)
36 assert(m
.cols() == cols());
37 assert(m
.rows() == rows());
38 for (int i
=0, j
=0; dat
->mult_ok(i
,j
); dat
->mult_next(i
,j
))
39 dat
->elem(i
,j
) += m(i
,j
);
43 Matrix::operator-=(const Matrix
&m
)
45 assert(m
.cols() == cols());
46 assert(m
.rows() == rows());
47 for (int i
=0, j
=0; dat
->mult_ok(i
,j
); dat
->mult_next(i
,j
))
48 dat
->elem(i
,j
) -= m(i
,j
);
53 Matrix::operator*=(Real a
)
55 for (int i
=0, j
=0; dat
->mult_ok(i
,j
); dat
->mult_next(i
,j
))
60 Matrix::operator=(const Matrix
&m
)
68 Matrix::Matrix(const Matrix
&m
)
76 Matrix::Matrix(int n
, int m
)
78 dat
= virtual_smat::get_full(n
,m
);
84 dat
= virtual_smat::get_full(n
,n
);
88 Matrix::Matrix(Vector v
, Vector w
)
90 dat
= virtual_smat::get_full(v
.dim(), w
.dim());
91 for (int i
=0, j
=0; dat
->mult_ok(i
,j
); dat
->mult_next(i
,j
))
92 dat
->elem(i
,j
)=v(i
)*w(j
);
97 Matrix::row(int k
) const
103 for(int i
=0; i
< n
; i
++)
110 Matrix::col(int k
) const
114 for(int i
=0; i
< n
; i
++)
120 Matrix::left_multiply(const Vector
& v
) const
122 Vector
dest(v
.dim());
123 assert(dat
->cols()==v
.dim());
124 for (int i
=0, j
=0; dat
->mult_ok(i
,j
); dat
->mult_next(i
,j
))
125 dest(i
)+= dat
->elem(j
,i
)*v(j
);
130 Matrix::operator *(const Vector
& v
) const
133 assert(dat
->cols()==v
.dim());
134 for (int i
=0, j
=0; dat
->mult_ok(i
,j
); dat
->mult_next(i
,j
))
135 dest(i
)+= dat
->elem(i
,j
)*v(j
);
140 operator /(Matrix
const& m1
,Real a
)
148 Matrix::transpose() // delegate to storage?
150 for (int i
=0, j
=0; dat
->mult_ok(i
,j
); dat
->mult_next(i
,j
)) {
153 Real r
=dat
->elem(i
,j
);
154 dat
->elem(i
,j
) = dat
->elem(j
,i
);
160 Matrix::operator-() const
169 Matrix::transposed() const
177 /* should do something smarter: bandmatrix * bandmatrix is also banded matrix. */
179 operator *(const Matrix
&m1
, const Matrix
&m2
)
181 Matrix
result(m1
.rows(), m2
.cols());
182 result
.set_product(m1
,m2
);
187 Matrix::set_product(const Matrix
&m1
, const Matrix
&m2
)
189 assert(m1
.cols()==m2
.rows());
190 assert(cols()==m2
.cols() && rows()==m1
.rows());
192 for (int i
=0, j
=0; dat
->mult_ok(i
,j
);
193 dat
->mult_next(i
,j
)) {
195 for (int k
= 0; k
< m1
.cols(); k
++)
196 r
+= m1(i
,k
)*m2(k
,j
);
202 Matrix::insert_row(Vector v
, int k
)
204 assert(v
.dim()==cols());
206 for (int j
=0; j
< cols(); j
++)
212 Matrix::swap_columns(int c1
, int c2
)
214 assert(c1
>=0&& c1
< cols()&&c2
< cols() && c2
>=0);
215 for (int i
=0; i
< rows(); i
++) {
216 Real r
=dat
->elem(i
,c1
);
217 dat
->elem(i
,c1
) = dat
->elem(i
,c2
);
223 Matrix::swap_rows(int c1
, int c2
)
225 assert(c1
>=0&& c1
< rows()&&c2
< rows() && c2
>=0);
226 for (int i
=0; i
< cols(); i
++) {
227 Real r
=dat
->elem(c1
,i
);
228 dat
->elem(c1
,i
) = dat
->elem(c2
,i
);
237 assert(cols() == rows());