Added C functions for linear algebra, to be converted to kraken and used as benchmark
This commit is contained in:
84
ChrisTest/C_Implementations/ChrisMatrix.h
Normal file
84
ChrisTest/C_Implementations/ChrisMatrix.h
Normal file
@@ -0,0 +1,84 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
/*********************
|
||||
* 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
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
31
ChrisTest/C_Implementations/main.c
Normal file
31
ChrisTest/C_Implementations/main.c
Normal file
@@ -0,0 +1,31 @@
|
||||
#include <stdio.h>
|
||||
#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;
|
||||
}
|
||||
37
ChrisTest/ChrisMatrix/ChrisMatrix.krak
Normal file
37
ChrisTest/ChrisMatrix/ChrisMatrix.krak
Normal file
@@ -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
|
||||
@@ -1,50 +0,0 @@
|
||||
/*
|
||||
../build/kraken InputFile.krak ../krakGrammer(tab complete) OutputFile
|
||||
*/
|
||||
import vector:*;
|
||||
import io:*;
|
||||
|
||||
/*****************************
|
||||
* Functions
|
||||
****************************/
|
||||
|
||||
|double| dot(|vector<double>| a, |vector<double>| 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<int>(a.size, b.size); i++;)
|
||||
{
|
||||
ans = ans + a.at(i) * b.at(i);
|
||||
}
|
||||
|
||||
println(ans);
|
||||
|
||||
return ans;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
78
ChrisTest/ChrisVec/ChrisVec.krak
Normal file
78
ChrisTest/ChrisVec/ChrisVec.krak
Normal file
@@ -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> |T| dot(|vector<T>| u, |vector<T>| 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<int>(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> |J| norm2(|vector<J>| r)
|
||||
{
|
||||
return(dot<J>(r,r));
|
||||
}//end norm function
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -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<double>(aVec, bVec);
|
||||
|
||||
println("Dot Product has completed");
|
||||
println();
|
||||
println();
|
||||
|double| NormSquared = norm2<double>(aVec);
|
||||
println("Norm has completed");
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
#!/bin/bash
|
||||
rm *krakout*
|
||||
39
ChrisTest/TrigTest.krak
Normal file
39
ChrisTest/TrigTest.krak
Normal file
@@ -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;
|
||||
}
|
||||
BIN
stdlib/.math.krak.un~
Normal file
BIN
stdlib/.math.krak.un~
Normal file
Binary file not shown.
106
stdlib/math.krak
106
stdlib/math.krak
@@ -1,9 +1,109 @@
|
||||
|
||||
|int| NotPi = 3;
|
||||
|float| Pi = 3.141592654;
|
||||
__if_comp__ __C__ __simple_passthrough__ """
|
||||
#include <math.h>
|
||||
"""
|
||||
|
||||
|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);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user