Skip to content

Improve factoring for square roots #23

@fredrik-johansson

Description

@fredrik-johansson

Composing matrix exponentials and logarithms is a good way to find expressions that don't simplify:

>>> ca_mat([[1,-1],[-1,-1]]).exp().log()
ca_mat of size 2 x 2
[1.00000 {(-b*d*f+b*e*f)/(2*c) where a = 1.41421 [Log(4.11325 {(c+d+e)/2})], b = -1.41421 [Log(0.243117 {(-c+d+e)/2})], c = 3.87013 [Sqrt(14.9779 {d^2+e^2-2})], d = 4.11325 [Exp(1.41421 {f})], e = 0.243117 [Exp(-1.41421 {-f})], f = 1.41421 [f^2-2=0]}, -1.00000 {(b*d^2+b*e^2-2*b)/(c*d*f-c*e*f) where a = 1.41421 [Log(4.11325 {(c+d+e)/2})], b = -1.41421 [Log(0.243117 {(-c+d+e)/2})], c = 3.87013 [Sqrt(14.9779 {d^2+e^2-2})], d = 4.11325 [Exp(1.41421 {f})], e = 0.243117 [Exp(-1.41421 {-f})], f = 1.41421 [f^2-2=0]}]
[-1.00000 {(b*d*f-b*e*f)/(2*c) where a = 1.41421 [Log(4.11325 {(c+d+e)/2})], b = -1.41421 [Log(0.243117 {(-c+d+e)/2})], c = 3.87013 [Sqrt(14.9779 {d^2+e^2-2})], d = 4.11325 [Exp(1.41421 {f})], e = 0.243117 [Exp(-1.41421 {-f})], f = 1.41421 [f^2-2=0]},             -1.00000 {(b*d*f-b*e*f)/(2*c) where a = 1.41421 [Log(4.11325 {(c+d+e)/2})], b = -1.41421 [Log(0.243117 {(-c+d+e)/2})], c = 3.87013 [Sqrt(14.9779 {d^2+e^2-2})], d = 4.11325 [Exp(1.41421 {f})], e = 0.243117 [Exp(-1.41421 {-f})], f = 1.41421 [f^2-2=0]}]

This is the problem:

>>> a = exp(2*sqrt(2))
>>> b = exp(-2*sqrt(2))
>>> sqrt(a+b-2); sqrt(a+1/a-2)
3.87013 {a where a = 3.87013 [Sqrt(14.9779 {b+c-2})], b = 16.9188 [Exp(2.82843 {2*d})], c = 0.0591057 [Exp(-2.82843 {-2*d})], d = 1.41421 [d^2-2=0]}
3.87013 {(a*b-a)/(b) where a = 4.11325 [Sqrt(16.9188 {b})], b = 16.9188 [Exp(2.82843 {2*c})], c = 1.41421 [c^2-2=0]}
>>> 
>>> sqrt(a+b-2) - (a-1)/sqrt(a)
0e-1125 {(a*b-c+1)/(b) where a = 3.87013 [Sqrt(14.9779 {c+d-2})], b = 4.11325 [Sqrt(16.9188 {c})], c = 16.9188 [Exp(2.82843 {2*e})], d = 0.0591057 [Exp(-2.82843 {-2*e})], e = 1.41421 [e^2-2=0]}
>>> 
>>> sqrt(a+1/a-2) - (a-1)/sqrt(a)
0

a+b-2 does not factor, though the equivalent rational function a+1/a-2 does.

The factoring algorithm for field elements somehow needs to be made aware of this. A possibility is to look specifically for exponentials to eliminate, but perhaps there's a more general approach.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions