lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> >   -- a * exp(I * z) + b)
> >   return z:copy(c):mul(I, true):exp(true):mul(a, true):add(b, true)
> 
> Sorry, not nice! Plus you need some sort of compiler (like the one I posted
> a few days ago) to make this at least bearable to the user.

What about an "operation mode" switch to in-place operations?

> print(complex.opmode()) -- in-place?
false
> c = complex.i
> print(c:exp(), c)
0.54030230586814+0.8414709848079i 0+1i
> old = complex.opmode(true) -- in-place now
> print(c:exp(), c)
0.54030230586814+0.8414709848079i 0.54030230586814+0.8414709848079i
> complex.opmode(old) -- reset
> print(c:exp(), c)
1.1438356437916+1.279883001373i 0.54030230586814+0.8414709848079i

Your function can now be implemented as

require "numlua"

local op -- operation mode: in-place?
local complex_enter = function () op = complex.opmode(true) end
local complex_exit = function () complex.opmode(op) end

local c = complex() -- cache
local I, cexp = complex.i, complex.exp
function f (a, b, z)
  z:copy(c) -- c := z
  complex_enter() -- in-place now
  c = cexp(c * I) * a + b
  complex_exit() -- restore
  return c
end

Cheers,
Luis

-- 
Computers are useless. They can only give you answers.
                -- Pablo Picasso

-- 
Luis Carvalho (Kozure)
lua -e 'print((("lexcarvalho@NO.gmail.SPAM.com"):gsub("(%u+%.)","")))'