9
$\begingroup$

Bug introduced in 3.0 and persisting through 13.0.1, fixed in 13.2.0 or earlier.


I noticed that initially valid syntax becomes invalid within nested RootSum objects. Consider for example the expression

test=RootSum1[
  Function[{x},2 x^7 Log[5805]-(25 Log[5805]^6 RootSum[5805-696 #1+190 #1^2-8 #1^3+#1^4&,Log[#1]/(-2+#1)&]^2)/16384],
  Function[{x},(E^x (48 x^2 RootSum[5805-696 #1+190 #1^2-8 #1^3+#1^4&,(-11 Log[#1]-4 Log[#1] #1+Log[#1] #1^2)/(-174+95 #1-6 #1^2+#1^3)&]))/(64 x^3-48 x^2 Log[5805]+100 x RootSum[5805-696 #1+190 #1^2-8 #1^3+#1^4&,Log[#1]/(-2+#1)&]^2)]
]

Which is pretty valid expression where RootSum is replaced by RootSum1 for demonstration. It contains two pure functions with valid syntax.

Now replace RootSum1 by RootSum:

(test /. RootSum1 -> RootSum) // N

(3.63594 + 0. I) RootSum[ Function[{x}, {-32768. x^7 + 25. 48889.1 (0.416541 + 0. I)}], Function[{x}, {( 2.71828^x x)/((10.4135 + 0. I) - 103.998 x + 16. x^2)}] ]

Note that syntax for Function became invalid: additional internal List appeared, which prevents outer computation. With the internal list removed computations complete:

(3.63594 + 0. I) RootSum[
  Function[{x}, -32768. x^7 + 25. 48889.1 (0.416541 + 0. I)], 
  Function[{x}, (2.71828^x x)/((10.4135 + 0. I) - 103.998 x + 16. x^2)]
]

(* -0.283396 - 3.9421*10^-17 I *)

Is this a bug? Can it be avoided?

$\endgroup$
8
  • 1
    $\begingroup$ (test // N) /. RootSum1 :> RootSum $\endgroup$
    – Bob Hanlon
    Commented Jun 14, 2022 at 12:18
  • $\begingroup$ This is just a demo for a problem. I want exact result. Numerical evaluation is used simply to reveal the internal syntax change problem. Spent quite a while time to find what happens. $\endgroup$
    – Acus
    Commented Jun 14, 2022 at 12:28
  • 1
    $\begingroup$ @BobHanlon Also, I dont' use Normal. This will replace RootSum to sum of Roots and will result in enormous expressions (the demo is a toy example). I want answer in nested RootSum objects, since they yield most compact symbolic expressions. $\endgroup$
    – Acus
    Commented Jun 14, 2022 at 12:40
  • 2
    $\begingroup$ test = RootSum[Function[{x}, 2 x^7 Log[5805] - (25 Log[5805]^6 RootSum[5805 - 696 #1 + 190 #1^2 - 8 #1^3 + #1^4 &, Log[#1]/(-2 + #1) &]^2)/16384], Function[{x}, (E^x (48 x^2 RootSum[5805 - 696 #1 + 190 #1^2 - 8 #1^3 + #1^4 &, (-11 Log[#1] - 4 Log[#1] #1 + Log[#1] #1^2)/(-174 + 95 #1 - 6 #1^2 + #1^3) &]))/(64 x^3 - 48 x^2 Log[5805] + 100 x RootSum[5805 - 696 #1 + 190 #1^2 - 8 #1^3 + #1^4 &, Log[#1]/(-2 + #1) &]^2)]] /. List -> Sequence; $\endgroup$
    – Bob Hanlon
    Commented Jun 14, 2022 at 12:43
  • 3
    $\begingroup$ Simpler buggy example: RootSum[Function[{x}, 2 x^7 - 1], Function[{x}, (E^x) RootSum[10 - #1 &, Log] ]]. Internally, the variable x is replaced by an internal variable, which near the end is replaced back by {x} instead of x. It seems to be a bug. $\endgroup$
    – Michael E2
    Commented Jun 14, 2022 at 15:08

2 Answers 2

7
$\begingroup$

The bug does not exist for Slot[1], just for Function with named parameters. (A dummy variable is substituted for x, presumably so that the body of the Function may evaluate safely. When the dummy variable is replaced in the output, it is replaced by {x} instead of x).

In the OP's example, one approach would be to construct RootSum objects using Slot[1] instead of a named variable.

One can fix the OP's example as follows. Since none of the root sums inside a Function[{x}, body] contain an x, all the instances of x may be replaced by Slot[1] (#):

val = Hold[RootSum][
    Function[{x}, 
     2 x^7 Log[
        5805] - (25 Log[5805]^6 RootSum[
           5805 - 696 #1 + 190 #1^2 - 8 #1^3 + #1^4 &, 
           Log[#1]/(-2 + #1) &]^2)/16384], 
    Function[{x}, (E^
         x (48 x^2 RootSum[
           5805 - 696 #1 + 190 #1^2 - 
             8 #1^3 + #1^4 &, (-11 Log[#1] - 4 Log[#1] #1 + 
               Log[#1] #1^2)/(-174 + 95 #1 - 
               6 #1^2 + #1^3) &]))/(64 x^3 - 48 x^2 Log[5805] + 
        100 x RootSum[5805 - 696 #1 + 190 #1^2 - 8 #1^3 + #1^4 &, 
           Log[#1]/(-2 + #1) &]^2)]] /. 
   Verbatim[Function][{v_}, body_] :> (Evaluate[body /. v -> #] &) // 
  ReleaseHold

val // N
(*  -0.283396 - 3.9421*10^-18 I  *)

Hopefully that works in general (that x is free to be replaced by #). One can use Inactive[RootSum] and Activate instead of Hold and ReleaseHold, if desired.

The bug does not appear every time Function[{x}, body] is used. There seems to have to be a RootSum in body and the other terms perhaps being Listable.

(* Bug *)
RootSum[Function[{x}, 2 x^7 - 1],
 Function[{x}, E^x RootSum[10 - #1 &, Log] ]]
(* No bug *)
RootSum[Function[{x}, 2 x^7 - 1], 
 Function[{x}, E^x Boole[x ∈ Complexes] RootSum[10 - #1 &, Log] ]]
RootSum[Function[{x}, 2 x^7 - 1], (* f undefined *)
 Function[{x}, f[x] + E^x RootSum[10 - #1 &, Log] ]]
RootSum[Function[{x}, 2 x^7 - 1], (* Internal RootSum = 0 *)
 Function[{x}, E^x RootSum[1 - #1 &, Log] ]]
$\endgroup$
7
$\begingroup$

Another workaround is not to put the single Function variable in a list.

In[1]:= test1=RootSum1[                                                         
  Function[x, 2 x^7 Log[5805]-(25 Log[5805]^6 RootSum[5805-696 #1+190 #1^2-8 #1^3+#1^4&,Log[#1]/(-2+#1)&]^2)/16384],                                            
  Function[x,(E^x (48 x^2 RootSum[5805-696 #1+190 #1^2-8 #1^3+#1^4&,(-11 Log[#1]-4 Log[#1] #1+Log[#1] #1^2)/(-174+95 #1-6 #1^2+#1^3)&]))/(64 x^3-48 x^2 Log[5805]+100 x RootSum[5805-696 #1+190 #1^2-8 #1^3+#1^4&,Log[#1]/(-2+#1)&]^2)]];                                                                              

In[2]:= (test1 /. RootSum1 -> RootSum) // N                                     

                              -17
Out[2]= -0.283396 - 2.60178 10    I

The bug is that the dummy variable gets replaced with the first argument of the input Function (even if the first argument was a list).

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.