Commit before breaking everything
[matches/honours.git] / research / transmission_spectroscopy / universesim / src / vector.c
1 /*
2  * Vector Library
3  * By John Hodge (thePowersGang)
4  */
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <string.h>
8 #include <tVector.h>
9
10 // === CODE ===
11 char *Vector_DumpEx(tVector *Vect, int Buf)
12 {
13         static char buffers[8][2+(sizeof(tLargeInt)*2+1)*VECTOR_COMPONENTS];
14          int    i, pos = 0;
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
20         }
21         buffers[Buf][pos++] = ')';
22         buffers[Buf][pos++] = '\0';
23         return buffers[Buf];
24 }
25
26 void Vector_Zero(tVector *Vect)
27 {
28         memset(Vect, 0, sizeof(tVector));
29 }
30
31 void Vector_Set(tVector *Dest, tVector *Src)
32 {
33         memcpy(Dest, Src, sizeof(tVector));
34 }
35
36 void Vector_Abs(tVector *Vect)
37 {
38          int    i;
39         for( i = 0; i < VECTOR_COMPONENTS; i++ )
40         {
41                 if(LargeInt_GetSign(&Vect->C[i]) < 0)
42                         LargeInt_Neg(&Vect->C[i], &Vect->C[i]);
43         }
44 }
45
46 void Vector_Add(tVector *Dest, tVector *Src)
47 {
48          int    i;
49         for( i = 0; i < VECTOR_COMPONENTS; i++ )
50                 LargeInt_Add( &Dest->C[i], &Dest->C[i], &Src->C[i] );
51 }
52
53 void Vector_Sub(tVector *Dest, tVector *Src)
54 {
55          int    i;
56         for( i = 0; i < VECTOR_COMPONENTS; i++ )
57                 LargeInt_Sub( &Dest->C[i], &Dest->C[i], &Src->C[i] );
58 }
59
60 void Vector_MulScalar(tVector *Dest, tLargeInt *Scalar)
61 {
62          int    i;
63         for( i = 0; i < VECTOR_COMPONENTS; i++ )
64                 LargeInt_Mul( &Dest->C[i], NULL, &Dest->C[i], Scalar );
65 }
66
67 void Vector_DivScalar(tVector *Dest, tLargeInt *Scalar)
68 {
69          int    i;
70         for( i = 0; i < VECTOR_COMPONENTS; i++ )
71                 LargeInt_Div( &Dest->C[i], NULL, &Dest->C[i], Scalar );
72 }
73
74 void Vector_Magnitude(tLargeInt *Dest, tVector *Vector)
75 {
76         tLargeInt       tmp;
77         tLargeInt       ret;
78          int    i;
79         
80         LargeInt_Zero(&ret);
81         
82         for( i = 0; i < VECTOR_COMPONENTS; i++ ) {
83                 LargeInt_PowNative( &tmp, &Vector->C[i], 2 );
84                 LargeInt_Add( &ret, &ret, &tmp );
85         }
86         
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
92         }
93 }
94
95 void Vector_MulMatrix(tVector *Dest, tMatrix *Matrix, tVector *Vector)
96 {
97         tVector tmpV;
98         tLargeInt       tmpI, tmpI2;
99          int    i, j;
100         
101         for( i = 0; i < VECTOR_COMPONENTS; i++ )
102         {
103                 LargeInt_Zero( &tmpI );
104                 for( j = 0; j < VECTOR_COMPONENTS; j++ )
105                 {
106                         LargeInt_Mul(&tmpI2, NULL, &Matrix->M[i][j], &Vector->C[j]);
107                         LargeInt_Add(&tmpI, &tmpI, &tmpI2);
108                 }
109                 LargeInt_Set( &tmpV.C[i], &tmpI );
110         }
111         Vector_Set( Dest, &tmpV );
112 }
113
114 void Vector_DivComp(tVector *Dest, tVector *Source)
115 {
116          int    i;
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] );
120 }
121
122 void Vector_GetMax(tVector *Dest, tVector *V1, tVector *V2)
123 {
124          int    i;
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]);
128                 else
129                         LargeInt_Set(&Dest->C[i], &V2->C[i]);
130         }
131 }
132
133 void Vector_GetMin(tVector *Dest, tVector *V1, tVector *V2)
134 {
135          int    i;
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]);
139                 else
140                         LargeInt_Set(&Dest->C[i], &V2->C[i]);
141         }
142 }
143
144 void Matrix_SetIdentity(tMatrix *Mat)
145 {
146          int    i, j;
147         for( i = 0; i < VECTOR_COMPONENTS; i++ ) {
148                 for( j = 0; j < VECTOR_COMPONENTS; j++ ) {
149                         LargeInt_Zero(&Mat->M[i][j]);
150                 }
151                 LargeInt_Inc(&Mat->M[i][i]);
152                 //printf("Mat->M[%i][%i] = %s\n", i, i, LargeInt_DumpEx(&Mat->M[i][i], 0) );
153         }
154 }

UCC git Repository :: git.ucc.asn.au