Re: writing an external function in std c++
On 18 Feb., 06:41, "Gerry Ford" <inva...@invalid.net> wrote:
I've been pecking away at writing a program that will calculate the inner
product of two double-width four-vectors.
Why? There are libraries that do this better than you or I could
reasonably hope to do unless we really took the time (and perhaps even
then! ;-)).
I am quite sure of that even if I don't really know what double-width
and four-vector is. These are neither terms of C++ nor of mathematics.
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));
This loop is really dangerous. You risk having five elements in your
vector. Also it is inefficient as you (presumably!) know that there
should be four elements in the vector.
Use reserve or prefer a fixed-size vector which imo is the best
solution provided that you will always have a fixed number of elements
in your vector.
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)
this would be something like
for (size_t i(0); i < 4; ++i)
{
a[i] = sqrt(i);
b[i] = -i*i;
}
end do
res = dot_product(vec_a, vec_b)
write (*,*) vec_a, vec_b
write (*,*) res
I can't imagine you having problems with the dot-product and I do not
understand the fortran output formats.
end program vector2
/Peter