+, -, *, /, ^
,
Div, Mod
,
Gcd
,
Lcm
,
<<, >>
,
FromBase, ToBase
,
Precision
,
GetPrecision
,
N
,
Rationalize
,
IsPrime
,
IsComposite
,
IsCoprime
,
IsSquareFree
,
IsPrimePower
,
NextPrime
,
IsTwinPrime
,
IsIrregularPrime
,
IsCarmichaelNumber
,
Factors
,
IsAmicablePair
,
Factor
,
Divisors
,
DivisorsSum
,
ProperDivisors
,
ProperDivisorsSum
,
Moebius
,
CatalanNumber
,
FermatNumber
,
HarmonicNumber
,
StirlingNumber1
,
StirlingNumber2
,
IntLog
,
IntNthRoot
,
PAdicExpand
,
ContFrac
,
ContFracList, ContFracEval
,
GuessRational, NearRational
,
Decimal
,
TruncRadian
,
Floor
,
Ceil
,
Round
,
Pslq
.
Arithmetic and other operations on numbers
Besides the usual arithmetical operations,
Yacas defines some more advanced operations on
numbers. Many of them also work on polynomials.
+, -, *, /, ^
|
arithmetic operations |
Div, Mod
|
division with remainder |
Gcd
|
greatest common divisor |
Lcm
|
least common multiple |
<<, >>
|
shift operators |
FromBase, ToBase
|
conversion from/to non-decimal base |
Precision
|
set the precision |
GetPrecision
|
get the current precision |
N
|
compute numerical approximation |
Rationalize
|
convert floating point numbers to fractions |
IsPrime
|
test for a prime number |
IsComposite
|
test for a composite number |
IsCoprime
|
test if integers are coprime |
IsSquareFree
|
test for a square free number |
IsPrimePower
|
test for a power of a prime number |
NextPrime
|
generate a prime following a number |
IsTwinPrime
|
test for a twin prime |
IsIrregularPrime
|
test for an irregular prime |
IsCarmichaelNumber
|
test for a Carmichael number |
Factors
|
factorization |
IsAmicablePair
|
test for a pair of amicable numbers |
Factor
|
factorization, in pretty form |
Divisors
|
number of divisors |
DivisorsSum
|
the sum of divisors |
ProperDivisors
|
the number of proper divisors |
ProperDivisorsSum
|
the sum of proper divisors |
Moebius
|
the Moebius function |
CatalanNumber
|
return the nth Catalan Number |
FermatNumber
|
return the nth Fermat Number |
HarmonicNumber
|
return the nth Harmonic Number |
StirlingNumber1
|
return the n,mth Stirling Number of the first kind |
StirlingNumber2
|
return the n,mth Stirling Number of the second kind |
IntLog
|
integer part of logarithm |
IntNthRoot
|
integer part of n-th root |
PAdicExpand
|
p-adic expansion |
ContFrac
|
continued fraction expansion |
ContFracList, ContFracEval
|
manipulate continued fractions |
GuessRational, NearRational
|
find optimal rational approximations |
Decimal
|
decimal representation of a rational |
TruncRadian
|
remainder modulo 2*Pi |
Floor
|
round a number downwards |
Ceil
|
round a number upwards |
Round
|
round a number to the nearest integer |
Pslq
|
search for integer relations between reals |
+, -, *, /, ^ -- arithmetic operations
Standard library
Calling format:
x+y (precedence 6)
+x
x-y (precedence 5)
-x
x*y (precedence 3)
x/y (precedence 3)
x^y (precedence 2)
|
Parameters:
x and y -- objects for which arithmetic operations are defined
Description:
These are the basic arithmetic operations. They can work on integers,
rational numbers, complex numbers, vectors, matrices and lists.
These operators are implemented in the standard math library (as opposed
to being built-in). This means that they can be extended by the user.
Examples:
In> 2+3
Out> 5;
In> 2*3
Out> 6;
|
Div, Mod -- division with remainder
Standard library
Calling format:
Parameters:
x, y -- integers or univariate polynomials
Description:
Div performs integer division and Mod returns the remainder after division. Div and
Mod are also defined for polynomials.
If Div(x,y) returns "a" and Mod(x,y) equals "b", then these numbers satisfy x=a*y+b and 0<=b<y.
Examples:
In> Div(5,3)
Out> 1;
In> Mod(5,3)
Out> 2;
|
See also:
Gcd
,
Lcm
.
Gcd -- greatest common divisor
Standard library
Calling format:
Parameters:
n, m -- integers or Gaussian integers or univariate polynomials
list -- a list of all integers or all univariate polynomials
Description:
This function returns the greatest common divisor of "n" and "m".
The gcd is the largest number that divides "n" and "m". It is
also known as the highest common factor (hcf). The library code calls
MathGcd, which is an internal function. This
function implements the "binary Euclidean algorithm" for determining the
greatest common divisor:
Routine for calculating Gcd(n,m)
- if n=m then return n
- if both n and m are even then return 2*Gcd(n/2,m/2)
- if exactly one of n or m (say n) is even then return Gcd(n/2,m)
- if both n and m are odd and, say, n>m then return Gcd((n-m)/2,m)
This is a rather fast algorithm on computers that can efficiently shift
integers. When factoring Gaussian integers, a slower recursive algorithm is used.
If the second calling form is used, Gcd will
return the greatest common divisor of all the integers or polynomials
in "list". It uses the identity
Gcd(a,b,c)=Gcd(Gcd(a,b),c).
Examples:
In> Gcd(55,10)
Out> 5;
In> Gcd({60,24,120})
Out> 12;
In> Gcd( 7300 + 12*I, 2700 + 100*I)
Out> Complex(-4,4);
|
See also:
Lcm
.
Lcm -- least common multiple
Standard library
Calling format:
Parameters:
n, m -- integers or univariate polynomials
list -- list of integers
Description:
This command returns the least common multiple of "n" and "m" or all of
the integers in the list list.
The least common multiple of two numbers "n" and "m" is the lowest
number which is an integer multiple of both "n" and "m".
It is calculated with the formula
Lcm(n,m)=Div(n*m,Gcd(n,m)).
This means it also works on polynomials, since Div, Gcd and multiplication are also defined for
them.
Examples:
In> Lcm(60,24)
Out> 120;
In> Lcm({3,5,7,9})
Out> 315;
|
See also:
Gcd
.
<<, >> -- shift operators
Standard library
Calling format:
Parameters:
n, m -- integers
Description:
These operators shift integers to the left or to the right.
They are similar to the C shift operators. These are sign-extended
shifts, so they act as multiplication or division by powers of 2.
Examples:
In> 1 << 10
Out> 1024;
In> -1024 >> 10
Out> -1;
|
FromBase, ToBase -- conversion from/to non-decimal base
Internal function
Calling format:
FromBase(base,number)
ToBase(base,number)
|
Parameters:
base -- integer, base to write the numbers in
number -- integer, number to write out in the base representation
Description:
FromBase converts "number", written in base
"base", to base 10. ToBase converts "number",
written in base 10, to base "base".
These functions use the p-adic expansion capabilities of the built-in
arbitrary precision math libraries.
Examples:
In> FromBase(2,111111)
Out> 63;
In> ToBase(16,255)
Out> ff;
|
The first command writes the binary number 111111
in decimal base. The second command converts 255
(in decimal base) to hexadecimal base.
See also:
PAdicExpand
.
Precision -- set the precision
Internal function
Calling format:
Parameters:
n -- integer, new precision
Description:
This command sets the number of binary digits to be used in
calculations. All subsequent floating point operations will allow for
at least n digits after the decimal point.
When the precision is changed, all variables containing previosly calculated values
remain unchanged.
The Precision function only makes all further calculations proceed with a different precision.
Examples:
In> Precision(10)
Out> True;
In> N(Sin(1))
Out> 0.8414709848;
In> Precision(20)
Out> True;
In> x:=N(Sin(1))
Out> 0.84147098480789650665;
In> GetPrecision()
Out> 20;
In> [ Precision(10); x; ]
Out> 0.84147098480789650665;
In> x+0
Out> 0.8414709848;
|
See also:
GetPrecision
,
N
.
GetPrecision -- get the current precision
Internal function
Calling format:
Description:
This command returns the current precision, as set by Precision.
Examples:
In> GetPrecision();
Out> 10;
In> Precision(20);
Out> True;
In> GetPrecision();
Out> 20;
|
See also:
Precision
,
N
.
N -- compute numerical approximation
Standard library
Calling format:
Parameters:
expr -- expression to evaluate
prec -- integer, precision to use
Description:
This function forces Yacas to give a numerical approximation to the
expression "expr", using "prec" digits if the second calling
sequence is used, and the precision as set by SetPrecision otherwise. This overrides the normal
behaviour, in which expressions are kept in symbolic form (eg. Sqrt(2) instead of 1.41421).
Application of the N operator will make Yacas
calculate floating point representations of functions whenever
possible. In addition, the variable Pi is bound to
the value of Pi calculated at the current precision.
(This value is a "cached constant", so it is not recalculated each time N is used, unless the precision is increased.)
Examples:
In> 1/2
Out> 1/2;
In> N(1/2)
Out> 0.5;
In> Sin(1)
Out> Sin(1);
In> N(Sin(1),10)
Out> 0.8414709848;
In> Pi
Out> Pi;
In> N(Pi,20)
Out> 3.14159265358979323846;
|
See also:
Precision
,
GetPrecision
,
Pi
,
CachedConstant
.
Rationalize -- convert floating point numbers to fractions
Standard library
Calling format:
Parameters:
expr -- an expression containing real numbers
Description:
This command converts every real number in the expression "expr"
into a rational number. This is useful when a calculation needs to be
done on floating point numbers and the algorithm is unstable.
Converting the floating point numbers to rational numbers will force
calculations to be done with infinite precision (by using rational
numbers as representations).
It does this by finding the smallest integer n such that multiplying
the number with 10^n is an integer. Then it divides by 10^n again,
depending on the internal gcd calculation to reduce the resulting
division of integers.
Examples:
In> {1.2,3.123,4.5}
Out> {1.2,3.123,4.5};
In> Rationalize(%)
Out> {6/5,3123/1000,9/2};
|
See also:
IsRational
.
IsPrime -- test for a prime number
Standard library
Calling format:
Parameters:
n -- integer to test
Description:
This command checks whether "n", which should be a positive integer,
is a prime number. A number is a prime number if it is only divisible
by 1 and itself. As a special case, 1 is not a prime number.
This function essentially checks for all integers between 2 and the
square root of "n" whether they divide "n", and hence may take a
long time for large numbers.
Examples:
In> IsPrime(1)
Out> False;
In> IsPrime(2)
Out> True;
In> IsPrime(10)
Out> False;
In> IsPrime(23)
Out> True;
In> Select("IsPrime", 1 .. 100)
Out> {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,
53,59,61,67,71,73,79,83,89,97};
|
See also:
IsPrimePower
,
Factors
.
IsComposite -- test for a composite number
Standard library
Calling format:
Parameters:
n -- positive integer
Description:
This function is the logical negation of IsPrime, except for the number 1, which is
neither prime nor composite.
Examples:
In> IsComposite(1)
Out> False;
In> IsComposite(7)
Out> False;
In> IsComposite(8)
Out> True;
In> Select(IsComposite,1...20)
Out> {4,6,8,9,10,12,14,15,16,18,20};
|
See also:
IsPrime
.
IsCoprime -- test if integers are coprime
Standard library
Calling format:
IsCoprime(m,n)
IsCoprime(list)
|
Parameters:
m,n -- positive integers
list -- list of positive integers
Description:
This function returns True if the given pair or list of integers are coprime,
also called relatively prime. A pair or list of numbers are coprime if they
share no common factors.
Examples:
In> IsCoprime({3,4,5,8})
Out> False;
In> IsCoprime(15,17)
Out> True;
|
See also:
Prime
.
IsSquareFree -- test for a square free number
Standard library
Calling format:
Parameters:
n -- positive integer
Description:
This function uses the Moebius function to tell if the given number is square free, which
means it has distinct prime factors. If Mobius(n)!=0, then n is square free. All prime
numbers are trivially square free.
Examples:
In> IsSquareFree(37)
Out> True;
In> IsSquareFree(4)
Out> False;
In> IsSquareFree(16)
Out> False;
In> IsSquareFree(18)
Out> False;
|
See also:
Moebius
.
IsPrimePower -- test for a power of a prime number
Standard library
Calling format:
Parameters:
n -- integer to test
Description:
This command tests whether "n", which should be a positive integer,
is a prime power, that is whether it is of the form p^m, with
"p" prime and "m" an integer.
This function essentially checks for all integers between 2 and the
square root of "n" for the largest divisor, and then tests whether
"n" is a power of this divisor. So it will take a long time for
large numbers.
Examples:
In> IsPrimePower(9)
Out> True;
In> IsPrimePower(10)
Out> False;
In> Select("IsPrimePower", 1 .. 50)
Out> {2,3,4,5,7,8,9,11,13,16,17,19,23,25,27,
29,31,32,37,41,43,47,49};
|
See also:
IsPrime
,
Factors
.
NextPrime -- generate a prime following a number
Standard library
Calling format:
Parameters:
i -- integer value
Description:
The function finds the smallest prime number that is greater than the given
integer value.
The routine generates "candidate numbers" using the formula n+2*Mod(-n,3)
where n is an odd number (this generates the sequence 5, 7, 11, 13, 17,
19, ...) and IsPrime() to test whether the next candidate number is in
fact prime.
Example:
See also:
IsPrime
.
IsTwinPrime -- test for a twin prime
Standard library
Calling format:
Parameters:
n -- positive integer
Description:
This function returns True if n is a twin prime. By definition, a twin
prime is a prime number n such that n+2 is also a prime number.
Examples:
In> IsTwinPrime(101)
Out> True;
In> IsTwinPrime(7)
Out> False;
In> Select(IsTwinPrime, 1...100)
Out> {3,5,11,17,29,41,59,71};
|
See also:
IsPrime
.
IsIrregularPrime -- test for an irregular prime
Standard library
Calling format:
Parameters:
n -- positive integer
Description:
This function returns True if n is an irregular prime. A prime number n
is irregular if and only if n divides the numerator of a Bernoulli number B[2*i],
where 2*i+1<n. Small irregular primes are quite rare; the only irregular primes under 100
are 37, 59 and 67. Asymptotically, roughly 40% of primes are irregular.
Examples:
In> IsIrregularPrime(5)
Out> False;
In> Select(IsIrregularPrime,1...100)
Out> {37,59,67};
|
See also:
IsPrime
.
IsCarmichaelNumber -- test for a Carmichael number
Standard library
Calling format:
Parameters:
n -- positive integer
Description:
This fuction returns True if n is a Carmichael number, also called an absolute pseudoprime.
They have the property that b^(n-1)%n==1 for all b satisfying Gcd(b,n)==1. These numbers
cannot be proved composite by Fermat's little theorem. Because the previous property is extremely
slow to test, the following equivalent property is tested by Yacas: for all prime factors p[i] of n,
(n-1)%(p[i]-1)==0 and n must be square free. Also, Carmichael numbers must be odd and have
at least three prime factors. Although these numbers are rare (there are only 43 that are less than a million),
it has recently been proven that there are infinitely many of them.
Examples:
In> IsCarmichaelNumber(561)
Out> True;
In> Time(Select(IsCarmichaelNumber,1 .. 10000))
504.19 seconds taken
Out> {561,1105,1729,2465,2821,6601,8911};
|
See also:
IsSquareFree
,
IsComposite
.
Factors -- factorization
Standard library
Calling format:
Parameters:
x -- integer or univariate polynomial
Description:
This function decomposes the integer number x into a product of
numbers. Alternatively, if x is a univariate polynomial, it is
decomposed in irreducible polynomials.
The factorization is returned as a list of pairs. The first member of
each pair is the factor, while the second member denotes the power to
which this factor should be raised. So the factorization
x=p1^n1*...*p9^n9
is returned as {{p1,n1}, ..., {p9,n9}}.
Examples:
In> Factors(24);
Out> {{2,3},{3,1}};
In> Factors(2*x^3 + 3*x^2 - 1);
Out> {{2,1},{x+1,2},{x-1/2,1}};
|
See also:
Factor
,
IsPrime
.
IsAmicablePair -- test for a pair of amicable numbers
Standard library
Calling format:
Parameters:
m, n -- positive integers
Description:
This function tests if a pair of numbers are amicable. A pair of
numbers m, n has this property if the sum of the proper divisors of m is
n and the sum of the proper divisors of n is m.
Examples:
In> IsAmicablePair(200958394875, 209194708485 )
Out> True;
In> IsAmicablePair(220, 284)
Out> True;
|
See also:
ProperDivisorsSum
.
Factor -- factorization, in pretty form
Standard library
Calling format:
Parameters:
x -- integer or univariate polynomial
Description:
This function factorizes "x", similarly to Factors, but
it shows the result in a nicer human readable format.
Examples:
In> PrettyForm(Factor(24));
3
2 * 3
Out> True;
In> PrettyForm(Factor(2*x^3 + 3*x^2 - 1));
2 / 1 \
2 * ( x + 1 ) * | x - - |
\ 2 /
Out> True;
|
See also:
Factors
,
IsPrime
,
PrettyForm
.
Divisors -- number of divisors
Standard library
Calling format:
Parameters:
n -- positive integer
Description:
Divisors returns the number of positive divisors of a number.
A number is prime if and only if it has two divisors, 1 and itself.
Examples:
In> Divisors(180)
Out> 18;
In> Divisors(37)
Out> 2;
|
See also:
DivisorsSum
,
ProperDivisors
,
ProperDivisorsSum
,
Moebius
.
DivisorsSum -- the sum of divisors
Standard library
Calling format:
Parameters:
n -- positive integer
Description:
DivisorsSum returns the sum all numbers that divide it. A number
n is prime if and only if the sum of its divisors are n+1.
Examples:
In> DivisorsSum(180)
Out> 546;
In> DivisorsSum(37)
Out> 38;
|
See also:
DivisorsSum
,
ProperDivisors
,
ProperDivisorsSum
,
Moebius
.
ProperDivisors -- the number of proper divisors
Standard library
Calling format:
Parameters:
n -- positive integer
Description:
ProperDivisors returns the number of proper divisors, i.e Divisors(n)-1,
since n is not counted.
An integer n is prime if and only if it has 1 proper divisor.
Examples:
In> ProperDivisors(180)
Out> 17;
In> ProperDivisors(37)
Out> 1;
|
See also:
DivisorsSum
,
ProperDivisors
,
ProperDivisorsSum
,
Moebius
.
ProperDivisorsSum -- the sum of proper divisors
Standard library
Calling format:
Parameters:
n -- positive integer
Description:
ProperDivisorsSum returns the sum of proper divisors, i.e. ProperDivisors(n)-n,
since n is not counted.
n is prime if and only if ProperDivisorsSum(n)==1.
Examples:
In> ProperDivisorsSum(180)
Out> 366;
In> ProperDivisorsSum(37)
Out> 1;
|
See also:
DivisorsSum
,
ProperDivisors
,
ProperDivisorsSum
,
Moebius
.
Moebius -- the Moebius function
Standard library
Calling format:
Parameters:
n -- positive integer
Description:
The Moebius function is 0 when a prime factor is repeated (which means it
is not square-free) and is (-1)^r if n has r distinct factors. Also,
Moebius(1)==1.
Examples:
In> Moebius(10)
Out> 1;
In> Moebius(11)
Out> -1;
In> Moebius(12)
Out> 0;
In> Moebius(13)
Out> -1;
|
See also:
DivisorsSum
,
ProperDivisors
,
ProperDivisorsSum
,
Moebius
.
CatalanNumber -- return the nth Catalan Number
Standard library
Calling format:
Parameters:
n -- positive integer
Description:
This function returns the n-th Catalan number, defined as Bin(2*n,n)/(n+1).
Examples:
In> CatalanNumber(10)
Out> 16796;
In> CatalanNumber(5)
Out> 42;
|
See also:
Bin
.
FermatNumber -- return the nth Fermat Number
Standard library
Calling format:
Parameters:
n -- positive integer
Description:
This function returns the n-th Fermat number, which is defined as
2^2^n+1.
Examples:
In> FermatNumber(7)
Out> 340282366920938463463374607431768211457;
|
See also:
Factor
.
HarmonicNumber -- return the nth Harmonic Number
Standard library
Calling format:
HarmonicNumber(n)
HarmonicNumber(n,r)
|
Parameters:
n, r -- positive integers
Description:
This function returns the n-th Harmonic number, which is defined
as Sum(k,1,n,1/k). If given a second argument, the Harmonic number
of order r is returned, which is defined as Sum(k,1,n,k^(-r)).
Examples:
In> HarmonicNumber(10)
Out> 7381/2520;
In> HarmonicNumber(15)
Out> 1195757/360360;
In> HarmonicNumber(1)
Out> 1;
In> HarmonicNumber(4,3)
Out> 2035/1728;
|
See also:
Sum
.
StirlingNumber1 -- return the n,mth Stirling Number of the first kind
Standard library
Calling format:
Parameters:
n, m -- positive integers
Description:
This function returns the signed Stirling Number of the first kind.
All Stirling Numbers are integers. If m>n, then StirlingNumber1 returns
0.
Examples:
In> StirlingNumber1(10,5)
Out> -269325;
In> StirlingNumber1(3,6)
Out> 0;
|
See also:
StirlingNumber2
.
StirlingNumber2 -- return the n,mth Stirling Number of the second kind
Standard library
Calling format:
StirlingNumber1(n,m)
Parameters:
n, m -- positive integers
Description:
This function returns the Stirling Number of the second kind.
All Stirling Numbers are positive integers. If m>n, then StirlingNumber2 returns
0.
Examples:
In> StirlingNumber2(3,6)
Out> 0;
In> StirlingNumber2(10,4)
Out> 34105;
|
See also:
StirlingNumber1
.
IntLog -- integer part of logarithm
Standard library
Calling format:
Parameters:
n, base -- positive integers
Description:
IntLog calculates the integer part of the logarithm of n in base base. The algorithm uses only integer math and may be faster than computing
Ln(n)/Ln(base)
with multiple precision floating-point math and rounding off to get the integer part.
This function can also be used to quickly count the digits in a given number.
Examples:
Count the number of bits:
In> IntLog(257^8, 2)
Out> 64;
|
Count the number of decimal digits:
In> IntLog(321^321, 10)
Out> 804;
|
See also:
IntNthRoot
,
Div
,
Mod
,
Ln
.
IntNthRoot -- integer part of n-th root
Standard library
Calling format:
Parameters:
x, n -- positive integers
Description:
IntNthRoot calculates the integer part of the n-th root of x. The algorithm uses only integer math and may be faster than computing x^(1/n) with floating-point and rounding.
This function is used to test numbers for prime powers.
Example:
In> IntNthRoot(65537^111, 37)
Out> 281487861809153;
|
See also:
IntLog
,
MathPower
,
IsPrimePower
.
PAdicExpand -- p-adic expansion
Standard library
Calling format:
Parameters:
n -- number or polynomial to expand
p -- base to expand in
Description:
This command computes the p-adic expansion of "n". In other words,
"n" is expanded in powers of "p". The argument "n" can be either
an integer or a univariate polynomial. The base "p" should be of the
same type.
Examples:
In> PrettyForm(PAdicExpand(1234, 10));
2 3
3 * 10 + 2 * 10 + 10 + 4
Out> True;
In> PrettyForm(PAdicExpand(x^3, x-1));
2 3
3 * ( x - 1 ) + 3 * ( x - 1 ) + ( x - 1 ) + 1
Out> True;
|
See also:
Mod
,
ContFrac
,
FromBase
,
ToBase
.
ContFrac -- continued fraction expansion
Standard library
Calling format:
ContFrac(x)
ContFrac(x, depth)
|
Parameters:
x -- number or polynomial to expand in continued fractions
depth -- integer, maximum required depth of result
Description:
This command returns the continued fraction expansion of x, which
should be either a floating point number or a polynomial. If
depth is not specified, it defaults to 6. The remainder is
denoted by rest.
This is especially useful for polynomials, since series expansions
that converge slowly will typically converge a lot faster if
calculated using a continued fraction expansion.
Examples:
In> PrettyForm(ContFrac(N(Pi)))
1
--------------------------- + 3
1
----------------------- + 7
1
------------------ + 15
1
-------------- + 1
1
-------- + 292
rest + 1
|
Out> True;
In> PrettyForm(ContFrac(x^2+x+1, 3))
x
---------------- + 1
x
1 - ------------
x
-------- + 1
rest + 1
Out> True;
|
See also:
ContFracList
,
NearRational
,
GuessRational
,
PAdicExpand
,
N
.
ContFracList, ContFracEval -- manipulate continued fractions
Standard library
Calling format:
ContFracList(frac)
ContFracList(frac, depth)
ContFracEval(list)
ContFracEval(list, rest)
|
Parameters:
frac -- a number to be expanded
depth -- desired number of terms
list -- a list of coefficients
rest -- expression to put at the end of the continued fraction
Description:
The function ContFracList computes terms of the continued fraction
representation of a rational number frac. It returns a list of terms of length depth. If depth is not specified, it returns all terms.
The function ContFracEval converts a list of coefficients into a continued fraction expression. The optional parameter rest specifies the symbol to put at the end of the expansion. If it is not given, the result is the same as if rest=0.
Examples:
In> A:=ContFracList(33/7 + 0.000001)
Out> {4,1,2,1,1,20409,2,1,13,2,1,4,1,1,3,3,2};
In> ContFracEval(Take(A, 5))
Out> 33/7;
In> ContFracEval(Take(A,3), remainder)
Out> 1/(1/(remainder+2)+1)+4;
|
See also:
ContFrac
,
GuessRational
.
GuessRational, NearRational -- find optimal rational approximations
Standard library
Calling format:
GuessRational(x)
GuessRational(x, digits)
NearRational(x)
NearRational(x, digits)
|
Parameters:
x -- a number to be approximated
digits -- desired number of decimal digits
Description:
The functions GuessRational(x) and NearRational(x) attempt to find "optimal"
rational approximations to a given value x. The approximations are "optimal"
in the sense of having smallest numerators and denominators among all rational
numbers close to x. This is done by computing a continued fraction
representation of x and truncating it at a suitably chosen term. Both
functions return a rational number which is an approximation of x.
Unlike the function Rationalize() which converts floating-point numbers to
rationals without loss of precision, the functions GuessRational() and
NearRational() are intended to find the best rational that is approximately
equal to a given value.
The function GuessRational() is useful if you have obtained a
floating-point representation of a rational number and you know
approximately how many digits its exact representation should contain.
This function takes an optional second parameter digits which limits
the number of decimal digits in the denominator of the resulting
rational number. If this parameter is not given, it defaults to half
the current precision. This function truncates the continuous fraction
expansion when it encounters an unusually large value (see example).
This procedure does not always give the "correct" rational number; a
rule of thumb is that the floating-point number should have at least as
many digits as the combined number of digits in the numerator and the
denominator of the correct rational number.
The function NearRational(x) is useful if one needs to
approximate a given value, i.e. to find an "optimal" rational number
that lies in a certain small interval around a certain value x. This
function takes an optional second parameter digits which has slightly
different meaning: it specifies the number of digits of precision of
the approximation; in other words, the difference between x and the
resulting rational number should be at most one digit of that
precision. The parameter digits also defaults to half of the current
precision.
Examples:
Start with a rational number and obtain a floating-point approximation:
In> x:=N(956/1013)
Out> 0.9437314906
In> Rationalize(x)
Out> 4718657453/5000000000;
In> V(GuessRational(x))
GuessRational: using 10 terms of the
continued fraction
Out> 956/1013;
In> ContFracList(x)
Out> {0,1,16,1,3,2,1,1,1,1,508848,3,1,2,1,2,2};
|
The first 10 terms of this continued fraction correspond to the correct continued fraction for the original rational number.
In> NearRational(x)
Out> 218/231;
|
This function found a different rational number closeby because the precision was not high enough.
In> NearRational(x, 10)
Out> 956/1013;
|
See also:
ContFrac
,
ContFracList
,
Rationalize
.
Decimal -- decimal representation of a rational
Standard library
Calling format:
Parameters:
frac -- a rational number
Description:
This function returns the infinite decimal representation of a
rational number frac. It returns a list, with the first element
being the number before the decimal point and the last element the
sequence of digits that will repeat forever. All the intermediate list
elements are the initial digits before the period sets in.
Examples:
In> Decimal(1/22)
Out> {0,0,{4,5}};
In> N(1/22,30)
Out> 0.045454545454545454545454545454;
|
See also:
N
.
TruncRadian -- remainder modulo 2*Pi
Standard library
Calling format:
Parameters:
r -- a number
Description:
TruncRadian calculates Mod(r,2*Pi), returning a value between 0
and 2*Pi. This function is used in the trigonometry functions, just
before doing a numerical calculation using a Taylor series. It greatly
speeds up the calculation if the value passed is a large number.
The library uses the formula
TruncRadian(r)=r-Floor(r/(2*Pi))*2*Pi,
where r and 2*Pi are calculated with twice the precision used in the
environment to make sure there is no rounding error in the significant
digits.
Examples:
In> 2*Pi()
Out> 6.283185307;
In> TruncRadian(6.28)
Out> 6.28;
In> TruncRadian(6.29)
Out> 0.0068146929;
|
See also:
Sin
,
Cos
,
Tan
.
Floor -- round a number downwards
Standard library
Calling format:
Parameters:
x -- a number
Description:
This function returns Floor(x), the largest integer smaller than or equal to x.
Examples:
In> Floor(1.1)
Out> 1;
In> Floor(-1.1)
Out> -2;
|
See also:
Ceil
,
Round
.
Ceil -- round a number upwards
Standard library
Calling format:
Parameters:
x -- a number
Description:
This function returns Ceil(x), the smallest integer larger than or equal to x.
Examples:
In> Ceil(1.1)
Out> 2;
In> Ceil(-1.1)
Out> -1;
|
See also:
Floor
,
Round
.
Round -- round a number to the nearest integer
Standard library
Calling format:
Parameters:
x -- a number
Description:
This function returns the integer closest to x. Half-integers
(i.e. numbers of the form n+0.5, with n an integer) are
rounded upwards.
Examples:
In> Round(1.49)
Out> 1;
In> Round(1.51)
Out> 2;
In> Round(-1.49)
Out> -1;
In> Round(-1.51)
Out> -2;
|
See also:
Floor
,
Ceil
.
Pslq -- search for integer relations between reals
Standard library
Calling format:
Parameters:
xlist -- list of numbers
precision -- required number of digits precision of calculation
Description:
This function is an integer relation detection algorithm. This means
that, given the numbers x[i] in the list "xlist", it tries
to find integer coefficients a[i] such that
a[1]*x[1] + ... + a[n]*x[n]=0.
The list of integer coefficients is returned.
The numbers in "xlist" must evaluate to floating point numbers if
the N operator is applied on them.
Example:
In> Pslq({ 2*Pi+3*Exp(1), Pi, Exp(1) },20)
Out> {1,-2,-3};
|
Note: in this example the system detects correctly that
1*(2*Pi+3*e)+(-2)*Pi+(-3)*e=0.
See also:
N
.