27 TMatrix::TMatrix(
int size) {
30 elm =
new double[ nc * nr ];
33 TMatrix::TMatrix(
int num_rows,
int num_cols) {
36 elm =
new double[ nc * nr ];
43 elm =
new double[nc*nr];
44 memcpy(elm,x.
elm,nc*nr*
sizeof(
double));
51 TMVector::TMVector(
int size) {
53 elm =
new double[size];
59 elm =
new double[size];
60 memcpy(elm,x.
elm,size*
sizeof(
double));
63 TMVector::~TMVector() {
68 for (
int i = 1; i <= M.
nr; i++) {
69 for (
int j = 1; j <= M.
nc; j++) {
70 stream << M.
Get(i, j) <<
" ";
78 for (
int i = 1; i <= V.
size; i++) {
79 stream << V.
elm[ i - 1 ] <<
"\n";
84 void TMatrix::Set(
int row,
int col,
double value) {
86 THROW( ExcAssertMsg() << EI_Message(
"Number of the row is greater than number of rows in the matrix.") );
88 THROW( ExcAssertMsg() << EI_Message(
"Number of the column is greater than number of columns in the matrix.") );
89 elm[ (row - 1) * nc + col - 1 ] = value;
93 double TMatrix::Get(
int row,
int col)
const {
95 THROW( ExcAssertMsg() << EI_Message(
"Number of the row is greater than number of rows in the matrix.") );
97 THROW( ExcAssertMsg() << EI_Message(
"Number of the column is greater than number of columns in the matrix.") );
98 return elm[ (row - 1) * nc + col - 1 ];
101 void TMatrix::SwapRows(
int r1,
int r2) {
102 if (r1 > nr || r2 > nr) {
103 THROW( ExcAssertMsg() << EI_Message(
"Number of the row is greater than number of rows in the matrix.") );
106 for (
int i = 1; i <= nc; i++) {
107 double tmp = Get(r1, i);
108 Set(r1, i, Get(r2, i));
115 void TMVector::SwapElements(
int i1,
int i2) {
116 if (i1 > size || i2 > size) {
117 THROW( ExcAssertMsg() << EI_Message(
"Number of the element is greater than size of the vector.") );
120 double tmp = elm[ i1 - 1 ];
121 elm[ i1 - 1 ] = elm[ i2 - 1 ];
127 double TMVector::Get(
int i) {
129 THROW( ExcAssertMsg() << EI_Message(
"Number of the element is greater than size of the vector.") );
136 void TMVector::Set(
int i,
double value) {
138 THROW( ExcAssertMsg() << EI_Message(
"Number of the element is greater than size of the vector.") );
139 elm[ i - 1 ] =
value;
143 int TMatrix::NRows()
const {
147 int TMatrix::NCols()
const {
155 for (
int i = 1; i < M.
NRows(); i++) {
156 double tmp = fabs(M.
Get(i, i));
158 for (
int j = i + 1; j <= M.
NRows(); j++)
159 if (fabs(M.
Get(j, i)) > tmp) {
160 tmp = fabs(M.
Get(j, i));
172 for (
int j = i + 1; j <= M.
NRows(); j++) {
173 tmp = M.
Get(j, i) / M.
Get(i, i);
174 for (
int k = i; k <= M.
NCols(); k++) {
175 M.
Set(j, k, M.
Get(j, k) - tmp * M.
Get(i, k));
181 for (
int i = M.
NRows(); i >= 1; i--) {
182 double tmp = b.
Get(i);
184 for (
int k = i + 1; k <= M.
NCols(); k++) {
185 tmp -= M.
Get(i, k) * X->
Get(k);
196 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)