+, -, *, /, ^ | 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 |
IntLog | integer part of logarithm |
IntNthRoot | integer part of n-th root |
NthRoot | calculate/simplify nth root of an integer |
ContFrac | continued fraction expansion |
ContFracList, ContFracEval | manipulate continued fractions |
GuessRational, NearRational, BracketRational | 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 |
x+y +x |
x-y |
-x x*y |
x/y |
x^y |
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.
In> 2+3 Out> 5; In> 2*3 Out> 6; |
Div(x,y) Mod(x,y) |
If Div(x,y) returns "a" and Mod(x,y) equals "b", then these numbers satisfy x=a*y+b and 0<=b<y.
In> Div(5,3) Out> 1; In> Mod(5,3) Out> 2; |
Gcd(n,m) Gcd(list) |
list -- a list of all integers or all univariate polynomials
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
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); |
Lcm(n,m) Lcm(list) |
This means it also works on polynomials, since Div, Gcd and multiplication are also defined for them.
In> Lcm(60,24) Out> 120; In> Lcm({3,5,7,9}) Out> 315; |
n<<m n>>m |
In> 1 << 10 Out> 1024; In> -1024 >> 10 Out> -1; |
FromBase(base,"string") ToBase(base, number) |
number -- integer, number to write out in a different base
"string" -- string representing a number in a different base
FromBase converts an integer, written as a string in base base, to base 10. ToBase converts number, written in base 10, to base base.
Non-integer arguments are not supported.
In> FromBase(2,"111111") Out> 63; |
Write the (decimal) number 255 in hexadecimal notation:
In> ToBase(16,255) Out> "ff"; |
Precision(n) |
This is not the number of digits after the decimal point. For example, 123.456 has 3 digits after the decimal point and 6 digits of mantissa. The number 123.456 is adequately computed by specifying Precision(6).
The call Precision(n) will not guarantee that all results are precise to n digits.
When the precision is changed, all variables containing previously calculated values remain unchanged. The Precision function only makes all further calculations proceed with a different precision.
Also, when typing floating-point numbers, the current value of Precision is used to implicitly determine the number of precise digits in the number.
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; |
The value x is not changed by a Precision() call:
In> [ Precision(10); x; ] Out> 0.84147098480789650665; |
The value x is rounded off to 10 digits after an arithmetic operation:
In> x+0. Out> 0.8414709848; |
In the above operation, 0. was interpreted as a number which is precise to 10 digits (the user does not need to type 0.0000000000 for this to happen). So the result of x+0. is precise only to 10 digits.
GetPrecision() |
In> GetPrecision(); Out> 10; In> Precision(20); Out> True; In> GetPrecision(); Out> 20; |
N(expr) N(expr, prec) |
prec -- integer, precision to use
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.)
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; |
Rationalize(expr) |
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.
In> {1.2,3.123,4.5} Out> {1.2,3.123,4.5}; In> Rationalize(%) Out> {6/5,3123/1000,9/2}; |
IntLog(n, base) |
This function can also be used to quickly count the digits in a given number.
In> IntLog(257^8, 2) Out> 64; |
Count the number of decimal digits:
In> IntLog(321^321, 10) Out> 804; |
IntNthRoot(x, n) |
This function is used to test numbers for prime powers.
In> IntNthRoot(65537^111, 37) Out> 281487861809153; |
NthRoot(m,n) |
n -- a positive integer greater than 1 ( n>1)
For large m and small n NthRoot may work quite slowly. Every result {f,r} for given m, n is saved in a lookup table, thus subsequent calls to NthRoot with the same values m, n will be executed quite fast.
In> NthRoot(12,2) Out> {2,3}; In> NthRoot(81,3) Out> {3,3}; In> NthRoot(3255552,2) Out> {144,157}; In> NthRoot(3255552,3) Out> {12,1884}; |
ContFrac(x) ContFrac(x, depth) |
depth -- integer, maximum required depth of result
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.
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; |
ContFracList(frac) ContFracList(frac, depth) ContFracEval(list) ContFracEval(list, rest) |
depth -- desired number of terms
list -- a list of coefficients
rest -- expression to put at the end of the continued fraction
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.
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; |
GuessRational(x) GuessRational(x, digits) NearRational(x) NearRational(x, digits) BracketRational(x, eps) |
digits -- desired number of decimal digits (integer)
eps -- desired precision
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.
The function BracketRational(x,eps) can be used to find approximations with a given relative precision from above and from below. This function returns a list of two rational numbers {r1,r2} such that r1<x<r2 and Abs(r2-r1)<Abs(x*eps). The argument x must be already evaluated to enough precision so that this approximation can be meaningfully found. If the approximation with the desired precision cannot be found, the function returns an empty list.
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}; |
In> NearRational(x) Out> 218/231; |
In> NearRational(x, 10) Out> 956/1013; |
In> BracketRational(N(Ln(10)), 10^(-8)) Out> {12381/5377,41062/17833}; |
Decimal(frac) |
In> Decimal(1/22) Out> {0,0,{4,5}}; In> N(1/22,30) Out> 0.045454545454545454545454545454; |
TruncRadian(r) |
The library uses the formula
In> 2*Pi() Out> 6.283185307; In> TruncRadian(6.28) Out> 6.28; In> TruncRadian(6.29) Out> 0.0068146929; |
Floor(x) |
In> Floor(1.1) Out> 1; In> Floor(-1.1) Out> -2; |
Ceil(x) |
In> Ceil(1.1) Out> 2; In> Ceil(-1.1) Out> -1; |
Round(x) |
In> Round(1.49) Out> 1; In> Round(1.51) Out> 2; In> Round(-1.49) Out> -1; In> Round(-1.51) Out> -2; |
Pslq(xlist,precision) |
precision -- required number of digits precision of calculation
The numbers in "xlist" must evaluate to floating point numbers if the N operator is applied on them.
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.