gpt4 book ai didi

c++ - 制作对 inner_product 的自定义调用 (C++ STL)

转载 作者:太空狗 更新时间:2023-10-29 20:45:04 26 4
gpt4 key购买 nike

我无法理解如何使用 inner_product结合 std::vector<float>和一个 std::vector<std::vector<float>> .给定,例如 <2,3><<4,5>,<6,7>> , 我想要 inner_product生产

2*<4,5> + 3*<6,7> = <8,10> + <18,21> = <26,31>.

假设

vector<float> foo;

vector<vector<float>> bar;

被初始化并且大小相等,我不知道是什么UK1 , UK2 , 和 UK3

vector<float> ip = 
inner_product(foo.begin(), foo.end(), bar.begin(), UK1, UK2, UK3);

应该是。我怀疑UK1应该是 vector充满0.0f s,与 vector 大小相同在bar . UK3也许应该是这样的

std::transform(UK4.begin(), UK4.end(), UK4.begin(),
std::bind1st(std::multiplies<float>(), UK5));

我想 UK2应该以某种方式代表组件 vector<float>添加!

我什至不想考虑当 bar 中的 vector 时这会变得多么复杂。被 float 类的对象替换属性...

最佳答案

像这样定义加法和乘法函数:

static vector<float> add(const vector<float> &a,const vector<float> &b)
{
assert(a.size()==b.size());
size_t n = a.size();
vector<float> result(n);
for (size_t i=0; i!=n; ++i) {
result[i] = a[i]+b[i];
}
return result;
}

static vector<float> mul(const float &a,const vector<float> &b)
{
size_t n = b.size();
vector<float> result = b;
for (size_t i=0; i!=n; ++i) {
result[i] *= a;
}
return result;
}

然后像这样使用它:

vector<float> zero(2,0);
vector<float> result =
inner_product(a.begin(),a.end(),b.begin(),zero,add,mul);

关于c++ - 制作对 inner_product 的自定义调用 (C++ STL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11688981/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com