Re: using dot_product from c++ II
"Jim Langston" <tazmaster@rocketmail.com> wrote in message
news:Ppoxj.405$bc1.96@newsfe06.lga...
Gerry Ford wrote:
[snipped and re-ordered]
So, if a points to vec_a, and likewise with b, how do I rewrite Jim
Langston's function to calculate an inner product from scratch?
This may be, but the errors you posted have nothing to do with the code as
supplied. Are you missing linking some library. I don't know what
library it is however.
It sounds like you may be attempting to link the fortran object with the
C++ object but forgetting to also link one of the required c++ libraries.
What library that is depends on your compiler and OS, etc... What are you
compiling with? What compiler and what OS? Looks like windows because of
the path, but doesn't look like MSVC++
The compiler I was using is dev-cpp on the platform that dare not speak its
name. I think I have configuration troubles with too many compilers on the
block. I believe this compiles with the exception of the pointer
re-reference and the function call that has mismatched types; it's a gemisch
of what you and Jason wrote:
#include <cmath>
#include <vector>
#include <iostream>
#include <iterator>
// using pointers instead of references
double dot_product_sqrt (const std::vector<double> *a, const
std::vector<double> *b) {
// you can remove this check if you know you'll never pass NULLs.
if (a == NULL || b == NULL) {
std::cerr << "NULL pointer passed to dot_product.\n";
return 0.0;
}
// you can remove this check if you know a and b are always the
//same size.
if (a->size() != b->size()) {
std::cerr << "Vectors for dot product are not same size!\n";
return 0.0;
}
double sum = 0.0;
for (size_t i = 0; i < a->size(); ++ i)
sum += ((*a)[i] * (*b)[i]);
// just use sqrt(), no need for std::pow(sum,.5):
return sqrt(sum);
}
int main()
{
std::vector<double> vec_a;
std::vector<double> vec_b;
float r = 42.0;
float s = 43.56;
float *rp = &r;
float *sp = &s;
//first error is next line
rp = & vec_a;
for ( int i = 0; i < 4; ++i )
{
vec_a.push_back( std::sqrt( static_cast<double>( i )) );
vec_b.push_back( i * i );
}
std::cout.precision(16);
std::copy(vec_a.begin(), vec_a.end(),
std::ostream_iterator<double>(std::cout, "\n"));
std::copy(vec_b.begin(), vec_b.end(),
std::ostream_iterator<double>(std::cout, "\n"));
std::cout << "Dot Product: " << dot_product_sqrt(vec_a, vec_b) << "\n";
return 0;
}
// end source. The first error is:
44 C:\Dev-Cpp\c++_source\vector10.cpp cannot convert `std::vector<double,
std::allocator<double> >*' to `float*' in assignment
The above version has main in c++, which is going to be different from the
ultimate program that calls the function from elsewhere. I don't have the
kinks worked out in herding the address of two vectors into the function.
It looks like I either need a radical cast, or I need to revise either
main's or the external function on what is being passed.
Grateful for any tips.
--
Gerry Ford
"Er hat sich georgiert." Der Spiegel, 2008, sich auf Chimpy Eins komma null
beziehend.