void zero_under_diag(Mat_DP& a) { for (int i = 0; i < a.nrows(); i++) { for (int j = 0; j < min(i, a.ncols()); j++) { a[i][j] = 0; } } }
void hilb(Mat_DP& A) { for (int i = 0; i < A.nrows(); i++) { for (int j = 0; j < A.ncols(); j++) { A[i][j] = 1.0 / (i + j + 1); } } }
bool is_tridiagonal(Mat_DP& a, double tolerance) { for (int i = 0; i < a.nrows(); i++) { for (int j = 0; j < a.ncols(); j++) { if (abs(i - j) > 1 && abs(a[i][j]) > tolerance) return false; } } return true; }
void random_tridiagonal(Mat_DP& a) { for (int i = 0; i < a.nrows(); i++) { for (int j = 0; j < a.ncols(); j++) { if (abs(i - j) <= 1) a[i][j] = rdm(minValue, maxValue); else a[i][j] = 0; } } }
bool is_upper_triangular(Mat_DP const& a, double tolerance) { int n = a.nrows(); int m = a.ncols(); for (int i = 0; i < n; i++) { for (int j = 0; j < min(i, m); j++) { if (abs(a[i][j]) > tolerance) { cout << "For a " << n << " x " << m << " matrix: value at (" << i << ", " << j << ") = " << a[i][j] << endl; return false; } } } return true; }
Mat_DP MATHEMATICS::math_transpose(Mat_DP m) { int x=m.nrows(), y=m.ncols(); Mat_DP transm(y,x); for(int i=0; i<y; i++) { for(int j=0; j<x; j++) { transm[i][j]=m[j][i]; } } return transm; }
void io_dataoutput(string filename, Mat_DP m) { ofstream outfile(filename.c_str()); // outfile << m.nrows() << endl; for (int i=0; i<m.nrows(); i++) { for (int j=0; j<m.ncols(); j++) { outfile << m[i][j] << " "; } outfile << endl; } outfile.close(); return; }
void random_upper_triangular(Mat_DP& a) { // could be done like this, but we do it more efficiently // ranmat2(a, minValue, maxValue); // zero_under_diag(a); for (int i = 0; i < a.nrows(); i++) { for (int j = 0; j < a.ncols(); j++) { if (i <= j) a[i][j] = rdm(minValue, maxValue); else a[i][j] = 0; } } }
double MATHEMATICS::math_det3(Mat_DP m) { int x=m.nrows(), y=m.ncols(); double a=0; Mat_DP b0(2,2), b1(2,2), b2(2,2); if (x!=3||y!=3) { cout << "Input matrix is not 3x3"; } else { b0[0][0]=m[1][1], b0[0][1]=m[1][2], b0[1][0]=m[2][1], b0[1][1]=m[2][2]; b1[0][0]=m[1][0], b1[0][1]=m[1][2], b1[1][0]=m[2][0], b1[1][1]=m[2][2]; b2[0][0]=m[1][0], b2[0][1]=m[1][1], b2[1][0]=m[2][0], b2[1][1]=m[2][1]; a=m[0][0]*MATHEMATICS::math_det2(b0)-m[0][1]*MATHEMATICS::math_det2(b1)+m[0][2]*MATHEMATICS::math_det2(b2); } return a; }
void GSsolve_iter(Mat_DP &a, Vec_DP &b, Vec_DP &x, Vec_DP &xold) { int i, j, rows = a.nrows(); if ((rows != a.ncols()) || (rows != b.size()) || (rows != x.size()) || (rows != xold.size())) { cout << "Argument error in GaussSeidel_iter\n" << endl; abort(); } double s; for (i = 0; i < rows; i++) { s = 0; for (j = 0; j < rows; j++) { s += (j <= i - 1) ? a[i][j] * x[j] : (j >= i + 1) ? a[i][j] * xold[j] : 0; } xold[i] = x[i]; x[i] = (b[i] - s) / a[i][i]; } }
Mat_DP MATHEMATICS::math_mxmply(Mat_DP a, Mat_DP b) { int x=a.nrows(), y=b.ncols(); double initial=0; Mat_DP c(initial, x, y); if(x!=y) { cout << "Matrix dimensions are invalid for multiplication"; return c; } else { int i, j, k; for(i=0; i<x; i++) { for(j=0; j<y; j++) { for(k=0; k<x; k++) { c[i][j]+=a[i][k]*b[k][j]; } } } return c; } }
void pinv_solve(Mat_DP &A, Vec_DP &x, Vec_DP &b) { Mat_DP pinvA(A.ncols(), A.nrows()); pseudoinv(A, pinvA); matvecmul(pinvA, b, x); }