dot product in c++

Discussion in 'C++' started by Gerry Ford, Feb 15, 2008.

  1. Gerry Ford

    Gerry Ford Guest

    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
    Gerry Ford, Feb 15, 2008
    #1
    1. Advertising

  2. Gerry Ford

    Jerry Coffin Guest

    In article <>,
    says...

    [ ... ]

    > #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;
    }

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
    Jerry Coffin, Feb 15, 2008
    #2
    1. Advertising

  3. Gerry Ford

    Ondra Holub Guest

    On 15 Ún, 05:12, "Gerry Ford" <> wrote:
    > 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);
    Ondra Holub, Feb 15, 2008
    #3
  4. Gerry Ford wrote:
    > 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.
    Gianni Mariani, Feb 15, 2008
    #4
  5. On Feb 15, 5:12 am, "Gerry Ford" <> wrote:
    > 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.

    --
    Erik Wikström
    Erik Wikström, Feb 15, 2008
    #5
  6. Gerry Ford

    Gerry Ford Guest

    "Erik Wikström" <> wrote in message
    news:...
    On Feb 15, 5:12 am, "Gerry Ford" <> wrote:
    > 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.


    --
    Gerry Ford

    "Er hat sich georgiert." Der Spiegel, 2008, sich auf Chimpy Eins komma null
    beziehend.
    Gerry Ford, Feb 16, 2008
    #6
  7. Gerry Ford

    Gerry Ford Guest

    "Jerry Coffin" <> wrote in message
    news:...
    > In article <>,
    > says...


    > 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?

    --
    Gerry Ford

    "Er hat sich georgiert." Der Spiegel, 2008, sich auf Chimpy Eins komma null
    beziehend.
    Gerry Ford, Feb 16, 2008
    #7
  8. Gerry Ford

    Jerry Coffin Guest

    In article <>,
    says...

    [ ... ]

    > 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;
    }


    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
    Jerry Coffin, Feb 17, 2008
    #8
  9. Gerry Ford

    Gerry Ford Guest

    "Erik Wikström" <> wrote in message
    news:...
    On Feb 15, 5:12 am, "Gerry Ford" <> wrote:

    > 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
    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
    with the address of the referring site.
    # end busted link

    Which on that page do you use?

    --
    Gerry Ford

    "Er hat sich georgiert." Der Spiegel, 2008, sich auf Chimpy Eins komma null
    beziehend.
    Gerry Ford, Feb 17, 2008
    #9
  10. On 2008-02-17 05:42, Gerry Ford wrote:
    > "Erik Wikstré—£" <> wrote in message
    > news:...
    > On Feb 15, 5:12 am, "Gerry Ford" <> wrote:
    >
    >> 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é—£
    >
    >
    > --->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
    > 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
    > 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.

    --
    Erik Wikström
    Erik Wikström, Feb 17, 2008
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wallge
    Replies:
    0
    Views:
    1,403
    wallge
    Feb 20, 2006
  2. Samuël van Laere

    To dot or not to dot?

    Samuël van Laere, Oct 16, 2003, in forum: HTML
    Replies:
    8
    Views:
    412
    Samuël van Laere
    Oct 16, 2003
  3. padma
    Replies:
    0
    Views:
    334
    padma
    Oct 3, 2007
  4. Replies:
    4
    Views:
    305
    Phrogz
    Nov 2, 2007
  5. Replies:
    6
    Views:
    223
    Thomas 'PointedEars' Lahn
    Dec 12, 2005
Loading...

Share This Page