@@ -2,756 +2,633 @@
2navhome: /docs/
2navhome: /docs/
5+# 3a: modular and signed ints
6−# 3a: signed and modular ints
7+### `++egcd`
7−### `++fe` :: modulo bloq
9−Modulo bloq
9+Extended Euclidean algorithm
11−Core containing XX
11+Produces `d`, the
12+[greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor)
13+of `a` and `b`. Also produces `u` and `v` such that `au + bv = GCD(a, b)`.
13−Accepts
15+#### Accepts
16−`a` is a `++bloq`.
17+`a` is an atom.
18−Source
19+`b` is an atom.
21− |_ a=bloq
21+#### Produces
23−### `++dif`
23+`d`, the greatest common divisor, is an atom.
25−Produces the difference between two atoms in the modular basis
25+`u`, the coefficient of `a`, is a signed integer.
28−Accepts
27+`v`, the coefficient of `b`, is a signed integer.
31−`a` is a `++bloq`.
29+#### Source
33−`b` is an atom.
31+ ++ egcd
34+ =+ [c=(sun a) d=(sun b)]
35+ =+ [u=[c=(sun 1) d=--0] v=[c=--0 d=(sun 1)]]
36+ |- ^- [d=@ u=@s v=@s]
43+ u [(dif d.u (pro q c.u)) c.u]
44+ v [(dif d.v (pro q c.v)) c.v]
37−Produces
48+#### Examples
40−A `@s`
50+ > (egcd 11 2)
45− ++ dif |=([b/@ c/@] (sit (sub (add out (sit b)) (sit c))))
56+Modulo prime
47−Examples
58+Container core for modular arithmetic functions.
50− > (~(dif fe 3) 63 64)
60+#### Source
64− > (~(dif fe 2) 63 64)
67−------------------------------------------------------------------------
62+ ++ fo
71−Invert mod field
67+#### `++dif`
71+Produces the difference between atoms `b` and `c`, with `a` as the modular base.
73−Inverts the order of the modular field.
73+#### Accepts
75−Accepts
75+`a` is an atom.
78−`b` is a `++bloq`.
77+`b` is an atom.
80−Produces
81+#### Produces
87−------
88+ |= [b=@ c=@]
89+ (sit (sub (add a b) (sit c)))
89− ++ inv |=(b/@ (sub (dec out) (sit b)))
91+#### Examples
91−Examples
93+ > (~(dif fo 6) 1 2)
94− > (~(inv fe 3) 255)
96+ > (~(dif fo 21) 11 45)
96− > (~(inv fe 3) 256)
98+
107−------------------------------------------------------------------------
101+Exponent
109−### `++net`
103+Produces the power of `b` raised to the `c`, with `a` as the modular base.
111−Reverse bytes
105+#### Accepts
113−Revereses bytes within block.
107+`a` is an atom.
115−Accepts
109+`b` is an atom.
118−`b` is a `++bloq`.
111+`c` is an atom.
120−Produces
113+#### Produces
125−Source
117+#### Source
128− ++ net |= b/@ ^- @
120+ |= [b=@ c=@]
129− => .(b (sit b))
121+ ?: =(0 b)
131− b
123+ =+ d=$(b (rsh 0 1 b))
132− =+ c=(dec a)
124+ =+ e=(pro d d)
133− %+ con
125+ ?:(=(0 (end 0 1 b)) e (pro c e))
134− (lsh c 1 $(a c, b (cut c [0 1] b)))
126+
135− $(a c, b (cut c [1 1] b))
137−Examples
127+#### Examples
139−
129+ > (~(exp fo 5) 8 2)
169− 72.057.594.037.927.936
171− 144.115.188.075.855.872
173− 216.172.782.113.783.808
175− 288.230.376.151.711.744
177− 360.287.970.189.639.680
179− 432.345.564.227.567.616
181− 504.403.158.265.495.552
185− 72.620.543.991.349.248
187−------------------------------------------------------------------------
132+ > (~(exp fo 95) 8 2)
135+ > (~(exp fo 195) 8 2)
138+ > (~(exp fo 995) 8 2)
189−### `++out`
144+Divide
191−Max integer value
146+Produces the quotient of `b` divided by `c`, with `a` as the modular base.
193−The maximum integer value that the current block can store.
148+#### Accepts
195−Accepts
150+`a` is an atom.
198−A `++bloq`.
152+`b` is an atom.
200−Produces
156+#### Produces
205−Source
160+#### Accepts
208− ++ out (bex (bex a))
162+`a` is an atom.
210−Examples
164+`b` is an atom.
213− > ~(out fe 0)
166+#### Source
224− \/179.769.313.486.231.590.772.930.519.078.902.473.361.797.697.894.230.657.\/
225− 273.430.081.157.732.675.805.500.963.132.708.477.322.407.536.021.120.113.
226− 879.871.393.357.658.789.768.814.416.622.492.847.430.639.474.124.377.767.
227− 893.424.865.485.276.302.219.601.246.094.119.453.082.952.085.005.768.838.
228− 150.682.342.462.881.473.913.110.540.827.237.163.350.510.684.586.298.239.
229− 947.245.938.479.716.304.835.356.329.624.224.137.216
232−------------------------------------------------------------------------
168+ ++ fra
234−### `++rol`
172+#### Examples
236−Roll left
174+ > (~(fra fo 2) 8 2)
238−Roll `d` to the left by `c` `b`-sized blocks.
177+ > (~(fra fo 3) 8 2)
240−Accepts
180+ > (~(fra fo 4) 8 2)
243−`a` is a `++bloq`.
183+ > (~(fra fo 5) 8 2)
245−`b` is a `++bloq`.
186+***
247−`c` is an atom.
189+Inverse
249−`d` is an atom.
191+Produces an atom by taking the signed modulus of `a` with the coefficient `u`;
192+`u` is produced by taking the `++egcd` of `a` and `b`.
251−Produces
200+#### Produces
256−Source
204+#### Source
208+ =+ c=(dul:si u:(egcd b a) a)
259− ++ rol |= [b/bloq c/@ d/@] ^- @ :: roll left
213+ > (~(inv fo 11) 2)
261− =+ f=(bex (sub a b))
263− (sit (con (lsh b g e) (rsh b (sub f g) e)))
265−Examples
216+ > (~(inv fo 71) 255)
268− > `@ux`(~(rol fe 6) 4 3 0xabac.dedf.1213)
219+ > (~(inv fo 79) 255)
269− 0x1213.0000.abac.dedf
220+ 22
270− > `@ux`(~(rol fe 6) 4 2 0xabac.dedf.1213)
271− 0xdedf.1213.0000.abac
272− > `@t`(~(rol fe 5) 3 1 'dfgh')
274− > `@t`(~(rol fe 5) 3 2 'dfgh')
276− > `@t`(~(rol fe 5) 3 0 'dfgh')
279−------------------------------------------------------------------------
222+ > (~(inv fo 78) 255)
281−### `++ror`
225+ > (~(inv fo 70) 255)
285−Roll `d` to the right by `c` `b`-sized blocks.
231+Multiplication
287−Accepts
233+Produces the multiplication of `b` and `c` modulo `a`.
290−`a` is a `++bloq`.
235+#### Accepts
292−`b` is a `++bloq`.
237+`a` is an atom.
294−`c` is an atom.
239+`b` is an atom.
296−`d` is an atom.
241+`c` is an atom.
298−Produces
243+#### Produces
303−Source
247+#### Source
306− ++ ror |= {b/bloq c/@ d/@} ^- @ :: roll right
308− =+ f=(bex (sub a b))
310− (sit (con (rsh b g e) (lsh b (sub f g) e)))
313−Examples
253+#### Examples
316− > `@ux`(~(ror fe 6) 4 1 0xabac.dedf.1213)
255+ > (~(pro fo 3) 11 4)
317− 0x1213.0000.abac.dedf
256+ 2
318− > `@ux`(~(ror fe 6) 3 5 0xabac.dedf.1213)
319− 0xacde.df12.1300.00ab
320− > `@ux`(~(ror fe 6) 3 3 0xabac.dedf.1213)
321− 0xdf12.1300.00ab.acde
322− > `@t`(~(rol fe 5) 3 0 'hijk')
324− > `@t`(~(rol fe 5) 3 1 'hijk')
326− > `@t`(~(rol fe 5) 3 2 'hijk')
329−------------------------------------------------------------------------
258+ > (mod 44 3)
335−Sum two numbers in this modular field.
266+Produces the remainder of `b` modulo `a`.
337−Accepts
268+#### Accepts
340−`a` is a `++bloq`.
270+`a` is an atom.
342`b` is an atom.
272`b` is an atom.
346−Produces
274+#### Produces
351−Source
278+#### Source
354− ++ sum |=([b/@ c/@] (sit (add b c))) :: wrapping add
280+ ++ sit
356−Examples
284+#### Examples
359− > (~(sum fe 3) 10 250)
286+ > (~(sit fo 3) 14)
365− > (~(sum fe 2) 14 2)
367− > (~(sum fe 2) 14 3)
369− > (~(sum fe 4) 10.000 256)
371− > (~(sum fe 4) 10.000 100.000)
376−Atom in mod block representation
292+Modular sum
378−Produce an atom in the current modular block representation.
294+Produces the remainder of `(b + c) mod a`.
380−Accepts
296+#### Accepts
383−`a` is a `++bloq`.
298+`a` is an atom.
385`b` is an atom.
300`b` is an atom.
387−Produces
304+#### Produces
392−Source
308+#### Source
395− ++ sit |=(b/@ (end a 1 b)) :: enforce modulo
310+ ++ sum
397−Examples
315+#### Examples
408− > (~(sit fe 2) 10.000)
317+ > (~(sum fo 3) 14 3)
426### `++si`
324### `++si`
428− ++ si !: :: signed integer
326+Signed integer
328+Container core for signed integer functions.
431−Container core for the signed integer functions.
336+#### Discussion
338+The signed-integer type is represented by the `@s` aura. Positive integers are
339+represented with a two prepending `-`s, and negative integers are represented
340+with two; `--1` and `-1`, respectively.
433−------------------------------------------------------------------------
342+Positive signed integers correspond to odd atoms of twice their absolute value,
343+and negative signed integers correspond to atoms of twice their absolute value
344+minus one. For example:
435−### `++abs`
357+### `++abs:si`
437Absolute value
359Absolute value
439−Produces the absolute value of a signed integer.
361+Produces the absolute value of signed integer `a`.
441−Accepts
363+#### Accepts
444−`a` is a signed integer, `@s`.
365+`a` is a signed integer.
446−Produces
367+#### Produces
454− ++ abs |=(a/@s (add (end 0 1 a) (rsh 0 1 a))) :: absolute value
456−Examples
373+ ++ abs |=(a=@s (add (end 0 1 a) (rsh 0 1 a)))
459− > (abs:si -2)
375+#### Examples
466−------------------------------------------------------------------------
377+ > (abs:si -11)
468−### `++dif`
380+ > (abs:si --520)
472−Produces the difference between two signed integers `b` and `c`.
386+Subtraction
474−Accepts
388+Produces the difference of `a` minus `b`.
477−`a` is a signed integer, `@s`.
390+#### Accepts
479−`b` is a signed integer, `@s`.
392+`a` is a signed integer.
481−Produces
394+`b` is a signed integer.
484−A `@s`.
396+#### Produces
487−Source
400+#### Source
490− ++ dif |= [a/@s b/@s] :: subtraction
402+ ++ dif |= [a=@s b=@s]
491 (sum a (new !(syn b) (abs b)))
403 (sum a (new !(syn b) (abs b)))
493−Examples
405+#### Examples
496− > (dif:si --10 -7)
407+ > (dif:si --3 -2)
500− > (dif:si `@s`0 --7)
502− > (dif:si `@s`0 `@s`7)
505−------------------------------------------------------------------------
410+ > (dif:si -3 --2)
507−### `++dul`
414+### `++dul:si`
511−Produces the modulus of two signed integers.
418+Produces the remainder of `b` modulo `a`.
513−Accepts
420+#### Examples
516−`a` is a signed integer, `@s`.
422+`a` is a signed integer.
518`b` is an atom.
424`b` is an atom.
520−Produces
426+#### Produces
523−An atom.
428+An atom.
525−Source
430+#### Source
528− ++ dul |= [a/@s b/@] :: modulus
432+ ++ dul |= [a=@s b=@]
529 =+(c=(old a) ?:(-.c (mod +.c b) (sub b +.c)))
433 =+(c=(old a) ?:(-.c (mod +.c b) (sub b +.c)))
531−Examples
435+#### Examples
536− > (dul:si --9 4)
437+ > `@s`(dul:si -1 --5)
538− > (dul:si --9 5)
439+
542− > (dul:si --90 --10)
442+ > `@s`(dul:si -1 10)
545−------------------------------------------------------------------------
445+ > `@s`(dul:si -11 -61)
547−### `++fra`
450+#### `++fra:si`
551−Produces the quotient of two signed integers.
454+Produces the quotient of `b` divided by `c`.
553−Accepts
456+#### Accepts
556−`a` is a signed integer, `@s`.
458+`a` is a signed integer.
558−`b` is a signed integer, `@s`.
460+`b` is a signed integer.
560−Produces
462+#### Produces
563−An atom.
464+A signed atom.
565−Source
466+#### Source
568− ++ fra |= [a/@s b/@s] :: divide
468+ ++ fra |= [a=@s b=@s]
569 (new =(0 (mix (syn a) (syn b))) (div (abs a) (abs b)))
469 (new =(0 (mix (syn a) (syn b))) (div (abs a) (abs b)))
571−Examples
471+#### Examples
574− > (fra:si --10 -2)
473+ > (fra:si -1 -1)
579−------------------------------------------------------------------------
479+ > (fra:si -0 -1)
581### `++new`
484### `++new`
583−Signed integer
486+Atom to @s
585−Produces a signed integer from a loobean sign value `a` and an atom `b`.
488+Produces a signed integer from an atom `b`. The product's sign is determined
489+by the value of flag `a`: `&` will result in a prepending `--`, and `|` will
490+result in a prepending `-`.
587−Accepts
492+#### Accepts
590−`a` is a loobean.
494+`a` is a flag.
592`b` is an atom.
496`b` is an atom.
594−Produces
498+#### Produces
597−A `@s`
500+A signed integer.
599−Source
502+#### Source
602− ++ new |= [a/? b/@] :: [sign value] to @s
504+ ++ new |= [a=? b=@]
603 `@s`?:(a (mul 2 b) ?:(=(0 b) 0 +((mul 2 (dec b)))))
505 `@s`?:(a (mul 2 b) ?:(=(0 b) 0 +((mul 2 (dec b)))))
605−Examples
507+#### Examples
608− > (new:si [& 10])
509+ > (new:si | 2)
615−------------------------------------------------------------------------
512+ > (new:si & 2)
617−### `++old`
515+ > (new:si & -2)
619−sign value
518+ > (new:si & --2)
621−Produces the cell `[sign value]` representations of a signed integer.
521+***
623−Accepts
524+Sign and absolute value
626−`a` is a `@s`.
526+Produces a cell composed of a `%.y` or `%.n`, depending on whether `a` is
527+positive or negative, and the absolute value of `a`.
628−Produces
529+#### Accepts
631−A cell of a boolean and an atom.
531+`a` is a signed integer.
633−Source
533+#### Produces
636− ++ old |=(a/@s [(syn a) (abs a)]) :: [sign value]
535+A cell composed of a `term` and an atom.
638−Examples
537+#### Source
641− > (old:si 7)
539+ ++ old |=(a=@s [(syn a) (abs a)])
653−------------------------------------------------------------------------
541+#### Examples
655−### `++pro`
543+ > (old:si -2)
657−Multiply
546+ > (old:si --2)
659−Produces the product of two signed integers.
549+***
661−Accepts
552+Multiplication
664−`a` is a `@s`.
554+Produces a signed integer by multiplying `a` and `b`.
666−`b` is a `@s`.
556+#### Accepts
668−Produces
558+`a` is an unsigned integer.
671−A `@s`.
560+`b` is an unsigned integer.
673−Source
562+#### Source
676− ++ pro |= [a/@s b/@s] :: multiplication
564+ ++ pro |= [a=@s b=@s]
677 (new =(0 (mix (syn a) (syn b))) (mul (abs a) (abs b)))
565 (new =(0 (mix (syn a) (syn b))) (mul (abs a) (abs b)))
679−Examples
567+#### Examples
682− > (pro:si -4 --2)
569+ > (pro:si -3 -3)
686− > (pro:si --10.000.000 -10)
688− > (pro:si -1.337 --0)
691−------------------------------------------------------------------------
572+ > (pro:si -3 --3)
693−### `++rem`
576+### `++rem:si`
697−Produces the remainder from a division of two signed integers.
580+Produces a signed integer that is the remainder of `a` divided by `b`.
699−Accepts
582+#### Accepts
702−`a` is a `@s`.
584+`a` is a signed integer.
704−`b` is a `@s`.
586+`b` is a signed integer.
706−Produces
588+#### Produces
709−A `@sd`.
590+A signed integer.
711−Source
592+#### Source
714− ++ rem |=([a/@s b/@s] (dif a (pro b (fra a b)))) :: remainder
594+ ++ rem |=([a=@s b=@s] (dif a (pro b (fra a b))))
716−Examples
596+#### Examples
719− > (rem:si -10 -4)
598+ > (rem:si -17 -3)
721− > (rem:si --10 --4)
601+ > (rem:si --17 -3)
723− > (rem:si --10 -4)
604+ > (rem:si -17 --3)
727− > (rem:si --0 --10.000)
730−------------------------------------------------------------------------
736−Sum two signed integers.
617+Produces an atom by adding `a` and `b`.
738−Accepts
619+#### Accepts
741−`b` is a `@s`.
621+`a` is a signed integer.
743−`c` is a signed integer `@s`.
623+`b` is a signed integer.
745−Produces
625+#### Produces
748−A `@s`.
627+A signed integer.
750−Source
629+#### Source
753− ++ sum |= [a/@s b/@s] :: addition
631+ ++ sum |= [a=@s b=@s]
755 =+ [c=(old a) d=(old b)]
632 =+ [c=(old a) d=(old b)]
@@ -765,115 +642,104 @@ Source
765 (new & (sub +.d +.c))
642 (new & (sub +.d +.c))
766 (new | (add +.c +.d))
643 (new | (add +.c +.d))
768−Examples
645+#### Examples
771− > (sum:si --10 --10)
647+ > (sum:si -11 --2)
780−------------------------------------------------------------------------
650+ > (sum:si --2 --2)
782−### `++sun`
654+#### `++sun:si`
784−Signed from unsigned
656+@u to @s
786−Produces a signed integer from an unsigned integer.
658+Multiplies the unsigned integer `a` by two, producing an atom.
788−Note that the result must be manually cast to some `@s` odor to be
660+#### Accepts
789−inferred as an unsigned integer in the type system.
791−Accepts
662+`a` is an unsigned integer.
794−`a` is a `@u`.
664+#### Produces
799−A `@s`.
668+#### Source
801−Source
670+ ++ sun |=(a=@u (mul 2 a))
804− ++ sun |=(a/@u (mul 2 a)) :: @u to @s
672+#### Examples
806−Examples
674+ > (mul 2 90)
809− > `@s`10
677+ > (mul 2 --90)
811− > (sun:si 10)
679+ > `@u`--90
817− > `@sd`(sun:si 12.345)
820−------------------------------------------------------------------------
682+ > (mul 2 --89)
822−### `++syn`
687+ > (mul 2 -89)
826−Produce the sign of a signed integer, where `&` is posiitve, and `|` is negative.
697+Tests whether signed atom `a` is positive or negative. `%.y` is produced if `a`
698+is positive, and `%.n` is produced if `a` is negative.
828−Accepts
700+#### Accepts
831−`a` is a `@s`.
702+`a` is a signed integer.
833−Produces
704+#### Produces
836−A boolean.
706+An `term`.
838−Source
708+#### Source
841− ++ syn |=(a/@s =(0 (end 0 1 a))) :: sign test
710+ ++ syn |=(a=@s =(0 (end 0 1 a)))
843−Examples
712+#### Examples
846 > (syn:si -2)
714 > (syn:si -2)
848 > (syn:si --2)
717 > (syn:si --2)
853−------------------------------------------------------------------------
855−### `++cmp`
722+### `++cmp:si`
859−Compare two signed integers.
726+Compares `a` and `b` to see which is greater. If `a` is greater than `b`, `--1`
727+is produced. If `b` is greater than `a`, `-1` is produced. If `a` and `b` are
728+equal, `--0` is produced.
861−Accepts
730+#### Accepts
864−`b` is a `@s`.
732+`a` is a signed integer.
866−`c` is a `@s`.
734+`b` is a signed integer.
868−Produces
736+#### Produces
871−A `@s`.
738+A signed integer.
873−Source
740+#### Source
876− ++ cmp |= [a/@s b/@s] :: compare
742+ ++ cmp |= [a=@s b=@s]
878 ?: =(a b)
744 ?: =(a b)
@@ -883,65 +749,28 @@ Source
886− ?: (syn b)
752+ ?: (syn b)
888− ?: (gth a b)
754+ ?: (gth a b)
892−Examples
759+#### Examples
895− > (cmp:si --10 --10)
761+ > (cmp:si -2 --1)
910− ++ egcd !: :: schneier's egcd
912− =+ si
767+ > (cmp:si --2 --1)
913− =+ [c=(sun a) d=(sun b)]
914− =+ [u=[c=(sun 1) d=--0] v=[c=--0 d=(sun 1)]]
915− |- ^- {d/@ u/@s v/@s}
918− :: ?> ?& =(c (sum (pro (sun a) c.u) (pro (sun b) c.v)))
919− :: =(d (sum (pro (sun a) d.u) (pro (sun b) d.v)))
925− u [(dif d.u (pro q c.u)) c.u]
926− v [(dif d.v (pro q c.v)) c.v]
931−Greatest common denominator
939− > (egcd (shaf ~ %ham) (shaf ~ %sam))
941− u=59.983.396.314.566.203.239.184.568.129.921.874.787
942− v=38.716.650.351.034.402.960.165.718.823.532.275.722