Advanced maths: features and limits
This page summarises the advanced maths in Varlig Calc: lists, vectors and matrices, complex numbers, sums and products, equation solving and calculus. For each feature it lists what the functions do, what they accept, how exact the answers are and where the limits lie. Use it to check the details when an answer surprises you.
For introductions with more examples, see the guides: Lists and statistics, Vectors and matrices, Complex numbers and Summation, equation solving and calculus.
At a glance
| Feature | Example | Answer |
|---|---|---|
| List | [1, 2+3, 4*2] |
[1, 5, 8] |
| Item from a list | [10, 20, 30][1] |
20 |
| Item from a nested list | [[1,2],[3,4]][1][0] |
3 |
| Total of quantities | sum([1 kg, 500 g]) |
1.5 kg |
| Statistics | average([1,2,3]), median([5,1,3]) |
2, 3 |
| Range, counting down | range(5, 1, -2) |
[5, 3, 1] |
| Adding lists | [1,2] + [3,4] |
[4, 6] |
| Matrix product | [[1,2],[3,4]] * [[1,0],[0,1]] |
[[1, 2], [3, 4]] |
| Inverse | inverse([[1,2],[3,4]]) |
[[-2, 1], [1.5, -0.5]] |
| Determinant | det([[1,2],[3,4]]) |
-2 |
| Transpose | transpose([[1,2],[3,4]]) |
[[1, 3], [2, 4]] |
| Linear system | solve_system([[2,1],[1,2]], [34,29]) |
[13, 8] |
| Complex arithmetic | (2+3i) * (2-3i) |
13 |
| Complex square root | sqrt(complex(-4,0)) |
2i |
| Sum over a range | sum(k^2, k, 1, 10) |
385 |
| Product over a range | product(k, k, 1, 5) |
120 |
| Solve an equation | solve(x^2 = 4, x, 0, 3) |
2 |
| Symbolic derivative | differentiate(sin(x), x) |
cos(x) |
| Derivative at a point | derivative(x^3, x, 2) |
12 |
| Definite integral | integrate(x^2, x, 0, 3) |
9 |
Lists, matrices and complex numbers are ordinary values. You can give them names, pass them to functions you define, and they recalculate when you edit an earlier line.
squares(n) = sum(k^2, k, 1, n)squares(3) gives 14first(items) = items[0]first([10, 20]) gives 10root(y) = solve(x^2 = y, x, 0, 10)root(9) gives 3Writing lists and maths functions
- Commas separate items and arguments. A comma followed by a space always separates, and a comma followed by exactly three digits groups thousands, so
[1,200, 1,800]has two items. Without spaces, every comma separates, as in[12,-51,4]. Where commas could be read either way, as in[1,234], the error shows both ways to write it. Use a decimal point for fractions of a number. - Matrices are lists of rows, such as
[[1,2],[3,4]]. There’s no semicolon shorthand like[1,2;3,4]. - Indexes start at 0 and must be whole numbers of 0 or more. An index past the end of the list is an error; it never wraps round or gets cut down to fit.
- Argument counts are checked.
sqrt(4,9)is an error rather than a guess.
Lists and statistics
These functions accept a list: length or count, sum, product, average or mean, median, min, max, and stddev (the sample standard deviation).
- An empty list has a
sumof 0, aproductof 1 and acountof 0. Other statistics of an empty list are errors. - Two lists of the same length add and subtract item by item. You can multiply or divide a list by a number, and compare two lists with
==. range(start, end, step)includes both ends. The step is optional.dot(a, b)multiplies two real vectors.
spend = [420, 385, 510, 460] gives [420, 385, 510, 460]sum(spend) gives 1,775average(spend) gives 443.75median(spend) gives 440spend[0] gives 420spend * 1.1 gives [462, 423.5, 561, 506]range(0, 1, 0.25) gives [0, 0.25, 0.5, 0.75, 1]List transformations
| Call | Answer | What it does |
|---|---|---|
sort([3,1,2]) |
[1, 2, 3] |
Sorts numbers without units into ascending order; equal items keep their order |
reverse([1,2,3]) |
[3, 2, 1] |
Reverses any list |
unique([3,1,3,2]) |
[3, 1, 2] |
Keeps the first of each value; items must match exactly, units included |
slice([10,20,30,40], 1, 3) |
[20, 30] |
Takes items from the start index up to, but not including, the end index |
map(x^2, x, [1,2,3]) |
[1, 4, 9] |
Works out the expression for each item |
filter(x > 1, x, [1,2,3]) |
[2, 3] |
Keeps the items where the condition is true |
variance([1,2,3]) |
1 |
Exact sample variance, dividing by one less than the count |
sliceneeds whole numbers where 0 ≤ start ≤ end ≤ the list’s length. Other bounds are errors.varianceneeds at least two numbers without units.mapandfiltertake the expression first, then the name to use for each item, then the list. The name only applies inside the call, so it doesn’t change a name you’ve defined elsewhere. The expression infiltermust givetrueorfalse.
Vectors and matrices
Vectors are flat lists and matrices are lists of rows. Matrices must be rectangular, contain real numbers without units, and have 1 to 32 rows and columns.
| Function | What it does |
|---|---|
A * B |
Matrix product; A’s column count must equal B’s row count |
transpose(A) |
Swaps rows and columns |
shape(A) |
[rows, columns] |
identity(n) |
An n by n identity matrix |
trace(A) |
Sum of the diagonal of a square matrix |
det(A) |
Determinant of a square matrix |
inverse(A) |
Inverse of a square matrix; a singular matrix is an error |
rank(A) |
Rank, by exact elimination, for any rectangular matrix |
dot(a, b) |
Dot product of two vectors of the same length |
cross(a, b) |
Cross product of two three-item vectors |
solve_system(A, b) |
Solves A*x = b for a square matrix A and a flat vector b |
eigenvalues(A) |
Eigenvalues of a real square matrix, including complex pairs, in no set order |
qr(A) |
[Q, R], where Q*R is approximately A |
lu(A) |
[P, L, U], where P*A is approximately L*U; square matrices only |
svd(A) |
[U, s, Vt], where s is a flat list of singular values and A is approximately U*diag(s)*Vt |
Exact results. det, inverse, rank and solve_system use exact fractions. rank has no rounding tolerance. solve_system reports an error for a singular system, and doesn’t handle non-linear systems or systems with infinitely many solutions.
Approximate results. eigenvalues, qr, lu and svd use floating point, and check that their output is finite. qr and svd accept rectangular matrices and return the compact (“thin”) factors. Eigenvalue and SVD calculations stop after 10,000 iterations. In the displayed answer, an entry that is only round-off next to the largest entry shows as 0, while indexing it returns the stored value. Badly conditioned matrices can lose accuracy, so don’t test these results with ==.
# Two adult tickets and one child ticket cost 34; one adult and two children cost 29solve_system([[2,1],[1,2]], [34,29]) gives [13, 8]det([[2,1],[1,2]]) gives 3rank([[1,2],[2,4]]) gives 1Complex numbers
i is the imaginary unit, unless you’ve defined a name or custom unit called i.
- Functions:
complex(real, imaginary),real,imag,conj,abs,arg(in radians) andsqrt(the principal root) all accept complex numbers. - Exactness: the parts, arithmetic and whole-number powers stay exact. General roots and
argare approximate. - Real results: when imaginary parts cancel, as in
(2+3i) * (2-3i), the answer is an ordinary real number. A value you write as complex, such as-4 + 0iorcomplex(-4,0), stays complex through arithmetic and names, even though it displays as-4. - Square roots and logarithms of negative numbers:
sqrt(-4),ln(-1)andlog(-100)stay in real numbers and show an imaginary-number error that spells out the complex form to write instead, such assqrt(-4 + 0i). That, orsqrt(complex(-4,0)), gives2i. - Display: a part that is only floating-point round-off shows as 0, so
exp(i*pi)shows-1. The stored value keeps its digits. - Comparisons:
<and>work only when both imaginary parts are zero. - Other functions:
sin,cos,tanand their inverses, the hyperbolic functions and their inverses,exp,ln,logorlog10,log2,cbrt, and fractional or complex powers all accept complex numbers. They use floating point and principal branches. The logarithm of zero, and any result that isn’t finite, is an error.
(2+3i) * (2-3i) gives 13abs(3+4i) gives 5sqrt(-4 + 0i) gives 2isqrt(complex(-4,0)) gives 2isqrt(-4) gives Error: imaginary number. For a complex answer, write sqrt(-4 + 0i)exp(i*pi) gives -1Sums and products
sum(expression, name, from, to) adds up the expression for each whole number from from to to, including both ends. summation is another name for it, and product multiplies instead.
- The name is local to the call. It doesn’t change a name you’ve already defined, and it doesn’t make the line depend on one.
- You can put one sum or product inside another.
- If
tois less thanfrom, the range is empty: the sum is 0 and the product is 1. - A single call can have at most 10,000 terms.
sum(k^2, k, 1, 10) gives 385product(k, k, 1, 5) gives 120k = 7 gives 7sum(k, k, 1, 3) gives 6k gives 7Solving equations
solve(expression, name, lower, upper) finds one real value of name between lower and upper where the expression is zero. solve(left = right, name, lower, upper) finds where the two sides are equal.
- The bounds must be finite numbers, with
lowerless thanupper. - A root exactly at a bound is accepted. Otherwise the expression must be positive at one bound and negative at the other, and it should be continuous in between.
- The search halves the interval each step. It succeeds when the expression is within
1e-10of zero and the interval is narrow enough, within 128 steps. If the expression is undefined somewhere it’s checked, or the search doesn’t settle, the line shows an error. - It finds one root, not all of them. For systems of linear equations, use
solve_system.
solve(1500 + 12*x = 20*x, x, 0, 1000) gives 187.5solve(x^2 = 2, x, 0, 2) gives 1.4142135624Calculus
Symbolic derivatives
differentiate(expression, name), also written derivative(expression, name), returns the derivative as text.
- It uses the sum, product, quotient, power and chain rules on real-number arithmetic without units, plus
sin,cos,tan,sinh,cosh,exp,lnandsqrt. - Other names in the expression are treated as constants, whether or not they have a value in the note:
differentiate(a*x^2, x)gives(a*(2*(x^(2-1)))). - The result isn’t simplified, and it’s limited to 8,192 characters.
- Other functions, such as
abs, are errors; use a derivative at a point for those. This isn’t a full computer algebra system. - The derivative has the same domain restrictions as the original expression.
differentiate(sin(x), x) gives cos(x)differentiate(x^3, x) gives (3*(x^(3-1)))Derivatives at a point
derivative(expression, name, point), or differentiate with three arguments, works out the slope at one point numerically. It uses refined central differences and checks them against one-sided estimates. The expression can use functions you’ve defined, but it must be smooth near the point and use real numbers without units. If the estimate doesn’t settle or a sample is undefined, the line shows an error.
Because the numerical forms work out a number, every name other than the variable needs a value. This applies to derivatives at a point, sum, product, solve and integrate; a name without a value shows Unsupported: Unknown name: followed by the name.
derivative(x^3, x, 2) gives 12f(x) = x^2derivative(f(x), x, 3) gives 6differentiate(a*x^2, x) gives (a*(2*(x^(2-1))))derivative(a*x^2, x, 3) gives Unsupported: Unknown name: aDefinite integrals
integrate(expression, name, lower, upper) works out the area under the expression between two finite bounds.
- Swapping the bounds changes the sign of the answer, and equal bounds give zero.
- It uses adaptive 7- and 15-point Gauss–Kronrod rules, which sample at uneven points. That keeps a regular wave such as
cos(128*pi*x)from fooling it:integrate(cos(128*pi*x), x, 0, 1)gives about-1.3e-15, which is zero to within rounding. - It aims for a total error estimate of at most
1e-9times the answer’s size (or1e-9for answers smaller than 1). It compares at least two levels of detail, allows for rounding error, and refines the part with the largest estimated error first. - It stops after 2,048 refinements, 20 levels of subdivision, or the 100,000 steps shared with other advanced maths in the line. Reaching a limit, or an undefined sample, is an error.
- The error estimate is a good guide, not a guarantee. Use smooth, finite expressions. Results for expressions with spikes, jumps or fast oscillation may be inaccurate.
- Indefinite integrals and infinite bounds aren’t supported.
integrate(x^2, x, 0, 3) gives 9integrate(x^2, x, 3, 0) gives -9Arguments and units in maths functions
factorialis another name forfact, andtruncforint.signreturns -1, 0 or 1.atan2(y, x)returns an angle in radians as a plain number.- The optional second argument of
round,floorandceilis a step, not a number of decimal places:round(17, 5)rounds to the nearest 5. To round to decimal places, see Rounding. - Logarithms, exponentials and hyperbolic functions don’t accept units or money. Ordinary trigonometry accepts angles such as
30°. - Square and cube roots keep units when the power divides evenly, and powers of quantities keep their units. Temperatures in Celsius or Fahrenheit can’t be squared or rooted.
gcd(12, 18) gives 6round(17, 5) gives 15sin(30°) gives 0.5sqrt(9 m²) gives 3 m(2 kg)^2 gives 4 kg²Limits
Limits are checked before an answer is shown:
- 1,024 items in a list or
range - matrices up to 32 × 32
- 10,000 terms in one
sumorproduct - 16 levels of nested lists and function calls
- 100,000 steps shared by all the advanced maths in a line, including nested calls
- 4,096 bits in the top or bottom of a fraction, the same as elsewhere in Varlig Calc
Hitting a limit shows an error on that line, and the rest of the note keeps working. You never get a partial sum, an inverse or an unconverged root presented as an answer. For all the limits and how exact answers are, see Reading results: display and precision.