mirror of
https://gitlab.com/libeigen/eigen.git
synced 2025-07-26 14:54:30 +08:00
bug #1281: fix AutoDiffScalar's make_coherent for nested expression of constant ADs.
(grafted from b9837ca9aeccb933e410102125fcd475e6cbcada )
This commit is contained in:
parent
f1b1f13d3c
commit
a8d516b04e
@ -453,6 +453,24 @@ struct auto_diff_special_op<_DerType, false>
|
|||||||
void operator+() const;
|
void operator+() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename BinOp, typename A, typename B, typename RefType>
|
||||||
|
void make_coherent_expression(CwiseBinaryOp<BinOp,A,B> xpr, const RefType &ref)
|
||||||
|
{
|
||||||
|
make_coherent(xpr.const_cast_derived().lhs(), ref);
|
||||||
|
make_coherent(xpr.const_cast_derived().rhs(), ref);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename UnaryOp, typename A, typename RefType>
|
||||||
|
void make_coherent_expression(const CwiseUnaryOp<UnaryOp,A> &xpr, const RefType &ref)
|
||||||
|
{
|
||||||
|
make_coherent(xpr.nestedExpression().const_cast_derived(), ref);
|
||||||
|
}
|
||||||
|
|
||||||
|
// needed for compilation only
|
||||||
|
template<typename UnaryOp, typename A, typename RefType>
|
||||||
|
void make_coherent_expression(const CwiseNullaryOp<UnaryOp,A> &, const RefType &)
|
||||||
|
{}
|
||||||
|
|
||||||
template<typename A_Scalar, int A_Rows, int A_Cols, int A_Options, int A_MaxRows, int A_MaxCols, typename B>
|
template<typename A_Scalar, int A_Rows, int A_Cols, int A_Options, int A_MaxRows, int A_MaxCols, typename B>
|
||||||
struct make_coherent_impl<Matrix<A_Scalar, A_Rows, A_Cols, A_Options, A_MaxRows, A_MaxCols>, B> {
|
struct make_coherent_impl<Matrix<A_Scalar, A_Rows, A_Cols, A_Options, A_MaxRows, A_MaxCols>, B> {
|
||||||
typedef Matrix<A_Scalar, A_Rows, A_Cols, A_Options, A_MaxRows, A_MaxCols> A;
|
typedef Matrix<A_Scalar, A_Rows, A_Cols, A_Options, A_MaxRows, A_MaxCols> A;
|
||||||
@ -462,6 +480,10 @@ struct make_coherent_impl<Matrix<A_Scalar, A_Rows, A_Cols, A_Options, A_MaxRows,
|
|||||||
a.resize(b.size());
|
a.resize(b.size());
|
||||||
a.setZero();
|
a.setZero();
|
||||||
}
|
}
|
||||||
|
else if (B::SizeAtCompileTime==Dynamic && a.size()!=0 && b.size()==0)
|
||||||
|
{
|
||||||
|
make_coherent_expression(b,a);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -474,13 +496,17 @@ struct make_coherent_impl<A, Matrix<B_Scalar, B_Rows, B_Cols, B_Options, B_MaxRo
|
|||||||
b.resize(a.size());
|
b.resize(a.size());
|
||||||
b.setZero();
|
b.setZero();
|
||||||
}
|
}
|
||||||
|
else if (A::SizeAtCompileTime==Dynamic && b.size()!=0 && a.size()==0)
|
||||||
|
{
|
||||||
|
make_coherent_expression(a,b);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename A_Scalar, int A_Rows, int A_Cols, int A_Options, int A_MaxRows, int A_MaxCols,
|
template<typename A_Scalar, int A_Rows, int A_Cols, int A_Options, int A_MaxRows, int A_MaxCols,
|
||||||
typename B_Scalar, int B_Rows, int B_Cols, int B_Options, int B_MaxRows, int B_MaxCols>
|
typename B_Scalar, int B_Rows, int B_Cols, int B_Options, int B_MaxRows, int B_MaxCols>
|
||||||
struct make_coherent_impl<Matrix<A_Scalar, A_Rows, A_Cols, A_Options, A_MaxRows, A_MaxCols>,
|
struct make_coherent_impl<Matrix<A_Scalar, A_Rows, A_Cols, A_Options, A_MaxRows, A_MaxCols>,
|
||||||
Matrix<B_Scalar, B_Rows, B_Cols, B_Options, B_MaxRows, B_MaxCols> > {
|
Matrix<B_Scalar, B_Rows, B_Cols, B_Options, B_MaxRows, B_MaxCols> > {
|
||||||
typedef Matrix<A_Scalar, A_Rows, A_Cols, A_Options, A_MaxRows, A_MaxCols> A;
|
typedef Matrix<A_Scalar, A_Rows, A_Cols, A_Options, A_MaxRows, A_MaxCols> A;
|
||||||
typedef Matrix<B_Scalar, B_Rows, B_Cols, B_Options, B_MaxRows, B_MaxCols> B;
|
typedef Matrix<B_Scalar, B_Rows, B_Cols, B_Options, B_MaxRows, B_MaxCols> B;
|
||||||
static void run(A& a, B& b) {
|
static void run(A& a, B& b) {
|
||||||
|
@ -352,6 +352,21 @@ double bug_1264() {
|
|||||||
return v2(0).value();
|
return v2(0).value();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check with expressions on constants
|
||||||
|
double bug_1281() {
|
||||||
|
int n = 2;
|
||||||
|
typedef AutoDiffScalar<VectorXd> AD;
|
||||||
|
const AD c = 1.;
|
||||||
|
AD x0(2,n,0);
|
||||||
|
AD y1 = (AD(c)+AD(c))*x0;
|
||||||
|
y1 = x0 * (AD(c)+AD(c));
|
||||||
|
AD y2 = (-AD(c))+x0;
|
||||||
|
y2 = x0+(-AD(c));
|
||||||
|
AD y3 = (AD(c)*(-AD(c))+AD(c))*x0;
|
||||||
|
y3 = x0 * (AD(c)*(-AD(c))+AD(c));
|
||||||
|
return (y1+y2+y3).value();
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void test_autodiff()
|
void test_autodiff()
|
||||||
@ -367,5 +382,6 @@ void test_autodiff()
|
|||||||
CALL_SUBTEST_5( bug_1223() );
|
CALL_SUBTEST_5( bug_1223() );
|
||||||
CALL_SUBTEST_5( bug_1260() );
|
CALL_SUBTEST_5( bug_1260() );
|
||||||
CALL_SUBTEST_5( bug_1261() );
|
CALL_SUBTEST_5( bug_1261() );
|
||||||
|
CALL_SUBTEST_5( bug_1281() );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user