dot product in c++

G

Gerry Ford

I just busted out my c++ reference from purgatory and find that it doesn't
help my question at hand, concerning the inner product of vectors. 'vector'
is not in the index.

The programs in this reference are of the form void(main), so I'm left to
find a better syntax on the net. Since I want vectors and I want output, I
would assume that I have at least the following gheasders:
#include <iostream.h>
#include <vectors.h>

I don't need anything off the command line, so I think that
int main (void) { return 0; }
is right. ??

I'm given to understand that many of fortran's vector capabilities have an
analog in c++. How would I declare and print a single four_vector, with
double-wide real entries. For the purpose of illustration, let's make the
ith entry the real square root of i:
0.0000..
1.0000...
1.414....
1.732..

Since we're taking roots, we'll have to include math.h as well. How does
one declare such a vector portably?
 
J

Jerry Coffin

[ ... ]
#include <iostream.h>
#include <vectors.h>

That would be:

#include <iostream>
#include <vector>

If your book includes '.h' on the names of the standard headers, it's
very out of date.
I don't need anything off the command line, so I think that
int main (void) { return 0; }
is right. ??

That's reasonable, yes.
I'm given to understand that many of fortran's vector capabilities have an
analog in c++. How would I declare and print a single four_vector, with
double-wide real entries. For the purpose of illustration, let's make the
ith entry the real square root of i:

That's fairly trivial. This doesn't really do anything to ensure that
four_vector really remains a vector of four objects -- if you changed
the limit in the loop to '100.0' (or whatever) it would happily build
that size of vector. Of course, if you decide "whatever" includes
something like 1E200, chances are you'll have a hard time finding a
computer with enough memory (even drive space) to hold the results. Then
again, you'll probably find the memory about as quickly as you find
patience to wait for the results...

#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::copy(four_vector.begin(), four_vector.end(),
std::eek:stream_iterator<double>(std::cout, "\n"));
return 0;
}
 
O

Ondra Holub

I just busted out my c++ reference from purgatory and find that it doesn't
help my question at hand, concerning the inner product of vectors. 'vector'
is not in the index.

The programs in this reference are of the form void(main), so I'm left to
find a better syntax on the net. Since I want vectors and I want output, I
would assume that I have at least the following gheasders:
#include <iostream.h>
#include <vectors.h>

I don't need anything off the command line, so I think that
int main (void) { return 0; }
is right. ??

I'm given to understand that many of fortran's vector capabilities have an
analog in c++. How would I declare and print a single four_vector, with
double-wide real entries. For the purpose of illustration, let's make the
ith entry the real square root of i:
0.0000..
1.0000...
1.414....
1.732..

Since we're taking roots, we'll have to include math.h as well. How does
one declare such a vector portably?

--
Gerry Ford

"The apple was really a peach."
-- Allison Dunn on the garden of eden

You should include headers from standard library without .h:

#include <vector>
#include <iostream>

Some C-headers were adopted to C++, for example math.h is used in C++
as

#include <cmath>

(But both work with small differencies.)

For dot product you can #include <numeric> and use
std::inner_product(vector1.begin(), vector1.end(), vector2.begin(),
0);
 
G

Gianni Mariani

Gerry said:
I just busted out my c++ reference from purgatory and find that it doesn't
help my question at hand, concerning the inner product of vectors. 'vector'
is not in the index.

The programs in this reference are of the form void(main), so I'm left to
find a better syntax on the net. Since I want vectors and I want output, I
would assume that I have at least the following gheasders:
#include <iostream.h>
#include <vectors.h>

These headers are not standard c++ - you need to use
#include <iostream> // note no .h

std::vector is a dynamic sized vector. You may want a fixed size.
There is no "standard" fixed vector with dot product defined. You could
define one if you wanted to.

I started on a small graphics lib and it's not even close to being
complete so it's just an example.

http://austria.svn.sourceforge.net/viewvc/austria/src/gratlib/code/grt_base.h?view=markup

With this heade you can write:

grt::vec<double,4> v1( 1, 0, 0, 0 );
grt::vec<double,4> v2( 1, 1, 1, 1 );

double dot = v1 * v2;

grt::vec<double,4> cross = v1 ^ v2;

Code is not well tested, it's in a work in progress.

I don't need anything off the command line, so I think that
int main (void) { return 0; }
is right. ??

I'm given to understand that many of fortran's vector capabilities have an
analog in c++. How would I declare and print a single four_vector, with
double-wide real entries. For the purpose of illustration, let's make the
ith entry the real square root of i:
0.0000..
1.0000...
1.414....
1.732..

Since we're taking roots, we'll have to include math.h as well. How does
one declare such a vector portably?

the grt code is portable with a compliant c++ compiler.
 
E

Erik Wikström

I just busted out my c++ reference from purgatory and find that it doesn't
help my question at hand, concerning the inner product of vectors. 'vector'
is not in the index.

The programs in this reference are of the form void(main), so I'm left to
find a better syntax on the net. Since I want vectors and I want output, I
would assume that I have at least the following gheasders:
#include <iostream.h>
#include <vectors.h>

I don't need anything off the command line, so I think that
int main (void) { return 0; }
is right. ??

I'm given to understand that many of fortran's vector capabilities have an
analog in c++. How would I declare and print a single four_vector, with
double-wide real entries. For the purpose of illustration, let's make the
ith entry the real square root of i:
0.0000..
1.0000...
1.414....
1.732..

Since we're taking roots, we'll have to include math.h as well. How does
one declare such a vector portably?

In addition to what others have said I'd like to point out that there
are a number of libraries supporting vectors, matrices, and linear
algebra, if that is what you are interested in I would suggest taking
a look at http://www.oonumerics.org/oon/#libraries which have a large
list of libraries with various functionality and quality. Since you
mentioned Fortran you might be familiar with BLAS in which case uBLAS
might be of interest, if you just need vectors/matrices and not BLAS
perhaps Blitz++ is better.
 
G

Gerry Ford

I just busted out my c++ reference from purgatory and find that it doesn't
help my question at hand, concerning the inner product of vectors.
'vector'
is not in the index.

The programs in this reference are of the form void(main), so I'm left to
find a better syntax on the net. Since I want vectors and I want output,
I
would assume that I have at least the following gheasders:
#include <iostream.h>
#include <vectors.h>

I don't need anything off the command line, so I think that
int main (void) { return 0; }
is right. ??

I'm given to understand that many of fortran's vector capabilities have an
analog in c++. How would I declare and print a single four_vector, with
double-wide real entries. For the purpose of illustration, let's make the
ith entry the real square root of i:
0.0000..
1.0000...
1.414....
1.732..

Since we're taking roots, we'll have to include math.h as well. How does
one declare such a vector portably?

In addition to what others have said I'd like to point out that there
are a number of libraries supporting vectors, matrices, and linear
algebra, if that is what you are interested in I would suggest taking
a look at http://www.oonumerics.org/oon/#libraries which have a large
list of libraries with various functionality and quality. Since you
mentioned Fortran you might be familiar with BLAS in which case uBLAS
might be of interest, if you just need vectors/matrices and not BLAS
perhaps Blitz++ is better.


---> Thanks all for responses.

I certainly look overdue for a refresher in this syntax! One thing I have
going in my favor is that I have two different c++ compilers installed
because I wanted other development tools, and the c++ part comes with the
package.

This topic arose in comp.lang.fortran as a mixed-syntax problem, although
either syntax could do it by itself (fortran has an inner product as an
intrinsic). There are still issues to address in passing the vectors, e.g.,
what to do with the zeroeth entry in c++ as opposed to the first in fortran,
but I'll address that when I'm ready. Thx again.

I'll chase down those links too.
 
G

Gerry Ford

Jerry Coffin said:
That's fairly trivial. This doesn't really do anything to ensure that
four_vector really remains a vector of four objects -- if you changed
the limit in the loop to '100.0' (or whatever) it would happily build
that size of vector. Of course, if you decide "whatever" includes
something like 1E200, chances are you'll have a hard time finding a
computer with enough memory (even drive space) to hold the results. Then
again, you'll probably find the memory about as quickly as you find
patience to wait for the results...

#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::copy(four_vector.begin(), four_vector.end(),
std::eek:stream_iterator<double>(std::cout, "\n"));
return 0;
}
This source does the trick; thanks to your attention to detail, I've got
output:
http://zaxfuuq.net/c++1.jpg

It seems, however, to be single-, not double-precision. Any ideas how to
change the above to get the ~13 sig figs one expects?
 
J

Jerry Coffin

[ ... ]
It seems, however, to be single-, not double-precision. Any ideas how to
change the above to get the ~13 sig figs one expects?

That's just a matter of the precision being used to write your output to
the stream. Fortunately, you can adjust it as needed:

#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::eek:stream_iterator<double>(std::cout, "\n"));
return 0;
}
 
G

Gerry Ford

Since we're taking roots, we'll have to include math.h as well. How does
one declare such a vector portably?

In addition to what others have said I'd like to point out that there
are a number of libraries supporting vectors, matrices, and linear
algebra, if that is what you are interested in I would suggest taking
a look at http://www.oonumerics.org/oon/#libraries which have a large
list of libraries with various functionality and quality. Since you
mentioned Fortran you might be familiar with BLAS in which case uBLAS
might be of interest, if you just need vectors/matrices and not BLAS
perhaps Blitz++ is better.

--
Erik Wikström


--->I clicked on only one of the links and got:
Missing Page

The page you're looking for was not found.

One of the following errors occurred:
1. You may have mistyped an address. Please retype the address or try
browsing the links on this page.

2. You may be using an out of date bookmark or favorite link. Try browsing
for the links at the top and update your bookmark or favorite accordingly.

3. You may have discovered a broken link in our site. Please contact us at
(e-mail address removed) with the address of the page that brought you here
and we will correct the error.

4. You may have found a broken link on a site that refers to ours. If so,
please notify the referring site of the problem or contact us at
(e-mail address removed) with the address of the referring site.
# end busted link

Which on that page do you use?
 
E

Erik Wikström

In addition to what others have said I'd like to point out that there
are a number of libraries supporting vectors, matrices, and linear
algebra, if that is what you are interested in I would suggest taking
a look at http://www.oonumerics.org/oon/#libraries which have a large
list of libraries with various functionality and quality. Since you
mentioned Fortran you might be familiar with BLAS in which case uBLAS
might be of interest, if you just need vectors/matrices and not BLAS
perhaps Blitz++ is better.

--
Erik Wikstré—£


--->I clicked on only one of the links and got:
Missing Page

The page you're looking for was not found.

One of the following errors occurred:
1. You may have mistyped an address. Please retype the address or try
browsing the links on this page.

2. You may be using an out of date bookmark or favorite link. Try browsing
for the links at the top and update your bookmark or favorite accordingly.

3. You may have discovered a broken link in our site. Please contact us at
(e-mail address removed) with the address of the page that brought you here
and we will correct the error.

4. You may have found a broken link on a site that refers to ours. If so,
please notify the referring site of the problem or contact us at
(e-mail address removed) with the address of the referring site.
# end busted link

I haven't actually used any of them, but as I said uBLAS or Blitz++
might be of interest. A number of the libraries on that page are quite
old and might not be supported anymore, but at least uBLAS will be
supported, and probably developed as well, since it is part of the boost
libraries.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top