@@ -570,50 +570,63 @@ partial record Set
570570 {
571571 partial record ConditionalSet
572572 {
573- // TODO: it might be optimized
574573 /// <inheritdoc/>
575574 public override Entity Substitute ( Entity x , Entity value )
576575 {
577576 if ( this == x )
578577 return value ;
579- var replacement = Variable . CreateTemp ( ( x + value + Predicate + Var ) . Vars ) ;
578+ var ( postSubs , boundName ) = SubstituteUnderABinder ( Predicate , Var , x , value ) ;
580579
581- // { x | x > a } -> { temp_1 | temp_1 > a }
582- var tempSubstituted = Predicate . Substitute ( Var , replacement ) ;
583-
584- // a = 0 -> { temp_1 | temp_1 > a } -> { temp_1 | temp_1 > 0 }
585- // x = 0 -> { temp_1 | temp_1 > a } -> { temp_1 | temp_1 > a }
586- var subs = tempSubstituted . Substitute ( x , value ) ;
587-
588- // { temp_1 | temp_1 > a } -> { x | x > a }
589- var postSubs = subs . Substitute ( replacement , Var ) ;
590-
591- return New ( Var , postSubs ) ;
580+ return New ( boundName , postSubs ) ;
592581 }
593582 }
594583 }
595584
585+ /// <summary>
586+ /// <paramref name="body"/>, bound over <paramref name="bound"/>, with <paramref name="x"/> replaced
587+ /// by <paramref name="value"/> where it occurs free, and the name the binder binds afterwards.
588+ /// </summary>
589+ /// <remarks>
590+ /// <para>
591+ /// Where <paramref name="value"/> holds the bound name, the binder takes a fresh one and keeps
592+ /// it, as <see cref="Lambda"/> does: renamed back after the substitution, the value's
593+ /// occurrence of the name was captured, and <c>integral(t + a, t, 0, 1)</c> with <c>a := t</c>
594+ /// became <c>integral(t + t, t, 0, 1)</c>, which is 1 where the integral is <c>t + 1/2</c>.
595+ /// </para>
596+ /// <para>
597+ /// The body is passed over only where it can change. A body without <paramref name="x"/>, or
598+ /// under a binder over <paramref name="x"/> itself, is returned as it is; where neither
599+ /// <paramref name="x"/> nor <paramref name="value"/> holds the bound name, one pass gives what a
600+ /// renaming would. Renaming it three times at every level, as every binder did, made a chain of
601+ /// nested integrals cost a multiple of itself per level
602+ /// (https://github.com/asc-community/AngouriMath/issues/1808).
603+ /// </para>
604+ /// </remarks>
605+ private static ( Entity Body , Entity Bound ) SubstituteUnderABinder ( Entity body , Entity bound , Entity x , Entity value )
606+ {
607+ if ( x == bound || ! body . ContainsNode ( x ) )
608+ return ( body , bound ) ;
609+ if ( value . ContainsNode ( bound ) )
610+ {
611+ var fresh = Variable . CreateUnique ( body + bound + x + value , bound is Variable named ? named . Name : "t" ) ;
612+ return ( body . Substitute ( bound , fresh ) . Substitute ( x , value ) , fresh ) ;
613+ }
614+ if ( ! x . ContainsNode ( bound ) )
615+ return ( body . Substitute ( x , value ) , bound ) ;
616+ var replacement = Variable . CreateTemp ( ( x + value + body + bound ) . Vars ) ;
617+ return ( body . Substitute ( bound , replacement ) . Substitute ( x , value ) . Substitute ( replacement , bound ) , bound ) ;
618+ }
619+
596620 partial record Integralf
597621 {
598- // TODO: it might be optimized
599622 /// <inheritdoc/>
600623 public override Entity Substitute ( Entity x , Entity value )
601624 {
602625 if ( this == x )
603626 return value ;
604- var replacement = Variable . CreateTemp ( ( x + value + Expression + Var ) . Vars ) ;
605-
606- // integrate(x ^ 2 + a, x) -> integrate(temp_1 ^ 2 + a, temp_1)
607- var tempSubstituted = Expression . Substitute ( Var , replacement ) ;
627+ var ( postSubs , boundName ) = SubstituteUnderABinder ( Expression , Var , x , value ) ;
608628
609- // a = 0 -> integrate(temp_1 ^ 2 + a, temp_1) -> integrate(temp_1 ^ 2 + 0, temp_1)
610- // x = 0 -> integrate(temp_1 ^ 2 + a, temp_1) -> integrate(temp_1 ^ 2 + a, temp_1)
611- var subs = tempSubstituted . Substitute ( x , value ) ;
612-
613- // integrate(temp_1 ^ 2 + a, temp_1) -> integrate(x ^ 2 + a, temp_1)
614- var postSubs = subs . Substitute ( replacement , Var ) ;
615-
616- return New ( postSubs , Var , Range is var ( from , to ) ? ( from . Substitute ( x , value ) , to . Substitute ( x , value ) ) : null ) ;
629+ return New ( postSubs , boundName , Range is var ( from , to ) ? ( from . Substitute ( x , value ) , to . Substitute ( x , value ) ) : null ) ;
617630 }
618631 }
619632
@@ -629,11 +642,8 @@ public override Entity Substitute(Entity x, Entity value)
629642 {
630643 if ( this == x )
631644 return value ;
632- var replacement = Variable . CreateTemp ( ( x + value + Expression + Var ) . Vars ) ;
633- var tempSubstituted = Expression . Substitute ( Var , replacement ) ;
634- var subs = tempSubstituted . Substitute ( x , value ) ;
635- var postSubs = subs . Substitute ( replacement , Var ) ;
636- return New ( postSubs , Var , From . Substitute ( x , value ) , To . Substitute ( x , value ) ) ;
645+ var ( postSubs , boundName ) = SubstituteUnderABinder ( Expression , Var , x , value ) ;
646+ return New ( postSubs , boundName , From . Substitute ( x , value ) , To . Substitute ( x , value ) ) ;
637647 }
638648 }
639649
@@ -645,9 +655,8 @@ public override Entity Substitute(Entity x, Entity value)
645655 {
646656 if ( this == x )
647657 return value ;
648- var replacement = Variable . CreateTemp ( ( x + value + Expression + Var ) . Vars ) ;
649- var renamed = Expression . Substitute ( Var , replacement ) . Substitute ( x , value ) . Substitute ( replacement , Var ) ;
650- return New ( renamed , Var , Over . Substitute ( x , value ) ) ;
658+ var ( renamed , boundName ) = SubstituteUnderABinder ( Expression , Var , x , value ) ;
659+ return New ( renamed , boundName , Over . Substitute ( x , value ) ) ;
651660 }
652661 }
653662
@@ -659,9 +668,8 @@ public override Entity Substitute(Entity x, Entity value)
659668 {
660669 if ( this == x )
661670 return value ;
662- var replacement = Variable . CreateTemp ( ( x + value + Expression + Var ) . Vars ) ;
663- var renamed = Expression . Substitute ( Var , replacement ) . Substitute ( x , value ) . Substitute ( replacement , Var ) ;
664- return New ( renamed , Var , Over . Substitute ( x , value ) ) ;
671+ var ( renamed , boundName ) = SubstituteUnderABinder ( Expression , Var , x , value ) ;
672+ return New ( renamed , boundName , Over . Substitute ( x , value ) ) ;
665673 }
666674 }
667675
@@ -672,9 +680,8 @@ public override Entity Substitute(Entity x, Entity value)
672680 {
673681 if ( this == x )
674682 return value ;
675- var replacement = Variable . CreateTemp ( ( x + value + Expression + Var ) . Vars ) ;
676- var renamed = Expression . Substitute ( Var , replacement ) . Substitute ( x , value ) . Substitute ( replacement , Var ) ;
677- return New ( renamed , Var , Over . Substitute ( x , value ) ) ;
683+ var ( renamed , boundName ) = SubstituteUnderABinder ( Expression , Var , x , value ) ;
684+ return New ( renamed , boundName , Over . Substitute ( x , value ) ) ;
678685 }
679686 }
680687
@@ -685,9 +692,8 @@ public override Entity Substitute(Entity x, Entity value)
685692 {
686693 if ( this == x )
687694 return value ;
688- var replacement = Variable . CreateTemp ( ( x + value + Expression + Var ) . Vars ) ;
689- var renamed = Expression . Substitute ( Var , replacement ) . Substitute ( x , value ) . Substitute ( replacement , Var ) ;
690- return New ( renamed , Var , Over . Substitute ( x , value ) ) ;
695+ var ( renamed , boundName ) = SubstituteUnderABinder ( Expression , Var , x , value ) ;
696+ return New ( renamed , boundName , Over . Substitute ( x , value ) ) ;
691697 }
692698 }
693699
@@ -698,9 +704,8 @@ public override Entity Substitute(Entity x, Entity value)
698704 {
699705 if ( this == x )
700706 return value ;
701- var replacement = Variable . CreateTemp ( ( x + value + Expression + Var ) . Vars ) ;
702- var renamed = Expression . Substitute ( Var , replacement ) . Substitute ( x , value ) . Substitute ( replacement , Var ) ;
703- return New ( renamed , Var , Over . Substitute ( x , value ) ) ;
707+ var ( renamed , boundName ) = SubstituteUnderABinder ( Expression , Var , x , value ) ;
708+ return New ( renamed , boundName , Over . Substitute ( x , value ) ) ;
704709 }
705710 }
706711
@@ -711,61 +716,36 @@ public override Entity Substitute(Entity x, Entity value)
711716 {
712717 if ( this == x )
713718 return value ;
714- var replacement = Variable . CreateTemp ( ( x + value + Expression + Var ) . Vars ) ;
715- var tempSubstituted = Expression . Substitute ( Var , replacement ) ;
716- var subs = tempSubstituted . Substitute ( x , value ) ;
717- var postSubs = subs . Substitute ( replacement , Var ) ;
718- return New ( postSubs , Var , From . Substitute ( x , value ) , To . Substitute ( x , value ) ) ;
719+ var ( postSubs , boundName ) = SubstituteUnderABinder ( Expression , Var , x , value ) ;
720+ return New ( postSubs , boundName , From . Substitute ( x , value ) , To . Substitute ( x , value ) ) ;
719721 }
720722 }
721723
722724 partial record Derivativef
723725 {
724- // TODO: it might be optimized
725726 /// <inheritdoc/>
726727 public override Entity Substitute ( Entity x , Entity value )
727728 {
728729 if ( this == x )
729730 return value ;
730- var replacement = Variable . CreateTemp ( ( x + value + Expression + Var ) . Vars ) ;
731-
732- // derive(x ^ 2 + a, x) -> derive(temp_1 ^ 2 + a, temp_1)
733- var tempSubstituted = Expression . Substitute ( Var , replacement ) ;
734-
735- // a = 0 -> derive(temp_1 ^ 2 + a, temp_1) -> derive(temp_1 ^ 2 + 0, temp_1)
736- // x = 0 -> derive(temp_1 ^ 2 + a, temp_1) -> derive(temp_1 ^ 2 + a, temp_1)
737- var subs = tempSubstituted . Substitute ( x , value ) ;
731+ var ( postSubs , boundName ) = SubstituteUnderABinder ( Expression , Var , x , value ) ;
738732
739- // derive(temp_1 ^ 2 + a, temp_1) -> derive(x ^ 2 + a, temp_1)
740- var postSubs = subs . Substitute ( replacement , Var ) ;
741-
742- return New ( postSubs , Var ) ;
733+ return New ( postSubs , boundName ) ;
743734 }
744735 }
745736
746737 partial record Limitf
747738 {
748- // TODO: it might be optimized
749739 /// <inheritdoc/>
750740 public override Entity Substitute ( Entity x , Entity value )
751741 {
752742 if ( this == x )
753743 return value ;
754- var replacement = Variable . CreateTemp ( ( x + value + Expression + Var ) . Vars ) ;
755-
756- // derive(x ^ 2 + a, x) -> derive(temp_1 ^ 2 + a, temp_1)
757- var tempSubstituted = Expression . Substitute ( Var , replacement ) ;
758-
759- // a = 0 -> derive(temp_1 ^ 2 + a, temp_1) -> derive(temp_1 ^ 2 + 0, temp_1)
760- // x = 0 -> derive(temp_1 ^ 2 + a, temp_1) -> derive(temp_1 ^ 2 + a, temp_1)
761- var subs = tempSubstituted . Substitute ( x , value ) ;
762-
763- // derive(temp_1 ^ 2 + a, temp_1) -> derive(x ^ 2 + a, temp_1)
764- var postSubs = subs . Substitute ( replacement , Var ) ;
744+ var ( postSubs , boundName ) = SubstituteUnderABinder ( Expression , Var , x , value ) ;
765745
766746 var dst = Destination . Substitute ( x , value ) ;
767747
768- return New ( postSubs , Var , dst , ApproachFrom ) ;
748+ return New ( postSubs , boundName , dst , ApproachFrom ) ;
769749 }
770750 }
771751
0 commit comments