Exemple #1
0
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;
        }
    }
}
Exemple #2
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);
        }
    }
}
Exemple #3
0
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;
}
Exemple #4
0
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;
        }
    }
}
Exemple #5
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;
}
Exemple #8
0
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;
        }
    }
}
Exemple #9
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];
    }
}
Exemple #11
0
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;
	}
}
int main(){
    Mat_DP data = getmat("data/data705.mat");
    Vec_DP xdata(data.nrows()), ydata(data.nrows()), zdata(data.nrows());
    mutils::isolateCol(data, 0, xdata);
    mutils::isolateCol(data, 1, ydata);
    mutils::isolateCol(data, 2, zdata);
    
    solve_system(xdata, ydata, zdata);
    auto f = [](double x, double y) {return coeffs[0]*x + coeffs[1]*y + coeffs[2];};
    
    myplot::surf_data_set container;
    myplot::surf_data surface(".");
    myplot::surf_data points("+");
    
    myplot::generate_data(f, container, surface, -10, 10, -10, 10);
    for (int i = 0; i < xdata.size(); i++) {
        points.add_point(xdata[i], ydata[i], zdata[i]);
    }
    container.push_back(points);
    cout << "coeffs = " << coeffs << endl;
    myplot::surf(container, "Parameter fitting", true);
}
int main(){
    Mat_DP A = getmat("data/A202.mat");
    Mat_DP b = getmat("data/b202.mat");
    Mat_DP invAtA(2,2);
    Mat_DP x;
    
    //A'
    Mat_DP At = !A;
    
    //A'A
    Mat_DP AtA = At * A;
    
    //A'b
    Mat_DP Atb = At * b;
    
    //inv(A'A)
    invmat(AtA, invAtA);
    
    x = invAtA * Atb;
    showmat(x);
    
    //-------------------plotting-----------------------------
    
    const char* filename1 = "data/data.tmp";
    const char* filename2 = "data/fitted.tmp";
    
    generatePlottingData(A,b,filename1);
    
    Mat_DP ydata(A.nrows(), 1);
    for (int i = 0; i < A.nrows(); i++) {
        ydata[i][0] = x[0][0] * A[i][0] + x[1][0];
    }
    generatePlottingData(A, ydata, filename2);
    
    plot(filename1, "k.", filename2, "r-", NULL);
}
Exemple #14
0
Vec_DP MATHEMATICS::math_mxmply(Mat_DP a, Vec_DP b) {
	
	int x=a.nrows(), y=b.size();
	double initial=0;
	Vec_DP c(initial, y);	

	if(x!=y) {
		cout << "Matrix dimensions are invalid for multiplication";
	return c;
	}
	else {
		int i, j;
		for(i=0; i<x; i++) {
			for(j=0; j<y; j++) {
				c[i]+=a[i][j]*b[j];
				}
		}
	return c;
	}
}
int main(){
    Mat_DP dirichlet = getmat("data/dirichlet1.mat");
    Vec_DP b = getVec(), ans(dirichlet.nrows());
    SVDsolve(dirichlet, b, ans);
    print_results("answer", ans);
}
Vec_DP getVec(){
    Mat_DP temp = getmat("data/b1.mat");
    Vec_DP b(temp.nrows());
    mutils::isolateCol(temp, 0, b);
    return b;
}
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);
}
Vec_DP getVec(const char *fname){
    Mat_DP temp = getmat(fname);
    Vec_DP b(temp.nrows());
    mutils::isolateCol(temp, 0, b);
    return b;
}