向量阵列,计算点产品
Vector arrays, calculation dot product
我正在尝试编写计算两个给定向量的点产物的C 程序。在向量A和B中,仅将非零元素存储到结构阵列中。每次我都取得无关紧要的结果。相关结果为50。我认为我无法正确阅读向量中的结构。请指教。预先感谢您
#include <iostream>
#include <vector>
using namespace std;
const int n=10; /* vector size limit */
struct element {
int x; /* original index of non-zero array element */
int val ; /* integer non-zero value at index x */
};
element row[n];
element col[n];
int i;
vector<int> a={0,0,7,0,5,0,0,8,0,4,-1};
vector<int> b={0,0,0,5,6,0,0,0,0,5,-1};
void generate_row_and_col()
{
for (i=0; i<=n; i++)
{
if(a[i]=!0)
{
row[i].x=i;
row[i].val=a[i];
}
}
for (i=0; i<=n; i++)
{
if(b[i]!=0)
{
col[i].x=i;
col[i].val=b[i];
}
}
}
int dotproduct()
{
/* calculate the dot product of row and col output the result*/
int i=0;
int j=0;
int product=0;
while(row[i].x!=-1 && col[j].x!=-1)
{
if(row[i].x == col[j].x)
{
product=product+row[i].val*col[j].val;
i++;
j++;
}
else if(row[i].x<col[j].x)
{
i++;
}
else
{
j++;
}
}
return product;
}
int main()
{
generate_row_and_col() ;
int r;
r=dotproduct();
cout<<"result="<<r<<endl;
return 0;
}
标准库具有恰好此目的的std::inner_product
。使用它可以将您的代码减少到类似的内容:
#include <numeric>
#include <iostream>
#include <vector>
int main() {
std::vector<int> a = { 0,0,7,0,5,0,0,8,0,4 };
std::vector<int> b = { 0,0,0,5,6,0,0,0,0,5 };
std::cout << std::inner_product(a.begin(), a.end(), b.begin(), 0);
}
使用= !0
是一个错误。应该是!= 0
。
我仍在猜测目标,但也许另一个清洁版本有帮助:
活在coliru
#include <iostream>
#include <vector>
#include <map>
using namespace std;
using Ints = vector<int>;
using Vec = map<int, int>;
Vec row, col;
Vec to_sparse_vec(Ints const& a) {
Vec v;
for (size_t i = 0; i < a.size(); ++i) {
if (a[i] != 0) v[i] = a[i];
}
return v;
}
int dotproduct(Vec row, Vec col) {
size_t n = max(row.rbegin()->first, col.rbegin()->first);
int product = 0;
for (size_t i = 0; i <= n; ++i) {
if (row[i] && col[i])
product += row[i] * col[i];
}
return product;
}
int main() {
auto row = to_sparse_vec({ 0, 0, 7, 0, 5, 0, 0, 8, 0, 4 });
auto col = to_sparse_vec({ 0, 0, 0, 5, 6, 0, 0, 0, 0, 5 });
cout << "result=" << dotproduct(row, col) << endl;
}
这假设Vec
表示为"稀疏向量"。
结果是50
(删除-1
元素)
您可以简化代码
#include <iostream>
#include <vector>
using namespace std;
template<class InputIt1, class InputIt2, class T>
T dot_product(InputIt1 first1, InputIt1 last1,
InputIt2 first2, T value)
{
while ((first1 != last1)&&(*first1!=-1) && (*first2 !=-1)) {
value = value + *first1 * *first2;
++first1;
++first2;
}
return value;
}
const int n=10; /* vector size limit */
struct element {
int x; /* original index of non-zero array element */
int val ; /* integer non-zero value at index x */
};
element row[n];
element col[n];
int i;
vector<int> a={0,0,7,0,5,0,0,8,0,4,-1};
vector<int> b={0,0,0,5,6,0,0,0,0,5,-1};
int main()
{
int r;
r=dot_product(a.begin(), a.end(), b.begin(), 0);
cout<<"result="<<r<<endl;
return 0;
}
输出
50
demo
我对代码进行了一些更改,现在可以正常工作。但是,在generate_row_and_col函数上的初始化行和col数组中,我仅初始化具有非零元素的索引,其余的未初始化。这不会在此程序中引起任何错误,但是就良好的编程实践而言,这些具有0个值的索引并未启动,并且数组看起来像(垃圾,垃圾,7,垃圾,5,5,垃圾,垃圾...。)。我们可以只限制generate_row_and_col函数,因此我们只能存储非零值索引。谢谢你
#include <iostream>
#include <vector>
using namespace std;
const int n=11; /* vector size */
struct element {
int index; /* original index of non-zero array element */
int value ; /* integer non-zero value at index x */
};
element row[n];
element col[n];
vector<int> a={0,0,7,0,5,0,0,8,0,4,-1};
vector<int> b={0,0,0,5,6,0,0,0,0,5,-1};
void generate_row_and_col()
{
for (int i=0; i<n; i++)
{
if(a[i]!=0)
{
row[i].index=i;
row[i].value=a[i];
}
}
for (int j=0; j<n; j++)
{
if(b[j]!=0)
{
col[j].index=j;
col[j].value=b[j];
}
}
}
int dotproduct()
{
/* calculate the dot product of row and col output the result*/
int i=0;
int j=0;
int product=0;
while(row[i].value!=-1 && col[j].value!=-1)
{
if(row[i].index == col[j].index)
{
product=product+row[i].value*col[j].value;
i++;
j++;
}
else if(row[i].index<col[j].index)
{
i++;
}
else
{
j++;
}
}
return product;
}
int main()
{
generate_row_and_col() ;
int r;
r=dotproduct();
cout<<"Dot Product = "<<r<<endl;
return 0;
}
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- 递归函数计算序列中的平方和(并输出过程)
- (C++)分析树以计算返回错误值的简单算术表达式
- 我的字符计数代码计算错误.为什么
- 在计算中使用二的幂有多有利可图
- OpenMP阵列性能较差
- 如何使用FFTW库C++计算3D阵列的FFT?
- 如何计算两个C 阵列的结合
- 如何使用多尺寸阵列计算C 中每个学生的平均值
- 如何计算结构阵列内的平均值
- 计算以毫秒为单位的分类算法对整数阵列排序的时间
- 向量阵列,计算点产品
- MPI-如何从本地阵列中计算到全局阵列
- 使用阵列C 计算三个等级的平均值
- 计算在多维阵列中不起作用
- C 2字节阵列计算(char阵列,字符串和整数)
- C 矢量阵列操作员高计算成本
- CUFFT:当输入是倾斜阵列时,如何计算fft
- 阵列最大间隙的计算
- 三维阵列偏移量计算