recursion help (i think)

Jul 30, 2018 at 5:09am
Hello all and thank you advance for any help that is provided.
I am trying to figure out how to create a function or just a line of code that will allow me to add indexes from 2 vectors whose sizes and elements are determined by the user.
This line of code works, but the user should able to enter many more elements than what this formula will calculate.

please help!

 
  t = ((v[0]*w[0])+(v[1]*w[1])+(v[2]*w[2])+(v[3]*w[3]))
Jul 30, 2018 at 5:49am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>

long long add( const std::vector<int>& first, const std::vector<int>& second )
{
    long long result = 0 ;

    const auto min_size = std::min( first.size(), second.size() ) ;
    for( std::size_t i = 0 ; i < min_size ; ++i ) result += (long long)first[i] * second[i] ;

    return result ;
}

long long add_recursive( const std::vector<int>& first, const std::vector<int>& second, std::size_t from_pos = 0 )
{
    if( from_pos >= std::min( first.size(), second.size() ) ) return 0 ;
    else return (long long)first[from_pos] * second[from_pos] + add_recursive( first, second, from_pos+1 ) ;
}

long long add_smart( const std::vector<int>& first, const std::vector<int>& second )
{
    const auto min_size = std::min( first.size(), second.size() ) ;
    // https://en.cppreference.com/w/cpp/algorithm/inner_product
    return std::inner_product( first.begin(), first.begin()+min_size, second.begin(), 0LL ) ;
}

int main()
{
    const std::vector<int> first { 1, 2, 3, 4, 5, 6, 7, 8, 9 } ;
    const std::vector<int> second { 2, 4, 6, 8, 10 } ;

    std::cout << add( first, second ) << '\n'
              << add_recursive( first, second ) << '\n'
              << add_smart( first, second ) << '\n' ;
}

http://coliru.stacked-crooked.com/a/9ad53ca5c6ecae97
Jul 30, 2018 at 8:29am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <vector>
#include <algorithm>    // for min
#include <numeric>      // for inner_product
using namespace std;

template <typename T> T operator *( const vector<T> &V, const vector<T> &W )
{
   size_t size = min( V.size(), W.size() );
   return inner_product( V.begin(), V.begin() + size, W.begin(), 0 );
}

int main()
{
   vector<double> V = { 1, 2, 3, 4 };
   vector<double> W = { 10, 20, 30, 40, 50, 60 };
   cout << V * W;
}



If you can guarantee that both arrays have the same size then it is quite pretty with valarrays instead:
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <valarray>
using namespace std;

int main()
{
   valarray<double> V = { 1, 2, 3, 4 };
   valarray<double> W = { 10, 20, 30, 40 };
   cout << ( V * W ).sum();
}
Last edited on Jul 30, 2018 at 8:32am
Jul 30, 2018 at 2:22pm
Title of this thread should probably be renamed to "dot product". Recursion is a different topic.
Jul 30, 2018 at 2:27pm
icy1 wrote:
Title of this thread should probably be renamed to "dot product"


Mmm. Inclined to agree.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
module stuff
   implicit none

   interface operator( .dot. )
      procedure dot
   end interface

contains

   real function dot( V, W )
      real, intent(in) :: V(:), W(:)
      integer n

      n = min( size( V ), size( W ) )
      dot = sum( V(1:n) * W(1:n) )

   end function dot

end module stuff

!=============================

program test
   use stuff
   implicit none
   real, allocatable :: V(:), W(:)

   V = [ 1, 2, 3, 4 ]
   W = [ 10, 20, 30, 40, 50, 60 ]
   write( *, * ) V .dot. W

end program test
Topic archived. No new replies allowed.