writing an external function in std c++

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

  1. Gerry Ford

    Gerry Ford Guest

    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::eek:stream_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.

    --
    Gerry Ford

    "Er hat sich georgiert." Der Spiegel, 2008, sich auf Chimpy Eins komma null
    beziehend.
     
    Gerry Ford, Feb 18, 2008
    #1
    1. Advertising

  2. Gerry Ford

    Kai-Uwe Bux Guest

    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::eek:stream_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.
    >


    What about using

    std::inner_product( vec_a.begin(), vec_a.end(), vec_b.begin(), double() );

    from the <numeric> header?


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Feb 18, 2008
    #2
    1. Advertising

  3. Gerry Ford

    Jim Langston Guest

    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::eek:stream_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
     
    Jim Langston, Feb 18, 2008
    #3
  4. Gerry Ford

    Gerry Ford Guest

    "Jim Langston" <> wrote in message
    news:Jwauj.420$...
    > Gerry Ford wrote:


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

    You're prettymuch right on all this. I appreciate you talking through this
    source for the benefit of those less familiar with my common C extension of
    choice.

    There isn't fortran code for dot_product, as it comes with the food over
    there. That shocked the heck out of me the first time I realized it. I was
    given to understand that c++ had likewise, but not to worry. With what we
    have for headers, we can write it from scratch.

    double float c++_dot_product (array vec_a , array vec_b, integer index)
    {
    // declare local vars
    res = 0;
    term=0;
    sum=0;
    for (i = 0; i < index; ++ i)
    {
    term=vec_a(i)*v_b(i);
    sum = sum + term;

    }
    res = powl(sum, .5);

    return res;
    }

    How does one correctly code the caller for this and the external function
    itself?

    --
    Gerry Ford

    "Er hat sich georgiert." Der Spiegel, 2008, sich auf Chimpy Eins komma null
    beziehend.
     
    Gerry Ford, Feb 18, 2008
    #4
  5. Gerry Ford

    Jim Langston Guest

    Gerry Ford wrote:
    > "Jim Langston" <> wrote in message
    > news:Jwauj.420$...
    >> Gerry Ford wrote:

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

    > You're prettymuch right on all this. I appreciate you talking
    > through this source for the benefit of those less familiar with my
    > common C extension of choice.
    >
    > There isn't fortran code for dot_product, as it comes with the food
    > over there. That shocked the heck out of me the first time I
    > realized it. I was given to understand that c++ had likewise, but
    > not to worry. With what we have for headers, we can write it from
    > scratch.
    > double float c++_dot_product (array vec_a , array vec_b, integer
    > index) {
    > // declare local vars
    > res = 0;
    > term=0;
    > sum=0;
    > for (i = 0; i < index; ++ i)
    > {
    > term=vec_a(i)*v_b(i);
    > sum = sum + term;
    >
    > }
    > res = powl(sum, .5);
    >
    > return res;
    > }
    >
    > How does one correctly code the caller for this and the external
    > function itself?


    Output of the follow program is:
    0
    1
    1.414213562373095
    1.732050807568877
    0
    1
    4
    9
    Dot Product: 4.716493561705802

    #include <cmath>
    #include <vector>
    #include <iostream>

    double dot_product (const std::vector<double>& vec_a, const
    std::vector<double>& vec_b)
    {
    if ( vec_a.size() != vec_b.size() )
    {
    std::cerr << "Vectors for dot product are not same size!\n";
    return 0.0;
    }

    double sum = 0;
    for ( std::size_t i = 0; i < vec_a.size(); ++i )
    {
    sum += vec_a * vec_b;
    }
    return std::pow(sum, .5);
    }

    // Following prototype is not needed in this program since
    // the definition is above, but would be used in another
    // source file without the previous definition.
    double dot_product (const std::vector<double>& vec_a, const
    std::vector<double>& vec_b);

    int main()
    {
    std::vector<double> vec_a;
    std::vector<double> vec_b;

    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::eek:stream_iterator<double>(std::cout, "\n"));
    std::copy(vec_b.begin(), vec_b.end(),
    std::eek:stream_iterator<double>(std::cout, "\n"));

    std::cout << "Dot Product: " << dot_product(vec_a, vec_b) << "\n";
    return 0;
    }

    An alternative to the static_cast<double>( i ) is chaning the for loop with
    a double.

    for ( double i = 0; i < 4.0; i += 1.0 )
    {
    vec_a.push_back( std::sqrt( i ) );
    vec_b.push_back( i * i );
    }

    --
    Jim Langston
     
    Jim Langston, Feb 18, 2008
    #5
  6. Jim Langston a écrit :
    > Gerry Ford wrote:
    >> "Jim Langston" <> wrote in message
    >> news:Jwauj.420$...
    >>> Gerry Ford wrote:
    >>>> 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.

    >> You're prettymuch right on all this. I appreciate you talking
    >> through this source for the benefit of those less familiar with my
    >> common C extension of choice.
    >>
    >> There isn't fortran code for dot_product, as it comes with the food
    >> over there. That shocked the heck out of me the first time I
    >> realized it. I was given to understand that c++ had likewise, but
    >> not to worry. With what we have for headers, we can write it from
    >> scratch.
    >> double float c++_dot_product (array vec_a , array vec_b, integer
    >> index) {
    >> // declare local vars
    >> res = 0;
    >> term=0;
    >> sum=0;
    >> for (i = 0; i < index; ++ i)
    >> {
    >> term=vec_a(i)*v_b(i);
    >> sum = sum + term;
    >>
    >> }
    >> res = powl(sum, .5);
    >>
    >> return res;
    >> }
    >>
    >> How does one correctly code the caller for this and the external
    >> function itself?

    >
    > Output of the follow program is:
    > 0
    > 1
    > 1.414213562373095
    > 1.732050807568877
    > 0
    > 1
    > 4
    > 9
    > Dot Product: 4.716493561705802
    >
    > #include <cmath>
    > #include <vector>
    > #include <iostream>
    >
    > double dot_product (const std::vector<double>& vec_a, const
    > std::vector<double>& vec_b)
    > {
    > if ( vec_a.size() != vec_b.size() )
    > {
    > std::cerr << "Vectors for dot product are not same size!\n";
    > return 0.0;
    > }
    >
    > double sum = 0;
    > for ( std::size_t i = 0; i < vec_a.size(); ++i )
    > {
    > sum += vec_a * vec_b;
    > }
    > return std::pow(sum, .5);
    > }
    >
    > // Following prototype is not needed in this program since
    > // the definition is above, but would be used in another
    > // source file without the previous definition.
    > double dot_product (const std::vector<double>& vec_a, const
    > std::vector<double>& vec_b);
    >
    > int main()
    > {
    > std::vector<double> vec_a;
    > std::vector<double> vec_b;
    >
    > 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::eek:stream_iterator<double>(std::cout, "\n"));
    > std::copy(vec_b.begin(), vec_b.end(),
    > std::eek:stream_iterator<double>(std::cout, "\n"));
    >
    > std::cout << "Dot Product: " << dot_product(vec_a, vec_b) << "\n";
    > return 0;
    > }
    >
    > An alternative to the static_cast<double>( i ) is chaning the for loop with
    > a double.
    >
    > for ( double i = 0; i < 4.0; i += 1.0 )
    > {
    > vec_a.push_back( std::sqrt( i ) );
    > vec_b.push_back( i * i );
    > }
    >

    In fact, in C++, the only version of std::sqrt() is double sqrt(double);
    the other variants with float and long double are C99 additions. In the
    standard case, the integer should be promoted to double and the
    static_cast<double>() is not necessary.
    for ( int i = 0; i < 4; ++i )
    {
    vec_a.push_back( std::sqrt( i ) );
    vec_b.push_back( i * i );
    }

    I guess that in the case of C99 extension, an integer in parameter is
    forwarded to the double flavor otherwise, there would be an ambiguity.

    Michael
     
    Michael DOUBEZ, Feb 18, 2008
    #6
  7. Gerry Ford

    James Kanze Guest

    On Feb 18, 1:49 pm, Michael DOUBEZ <> wrote:

    [...]
    > In fact, in C++, the only version of std::sqrt() is double
    > sqrt(double); the other variants with float and long double
    > are C99 additions.


    Overloads for float and long have been present in C++ at least
    since the 1998 version of the standard.

    > In the standard case, the integer should be promoted to double
    > and the static_cast<double>() is not necessary.


    > for ( int i = 0; i < 4; ++i )
    > {
    > vec_a.push_back( std::sqrt( i ) );
    > vec_b.push_back( i * i );
    > }


    > I guess that in the case of C99 extension, an integer in
    > parameter is forwarded to the double flavor otherwise, there
    > would be an ambiguity.


    Why? I think that there will be an ambiguity if std::sqrt() is
    called with an int. That's what the standard (and Sun CC) say,
    anyway.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Feb 18, 2008
    #7
  8. Gerry Ford

    peter koch Guest

    On 18 Feb., 06:41, "Gerry Ford" <> 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::eek:stream_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 = sqrt(i);
    b = -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
     
    peter koch, Feb 18, 2008
    #8
  9. Gerry Ford

    Gerry Ford Guest

    "Jim Langston" <> wrote in message
    news:_eeuj.3$...
    > Gerry Ford wrote:



    Thanks, Jim. In particular I appreciate the absence of disparaging remarks
    concerning the god-awful syntax on my first function in c++ in a decade.

    > std::copy(vec_a.begin(), vec_a.end(),
    > std::eek:stream_iterator<double>(std::cout, "\n"));
    > std::copy(vec_b.begin(), vec_b.end(),
    > std::eek:stream_iterator<double>(std::cout, "\n"));


    I've got 2 compilers that object to the above, to wit:
    42 C:\Program Files\gfortran\fortran source\vector3.cpp `ostream_iterator'
    is not a member of `std'
    ,with dev cpp and
    vector3.cpp:52:12: warning: no newline at end of file
    vector3.cpp: In function 'int main()':
    vector3.cpp:42: error: 'ostream_iterator' is not a member of 'std'
    vector3.cpp:42: error: expected primary-expression before 'double'
    vector3.cpp:44: error: 'ostream_iterator' is not a member of 'std'
    vector3.cpp:44: error: expected primary-expression before 'double'
    with g++.

    What gives?
    --
    Gerry Ford

    "Er hat sich georgiert." Der Spiegel, 2008, sich auf Chimpy Eins komma null
    beziehend.
     
    Gerry Ford, Feb 19, 2008
    #9
  10. Gerry Ford

    Gerry Ford Guest

    "peter koch" <> wrote in message
    news:...
    On 18 Feb., 06:41, "Gerry Ford" <> 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.

    --->Do you know how to get these libraries hooked into a garden-variety
    console app?

    As to the rest, you sound german, so I'll address you so. Ein Vierervektor
    soll etwa nicht in der Mathematik sein? Sie ercheinen im ersten Paragraph
    in Einstein 1916 Mathematische Hilfsmittel fuer die Aufstellung allgemein
    kovarianter Gleichungen.

    Double width is twice the width of a float.

    --
    Gerry Ford

    "Er hat sich georgiert." Der Spiegel, 2008, sich auf Chimpy Eins komma null
    beziehend.
     
    Gerry Ford, Feb 19, 2008
    #10
  11. James Kanze a écrit :
    > On Feb 18, 1:49 pm, Michael DOUBEZ <> wrote:
    >
    > [...]
    >> In fact, in C++, the only version of std::sqrt() is double
    >> sqrt(double); the other variants with float and long double
    >> are C99 additions.

    >
    > Overloads for float and long have been present in C++ at least
    > since the 1998 version of the standard.


    Funny thing. I really believed it was an extension.

    >> In the standard case, the integer should be promoted to double
    >> and the static_cast<double>() is not necessary.

    >
    >> for ( int i = 0; i < 4; ++i )
    >> {
    >> vec_a.push_back( std::sqrt( i ) );
    >> vec_b.push_back( i * i );
    >> }

    >
    >> I guess that in the case of C99 extension, an integer in
    >> parameter is forwarded to the double flavor otherwise, there
    >> would be an ambiguity.

    >
    > Why? I think that there will be an ambiguity if std::sqrt() is
    > called with an int. That's what the standard (and Sun CC) say,
    > anyway.


    I have found the 26.5-5 overload of cmath functions with float and long
    double but where does the standard say it must trigger an ambiguity ?
    Although it seems logical, wouldn't that break old C code making the
    distinction between sqrt and sqrtf ?
    I guess it is not that much a change.

    Michael
     
    Michael DOUBEZ, Feb 19, 2008
    #11
  12. In message
    <>,
    peter koch <> writes
    >On 18 Feb., 06:41, "Gerry Ford" <> 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.


    "Four-vector" is a standard term in relativistic physics.
    >
    >>
    >> 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.


    With probability approaching zero. I doubt if there are _any_
    floating-point implementations where a double can't exactly represent
    all integers up to some value considerably larger than a 32-bit int, and
    perform exact arithmetic on them. Even a float can count up to 4 without
    error.

    > Also it is inefficient as you (presumably!) know that there
    >should be four elements in the vector.


    _Measurably_ inefficient?

    >Use reserve


    You really think it will help? The very first push_back will do
    something internally equivalent to reserve(N) for some N which quite
    likely exceeds 4.

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


    There may well be a good argument for having a dedicated class
    representing four-vectors, but this isn't it.

    --
    Richard Herring
     
    Richard Herring, Feb 19, 2008
    #12
  13. Gerry Ford

    James Kanze Guest

    Michael DOUBEZ wrote:
    > James Kanze a ?crit :
    > > On Feb 18, 1:49 pm, Michael DOUBEZ <> wrote:


    > > [...]
    > >> In fact, in C++, the only version of std::sqrt() is double
    > >> sqrt(double); the other variants with float and long double
    > >> are C99 additions.


    > > Overloads for float and long have been present in C++ at least
    > > since the 1998 version of the standard.


    > Funny thing. I really believed it was an extension.


    > >> In the standard case, the integer should be promoted to double
    > >> and the static_cast<double>() is not necessary.


    > >> for ( int i = 0; i < 4; ++i )
    > >> {
    > >> vec_a.push_back( std::sqrt( i ) );
    > >> vec_b.push_back( i * i );
    > >> }


    > >> I guess that in the case of C99 extension, an integer in
    > >> parameter is forwarded to the double flavor otherwise, there
    > >> would be an ambiguity.


    > > Why? I think that there will be an ambiguity if std::sqrt() is
    > > called with an int. That's what the standard (and Sun CC) say,
    > > anyway.


    > I have found the 26.5-5 overload of cmath functions with float
    > and long double but where does the standard say it must
    > trigger an ambiguity ?


    Where does it say which one should be called?:)

    Basically, in §13.3.3.1.1, all of the standard conversions are
    ranked, and int->float, int->double and int->long double all
    have the same rank (conversion). Even in cases where int->float
    is lossy.

    > Although it seems logical, wouldn't that break old C code
    > making the distinction between sqrt and sqrtf ?


    Yep. There's no such thing as an ambiguous function call in C:
    "sqrt( i )" calls "sqrt( double )", because that's the only
    function named sqrt.

    Ideally, only std::sqrt() is overloaded; and ::sqrt() continues
    to behave as in C. (I'm not too sure what the standard
    requires/allows in this respect.)

    Note too that there is no std::sqrtf.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Feb 19, 2008
    #13
  14. Gerry Ford schrieb:
    > Thanks, Jim. In particular I appreciate the absence of disparaging remarks
    > concerning the god-awful syntax on my first function in c++ in a decade.
    >
    >> std::copy(vec_a.begin(), vec_a.end(),
    >> std::eek:stream_iterator<double>(std::cout, "\n"));
    >> std::copy(vec_b.begin(), vec_b.end(),
    >> std::eek:stream_iterator<double>(std::cout, "\n"));

    >
    > I've got 2 compilers that object to the above, to wit:
    > 42 C:\Program Files\gfortran\fortran source\vector3.cpp `ostream_iterator'
    > is not a member of `std'
    > ,with dev cpp and
    > vector3.cpp:52:12: warning: no newline at end of file
    > vector3.cpp: In function 'int main()':
    > vector3.cpp:42: error: 'ostream_iterator' is not a member of 'std'
    > vector3.cpp:42: error: expected primary-expression before 'double'
    > vector3.cpp:44: error: 'ostream_iterator' is not a member of 'std'
    > vector3.cpp:44: error: expected primary-expression before 'double'
    > with g++.
    >
    > What gives?


    #include <iterator>

    --
    Thomas
    http://www.netmeister.org/news/learn2quote.html
    Sentences long extremely and notation Polish reverse in writing about
     
    Thomas J. Gritzan, Feb 19, 2008
    #14
  15. Gerry Ford schrieb:
    [...]
    > There isn't fortran code for dot_product, as it comes with the food over
    > there. That shocked the heck out of me the first time I realized it. I was
    > given to understand that c++ had likewise, but not to worry. With what we
    > have for headers, we can write it from scratch.
    >
    > double float c++_dot_product (array vec_a , array vec_b, integer index)
    > {
    > // declare local vars
    > res = 0;
    > term=0;
    > sum=0;
    > for (i = 0; i < index; ++ i)
    > {
    > term=vec_a(i)*v_b(i);
    > sum = sum + term;
    >
    > }
    > res = powl(sum, .5);
    >
    > return res;
    > }
    >
    > How does one correctly code the caller for this and the external function
    > itself?


    Two questions:
    1) Why do you take the root from the result? A dot product is the sum of
    the product of the elements.
    2) Why don't you use std::inner_product?

    #include <numeric>

    double res = std::inner_product(vec1.begin(), vec1.end(), vec2.begin(), 0.0);

    --
    Thomas
    http://www.netmeister.org/news/learn2quote.html
    An ideal world is left as an excercise to the reader.
    --- Paul Graham, On Lisp 8.1
     
    Thomas J. Gritzan, Feb 19, 2008
    #15
  16. Gerry Ford

    peter koch Guest

    On 19 Feb., 11:41, Richard Herring <junk@[127.0.0.1]> wrote:
    > In message
    > <>,
    > peter koch <> writes
    >
    > >On 18 Feb., 06:41, "Gerry Ford" <> 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.

    >
    > "Four-vector" is a standard term in relativistic physics.
    >
    >
    >
    >
    >
    >
    >
    > >> 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.

    >
    > With probability approaching zero. I doubt if there are _any_
    > floating-point implementations where a double can't exactly represent
    > all integers up to some value considerably larger than a 32-bit int, and
    > perform exact arithmetic on them. Even a float can count up to 4 without
    > error.


    I fully agree with you here. I saw a loop controled by a double and my
    gut instinct told me that this was bad. Having integer increments make
    this safe, of course so there is no reason to worry.
    >
    > > Also it is inefficient as you (presumably!) know that there
    > >should be four elements in the vector.

    >
    > _Measurably_ inefficient?
    >
    > >Use reserve

    >
    > You really think it will help? The very first push_back will do
    > something internally equivalent to reserve(N) for some N which quite
    > likely exceeds 4.


    Perhaps. I have not examined my implementation but would expect it to
    have reserves of 1, 2 and four.
    >
    > >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.

    >
    > There may well be a good argument for having a dedicated class
    > representing four-vectors, but this isn't it.


    If all your four-vectors are dynamically allocated, I would expect new/
    delete to easily dominate some types of computations - all depending
    on how many temporaries you need, of course.

    /Peter
     
    peter koch, Feb 19, 2008
    #16
  17. Gerry Ford

    peter koch Guest

    On 19 Feb., 04:59, "Gerry Ford" <> wrote:
    > "peter koch" <> wrote in message
    >
    > news:...
    > On 18 Feb., 06:41, "Gerry Ford" <> wrote:> I've been pecking away at writing a program that will calculate the inner
    > > product of two double-width four-vectors.

    >
    >
    > --->Do you know how to get these libraries hooked into a garden-variety
    > console app?


    Hooked up? There are no specific stuff to do in order to hook up. Many
    numerical applications probably are "garden-variety" console
    applications.

    >
    > As to the rest, you sound german, so I'll address you so.  Ein Vierervektor
    > soll etwa nicht in der Mathematik sein?  Sie ercheinen im ersten Paragraph
    > in Einstein 1916 Mathematische Hilfsmittel fuer die Aufstellung allgemein
    > kovarianter Gleichungen.
    >
    > Double width is twice the width of a float.

    I am not german and don't see how I sound as if I am? And actually, I
    expected you to be american so this nationality guessing is not so
    easy I presume.
     
    peter koch, Feb 19, 2008
    #17
  18. Gerry Ford

    James Kanze Guest

    On Feb 19, 7:28 pm, peter koch <> wrote:
    > On 19 Feb., 11:41, Richard Herring <junk@[127.0.0.1]> wrote:


    [...]
    > > You really think it will help? The very first push_back will do
    > > something internally equivalent to reserve(N) for some N which quite
    > > likely exceeds 4.


    > Perhaps. I have not examined my implementation but would expect it to
    > have reserves of 1, 2 and four.


    Why? (That's certainly not what my implementation would do, if
    I were writing it.)

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Feb 19, 2008
    #18
  19. Gerry Ford

    peter koch Guest

    On 19 Feb., 22:32, James Kanze <> wrote:
    > On Feb 19, 7:28 pm, peter koch <> wrote:
    >
    > > On 19 Feb., 11:41, Richard Herring <junk@[127.0.0.1]> wrote:

    >
    >     [...]
    >
    > > > You really think it will help? The very first push_back will do
    > > > something internally equivalent to reserve(N) for some N which quite
    > > > likely exceeds 4.

    > > Perhaps. I have not examined my implementation but would expect it to
    > > have reserves of 1, 2 and four.

    >
    > Why?  (That's certainly not what my implementation would do, if
    > I were writing it.)


    Because I would expect std::vector to grow exponentially. Now, I know
    that this does not mean doubling, but for very small numbers I would
    expect an effective doubling.
    I see behind the lines a suggestion that there might be a faster
    growth for small values and this could well be so. Intuitively I
    understood this not to be according to the standard, but this was pure
    intuition.
    Now looking at it, it looks as my implementation allocates four times
    - namely as 1,2,3 and 4 elements. It tries to grow to
    max(new_size,current_capacity + current_capacity/2).

    /Peter
     
    peter koch, Feb 19, 2008
    #19
  20. Gerry Ford

    Gerry Ford Guest

    That's certainly what I wanted, not so much what I got.

    I'll try that syntax and think I can pull it off externally.

    Grateful for your thoughtful comment,


    --
    Gerry Ford

    "Er hat sich georgiert." Der Spiegel, 2008, sich auf Chimpy Eins komma null
    beziehend.
    "Thomas J. Gritzan" <> wrote in message
    news:fpel9a$6fl$...
    > Gerry Ford schrieb:
    > [...]
    >> There isn't fortran code for dot_product, as it comes with the food over
    >> there. That shocked the heck out of me the first time I realized it. I
    >> was given to understand that c++ had likewise, but not to worry. With
    >> what we have for headers, we can write it from scratch.
    >>
    >> double float c++_dot_product (array vec_a , array vec_b, integer index)
    >> {
    >> // declare local vars
    >> res = 0;
    >> term=0;
    >> sum=0;
    >> for (i = 0; i < index; ++ i)
    >> {
    >> term=vec_a(i)*v_b(i);
    >> sum = sum + term;
    >>
    >> }
    >> res = powl(sum, .5);
    >>
    >> return res;
    >> }
    >>
    >> How does one correctly code the caller for this and the external function
    >> itself?

    >
    > Two questions:
    > 1) Why do you take the root from the result? A dot product is the sum of
    > the product of the elements.
    > 2) Why don't you use std::inner_product?
    >
    > #include <numeric>
    >
    > double res = std::inner_product(vec1.begin(), vec1.end(), vec2.begin(),
    > 0.0);
    >
    > --
    > Thomas
    > http://www.netmeister.org/news/learn2quote.html
    > An ideal world is left as an excercise to the reader.
    > --- Paul Graham, On Lisp 8.1
     
    Gerry Ford, Feb 20, 2008
    #20
    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.

Share This Page