Arithmetic and other operations on numbers

(directly go to documentation on : +, -, *, /, ^, Div, Mod, Gcd, Lcm, <<, >>, FromBase, ToBase, N, Rationalize, ContFrac, Decimal, Floor, Ceil, Round, Min, Max, Numer, Denom, Pslq. )

# 2. 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 addition
- | arithmetic subtraction or negation
* | arithmetic multiplication
/ | arithmetic division
^ | arithmetic power
Div | Determine divisor of two mathematical objects
Mod | Determine remainder of two mathematical objects after dividing one by the other
Gcd | greatest common divisor
Lcm | least common multiple
<< | binary shift left operator
>> | binary shift right operator
FromBase | conversion of a number from non-decimal base to decimal base
ToBase | conversion of a number in decimal base to non-decimal base
N | try determine numerical approximation of expression
Rationalize | convert floating point numbers to fractions
ContFrac | continued fraction expansion
Decimal | decimal representation of a rational
Floor | round a number downwards
Ceil | round a number upwards
Round | round a number to the nearest integer
Min | minimum of a number of values
Max | maximum of a number of values
Numer | numerator of an expression
Denom | denominator of an expression
Pslq | search for integer relations between reals

###

+ -- arithmetic addition

##### Standard library

##### Calling format:

    x+y
    +x

Precedence: 70

##### Parameters:

`x` and `y` -- objects for which arithmetic addition is defined

##### Description:

The addition operators 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;

###

- -- arithmetic subtraction or negation

##### Standard library

##### Calling format:

    x-y

Precedence: left-side: 70 , right-side: 40

    -x

##### Parameters:

`x` and `y` -- objects for which subtraction is defined

##### Description:

The subtraction operators 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> -1;
    In> - 3
    Out> -3;

###

* -- arithmetic multiplication

##### Standard library

##### Calling format:

    x*y

Precedence: 40

##### Parameters:

`x` and `y` -- objects for which arithmetic multiplication is defined

##### Description:

The multiplication operator can work on integers, rational numbers, complex numbers, vectors, matrices and lists.

This operator is 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> 6;

###

/ -- arithmetic division

##### Standard library

##### Calling format:

    x/y

Precedence: 30

##### Parameters:

`x` and `y` -- objects for which arithmetic division is defined

##### Description:

The division operator can work on integers, rational numbers, complex numbers, vectors, matrices and lists.

This operator is implemented in the standard math library (as opposed to being built-in). This means that they can be extended by the user.

##### Examples:

    In> 6/2
    Out> 3;

###

^ -- arithmetic power

##### Standard library

##### Calling format:

    x^y

Precedence: 20

##### 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> 8;

###

Div -- Determine divisor of two mathematical objects

###

Mod -- Determine remainder of two mathematical objects after dividing one by the other

##### Standard library

##### Calling format:

    Div(x,y)
    Mod(x,y)

##### 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:

    Gcd(n,m)
    Gcd(list)

##### 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:

    Lcm(n,m)
    Lcm(list)

##### 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 .

###

<< -- binary shift left operator

###

>> -- binary shift right operator

##### Standard library

##### Calling format:

    n<<m
    n>>m

##### 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 -- conversion of a number from non-decimal base to decimal base

###

ToBase -- conversion of a number in decimal base to non-decimal base

##### Internal function

##### Calling format:

    FromBase(base,"string")
    ToBase(base, number)

##### Parameters:

`base` -- integer, base to convert to/from

`number` -- integer, number to write out in a different base

`"string"` -- string representing a number in a different base

##### Description:

In Yacas, all numbers are written in decimal notation (base 10). The two functions `FromBase`, `ToBase` convert numbers between base 10 and a different base. Numbers in non-decimal notation are represented by strings.

`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.

##### Examples:

Write the binary number `111111` as a decimal number:

    In> FromBase(2,"111111")
    Out> 63;

Write the (decimal) number `255` in hexadecimal notation:

    In> ToBase(16,255)
    Out> "ff";

##### See also:

PAdicExpand .

###

N -- try determine numerical approximation of expression

##### Standard library

##### Calling format:

    N(expression)
    N(expression, precision)

##### Parameters:

`expression` -- expression to evaluate

`precision` -- integer, precision to use

##### Description:

The function `N` instructs `Yacas` to try to coerce an expression in to a numerical approximation to the expression `expr`, using `prec` digits precision if the second calling sequence is used, and the default precision 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.)

`N` is a macro. Its argument `expr` will only be evaluated after switching to numeric mode.

##### 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:

Pi .

###

Rationalize -- convert floating point numbers to fractions

##### Standard 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 .

###

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:

PAdicExpand , N .

###

Decimal -- decimal representation of a rational

##### Standard library

##### Calling format:

    Decimal(frac)

##### 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 .

###

Floor -- round a number downwards

##### Standard library

##### Calling format:

    Floor(x)

##### 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:

    Ceil(x)

##### 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:

    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 .

###

Min -- minimum of a number of values

##### Standard library

##### Calling format:

    Min(x,y)
    Min(list)

##### Parameters:

`x`, `y` -- pair of values to determine the minimum of

`list` -- list of values from which the minimum is sought

##### Description:

This function returns the minimum value of its argument(s). If the first calling sequence is used, the smaller of "x" and "y" is returned. If one uses the second form, the smallest of the entries in "list" is returned. In both cases, this function can only be used with numerical values and not with symbolic arguments.

##### Examples:

    In> Min(2,3);
    Out> 2;
    In> Min({5,8,4});
    Out> 4;

##### See also:

Max , Sum .

###

Max -- maximum of a number of values

##### Standard library

##### Calling format:

    Max(x,y)
    Max(list)

##### Parameters:

`x`, `y` -- pair of values to determine the maximum of

`list` -- list of values from which the maximum is sought

##### Description:

This function returns the maximum value of its argument(s). If the first calling sequence is used, the larger of "x" and "y" is returned. If one uses the second form, the largest of the entries in "list" is returned. In both cases, this function can only be used with numerical values and not with symbolic arguments.

##### Examples:

    In> Max(2,3);
    Out> 3;
    In> Max({5,8,4});
    Out> 8;

##### See also:

Min , Sum .

###

Numer -- numerator of an expression

##### Standard library

##### Calling format:

    Numer(expr)

##### Parameters:

`expr` -- expression to determine numerator of

##### Description:

This function determines the numerator of the rational expression "expr" and returns it. As a special case, if its argument is numeric but not rational, it returns this number. If "expr" is neither rational nor numeric, the function returns unevaluated.

##### Examples:

    In> Numer(2/7)
    Out> 2;
    In> Numer(a / x^2)
    Out> a;
    In> Numer(5)
    Out> 5;

##### See also:

Denom , IsRational , IsNumber .

###

Denom -- denominator of an expression

##### Standard library

##### Calling format:

    Denom(expr)

##### Parameters:

`expr` -- expression to determine denominator of

##### Description:

This function determines the denominator of the rational expression "expr" and returns it. As a special case, if its argument is numeric but not rational, it returns `1`. If "expr" is neither rational nor numeric, the function returns unevaluated.

##### Examples:

    In> Denom(2/7)
    Out> 7;
    In> Denom(a / x^2)
    Out> x^2;
    In> Denom(5)
    Out> 1;

##### See also:

Numer , IsRational , IsNumber .

###

Pslq -- search for integer relations between reals

##### Standard library

##### Calling format:

    Pslq(xlist,precision)

##### 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 .