{"action":"closed","issue":{"url":"https://api.github.com/repos/coin-or/CppAD/issues/7","labels_url":"https://api.github.com/repos/coin-or/CppAD/issues/7/labels{/name}","comments_url":"https://api.github.com/repos/coin-or/CppAD/issues/7/comments","events_url":"https://api.github.com/repos/coin-or/CppAD/issues/7/events","html_url":"https://github.com/coin-or/CppAD/issues/7","id":58741735,"number":7,"title":"Conditional expressions erroneously skip operations for AD<AD<double> >","user":{"login":"joaoleal","id":512865,"avatar_url":"https://avatars.githubusercontent.com/u/512865?v=3","gravatar_id":"","url":"https://api.github.com/users/joaoleal","html_url":"https://github.com/joaoleal","followers_url":"https://api.github.com/users/joaoleal/followers","following_url":"https://api.github.com/users/joaoleal/following{/other_user}","gists_url":"https://api.github.com/users/joaoleal/gists{/gist_id}","starred_url":"https://api.github.com/users/joaoleal/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/joaoleal/subscriptions","organizations_url":"https://api.github.com/users/joaoleal/orgs","repos_url":"https://api.github.com/users/joaoleal/repos","events_url":"https://api.github.com/users/joaoleal/events{/privacy}","received_events_url":"https://api.github.com/users/joaoleal/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":5,"created_at":"2015-02-24T14:00:56Z","updated_at":"2015-05-27T05:57:04Z","closed_at":"2015-05-27T05:57:04Z","body":"The `optimize()` method in ADFun will determine which operations can be skipped when using conditional expressions so that only the required branch needs to be evaluated.\r\nWhen using `AD<AD<double> >` or `AD<CG<double> >` these operations should not be skipped.\r\n\r\nHere is a file to reproduce the issue:\r\n```\r\n#! /bin/bash -e\r\n# $Id$\r\n# -----------------------------------------------------------------------------\r\n# CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-13 Bradley M. Bell\r\n#\r\n# CppAD is distributed under multiple licenses. This distribution is under\r\n# the terms of the \r\n#                     Eclipse Public License Version 1.0.\r\n#\r\n# A copy of this license is included in the COPYING file of this distribution.\r\n# Please visit http://www.coin-or.org/CppAD/ for information on other licenses.\r\n# -----------------------------------------------------------------------------\r\ncat << EOF\r\nDescription\r\nEOF\r\ncat << EOF > bug.$$\r\n#include <iostream>\r\n#include <cppad/cppad.hpp>\r\nint main(void) {\r\n    using namespace CppAD;\r\n\r\n    typedef AD<double> adouble;\r\n    typedef AD<adouble> a2double;\r\n\r\n    std::vector<double> x{0, 1};\r\n\r\n    /**\r\n     * First tape \r\n     * (using AD<AD<double> >)\r\n     */\r\n    std::vector<a2double> a2x(x.size());\r\n    for (size_t i = 0; i < a2x.size(); i++) {\r\n        a2x[i] = adouble(x[i]);\r\n    }\r\n    Independent(a2x);\r\n\r\n    std::vector<a2double> a2y(1);\r\n    a2double a = a2x[0] * a2x[1];\r\n    a2y[0] = CondExpEq(a2x[0], a2double(1.0), a, a2double(0.0));\r\n\r\n    // create f: X -> Y \r\n    ADFun<adouble> f1(a2x, a2y);\r\n    f1.optimize(); //  <<<<<<<<<<<<<<< causes the issue\r\n\r\n    /**\r\n     * Second tape\r\n     * (using AD<double>)\r\n     */\r\n    std::vector<adouble> ax{adouble(1), adouble(0)};\r\n    Independent(ax);\r\n\r\n    std::vector<adouble> ay = f1.Forward(0, ax);// <<<<<<<<<<<< assertion fails here!\r\n\r\n    CppAD::ADFun<double> f2(ax, ay);\r\n\r\n    /**\r\n     * Use second tape\r\n     */\r\n    x = {1, 0.5};\r\n\r\n    std::vector<double> y = f2.Forward(0, x);\r\n\r\n    //std::cout << y[0] << std::endl;\r\n    assert(std::abs(y[0] - x[0] * x[1]) < 1e-10);\r\n\r\n}\r\nEOF\r\n# -----------------------------------------------------------------------------\r\nif [ ! -e build ]\r\nthen\r\n\tmkdir build\r\nfi\r\ncd build\r\necho \"$0\"\r\nname=`echo $0 | sed -e 's|.*/||' -e 's|\\..*||'`\r\nmv ../bug.$$ $name.cpp\r\necho \"g++ -I../.. --std=c++11 -g $name.cpp -o $name\"\r\ng++ -I../.. --std=c++11 -g $name.cpp -o $name\r\n#\r\necho \"./$name\"\r\nif ./$name\r\nthen\r\n\techo \"OK\"\r\nelse\r\n\techo \"Error\"\r\nfi\r\n```"}}