gpt4 book ai didi

c++ - 具有自定义标量类型的特征:具有自定义类型的矩阵乘法因 `use of overloaded operator '而失败*' is ambiguous`

转载 作者:行者123 更新时间:2023-12-02 10:15:38 27 4
gpt4 key购买 nike

我正在尝试在official documentation之后将boost.units集成到Eigen中。我也查看了两个执行相同操作的项目,并且我也用它们的代码测试了该操作,尽管我遇到了相同的错误(Project 1 Project 2)。

我遇到的问题是,在将两个矩阵与基于boost::units的自定义标量类型相乘时,乘法运算符似乎存在一些歧义。
clang 10.0.0.3和Apple clang 11.0.3会发生此行为。

触发错误的代码(use of overloaded operator '*' is ambiguous)如下:

    const Eigen::Matrix<boost::units::quantity<boost::units::si::length, double>, 2, 3> x;
const Eigen::Matrix<boost::units::quantity<boost::units::si::dimensionless, double>, 3, 1> y;
const Eigen::Matrix<boost::units::quantity<boost::units::si::length, double>, 2, 1> result = x * y;

对于集成,我创建了包含 NumTraitsboost::units::quantity<T>的文件,如下所示:
namespace Eigen {

template<class T> struct NumTraits<boost::units::quantity<T>> : NumTraits<double> {
typedef boost::units::quantity<T> Real;
typedef boost::units::quantity<T> NonInteger;
typedef boost::units::quantity<T> Nested;

enum {
IsComplex = 0,
IsInteger = 0,
IsSigned = 1,
RequireInitialization = 1,
ReadCost = 1,
AddCost = 3,
MulCost = 3
};
};
} // namespace Eigen

对于乘法支持,我具有以下 struct定义:
namespace Eigen {

namespace units = boost::units;

template<typename scalarA, typename scalarB>
struct ScalarBinaryOpTraits<units::quantity<scalarA>, scalarB,
internal::scalar_product_op<units::quantity<scalarA>, scalarB>> {
typedef units::quantity<typename units::multiply_typeof_helper<scalarA, scalarB>::type> ReturnType;
};

template<typename scalarA, typename scalarB>
struct ScalarBinaryOpTraits<scalarA, units::quantity<scalarB>,
internal::scalar_product_op<scalarA, units::quantity<scalarB>>> {
typedef units::quantity<typename units::multiply_typeof_helper<scalarA, scalarB>::type> ReturnType;
};

template<typename scalarA, typename scalarB>
struct ScalarBinaryOpTraits<scalarA, units::quantity<scalarB>,
internal::scalar_product_op<units::quantity<scalarA>, units::quantity<scalarB>>> {
typedef units::quantity<typename units::multiply_typeof_helper<scalarA, scalarB>::type> ReturnType;
};

template<typename scalarA, typename scalarB>
struct ScalarBinaryOpTraits<units::quantity<scalarA>, scalarB,
internal::scalar_conj_product_op<units::quantity<scalarA>, scalarB>> {
typedef typename units::multiply_typeof_helper<scalarA, scalarB>::type ReturnType;
};

template<typename scalarA, typename scalarB>
struct ScalarBinaryOpTraits<scalarA, units::quantity<scalarB>,
internal::scalar_conj_product_op<scalarA, units::quantity<scalarB>>> {
typedef typename units::multiply_typeof_helper<scalarA, scalarB>::type ReturnType;
};

template<typename scalarA, typename scalarB>
struct ScalarBinaryOpTraits<units::quantity<scalarA>, units::quantity<scalarB>,
internal::scalar_conj_product_op<units::quantity<scalarA>, units::quantity<scalarB>>> {
typedef units::quantity<typename units::multiply_typeof_helper<scalarA, scalarB>::type> ReturnType;
};

} // namespace Eigen

完整的错误输出:
Scanning dependencies of target LaserCalibration
[ 8%] Building CXX object CMakeFiles/LaserCalibration.dir/maths/Maths.cpp.o
/Users/dwright/projects/dv-laser-calibration/maths/Maths.cpp:9:97: error: use of overloaded operator '*' is ambiguous (with operand types 'const Eigen::Matrix<boost::units::quantity<boost::units::si::length, double>, 2, 3>' (aka 'const Matrix<quantity<unit<list<dim<boost::units::length_base_dimension, static_rational<1> >, boost::units::dimensionless_type>, homogeneous_system<list<boost::units::si::meter_base_unit, boost::units::list<boost::units::scaled_base_unit<boost::units::cgs::gram_base_unit, boost::units::scale<10, static_rational<3> > >, boost::units::list<boost::units::si::second_base_unit, boost::units::list<boost::units::si::ampere_base_unit, boost::units::list<boost::units::si::kelvin_base_unit, boost::units::list<boost::units::si::mole_base_unit, boost::units::list<boost::units::si::candela_base_unit, boost::units::list<boost::units::angle::radian_base_unit, boost::units::list<boost::units::angle::steradian_base_unit, boost::units::dimensionless_type> > > > > > > > > > >, double>, 2, 3>') and 'const Eigen::Matrix<boost::units::quantity<boost::units::si::dimensionless, double>, 3, 1>' (aka 'const Matrix<quantity<unit<boost::units::dimensionless_type, homogeneous_system<list<boost::units::si::meter_base_unit, boost::units::list<boost::units::scaled_base_unit<boost::units::cgs::gram_base_unit, boost::units::scale<10, static_rational<3> > >, boost::units::list<boost::units::si::second_base_unit, boost::units::list<boost::units::si::ampere_base_unit, boost::units::list<boost::units::si::kelvin_base_unit, boost::units::list<boost::units::si::mole_base_unit, boost::units::list<boost::units::si::candela_base_unit, boost::units::list<boost::units::angle::radian_base_unit, boost::units::list<boost::units::angle::steradian_base_unit, boost::units::dimensionless_type> > > > > > > > > > >, double>, 3, 1>'))
const Eigen::Matrix<boost::units::quantity<boost::units::si::length, double>, 2, 1> result = x * y;
~ ^ ~
/usr/local/include/eigen3/Eigen/src/Core/../plugins/CommonCwiseBinaryOps.h:50:29: note: candidate function [with T = Eigen::Matrix<boost::units::quantity<boost::units::unit<boost::units::list<boost::units::dim<boost::units::length_base_dimension, boost::units::static_rational<1, 1> >, boost::units::dimensionless_type>, boost::units::homogeneous_system<boost::units::list<boost::units::si::meter_base_unit, boost::units::list<boost::units::scaled_base_unit<boost::units::cgs::gram_base_unit, boost::units::scale<10, static_rational<3> > >, boost::units::list<boost::units::si::second_base_unit, boost::units::list<boost::units::si::ampere_base_unit, boost::units::list<boost::units::si::kelvin_base_unit, boost::units::list<boost::units::si::mole_base_unit, boost::units::list<boost::units::si::candela_base_unit, boost::units::list<boost::units::angle::radian_base_unit, boost::units::list<boost::units::angle::steradian_base_unit, boost::units::dimensionless_type> > > > > > > > > >, void>, double>, 2, 3, 0, 2, 3>]
EIGEN_MAKE_SCALAR_BINARY_OP(operator*,product)
^
/usr/local/include/eigen3/Eigen/src/Core/../plugins/CommonCwiseBinaryOps.h:50:29: note: candidate function [with T = Eigen::Matrix<boost::units::quantity<boost::units::unit<boost::units::dimensionless_type, boost::units::homogeneous_system<boost::units::list<boost::units::si::meter_base_unit, boost::units::list<boost::units::scaled_base_unit<boost::units::cgs::gram_base_unit, boost::units::scale<10, static_rational<3> > >, boost::units::list<boost::units::si::second_base_unit, boost::units::list<boost::units::si::ampere_base_unit, boost::units::list<boost::units::si::kelvin_base_unit, boost::units::list<boost::units::si::mole_base_unit, boost::units::list<boost::units::si::candela_base_unit, boost::units::list<boost::units::angle::radian_base_unit, boost::units::list<boost::units::angle::steradian_base_unit, boost::units::dimensionless_type> > > > > > > > > >, void>, double>, 3, 1, 0, 3, 1>]
/usr/local/include/eigen3/Eigen/src/Core/MatrixBase.h:166:5: note: candidate function [with OtherDerived = Eigen::Matrix<boost::units::quantity<boost::units::unit<boost::units::dimensionless_type, boost::units::homogeneous_system<boost::units::list<boost::units::si::meter_base_unit, boost::units::list<boost::units::scaled_base_unit<boost::units::cgs::gram_base_unit, boost::units::scale<10, static_rational<3> > >, boost::units::list<boost::units::si::second_base_unit, boost::units::list<boost::units::si::ampere_base_unit, boost::units::list<boost::units::si::kelvin_base_unit, boost::units::list<boost::units::si::mole_base_unit, boost::units::list<boost::units::si::candela_base_unit, boost::units::list<boost::units::angle::radian_base_unit, boost::units::list<boost::units::angle::steradian_base_unit, boost::units::dimensionless_type> > > > > > > > > >, void>, double>, 3, 1, 0, 3, 1>]
operator*(const MatrixBase<OtherDerived> &other) const;
^
/Users/dwright/projects/dv-laser-calibration/maths/Maths.cpp:6:106: warning: unused parameter 'event' [-Wunused-parameter]
Eigen::Matrix<boost::units::quantity<double>, 2, 1> Maths::convertToPhysicalCoordinates(const dv::Event &event) const {
^
In file included from /Users/dwright/projects/dv-laser-calibration/maths/Maths.cpp:1:
/Users/dwright/projects/dv-laser-calibration/maths/Maths.hpp:16:25: warning: private field 'parameters' is not used [-Wunused-private-field]
CalibrationParameters &parameters;
^
2 warnings and 1 error generated.
make[2]: *** [CMakeFiles/LaserCalibration.dir/maths/Maths.cpp.o] Error 1
make[1]: *** [CMakeFiles/LaserCalibration.dir/all] Error 2
make: *** [all] Error 2
11:04:41: The process "/usr/local/Cellar/cmake/3.17.2/bin/cmake" exited with code 2.
Error while building/deploying project LaserCalibration (kit: Imported Kit)
When executing step "CMake Build"
11:04:41: Elapsed time: 00:02.

最佳答案

忽略共轭乘积特征,我可以看到第三个特化似乎已经消失了:

template <typename scalarA, typename scalarB>
struct ScalarBinaryOpTraits<
scalarA, units::quantity<scalarB>,
internal::scalar_product_op<units::quantity<scalarA>,
units::quantity<scalarB>>> {
typedef units::quantity<
typename units::multiply_typeof_helper<scalarA, scalarB>::type>
ReturnType;
};

该行:
    scalarA, units::quantity<scalarB>,

应该阅读:
    units::quantity<scalarA>, units::quantity<scalarB>,

消除原因

我们刚刚确定的第三个专业显然应该与我们的情况相匹配。但是,其他专长不排除两个标量都是数量的情况。

注释掉不相关的两个确实使事情顺利了:

Live On Compiler Explorer
#include <boost/units/unit.hpp>
#include <boost/units/io.hpp>
#include <boost/units/limits.hpp>
#include <boost/units/quantity.hpp>
#include <boost/units/systems/si.hpp>
#include <boost/units/cmath.hpp>
#include <Eigen/Core>

namespace Eigen {
namespace units = boost::units;

#if 0
template <typename scalarA, typename scalarB>
struct ScalarBinaryOpTraits<
units::quantity<scalarA>, scalarB,
internal::scalar_product_op<units::quantity<scalarA>, scalarB>> {
typedef units::quantity<
typename units::multiply_typeof_helper<scalarA, scalarB>::type>
ReturnType;
};
#endif

#if 0
template <typename scalarA, typename scalarB>
struct ScalarBinaryOpTraits<
scalarA, units::quantity<scalarB>,
internal::scalar_product_op<scalarA, units::quantity<scalarB>>> {
typedef units::quantity<
typename units::multiply_typeof_helper<scalarA, scalarB>::type>
ReturnType;
};
#endif

#if 1
template <typename scalarA, typename scalarB>
struct ScalarBinaryOpTraits<
units::quantity<scalarA>, units::quantity<scalarB>,
internal::scalar_product_op<units::quantity<scalarA>,
units::quantity<scalarB>>> {
typedef units::quantity<
typename units::multiply_typeof_helper<scalarA, scalarB>::type>
ReturnType;
};
#endif
} // namespace Eigen

#include <iostream>
int main() {
using V = boost::units::quantity<boost::units::si::length, double>;
using U = boost::units::quantity<boost::units::si::dimensionless, double>;
constexpr auto Vu = boost::units::si::meter;
constexpr auto Uu = 1.0;

Eigen::Matrix<V, 2, 3> x;
Eigen::Matrix<U, 3, 1> y;

x << 1*Vu, 2*Vu, 3*Vu, 4*Vu, 5*Vu, 6*Vu;
y << 1*Uu, 2*Uu, 3*Uu;

std::cout << "x:\n" << x << "\n";
std::cout << "y:\n" << y << "\n";
auto result = (x * y).eval();

std::cout << "result:\n" << result << "\n";
}

版画
x:
1 m 2 m 3 m
4 m 5 m 6 m
y:
1 dimensionless
2 dimensionless
3 dimensionless
result:
14 m
32 m

固定?

我会尝试将所有三个结合在一起。但是 ScalarBinaryOpTraits不支持SFINAE。尽管如此,我还是试图让事情变得顺利:
namespace Eigen {
namespace units = boost::units;

namespace /*file-static*/ {
template <typename A, typename B, typename R = units::multiply_typeof_helper<A, B> >
struct QuantityProductImpl {
using ReturnType = typename R::type;
};
}

template <typename A, typename B>
struct ScalarBinaryOpTraits<A, B,
std::enable_if_t< units::is_quantity<A>::value && units::is_quantity<B>::value,
internal::scalar_product_op<A, B>
>
> : QuantityProductImpl<A, B> { };
} // namespace Eigen

哪个确实像以前一样工作: Live On Compiler Explorer

但是,如果将 &&更改为 ||(或XOR逻辑的 !=),我们将再次变得模棱两可。不知道如何进行,但是也许可以给您一些想法?

关于c++ - 具有自定义标量类型的特征:具有自定义类型的矩阵乘法因 `use of overloaded operator '而失败*' is ambiguous`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62040237/

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