3 * By John Hodge (thePowersGang)
11 char *Vector_DumpEx(tVector *Vect, int Buf)
13 static char buffers[8][2+(sizeof(tLargeInt)*2+1)*VECTOR_COMPONENTS];
15 buffers[Buf][pos++] = '(';
16 for( i = 0; i < VECTOR_COMPONENTS; i++ ) {
17 if( i ) buffers[Buf][pos++] = ',';
18 strcpy(&buffers[Buf][pos], LargeInt_DumpEx(&Vect->C[i], 7));
19 pos += sizeof(tLargeInt)*2; // Num of hex chars
21 buffers[Buf][pos++] = ')';
22 buffers[Buf][pos++] = '\0';
26 void Vector_Zero(tVector *Vect)
28 memset(Vect, 0, sizeof(tVector));
31 void Vector_Set(tVector *Dest, tVector *Src)
33 memcpy(Dest, Src, sizeof(tVector));
36 void Vector_Abs(tVector *Vect)
39 for( i = 0; i < VECTOR_COMPONENTS; i++ )
41 if(LargeInt_GetSign(&Vect->C[i]) < 0)
42 LargeInt_Neg(&Vect->C[i], &Vect->C[i]);
46 void Vector_Add(tVector *Dest, tVector *Src)
49 for( i = 0; i < VECTOR_COMPONENTS; i++ )
50 LargeInt_Add( &Dest->C[i], &Dest->C[i], &Src->C[i] );
53 void Vector_Sub(tVector *Dest, tVector *Src)
56 for( i = 0; i < VECTOR_COMPONENTS; i++ )
57 LargeInt_Sub( &Dest->C[i], &Dest->C[i], &Src->C[i] );
60 void Vector_MulScalar(tVector *Dest, tLargeInt *Scalar)
63 for( i = 0; i < VECTOR_COMPONENTS; i++ )
64 LargeInt_Mul( &Dest->C[i], NULL, &Dest->C[i], Scalar );
67 void Vector_DivScalar(tVector *Dest, tLargeInt *Scalar)
70 for( i = 0; i < VECTOR_COMPONENTS; i++ )
71 LargeInt_Div( &Dest->C[i], NULL, &Dest->C[i], Scalar );
74 void Vector_Magnitude(tLargeInt *Dest, tVector *Vector)
82 for( i = 0; i < VECTOR_COMPONENTS; i++ ) {
83 LargeInt_PowNative( &tmp, &Vector->C[i], 2 );
84 LargeInt_Add( &ret, &ret, &tmp );
87 LargeInt_RootNative( Dest, &ret, 2 );
88 if(LargeInt_GetSign(Dest) == 0) {
89 //fprintf(stderr, "WARNING: Vector %s has no magnitude\n", Vector_DumpEx(Vector, 0));
90 //fprintf(stderr, "WARNING: Vector has no magnitude\n");
91 LargeInt_Inc(Dest); // Make the magnitude one to appease divisions later down the line
95 void Vector_MulMatrix(tVector *Dest, tMatrix *Matrix, tVector *Vector)
98 tLargeInt tmpI, tmpI2;
101 for( i = 0; i < VECTOR_COMPONENTS; i++ )
103 LargeInt_Zero( &tmpI );
104 for( j = 0; j < VECTOR_COMPONENTS; j++ )
106 LargeInt_Mul(&tmpI2, NULL, &Matrix->M[i][j], &Vector->C[j]);
107 LargeInt_Add(&tmpI, &tmpI, &tmpI2);
109 LargeInt_Set( &tmpV.C[i], &tmpI );
111 Vector_Set( Dest, &tmpV );
114 void Vector_DivComp(tVector *Dest, tVector *Source)
117 //printf("Vector_DivComp: Dest=%s, Source=%s\n", Vector_DumpEx(Dest, 0), Vector_DumpEx(Source, 1));
118 for( i = 0; i < VECTOR_COMPONENTS; i++ )
119 LargeInt_Div( &Dest->C[i], NULL, &Dest->C[i], &Source->C[i] );
122 void Vector_GetMax(tVector *Dest, tVector *V1, tVector *V2)
125 for( i = 0; i < VECTOR_COMPONENTS; i++ ) {
126 if( LargeInt_Compare(&V1->C[i], &V2->C[i]) > 0 )
127 LargeInt_Set(&Dest->C[i], &V1->C[i]);
129 LargeInt_Set(&Dest->C[i], &V2->C[i]);
133 void Vector_GetMin(tVector *Dest, tVector *V1, tVector *V2)
136 for( i = 0; i < VECTOR_COMPONENTS; i++ ) {
137 if( LargeInt_Compare(&V1->C[i], &V2->C[i]) < 0 )
138 LargeInt_Set(&Dest->C[i], &V1->C[i]);
140 LargeInt_Set(&Dest->C[i], &V2->C[i]);
144 void Matrix_SetIdentity(tMatrix *Mat)
147 for( i = 0; i < VECTOR_COMPONENTS; i++ ) {
148 for( j = 0; j < VECTOR_COMPONENTS; j++ ) {
149 LargeInt_Zero(&Mat->M[i][j]);
151 LargeInt_Inc(&Mat->M[i][i]);
152 //printf("Mat->M[%i][%i] = %s\n", i, i, LargeInt_DumpEx(&Mat->M[i][i], 0) );