1 #ifndef math_Matrix_H__
2 #define math_Matrix_H__
5 #include "../math/utility.h"
21 typedef typename std::vector<Entry>::reference
EntryRef ;
22 typedef typename std::vector<Entry>::const_reference
EntryRefK;
27 std::vector<Entry> entries_;
30 rows_(0), cols_(0), entries_(0) {
33 Myself(Myself
const& b):
34 rows_(b.rows_), cols_(b.cols_), entries_(b.entries_) {
37 Myself(
size_t r,
size_t c, Entry
const& e):
38 rows_(r), cols_(c), entries_(r * c, e) {
44 size_t index(
size_t r,
size_t c)
const {
49 std::vector<Entry> tmp(entries_);
83 Matrix(
size_t r,
size_t c, Entry
const& e): self(Myself(r, c, e)) {
116 void reset(
size_t r,
size_t c, Entry
const& e) {
119 self()->entries_.clear();
120 self()->entries_.resize(r * c, e);
152 size_t rows(
size_t r, Entry
const& e) {
154 self()->entries_.resize(r *
cols(), e);
169 size_t cols(
size_t c, Entry
const& e) {
172 self()->entries_.resize(
rows() * c);
174 for (
size_t i = 0, I =
rows(); i < I; i++) {
175 size_t j, J1 = std::min(old->cols_,
cols()), J2 =
cols();
176 for (j = 0; j < J1; j++)
177 self()->entries_[
self->index(i, j)] = old->entries_[old->index(i, j)];
178 for (j = J1; j < J2; j++)
179 self()->entries_[
self->index(i, j)] = e;
195 size_t size(
size_t r,
size_t c, Entry
const& e) {
207 self()->entries_.clear();
212 Entry
entry(
size_t r,
size_t c)
const {
213 return self->entries_[
self->index(r, c)];
217 Entry
entry(
size_t r,
size_t c, Entry
const& e) {
218 self()->entries_[self->index(r, c)] = e;
224 return self()->entries_[self->index(r, c)];
238 ssize_t cFirst, ssize_t cLast,
240 for (ssize_t r = rFirst; r <= rLast; r++) {
241 for (ssize_t c = cFirst; c <=cFirst; c++) {
259 size_t cFirst,
size_t cLast)
const {
260 if (rFirst > rLast || cFirst > cLast)
return Matrix();
261 if (rFirst == 0 && cFirst == 0) {
263 ret.
size(rLast + 1, cLast + 1, Entry(0));
266 Matrix ret(rLast - rFirst + 1, cLast - cFirst + 1,
entry(rFirst, cFirst));
267 for (
size_t r = rFirst; r <= rLast; r++)
268 for (
size_t c = cFirst; c <= cLast; c++)
269 ret.
entry(r - rFirst, c - cFirst,
entry(r, c));
291 for (
size_t r = 0, R =
rows(); r < R; r++)
292 for (
size_t c = 0, C =
cols(); c < C; c++)
304 for (
size_t r = 0, R =
rows(); r < R; r++)
305 for (
size_t c = 0, C =
cols(); c < C; c++)
317 for (
size_t r = 0, R =
rows(); r < R; r++)
318 for (
size_t c = 0, C =
cols(); c < C; c++)
330 for (
size_t r = 0, R =
rows(); r < R; r++)
331 for (
size_t c = 0, C = m.
cols(); c < C; c++)
332 for (
size_t k = 0, K =
cols(); k < K; k++)
340 for (
size_t r = 0, R =
rows(); r < R; r++)
341 for (
size_t c = 0, C =
cols(); c < C; c++)
349 for (
size_t r = 0, R =
rows(); r < R; r++)
350 for (
size_t c = 0, C =
cols(); c < C; c++)
368 for (
size_t r = 0, R =
rows(); r < R; r++)
369 for (
size_t c = 0, C =
cols(); c < C; c++)
370 entry(r, c, (r == c ? Entry(1) : Entry(0)));
379 for (
size_t i = 0, I =
rows(); i < I; ++i) {
380 for (
size_t j = i + 1, J =
cols(); j < J; ++j) {
381 entry(i, j, Entry(0));
404 for (
size_t r = 0, R =
rows(); r < R; r++) {
405 for (
size_t c = 0, C =
cols(); c < C; c++) {
407 tmp.
entry(r, c +
cols(), (r == c ? Entry(1) : Entry(0)));
411 for (ssize_t r =
rows() - 1; r >= 0; r--) {
413 for (ssize_t r2 = r - 1; r2 >= 0; r2--) {
414 Entry rat(-tmp.
entry(r2, r) / tmp.
entry(r, r));
415 for (
size_t c = r, C = tmp.
cols(); c < C; c++) {
416 tmp.
entry(r2, c, tmp.
entry(r2, c) + rat * tmp(r, c));
419 Entry rat(tmp.
entry(r, r));
420 for (
size_t c =
cols(), C = tmp.
cols(); c < C; c++) {
437 for (
size_t r = 0, R =
cols(); r < R; r++)
438 for (
size_t c = 0, C =
rows(); c < C; c++)
458 for (
size_t r = 0, c = 0, R =
rows(), C =
cols(); r < R && c < C; r++) {
460 for ( ; c < C; c++) {
462 for (
size_t r2 = r; r2 < R; r2++)
465 if (
entry(maxR, c) != Entry(0))
break;
468 if (maxR != (ssize_t)r) {
469 for (
size_t c2 = c; c2 < C; c2++)
470 std::swap(
self()->entries_[self->index( r, c2)],
471 self()->entries_[self->index(maxR, c2)]);
473 for (
size_t r2 = r + 1; r2 < R; r2++) {
475 entry(r2, c, Entry(0));
476 for (
size_t c2 = c + 1; c2 < C; c2++)
495 return entry(r, c, e);
536 #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.