-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpadics-doc.wxm
1391 lines (889 loc) · 32.1 KB
/
padics-doc.wxm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* [wxMaxima batch file version 1] [ DO NOT EDIT BY HAND! ]*/
/* [ Created with wxMaxima version 18.10.1 ] */
/* [wxMaxima: title start ]
Computations with p-adic numbers in Maxima
[wxMaxima: title end ] */
/* [wxMaxima: comment start ]
Jose A Vallejo
Facultad de Ciencias
Universidad Autonoma de San Luis Potosi (Mexico)
http://galia.fc.uaslp.mx/~jvallejo
email:[email protected]
[wxMaxima: comment end ] */
/* [wxMaxima: comment start ]
Abstract: This is just a first attempt to create a Maxima package for
working with p-adic numbers. It is extremely ugly and slow, lacking
anything remotely resembling elegance or optimization, but at least
it gives correct answers (for all those examples that I have been
able to find in the literature).
The current version is suitable for basic courses on the subject of p-adic
analysis, and maybe for constructing examples of some simple theoretical
ideas.
Topics covered are:
1. p-adic norm and distance
2. Finite-segment representations of Q_p (Hensel codes)
3. p-adic arithmetic
4. Conversion from Hensel codes to rational functions
5. Newton's method and square roots in Q_p
6. p-adic systems of linear equations
REMARK: Some functions in the package make use of the commands
firstn and lastn, which require a Maxima version 5.41 or higher.
[wxMaxima: comment end ] */
/* [wxMaxima: section start ]
Loading the package
[wxMaxima: section end ] */
/* [wxMaxima: comment start ]
The most simple way to do it consists in putting a copy of padics.mac
in your working directory. If a wxMaxima worksheet is opened from
that directory, you can load the package with
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
load("padics.mac");
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
The other option is to do a global installation, putting a copy of
padics.mac in /usr/share/maxima/5.42.1/share/contrib or its
Windows equivalent. Then, load the package with the same command
above.
[wxMaxima: comment end ] */
/* [wxMaxima: section start ]
p-adic norm
[wxMaxima: section end ] */
/* [wxMaxima: comment start ]
We can compute the p-adic order of a rational number with padic_order.
The syntax is padic_order(rational,prime):
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
padic_order(144,3);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
padic_order(17,3);
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
We follow the convention that the order of 0 is always (real) infinity:
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
makelist(padic_order(0,i),i,[2,3,5,7,11,13]);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
padic_order(3/10,5);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
padic_order(36015/88,7);
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
Notice that the p-adic order is an even function:
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
padic_order(-3/10,5);
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
The reduction to the canonical form of a rational number
r=a/b=p^porder(r) a'/b'
can be achieved with padic_canonical. The result has the form of a list
[p^porder(r),a'/b']
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
padic_canonical(0.234,2);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
padic_canonical(0,3);
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
The p-adic norm of a rational numer is computed by padic_norm:
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
makelist(padic_norm(0,j),j,[2,3,5,7,11,13,17]);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
padic_norm(17,17);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
padic_norm(144,3);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
padic_norm(12,5);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
makelist(padic_norm(162/13,k),k,[3,13]);
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
The next example comes from http://mathworld.wolfram.com/p-adicNorm.html
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
makelist(padic_norm(140/297,k),k,[2,3,5,7,11]);
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
Another example, this one from www.asiapacific-mathnews.com/03/0304/0001_0006.pdf
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
makelist(padic_norm(63/550,k),k,[2,3,5,7,11,13]);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
padic_norm(0.234,2);
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
Let us check the triangle equality:
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
padic_norm(3/10,5);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
padic_norm(40,5);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
padic_norm(3/10-40,5);
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
The next examples come from https://www.sangakoo.com/en/unit/p-adic-distance
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
padic_norm(10/12,2);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
padic_norm(10/12,5);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
padic_norm(10/12,7);
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
Of course, once we have a norm available, we can define the
associated p-adic distance, here denoted padic_distance:
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
padic_distance(2,28814,7);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
padic_distance(2,3,7);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
padic_distance(2166^2,2,7);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
padic_distance(3,3+29^4,29);
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
The next example comes from https://www.sangakoo.com/en/unit/p-adic-distance
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
padic_distance(82,1,3);
/* [wxMaxima: input end ] */
/* [wxMaxima: section start ]
p-adic expansions (Hensel codes)
[wxMaxima: section end ] */
/* [wxMaxima: comment start ]
The only explicit representation we can get are those of rational numbers
(periodic p-adic expansions). Here we consider Hensel (pseudo)codes,
which basically are truncations of the p-adic expansions to a given order.
The output has the form [[exponent],mantissa], and the algorithm used
here is based on the one proposed by G. Bachman in [1].
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
hensel(5/7,7,7);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
hensel(-84,7,9);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
hensel(8/3,5,9);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
hensel(3/4,5,4);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
hensel(2/15,5,7);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
hensel(7/6,5,4);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
hensel(2/7,5,4);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
hensel(1/12,5,7);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
hensel(5/8,5,4);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
hensel(1/2,5,4);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
hensel(1/3,5,4);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
hensel(1/4,5,4);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
hensel(1/4,5,7);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
hensel(1/25,5,4);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
hensel(-7/8,3,5);
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
The command nicehensel displays the result in the form commonly
found in textbooks and expository works (this form has the drawback
that, when p>7, is is impossible to distinguish between the number 11
and two consecutive 1's, so it will not be used in what follows), that is,
something like
r = a_{-e}...a_{-1}.a_0a_1a_2...
where e is the order of r.
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
nicehensel(8/3,5,9);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
nicehensel(8/75,5,9);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
nicehensel(3/4,5,4);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
nicehensel(2/15,5,7);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
nicehensel(1/3,5,7);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
nicehensel(-1/3,5,7);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
nicehensel(5/1,5,4);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
nicehensel(25,5,4);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
nicehensel(2/7,5,4);
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
Let us compare this with the table presented in [2]
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
h[i,j]:=nicehensel(i/j,5,4)$
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
genmatrix(h,17,17);
/* [wxMaxima: input end ] */
/* [wxMaxima: section start ]
Arithmetic of p-adics
[wxMaxima: section end ] */
/* [wxMaxima: comment start ]
The basic functions are implemented as:
* padic_sum (sum, addition)
* padic_substract (difference, substraction)
* padic_multiply (product, multiplication)
* padic_divide (division, quotient)
The syntax is quite evident: each function takes the arguments
(operand1,operand2,p)
[wxMaxima: comment end ] */
/* [wxMaxima: comment start ]
Some test numbers:
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
l1:hensel(3/10,5,4);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
l2:hensel(1/2,5,4);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
padic_sum(l1,l2,5);
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
Notice that the Hensel code for the sum 3/10+1/1 corresponds to 4/5:
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
hensel(4/5,5,4);
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
Also, notice that a conequence of using a finite segment representation is that
adding up a really small number with a relly big one just gives the bigger:
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
padic_sum([[2],2,5,1,5],[[-3],3,3,3,2],7);
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
Another test (this is an example in [2]) with p=5 and r=9
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
h1:hensel(2/3,5,9);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
h2:hensel(5/6,5,9);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
padic_sum(h1,h2,5);
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
Let us check the following example in [2]:
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
padic_substract(h1,h2,5);
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
And those of [3]:
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
padic_substract(hensel(3/4,5,4),hensel(3/2,5,4),5);
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
An example on multiplication, also from [3]
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
t1:hensel(4/15,5,4);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
t2:hensel(5/2,5,4);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
padic_multiply(t1,t2,5);
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
Another example from [2]:
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
padic_multiply(h1,h2,5);
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
Example from [4]:
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
al:hensel(1/4,5,4);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
be:hensel(1/3,5,4);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
padic_multiply(al,be,5);
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
The function normalhensel normalizes the Hensel code so that the
first digit after the dot is not zero:
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
normalize_hensel([[-1],0,0,1,2,3]);
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
It is internally used by the function for computing divisions. Our first
example is a trivial one:
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
padic_divide([[0],4,0,0,0,0,0,0],[[0],2,0,0,0,0,0,0],7);
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
The next example is from [2]:
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
dividend:[[0],4,1,3,1,3,1,3];
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
divisor:[[0],3,4,2,4,2,4,2];
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
padic_divide(dividend,divisor,5);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
d1:[[0],2,1,1,1];
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
d2:[[-1],1,1,0,0];
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
padic_divide(d1,d2,5);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
padic_divide([[0],4,3,3,3],[[0],0,1,4,0],5);
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
This is the example presented in [3]: 1/4/(1/2+1/3)+1/25
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
padic_sum(padic_divide([[0],4,3,3,3],padic_sum([[0],3,2,2,2],[[0],2,3,1,3],5),5),[[-2],1,0,0,0],5);
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
A quick check that the answer is correct:
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
1/4/(1/2+1/3)+1/25;
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
hensel(17/50,5,4);
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
Another example, from [4]:
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
alf:hensel(8/9,5,4);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
bet:hensel(1/2,5,4);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
padic_divide(alf,bet,5);
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
From [6]:
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
hensel(1/333333,5,27);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
time(%);
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
Reference [6] states that 3 seconds were required for this computation,
using a C++ library, in 2005.
[wxMaxima: comment end ] */
/* [wxMaxima: section start ]
From Hensel codes to rational numbers
[wxMaxima: section end ] */
/* [wxMaxima: comment start ]
Passing from Hensel codes to equivalent rational numbers in Q_p
requires the use of the appropriate Farey fractions. A function for
generating the Farey fractions I_n is farey:
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
farey(17);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
time(%);
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
The package implements the algorithm by Gregory and Krishnamurthy
described in the book [7]. We have added a few cases detected
independently to give cleaner results. For instance, cases such as
[[m],a0,0,0,0,...],[[m],a0,p-1,p-1,p-1,...] or [[m],a0,a1,...,ak,0,0,...,0s]
with s>k. The function is hensel_to_farey, and takes as arguments a
list containing the Hensel code of a number and the prime p:
[wxMaxima: comment end ] */
/* [wxMaxima: comment start ]
Some trivial examples:
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
hensel_to_farey([[2],3,0,0,0,0,0,0,0],7);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
hensel_to_farey([[0],4,4,4,4,4,4,4],5);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
hensel_to_farey([[0],3,3,3,3,3,3,3],5);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
hensel_to_farey([[0],0,0,0,0,0,0],5);
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
From [6] (pg 12)
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
hensel_to_farey([[0],2,3,1,5],7);
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
From [7] (pp. 100 and ff)
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
hensel_to_farey([[0],0,2,4,1],5);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
hensel_to_farey([[1],2,4,1,4],5);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
hensel_to_farey([[-1],3,2,2,2],5);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
hensel_to_farey([[0],3,2,2,2],5);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
hensel_to_farey([[0],2,3,1,3],5);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
hensel_to_farey([[0],0,6,0,6],7);
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
A quick check:
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
hensel(1/3,5,4);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
hensel(-7/8,7,4);
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
Example from [11]:
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
hensel_to_farey([[0],2,2,1,0],5);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
hensel_to_farey([[0],1,2,1,4],5);
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
Examples from the paper [8]:
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
hensel_to_farey([[0],2,2,1,0],5);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
hensel_to_farey([[0],2,2,3,4],5);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
hensel_to_farey([[0],4,2,3,4],5);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
hensel_to_farey([[0],1,1,0,0,1,0],3);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
hensel_to_farey([[0],1,2,1,4],5);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
hensel_to_farey([[0],3,4,2,3],5);
/* [wxMaxima: input end ] */
/* [wxMaxima: section start ]
Hensel codes of square roots
[wxMaxima: section end ] */
/* [wxMaxima: comment start ]
Now, suppose we want to compute the square root of 2 with p=7.
We will follow [9] for that.
[wxMaxima: comment end ] */
/* [wxMaxima: comment start ]
An integer q is called a quadratic residue mod p if there exists an
integer x such that x^2 =q mod p.
[wxMaxima: comment end ] */
/* [wxMaxima: comment start ]
Remark: If p=2, every integer is a quadratic residue. If p is a prime
different from 2, there are (p-1)/2 residues and (p-1)/2 non-residues
in F_p-{0} (that is, the multiplicative group of F_p or F^*_p).
As a consequence of the reciprocity law:
a) If p=1 mod 4, then -1 is a quadratic residue mod p.
b) I p=3 mod 4, then -1 is a non residue mod p.
Notice that every prime is equivalent to 1 or 3 mod 4.
[wxMaxima: comment end ] */
/* [wxMaxima: comment start ]
Euler's criterion for quadratic reciprocity states that (given a in Z
and p an odd prime) the Legendre symbol evaluates to (a|p)=a^(p-1)/2.
And this equals 1 if a is a quadratic residue and -1 if it is not.
[wxMaxima: comment end ] */
/* [wxMaxima: comment start ]
First, we introduce a function to determine whether a given integer
is a quadratic residue modulo p or not:
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
sqrtmod(2,5);
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
Thus n=2 is not a quadratic residue modulo p=5. But it is modulo p=7:
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
sqrtmod(2,7);
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
If n is a quadratic residue modulo p with root x, then another root is
given by the y such that y=-x mod p. We compute the padic roots with
Newton's method, and the command padic_sqrt --whose syntax is
padic_sqrt(number,p)-- admits an optional argument fixing the number
of iterations to be done, as in padic_sqrt(number,p,iterations).
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
padic_sqrt(2,7);
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
We can get the corresponding Hensel of the roots codes through
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
map(lambda([u],hensel(u,7,9)),%);
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
Another example: the square root of 7 in Q_3 (from [9])
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
padic_sqrt(7,3,3);
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
The first fraction contains 3x2=6 exact digits to determine the square
root of 7 in Q_3. To get its corresponding Farey sequence it does not
make sense to take more than 6 digits. Hence we do the following:
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
map(lambda([u],hensel(u,3,6)),%);
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
hensel_to_farey(%[1],3);
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
The result is not exactly the rational we started with, but it is very close
in Q_3:
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
padic_distance(977/368,1/25,3);
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
More examples:
[wxMaxima: comment end ] */
/* [wxMaxima: input start ] */
padic_sqrt(6,5)[1];
/* [wxMaxima: input end ] */
/* [wxMaxima: input start ] */
hensel(%,5,4);
/* [wxMaxima: input end ] */
/* [wxMaxima: comment start ]
The results can be compared against those given in