(original) (raw)

NumCalc Tutorial

NumCalc is a lightweight and easy to use scientific calculator based on QuickJS. It has the following features:

NumCalc is a superset of Javascript, so most Javascript language and library features are also available. A command line version of NumCalc (

qjscalc

) is provided with the QuickJS Javascript engine.

Basic Use

You can type:

2*(1+3)

which yields:

8

_

is a variable containing the last result:

3*_

yields:

24

Fractions: NumCalc tries to give exact results with fractions:

1/3+1/2

which yields:

5/6

fractions and integers are not limited in size (except by the memory and the time you want to spent waiting for the result !). For example:

2^256

yields the exact value of 2 to the power of 256.

Floating point numbers: You can still get the approximate floating point result by enabling the numeric mode, by using a decimal point in your input or by using the Float() function:

1/3+1/7.0

which yields:

0.47619047619047619047619047619047617

The floating point numbers are internally represented in base 2 with the IEEE 754 semantics. The internal precision (=number of mantissa bits) can be changed with the

\p

(in bits) or

\digits

(in decimal digits):

\p

FP precision=113 bits (~34 digits), exponent size=15 bits

\p 192 1/7.0

0.14285714285714285714285714285714285714285714285714285714287

Shortcuts are available for common IEEE 754 floating point number sizes:

\p f64

is the same as

\p 53 11

.

Functions: all the usual mathematical functions are available such as: sqrt (square root), sin, cos, tab, log (logarithm in base e), exp, ...
Constants are referenced by upper case names, e.g. PI for the pi constant. Example:

sin(PI/8)

0.3826834323650897717284599840303989

Variables: You can store your results in variables and reuse them in the following expressions:

a=sin(PI/8)

b=sqrt(a)

Multiple expressions: You can type several expressions on the same line by separating them with a semicolon.

a=1+3;b=a^2+1

Binary arithmetic: Use the 0x prefix to enter hexadecimal numbers (e.g. 0x1a). The 0b prefix is used for binary numbers. The output radix can be changed with the \x (hexadecimal) and \d (decimal) directives.

The calculator supports all the standard Javascript bitwise logical and shift operators. The notable exception is the exclusive or which is implemented with the ^^ operator because the ^ binary operator is reserved for the power operator.

All binary arithmetic functions assume that integers are represented in two's complement notation. Hence a negative number can be seen as having an infinite number of '1' to the left.

Advanced Use

You can stop reading this tutorial at this point if you don't intend to deal with more advanced mathematical objects

Complex numbers: use the constant I to enter complex numbers:

(1+2*I)*(2+3*I)

Most predefined functions deal with complex numbers:

exp(-I*PI)

Polynomials: use the constant X to enter polynomials. Polynomials are represented internally as an array of their coefficient, so the actual variable used to represent them does not matter. In the calculator, the X variable is used by convention.

(1+X)^3

X^3+3*X^2+3*X+1

You can evaluate a polynomial at one point by using the

apply

method:

p=(1+X)^3;p.apply(1/2)

apply

is also useful to substitute the X variable with another polynomial.
A polynomial complex root finder based on the Laguerre's method is also included:

polroots(X^3-1)

Rational fonctions: if you divide two polynomials you get a rational fonction and they can be manipulated as objects too:

deriv(1/(1+X)+2/(2+X)+3/(3+X))

gets the derivative of the rational function. Note that integration of rational fonctions is not supported.

Taylor series: Taylor series are created with the O function. Taylor series can be seen as polynomials where all the terms of degrees >= O(X^n) are thrown away. To be more precise, the calculator handles Laurent series as well. It means that the exponents can be negative too.
Example of use:

(1+X+O(X^3))^(1/3)

1+1/3*X-1/9*X^2+O(X^3)

Computes the order 2 taylor expansion of (1+x)^(1/3). The calculator also knows the Taylor expansion of most usual functions:

sin(X+O(X^4))

X-1/6*X^3+O(X^4)

Linear algebra: Vectors and matrixes are supported. Example:
Enter a 3x3 matrix:

a=[[1,2,3],[2,3,4],[5,6,8]]

Compute its determinant:

det(a)

Inverse it:

inverse(a)

The functions

rank

,

ker

,

charpoly

and

eigenvals

yield respectively the rank, kernel, characteric polynomial and eigen values of a matrix.

Vectors are also supported:

v=[1,2,3]

When dealing with matrix multiplication, they are considered as column vectors, e.g:

a*v

[14, 20, 41]

dp(x,y)

does the dot product of vectors.

cp(x,y)

does the 3 dimensional cross product of vectors. Most operations defined on numbers also work on vectors and matrixes component-wise.

Primes: isprime(n) and nextprime(n) test if a number is prime using the Miller Rabin probabilistic test. factor(n) factorize a number using trial divisions.

Modulo arithmetic: invmod(a,m) returns the inverse of a modulo m. pmod(a,b,m) compute a^b modulo m with less computational resources than doing a^b%m. It also works with polynomials.

Mod(a, n) represents the residue of a modulo the integer n. It supports the usual operators:

1/Mod(3,17)^11+2

yields:

Mod(7,17)

Polynomial modulo P: PolyMod works the same way as Mod but with polynomials:

1/PolyMod(X,X^2+1)

yields:

PolyMod(-X,X^2+1)

Credits

NumCalc borrows many ideas from PARI/GPand calc.

It uses the QuickJSJavascript engine.

NumCalc Reference

Javascript support

Numcalc relies on a superset of Javascript. The main difference with Javascript are:

The exact differences are listed in the paper jsbignum.pdf.

Directives

Directives change the state of the command line user interface. They are preceeded by the antislash character.

\h Show the list of directives
\x Switch to hexadecimal number display
\d Switch to decimal number display
\t Toggle timing display
\clear Clear the terminal
\a Switch to algebraic mode. Division of integers returns a fraction.
\n Switch to numeric mode. Division of integers returns a floating point number.
\p [m [e] | fN] Set the floating point precision to 'm' bits and 'e' exponent bits. 'fN' is an abbreviation for the IEEE 754 floating point number format of 'N' bits (.e.g '\p f64' is an abbreviation of '\p 53 11').
\digits n Set the floating point precision to ceil(n*log2(10)) bits

Global definitions

Arithmetic

norm2(x) Norm of x. For a real number, return x^2. For a complex number, return re(x)^2+im(x)^2
abs(x) Absolute value of x. For a complex number, return sqrt(re(x)^2+im(x)^2)
trunc(x) Largest integer in absolute value <= x. If x is a rational fonction, return its integer part.
floor(x) Largest integer <= x
ceil(x) Smallest integer >= x
fact(n) Factorial of n
comb(n, p) Binomial coefficient (n, p)
bestappr(x,b) Fractional approximation of x using a denominator <= b
sqrt(x) Square root of x
conj(x) Complex conjugate of x
arg(x) Argument of x
inverse(x) Multiplicative inverse of x
gcd(a,b) Greatest Common Divisor of a and b (integers or polynomials)
isprime(n[,t]) Return true if n is prime, 0 if not. A probabilistic Miller Rabin test is done. If true is returned, the probability that n is not prime is 1-0.5^t. By default, t = 30
nextprime(n) Next pseudo prime following n. It is called a pseudo prime because a probabilistic primality test is done.
factor(n) Factorization of n. Return a vector containing the prime factors of n sorted in increasing order
invmod(x,m) Inverse of x modulo m (integer or polynomial)
pmod(a,b,m) a^b modulo m (integer or polynomial)
I Square root of -1
a+b Add two numbers, polynomials or matrixes
a-b Substract two numbers, polynomials or matrixes
a*b Multiply two numbers, polynomials or matrixes
a/b Divide two numbers, polynomials or matrixes
a%b Euclidian remainder of a divided by b, i.e. always positive
a^b or a**b a raised to the power of y
Integer(x) Convert x to integer
Float(x) Convert x to floating point
Fraction(a,b) Return the fraction a/b
Mod(a,m) Represents a modulo m

Bit Manipulation

a&b Bitwise and between the integers a and b
a|b Bitwise or between the integers a and b
a^^b Bitwise exclusive or between the integers a and b
~n Bitwise not of the integer n
a<<n Shift left by n bits. Equivalent to floor(a*2^(-n))
a>>n Shift right by n bits. Equivalent to floor(a/2^n)

Transcendental

exp(x) Exponential of x
log(x) Natural logarithm of x
log2(x) Logarithm of x in base 2
log10(x) Logarithm of x in base 10
sin(x) Sine of x
cos(x) Cosine of x
tan(x) Tangent of x
asin(x) Inverse sine of x
acos(x) Inverse cosine of x
atan(x) Inverse tangent of x
atan2(y, x) Inverse tangent of y/x (same as arg(x+I*y))
sinc(x) Normalized sinc function, defined as sin(PI*x)/(PI*x)
PI Pi constant
todb(x) Convert x to decibels
fromdb(x) Convert x from decibels to linear
todeg(x) Convert from radians to degrees
fromdeg(x) Convert from degrees to radians

Linear Algebra, Matrixes

Matrix(h, w) Zero filled matrix of h rows and w columns
[1,2,3] Enter a vector
[[1,2],[3,4]] Enter a matrix
x[n] x[n] is the nth element of vector x (zero based)
x[n][m] x[n][m] is the matrix element at row n and column m (zero based)
dp(x,y) Dot product of vectors x and y
cp(x,y) Cross product of vectors x and y
idn(n) Identity matrix of order n
diag(v) Creates the diagonal matrix whose diagonal entries are the entries of the vector v
trans(a) Transpose of the matrix or vector a
det(a) Determinant of matrix a
trace(a) Trace of matrix a
charpoly(x) characteristic polynomial of the matrix x (=det(x*Id-x))
eigenvals(x) Return a vector containing the eigen values of the matrix x
rank(x) Rank of matrix x (only works with integer matrixes)
ker(x) Basis of the kernel of matrix x (only works with integer matrixes)
Matrix.hilbert(n) Hilbert matrix of order n

Polynomial, Rational functions, Power series

Polynomial(a) Build a polynomial from vector a.
PolyMod(p, q) Represents the polynomial p modulo q (p and q are polynomials)
RationalFunction(p,q) Return the rational function p/q (p and q are polynomials).
X Same as Polynomial([0, 1])
p.deg() Degree of the polynomial or rational function p. Return -Infinity if p is the zero polynomial
p.apply(a) Evaluate the the polynomial, rational fonction or power series p by replacing the variable with a
deriv(p) Derivative of the polynomial, rational fonction or power series p
integ(p) Formal integration of the polynomial or power series p. The constant term is set to zero
polroots(p) Return a vector containing the roots of polynomial p.
Series(p, n) Return a power series by truncating the polynomial p to 'n' terms
O(x) Return a power series of zero term matching the degree of the polynomial or rational fonction x

Object properties

Integer

isInteger(a) Return true if a is of the Integer type.
tdiv(a, b) trunc(a/b)
fdiv(a, b) floor(a/b)
cdiv(a, b) ceil(a/b)
ediv(a, b) floor(a/b)*sign(b) (Euclidian division)
tdivrem(a, b) Division and remainder (trunc)
fdivrem(a, b) Division and remainder (floor)
cdivrem(a, b) Division and remainder (ceil)
edivrem(a, b) Euclidian division and remainder
sqrt(a) floor(sqrt(a))
sqrtrem(a) if r=floor(sqrt(a), return [r, a-r^2]
floorLog2(a) floor(log2(a))
ctz(a) Return e such as a=m 2^e with m an odd integer (count trailing zeros). Return 0 if a = 0.

Float

isFloat(a) Return true if a is a floating point number.

Fraction instances

num Numerator (integer)
den Denominator (integer)

Complex instances

re Real part
im Imaginary part

Mod instances

res Residue modulo mod (integer)
mod Modulo (integer)

RationalFunction instances

num Numerator (polynomial)
den Denominator (polynomial)

PolyMod instances

res Residue modulo mod (polynomial)
mod Modulo (polynomial)