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;
}

猜你喜欢

转载自www.cnblogs.com/bingoyes/p/10702528.html