1 (kill(all), load(raddenest), 'done);
4 /* Check helper functions */
6 _sqrtpowerp(sqrt(125));
12 _sqrtp(sqrt(15+3^(1/5)));
21 _algebraicp(1/sqrt(125));
24 _algebraicp(sqrt(3)^17);
27 _algebraicp(sqrt(-2*sqrt(10)+4*sqrt(5)-2^(3/2)+14));
30 _algebraicp(sqrt(2)*(3/(sqrt(7)+sqrt(5)*sqrt(2))));
33 _algebraicp(sqrt(5)*cos(2));
36 _algebraicp(sqrt(4)^(17/3));
39 _algebraicp(sqrt(5)^(17/3));
42 _algebraicp(sqrt(-2));
48 _sqrt_depth(cos(1)+sqrt(5-cos(2)));
51 _sqrt_depth(1 + sqrt(2)*(1 + sqrt(3)));
54 _sqrt_depth(sqrt(2+sqrt(2))*(4+2^(3/2)));
57 _sqrt_depth((1+5^(3/2))^(7/2));
60 _sqrt_depth(1 -sqrt(3)/2+ sqrt(2)*sqrt(1 + sqrt(3)));
63 _sqrt_depth(sqrt(sqrt(3)-2)-sqrt(sqrt(sqrt(2)+3)+5));
66 _sqrtcontract(sqrt(2)/(7*3^(3/2)));
69 _sqrtcontract(5^(3/2)/2^(7/2));
76 [[1, 0, 0, 0], [0, 1, 0, 0], [1, 1, 0, 0], [0, 0, 1, 0], [1, 0, 1, 0],
77 [0, 1, 1, 0], [1, 1, 1, 0], [0, 0, 0, 1], [1, 0, 0, 1], [0, 1, 0, 1],
78 [1, 1, 0, 1], [0, 0, 1, 1], [1, 0, 1, 1], [0, 1, 1, 1], [1, 1, 1, 1]]$
86 _splitcoef(3*32^(1/4));
89 _splitcoef(15/sqrt(2));
92 _splitcoef(sqrt(7^3)*2);
95 _splitcoef(-5^(7/2)*sqrt(3)^21);
98 _splitcoef(sqrt(11)/7^(5/2));
101 _sqrt_match((2*sqrt(2) + 4)*sqrt(2 + sqrt(2)) + 5*sqrt(2) + 8);
102 [5*sqrt(2)+8,2^(3/2)+4,sqrt(2)+2]$
104 _sqrt_match(1 + sqrt(2) - sqrt(2)*sqrt(3) - 2*sqrt(1+sqrt(5)));
105 [-sqrt(2)*sqrt(3)+sqrt(2)+1,-2,sqrt(5)+1]$
107 _sqrt_match(15/sqrt(2)+103/4);
110 _sqrt_match(3+5^(7/3));
113 _split_gcd([55, 35, 22, 14, 77, 10]);
114 [5,[55,35,10],[22,14,77]]$
119 _split_surds(3*sqrt(3) + sqrt(5)/7 + sqrt(6) + sqrt(10) + sqrt(15));
120 [3,sqrt(5)+sqrt(2)+3,sqrt(10)+sqrt(5)/7]$
122 /* Denesting functions */
124 _sqrtdenest_rec(sqrt(-72*sqrt(2) + 158*sqrt(5) + 498));
125 -sqrt(10)+9*sqrt(5)+sqrt(2)+9$
127 (w:-6*sqrt(55)-6*sqrt(35)-2*sqrt(22)-2*sqrt(14)
128 +2*sqrt(77)+6*sqrt(10)+65,
129 _sqrtdenest_rec(sqrt(w)));
130 -sqrt(11)-sqrt(7)+3*sqrt(5)+sqrt(2)$
132 ([a,b,r]: [16-2*sqrt(29), 2, -10*sqrt(29)+55],
133 _sqrt_symbolic_denest(a, b, r));
134 sqrt(-2*sqrt(29) + 11) + sqrt(5)$
136 (w: sqrt(sqrt(sqrt(3) + 1) + 1) + 1 + sqrt(2),
137 [a,b,r]: _sqrt_match(expand(w^2)),
138 _sqrt_symbolic_denest(a,b,r));
139 sqrt(sqrt(sqrt(3)+1)+1)+sqrt(2)+1$
144 (w: sqrt(expand((sqrt(1+sqrt(x))-1)^2)),
145 [a,b,r]: _sqrt_match(w^2),
146 _sqrt_symbolic_denest(a,b,r));
147 abs(sqrt(1+sqrt(x))-1)$
152 block([domain:'complex],
153 w: sqrt(expand((sqrt(1-sqrt(x))-1)^2)),
154 [a,b,r]: _sqrt_match(w^2),
155 _sqrt_symbolic_denest(a,b,r));
156 ''(block([domain:'complex], sqrt((sqrt(1-sqrt(x))-1)^2)))$
158 /* biquadratic denesting with sqrt(a/2 + sqd/2) */
159 (rad: -sqrt(15) - sqrt(2)*sqrt(-sqrt(5) + 5) - sqrt(3) + 8,
161 [a,b,r]: _sqrt_match(rad),
162 d2: expand(a^2-b^2*r),
163 _sqrt_biquadratic_denest(z,a,b,r,d2));
164 sqrt(30)/4-(sqrt(5-sqrt(5))*sqrt(15))/4+sqrt(10)/4-sqrt(6)/4+(sqrt(5-sqrt(5))*sqrt(5))/4-(sqrt(3)*sqrt(5-sqrt(5)))/4+sqrt(5-sqrt(5))/4-1/2^(3/2)$
166 /* biquadratic denesting with sqrt(a/2 - sqd/2) */
167 (w: 2 + sqrt(2) + sqrt(3) + (1 + sqrt(3))*sqrt(2 + sqrt(2) + 5*sqrt(3)),
170 [a,b,r]: _sqrt_match(rad),
171 d2: expand(a^2-b^2*r),
172 _sqrt_biquadratic_denest(z,a,b,r,d2));
173 sqrt(3)*sqrt(5*sqrt(3)+sqrt(2)+2)+sqrt(5*sqrt(3)+sqrt(2)+2)+sqrt(3)+sqrt(2)+2$
175 /* original Maxima 5.39 sqdnst tests */
177 raddenest(sqrt(3+2*sqrt(2)));
180 raddenest(sqrt(5+2*sqrt(6)));
183 raddenest(sqrt(sqrt(3)/2+1)/sqrt(11*sqrt(2)-12));
184 /*(sqrt(3)/2+1/2)/(3*2^(1/4)-2^(3/4))$*/
185 (1/2+sqrt(3)/2)/(3*2^(1/4)-8^(1/4))$
187 raddenest((19601-13860*sqrt(2))^(7/4));
190 raddenest(sqrt(3+2*sqrt(3)));
191 /*3^(3/4)/sqrt(2)+3^(1/4)/sqrt(2);*/
192 27^(1/4)/sqrt(2)+3^(1/4)/sqrt(2)$
194 raddenest(sqrt(4+3*sqrt(2)));
197 raddenest(sqrt(199999-600*sqrt(111110)));
198 10^(5/2)-3*sqrt(11111)$
200 raddenest((sqrt(2*sqrt(6)+5)+1)^(1/3));
201 (sqrt(3)+sqrt(2)+1)^(1/3)$
203 raddenest(sqrt(5+4*sqrt(5+2*sqrt(6))));
204 /*sqrt(4*(sqrt(3)+sqrt(2))+5);*/
205 sqrt(4*sqrt(3)+2^(5/2)+5)$
207 raddenest(sqrt(5*sqrt(3)+6*sqrt(2)));
208 3^(3/4)+sqrt(2)*3^(1/4)$
210 /* Sympy 1.0 test suite
211 Some of the examples don't denest;
212 check that there's no uncaught error */
214 raddenest(sqrt(5+2*sqrt(6)));
217 raddenest(sqrt(5+sqrt(7)));
220 raddenest(sqrt(3 + sqrt(5 + 2*sqrt(7))));
221 /*sqrt((sqrt(7)-2)/2)*(5+2*sqrt(7))^(1/4)+((2*sqrt(7)-5)/3)^(1/4)*sqrt((3*sqrt(7)+6)/2)$*/
222 sqrt(6*sqrt(7)+12)/(2*(5+2*sqrt(7))^(1/4))+(3*(5+2*sqrt(7))^(1/4))/(sqrt(2)*sqrt(3*sqrt(7)+6))$
224 /* sympy test_sqrtdenest2() */
225 raddenest(sqrt(16 - 2*sqrt(29) + 2*sqrt(55 - 10*sqrt(29))));
226 sqrt(5) + sqrt(11 - 2*sqrt(29))$
228 raddenest(sqrt(-sqrt(5) + sqrt(-2*sqrt(29) + 2*sqrt(-10*sqrt(29) + 55) + 16)));
229 (-2*sqrt(29) + 11)^(1/4)$
231 raddenest(sqrt(1 + sqrt(1+sqrt(7))));
232 sqrt(1 + sqrt(1+sqrt(7)))$
234 (e: sqrt(expand((1 + sqrt(1 + 2*sqrt(3 + sqrt(2) + sqrt(5))))^2)),
236 1+sqrt(1+2*sqrt(sqrt(2)+sqrt(5)+3))$
238 raddenest(sqrt(5*sqrt(3)+6*sqrt(2)));
239 sqrt(2)*3^(1/4)+3^(3/4)$
241 raddenest(sqrt(expand((1 + sqrt(5) + sqrt(1 + sqrt(3)))^2)));
242 1+sqrt(5)+sqrt(1+sqrt(3))$
244 raddenest(sqrt(expand((1 + sqrt(5) + sqrt(7) + sqrt(1 + sqrt(3)))^2)));
245 1 + sqrt(1 + sqrt(3)) + sqrt(5) + sqrt(7)$
247 (e: sqrt(expand((1 + cos(2) + cos(3) + sqrt(1 + sqrt(3)))**2)),
249 cos(3) + cos(2) + 1 + sqrt(1 + sqrt(3))$
251 ( e: sqrt(-2*sqrt(10) + 2*sqrt(2)*sqrt(-2*sqrt(10) + 11) + 14),
253 sqrt(-2*sqrt(10) - 2*sqrt(2) + 4*sqrt(5) + 14)$
255 /* check that the result is not more complicated than the input */
256 (z: sqrt(-2*sqrt(29) + cos(2) + 2*sqrt(-10*sqrt(29) + 55) + 16),
258 sqrt(-2*sqrt(29) + cos(2) + 2*sqrt(-10*sqrt(29) + 55) + 16)$
260 raddenest(sqrt(sqrt(6) + sqrt(15)));
261 sqrt(sqrt(6) + sqrt(15))$
263 (z: sqrt(15 - 2*sqrt(31) + 2*sqrt(55 - 10*sqrt(29))),
265 sqrt(15 - 2*sqrt(31) + 2*sqrt(55 - 10*sqrt(29)))$
267 /* sympy test_sqrtdenest_rec() */
268 raddenest(sqrt(-4*sqrt(14) - 2*sqrt(6) + 4*sqrt(21) + 33));
269 -sqrt(2)+sqrt(3)+2*sqrt(7)$
271 raddenest(sqrt(-28*sqrt(7) - 14*sqrt(5) + 4*sqrt(35) + 82));
272 -7 + sqrt(5) + 2*sqrt(7)$
274 raddenest(sqrt(6*sqrt(2)/11 + 2*sqrt(22)/11 + 6*sqrt(11)/11 + 2));
275 2/sqrt(22)+3/sqrt(11)+1;
277 raddenest(sqrt(468*sqrt(3) + 3024*sqrt(2) + 2912*sqrt(6) + 19735));
278 9*sqrt(3) + 26 + 56*sqrt(6)$
280 raddenest(sqrt(-490*sqrt(3) - 98*sqrt(115) - 98*sqrt(345) - 2107));
281 %i*(7*sqrt(5) + 7*sqrt(15) + 7*sqrt(23))$
283 raddenest(sqrt(-4*sqrt(14) - 2*sqrt(6) + 4*sqrt(21) + 34));
284 sqrt(-4*sqrt(14) - 2*sqrt(6) + 4*sqrt(21) + 34)$
286 raddenest(sqrt(-8*sqrt(2) - 2*sqrt(5) + 18));
287 -sqrt(10) + 1 + sqrt(2) + sqrt(5)$
289 raddenest(sqrt(8*sqrt(2) + 2*sqrt(5) - 18));
290 %i*(-sqrt(10) + 1 + sqrt(2) + sqrt(5))$
292 raddenest(sqrt(8*sqrt(2)/3 + 14*sqrt(5)/3 + 154/9));
293 -sqrt(10)/3 + sqrt(2) + sqrt(5) + 3$
295 raddenest(sqrt(sqrt(2*sqrt(6) + 5) + sqrt(2*sqrt(7) + 8)));
296 sqrt(1 + sqrt(2) + sqrt(3) + sqrt(7))$
298 raddenest(sqrt(4*sqrt(15) + 8*sqrt(5) + 12*sqrt(3) + 24));
299 1 + sqrt(3) + sqrt(5) + sqrt(15)$
301 (w: 1 + sqrt(2) + sqrt(3) + sqrt(5) + sqrt(7),
302 raddenest(sqrt(expand(w^2))));
303 1+sqrt(2)+sqrt(3)+sqrt(5)+sqrt(7)$
305 (w: 1 + sqrt(2) + sqrt(3) + sqrt(5) + sqrt(7),
306 z: sqrt(expand(w^2)+1),
308 sqrt(2*sqrt(35)+2*sqrt(21)+2*sqrt(15)+2*sqrt(14)+2*sqrt(10)+2*sqrt(7)+2*sqrt(6)+2*sqrt(5)+2*sqrt(3)+2^(3/2)+19)$
310 raddenest(sqrt(2*sqrt(10) + 6*sqrt(2) + 4*sqrt(5) + 12 + 10*sqrt(15)
312 sqrt(2*sqrt(10) + 6*sqrt(2) + 4*sqrt(5) + 12 + 10*sqrt(15) + 30*sqrt(3))$
314 raddenest(sqrt( -320 + 32*sqrt(5) + 64*sqrt(15)));
315 sqrt( -320 + 32*sqrt(5) + 64*sqrt(15))$
317 /*def test_sqrtdenest3(): */
318 raddenest(sqrt(13 - 2*sqrt(10) + 2*sqrt(2)*sqrt(-2*sqrt(10) + 11)));
319 -1 + sqrt(2) + sqrt(10)$
321 (z: sqrt(13 - 2*sqrt(10) + 2*sqrt(2)*sqrt(-2*sqrt(10) + 11)),
323 -1 + sqrt(2) + sqrt(10)$
325 (n: sqrt(2*sqrt(6)/7 + 2*sqrt(7)/7 + 2*sqrt(42)/7 + 2),
326 d: sqrt(16 - 2*sqrt(29) + 2*sqrt(55 - 10*sqrt(29))),
328 (1+1/sqrt(7)+6/sqrt(42))/(sqrt(11-2*sqrt(29))+sqrt(5))$
330 raddenest(sqrt(sqrt(sqrt(2) + 2) + 2));
331 sqrt(sqrt(sqrt(2) + 2) + 2)$
333 raddenest(sqrt(-2*sqrt(10) + 4*sqrt(2)*sqrt(-2*sqrt(10) + 11) + 20));
334 sqrt(-2*sqrt(10) - 4*sqrt(2) + 8*sqrt(5) + 20)$
336 raddenest(sqrt((112 + 70*sqrt(2)) + (46 + 34*sqrt(2))*sqrt(5)));
337 sqrt(10) + 5 + 4*sqrt(2) + 3*sqrt(5)$
339 (z: sqrt(5 + sqrt(2*sqrt(6) + 5)*sqrt(-2*sqrt(29)
340 + 2*sqrt(-10*sqrt(29) + 55) + 16)),
342 sqrt(sqrt(22-4*sqrt(29))+sqrt(33-6*sqrt(29))+sqrt(15)+sqrt(10)+5)$
344 (z: sqrt(5 + sqrt(2*sqrt(6) + 5)*sqrt(-2*sqrt(29)
345 + 2*sqrt(-10*sqrt(29) + 55) + 16)),
347 /*sqrt(sqrt(2)*sqrt(-2*sqrt(29) + 11) + sqrt(3)*sqrt(-2*sqrt(29) + 11)
348 + sqrt(10) + sqrt(15) + 5);*/
349 sqrt(sqrt(22-4*sqrt(29))+sqrt(33-6*sqrt(29))+sqrt(15)+sqrt(10)+5)$
351 /* def test_sqrtdenest4():
352 # see Denest_en.pdf in https://github.com/sympy/sympy/issues/3192 */
353 (z: sqrt(8 - sqrt(2)*sqrt(5 - sqrt(5)) - sqrt(3)*(1 + sqrt(5))),
355 sqrt(30)/4+sqrt(10)/4-sqrt(6)/4+sqrt(25-5^(3/2))/4-sqrt(75-3*5^(3/2))/4+sqrt(5-sqrt(5))/4-sqrt(15-3*sqrt(5))/4-1/2^(3/2)$
357 raddenest(sqrt(2*sqrt(2)*sqrt(sqrt(2) + 2) + 5*sqrt(2)
358 + 4*sqrt(sqrt(2) + 2) + 8));
359 sqrt(2) + sqrt(sqrt(2) + 2) + 2$
361 (w: 2 + sqrt(2) + sqrt(3) + (1 + sqrt(3))*sqrt(2 + sqrt(2) + 5*sqrt(3)),
362 z: sqrt(expand(w^2)),
364 sqrt(5*3^(3/2)+3*sqrt(2)+6)+sqrt(5*sqrt(3)+sqrt(2)+2)+sqrt(3)+sqrt(2)+2$
367 /* def test_sqrt_symbolic_denest():*/
368 raddenest(sqrt(expand((1 + sqrt(sqrt(2 + x) + 3))^2)));
369 sqrt((1 + sqrt(sqrt(2 + x) + 3))^2)$
371 raddenest(sqrt(expand((1 + sqrt(sqrt(2 + cos(1)) + 3))^2)));
372 1 + sqrt(sqrt(2 + cos(1)) + 3)$
374 raddenest(sqrt(expand((1 + cos(2))^4 + 1)));
375 sqrt(cos(2)^4+4*cos(2)^3+6*cos(2)^2+4*cos(2)+2)$
377 raddenest(sqrt(expand((1 + sqrt(sqrt(2 + cos(3*x)) + 3))^2 + 1)));
378 sqrt(2*sqrt(sqrt(cos(3*x)+2)+3)+sqrt(cos(3*x)+2)+5)$
380 raddenest(sqrt(2*sqrt(1 + sqrt(3))*cos(3) + cos(3)^2 + 1 + sqrt(3)*cos(3)^2));
381 -1 - sqrt(1 + sqrt(3))*cos(3)$
383 (ra: sqrt(1 + sqrt(3)),
384 z: sqrt(20*ra*sqrt(3 + 3*sqrt(3)) + 12*sqrt(3)*ra*sqrt(3 + 3*sqrt(3))
389 (z: sqrt(1/(4*sqrt(3) + 7) + 1),
393 (z: sqrt(1/(4*sqrt(3) + 7) + 1),
397 /* additional checks */
399 raddenest((3+sqrt(8))^(5/2));
402 /* _rad_denest_ramanujan */
403 raddenest(sqrt(5^(1/3)-4^(1/3)));
404 (2^(1/3)+20^(1/3)-25^(1/3))/3$
406 raddenest(sqrt(28^(1/3)-3));
407 (-1-28^(1/3)+98^(1/3))/3$
409 raddenest(sqrt(3-28^(1/3)));
410 ((-1-28^(1/3)+98^(1/3))*%i)/3$
412 raddenest(sqrt((-13)^(1/3)+(-108)^(1/3)));
413 ((-2^(1/3)+52^(1/3)+169^(1/3))*%i)/3$
415 block([domain:'complex],
416 raddenest(sqrt(13^(1/3)+108^(1/3))));
417 (-2^(1/3)+52^(1/3)+169^(1/3))/3$
419 block([domain:'real],
420 raddenest(sqrt(48^(1/3)-12^(1/3))));
421 (2*9^(1/3)+18^(1/3)-36^(1/3))/3$
423 block([domain:'complex],
424 raddenest(sqrt(48^(1/3)-12^(1/3))));
425 sqrt(2*6^(1/3)-12^(1/3))$
427 /* _rad_denest_cardano */
429 raddenest((2-sqrt(5))^(1/3));
432 raddenest((38+17*sqrt(5))^(1/12));
435 raddenest((41-29*sqrt(2))^(1/5));
438 raddenest((375308427136/390625-(9950503296*sqrt(29))/78125)^(1/8));
441 /* additional symbolic capabalities */
443 (assume(y>0),raddenest(sqrt(9*y+6*x^2*sqrt(y)+x^4)));
447 raddenest(sqrt(-4*log(3)*sqrt(3*x+sqrt(3))+12*x+log(3)^2+4*sqrt(3))));
448 abs(sqrt(12*x+4*sqrt(3))-log(3))$
450 block([domain:'complex],assume(x>0),
451 raddenest(sqrt(-4*log(3)*sqrt(3*x+sqrt(3))+12*x+log(3)^2+4*sqrt(3))));
452 sqrt((sqrt(4*sqrt(3)+12*x)-log(3))^2)$
455 (forget(x>0,y>0),kill(w,a,b,r,rad,z,d2,e,n,d,ra),'done);