我正在尝试编写一个带有函数double_product(vector<double> a, vector<double> b)
的程序,它可以计算两个向量的标量积。标量产品是
$a_{0}b_{0}+a_{1}b_{1}+...+a_{n-1}b_{n-1}$.
这就是我所拥有的。这是一团糟,但我正在努力!
#include <iostream>
#include <vector>
using namespace std;
class Scalar_product
{
public:
Scalar_product(vector<double> a, vector<double> b);
};
double scalar_product(vector<double> a, vector<double> b)
{
double product = 0;
for (int i = 0; i <= a.size()-1; i++)
for (int i = 0; i <= b.size()-1; i++)
product = product + (a[i])*(b[i]);
return product;
}
int main() {
cout << product << endl;
return 0;
}
答案 0 :(得分:41)
除非您需要自己完成此操作(例如,将其写成作业),否则您应该使用已编写的标准算法来完全按照您的要求执行此操作:
#include <iostream>
#include <numeric>
#include <vector>
int main() {
std::vector<double> a {1, 2, 3};
std::vector<double> b {4, 5, 6};
std::cout << "The scalar product is: "
<< std::inner_product(std::begin(a), std::end(a), std::begin(b), 0.0);
return 0;
}
请注意,虽然{+ 1}}和begin(a)
是C ++ 11中的新功能,但自{C ++ 98}以来end(a)
已经可用。如果您使用的是C ++ 98(或03),那么编写自己的等效std::inner_product
和begin
来处理数组非常容易:
end
使用这些,前面代码的C ++ 98版本可能如下所示:
template <class T, size_t N>
T *begin(T (&array)[N]) {
return array;
}
template <class T, size_t N>
T *end(T (&array)[N]) {
return array + N;
}
请注意,上面的int main() {
double a[] = {1, 2, 3};
double b[] = {4, 5, 6};
std::cout << "The scalar product is: "
<< std::inner_product(begin(a), end(a), begin(b), 0.0);
return 0;
}
和begin
仅适用于数组,其中C ++ 11(及更高版本)中的end
和begin
也适用于定义end
和.begin()
的常规集合类型(当然,添加重载来处理这些也很简单):
.end()
答案 1 :(得分:12)
您可以删除已定义的class
。你不需要它。
在scalar_product
函数中:
double scalar_product(vector<double> a, vector<double> b)
{
double product = 0;
for (int i = 0; i <= a.size()-1; i++)
for (int i = 0; i <= b.size()-1; i++)
product = product + (a[i])*(b[i]);
return product;
}
几乎就在那里。你不需要2个循环。只有一个。
double scalar_product(vector<double> a, vector<double> b)
{
if( a.size() != b.size() ) // error check
{
puts( "Error a's size not equal to b's size" ) ;
return -1 ; // not defined
}
// compute
double product = 0;
for (int i = 0; i <= a.size()-1; i++)
product += (a[i])*(b[i]); // += means add to product
return product;
}
现在要调用这个函数,你需要在main()
中创建2个向量对象,用值填充它们(当然是相同数量的值!)然后调用{ {1}}
答案 2 :(得分:3)
虽然您已经找到了许多可行的解决方案,但是让我提出另一个变体来介绍一些可以帮助您编写更好代码的概念:
class
仅需要将数据打包在一起考虑到这一点:
// Takes two vectors of the same size and computes their scalar product
// Returns a positive value
double scalar_product(std::vector<double> const& a, std::vector<double> const& b)
{
if (a.size() != b.size()) { throw std::runtime_error("different sizes"); }
return std::inner_product(a.begin(), a.end(), b.begin(), 0.0);
} // scalar_product
您可以决定直接使用inner_product
算法,但让我们面对它:
所以最好把它包起来。
注意:我使用const&
指示编译器不要复制向量。
答案 3 :(得分:1)
这是您应该拥有的代码。我看到你在代码中使用了类,这里你并不需要。如果问题要求您使用课程,请告诉我。
因为你是新手,这段代码可能会吓到你。所以,我会尽力解释这个问题。在代码中查找注释以了解正在执行的操作并询问您是否不理解。
//Scalar.cpp
#include <stdlib.h>
#include <iostream>
#include <vector>
using namespace std;
/**
This function returns the scalar product of two vectors "a" and "b"
*/
double scalar_product(vector<double> a, vector<double> b)
{
//In C++, you should declare every variable before you use it. So, you declare product and initialize it to 0.
double product = 0;
//Here you check whether the two vectors are of equal size. If they are not then the vectors cannot be multiplied for scalar product.
if(a.size()!=b.size()){
cout << "Vectors are not of the same size and hence the scalar product cannot be calculated" << endl;
return -1; //Note: This -1 is not the answer, but just a number indicating that the product is not possible. Some pair of vectors might actually have a -1, but in that case you will not see the error above.
}
//you loop through the vectors. As bobo also pointed you do not need two loops.
for (int i = 0; i < a.size(); i++)
{
product = product + a[i]*b[i];
}
//finally you return the product
return product;
}
//This is your main function that will be executed before anything else.
int main() {
//you declare two vectors "veca" and "vecb" of length 2 each
vector<double> veca(2);
vector<double> vecb(2);
//put some random values into the vectors
veca[0] = 1.5;
veca[1] = .7;
vecb[0] = 1.0;
vecb[1] = .7;
//This is important! You called the function you just defined above with the two parameters as "veca" and "vecb". I hope this cout is simple!
cout << scalar_product(veca,vecb) << endl;
}
如果您使用的是IDE,那么只需编译并运行即可。如果您在基于Unix的系统上使用g ++编译器使用命令行,那么您将这样做(其中Scalar.cpp是包含代码的文件):
g++ Scalar.cpp -o scalar
要运行它,只需输入
即可./scalar
您应该获得1.99
作为上述程序的输出。
答案 4 :(得分:0)
你似乎想要专门为矢量创建一个类。我在我的示例中创建的类是针对3维向量而定制的,但如果需要,您可以将其更改为另一个。该类包含i,j,k,但也可以基于其他MathVector进行标量产品。另一个向量通过C ++引用传入。很难推断出问题是什么,但我认为这可能会回答它。
#include <iostream>
using namespace std;
class MathVector
{
private:
double i,j,k;
public:
MathVector(double i,double j,double k)
{
this->i=i;
this->j=j;
this->k=k;
}
double getI(){return i;}
double getJ(){return j;}
double getK(){return k;}
double scalar(MathVector &other)
{
return (i*other.getI())+(j*other.getJ())+(k*other.getK());
}
};
int main(int argc, char **argv)
{
MathVector a(1,2,5), b(2,4,1);
cout << a.scalar(b) << endl;
return 0;
}