[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
**Subject**: **Re: A pure-lua complex math library with some new design choices**
**From**: Luiz Henrique de Figueiredo <lhf@...>
**Date**: Mon, 28 Nov 2016 07:46:33 -0200

> I wrote a new pure-lua implementation of complex numbers.
Nice!
I suggest you include a sample program such as mandelbrot.lua attached below
to show the library in action and to test performance.
However, complex arithmetic (or any arithmetic for that matter) is not to
be taken lightly. Care is needed to avoid unnecessary loss of precision or
overflow. For instance, your code for cabs may overflow:
local abs = applyOldOrNew(
function(c) return luamath.sqrt(re(c)*re(c) + im(c)*im(c)) end,
luamath.abs, 'abs')
See http://www.johndcook.com/blog/2010/06/02/whats-so-hard-about-finding-a-hypotenuse/
See http://www.netlib.org/fdlibm/e_hypot.c for a robust implementation of hypot.
For a reference implementation of complex arithmetic, see the cephes library at
http://www.netlib.org/cephes/
> Second, I did download a few other complex libraries for lua and tried them. None clicked exactly with what I was looking for, so decided to have a go at rolling my own.
I'd like feedback on what you found lacking in my lcomplex library.
Send it off-list if you prefer. Thanks.
--lhf
-- mandelbrot.lua
local complex=require"complex"
local abs=complex.abs
local math=math
complex=complex.new
local function level(x,y)
local c=complex(x,y)
local l=0
local z=c
repeat
z=z*z+c
l=l+1
until abs(z)>2 or l>255
return math.floor(math.sqrt(255*(l-1)))
end
local xmin=-2
local xmax= 1
local ymin=-1.5
local ymax= 1.5
local N=800
local dx=(xmax-xmin)/N
local dy=(ymax-ymin)/N
print("P2")
print("# mandelbrot set",xmin,xmax,ymin,ymax,N)
print(N,N,255)
for i=1,N do
local y=ymax-(i-1)*dy
for j=1,N do
local x=xmin+(j-1)*dx
print(level(x,y))
end
end