coin-or / CppAD

A C++ Algorithmic Differentiation Package: Home Page
https://cppad.readthedocs.io
Other
446 stars 94 forks source link

ADFun::optimize gives compile error with std::complex #205

Closed pvir closed 1 month ago

pvir commented 2 months ago

The following code fails with compilation error. This used to work with 20230000.0 but fails since 20240000.0:

#include <vector>
#include <iostream>
#include <cppad/cppad.hpp>

typedef std::complex<double> Complex;
typedef CppAD::AD<Complex> ADComplex;

int main()
{
    std::vector<ADComplex> a(1), b(1);

    CppAD::Independent(a);
    b[0] = a[0];
    CppAD::ADFun<Complex> f{a, b};

    f.optimize();   // If this line is commented out, the code compiles and runs

    std::vector<Complex> da(1), db(1);
    da[0] = 1;
    db = f.Forward(1, da);

    std::cout << db[0] << std::endl;
    return 0;
}

Similar error occurs if I change one of the examples as follows:

diff --git a/example/general/complex_poly.cpp b/example/general/complex_poly.cpp
index 5c1ba4a2c..7134cb8c8 100644
--- a/example/general/complex_poly.cpp
+++ b/example/general/complex_poly.cpp
@@ -56,6 +56,7 @@ bool complex_poly(void)

    // create f: Z -> P and vectors used for derivative calculations
    CppAD::ADFun<Complex> f(Z, P);
+   f.optimize();
    CPPAD_TESTVECTOR(Complex) v( f.Domain() );
    CPPAD_TESTVECTOR(Complex) w( f.Range() );

Error:

$ pkgconf --modversion cppad
20240000.3
$ g++ --version|head -n1
g++ (GCC) 13.1.1 20230614 (Red Hat 13.1.1-4)
$ g++ -o test `pkgconf --cflags --libs cppad` test.cpp
In file included from /usr/include/cppad/local/val_graph/enable_parallel.hpp:16,
                 from /usr/include/cppad/core/parallel_ad.hpp:79,
                 from /usr/include/cppad/core/ad_fun.hpp:868,
                 from /usr/include/cppad/cppad.hpp:53,
                 from test.cpp:3:
/usr/include/cppad/local/val_graph/pri_op.hpp: In instantiation of 'void CppAD::local::val_graph::pri_op_t<Value>::eval(const CppAD::local::val_graph::tape_t<Base>*, bool, CppAD::local::val_graph::addr_t, CppAD::local::val_graph::addr_t, CppAD::local::val_graph::Vector<Value>&, CppAD::local::val_graph::Vector<CppAD::vector<long unsigned int> >&, size_t&) const [with Value = std::complex<double>; CppAD::local::val_graph::addr_t = long unsigned int; CppAD::local::val_graph::Vector<Value> = CppAD::vector<std::complex<double> >; CppAD::local::val_graph::Vector<CppAD::vector<long unsigned int> > = CppAD::vector<CppAD::vector<long unsigned int> >; size_t = long unsigned int]':
/usr/include/cppad/local/val_graph/pri_op.hpp:149:9:   required from here
/usr/include/cppad/local/val_graph/pri_op.hpp:181:16: error: no match for 'operator<=' (operand types are 'const std::complex<double>' and 'std::complex<double>')
  181 |       if( flag <= Value(0) )
      |           ~~~~~^~~~~~~~~~~
...

err.txt

bradbell commented 2 months ago

I verified the error above. The problem is the val_graph optimizer requires <= comparison between Base types; see val_graph on https://cppad.readthedocs.io/latest/optimize.html#val-graph

It is not even being used by this example and so I should be able fix this.

bradbell commented 1 month ago

Please try the master branch and see if your problem is fixed there. If it is, I will port the fix to the 2024 stable branch.

I think that I have fixed the problem with the commit https://github.com/coin-or/CppAD/commit/5bef0ad2fe56db9923678395ff6d0bae117a46f6 See the heading 05-12 on https://cppad.readthedocs.io/latest/2024.html#mm-dd-05-12

In particular, the following test is now passing: https://github.com/coin-or/CppAD/blob/master/test_more/general/base_complex.cpp

pvir commented 1 month ago

Thank you, my problem is fixed with this change also in the real use case.

bradbell commented 1 month ago

The fix has been ported to the following release: https://github.com/coin-or/CppAD/releases/tag/20240000.5

I recreated this release on 1:20pm GMT because the workflows needed to be updated to work on the current github.