File tree Expand file tree Collapse file tree
hipparchus-core/src/main/java/org/hipparchus/analysis/differentiation Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -549,25 +549,29 @@ private MultivariateCompositionMapper[][] getRebaser(final DSCompiler baseCompil
549549 // it is an entry of the form ∂ⁿf/∂qⱼ⋯∂qₖ∂qₗ where n is max order
550550 final List <MultivariateCompositionMapper > row = new ArrayList <>();
551551
552- // find a variable with respect to which we have a derivative
553- // we select the minimum non-zero derivation order to speed up computation
552+ // find a variable with respect to which we have a non-zero derivative
553+ // we select the row with the smallest number of terms to speed up computation
554554 final int [] orders = baseCompiler .derivativesOrders [k ].clone ();
555- int minNonZeroOrder = Integer .MAX_VALUE ;
555+ MultivariateCompositionMapper [] lowerRow = null ;
556+ int nbTerms = -1 ;
556557 int qIndex = -1 ;
557558 for (int j = 0 ; j < orders .length ; ++j ) {
558- final int oj = orders [j ];
559- if (oj > 0 && oj < minNonZeroOrder ) {
560- minNonZeroOrder = oj ;
561- qIndex = j ;
559+ if (orders [j ] > 0 ) {
560+ orders [j ]--;
561+ final MultivariateCompositionMapper [] mcmRow = rebaser [baseCompiler .getPartialDerivativeIndex (orders )];
562+ orders [j ]++;
563+ int n = 0 ;
564+ for (final MultivariateCompositionMapper lowerTerm : mcmRow ) {
565+ n += lowerTerm .productIndices .length ;
566+ }
567+ if (nbTerms < 0 || n < nbTerms ) {
568+ lowerRow = mcmRow ;
569+ nbTerms = n ;
570+ qIndex = j ;
571+ }
562572 }
563573 }
564574
565- // find the entry corresponding to differentiating one order less with respect to this variable
566- // ∂fⁿ⁻¹/∂qⱼ⋯∂qₖ
567- orders [qIndex ]--;
568- final MultivariateCompositionMapper [] lowerRow =
569- rebaser [baseCompiler .getPartialDerivativeIndex (orders )];
570-
571575 // apply recursion formula
572576 for (final MultivariateCompositionMapper lowerTerm : lowerRow ) {
573577
You can’t perform that action at this time.
0 commit comments