Add math.algebra module with some useful words.

- ext-euclidian implements the extended Euclidian algorithm
  - ring-inverse computes an inverse in a Z/nZ ring
  - chinese-remainder solves a multi-constraints modular equation
db4
Samuel Tardieu 2007-12-26 22:42:33 +01:00
parent 9f0fb715ab
commit c4529fb557
5 changed files with 55 additions and 0 deletions

View File

@ -0,0 +1,14 @@
USING: help.markup help.syntax ;
IN: math.algebra
HELP: ext-euclidian
{ $values { "a" "a positive integer" } { "b" "a positive integer" } { "gcd" "a positive integer" } { "u" "an integer" } { "v" "an integer" } }
{ $description "Compute the greatest common divisor " { $snippet "gcd" } " of integers " { $snippet "a" } " and " { $snippet "b" } " using the extended Euclidian algorithm. In addition, this word also computes two other values " { $snippet "u" } " and " { $snippet "v" } " such that " { $snippet "a*u + b*v = gcd" } "." } ;
HELP: ring-inverse
{ $values { "a" "a positive integer" } { "b" "a positive integer" } { "i" "a positive integer" } }
{ $description "If " { $snippet "a" } " and " { $snippet "b" } " are coprime, " { $snippet "i" } " is the smallest positive integer such as " { $snippet "a*i = 1" } " in ring " { $snippet "Z/bZ" } "." } ;
HELP: chinese-remainder
{ $values { "aseq" "a sequence of integers" } { "nseq" "a sequence of positive integers" } { "x" "an integer" } }
{ $description "If " { $snippet "nseq" } " integers are pairwise coprimes, " { $snippet "x" } " is the smallest positive integer congruent to each element in " { $snippet "aseq" } " modulo the corresponding element in " { $snippet "nseq" } "." } ;

View File

@ -0,0 +1,5 @@
USING: math.algebra tools.test ;
{ 2 5 -2 } [ 10 24 ext-euclidian ] unit-test
{ 17 } [ 19 23 ring-inverse ] unit-test
{ 11 } [ { 2 3 1 } { 3 4 5 } chinese-remainder ] unit-test

View File

@ -0,0 +1,34 @@
! Copyright (c) 2007 Samuel Tardieu
! See http://factorcode.org/license.txt for BSD license.
USING: kernel math math.ranges namespaces sequences vars math.algebra ;
IN: math.algebra
<PRIVATE
VARS: r-1 u-1 v-1 r u v ;
: init ( a b -- )
>r >r-1 0 >u 1 >u-1 1 >v 0 >v-1 ;
: advance ( r u v -- )
v> >v-1 >v u> >u-1 >u r> >r-1 >r ; inline
: step ( -- )
r-1> r> 2dup /mod drop [ * - ] keep u-1> over u> * - v-1> rot v> * -
advance ;
PRIVATE>
! Extended Euclidian: http://en.wikipedia.org/wiki/Extended_Euclidean_algorithm
: ext-euclidian ( a b -- gcd u v )
[ init [ r> 0 > ] [ step ] [ ] while r-1> u-1> v-1> ] with-scope ; foldable
! Inverse a in ring Z/bZ
: ring-inverse ( a b -- i )
[ ext-euclidian drop nip ] keep rem ; foldable
! Chinese remainder: http://en.wikipedia.org/wiki/Chinese_remainder_theorem
: chinese-remainder ( aseq nseq -- x )
dup product
[ [ over / [ ext-euclidian ] keep * 2nip * ] curry 2map sum ] keep rem ;
foldable

View File

@ -0,0 +1 @@
Samuel Tardieu

View File

@ -0,0 +1 @@
Various algebra-related words