79 lines
1.1 KiB
Plaintext
79 lines
1.1 KiB
Plaintext
|
|
|
||
|
|
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
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|