Code

Help

Introduction

The program implements an interactive math-oriented playground based on Javascript. It runs directly in web browser, either online or offline. The program includes basic implementations of complex numbers, matrices and units. While it could be possible to further extend the capabilities by using available math libraries (Numeric Javascript, Math.js, etc), for any serious task one should consider a more performant system, such as Python with SciPy in Jupyter environment.

To allow immediate evaluation of simple formulas, all common mathematical functions and a number of math and physics constants are directly available. Hence, one can simply enter a formula such as sin(45*deg) and after hitting Ctrl+Enter the calculation result will appear in the Output area. To display multiple results, use the function print. Simple graphs are produced with the command plot.

If a part of the code in the Code area is selected, only the selection gets evaluated, otherwise all of the code is executed.

Basic Math Functions

abs(x) Absolute value.
sqrt(x) Square root.
exp(x)
pow(a,x)
Exponentiation (\(e^x\)) and general power function (\(a^x\)).
ln(x)
log(x,b)
log10(x)
Logarithms. log(x,b) returns \(\log_b x\). Parameter b is optional so that log(x) returns \(\ln x\).
sin(x)
cos(x)
tan(x)
Trigonometric functions.
asin(z)
acos(z),
atan(z)
atan(x,y)
Inverse trigonometric functions.
sinh(x)
cosh(x)
tanh(x)
Hyperbolic functions.
min(x1,x2,...)
max(x1,x2,...)
sum(x1,x2,...)
mean(x1,x2,...)
stdev(x1,x2,...)
Select minimum or maximum value. Calculate sum, mean and standard deviation. These functions accept arbitrary number of arguments and each arguments can be an array itself.
rnd()
rnd(b)
rnd(a,b)
Generate random numbers spanning \([0,1)\), \([0,b)\) or \([a,b)\).
(number).pow(x)
(number).root(x)
Number raised to the specified power.

Math and Physics Constants

Constants with units become available only after calling loadConst(true).

pi, π
e
deg
Mathematical constants.
deg denotes pi/180 (value of a degree in radians).
N_A Avogadro's number.
k_b Boltzmann constant.
eps_0, ε_0 Electric constant (permittivity of vacuum).
q_e Elementary charge
alpha Fine-structure constant.
G Gravitational constant.
mu_0, μ_0 Magnetic constant (permeability of vacuum).
m_e, m_p, m_n Mass of electron, proton or neutron.
h, hbar, ħ Planck constant. hbar or ħ denotes h/(2*pi).
c Speed of light in vacuum.
g Standard gravity (acceleration of free fall).
sigma, σ Stefan-Boltzmann constant.
R Universal gas law constant.
b Wien displacement law constant.
lb, psi, inch, foot, mile, yd, eV, kph, mph, at Values of some common non-SI units (expressed in the SI base units). These quantities are defined only when calling loadConst().

Printing

print(a1,a2,...) Sequentially display all arguments in the Output window.
print.digits The default accuracy (number of digits) used for printing numerical values. Defaults to 6.
print.fix If set (true), numerical values are printed in fixed-point format.
print.lines Sets the number of lines visible in the output window.
print.term A (string) value used to terminate the output. Defaults to "\n" (new line).

Plotting

plot(x1,y1,x2,y2,...,opts)
plot(f1,f2,...,opts)
plot([f1,f2,...],opts)
Makes a graph (in a new window) of the provided data arrays or functions. Additional settings are specified either as the last parameter opts or by using the following global parameters.
plot.range The default plotting range in the form [start, stop, step].
plot.colors The cycle of default colors (an array).
plot.styles The default styles (a string or an array of strings).
'-' indicates line plot and 'o' indicates scatter plot.

Special Math Functions and Objects

Random.uniform()
Random.gauss()
Uniform or Gaussian random numbers.
Polynom(coeff) Returns the function \(f(x)=p_0+p_1x+\ldots+p_nx^n\), where the coefficients \(p_i\) are contained in the array coeff.
Special.erf(x)
Special.gamma(x)
Error and Gamma functions.
Solve.linear(A,b) Returns the solution vector \(x\) (array of numbers) for the system of linear equations \(Ax=b\), where \(A\) is square matrix of the coefficients and \(b\) is vector of the constant terms. Gaussian elimination method is used.
Solve.root(f,a,b,ε) Finds a root of the function f with an accuracy better than ε. The root must be located between a and b.
Solve.quad(a,b,c) Returns an array of solutions of the quadratic equation \(ax^2+bx+c=0\). You can send the object directly to print to display the solution.
Solve.diff(f,y0,tmax,dt) Uses the classical 4. order Runga-Kutta method to solve the differential equation \(y'=f(t,y)\). Parameter y0 is the initial state at \(t=0\). The iteration proceeds till tmax with a step of dt. In the case of a system of differential equations y0 must be an array and the function f must also return an array of derivatives. A 2-dimensional array is returned, where the first row contains the values of the independent variable and the following rows contain the respective values of the dependent variables.
Fit.line(x,y) Returns a function representing the line which is the best least-squares fit to the data in the arrays x and y. You can send the object directly to print to display the solution. Properties slope and offset contain the optimized parameters of the line.
Fit.poly(x,y,n) Returns a function representing a polynomial of degee n which is the best least-squares fit to the data in the arrays x and y. You can send the function object directly to print to display the solution. Property coeff of the object contains the array of optimized polynomial coefficients.
Fit.spline(x,y) Returns a function representing a "natural" cubic spline going through the data points in the arrays x and y.
(array).add(x)
(array).sub(x)
(array).mul(x)
(array).div(x)
Element-wise arithmetic with arrays. The argument x can be either an array or a number. The procedure is recursively applied to array-type elements.
range(start,stop,step)
linspace(start,stop,count)
Returns a sequence of equally spaced values.
Complex(re,im)
Complex.polar(r,θ)
Creates an object representing a complex number. The object has methods to perform common mathematical operations with complex numbers (add, sub, mul, div, abs, arg, cc, exp, pow, sqrt, log, sin, cos, tan, etc).
Matrix(array)
Matrix(rows,cols,fn)
Creates an object representing a matrix. The object has methods to add (add), subtract (sub) and multiply (mul) matrices, to calculate transpose (trans), inverse (inv) and determinant (det), and to extract a submatrix (slice, rows, cols).
Qty(value,unit) Creates an object representing a value value with unit unit. Similarly to complex numbers the object defines a set of mathematical operations and also methods to(unit) and in(unit) for unit conversion.
Fourier.square(n)
Fourier.triangle(n)
Fourier.sawtooth(n)
Fourier.rectified(n)
Fourier.pulse(duty)(n)
Returns the \(n\)-th Fourier component of the basic waveforms. n is an integer ≥0. Fourier.series(wave,n) calculates the sum of the first n terms in the Fourier series. Each of these functions returns the waveform as a function of \(\omega t\) (the product of angular frequency and time).

Other Useful Functions and Methods

time() Returns current time in milliseconds.
isNum(x)
isStr(x)
isArr(x)
isFun(x)
Check the data type of the argument.
(object).prec(n) Converts the object to string so that any contained numerical value is displayed as a floating-point number with n significant digits. Trailing zeroes are omitted. Numbers ≥107 and ≥10n+1 or <10−3 are displayed in exponential notation.
(object).fixed(n) Converts the object to string so that any contained numerical value is displayed as a fixed-point number with n digits after the decimal point. Trailing zeroes are preserved.
(string).pad(len,c,alignLeft) Pads the string with character c (space by default) so that the length of the resulting string is len.
(array).clone() Makes a shallow copy of the array. Array-type elements are recursively cloned as well.
zip(a1,a2,...) Returns an array, where the nth element is an array composed from the nth elements of the argument arrays.

You can download the program for offline use or to add more features .

The program uses CodeMirror for syntax highlighting and Flot for plotting.