英文:
Why does C++ Eigen require casting for matrix operations between different scalar type?
问题
例如,如果A是一个双精度矩阵,B是整数矩阵。
A * B引发了编译器错误,必须是A.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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论