RTensor

version 2.3


important remarks

I made this many months ago with reckless/weird development practices, and haven't tested it since. Hopefully, it still works, but one of the dozens of moving parts may have broken. I might fix it sometime.

There is prose documentation.

Comments and questions on RTensor are welcome. Email them to contact@[this domain].

dkl9 home

documentation (example table) RTensor == (R)ecursive (Tensor)
if, when pressing enter, nothing happens, it's probably an uncaught evaluation error, or a very slow computation
you can use up/down arrow keys to navigate history
some examples can be double-clicked on to run them
exampleexplanation
123integer literal (scalar)
3.14non-integer literal (scalar)
.. 3.14single-element vector
abcidentifier
f'prime symbols in identifiers
pi; phi; true; false; nullbuilt-in special constants
a = exprsave value to identifier
_most recent result
a + b; a - b; a * b; a / bbasic operations
a ^ bexponents
b \ xbase-b logarithm of x
a == bcheck equality
(-/10)^2 == 10equality is sensitive to numerical error
a < bcomparison
a < x < bcan't chain comparison
n -/ xnth-root of x
a, b, ctensor [a, b, c]
(a, b, c)[2]tensor indexing; returns b
(a, b, c)[4]tensor index out of range
v[i] = aassign to item in vector
(3, 4, 5) * 0.5vector-scalar operation
(1, 2) + (3, 4)vector-vector operation
(6, 2, 3) + (4, 4)incompatible dimensions
(3, 4, 5), (6, 7, 8)matrix
(3, 4, 5) .. (6, 7, 8)vector concatenation
1 .. 10range of integers
1..10spacing matters here
1 .. (5, 10)invalid
b^2 - 4*a*cquadratic discriminant
b ^ 2 -4 *a*cspacing (usually) doesn't matter
-x; /x; ^x; \xnegative; reciprocal; ex; ln(x)
== xis x zero?
< xis x negative?
-/xsquare root
/-xnegative reciprocal
a = 3; sin(a)^2 + cos(a)^2semicolon-separated statements
cos(x)function evaluation
arctan(3, 2)two-argument function
cos((0, 1))function of a vector
f(x) = x^2function definition
f = x => x^2anonymous function (saved to variable here)
f = x => y => x + y; f(3)(5)two-argument function by currying
f(x) = y => x + y; f(3)(5)mixed currying syntax
f = x, y => x + y; f(3, 5)two-argument function
f(x) = (a => a^2 + a)(\x); f(3)hacky local variables
f(x) = (a = \x)*0 + (a^2 + a); f(3)less hacky local variables
abs(x); floor(x); clear()
len(v); any(v); all(v); tail(v); trim(v)
sin(x); cos(x)
arcsin(y); arccos(y); arctan(y)
various built-in functions
zero(n)n-element vector of zeroes
tail(..0); trim(..0); zero(0); 1 .. 0empty list (four ways)
random()random number, 0 to 1
random(a)invalid
random(a, b)random integer, a to b (inclusive)
fact(n) = n * fact(n - 1)
fact(0) = 1
recursive functions
fact(0) = 1
fact(n) = n * fact(n - 1)
always define base cases last
A(m, n) = A(m - 1, A(m, n - 1))
A(m, 0) = A(m - 1, 1)
A(0, n) = n + 1
the Ackermann function,
right out of Wikipedia
(beware: very recursive and slow)
psi(x) = if(all((-1 < x, x < 1)), ^/-(1 - x^2), 0) the Ψ bump function, right out of Wikipedia
fact(n) = if[n == 0, 1, n * fact(n - 1)] recursive function with a conditional
map(1 .. 5, fact) first few factorials
(assuming fact is already defined)
((f => (n => (if(n==1,(_=>1),f(f)))(n-1) * n))((f => (n => (if(n==1,(_=>1),f(f)))(n-1) * n))))(6) factorial without named functions
filter(v, (x => 1 - <x))remove negative items from vector
reduce(v, (a, b => a + b))sum of the items of a vector
aloz(v, i) = if[len(v) < i, 0, v[i]] access list at index, or 0 if index beyond length
dot(u, v) = if[any((==len(u), ==len(v))), 0, u[1] * v[1] + dot(tail(u), tail(v))] dot product of vectors
prime(n) = for[(g = 2), floor(n / g) < n / g, (g = if(<(n - g^2 - 1), n, g + 1)), g] == n sieve-based primality checker
div(a, b) = floor(a / b) == a / b
fp(n, l) = 1 - any(map(l, (k => div(n, k))))
last(v) = v(len(v))
fnp(n) = for[(v = zero(0)) + (p = 2), len(v) < n, if[fp(p, v), (v = v, p), (p = p + 1)], v]
first n primes by accelerated sieve
dv(a, b) = (x => x == floor(x))(b / a)
factor(n, g) = if(dv(g, n), zero(1) + g, zero(0)) .. factor(if(dv(g, n), n / g, n), if(dv(g, n), g, g + 1))
factor(1, g) = zero(0)
factor(n) = factor(floor(n), 2)
integer factorisation
cond(v) = len(v) < 20
next(v) = v, (v[len(v)] + v[len(v) - 1])
for[(v = 1, 1), cond(v), (v = next(v)), v]
list of Fibonacci numbers
qs(v) = qs(filter(tail(v), (x => x < v[1]))) .. (.. v[1]) .. qs(filter(tail(v), (x => 1 - (x < v[1]))))
qs(zero(0)) = zero(0)
tv = map(zero(30), (_x => random(1, 100)))
qs(tv)
quicksort algorithm implementation
a(n) = a(n-a(n-1)) + a(n-a(n-2))
a(1) = 1
a(2) = 1
the Hofstadter Q-sequence,
right out of the OEIS
(beware: very recursive and slow)
Qs(v) = v, (v(len(v) + 1 - v(len(v))) + v(len(v) + 1 - v(len(v) - 1)))
Qc(v) = len(v) < 100
for((1, 1), Qc, Qs)
the Hofstadter Q-sequence,
computed iteratively (much faster)
pe(x, p) = sum[(i = 1), len(p), p[i] * x^(i - 1)]
pe(4, (-2, 5, 6))
polynomial evaluation
6x2 + 5x - 2 at x = 4
pd(p) = tail(map(p, (x, i => (i - 1) * x)))
pd((2, 1, 4, 1))
polynomial derivative: x3 + 4x2 + x + 2
rnr(v) = 1 - ==pe(v(1), v(2))
nrs(v) = (v(1) - pe(v(1), v(2)) / pe(v(1), pd(v(2)))), v(2)
pr(x, p) = (for((x, p), rnr, nrs))(1)
pr(0, (2, 1, 4, 1))
polynomial rootfinding by Newton's method
(assuming pe and pd are already defined)
root of x3 + 4x2 + x + 2 near 0
(may loop or error when unable to find root)
digits(n, b) = digits(floor(n / b), b), (n - b * floor(n / b))
digits(0, b) = zero(0)
vector of digits of number in base
(errors on negative inputs)
T(n) = sum[(k = 1), n, k] the triangle numbers,
right out of Wolfram MathWorld
render[(x = (-b + pm(-/(b^2 - 4*a*c))) / (2*a))] render quadratic formula
html[(x = (-b + pm(-/(b^2 - 4*a*c))) / (2*a))] HTML behind the quadratic formula
render[(sum((k=1), n, k^2) = (n + 1) * (2*n + 1) * n / 6)] render sum-of-squares formula
render[(pi/4 = int(0, 1, -/(1 - x^2)*dx))] render an integral for π ÷ 4
mod(a,b) = a - b * floor(a / b)
gcd(m, n) = gcd(n, mod(m, n))
gcd(m, 0) = m
Euclid's GCD algorithm
int(a, b, f, n) = sum[(k = 1), n, (b - a) / n * f(a + (b - a) * ((k - /2) / n))] numerical integration by midpoint sum
cint(a, b, u, f, n) = sum[(k = 1), n, (u(a + (b - a) * ((k + 1) / n)) - u(a + (b - a) * (k / n))) * f(u(a + (b - a) * ((k - /2) / n)))] numerical contour integration
(with parametric curve function u)
fact2(n) = fact(2 * n)
succ(n) = n + 1
cat = fact2 / fact^2 / succ
Catalan numbers by functional arithmetic
(assuming fact is already defined)
latex[integers == filter(reals, (r => floor(r) == ceil(r)))] generate LATEX for a weird definition of the integers

use alternative numeric type: