diff --git a/ChrisTest/C_Implementations/ChrisMatrix.h b/ChrisTest/C_Implementations/ChrisMatrix.h new file mode 100644 index 0000000..af0134e --- /dev/null +++ b/ChrisTest/C_Implementations/ChrisMatrix.h @@ -0,0 +1,84 @@ +#include + +/********************* + * Matrix Transpose + * + * No real concept of vector transpose + * in this kraken implementation + * *******************/ +void transpose(int m, int n, double** A) +{ + int i, j; + + /****************************** + * Allocate memory for tmp array + * ***************************/ + + double** A_trans; + A_trans = malloc(sizeof(double*) * m); + + for(i = 0; i < m; i++) + { + A_trans[i] = malloc(sizeof(double) * n); + } + + /********************* + * Copy A into tmp + ********************/ + for(i = 0; i < m; i++) + { + for(j = 0; j < n; j++) + { + A_trans[i][j] = A[i][j]; + }//end j loop + }//end i loop + + /*************** + * Transpose operation + ***************/ + for(i = 0; i < m; i++) + { + for(j = 0; j < n; j++) + { + A[i][j] = A_trans[j][i]; + }//end j loop + }//end i loop + + /********** + * Free Memory + * *******/ + for(i = 0; i < m; i++) + free(A_trans[i]); + free(A_trans); + +}//end transpose function + +void multiply(int m, int n, int q, double** A, double** B, double**C) +{ + //Kraken: check if C is empty, then allocate memory + + + + return; +} + +void printMatrix(int m, int n, double** A) +{ + int i, j; + + for(i = 0; i < m; i++) + { + for(j = 0; j < n; j++) + { + printf("%f ",A[i][j]); + }//end j loop + printf("\n"); + }//end i loop + printf("\n"); +}//end print function + + + + + + diff --git a/ChrisTest/C_Implementations/main.c b/ChrisTest/C_Implementations/main.c new file mode 100644 index 0000000..be9c540 --- /dev/null +++ b/ChrisTest/C_Implementations/main.c @@ -0,0 +1,31 @@ +#include +#include "ChrisMatrix.h" + +int main(int argc, char** argv) +{ + double** A; + int m = 3; + int n = 3; + int i, j; + + A = malloc(sizeof(double*) * m); + for(i = 0; i < m; i++) + A[i] = malloc(sizeof(double) * n); + + int k = 0; + for(i = 0; i < m; i++) + { + for(j = 0; j < n; j++) + { + A[i][j] = k; + k++; + }//end j loop + }//end i loop + + printMatrix(m, n, A); + transpose(m,n,A); + printMatrix(m,n,A); + printf("Hello World\n"); + + return 0; +} diff --git a/ChrisTest/ChrisMatrix/ChrisMatrix.krak b/ChrisTest/ChrisMatrix/ChrisMatrix.krak new file mode 100644 index 0000000..78e8a89 --- /dev/null +++ b/ChrisTest/ChrisMatrix/ChrisMatrix.krak @@ -0,0 +1,37 @@ +import vector:*; +import io:*; + +typedef ChrisMatrix(Destructable) +{ + /*************** + * Member Data + **************/ + |double**| data; + |int| rowSize; + |int| columnSize; + + /************************** + * Constructor/Destructor + *************************/ + |double***| construct() + { + rowSize = 0; + colSize = 0; + //data = NULL; Will this work? + return this; + } + + |bool| Initialize(|int| m, |int| n) + { + data = malloc(sizeof(double*)*m); + for(|int| i = 0; i < m; i++) + { + data[i] = malloc(sizeof(double)*n); + } + + rowSize = m; + colSize = n; + + }//end itialize function + +}//end Matrix class diff --git a/ChrisTest/ChrisVec.krak b/ChrisTest/ChrisVec.krak deleted file mode 100644 index c7ed622..0000000 --- a/ChrisTest/ChrisVec.krak +++ /dev/null @@ -1,50 +0,0 @@ -/* - ../build/kraken InputFile.krak ../krakGrammer(tab complete) OutputFile -*/ -import vector:*; -import io:*; - -/***************************** - * Functions - ****************************/ - -|double| dot(|vector| a, |vector| b) -{ - if(a.size < b.size) - { - println("Error: Vectors not of same size"); - print("Vector a is of size: "); - println(a.size); - print("Vector b is of size: "); - println(b.size); - println("Did you know that Grover Cleveland served two seperate terms as President of the United States, four years apart?"); - } - - |double| ans = 0; - for(|int| i = 0; i < lesser(a.size, b.size); i++;) - { - ans = ans + a.at(i) * b.at(i); - } - - println(ans); - - return ans; -} - - - - - - - - - - - - - - - - - - diff --git a/ChrisTest/ChrisVec/ChrisVec.krak b/ChrisTest/ChrisVec/ChrisVec.krak new file mode 100644 index 0000000..40dc52b --- /dev/null +++ b/ChrisTest/ChrisVec/ChrisVec.krak @@ -0,0 +1,78 @@ + +import vector:*; +import io:*; + +/***************************** + * Functions + ****************************/ + +/*************************** + * dot(vec u, vec v) + * + * This returns the dot product + * of two vectors. This is equivalent + * to multiplying the components of the + * vector element by element, and then summing + * the resulting vector. + * + ***************************/ + +template |T| dot(|vector| u, |vector| v) +{ + if(u.size < v.size) + { + println("Error: Vectors not of same size"); + print("Vector a is of size: "); + println(u.size); + print("Vector b is of size: "); + println(v.size); + } + + |T| ans = 0; + + for(|int| i = 0; i < lesser(u.size, v.size); i++;) + { + ans = ans + u.at(i) * v.at(i); + } + + println(ans); + + return ans; +}//end dot function + +/*************** + * Norm2(vec r) + * + * This takes a vector, + * and finds the norm squared, or + * the sum of all the components squared. + * + * This is equivalent to dot(r,r) + * + **************/ +template |J| norm2(|vector| r) +{ + return(dot(r,r)); +}//end norm function + + + + + + + + + + + + + + + + + + + + + + diff --git a/ChrisTest/ChrisVecTest.krak b/ChrisTest/ChrisVec/ChrisVecTest.krak similarity index 63% rename from ChrisTest/ChrisVecTest.krak rename to ChrisTest/ChrisVec/ChrisVecTest.krak index a5af09d..73b161b 100644 --- a/ChrisTest/ChrisVecTest.krak +++ b/ChrisTest/ChrisVec/ChrisVecTest.krak @@ -4,6 +4,7 @@ import vector:*; import ChrisVec:*; +import io:*; |int| main() { @@ -22,7 +23,14 @@ import ChrisVec:*; bVec.addEnd(12.3); - |double| dotProd = dot(aVec, bVec); + |double| dotProd = dot(aVec, bVec); + + println("Dot Product has completed"); + println(); + println(); + |double| NormSquared = norm2(aVec); + println("Norm has completed"); + return 0; } diff --git a/ChrisTest/ErrorLog.txt b/ChrisTest/ErrorLog.txt index ddeec13..cb2650d 100644 --- a/ChrisTest/ErrorLog.txt +++ b/ChrisTest/ErrorLog.txt @@ -1,2 +1,5 @@ -else statements don't work -!= doesn't work +If two directories away from /GitHub/kraken/, can't find grammer file +error with templated functions and templates + +else statements don't work FIXED +!= doesn't work FIXED diff --git a/ChrisTest/HelloWorld.krak b/ChrisTest/HelloWorld/HelloWorld.krak similarity index 100% rename from ChrisTest/HelloWorld.krak rename to ChrisTest/HelloWorld/HelloWorld.krak diff --git a/ChrisTest/SupressOut.sh b/ChrisTest/SupressOut.sh deleted file mode 100755 index eee017e..0000000 --- a/ChrisTest/SupressOut.sh +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/bash -rm *krakout* diff --git a/ChrisTest/TrigTest.krak b/ChrisTest/TrigTest.krak new file mode 100644 index 0000000..4674134 --- /dev/null +++ b/ChrisTest/TrigTest.krak @@ -0,0 +1,39 @@ +import io:*; +import math:*; + +|int| main() +{ + |double| ans; + |double| STD_PI = 4.0*atan(1.0); + + /* + ans = 4.0*atan(1.0); + print("atan = "); + println(ans); + + ans = atan2(0.0,1.0); + print("atan2 = "); + println(ans); + + ans = acos(1.0); + print("acos = "); + println(ans); + + ans = 2.0 * asin(1.0); + print("asin = "); + println(ans); + + ans = tan(STD_PI / 4.0); + print("tan = "); + println(ans); + + ans = cos(0.0); + print("cos = "); + println(ans); + + ans = sin(0.0); + print("sin = "); + println(ans); + */ + return 0; +} diff --git a/stdlib/.math.krak.un~ b/stdlib/.math.krak.un~ new file mode 100644 index 0000000..0745549 Binary files /dev/null and b/stdlib/.math.krak.un~ differ diff --git a/stdlib/math.krak b/stdlib/math.krak index 0652e47..d59cd17 100644 --- a/stdlib/math.krak +++ b/stdlib/math.krak @@ -1,9 +1,109 @@ - -|int| NotPi = 3; -|float| Pi = 3.141592654; +__if_comp__ __C__ __simple_passthrough__ """ + #include +""" |int| fibanacci(|int| num) { if (num < 2) return 1; return fibanacci(num-1) + fibanacci(num-2); } + +/********************* + * Trig Functions + ********************/ + +|double| atan(|double| arg) +{ + |double| ans = 0; + __if_comp__ __C__{ + __simple_Passthrough__ """ + ans = atan(arg); + """ + }//end C wrapper + + return ans; +}//end atan function + +|double| atan2(|double| x, |double| y) +{ + |double| ans = 0; + __if_comp__ __C__{ + __simple_Passthrough__ """ + ans = atan2(x,y); + """ + }//end C wrapper + + return ans; +}//end atan2 function + +|double| acos(|double| arg) +{ + |double| ans = 0; + __if_comp__ __C__{ + __simple_Passthrough__ """ + ans = acos(arg); + """ + }//end C wrapper + + return ans; +}//end acos function + +|double| asin(|double| arg) +{ + |double| ans = 0; + __if_comp__ __C__{ + __simple_Passthrough__ """ + ans = asin(arg); + """ + }//end C wrapper + + return ans; +}//end asin function + +|double| tan(|double| arg) +{ + |double| ans = 0; + __if_comp__ __C__{ + __simple_Passthrough__ """ + ans = tan(arg); + """ + }//end C wrapper + + return ans; +}//end tan function + +|double| cos(|double| arg) +{ + |double| ans = 0; + __if_comp__ __C__{ + __simple_Passthrough__ """ + ans = cos(arg); + """ + }//end C wrapper + + return ans; +}//end cos function + +|double| sin(|double| arg) +{ + |double| ans = 0; + __if_comp__ __C__{ + __simple_Passthrough__ """ + ans = sin(arg); + """ + }//end C wrapper + + return ans; +}//end sin function + +//|int| NotPi = 3; +//|double| STD_PI = 4*atan(1); + + + + + + + + +