bingoyes' tiny dream

Gauss Elimination

bool Gauss(){
    int now=1,nxt;
	double t;
    R(i,1,n){
    //enumerate the column
        for(nxt=now;nxt<=n;++nxt)
			if(fabs(a[nxt][i])>eps)break;
	//find a nonzero element in the ith row as 'nxt'
        if(now!=nxt)
            R(j,1,n+1)
                swap(a[nxt][j],a[now][j]);
	//exchange the two lines
        t=a[now][i];
        R(j,1,n+1)
            a[now][j]/=t;
        R(j,1,n)
            if(j!=now){
                t=a[j][i];
                R(k,1,n+1)
                    a[j][k]-=t*a[now][k];
            }
        //elimination each row
        ++now;
        //this column is not useless, goto the next one.
    }
    R(i,now,n)
    	if(fabs(a[i][n+1])>eps)
			return 0;
    return 1;
}
posted @ 2019-04-13 19:36  邱涵的秘密基地  阅读(170)  评论(0编辑  收藏  举报