Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
| Download
GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it
Project: cocalc-sagemath-dev-slelievre
Views: 4183461[1X17 [33X[0;0YRational Numbers[133X[101X23[33X[0;0YThe [13Xrationals[113X form a very important field. On the one hand it is the4quotient field of the integers (see chapter [14X14[114X). On the other hand it is the5prime field of the fields of characteristic zero (see chapter [14X60[114X).[133X67[33X[0;0YThe former comment suggests the representation actually used. A rational is8represented as a pair of integers, called [13Xnumerator[113X and [13Xdenominator[113X.9Numerator and denominator are [13Xreduced[113X, i.e., their greatest common divisor10is 1. If the denominator is 1, the rational is in fact an integer and is11represented as such. The numerator holds the sign of the rational, thus the12denominator is always positive.[133X1314[33X[0;0YBecause the underlying integer arithmetic can compute with arbitrary size15integers, the rational arithmetic is always exact, even for rationals whose16numerators and denominators have thousands of digits.[133X1718[4X[32X Example [32X[104X19[4X[25Xgap>[125X [27X2/3;[127X[104X20[4X[28X2/3[128X[104X21[4X[25Xgap>[125X [27X66/123; # numerator and denominator are made relatively prime[127X[104X22[4X[28X22/41[128X[104X23[4X[25Xgap>[125X [27X17/-13; # the numerator carries the sign;[127X[104X24[4X[28X-17/13[128X[104X25[4X[25Xgap>[125X [27X121/11; # rationals with denominator 1 (when canceled) are integers[127X[104X26[4X[28X11[128X[104X27[4X[32X[104X282930[1X17.1 [33X[0;0YRationals: Global Variables[133X[101X3132[1X17.1-1 Rationals[101X3334[33X[1;0Y[29X[2XRationals[102X[32X global variable[133X35[33X[1;0Y[29X[2XIsRationals[102X( [3Xobj[103X ) [32X filter[133X3637[33X[0;0Y[2XRationals[102X is the field [22Xℚ[122X of rational integers, as a set of cyclotomic38numbers, see Chapter [14X18[114X for basic operations, Functions for the field39[2XRationals[102X can be found in the chapters [14X58[114X and [14X60[114X.[133X4041[33X[0;0Y[2XIsRationals[102X returns [9Xtrue[109X for a prime field that consists of cyclotomic42numbers –for example the [5XGAP[105X object [2XRationals[102X– and [9Xfalse[109X for all other [5XGAP[105X43objects.[133X4445[4X[32X Example [32X[104X46[4X[25Xgap>[125X [27XSize( Rationals ); 2/3 in Rationals;[127X[104X47[4X[28Xinfinity[128X[104X48[4X[28Xtrue[128X[104X49[4X[32X[104X505152[1X17.2 [33X[0;0YElementary Operations for Rationals[133X[101X5354[1X17.2-1 IsRat[101X5556[33X[1;0Y[29X[2XIsRat[102X( [3Xobj[103X ) [32X Category[133X5758[33X[0;0YEvery rational number lies in the category [2XIsRat[102X, which is a subcategory of59[2XIsCyc[102X ([14X18.1-3[114X).[133X6061[4X[32X Example [32X[104X62[4X[25Xgap>[125X [27XIsRat( 2/3 );[127X[104X63[4X[28Xtrue[128X[104X64[4X[25Xgap>[125X [27XIsRat( 17/-13 );[127X[104X65[4X[28Xtrue[128X[104X66[4X[25Xgap>[125X [27XIsRat( 11 );[127X[104X67[4X[28Xtrue[128X[104X68[4X[25Xgap>[125X [27XIsRat( IsRat ); # `IsRat' is a function, not a rational[127X[104X69[4X[28Xfalse[128X[104X70[4X[32X[104X7172[1X17.2-2 IsPosRat[101X7374[33X[1;0Y[29X[2XIsPosRat[102X( [3Xobj[103X ) [32X Category[133X7576[33X[0;0YEvery positive rational number lies in the category [2XIsPosRat[102X.[133X7778[1X17.2-3 IsNegRat[101X7980[33X[1;0Y[29X[2XIsNegRat[102X( [3Xobj[103X ) [32X Category[133X8182[33X[0;0YEvery negative rational number lies in the category [2XIsNegRat[102X.[133X8384[1X17.2-4 NumeratorRat[101X8586[33X[1;0Y[29X[2XNumeratorRat[102X( [3Xrat[103X ) [32X function[133X8788[33X[0;0Y[2XNumeratorRat[102X returns the numerator of the rational [3Xrat[103X. Because the89numerator holds the sign of the rational it may be any integer. Integers are90rationals with denominator [22X1[122X, thus [2XNumeratorRat[102X is the identity function for91integers.[133X9293[4X[32X Example [32X[104X94[4X[25Xgap>[125X [27XNumeratorRat( 2/3 );[127X[104X95[4X[28X2[128X[104X96[4X[25Xgap>[125X [27X# numerator and denominator are made relatively prime:[127X[104X97[4X[25Xgap>[125X [27XNumeratorRat( 66/123 );[127X[104X98[4X[28X22[128X[104X99[4X[25Xgap>[125X [27XNumeratorRat( 17/-13 ); # numerator holds the sign of the rational[127X[104X100[4X[28X-17[128X[104X101[4X[25Xgap>[125X [27XNumeratorRat( 11 ); # integers are rationals with denominator 1[127X[104X102[4X[28X11[128X[104X103[4X[32X[104X104105[1X17.2-5 DenominatorRat[101X106107[33X[1;0Y[29X[2XDenominatorRat[102X( [3Xrat[103X ) [32X function[133X108109[33X[0;0Y[2XDenominatorRat[102X returns the denominator of the rational [3Xrat[103X. Because the110numerator holds the sign of the rational the denominator is always a111positive integer. Integers are rationals with the denominator 1, thus112[2XDenominatorRat[102X returns 1 for integers.[133X113114[4X[32X Example [32X[104X115[4X[25Xgap>[125X [27XDenominatorRat( 2/3 );[127X[104X116[4X[28X3[128X[104X117[4X[25Xgap>[125X [27X# numerator and denominator are made relatively prime:[127X[104X118[4X[25Xgap>[125X [27XDenominatorRat( 66/123 );[127X[104X119[4X[28X41[128X[104X120[4X[25Xgap>[125X [27X# the denominator holds the sign of the rational:[127X[104X121[4X[25Xgap>[125X [27XDenominatorRat( 17/-13 );[127X[104X122[4X[28X13[128X[104X123[4X[25Xgap>[125X [27XDenominatorRat( 11 ); # integers are rationals with denominator 1[127X[104X124[4X[28X1[128X[104X125[4X[32X[104X126127[1X17.2-6 Rat[101X128129[33X[1;0Y[29X[2XRat[102X( [3Xelm[103X ) [32X attribute[133X130131[33X[0;0Y[2XRat[102X returns a rational number [3Xrat[103X whose meaning depends on the type of [3Xelm[103X.[133X132133[33X[0;0YIf [3Xelm[103X is a string consisting of digits [10X'0'[110X, [10X'1'[110X, [22X...[122X, [10X'9'[110X and [10X'-'[110X (at the134first position), [10X'/'[110X and the decimal dot [10X'.'[110X then [3Xrat[103X is the rational135described by this string. The operation [2XString[102X ([14X27.7-6[114X) can be used to136compute a string for rational numbers, in fact for all cyclotomics.[133X137138[4X[32X Example [32X[104X139[4X[25Xgap>[125X [27XRat( "1/2" ); Rat( "35/14" ); Rat( "35/-27" ); Rat( "3.14159" );[127X[104X140[4X[28X1/2[128X[104X141[4X[28X5/2[128X[104X142[4X[28X-35/27[128X[104X143[4X[28X314159/100000[128X[104X144[4X[32X[104X145146[1X17.2-7 Random[101X147148[33X[1;0Y[29X[2XRandom[102X( [3XRationals[103X ) [32X operation[133X149150[33X[0;0Y[2XRandom[102X for rationals returns pseudo random rationals which are the quotient151of two random integers. See the description of [2XRandom[102X ([14X14.2-12[114X) for details.152(Also see [2XRandom[102X ([14X30.7-1[114X).)[133X153154155156