Skip to content
fdformulaPublic

About

MathLua turns Lua into a tiny, portable, free but powerful MATLAB and more. It provides commonly used MATLAB functions and convenient matrix operations as well. They make preparing, processing, and plotting data much easier and faster than working in most other programming languages.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

MathLua - A Daily Computing Tool

The heart of MathLua is Mathly, a Lua module that turns Lua into a tiny, portable, free but powerful MATLAB and more. It provides commonly used MATLAB functions and convenient matrix operations as well. They make preparing, processing, and plotting data much easier and faster than working in most other programming languages.

No other packages are needed! The entire MathLua toolkit, including Lua interpreter, is less than 5 MB. Because it is super lightweight and fast, it can run fast even on old and slow devices.

MathLua is especially good for instructors and students of algebra, calculus, linear algebra, statistics, numerical computing, and so on. It can simply be a daily computing tool.

MathLua makes it easy to prepare, process, and plot data.

x = linspace(0, pi, 200) -- prepare data
y = x * sin(x^2)         -- process data
plot(x, y, '-r')         -- plot data

Elementwise operations make it easy to process data.

x = tt{0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100} -- enable elementwise operations
y = tt{1.94, 3.96, 5, 6.5, 8.07, 6.09, 9.14, 10.97, 11.21, 14.74, 18.28}

n   = length(x)
Sx  = sum(x)
Sy  = sum(y)
Sxy = sum(x * y) -- elementwise operations: x * y, x * x, y * y, x * sin(x^2) - 5
Sxx = sum(x * x)
Syy = sum(y * y)

tmp = n*Sxx - Sx^2
r = (n*Sxy - Sx*Sy) / sqrt(tmp * (n*Syy - Sy^2))
print(r) -- 0.9569...

a1 = (n*Sxy - Sx*Sy) / tmp
a0 = (Sxx*Sy - Sxy*Sx) / tmp
function f(x) return a1*x + a0 end
axisnotsquare()
plot(x, y, 'ob', f)

Code can be much simpler if we know more about MathLua.

x = {0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100}
y = {1.94, 3.96, 5, 6.5, 8.07, 6.09, 9.14, 10.97, 11.21, 14.74, 18.28}

s, coeffs = polyfit(x, y, 1) -- linear least-squares regression
corr(x, y) -- 0.9569...

axisnotsquare()
plot(x, y, 'ob', '@(x) ' .. s)

MathLua can do calculus.

f = ff('@(x) 0.5*x^2') -- f(x) = 0.5 x^2
x0 = 2
plot(f, '-r', '@(x) f(x0) + diff(f, x0)*(x - x0)')

It is easy to implement animations.

fstr = '@(x) a * (x - h)^2 + k' -- a, h, and k are each a control here
opts = {
  a = {-3, 3, 0.02, default = 3}, h = {-10, 10, 0.5, default = 0}, k = {-90, 90, default = 0},
  xrange = {-10, 10}, yrange = {-100, 100},
  layout = {
    width = 600, height = 400, square = false,
    title = "y = a (x - h)^2 + k"
  }
}
manipulate(fstr, opts)

More animations? See Gallery of Math Animations

MathLua manual? See https://fdformula.github.io/mathly

About

MathLua turns Lua into a tiny, portable, free but powerful MATLAB and more. It provides commonly used MATLAB functions and convenient matrix operations as well. They make preparing, processing, and plotting data much easier and faster than working in most other programming languages.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Contributors

Languages