1 #ifndef math_Matrix_H__
2 #define math_Matrix_H__
20 typedef typename std::vector<Entry>::reference
EntryRef ;
21 typedef typename std::vector<Entry>::const_reference
EntryRefK;
26 std::vector<Entry> entries_;
29 rows_(0), cols_(0), entries_(0) {
32 Myself(Myself
const& b):
33 rows_(b.rows_), cols_(b.cols_), entries_(b.entries_) {
36 Myself(
size_t r,
size_t c, Entry
const& e):
37 rows_(r), cols_(c), entries_(r * c, e) {
43 size_t index(
size_t r,
size_t c)
const {
48 std::vector<Entry> tmp(entries_);
82 Matrix(
size_t r,
size_t c, Entry
const& e): self(Myself(r, c, e)) {
115 void reset(
size_t r,
size_t c, Entry
const& e) {
118 self()->entries_.clear();
119 self()->entries_.resize(r * c, e);
151 size_t rows(
size_t r, Entry
const& e) {
153 self()->entries_.resize(r *
cols(), e);
168 size_t cols(
size_t c, Entry
const& e) {
171 self()->entries_.resize(
rows() * c);
173 for (
size_t i = 0, I =
rows(); i < I; i++) {
174 size_t j, J1 = std::min(old->cols_,
cols()), J2 =
cols();
175 for (j = 0; j < J1; j++)
176 self()->entries_[
self->index(i, j)] = old->entries_[old->index(i, j)];
177 for (j = J1; j < J2; j++)
178 self()->entries_[
self->index(i, j)] = e;
194 size_t size(
size_t r,
size_t c, Entry
const& e) {
206 self()->entries_.clear();
211 Entry
entry(
size_t r,
size_t c)
const {
212 return self->entries_[
self->index(r, c)];
216 Entry
entry(
size_t r,
size_t c, Entry
const& e) {
217 self()->entries_[self->index(r, c)] = e;
223 return self()->entries_[self->index(r, c)];
237 ssize_t cFirst, ssize_t cLast,
239 for (ssize_t r = rFirst; r <= rLast; r++) {
240 for (ssize_t c = cFirst; c <=cFirst; c++) {
258 size_t cFirst,
size_t cLast)
const {
259 if (rFirst > rLast || cFirst > cLast)
return Matrix();
260 if (rFirst == 0 && cFirst == 0) {
262 ret.
size(rLast + 1, cLast + 1, Entry(0));
265 Matrix ret(rLast - rFirst + 1, cLast - cFirst + 1,
entry(rFirst, cFirst));
266 for (
size_t r = rFirst; r <= rLast; r++)
267 for (
size_t c = cFirst; c <= cLast; c++)
268 ret.
entry(r - rFirst, c - cFirst,
entry(r, c));
290 for (
size_t r = 0, R =
rows(); r < R; r++)
291 for (
size_t c = 0, C =
cols(); c < C; c++)
303 for (
size_t r = 0, R =
rows(); r < R; r++)
304 for (
size_t c = 0, C =
cols(); c < C; c++)
316 for (
size_t r = 0, R =
rows(); r < R; r++)
317 for (
size_t c = 0, C =
cols(); c < C; c++)
329 for (
size_t r = 0, R =
rows(); r < R; r++)
330 for (
size_t c = 0, C = m.
cols(); c < C; c++)
331 for (
size_t k = 0, K =
cols(); k < K; k++)
339 for (
size_t r = 0, R =
rows(); r < R; r++)
340 for (
size_t c = 0, C =
cols(); c < C; c++)
348 for (
size_t r = 0, R =
rows(); r < R; r++)
349 for (
size_t c = 0, C =
cols(); c < C; c++)
367 for (
size_t r = 0, R =
rows(); r < R; r++)
368 for (
size_t c = 0, C =
cols(); c < C; c++)
369 entry(r, c, (r == c ? Entry(1) : Entry(0)));
378 for (
size_t i = 0, I =
rows(); i < I; ++i) {
379 for (
size_t j = i + 1, J =
cols(); j < J; ++j) {
380 entry(i, j, Entry(0));
403 for (
size_t r = 0, R =
rows(); r < R; r++) {
404 for (
size_t c = 0, C =
cols(); c < C; c++) {
406 tmp.
entry(r, c +
cols(), (r == c ? Entry(1) : Entry(0)));
410 for (ssize_t r =
rows() - 1; r >= 0; r--) {
412 for (ssize_t r2 = r - 1; r2 >= 0; r2--) {
413 Entry rat(-tmp.
entry(r2, r) / tmp.
entry(r, r));
414 for (
size_t c = r, C = tmp.
cols(); c < C; c++) {
415 tmp.
entry(r2, c, tmp.
entry(r2, c) + rat * tmp(r, c));
418 Entry rat(tmp.
entry(r, r));
419 for (
size_t c =
cols(), C = tmp.
cols(); c < C; c++) {
436 for (
size_t r = 0, R =
cols(); r < R; r++)
437 for (
size_t c = 0, C =
rows(); c < C; c++)
457 for (
size_t r = 0, c = 0, R =
rows(), C =
cols(); r < R && c < C; r++) {
459 for ( ; c < C; c++) {
461 for (
size_t r2 = r; r2 < R; r2++)
464 if (
entry(maxR, c) != Entry(0))
break;
467 if (maxR != (ssize_t)r) {
468 for (
size_t c2 = c; c2 < C; c2++)
469 std::swap(
self()->entries_[self->index( r, c2)],
470 self()->entries_[self->index(maxR, c2)]);
472 for (
size_t r2 = r + 1; r2 < R; r2++) {
474 entry(r2, c, Entry(0));
475 for (
size_t c2 = c + 1; c2 < C; c2++)
494 return entry(r, c, e);
535 #endif // math_Matrix_H__
Matrix col(size_t c) const
Return the c -th column.
Matrix & triangulared()
triangluar itself
std::vector< Entry >::const_reference EntryRefK
Matrix & referenceFrom(Matrix const &m)
reference
size_t rows() const
Return number of rows.
Matrix operator*(Entry const &s) const
same as mul(m)
Matrix div(Entry const &s) const
return (*this) / s. s is a scalar
Matrix operator+() const
same as positive()
std::vector< Entry >::reference EntryRef
size_t rows(size_t r, Entry const &e)
resize the matrix such that number of rows become r.
Matrix & transposed()
Let itself become itself's transpose matrix.
size_t cols() const
Return number of cols.
Entry operator()(size_t r, size_t c, Entry const &e)
same as entry(r,c,e)
Matrix inverse() const
Return a matrix which is an inverse matrix of (*this)
Matrix subMatrix(size_t rFirst, size_t rLast, size_t cFirst, size_t cLast) const
Return a rLast-rFirst+1 x cLast-cFirst+1 matrix.
bool valid() const
Return whether it is a valid matrix.
Matrix operator*(Matrix const &m) const
same as mul(m)
Matrix row(size_t r) const
Return the r -th row.
Matrix & operator=(Matrix const &m)
same as copyFrom
Matrix diagonal() const
Return a matrix which is a diangonal form of me.
Matrix(Matrix const &m)
constructor
Matrix & copyFrom(Matrix const &m)
copy
T tAbs(T const &t)
就只是個取絕對值
Matrix(size_t r, size_t c, Entry const &e)
constructor
void entries(ssize_t rFirst, ssize_t rLast, ssize_t cFirst, ssize_t cLast, Entry const &e)
Change the entries from rFirst x cFirst to rLast x cLast.
size_t size() const
Return number of rows times number of cols.
size_t size(size_t r, size_t c, Entry const &e)
resize
void clear()
free the memory
Matrix identity() const
Return a identity matrix with size equal to itself.
Matrix & diagonaled()
Let itself be an diagonal form of original itself.
Matrix transpose() const
return itself's transpose matrix
Matrix mul(Matrix const &m) const
return (*this) times m.
Matrix mul(Entry const &s) const
return (*this) times s. s is a scalar
EntryRef entryGet(size_t r, size_t c)
Get the entry at r x c.
Matrix & inversed()
let itself become itself's inverse matrix
Matrix sub(Matrix const &m) const
return (*this) - m.
Matrix operator+(Matrix const &m) const
same as add(m)
Entry operator()(size_t r, size_t c) const
same as entry(r,c)
Matrix operator-() const
same as negative()
Entry entry(size_t r, size_t c, Entry const &e)
Change the entry at r x c.
Matrix & identitied()
Let itself be an identity matrix.
Entry entry(size_t r, size_t c) const
Access the entry at r x c.
Matrix negative() const
return -(*this)
size_t cols(size_t c, Entry const &e)
resize the matrix such that number of cols become c
Matrix operator/(Entry const &s) const
same as div(s)
Matrix positive() const
return +(*this)
void reset(size_t r, size_t c, Entry const &e)
reset the size of the matrix to r x c with entry all be e
Matrix operator-(Matrix const &m) const
same as sub(m)
Matrix triangular() const
return a matrix which is the triangular form of (*this)
Matrix add(Matrix const &m) const
return (*this) + m.