C++ Eigen需要为不同标量类型之间的矩阵操作进行类型转换的原因是什么?

huangapple go评论71阅读模式
英文:

Why does C++ Eigen require casting for matrix operations between different scalar type?

问题

例如,如果A是一个双精度矩阵,B是整数矩阵。
A * B引发了编译器错误,必须是A.cast() * B或A * B.cast()。

为什么Eigen要求这样做?
它本可以遵循C++的double * int = double规则。

对于相同标量类型的操作有性能优化吗?

非常感谢!

英文:

For example, if A is a double matrix, B is int matrix.
A * B raises compiler error and it hast to be A.cast<int>() * B or A * B.cast<double>().

Why does eigen require this?
It could have followed double * int = double convention of C++.

Is there a performance optimization for operation of the same scalar type?

Thank you very much!

答案1

得分: 4

整数和浮点数的基本自动转换是C/C++程序中大量错误的根源。

因此,Eigen决定要求显式转换。

是的,矩阵乘法很可能会针对两侧相同类型进行优化。 针对不同类型进行优化会产生许多不同情况;对于10种类型,必须生成和优化100种不同的有序对。

相比之下,如果它们是相同类型的话,只有10种情况。

而15种类型是相当合理的:(有符号/无符号) x (8、16、32、64、128) 位整数,然后是8、16、32、64、128位浮点数类型。

英文:

The basic integer and floating point automatic conversions are a source of a large number of bugs in C/C++ programs.

So Eigen decided to require explicit conversion.

And yes, the odds are that matrix multiplication is going to be optimized for identical types on both sides. Optimizing for non-identical types results in a lot of different cases; with 10 types, there are 100 different ordered pairs that would have to be generated and optimized.

In comparison, just 10 if they are the same type.

And 15 types is quite plausible: (signed/unsigned) x (8, 16, 32, 64, 128) bit integers, then 8, 16, 32, 64, 128 bit floating point types.

huangapple
  • 本文由 发表于 2023年3月4日 00:47:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75629787.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定