28 TMatrix::TMatrix(
int size) {
31 elm =
new double[ nc * nr ];
34 TMatrix::TMatrix(
int num_rows,
int num_cols) {
37 elm =
new double[ nc * nr ];
44 elm =
new double[nc*nr];
45 memcpy(elm,x.
elm,nc*nr*
sizeof(
double));
52 TMVector::TMVector(
int size) {
54 elm =
new double[size];
60 elm =
new double[size];
61 memcpy(elm,x.
elm,size*
sizeof(
double));
64 TMVector::~TMVector() {
69 for (
int i = 1; i <= M.
nr; i++) {
70 for (
int j = 1; j <= M.
nc; j++) {
71 stream << M.
Get(i, j) <<
" ";
79 for (
int i = 1; i <= V.
size; i++) {
80 stream << V.
elm[ i - 1 ] <<
"\n";
85 void TMatrix::Set(
int row,
int col,
double value) {
87 THROW( ExcAssertMsg() << EI_Message(
"Number of the row is greater than number of rows in the matrix.") );
89 THROW( ExcAssertMsg() << EI_Message(
"Number of the column is greater than number of columns in the matrix.") );
90 elm[ (row - 1) * nc + col - 1 ] = value;
94 double TMatrix::Get(
int row,
int col)
const {
96 THROW( ExcAssertMsg() << EI_Message(
"Number of the row is greater than number of rows in the matrix.") );
98 THROW( ExcAssertMsg() << EI_Message(
"Number of the column is greater than number of columns in the matrix.") );
99 return elm[ (row - 1) * nc + col - 1 ];
102 void TMatrix::SwapRows(
int r1,
int r2) {
103 if (r1 > nr || r2 > nr) {
104 THROW( ExcAssertMsg() << EI_Message(
"Number of the row is greater than number of rows in the matrix.") );
107 for (
int i = 1; i <= nc; i++) {
108 double tmp = Get(r1, i);
109 Set(r1, i, Get(r2, i));
116 void TMVector::SwapElements(
int i1,
int i2) {
117 if (i1 > size || i2 > size) {
118 THROW( ExcAssertMsg() << EI_Message(
"Number of the element is greater than size of the vector.") );
121 double tmp = elm[ i1 - 1 ];
122 elm[ i1 - 1 ] = elm[ i2 - 1 ];
128 double TMVector::Get(
int i) {
130 THROW( ExcAssertMsg() << EI_Message(
"Number of the element is greater than size of the vector.") );
137 void TMVector::Set(
int i,
double value) {
139 THROW( ExcAssertMsg() << EI_Message(
"Number of the element is greater than size of the vector.") );
140 elm[ i - 1 ] =
value;
144 int TMatrix::NRows()
const {
148 int TMatrix::NCols()
const {
156 for (
int i = 1; i < M.
NRows(); i++) {
157 double tmp = fabs(M.
Get(i, i));
159 for (
int j = i + 1; j <= M.
NRows(); j++)
160 if (fabs(M.
Get(j, i)) > tmp) {
161 tmp = fabs(M.
Get(j, i));
173 for (
int j = i + 1; j <= M.
NRows(); j++) {
174 tmp = M.
Get(j, i) / M.
Get(i, i);
175 for (
int k = i; k <= M.
NCols(); k++) {
176 M.
Set(j, k, M.
Get(j, k) - tmp * M.
Get(i, k));
182 for (
int i = M.
NRows(); i >= 1; i--) {
183 double tmp = b.
Get(i);
185 for (
int k = i + 1; k <= M.
NCols(); k++) {
186 tmp -= M.
Get(i, k) * X->
Get(k);
197 X->
Set(i, tmp / M.
Get(i, i));
void Set(int, int, double)
static constexpr bool value
TNSolutions Gauss(const TMatrix &, TMVector *, const TMVector &)
std::ostream & operator<<(std::ostream &stream, const TMVector &V)
double Get(int, int) const
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
void SwapElements(int, int)