向量阵列,计算点产品

Vector arrays, calculation dot product

本文关键字:计算 阵列 向量      更新时间:2023-10-16

我正在尝试编写计算两个给定向量的点产物的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;
}