+, -, *, /, ^
,
Div, Mod
,
Gcd
,
Lcm
,
<<, >>
,
FromBase, ToBase
,
Precision
,
GetPrecision
,
N
,
Rationalize
,
IsPrime
,
IsPrimePower
,
Factors
,
Factor
,
PAdicExpand
,
ContFrac
,
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
Standard math 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 math 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 math library
Calling format:
Parameters:
n, m - 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.
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;
|
See also:
Lcm
.
Lcm -- Least common multiple
Standard math library
Calling format:
Parameters:
n, m -- integers or univariate polynomials
Description:
This command returns the least common multiple of "n" and "m".
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:
See also:
Gcd
.
<<, >> -- Shift operators
Standard math 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 like 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 - a base to write the numbers in
number - a 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 -- Sets the precision
Internal function
Calling format:
Parameters:
n - 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.
Examples:
In> Precision(10)
Out> True;
In> N(Sin(1))
Out> 0.8414709848;
In> Precision(20)
Out> True;
In> N(Sin(1))
Out> 0.84147098480789650665;
In> GetPrecision()
Out> 20;
|
See also:
GetPrecision
,
N
.
GetPrecision -- Returns 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 -- Numerical approximation
Standard math library
Calling format:
N(expr)
N(expr, prec)
Parameters:
expr - expression to evaluate
prec - 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 up to the required precision.
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
.
Rationalize -- Convert floating point numbers to fractions
Standard math library
Calling format:
Rationalize(expr)
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 math library
Calling format:
IsPrime(n)
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
.
IsPrimePower -- Test for a power of a prime number
Standard math library
Calling format:
IsPrime(n)
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
.
Factors -- Factorization
Standard math library
Calling format:
Factors(x)
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
.
Factor -- Factorization, in pretty form
Standard math library
Calling format:
Factors(x)
Parameters:
x - integer or univariate polynomial
Description:
This function factorizes "x", like 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
.
PAdicExpand -- p-adic expansion
Standard math library
Calling format:
PAdicExpand(n, p)
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 math library
Calling format:
ContFrac(x)
ContFrac(x, maxdepth)
|
Parameters:
x - expression to expand in continued fractions
maxdepth - 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
maxdepth 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:
PAdicExpand
,
N
.
Decimal -- Decimal representation of a rational
Standard math library
Calling format:
Decimal(frac)
Parameters:
frac - a rational number
Description:
This function returns the infinite decimal representation of the
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.
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 math library
Calling format:
TruncRadian(r)
Parameters:
r - a radian
Description:
TruncRadian calculates r mod 2*Pi, returning a value
between 0 and 2*Pi. This function is used in the trigonometry
functions, just before doing the numerical calculation. It
greatly speeds up the calculation if the value passed is a big
number.
The library uses the formula
/ r \
r - MathFloor| ------ | * 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 math library
Calling format:
Floor(x)
Parameters:
x - a number
Description:
This function returns the largest integer smaller than "x".
Examples:
In> Floor(1.1)
Out> 1;
In> Floor(-1.1)
Out> -2;
|
See also:
Ceil
,
Round
.
Ceil -- Round a number upwards
Standard math library
Calling format:
Ceil(x)
Parameters:
x - a number
Description:
This function returns the smallest integer larger than "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 math library
Calling format:
Round(x)
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 math 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
.