holding does not work as intended

asked 2020-06-26 16:32:33 -0500

Ti Fe gravatar image

Hello everyone, I am not a native speaker, so I am not too sure about how to express the problem I have, although I will certainly give my best to do so.

I was experimenting with sage a bit and wanted it to generate an overview of a specific simplification of specific root expressions, namely sqrt(a + sqrt(b)) == sqrt(c) + sqrt(d) this simplification is possible under certain restraints for integers a and b, but that is not the point here.

For that matter I wanted to create a list of Tuples containing the expression exactly as written above and the corresponding boolean.

I therefore defined a function like this:

def test_cases(a):
    bs_cs_ds = [\
        (SR(4*n*m), SR(n), SR(m))\
        for (n,m) in [(x, a-x)\
            for x in [1..floor(a/2)]]]
    expressions = [\
        SR(a).add(sqrt(SR(b),hold=True),hold=True).sqrt(hold=True)\
        == sqrt(SR(c), hold=True).add(sqrt(SR(d), hold=True), hold=True)\
            for b,c,d in bs_cs_ds]
    return [(expr, bool(expr)) for expr in expressions]

So in my opinion I hold the evaluations/simplification for all possible functions (add and sqrt). But for some reason sage will still try to simplify the expressions. For example the input

test_cases(4)[1]

returns (sqrt(4 + 4) == sqrt(2) + sqrt(2), True), instead of (sqrt(4 + sqrt(16)) == sqrt(2) + sqrt(2), True).

So I apparently do not understand how holding works. I was under the impression, that it stops the evaluation/simplification for the given function or expression?

tl;dr: SR(a).add(SR(sqrt(SR(b), hold=True)), hold=True).sqrt(hold=True) does not return the expression sqrt(a + sqrt(b)) (one without any simplifications) for integers a,b, as I would expect for my input. But instead simplifies the inner root by factoring out etc.

To formulate a question: Can somebody correct my code to result in the intended behavior, or at least explain why Sage does not behave as expected from my point of view?

edit retag flag offensive close merge delete