This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Autodiff question

Tags: None
(comma "," separated)
Ianic
Registered Member
Posts
6
Karma
0

Autodiff question

Thu May 12, 2011 3:06 pm
Hi there!

I know that the autodiff functionality is unsupported, but I will try anyhow... I am using it to calculate the jacobian basically using a copy-paste from the relevant parts of the autodiff.cpp test. I found that when a system of equations has a stand-alone constant, the resulting code won't compile, e.g. (taken from autodiff.cpp)

Code: Select all
template<typename _Scalar, int NX=Dynamic, int NY=Dynamic>
struct TestFunc1
{
  typedef _Scalar Scalar;
  enum {
    InputsAtCompileTime = NX,
    ValuesAtCompileTime = NY
  };

  ...

  template<typename T>
  void operator() (const Matrix<T,InputsAtCompileTime,1>& x, Matrix<T,ValuesAtCompileTime,1>* _v) const
  {
    Matrix<T,ValuesAtCompileTime,1>& v = *_v;

    v[0] = 2 * x[0] * x[0] + x[0] * x[1] + 2.0; // <- won't compile with 2.0 present!
    v[1] = 3 * x[1] * x[0] + 0.5 * x[1] * x[1];
  }


Would anyone help me with overcoming this? Thank you in advance!

Yan
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Autodiff question

Thu May 12, 2011 6:46 pm
hm which compiler because here (gcc 4.5) it compiles fine with the "+ 2.0"
Ianic
Registered Member
Posts
6
Karma
0

Re: Autodiff question

Thu May 12, 2011 9:26 pm
I am sorry for the misguidance. When the constant is added, the code works, but when it is subtracted, the compilation fails (gcc 4.6). That is because the "-" operator is missing in the AutoDiffScalar.h file. I added it myself and now everything works! I love open source! Still not sure why it was omitted in the first place...
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Autodiff question

Thu May 12, 2011 9:40 pm
oops, you are right. Fixed in devel branch.
Ianic
Registered Member
Posts
6
Karma
0

Re: Autodiff question

Thu May 12, 2011 10:02 pm
I had a look at the devel branch, you type

Code: Select all
friend inline const AutoDiffScalar<DerType&> operator-(const Scalar& a, const AutoDiffScalar& b)
{
    return AutoDiffScalar<DerType&>(a - b.value(), b.derivatives());
}

Shouldn't there be
Code: Select all
return AutoDiffScalar<DerType&>(a - b.value(), -b.derivatives());

instead?
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Autodiff question

Sat May 14, 2011 9:29 pm
right, I've been too fast. (fix committed)


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], q.ignora, watchstar