48 return sqrt( v[0]*v[0]+v[1]*v[1]+v[2]*v[2] );
55 return u[0]*v[0]+u[1]*v[1]+u[2]*v[2];
65 xprintf(
Warn,
"Normalization of nearly zero vector.\n");
67 u[0] /= l; u[1] /= l; u[2] /= l;
74 u[ 0 ] *= k; u[ 1 ] *= k; u[ 2 ] *= k;
85 x[ 0 ] = u[ 1 ] * v[ 2 ] - u[ 2 ] * v[ 1 ];
86 x[ 1 ] = u[ 2 ] * v[ 0 ] - u[ 0 ] * v[ 2 ];
87 x[ 2 ] = u[ 0 ] * v[ 1 ] - u[ 1 ] * v[ 0 ];
94 x[ 0 ] = u[ 0 ] - v[ 0 ];
95 x[ 1 ] = u[ 1 ] - v[ 1 ];
96 x[ 2 ] = u[ 2 ] - v[ 2 ];
107 return a[0][0]*
SUBDET2(1,2,1,2)
119 *b=1/(*a);
return (*a);
139 if ( fabs(Det) <
NUM_ZERO )
return Det;
141 b[1][0]=-a[1][0]/Det;
142 b[0][1]=-a[0][1]/Det;
159 Det=a[0][0]*b[0][0]+a[0][1]*b[1][0]+a[0][2]*b[2][0];
160 if ( fabs(Det)<
NUM_ZERO)
return Det;
161 b[0][0]/=Det; b[1][0]/=Det; b[2][0]/=Det;
179 double u[6], l[6], Det;
197 b[0][0]=+a[1][1]*l[5]-a[1][2]*l[4]+a[1][3]*l[3];
198 b[1][0]=-a[1][0]*l[5]+a[1][2]*l[2]-a[1][3]*l[1];
199 b[2][0]=+a[1][0]*l[4]-a[1][1]*l[2]+a[1][3]*l[0];
200 b[3][0]=-a[1][0]*l[3]+a[1][1]*l[1]-a[1][2]*l[0];
202 Det=a[0][0]*b[0][0]+a[0][1]*b[1][0]+a[0][2]*b[2][0]+a[0][3]*b[3][0];
203 if ( fabs(Det)<
NUM_ZERO)
return Det;
204 b[0][0]/=Det; b[1][0]/=Det; b[2][0]/=Det; b[3][0]/=Det;
206 b[0][1]=(-a[0][1]*l[5]+a[0][2]*l[4]-a[0][3]*l[3])/Det;
207 b[1][1]=(+a[0][0]*l[5]-a[0][2]*l[2]+a[0][3]*l[1])/Det;
208 b[2][1]=(-a[0][0]*l[4]+a[0][1]*l[2]-a[0][3]*l[0])/Det;
209 b[3][1]=(+a[0][0]*l[3]-a[0][1]*l[1]+a[0][2]*l[0])/Det;
211 b[0][2]=(+a[3][1]*u[5]-a[3][2]*u[4]+a[3][3]*u[3])/Det;
212 b[1][2]=(-a[3][0]*u[5]+a[3][2]*u[2]-a[3][3]*u[1])/Det;
213 b[2][2]=(+a[3][0]*u[4]-a[3][1]*u[2]+a[3][3]*u[0])/Det;
214 b[3][2]=(-a[3][0]*u[3]+a[3][1]*u[1]-a[3][2]*u[0])/Det;
216 b[0][3]=(-a[2][1]*u[5]+a[2][2]*u[4]-a[2][3]*u[3])/Det;
217 b[1][3]=(+a[2][0]*u[5]-a[2][2]*u[2]+a[2][3]*u[1])/Det;
218 b[2][3]=(-a[2][0]*u[4]+a[2][1]*u[2]-a[2][3]*u[0])/Det;
219 b[3][3]=(+a[2][0]*u[3]-a[2][1]*u[1]+a[2][2]*u[0])/Det;
228 printf(
"matrix %d x %d :\n",size,size);
229 for (i=0;i<size;i++) {
230 for(j=0;j<size;j++) printf(
"%f ",mtx[i*size+j]);
244 for(row=0;row<n-1;row++) {
246 Div=a[row][row];a[row][row]=1;
247 for(col=0;col<n;col++) a[row][col]/=Div;
249 for(subrow=row+1;subrow<n;subrow++) {
250 Mult=a[subrow][row];a[subrow][row]=0;
251 for(col=0;col<n;col++) a[subrow][col]-=a[row][col]*Mult;
255 Div=a[row][row];a[row][row]=1;
256 for(col=0;col<n;col++) a[row][col]/=Div;
260 for(subrow=row-1;subrow>=0;subrow--) {
261 Mult=a[subrow][row];a[subrow][row]=0;
262 for(col=0;col<n;col++) a[subrow][col]-=a[row][col]*Mult;
272 int gauss(
double *A,
double *B,
int s,
double *R)
274 int res=1,i,j,k,size;
278 M = (
double *)
xmalloc( (size-1) * size *
sizeof(double));
282 M[i*(size)+j]=A[i*s+j];
285 for(i=0;i<size-1;i++)
289 for(j=i+1;j<size-1;j++)
296 M[j*size+k]=M[i*size+k];
305 if (fabs(max)<fabs(M[i*size+k])) max=fabs(M[i*size+k]);
309 for(j=i+1;j<size-1;j++)
313 koef=M[i*size+i]/M[j*size+i];
315 if((M[i*size+i]>=0 && M[j*size+i]>=0) ||
316 (M[i*size+i]<0 && M[j*size+i]<0)) koef*=-1;
318 M[j*size+k]=M[j*size+k]*koef + M[i*size+k];
323 for(i=size-1-1;i>=0;i--)
326 for(j=i+1;j<size-1;j++)
328 koef=koef+R[j]*M[i*size+j];
330 if (!(
DBL_EQ(M[i*size+i],0)))
331 R[i]=(M[i*size + size-1]-koef)/M[i*size+i];
334 if (koef==0 && M[i*size + size-1]==0)
357 return fi * 180 /
M_PI;
365 double *B,
int rb,
int cb,
double *X)
368 ASSERT(!(ca != rb),
"Matrix A has different number of columns than matrix B rows in the function matrix_x_matrix()\n");
369 for (i = 0; i < ra; i++)
370 for (j = 0; j < cb; j++)
373 for (k = 0; k < rb; k++)
374 X[ i * cb + j] += A[ i * ca + k] * B[ k * cb + j];
void vector_product(double u[], double v[], double x[])
static double Inverse3(SmallMtx3 a, SmallMtx3 b)
#define SUBDET2(i, j, k, l)
void matrix_x_matrix(double *A, int ra, int ca, double *B, int rb, int cb, double *X)
void normalize_vector(double u[])
double vector_length(double v[])
void scale_vector(double u[], double k)
Global macros to enhance readability and debugging, general constants.
static double Inverse2(SmallMtx2 a, SmallMtx2 b)
double scalar_product(double u[], double v[])
void MatInverse(int n, double **a)
void PrintSmallMatrix(double *mtx, int size)
#define NUM_ZERO
Numerical helpers.
void * xmalloc(size_t size)
Memory allocation with checking.
double MatrixInverse(double *a, double *b, int size)
void vector_difference(double u[], double v[], double x[])
int gauss(double *A, double *B, int s, double *R)
static double Inverse4(SmallMtx4 a, SmallMtx4 b)
double get_vectors_angle(double u[3], double v[3])