Re: writing an external function in std c++
Gerry Ford wrote:
I've been pecking away at writing a program that will calculate the
inner product of two double-width four-vectors.
Larry thinks I'm well started with the following source to populate a
vector:
#include <vector>
#include <algorithm>
#include <iterator>
#include <iostream>
#include <math.h>
int main() {
std::vector<double> four_vector;
for (double i=0.0; i<4.0; i++)
four_vector.push_back(sqrt(i));
std::cout.precision(16);
std::copy(four_vector.begin(), four_vector.end(),
std::ostream_iterator<double>(std::cout, "\n"));
return 0;
}
How do I imitate the following fortran source:
program vector2
implicit none
integer index, i
integer, parameter :: some_kind_number = selected_real_kind (p=16)
real (kind = some_kind_number), dimension(4):: vec_a, vec_b
real (kind = some_kind_number) :: res
index = 4
do i = 1, index
vec_a(i)= i**.5
vec_b(i)= (-1)*(i**2)
end do
res = dot_product(vec_a, vec_b)
write (*,*) vec_a, vec_b
write (*,*) res
end program vector2
! gfortran2 -o vector2 vector2.f95
! vector2 >text55.txt 2>text56.txt
//end source continue comment
, except making the inner product calculated externally. I have zero
chance of getting it correct, so I'll spare you the flailing attempt.
Screenshot here: http://zaxfuuq.net/c++5.jpg
To imitate it, I believe the appropriate c++ inner product would be
around negative 25.
You are not actually showing the fortran function that calculates the dot
product. That fortran source code simply populates 2 arrays of 4 elements
then calls the function dot_product(). It is the function dot_product you
want to code, but you aren't showing the souce for that.
In fortran ** is the power symbol, in C and C++ you can use the pow
function. Although some number raised to the power of 2 it's just as simple
to multiply the number by itself. Raising a number to the power of .5 is
taking the square root of it. So basically all that fortran code is filling
one array of 4 elements with the square roots of 1 to 4, the second 4
element array with the squares of 1 to 4, then calling the function
dot_product on them which returns a single number.
--
Jim Langston
tazmaster@rocketmail.com