0

I'm trying to implement an Euclidean vector for my programming assignment. I need to overload operator* to provide dot product calculation for two vectors with arbitrary same dimension. As 3D vector for example:

Vector<3> v1, v2; //two 3D vectors.

double dotProduct = v1 * v2;

The value of dotProduct should be v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2]

So, my problem is how to get this value without using any explicit loop and std::accumulate() operation in numeric.h header file? Because those are forbidden in this assignment.

P.S. I may use functor(self defined) together with STL algorithm.

7
  • 1) Do you know that the operands will always be three-dimensional? 2) What is a Vector? Commented Nov 3, 2012 at 2:51
  • As you've written dotProduct = v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2], it appears to me that you have an idea of how to approach this. What have you tried and where exactly are you stuck? Commented Nov 3, 2012 at 2:56
  • 1
    There is no <numeric.h>, only <numeric>. Your answer is on this page. (Hint: the dot product is a type of what?) Commented Nov 3, 2012 at 3:00
  • 2
    What you're trying to compute is normally called an inner product. Searching existing algorithms with that mind might be fruitful. Commented Nov 3, 2012 at 3:04
  • std::for_each may serve well when using builtin loops isn't permitted. Commented Nov 3, 2012 at 3:13

4 Answers 4

2

If you really want to avoid explicit loops and algorithms in general (not just std::accumulate), you could use std::valarrays instead:

std::valarray<double> a;
std::valarray<double> b;

// code to put data in a and b goes here

double dotProduct = (a * b).sum();

I've used double as the type here, but you can (of course) use whatever type makes sense for the data/situation you're dealing with.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use std::inner_product, see http://en.cppreference.com/w/cpp/algorithm/inner_product

double dotProduct = std::inner_product(v1.begin(), v1.end(), v2.begin());

Comments

0

If you can't use explicit loops, perhaps your teacher is asking you to use recursion.

template<int N>
int VectorSum(const Vector<N>& v1, const Vector<N>& v2, int m) {
  if(m) return v1[m]*v2[m] + VectorSum(v1, v2, m-1);
  return v1[0]*v2[0];
}

template<int N>
int operator+(const Vector<N>& v1, const Vector<N>& v2) {
  return VectorSum(v1, v2, N-1);
}

Comments

0

I happened to read a text about it, so I copy it for you. may be it's introduced in the book :C++ Templates: The Complete Guide.

#include <iostream>

template<int DIM,typename T>
struct DotProduct {
    static T execute(const T v1[],const T v2[]);
};
template<int DIM,typename T>
T DotProduct<DIM,T>::execute(const T v1[],const T v2[]) {
    return v1[0]*v2[0] + DotProduct<DIM-1,T>::execute(v1+1,v2+1);
};

template<typename T>
struct DotProduct<1,T> {
    static T execute(const T v1[],const T v2[]);
};

template<typename T>
T DotProduct<1,T>::execute(const T v1[],const T v2[]) {
    return v1[0]*v2[0];
};

int main()
{
    int v1[] = {1,2,3}; 
    int v2[] = {4,5,6};
    int r2 = DotProduct<3,int>::execute(v1,v2);
    std::cout << r2 << std::endl;
    return 0;
}

1 Comment

A nice example for using template and recursion! Thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.