1212#include " properties/Accent.hh"
1313#include < functional>
1414
15- #define DEBUG
15+ // #define DEBUG
1616
1717using namespace cadabra ;
1818
@@ -539,8 +539,10 @@ Ex::iterator evaluate::handle_derivative(iterator it)
539539 size_t ni=number_of_direct_indices (it);
540540
541541 cadabra::do_list (tr, ivalues, [&](Ex::iterator iv) {
542- // std::cerr << "====" << std::endl;
543- // std::cerr << Ex(iv) << std::endl;
542+ #ifdef DEBUG
543+ std::cerr << " ====" << std::endl;
544+ std::cerr << Ex (iv) << std::endl;
545+ #endif
544546 // For each internal dummy set, keep track of the
545547 // position in the permutation array where we generate
546548 // its value.
@@ -551,9 +553,11 @@ Ex::iterator evaluate::handle_derivative(iterator it)
551553 auto deps=dependencies (rhs);
552554
553555 // If the argument does not depend on anything, all derivatives
554- // would produce zero.
555- if (deps.size ()==0 )
556+ // would produce zero. Remove this \equals node from the tree.
557+ if (deps.size ()==0 ) {
558+ tr.erase (iv);
556559 return true ;
560+ }
557561
558562 // All indices on \partial can take any of the values of the
559563 // dependencies, EXCEPT when the index is a dummy index. In
@@ -568,7 +572,9 @@ Ex::iterator evaluate::handle_derivative(iterator it)
568572
569573 combin::combinations<Ex> cb;
570574 for (auto & obj: deps) {
571- // std::cerr << "dep " << obj << std::endl;
575+ #ifdef DEBUG
576+ std::cerr << " dep " << obj << std::endl;
577+ #endif
572578 cb.original .push_back (obj);
573579 }
574580 cb.multiple_pick =true ;
@@ -608,18 +614,24 @@ Ex::iterator evaluate::handle_derivative(iterator it)
608614 // derivative, create an entry in the \components node.
609615
610616 for (unsigned int i=0 ; i<cb.size () || cb.size ()==0 ; ++i) {
611- // std::cerr << "Index combination " << i << std::endl;
617+ #ifdef DEBUG
618+ std::cerr << " Index combination " << i << std::endl;
619+ #endif
612620 Ex eqcopy (iv);
613621 auto lhs=eqcopy.begin (eqcopy.begin ());
614622 assert (*lhs->name ==" \\ comma" );
615623
616624 if (cb.size ()>0 ) {
625+ #ifdef DEBUG
626+ std::cerr << " Copying values of derivative indices" << std::endl;
627+ #endif
617628 // Setup the index values; simply copy from the cb array, but only
618629 // if the indices are not internal dummy.
619630 for (size_t j=0 ; j<cb[i].size (); ++j) {
620631 auto fd = ind_dummy.find (Ex (tr.child (it, j)));
621- if (fd==ind_dummy.end ())
632+ if (fd==ind_dummy.end ()) {
622633 eqcopy.append_child (iterator (lhs), cb[i][j].begin () );
634+ }
623635 }
624636 }
625637 auto rhs=lhs;
@@ -684,14 +696,16 @@ Ex::iterator evaluate::handle_derivative(iterator it)
684696
685697 if (cb.size ()==0 ) break ;
686698 }
699+
700+ // Erase the original \equals entry (we generated a full replacement above).
687701 tr.erase (iv);
688702 return true ;
689703 });
690704
691705 one (it->multiplier );
692706 // std::cerr << "now " << Ex(it) << std::endl;
693707
694-
708+
695709 // Now move the free (but not the internal dummy!) partial indices
696710 // to the components node, and then unwrap the partial node.
697711
@@ -719,11 +733,15 @@ Ex::iterator evaluate::handle_derivative(iterator it)
719733 ++se;
720734 }
721735
722- // std::cerr << "after index move " << Ex(it) << std::endl;
736+ #ifdef DEBUG
737+ std::cerr << " after index move " << Ex (it) << std::endl;
738+ #endif
723739
724740 merge_component_children (it);
725741
726- // std::cerr << "after merge " << Ex(it) << std::endl;
742+ #ifdef DEBUG
743+ std::cerr << " after merge " << Ex (it) << std::endl;
744+ #endif
727745
728746 simplify_components (it);
729747 // std::cerr << "then " << Ex(it) << std::endl;
0 commit comments