Added C functions for linear algebra, to be converted to kraken and used as benchmark
This commit is contained in:
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
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
36
ChrisTest/ChrisVec/ChrisVecTest.krak
Normal file
36
ChrisTest/ChrisVec/ChrisVecTest.krak
Normal file
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
../build/kraken InputFile.krak ../krakGrammer(tabComplete) OutputFile
|
||||
*/
|
||||
|
||||
import vector:*;
|
||||
import ChrisVec:*;
|
||||
import io:*;
|
||||
|
||||
|int| main()
|
||||
{
|
||||
|
||||
|vector<double>| aVec.construct();
|
||||
|vector<double>| bVec.construct();
|
||||
|
||||
|
||||
|double| j;
|
||||
for(|int| i = 0; i < 4; i++;)
|
||||
{
|
||||
j = i + 0.0;
|
||||
aVec.addEnd(j);
|
||||
bVec.addEnd(j);
|
||||
}
|
||||
|
||||
bVec.addEnd(12.3);
|
||||
|
||||
|double| dotProd = dot<double>(aVec, bVec);
|
||||
|
||||
println("Dot Product has completed");
|
||||
println();
|
||||
println();
|
||||
|double| NormSquared = norm2<double>(aVec);
|
||||
println("Norm has completed");
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user