[ << ] [ < ] [ Up ] [ > ] [ >> ] [Top] [Contents] [Index] [ ? ]

6.1 Numbers

 [ << ] [ < ] [ Up ] [ > ] [ >> ] [Top] [Contents] [Index] [ ? ]

6.1.1 `idiv`, `irem`

idiv(i1,i2)

:: Integer quotient of i1 divided by i2.

irem(i1,i2)

:: Integer remainder of i1 divided by i2.

return

integer

i1 i2

integer

• Integer quotient and remainder of i1 divided by i2.
• i2 must not be 0.
• If the dividend is negative, the results are obtained by changing the sign of the results for absolute values of the dividend.
• One can use i1 `%` i2 for replacement of `irem()` which only differs in the point that the result is always normalized to non-negative values.
• Use `sdiv()`, `srem()` for polynomial quotient.
```[0] idiv(100,7);
14
[0] idiv(-100,7);
-14
[1] irem(100,7);
2
[1] irem(-100,7);
-2
```
References

 [ << ] [ < ] [ Up ] [ > ] [ >> ] [Top] [Contents] [Index] [ ? ]

6.1.2 `fac`

fac(i)

:: The factorial of i.

return

integer

i

integer

• The factorial of i.
• Returns 0 if the argument i is negative.
```[0] fac(50);
30414093201713378043612608166064768844377641568960512000000000000
```

 [ << ] [ < ] [ Up ] [ > ] [ >> ] [Top] [Contents] [Index] [ ? ]

6.1.3 `igcd`,`igcdcntl`

igcd(i1,i2)

:: The integer greatest common divisor of i1 and i2.

igcdcntl([i])

:: Selects an algorithm for integer GCD.

return

integer

i1 i2 i

integer

• Function `igcd()` returns the integer greatest common divisor of the given two integers.
• An error will result if the argument is not an integer; the result is not valid even if one is returned.
• Use `gcd()`, `gcdz()` for polynomial GCD.
• Various method of integer GCD computation are implemented and they can be selected by `igcdcntl`.
`0`

Euclid algorithm (default)

`1`

binary GCD

`2`

bmod GCD

`3`

accelerated integer GCD

`2`, `3` are due to `[Weber]`.

In most cases `3` is the fastest, but there are exceptions.

```[0] A=lrandom(10^4)\$
[1] B=lrandom(10^4)\$
[2] C=lrandom(10^4)\$
[3] D=A*C\$
[4] E=A*B\$
[5] cputime(1)\$
[6] igcd(D,E)\$
0.6sec + gc : 1.93sec(2.531sec)
[7] igcdcntl(1)\$
[8] igcd(D,E)\$
0.27sec(0.2635sec)
[9] igcdcntl(2)\$
[10] igcd(D,E)\$
0.19sec(0.1928sec)
[11] igcdcntl(3)\$
[12] igcd(D,E)\$
0.08sec(0.08023sec)
```
References

 [ << ] [ < ] [ Up ] [ > ] [ >> ] [Top] [Contents] [Index] [ ? ]

6.1.4 `ilcm`

ilcm(i1,i2)

:: The integer least common multiple of i1 and i2.

return

integer

i1 i2

integer

• This function computes the integer least common multiple of i1, i2.
• If one of argument is equal to 0, the return 0.
References

 [ << ] [ < ] [ Up ] [ > ] [ >> ] [Top] [Contents] [Index] [ ? ]

6.1.5 `isqrt`

isqrt(n)

:: The integer square root of n.

return

non-negative integer

n

non-negative integer

 [ << ] [ < ] [ Up ] [ > ] [ >> ] [Top] [Contents] [Index] [ ? ]

6.1.6 `inv`

inv(i,m)

:: the inverse (reciprocal) of i modulo m.

return

integer

i m

integer

• This function computes an integer such that ia ≡ 1 mod (m).
• The integer i and m must be mutually prime. However, `inv()` does not check it.
```[71] igcd(1234,4321);
1
[72] inv(1234,4321);
3239
[73] irem(3239*1234,4321);
1
```
References

 [ << ] [ < ] [ Up ] [ > ] [ >> ] [Top] [Contents] [Index] [ ? ]

6.1.7 `prime`, `lprime`

prime(index)
lprime(index)

:: Returns a prime number.

return

integer

index

integer

• The two functions, `prime()` and `lprime()`, returns an element stored in the system table of prime numbers. Here, `index` is a non-negative integer and be used as an index for the prime tables. The function `prime()` can return one of 1900 primes up to 16381 indexed so that the smaller one has smaller index. The function `lprime()` can return one of 999 primes which are 8 digit sized and indexed so that the larger one has the smaller index. The two function always returns 0 for other indices.
• For more general function for prime generation, there is a `PARI` function

`pari(nextprime,number)`.

```[95] prime(0);
2
[96] prime(1228);
9973
[97] lprime(0);
99999989
[98] lprime(999);
0
```
References

 [ << ] [ < ] [ Up ] [ > ] [ >> ] [Top] [Contents] [Index] [ ? ]

6.1.8 `random`

random([seed])
seed
return

non-negative integer

• Generates a random number which is a non-negative integer less than 2^32.
• If a non zero argument is specified, then after setting it as a random seed, a random number is generated.
• As the default seed is fixed, the sequence of the random numbers is always the same if a seed is not set.
• The algorithm is Mersenne Twister (http://www.math.keio.ac.jp/matsumoto/mt.html) by M. Matsumoto and T. Nishimura. The implementation is done also by themselves.
• The period of the random number sequence is 2^19937-1.
• One can save the state of the random number generator with `mt_save`. By loading the state file with `mt_load`, one can trace a single random number sequence arcoss multiple sessions.
References

 [ << ] [ < ] [ Up ] [ > ] [ >> ] [Top] [Contents] [Index] [ ? ]

6.1.9 `lrandom`

lrandom(bit)

:: Generates a long random number.

bit
return

integer

• Generates a non-negative integer of at most bit bits.
• The result is a concatination of outputs of `random`.
References

 [ << ] [ < ] [ Up ] [ > ] [ >> ] [Top] [Contents] [Index] [ ? ]

6.1.10 `mt_save`, `mt_load`

mt_save(fname)

:: Saves the state of the random number generator.

:: Loads a saved state of the random number generator.

return

0 or 1

fname

string

• One can save the state of the random number generator with `mt_save`. By loading the state file with `mt_load`, one can trace a single random number sequence arcoss multiple Asir sessions.
```[340] random();
3510405877
[341] mt_save("/tmp/mt_state");
1
[342] random();
4290933890
[343] quit;
% asir
This is Asir, Version 991108.
1
[341] random();
4290933890
```
References

 [ << ] [ < ] [ Up ] [ > ] [ >> ] [Top] [Contents] [Index] [ ? ]

6.1.11 `nm`, `dn`

nm(rat)

:: Numerator of rat.

dn(rat)

:: Denominator of rat.

return

integer or polynomial

rat

rational number or rational expression

• Numerator and denominator of a given rational expression.
• For a rational number, they return its numerator and denominator, respectively. For a rational expression whose numerator and denominator may contain rational numbers, they do not separate those rational coefficients to numerators and denominators.
• For a rational number, the denominator is always kept positive, and the sign is contained in the numerator.
• Risa/Asir does not cancel the common divisors unless otherwise explicitly specified by the user. Therefore, `nm()` and `dn()` return the numerator and the denominator as it is, respectively.
```[2] [nm(-43/8),dn(-43/8)];
[-43,8]
[3] dn((x*z)/(x*y));
y*x
[3] dn(red((x*z)/(x*y)));
y
```
References

 [ << ] [ < ] [ Up ] [ > ] [ >> ] [Top] [Contents] [Index] [ ? ]

6.1.12 `conj`, `real`, `imag`

real(comp)

:: Real part of comp.

imag(comp)

:: Imaginary part of comp.

conj(comp)

:: Complex conjugate of comp.

return comp

complex number

• Basic operations for complex numbers.
• These functions works also for polynomials with complex coefficients.
```[111] A=(2+@i)^3;
(2+11*@i)
[112] [real(A),imag(A),conj(A)];
[2,11,(2-11*@i)]
```

 [ << ] [ < ] [ Up ] [ > ] [ >> ] [Top] [Contents] [Index] [ ? ]

6.1.13 `eval`, `deval`

eval(obj[,prec])
deval(obj)

:: Evaluate obj numerically.

return

number or expression

obj

general expression

prec

integer

• Evaluates the value of the functions contained in obj as far as possible.
• `deval` returns double float. Rational numbers remain unchanged in results from `eval`.
• In `eval` the computation is done by MPFR library. In `deval` the computation is done by the C math library.
• `deval` cannot handle complex numbers.
• When prec is specified, computation will be performed with a precision of about prec-digits. If prec is not specified, computation is performed with the precision set currently. (See section `setbprec`, `setprec`.)
• Currently available numerical functions are listed below.

`sin`, `cos`, `tan`,

`asin`, `acos`, `atan`,

`sinh`, `cosh`, `tanh`, `asinh`, `acosh`, `atanh`,

`exp`, `log`, `pow(a,b) (a^b)`

• Symbols for special values are as the followings. Note that `@i` cannot be handled by `deval`.
@i

unit of imaginary number

@pi

the number pi, the ratio of circumference to diameter

@e

Napier’s number (exp(1))

```[118] eval(exp(@pi*@i));
-1.0000000000000000000000000000
[119] eval(2^(1/2));
1.414213562373095048763788073031
[120] eval(sin(@pi/3));
0.86602540378443864674620506632
[121] eval(sin(@pi/3)-3^(1/2)/2,50);
-2.78791084448179148471 E-58
[122] eval(1/2);
1/2
[123] deval(sin(1)^2+cos(1)^2);
1
```
References

 [ << ] [ < ] [ Up ] [ > ] [ >> ] [Top] [Contents] [Index] [ ? ]

6.1.14 `pari`

pari(func,arg,prec)

:: Call PARI function func.

return

Depends on func.

func

Function name of PARI.

arg

Arguments of func.

prec

integer

• This command connects Asir to PARI system so that several functions of PARI can be conveniently used from Risa/Asir.
• PARI `[Batut et al.]` is developed at Bordeaux University, and distributed as a free software. Though it has a certain facility to computer algebra, its major target is the operation of numbers (bignum, bigfloat) related to the number theory. It facilitates various function evaluations as well as arithmetic operations at a remarkable speed. It can also be used from other external programs as a library. It provides a language interface named ‘gp’ to its library, which enables a user to use PARI as a calculator which runs on UNIX.
• The last argument (optional) int specifies the precision in digits for bigfloat operation. If the precision is not explicitly specified, operation will be performed with the precision set by `setprec()`.
• Some of currently available functions of PARI system are as follows. Note these are only a part of functions in PARI system. A complete list of functions which can be called from asir is in the ox_pari manual of the asir-contrib. For details of individual functions, refer to the PARI manual. (Some of them can be seen in the following example.)

`abs`, `adj`, `arg`, `bigomega`, `binary`, `ceil`, `centerlift`, `cf`, `classno`, `classno2`, `conj`, `content`, `denom`, `det`, `det2`, `detr`, `dilog`, `disc`, `discf`, `divisors`, `eigen`, `eintg1`, `erfc`, `eta`, `floor`, `frac`, `galois`, `galoisconj`, `gamh`, `gamma`, `hclassno`, `hermite`, `hess`, `imag`, `image`, `image2`, `indexrank`, `indsort`, `initalg`, `isfund`, `isprime`, `ispsp`, `isqrt`, `issqfree`, `issquare`, `jacobi`, `jell`, `ker`, `keri`, `kerint`, `kerintg1`, `kerint2`, `kerr`, `length`, `lexsort`, `lift`, `lindep`, `lll`, `lllg1`, `lllgen`, `lllgram`, `lllgramg1`, `lllgramgen`, `lllgramint`, `lllgramkerim`, `lllgramkerimgen`, `lllint`, `lllkerim`, `lllkerimgen`, `lllrat`, `lngamma`, `logagm`, `mat`, `matrixqz2`, `matrixqz3`, `matsize`, `modreverse`, `mu`, `nextprime`, `norm`, `norml2`, `numdiv`, `numer`, `omega`, `order`, `ordred`, `phi`, `pnqn`, `polred`, `polred2`, `primroot`, `psi`, `quadgen`, `quadpoly`, `real`, `recip`, `redcomp`, `redreal`, `regula`, `reorder`, `reverse`, `rhoreal`, `roots`, `rootslong`, `round`, `sigma`, `signat`, `simplify`, `smalldiscf`, `smallfact`, `smallpolred`, `smallpolred2`, `smith`, `smith2`, `sort`, `sqr`, `sqred`, `sqrt`, `supplement`, `trace`, `trans`, `trunc`, `type`, `unit`, `vec`, `wf`, `wf2`, `zeta`

• Asir currently uses only a very small subset of PARI.
```/* Eigen vectors of a numerical matrix */
[0] pari(eigen,newmat(2,2,[[1,1],[1,2]]));
[ -1.61803398874989484819771921990 0.61803398874989484826 ]
[ 1 1 ]
/* Roots of a polynomial */
[1] pari(roots,t^2-2);
[ -1.41421356237309504876 1.41421356237309504876 ]
```
References

 [ << ] [ < ] [ Up ] [ > ] [ >> ] [Top] [Contents] [Index] [ ? ]

6.1.15 `setbprec`, `setprec`

setbprec([n])
setprec([n])

:: setbprec, setprec set the precision for bigfloat operations to n bits, n digits respectively.

return

integer

n

integer

• When an argument n is given, these functions set the precision for bigfloat operations to n bits or n digits. The return value is always the previous precision regardless of the existence of an argument.
• Bigfloat operations are done by MPFR library.
• This is effective for computations in bigfloat. Refer to `ctrl()` for turning on the ‘bigfloat flag.’
• There is no upper limit for precision digits. It sets the precision to some digits around the specified precision. Therefore, it is safe to specify a larger value.
```[1] setprec();
15
[2] setprec(100);
15
[3] setprec(100);
99
[4] setbprec();
332
```

 [ << ] [ < ] [ Up ] [ > ] [ >> ] [Top] [Contents] [Index] [ ? ]

6.1.16 `setround`

setround([mode])

:: Sets the rounding mode mode.

return

integer

mode

integer

• When an argument mode is given, these functions set the rounding mode for bigfloat operations to mode. The return value is always the previous rounding mode regardless of the existence of an argument. The meanings of rounding modes are as follows
`0`

Round to nearest

`1`

Round toward 0

`2`

Round toward +infinity

`3`

Round toward -infinity

• This is effective for computations in bigfloat. Refer to `ctrl()` for turning on the ‘bigfloat flag.’
```[1] setprec();
15
[2] setprec(100);
15
[3] setprec(100);
99
[4] setbprec();
332
```

 [ << ] [ < ] [ Up ] [ > ] [ >> ] [Top] [Contents] [Index] [ ? ]

6.1.17 `setmod`

setmod([p])

:: Sets the ground field to GF(p).

return

integer

n

prime less than 2^27

• Sets the ground field to GF(p) and returns the value p.
• A member of a finite field does not have any information about the field and the arithmetic operations over GF(p) are applied with p set at the time.
• As for large finite fields, see section Finite fields.
```[0] A=dp_mod(dp_ptod(2*x,[x]),3,[]);
(2)*<<1>>
[1] A+A;
[1] setmod(3);
3
[2] A+A;
(1)*<<1>>
```
References

 [ << ] [ < ] [ Up ] [ > ] [ >> ] [Top] [Contents] [Index] [ ? ]

6.1.18 `ntoint32`, `int32ton`

ntoint32(n)
int32ton(int32)

:: Type-conversion between a non-negative integer and an unsigned 32bit integer.

return

unsigned 32bit integer or non-negative integer

n

non-negative interger less than 2^32

int32

unsigned 32bit integer

• These functions do conversions between non-negative integers (the type id 1) and unsigned 32bit integers (the type id 10).
• An unsigned 32bit integer is a fundamental construct of OpenXM and one often has to send an integer to a server as an unsigned 32bit integer. These functions are used in such a case.
References

 [ << ] [ < ] [ Up ] [ > ] [ >> ] [Top] [Contents] [Index] [ ? ]

6.1.19 `inttorat`

inttorat(a,m,b)

:: Perform the rational reconstruction.

return

list or 0

a
m
b

integer

• For an integer a, find a positive integer x and an intger y satisfying xa=y mod m, x, |y| < b and GCD(x,y)=1.
• If such x, y exist then a list [y,x] is returned. Otherwise 0 is returned.
• If b is set to floor(sqrt(M/2)), then x and y are unique if they exist. floor(sqrt(M/2)) can be computed by `floor` and `isqrt`.
```[2121] M=lprime(0)*lprime(1);
9996359931312779
[2122] B=isqrt(floor(M/2));
70697807
[2123] A=234234829304;
234234829304
[2124] inttorat(A,M,B);
[-20335178,86975031]
```
References

 [ << ] [ < ] [ Up ] [ > ] [ >> ]

This document was generated on April 22, 2021 using texi2html 5.0.