59 |
|
p3a[1] += m4[3][1]; |
60 |
|
p3a[2] += m4[3][2]; |
61 |
|
} |
62 |
– |
|
63 |
– |
|
64 |
– |
/* |
65 |
– |
* invmat - computes the inverse of mat into inverse. Returns 1 |
66 |
– |
* if there exists an inverse, 0 otherwise. It uses Gaussian Elimination |
67 |
– |
* method with partial pivoting. |
68 |
– |
*/ |
69 |
– |
|
70 |
– |
invmat(inverse,mat) |
71 |
– |
MAT4 mat, inverse; |
72 |
– |
{ |
73 |
– |
#define SWAP(a,b,t) (t=a,a=b,b=t) |
74 |
– |
#define ABS(x) (x>=0?x:-(x)) |
75 |
– |
|
76 |
– |
register int i,j,k; |
77 |
– |
register double temp; |
78 |
– |
|
79 |
– |
copymat4(m4tmp, mat); |
80 |
– |
setident4(inverse); |
81 |
– |
|
82 |
– |
for(i = 0; i < 4; i++) { |
83 |
– |
/* Look for row with largest pivot and swap rows */ |
84 |
– |
temp = 0; j = -1; |
85 |
– |
for(k = i; k < 4; k++) |
86 |
– |
if(ABS(m4tmp[k][i]) > temp) { |
87 |
– |
temp = ABS(m4tmp[k][i]); |
88 |
– |
j = k; |
89 |
– |
} |
90 |
– |
if(j == -1) /* No replacing row -> no inverse */ |
91 |
– |
return(0); |
92 |
– |
if (j != i) |
93 |
– |
for(k = 0; k < 4; k++) { |
94 |
– |
SWAP(m4tmp[i][k],m4tmp[j][k],temp); |
95 |
– |
SWAP(inverse[i][k],inverse[j][k],temp); |
96 |
– |
} |
97 |
– |
|
98 |
– |
temp = m4tmp[i][i]; |
99 |
– |
for(k = 0; k < 4; k++) { |
100 |
– |
m4tmp[i][k] /= temp; |
101 |
– |
inverse[i][k] /= temp; |
102 |
– |
} |
103 |
– |
for(j = 0; j < 4; j++) { |
104 |
– |
if(j != i) { |
105 |
– |
temp = m4tmp[j][i]; |
106 |
– |
for(k = 0; k < 4; k++) { |
107 |
– |
m4tmp[j][k] -= m4tmp[i][k]*temp; |
108 |
– |
inverse[j][k] -= inverse[i][k]*temp; |
109 |
– |
} |
110 |
– |
} |
111 |
– |
} |
112 |
– |
} |
113 |
– |
return(1); |
114 |
– |
|
115 |
– |
#undef ABS |
116 |
– |
#undef SWAP |
117 |
– |
} |