on master help sign in
Update 3a.md
cc7af72rmariani
docs/hoon/library/3a.md+443−614
@@ -2,756 +2,633 @@
2navhome: /docs/
2navhome: /docs/
3---
3---
4
4
5+# 3a: modular and signed ints
5
6
6−# 3a: signed and modular ints
7+### `++egcd`
7−### `++fe` :: modulo bloq
8
8
9−Modulo bloq
9+Extended Euclidean algorithm
10
10
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)`.
12
14
13−Accepts
15+#### Accepts
14−-------
15
16
16−`a` is a `++bloq`.
17+`a` is an atom.
17
18
18−Source
19+`b` is an atom.
19−------
20
20
21− |_ a=bloq
21+#### Produces
22
22
23−### `++dif`
23+`d`, the greatest common divisor, is an atom.
24
24
25−Produces the difference between two atoms in the modular basis
25+`u`, the coefficient of `a`, is a signed integer.
26−representation.
27
26
28−Accepts
27+`v`, the coefficient of `b`, is a signed integer.
29−-------
30
28
31−`a` is a `++bloq`.
29+#### Source
32
30
33−`b` is an atom.
31+ ++ egcd
32+ |= [a=@ b=@]
33+ =+ si
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]
37+ ?: =(--0 c)
38+ [(abs d) d.u d.v]
39+ =+ q=(fra d c)
40+ %= $
41+ c (dif d (pro q c))
42+ d c
43+ u [(dif d.u (pro q c.u)) c.u]
44+ v [(dif d.v (pro q c.v)) c.v]
45+ ==
34
46
35−`c` is an atom.
36
47
37−Produces
48+#### Examples
38−--------
39
49
40−A `@s`
50+ > (egcd 11 2)
51+ [d=1 u=--1 v=-5]
41
52
42−Source
53+***
43−------
54+### `++fo`
44
55
45− ++ dif |=([b/@ c/@] (sit (sub (add out (sit b)) (sit c))))
56+Modulo prime
46
57
47−Examples
58+Container core for modular arithmetic functions.
48−--------
49
59
50− > (~(dif fe 3) 63 64)
60+#### Source
51− 255
52− > (~(dif fe 3) 5 10)
53− 251
54− > (~(dif fe 3) 0 1)
55− 255
56− > (~(dif fe 0) 9 10)
57− 1
58− > (~(dif fe 0) 9 11)
59− 0
60− > (~(dif fe 0) 9 12)
61− 1
62− > (~(dif fe 2) 9 12)
63− 13
64− > (~(dif fe 2) 63 64)
65− 15
66
61
67−------------------------------------------------------------------------
62+ ++ fo
63+ ^?
64+ |_ a=@
68
65
69−### `++inv`
70
66
71−Invert mod field
67+#### `++dif`
68+
69+Subtraction
70+
71+Produces the difference between atoms `b` and `c`, with `a` as the modular base.
72
72
73−Inverts the order of the modular field.
73+#### Accepts
74
74
75−Accepts
75+`a` is an atom.
76−-------
77
76
78−`b` is a `++bloq`.
77+`b` is an atom.
78+
79+`c` is an atom.
79
80
80−Produces
81+#### Produces
81−--------
82
82
83An atom.
83An atom.
84
84
85+#### Source
85
86
86−Source
87+ ++ dif
87−------
88+ |= [b=@ c=@]
89+ (sit (sub (add a b) (sit c)))
88
90
89− ++ inv |=(b/@ (sub (dec out) (sit b)))
91+#### Examples
90
92
91−Examples
93+ > (~(dif fo 6) 1 2)
92−--------
94+ 5
93
95
94− > (~(inv fe 3) 255)
96+ > (~(dif fo 21) 11 45)
95− 0
97+ 8
96− > (~(inv fe 3) 256)
98+
97− 255
99+#### `++exp`
98− > (~(inv fe 3) 0)
99− 255
100− > (~(inv fe 3) 1)
101− 254
102− > (~(inv fe 3) 2)
103− 253
104− > (~(inv fe 3) 55)
105− 200
106
100
107−------------------------------------------------------------------------
101+Exponent
108
102
109−### `++net`
103+Produces the power of `b` raised to the `c`, with `a` as the modular base.
110
104
111−Reverse bytes
105+#### Accepts
112
106
113−Revereses bytes within block.
107+`a` is an atom.
114
108
115−Accepts
109+`b` is an atom.
116−-------
117
110
118−`b` is a `++bloq`.
111+`c` is an atom.
119
112
120−Produces
113+#### Produces
121−--------
122
114
123An atom.
115An atom.
124
116
125−Source
117+#### Source
126−------
118+
127−
119+ ++ exp
128− ++ net |= b/@ ^- @
120+ |= [b=@ c=@]
129− => .(b (sit b))
121+ ?: =(0 b)
130− ?: (lte a 3)
122+ 1
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))
136−
137−Examples
127+#### Examples
138−--------
128+
139−
129+ > (~(exp fo 5) 8 2)
140− > (~(net fe 3) 64)
141− 64
142− > (~(net fe 3) 128)
143− 128
144− > (~(net fe 3) 255)
145− 255
146− > (~(net fe 3) 256)
147− 0
148− > (~(net fe 3) 257)
149− 1
150− > (~(net fe 3) 500)
151− 244
152− > (~(net fe 3) 511)
153− 255
154− > (~(net fe 3) 512)
155− 0
156− > (~(net fe 3) 513)
157− 1
158− > (~(net fe 3) 0)
159− 0
160− > (~(net fe 3) 1)
161− 1
162− > (~(net fe 0) 1)
163− 1
164− > (~(net fe 0) 2)
165− 0
166− > (~(net fe 0) 3)
167 1
130 1
168− > (~(net fe 6) 1)
169− 72.057.594.037.927.936
170− > (~(net fe 6) 2)
171− 144.115.188.075.855.872
172− > (~(net fe 6) 3)
173− 216.172.782.113.783.808
174− > (~(net fe 6) 4)
175− 288.230.376.151.711.744
176− > (~(net fe 6) 5)
177− 360.287.970.189.639.680
178− > (~(net fe 6) 6)
179− 432.345.564.227.567.616
180− > (~(net fe 6) 7)
181− 504.403.158.265.495.552
182− > (~(net fe 6) 512)
183− 562.949.953.421.312
184− > (~(net fe 6) 513)
185− 72.620.543.991.349.248
186
131
187−------------------------------------------------------------------------
132+ > (~(exp fo 95) 8 2)
133+ 66
134+
135+ > (~(exp fo 195) 8 2)
136+ 61
137+
138+ > (~(exp fo 995) 8 2)
139+ 256
140+
141+***
142+### `++fra`
188
143
189−### `++out`
144+Divide
190
145
191−Max integer value
146+Produces the quotient of `b` divided by `c`, with `a` as the modular base.
192
147
193−The maximum integer value that the current block can store.
148+#### Accepts
194
149
195−Accepts
150+`a` is an atom.
196−-------
197
151
198−A `++bloq`.
152+`b` is an atom.
199
153
154+`c` is an atom.
155+
200−Produces
156+#### Produces
201−--------
202
157
203An atom.
158An atom.
204
159
205−Source
160+#### Accepts
206−------
207
161
208− ++ out (bex (bex a))
162+`a` is an atom.
209
163
210−Examples
164+`b` is an atom.
211−--------
212
165
213− > ~(out fe 0)
166+#### Source
214− 2
215− > ~(out fe 1)
216− 4
217− > ~(out fe 2)
218− 16
219− > ~(out fe 3)
220− 256
221− > ~(out fe 4)
222− 65.536
223− > ~(out fe 10)
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
230− \/ \/
231
167
232−------------------------------------------------------------------------
168+ ++ fra
169+ |= [b=@ c=@]
170+ (pro b (inv c))
233
171
234−### `++rol`
172+#### Examples
235
173
236−Roll left
174+ > (~(fra fo 2) 8 2)
175+ 0
237
176
238−Roll `d` to the left by `c` `b`-sized blocks.
177+ > (~(fra fo 3) 8 2)
178+ 1
239
179
240−Accepts
180+ > (~(fra fo 4) 8 2)
241−-------
181+ 0
242
182
243−`a` is a `++bloq`.
183+ > (~(fra fo 5) 8 2)
184+ 4
244
185
245−`b` is a `++bloq`.
186+***
187+### `++inv`
246
188
247−`c` is an atom.
189+Inverse
248
190
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`.
250
193
194+#### Accepts
195+
196+`a` is an atom.
197+
198+`b` is an atom.
199+
251−Produces
200+#### Produces
252−--------
253
201
254An atom.
202An atom.
255
203
256−Source
204+#### Source
257−------
205+
206+ ++ inv
207+ |= b=@
208+ =+ c=(dul:si u:(egcd b a) a)
209+ c
210+
211+#### Examples
258
212
259− ++ rol |= [b/bloq c/@ d/@] ^- @ :: roll left
213+ > (~(inv fo 11) 2)
260− =+ e=(sit d)
214+ 6
261− =+ f=(bex (sub a b))
262− =+ g=(mod c f)
263− (sit (con (lsh b g e) (rsh b (sub f g) e)))
264
215
265−Examples
216+ > (~(inv fo 71) 255)
266−--------
217+ 22
267
218
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')
273− 'hdfg'
274− > `@t`(~(rol fe 5) 3 2 'dfgh')
275− 'ghdf'
276− > `@t`(~(rol fe 5) 3 0 'dfgh')
277− 'dfgh'
278
221
279−------------------------------------------------------------------------
222+ > (~(inv fo 78) 255)
223+ 67
280
224
281−### `++ror`
225+ > (~(inv fo 70) 255)
226+ 67
282
227
283−Roll right
228+***
229+### `++pro`
284
230
285−Roll `d` to the right by `c` `b`-sized blocks.
231+Multiplication
286
232
287−Accepts
233+Produces the multiplication of `b` and `c` modulo `a`.
288−-------
289
234
290−`a` is a `++bloq`.
235+#### Accepts
291
236
292−`b` is a `++bloq`.
237+`a` is an atom.
293
238
294−`c` is an atom.
239+`b` is an atom.
295
240
296−`d` is an atom.
241+`c` is an atom.
297
242
298−Produces
243+#### Produces
299−--------
300
244
301An atom.
245An atom.
302
246
303−Source
247+#### Source
304−------
305−
306− ++ ror |= {b/bloq c/@ d/@} ^- @ :: roll right
307− =+ e=(sit d)
308− =+ f=(bex (sub a b))
309− =+ g=(mod c f)
310− (sit (con (rsh b g e) (lsh b (sub f g) e)))
311
248
249+ ++ pro
250+ |= [b=@ c=@]
251+ (sit (mul b c))
312
252
313−Examples
253+#### Examples
314−--------
315
254
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')
323− 'hijk'
324− > `@t`(~(rol fe 5) 3 1 'hijk')
325− 'khij'
326− > `@t`(~(rol fe 5) 3 2 'hijk')
327− 'jkhi'
328
257
329−------------------------------------------------------------------------
258+ > (mod 44 3)
259+ 2
330
260
331−### `++sum`
261+***
262+### `++sit`
332
263
333−Sum
264+Modulus
334
265
335−Sum two numbers in this modular field.
266+Produces the remainder of `b` modulo `a`.
336
267
337−Accepts
268+#### Accepts
338−-------
339
269
340−`a` is a `++bloq`.
270+`a` is an atom.
341
271
342`b` is an atom.
272`b` is an atom.
343
273
344−`c` is an atom.
345−
346−Produces
274+#### Produces
347−--------
348
275
349An atom.
276An atom.
350
277
351−Source
278+#### Source
352−------
353
279
354− ++ sum |=([b/@ c/@] (sit (add b c))) :: wrapping add
280+ ++ sit
281+ |= b=@
282+ (mod b a)
355
283
356−Examples
284+#### Examples
357−--------
358
285
359− > (~(sum fe 3) 10 250)
286+ > (~(sit fo 3) 14)
360− 4
287+ 2
361− > (~(sum fe 0) 0 1)
362− 1
363− > (~(sum fe 0) 0 2)
364− 0
365− > (~(sum fe 2) 14 2)
366− 0
367− > (~(sum fe 2) 14 3)
368− 1
369− > (~(sum fe 4) 10.000 256)
370− 10.256
371− > (~(sum fe 4) 10.000 100.000)
372− 44.464
373
288
374−### `++sit`
289+***
290+### `++sum`
375
291
376−Atom in mod block representation
292+Modular sum
377
293
378−Produce an atom in the current modular block representation.
294+Produces the remainder of `(b + c) mod a`.
379
295
380−Accepts
296+#### Accepts
381−-------
382
297
383−`a` is a `++bloq`.
298+`a` is an atom.
384
299
385`b` is an atom.
300`b` is an atom.
386
301
302+`c` is an atom.
303+
387−Produces
304+#### Produces
388−--------
389
305
390An atom.
306An atom.
391
307
392−Source
308+#### Source
393−------
394
309
395− ++ sit |=(b/@ (end a 1 b)) :: enforce modulo
310+ ++ sum
311+ |= [b=@ c=@]
312+ (sit (add b c))
313+ --
396
314
397−Examples
315+#### Examples
398−--------
399−
400− > (~(sit fe 3) 255)
401− 255
402− > (~(sit fe 3) 256)
403− 0
404− > (~(sit fe 3) 257)
405− 1
406− > (~(sit fe 2) 257)
407− 1
408− > (~(sit fe 2) 10.000)
409− 0
410− > (~(sit fe 2) 100)
411− 4
412− > (~(sit fe 2) 19)
413− 3
414− > (~(sit fe 2) 17)
415− 1
416− > (~(sit fe 0) 17)
417− 1
418− > (~(sit fe 0) 0)
419− 0
420− > (~(sit fe 0) 1)
421− 1
422
316
317+ > (~(sum fo 3) 14 3)
318+ 2
423
319
320+ > (mod 17 3)
321+ 2
424
322
425***
323***
426### `++si`
324### `++si`
427
325
428− ++ si !: :: signed integer
326+Signed integer
327+
328+Container core for signed integer functions.
329+
330+#### Source
331+
332+ ++ si
333+ ^?
429 |%
334 |%
430
335
431−Container core for the signed integer functions.
336+#### Discussion
337+
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.
432
341
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:
434
345
346+ > `@`--4
347+ 8
348+ > `@s`8
349+ --4
350+
351+ > `@`-4
352+ 7
353+ > `@s`7
354+ -4
355+
356+***
435−### `++abs`
357+### `++abs:si`
436
358
437Absolute value
359Absolute value
438
360
439−Produces the absolute value of a signed integer.
361+Produces the absolute value of signed integer `a`.
440
362
441−Accepts
363+#### Accepts
442−-------
443
364
444−`a` is a signed integer, `@s`.
365+`a` is a signed integer.
445
366
446−Produces
367+#### Produces
447−--------
448
368
449An atom.
369An atom.
450
370
451−Source
371+### Source
452−------
453−
454− ++ abs |=(a/@s (add (end 0 1 a) (rsh 0 1 a))) :: absolute value
455
372
456−Examples
373+ ++ abs |=(a=@s (add (end 0 1 a) (rsh 0 1 a)))
457−--------
458
374
459− > (abs:si -2)
375+#### Examples
460− 2
461− > (abs:si -10.000)
462− 10.000
463− > (abs:si --2)
464− 2
465
376
466−------------------------------------------------------------------------
377+ > (abs:si -11)
378+ 11
467
379
468−### `++dif`
380+ > (abs:si --520)
381+ 520
469
382
470−Difference
383+***
384+### `++dif:si`
471
385
472−Produces the difference between two signed integers `b` and `c`.
386+Subtraction
473
387
474−Accepts
388+Produces the difference of `a` minus `b`.
475−-------
476
389
477−`a` is a signed integer, `@s`.
390+#### Accepts
478
391
479−`b` is a signed integer, `@s`.
392+`a` is a signed integer.
480
393
481−Produces
394+`b` is a signed integer.
482−--------
483
395
484−A `@s`.
396+#### Produces
485
397
398+A signed integer.
486
399
487−Source
400+#### Source
488−------
489
401
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)))
492
404
493−Examples
405+#### Examples
494−--------
495
406
496− > (dif:si --10 -7)
407+ > (dif:si --3 -2)
497− --17
408+ --5
498− > (dif:si --10 --7)
499− --3
500− > (dif:si `@s`0 --7)
501− -7
502− > (dif:si `@s`0 `@s`7)
503− --4
504
409
505−------------------------------------------------------------------------
410+ > (dif:si -3 --2)
411+ -5
506
412
413+***
507−### `++dul`
414+### `++dul:si`
508
415
509Modulus
416Modulus
510
417
511−Produces the modulus of two signed integers.
418+Produces the remainder of `b` modulo `a`.
512
419
513−Accepts
420+#### Examples
514−-------
515
421
516−`a` is a signed integer, `@s`.
422+`a` is a signed integer.
517
423
518`b` is an atom.
424`b` is an atom.
519
425
520−Produces
426+#### Produces
521−--------
522
427
523−An atom.
428+An atom.
524
429
525−Source
430+#### Source
526−------
527
431
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)))
530
434
531−Examples
435+#### Examples
532−--------
533
436
534− > (dul:si --9 3)
535− 0
536− > (dul:si --9 4)
437+ > `@s`(dul:si -1 --5)
537− 1
438+ -5
538− > (dul:si --9 5)
439+
539− 4
440+ > `@`--5
540− > (dul:si --9 6)
541− 3
542− > (dul:si --90 --10)
543 10
441 10
442+ > `@s`(dul:si -1 10)
443+ -5
544
444
545−------------------------------------------------------------------------
445+ > `@s`(dul:si -11 -61)
446+ --55
546
447
448+
449+***
547−### `++fra`
450+#### `++fra:si`
548
451
549Divide
452Divide
550
453
551−Produces the quotient of two signed integers.
454+Produces the quotient of `b` divided by `c`.
552
455
553−Accepts
456+#### Accepts
554−-------
555
457
556−`a` is a signed integer, `@s`.
458+`a` is a signed integer.
557
459
558−`b` is a signed integer, `@s`.
460+`b` is a signed integer.
559
461
560−Produces
462+#### Produces
561−--------
562
463
563−An atom.
464+A signed atom.
564
465
565−Source
466+#### Source
566−------
567
467
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)))
570
470
571−Examples
471+#### Examples
572−--------
573
472
574− > (fra:si --10 -2)
473+ > (fra:si -1 -1)
474+ --1
475+
476+ > (fra:si -11 --2)
575 -5
477 -5
576− > (fra:si -20 -5)
577− --4
578
478
579−------------------------------------------------------------------------
479+ > (fra:si -0 -1)
480+ --0
481+
580
482
483+***
581### `++new`
484### `++new`
582
485
583−Signed integer
486+Atom to @s
584
487
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 `-`.
586
491
587−Accepts
492+#### Accepts
588−-------
589
493
590−`a` is a loobean.
494+`a` is a flag.
591
495
592`b` is an atom.
496`b` is an atom.
593
497
594−Produces
498+#### Produces
595−--------
596
499
597−A `@s`
500+A signed integer.
598
501
599−Source
502+#### Source
600−------
601
503
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)))))
604
506
605−Examples
507+#### Examples
606−--------
607
508
608− > (new:si [& 10])
509+ > (new:si | 2)
609− --10
510+ -2
610− > (new:si [| 10])
611− -10
612− > (new:si [%.y 7])
613− --7
614
511
615−------------------------------------------------------------------------
512+ > (new:si & 2)
513+ --2
616
514
617−### `++old`
515+ > (new:si & -2)
516+ --3
618
517
619−sign value
518+ > (new:si & --2)
519+ --4
620
520
621−Produces the cell `[sign value]` representations of a signed integer.
521+***
522+### `++old:si`
622
523
623−Accepts
524+Sign and absolute value
624−-------
625
525
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`.
627
528
628−Produces
529+#### Accepts
629−--------
630
530
631−A cell of a boolean and an atom.
531+`a` is a signed integer.
632
532
633−Source
533+#### Produces
634−------
635
534
636− ++ old |=(a/@s [(syn a) (abs a)]) :: [sign value]
535+A cell composed of a `term` and an atom.
637
536
638−Examples
537+#### Source
639−--------
640
538
641− > (old:si 7)
539+ ++ old |=(a=@s [(syn a) (abs a)])
642− ! type-fail
643− ! exit
644− > (old:si -7)
645− [%.n 7]
646− > (old:si --7)
647− [%.y 7]
648− > (old:si `@s`7)
649− [%.n 4]
650− > (old:si -0)
651− [%.y 0]
652
540
653−------------------------------------------------------------------------
541+#### Examples
654
542
655−### `++pro`
543+ > (old:si -2)
544+ [%.n 2]
656
545
657−Multiply
546+ > (old:si --2)
547+ [%.y 2]
658
548
659−Produces the product of two signed integers.
549+***
550+### `++pro`
660
551
661−Accepts
552+Multiplication
662−-------
663
553
664−`a` is a `@s`.
554+Produces a signed integer by multiplying `a` and `b`.
665
555
666−`b` is a `@s`.
556+#### Accepts
667
557
668−Produces
558+`a` is an unsigned integer.
669−--------
670
559
671−A `@s`.
560+`b` is an unsigned integer.
672
561
673−Source
562+#### Source
674−------
675
563
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)))
678
566
679−Examples
567+#### Examples
680−--------
681
568
682− > (pro:si -4 --2)
569+ > (pro:si -3 -3)
683− -8
570+ --9
684− > (pro:si -4 -2)
685− --8
686− > (pro:si --10.000.000 -10)
687− -100.000.000
688− > (pro:si -1.337 --0)
689− --0
690
571
691−------------------------------------------------------------------------
572+ > (pro:si -3 --3)
573+ -9
692
574
575+***
693−### `++rem`
576+### `++rem:si`
694
577
695Remainder
578Remainder
696
579
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`.
698
581
699−Accepts
582+#### Accepts
700−-------
701
583
702−`a` is a `@s`.
584+`a` is a signed integer.
703
585
704−`b` is a `@s`.
586+`b` is a signed integer.
705
587
706−Produces
588+#### Produces
707−--------
708
589
709−A `@sd`.
590+A signed integer.
710
591
711−Source
592+#### Source
712−------
713
593
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))))
715
595
716−Examples
596+#### Examples
717−--------
718
597
719− > (rem:si -10 -4)
598+ > (rem:si -17 -3)
720 -2
599 -2
600+
721− > (rem:si --10 --4)
601+ > (rem:si --17 -3)
722 --2
602 --2
603+
723− > (rem:si --10 -4)
604+ > (rem:si -17 --3)
605+ -2
606+
607+ > (rem:si --17 --3)
724 --2
608 --2
725− > (rem:si --7 --3)
726− --1
727− > (rem:si --0 --10.000)
728− --0
729
609
730−------------------------------------------------------------------------
731
610
732−### `++sum`
733
611
734−Add
612+***
613+### `++sum:si`
614+
615+Addition
735
616
736−Sum two signed integers.
617+Produces an atom by adding `a` and `b`.
737
618
738−Accepts
619+#### Accepts
739−-------
740
620
741−`b` is a `@s`.
621+`a` is a signed integer.
742
622
743−`c` is a signed integer `@s`.
623+`b` is a signed integer.
744
624
745−Produces
625+#### Produces
746−--------
747
626
748−A `@s`.
627+A signed integer.
749
628
750−Source
629+#### Source
751−------
752
630
753− ++ sum |= [a/@s b/@s] :: addition
631+ ++ sum |= [a=@s b=@s]
754− ~| %si-sum
755 =+ [c=(old a) d=(old b)]
632 =+ [c=(old a) d=(old b)]
756 ?: -.c
633 ?: -.c
757 ?: -.d
634 ?: -.d
@@ -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))
767
644
768−Examples
645+#### Examples
769−--------
770
646
771− > (sum:si --10 --10)
647+ > (sum:si -11 --2)
772− --20
648+ -9
773− > (sum:si --10 -0)
774− --10
775− > (sum:si -10 -7)
776− -17
777− > (sum:si -10 --7)
778− -3
779
649
780−------------------------------------------------------------------------
650+ > (sum:si --2 --2)
651+ --4
781
652
653+***
782−### `++sun`
654+#### `++sun:si`
783
655
784−Signed from unsigned
656+@u to @s
785
657
786−Produces a signed integer from an unsigned integer.
658+Multiplies the unsigned integer `a` by two, producing an atom.
787
659
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.
790
661
791−Accepts
662+`a` is an unsigned integer.
792−-------
793
663
794−`a` is a `@u`.
664+#### Produces
795
665
796−Produces
666+An atom.
797−--------
798
667
799−A `@s`.
668+#### Source
800
669
801−Source
670+ ++ sun |=(a=@u (mul 2 a))
802−------
803
671
804− ++ sun |=(a/@u (mul 2 a)) :: @u to @s
672+#### Examples
805
673
806−Examples
674+ > (mul 2 90)
807−--------
675+ 180
808
676
809− > `@s`10
677+ > (mul 2 --90)
810− --5
678+ 360
811− > (sun:si 10)
679+ > `@u`--90
812− 20
680+ 180
813− > `@s`(sun:si 10)
814− --10
815− > `@sd`(sun:si 10)
816− --10
817− > `@sd`(sun:si 12.345)
818− --12.345
819
681
820−------------------------------------------------------------------------
682+ > (mul 2 --89)
683+ 356
684+ > `@u`--89
685+ 178
821
686
822−### `++syn`
687+ > (mul 2 -89)
688+ 354
689+ > `@u`-89
690+ 177
823
691
692+***
693+### `++syn:si`
694+
824−Sign
695+Sign test
825
696
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.
827
699
828−Accepts
700+#### Accepts
829−-------
830
701
831−`a` is a `@s`.
702+`a` is a signed integer.
832
703
833−Produces
704+#### Produces
834−--------
835
705
836−A boolean.
706+An `term`.
837
707
838−Source
708+#### Source
839−------
840
709
841− ++ syn |=(a/@s =(0 (end 0 1 a))) :: sign test
710+ ++ syn |=(a=@s =(0 (end 0 1 a)))
842
711
843−Examples
712+#### Examples
844−--------
845
713
846 > (syn:si -2)
714 > (syn:si -2)
847 %.n
715 %.n
716+
848 > (syn:si --2)
717 > (syn:si --2)
849 %.y
718 %.y
850− > (syn:si -0)
851− %.y
852
719
853−------------------------------------------------------------------------
854
720
721+***
855−### `++cmp`
722+### `++cmp:si`
856
723
857Compare
724Compare
858
725
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.
860
729
861−Accepts
730+#### Accepts
862−-------
863
731
864−`b` is a `@s`.
732+`a` is a signed integer.
865
733
866−`c` is a `@s`.
734+`b` is a signed integer.
867
735
868−Produces
736+#### Produces
869−--------
870
737
871−A `@s`.
738+A signed integer.
872
739
873−Source
740+#### Source
874−------
875
741
876− ++ cmp |= [a/@s b/@s] :: compare
742+ ++ cmp |= [a=@s b=@s]
877 ^- @s
743 ^- @s
878 ?: =(a b)
744 ?: =(a b)
879 --0
745 --0
@@ -883,65 +749,28 @@ Source
883 --1
749 --1
884 -1
750 -1
885 --1
751 --1
886− ?: (syn b)
752+ ?: (syn b)
887− -1
753+ -1
888− ?: (gth a b)
754+ ?: (gth a b)
889− -1
755+ -1
890− --1
756+ --1
757+ --
891
758
892−Examples
759+#### Examples
893−--------
894
760
895− > (cmp:si --10 --10)
761+ > (cmp:si -2 --1)
896− --0
897− > (cmp:si --10 -0)
898− --1
899− > (cmp:si -10 -7)
900− -1
901− > (cmp:si -10 --7)
902 -1
762 -1
903
763
764+ > (cmp:si -2 --1)
765+ -1
904
766
905−***
906−### `++egcd`
907−
908−GCD
909−
910− ++ egcd !: :: schneier's egcd
911− |= {a/@ b/@}
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}
916− ?: =(--0 c)
917− [(abs d) d.u d.v]
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)))
920− :: ==
921− =+ q=(fra d c)
922− %= $
923− c (dif d (pro q c))
924− d c
925− u [(dif d.u (pro q c.u)) c.u]
926− v [(dif d.v (pro q c.v)) c.v]
927− ==
928− ::
929−
930−
931−Greatest common denominator
932−
933− > (egcd 20 15)
934− [d=5 u=2 v=1]
935− > (egcd 24 16)
936− [d=8 u=2 v=1]
937− > (egcd 7 5)
938− [d=1 u=3 v=6]
939− > (egcd (shaf ~ %ham) (shaf ~ %sam))
940− [ d=1
768+ --1
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
943− ]
944
769
770+ > (cmp:si --2 --2)
771+ --0
945
772
773+ > (cmp:si --2 --5)
774+ -1
946
775
947***
776***