Lines Matching +full:boost +full:- +full:root
1 // Copyright (C) 2016-2018 T. Zachary Laine
3 // Distributed under the Boost Software License, Version 1.0. (See
5 // http://www.boost.org/LICENSE_1_0.txt)
10 #include <boost/yap/algorithm.hpp>
11 #include <boost/polymorphic_cast.hpp>
12 #include <boost/hana/for_each.hpp>
15 #include <boost/test/included/unit_test.hpp>
18 double const Epsilon = 10.0e-6;
24 template <boost::yap::expr_kind Kind, typename Tuple>
27 static boost::yap::expr_kind const kind = Kind;
53 autodiff_expr<boost::yap::expr_kind::terminal, boost::hana::tuple<OPCODE>>
56 autodiff_expr {boost::hana::tuple<OPCODE>{Opcode}} in autodiff_fn_expr()
71 // Create a var-node for each placeholder when we see it for the first
74 Node * operator() (boost::yap::expr_tag<boost::yap::expr_kind::terminal>, in operator ()()
75 boost::yap::placeholder<I>) in operator ()()
79 auto & retval = list_[I - 1]; in operator ()()
85 // Create a param-node for every numeric terminal in the expression.
86 Node * operator() (boost::yap::expr_tag<boost::yap::expr_kind::terminal>, double x) in operator ()()
91 Node * operator() (boost::yap::expr_tag<boost::yap::expr_kind::call>, in operator ()()
96 boost::yap::transform(boost::yap::as_expr<autodiff_expr>(expr), *this) in operator ()()
101 Node * operator() (boost::yap::expr_tag<boost::yap::expr_kind::negate>, in operator ()()
106 boost::yap::transform(boost::yap::as_expr<autodiff_expr>(expr), *this) in operator ()()
111 static OPCODE op_for_kind (boost::yap::expr_kind kind) in op_for_kind()
114 case boost::yap::expr_kind::plus: return OP_PLUS; in op_for_kind()
115 case boost::yap::expr_kind::minus: return OP_MINUS; in op_for_kind()
116 case boost::yap::expr_kind::multiplies: return OP_TIMES; in op_for_kind()
117 case boost::yap::expr_kind::divides: return OP_DIVID; in op_for_kind()
125 template <boost::yap::expr_kind Kind, typename Expr1, typename Expr2>
126 Node * operator() (boost::yap::expr_tag<Kind>, Expr1 const & expr1, Expr2 const & expr2) in operator ()()
130 boost::yap::transform(boost::yap::as_expr<autodiff_expr>(expr1), *this), in operator ()()
131 boost::yap::transform(boost::yap::as_expr<autodiff_expr>(expr2), *this) in operator ()()
146 retval = boost::yap::transform(expr, xform{list}); in to_auto_diff_node()
150 // Fill in the values of the value-nodes in list with the "args" in to_auto_diff_node()
153 boost::hana::for_each( in to_auto_diff_node()
154 boost::hana::make_tuple(args ...), in to_auto_diff_node()
157 VNode * v = boost::polymorphic_downcast<VNode *>(n); in to_auto_diff_node()
158 v->val = x; in to_auto_diff_node()
178 //f(x1,x2,x3) = -5*x1+sin(10)*x1+10*x2-x3/6 in BOOST_FIXTURE_TEST_SUITE()
179 PNode* v5 = create_param_node(-5); in BOOST_FIXTURE_TEST_SUITE()
193 OPNode* op8 = create_binary_op_node(OP_MINUS,op6,op7); //op8 = op6 - op7 in BOOST_FIXTURE_TEST_SUITE()
194 x1->val = -1.9; in BOOST_FIXTURE_TEST_SUITE()
195 x2->val = 2; in BOOST_FIXTURE_TEST_SUITE()
196 x3->val = 5./6.; in BOOST_FIXTURE_TEST_SUITE()
207 //f(x1,x2,x3) = -5*x1+sin(10)*x1+10*x2-x3/6 in build_linear_fun1()
210 -5 * 1_p + sin_(10) * 1_p + 10 * 2_p - 3_p / 6, in build_linear_fun1()
212 -1.9, in build_linear_fun1()
221 //f(x1,x2,x3) = -5*x1+-10*x1+10*x2-x3/6 in build_linear_function2_manually()
222 PNode* v5 = create_param_node(-5); in build_linear_function2_manually()
232 OPNode* op2 = create_uary_op_node(OP_NEG,v10); //op2 = -v10 in build_linear_function2_manually()
238 OPNode* op8 = create_binary_op_node(OP_MINUS,op6,op7);//op8 = op6 - op7 in build_linear_function2_manually()
239 x1->val = -1.9; in build_linear_function2_manually()
240 x2->val = 2; in build_linear_function2_manually()
241 x3->val = 5./6.; in build_linear_function2_manually()
247 //f(x1,x2,x3) = -5*x1+-10*x1+10*x2-x3/6 in build_linear_function2()
249 auto ten = boost::yap::make_terminal<autodiff_expr>(10); in build_linear_function2()
251 -5 * 1_p + -ten * 1_p + 10 * 2_p - 3_p / 6, in build_linear_function2()
253 -1.9, in build_linear_function2()
261 // (x1*x2 * sin(x1))/x3 + x2*x4 - x1/x2 in build_nl_function1_manually()
266 x1->val = -1.23; in build_nl_function1_manually()
267 x2->val = 7.1231; in build_nl_function1_manually()
268 x3->val = 2; in build_nl_function1_manually()
269 x4->val = -10; in build_nl_function1_manually()
288 // (x1*x2 * sin(x1))/x3 + x2*x4 - x1/x2 in build_nl_function1()
291 (1_p * 2_p * sin_(1_p)) / 3_p + 2_p * 4_p - 1_p / 2_p, in build_nl_function1()
293 -1.23, in build_nl_function1()
296 -10 in build_nl_function1()
304 Node* root = build_linear_fun1(list); in BOOST_AUTO_TEST_CASE() local
306 double val1 = grad_reverse(root,list,grad); in BOOST_AUTO_TEST_CASE()
307 double val2 = eval_function(root); in BOOST_AUTO_TEST_CASE()
308 double x1g[] = {-5.5440211108893697744548489936278,10.0,-0.16666666666666666666666666666667}; in BOOST_AUTO_TEST_CASE()
321 nonlinearEdges(root,s); in BOOST_AUTO_TEST_CASE()
330 x1->val = 10; in BOOST_AUTO_TEST_CASE()
331 OPNode* root = create_uary_op_node(OP_SIN,x1); in BOOST_AUTO_TEST_CASE() local
335 grad_reverse(root,nodes,grad); in BOOST_AUTO_TEST_CASE()
336 double x1g = -0.83907152907645244; in BOOST_AUTO_TEST_CASE()
337 //the matlab give cos(10) = -0.839071529076452 in BOOST_AUTO_TEST_CASE()
343 nonlinearEdges(root,s); in BOOST_AUTO_TEST_CASE()
351 x1->val = -2; in BOOST_AUTO_TEST_CASE()
357 CHECK_CLOSE(val,-2); in BOOST_AUTO_TEST_CASE()
367 PNode* p = create_param_node(-10); in BOOST_AUTO_TEST_CASE()
371 CHECK_CLOSE(val,-10); in BOOST_AUTO_TEST_CASE()
382 x1->val = 10; in BOOST_AUTO_TEST_CASE()
383 PNode* p2 = create_param_node(-1); in BOOST_AUTO_TEST_CASE()
387 Node* root = create_binary_op_node(OP_TIMES,x1,p2); in BOOST_AUTO_TEST_CASE() local
388 grad_reverse(root,nodes,grad); in BOOST_AUTO_TEST_CASE()
389 CHECK_CLOSE(grad[0],-1); in BOOST_AUTO_TEST_CASE()
394 root = create_uary_op_node(OP_NEG,x1); in BOOST_AUTO_TEST_CASE()
395 grad_reverse(root,nodes,grad); in BOOST_AUTO_TEST_CASE()
396 CHECK_CLOSE(grad[0],-1); in BOOST_AUTO_TEST_CASE()
400 nonlinearEdges(root,s); in BOOST_AUTO_TEST_CASE()
408 Node* root = build_nl_function1(list); in BOOST_AUTO_TEST_CASE() local
409 double val = eval_function(root); in BOOST_AUTO_TEST_CASE()
411 grad_reverse(root,list,grad); in BOOST_AUTO_TEST_CASE()
412 double eval =-66.929555552886214; in BOOST_AUTO_TEST_CASE()
413 double gx[] = {-4.961306690356109,-9.444611307649055,-2.064383410399700,7.123100000000000}; in BOOST_AUTO_TEST_CASE()
420 unsigned int nzgrad = nzGrad(root); in BOOST_AUTO_TEST_CASE()
421 unsigned int tol = numTotalNodes(root); in BOOST_AUTO_TEST_CASE()
426 nonlinearEdges(root,s); in BOOST_AUTO_TEST_CASE()
434 Node* root = build_linear_fun1(nodes); in BOOST_AUTO_TEST_CASE() local
436 double val = grad_reverse(root,nodes,grad); in BOOST_AUTO_TEST_CASE()
437 double eval = eval_function(root); in BOOST_AUTO_TEST_CASE()
444 static_cast<VNode*>(nodes[i])->u = 0; in BOOST_AUTO_TEST_CASE()
447 static_cast<VNode*>(nodes[0])->u = 1; in BOOST_AUTO_TEST_CASE()
450 hval = hess_reverse(root,nodes,dhess); in BOOST_AUTO_TEST_CASE()
461 Node* root = build_linear_function2(nodes); in BOOST_AUTO_TEST_CASE() local
463 double val = grad_reverse(root,nodes,grad); in BOOST_AUTO_TEST_CASE()
464 double eval = eval_function(root); in BOOST_AUTO_TEST_CASE()
469 static_cast<VNode*>(nodes[i])->u = 0; in BOOST_AUTO_TEST_CASE()
472 static_cast<VNode*>(nodes[0])->u = 1; in BOOST_AUTO_TEST_CASE()
475 hval = hess_reverse(root,nodes,dhess); in BOOST_AUTO_TEST_CASE()
484 nonlinearEdges(root,s); in BOOST_AUTO_TEST_CASE()
492 // Node* root = build_nl_function1(nodes); in BOOST_AUTO_TEST_CASE()
496 x1->val = 1; in BOOST_AUTO_TEST_CASE()
497 x1->u =1; in BOOST_AUTO_TEST_CASE()
499 Node* root = create_uary_op_node(OP_SIN,op); in BOOST_AUTO_TEST_CASE() local
501 double eval = eval_function(root); in BOOST_AUTO_TEST_CASE()
503 double hval = hess_reverse(root,nodes,dhess); in BOOST_AUTO_TEST_CASE()
506 CHECK_CLOSE(dhess[0], -0.778395788418109); in BOOST_AUTO_TEST_CASE()
509 nonlinearEdges(root,s); in BOOST_AUTO_TEST_CASE()
521 x1->val = 2.5; in BOOST_AUTO_TEST_CASE()
522 x2->val = -9; in BOOST_AUTO_TEST_CASE()
524 Node* root = create_binary_op_node(OP_TIMES,x1,op1); in BOOST_AUTO_TEST_CASE() local
525 double eval = eval_function(root); in BOOST_AUTO_TEST_CASE()
528 static_cast<VNode*>(nodes[i])->u = 0; in BOOST_AUTO_TEST_CASE()
530 static_cast<VNode*>(nodes[0])->u = 1; in BOOST_AUTO_TEST_CASE()
533 double hval = hess_reverse(root,nodes,dhess); in BOOST_AUTO_TEST_CASE()
536 double hx[]={-18,5}; in BOOST_AUTO_TEST_CASE()
544 nonlinearEdges(root,s); in BOOST_AUTO_TEST_CASE()
556 x1->val = 2.5; in BOOST_AUTO_TEST_CASE()
557 x2->val = -9; in BOOST_AUTO_TEST_CASE()
562 Node* root = create_binary_op_node(OP_TIMES,op3,op4); in BOOST_AUTO_TEST_CASE() local
564 double eval = eval_function(root); in BOOST_AUTO_TEST_CASE()
568 static_cast<VNode*>(nodes[i])->u = 0; in BOOST_AUTO_TEST_CASE()
570 static_cast<VNode*>(nodes[0])->u = 1; in BOOST_AUTO_TEST_CASE()
573 double hval = hess_reverse(root,nodes,dhess); in BOOST_AUTO_TEST_CASE()
583 static_cast<VNode*>(nodes[i])->u = 0; in BOOST_AUTO_TEST_CASE()
585 static_cast<VNode*>(nodes[1])->u = 1; in BOOST_AUTO_TEST_CASE()
587 double hx2[] = {0, -972}; in BOOST_AUTO_TEST_CASE()
588 hval = hess_reverse(root,nodes,dhess); in BOOST_AUTO_TEST_CASE()
595 nonlinearEdges(root,s); in BOOST_AUTO_TEST_CASE()
602 // Node* root = build_nl_function1(nodes); in BOOST_AUTO_TEST_CASE()
608 x1->val = 2.5; in BOOST_AUTO_TEST_CASE()
609 x2->val = -9; in BOOST_AUTO_TEST_CASE()
610 Node* root = create_binary_op_node(OP_POW,x1,x2); in BOOST_AUTO_TEST_CASE() local
612 double eval = eval_function(root); in BOOST_AUTO_TEST_CASE()
614 static_cast<VNode*>(nodes[0])->u=1;static_cast<VNode*>(nodes[1])->u=0; in BOOST_AUTO_TEST_CASE()
616 double hval = hess_reverse(root,nodes,dhess); in BOOST_AUTO_TEST_CASE()
618 double hx1[] ={0.003774873600000 , -0.000759862823419}; in BOOST_AUTO_TEST_CASE()
619 double hx2[] ={-0.000759862823419, 0.000220093141567}; in BOOST_AUTO_TEST_CASE()
624 static_cast<VNode*>(nodes[0])->u=0;static_cast<VNode*>(nodes[1])->u=1; in BOOST_AUTO_TEST_CASE()
625 hess_reverse(root,nodes,dhess); in BOOST_AUTO_TEST_CASE()
632 nonlinearEdges(root,s); in BOOST_AUTO_TEST_CASE()
640 Node* root = build_nl_function1(nodes); in BOOST_AUTO_TEST_CASE() local
642 double eval = eval_function(root); in BOOST_AUTO_TEST_CASE()
646 double hx0[] ={-1.747958066718855, in BOOST_AUTO_TEST_CASE()
647 -0.657091724418110, in BOOST_AUTO_TEST_CASE()
650 double hx1[] ={ -0.657091724418110, in BOOST_AUTO_TEST_CASE()
652 -0.289815306593997, in BOOST_AUTO_TEST_CASE()
655 -0.289815306593997, in BOOST_AUTO_TEST_CASE()
661 static_cast<VNode*>(nodes[i])->u = 0; in BOOST_AUTO_TEST_CASE()
663 static_cast<VNode*>(nodes[0])->u = 1; in BOOST_AUTO_TEST_CASE()
664 double hval = hess_reverse(root,nodes,dhess); in BOOST_AUTO_TEST_CASE()
672 static_cast<VNode*>(nodes[i])->u = 0; in BOOST_AUTO_TEST_CASE()
674 static_cast<VNode*>(nodes[1])->u = 1; in BOOST_AUTO_TEST_CASE()
675 hess_reverse(root, nodes, dhess); in BOOST_AUTO_TEST_CASE()
681 static_cast<VNode*>(nodes[i])->u = 0; in BOOST_AUTO_TEST_CASE()
683 static_cast<VNode*>(nodes[2])->u = 1; in BOOST_AUTO_TEST_CASE()
684 hess_reverse(root, nodes, dhess); in BOOST_AUTO_TEST_CASE()
690 static_cast<VNode*>(nodes[i])->u = 0; in BOOST_AUTO_TEST_CASE()
692 static_cast<VNode*>(nodes[3])->u = 1; in BOOST_AUTO_TEST_CASE()
693 hess_reverse(root, nodes, dhess); in BOOST_AUTO_TEST_CASE()
700 void test_hess_forward(Node* root, unsigned int& nvar) in test_hess_forward() argument
705 hess_forward(root,nvar,&hess); in test_hess_forward()
720 static_cast<VNode*>(list[0])->val = -10.5; in BOOST_AUTO_TEST_CASE()
721 static_cast<VNode*>(list[0])->u = 1; in BOOST_AUTO_TEST_CASE()
723 CHECK_CLOSE(deval,-10.5); in BOOST_AUTO_TEST_CASE()
732 PNode* p1 = create_param_node(-1.5); in BOOST_AUTO_TEST_CASE()
735 CHECK_CLOSE(deval,-1.5); in BOOST_AUTO_TEST_CASE()
750 static_cast<VNode*>(list[0])->val = 2.5; in BOOST_AUTO_TEST_CASE()
751 static_cast<VNode*>(list[0])->u =1; in BOOST_AUTO_TEST_CASE()
753 Node* root = create_binary_op_node(OP_TIMES,op1,op1); in BOOST_AUTO_TEST_CASE() local
754 double deval = hess_reverse(root,list,dhess); in BOOST_AUTO_TEST_CASE()
755 double eval = eval_function(root); in BOOST_AUTO_TEST_CASE()
761 nonlinearEdges(root,s); in BOOST_AUTO_TEST_CASE()
778 Node* root = create_binary_op_node(OP_TIMES, op2, op3); in BOOST_AUTO_TEST_CASE() local
779 static_cast<VNode*>(list[0])->val = 2.1; in BOOST_AUTO_TEST_CASE()
780 static_cast<VNode*>(list[1])->val = 1.8; in BOOST_AUTO_TEST_CASE()
781 double eval = eval_function(root); in BOOST_AUTO_TEST_CASE()
784 static_cast<VNode*>(list[0])->u = 0; in BOOST_AUTO_TEST_CASE()
785 static_cast<VNode*>(list[1])->u = 1; in BOOST_AUTO_TEST_CASE()
786 double deval = hess_reverse(root,list,dhess); in BOOST_AUTO_TEST_CASE()
789 CHECK_CLOSE(dhess[0], -6.945893481707861); in BOOST_AUTO_TEST_CASE()
790 CHECK_CLOSE(dhess[1], -8.441601940854081); in BOOST_AUTO_TEST_CASE()
793 static_cast<VNode*>(list[0])->u = 1; in BOOST_AUTO_TEST_CASE()
794 static_cast<VNode*>(list[1])->u = 0; in BOOST_AUTO_TEST_CASE()
795 deval = hess_reverse(root,list,dhess); in BOOST_AUTO_TEST_CASE()
798 CHECK_CLOSE(dhess[0], -6.201993262668304); in BOOST_AUTO_TEST_CASE()
799 CHECK_CLOSE(dhess[1], -6.945893481707861); in BOOST_AUTO_TEST_CASE()
809 static_cast<VNode*>(x1)->val = 0; in BOOST_AUTO_TEST_CASE()
823 x1->val = 0; in BOOST_AUTO_TEST_CASE()
824 x1->u = 1; in BOOST_AUTO_TEST_CASE()
842 Node* root = create_binary_op_node(OP_PLUS,op1,op2); in BOOST_AUTO_TEST_CASE() local
843 x1->val = 1; in BOOST_AUTO_TEST_CASE()
844 x2->val = 1; in BOOST_AUTO_TEST_CASE()
846 grad_reverse(root,list,grad); in BOOST_AUTO_TEST_CASE()