Commit before breaking everything
[matches/honours.git] / research / transmission_spectroscopy / universesim / src / vector.c
diff --git a/research/transmission_spectroscopy/universesim/src/vector.c b/research/transmission_spectroscopy/universesim/src/vector.c
new file mode 100644 (file)
index 0000000..b998fe3
--- /dev/null
@@ -0,0 +1,154 @@
+/*
+ * Vector Library
+ * By John Hodge (thePowersGang)
+ */
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <tVector.h>
+
+// === CODE ===
+char *Vector_DumpEx(tVector *Vect, int Buf)
+{
+       static char buffers[8][2+(sizeof(tLargeInt)*2+1)*VECTOR_COMPONENTS];
+        int    i, pos = 0;
+       buffers[Buf][pos++] = '(';
+       for( i = 0; i < VECTOR_COMPONENTS; i++ ) {
+               if( i ) buffers[Buf][pos++] = ',';
+               strcpy(&buffers[Buf][pos], LargeInt_DumpEx(&Vect->C[i], 7));
+               pos += sizeof(tLargeInt)*2;     // Num of hex chars
+       }
+       buffers[Buf][pos++] = ')';
+       buffers[Buf][pos++] = '\0';
+       return buffers[Buf];
+}
+
+void Vector_Zero(tVector *Vect)
+{
+       memset(Vect, 0, sizeof(tVector));
+}
+
+void Vector_Set(tVector *Dest, tVector *Src)
+{
+       memcpy(Dest, Src, sizeof(tVector));
+}
+
+void Vector_Abs(tVector *Vect)
+{
+        int    i;
+       for( i = 0; i < VECTOR_COMPONENTS; i++ )
+       {
+               if(LargeInt_GetSign(&Vect->C[i]) < 0)
+                       LargeInt_Neg(&Vect->C[i], &Vect->C[i]);
+       }
+}
+
+void Vector_Add(tVector *Dest, tVector *Src)
+{
+        int    i;
+       for( i = 0; i < VECTOR_COMPONENTS; i++ )
+               LargeInt_Add( &Dest->C[i], &Dest->C[i], &Src->C[i] );
+}
+
+void Vector_Sub(tVector *Dest, tVector *Src)
+{
+        int    i;
+       for( i = 0; i < VECTOR_COMPONENTS; i++ )
+               LargeInt_Sub( &Dest->C[i], &Dest->C[i], &Src->C[i] );
+}
+
+void Vector_MulScalar(tVector *Dest, tLargeInt *Scalar)
+{
+        int    i;
+       for( i = 0; i < VECTOR_COMPONENTS; i++ )
+               LargeInt_Mul( &Dest->C[i], NULL, &Dest->C[i], Scalar );
+}
+
+void Vector_DivScalar(tVector *Dest, tLargeInt *Scalar)
+{
+        int    i;
+       for( i = 0; i < VECTOR_COMPONENTS; i++ )
+               LargeInt_Div( &Dest->C[i], NULL, &Dest->C[i], Scalar );
+}
+
+void Vector_Magnitude(tLargeInt *Dest, tVector *Vector)
+{
+       tLargeInt       tmp;
+       tLargeInt       ret;
+        int    i;
+       
+       LargeInt_Zero(&ret);
+       
+       for( i = 0; i < VECTOR_COMPONENTS; i++ ) {
+               LargeInt_PowNative( &tmp, &Vector->C[i], 2 );
+               LargeInt_Add( &ret, &ret, &tmp );
+       }
+       
+       LargeInt_RootNative( Dest, &ret, 2 );
+       if(LargeInt_GetSign(Dest) == 0) {
+               //fprintf(stderr, "WARNING: Vector %s has no magnitude\n", Vector_DumpEx(Vector, 0));
+               //fprintf(stderr, "WARNING: Vector has no magnitude\n");
+               LargeInt_Inc(Dest);     // Make the magnitude one to appease divisions later down the line
+       }
+}
+
+void Vector_MulMatrix(tVector *Dest, tMatrix *Matrix, tVector *Vector)
+{
+       tVector tmpV;
+       tLargeInt       tmpI, tmpI2;
+        int    i, j;
+       
+       for( i = 0; i < VECTOR_COMPONENTS; i++ )
+       {
+               LargeInt_Zero( &tmpI );
+               for( j = 0; j < VECTOR_COMPONENTS; j++ )
+               {
+                       LargeInt_Mul(&tmpI2, NULL, &Matrix->M[i][j], &Vector->C[j]);
+                       LargeInt_Add(&tmpI, &tmpI, &tmpI2);
+               }
+               LargeInt_Set( &tmpV.C[i], &tmpI );
+       }
+       Vector_Set( Dest, &tmpV );
+}
+
+void Vector_DivComp(tVector *Dest, tVector *Source)
+{
+        int    i;
+       //printf("Vector_DivComp: Dest=%s, Source=%s\n", Vector_DumpEx(Dest, 0), Vector_DumpEx(Source, 1));
+       for( i = 0; i < VECTOR_COMPONENTS; i++ )
+               LargeInt_Div( &Dest->C[i], NULL, &Dest->C[i], &Source->C[i] );
+}
+
+void Vector_GetMax(tVector *Dest, tVector *V1, tVector *V2)
+{
+        int    i;
+       for( i = 0; i < VECTOR_COMPONENTS; i++ ) {
+               if( LargeInt_Compare(&V1->C[i], &V2->C[i]) > 0 )
+                       LargeInt_Set(&Dest->C[i], &V1->C[i]);
+               else
+                       LargeInt_Set(&Dest->C[i], &V2->C[i]);
+       }
+}
+
+void Vector_GetMin(tVector *Dest, tVector *V1, tVector *V2)
+{
+        int    i;
+       for( i = 0; i < VECTOR_COMPONENTS; i++ ) {
+               if( LargeInt_Compare(&V1->C[i], &V2->C[i]) < 0 )
+                       LargeInt_Set(&Dest->C[i], &V1->C[i]);
+               else
+                       LargeInt_Set(&Dest->C[i], &V2->C[i]);
+       }
+}
+
+void Matrix_SetIdentity(tMatrix *Mat)
+{
+        int    i, j;
+       for( i = 0; i < VECTOR_COMPONENTS; i++ ) {
+               for( j = 0; j < VECTOR_COMPONENTS; j++ ) {
+                       LargeInt_Zero(&Mat->M[i][j]);
+               }
+               LargeInt_Inc(&Mat->M[i][i]);
+               //printf("Mat->M[%i][%i] = %s\n", i, i, LargeInt_DumpEx(&Mat->M[i][i], 0) );
+       }
+}

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