samedi 2 juillet 2016

How to implement a matrix formula more efficient?

Recently, I would like to use C to implement a matrix formula, as shown below:

enter image description here

where, $P_{i,j}={x_{i,j},y_{i,j},z_{i,j}}$

Here is my C-like algorithm:

SurfacePoint(i,j,p,q,Nu,Nv,P){
    uidx = i - p;
    S = 0.0;
    for(l=0; l<=q; l++){
        temp = 0.0;
        vidx = j - q + l
        for(k=0; k<=p; k++){
            temp = temp + Nu[k]*P[uidx+k][vidx];
        }
        S = S + Nv[l]*temp;
    }
    return S;
}

However, owing to P is a vector,rather than a real number, so I need to use a array temp and array S to restore the result

Implementation

uidx = i - p;
for(l = 0; l <= q; l++){
    temp[0] = 0;
    temp[1] = 0;
    temp[2] = 0;
    vidx = j - q + l;
    for(k = 0; k <= p; k++){
        temp[0] = temp[0] + Nu[k]*P[uidx+k][vidx][0];
        temp[1] = temp[1] + Nu[k]*P[uidx+k][vidx][1];
        temp[2] = temp[2] + Nu[k]*P[uidx+k][vidx][2];
    }                   
    S[0] = S[0] + Nv[l]*temp[0];
    S[1] = S[1] + Nv[l]*temp[1];
    S[2] = S[2] + Nv[l]*temp[2];
}

For the C, I am is a newcomer, so I would like to know:

  • Is there more efficient method to implement it?

Aucun commentaire:

Enregistrer un commentaire