lua-users home
lua-l archive

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


On Sat, Aug 20, 2011 at 08:13:39PM +0200, Luis Carvalho wrote:
> I'm happy to announce the availability of Numeric Lua 0.3! 

Most of it is very nice.  I had great trouble with LuaRocks and could 
not get LuaHelp to see the NumLua help files, so I wrote the Lua script 
below, which does not need LuaHelp.

The following is not so nice:

1. x:fft() is only allowed when x is already complex.

> x=matrix{1,2,3}
> =x:fft()
stdin:1: calling 'fft' on bad self
> y=matrix({1,2,3},true)
> =y:fft():pretty()
             6+0i   -1.5+0.866025i   -1.5-0.866025i

2. But one can't use `complex` to convert a real matrix to complex.
Worse, you're not warned about it.

> =complex(x)
0+0i

Dirk

--------------------------------------------------------------------

Assuming directory structure like this:
./numlua.so 
./numlua/lhp/

~~~~ numlua.lua
#! /usr/local/bin/lua5.2 -i
-- Lua 5.2 is required only because package.searchpath is used.
-- UN*X: mark this file executable and alias to $HOME/bin/numlua
-- Windows: remove top line, make numlua.bat invoking Lua 5.2 on this file
-- usage: type `numlua` at the prompt

do 

local old = {}
local new = {} 
local section = {}

for k,v in pairs(_G) do old[k]=v end
require "numlua"
local where = package.searchpath("numlua",package.cpath)
local dir, delim = where:match("(.*)([/\\])")
local helpdir = dir..delim.."numlua"..delim.."lhp"
print ("looking for help files in "..helpdir)
for k,v in pairs(_G) do if old[k]~=v then 
    new[#new+1]=k; 
    local t={}    
    for j,w in pairs(v) do t[#t+1]=j end
    table.sort(t); section[k]=t
    local filename = helpdir..delim..k..".lhp"
    if io.open(filename) then section[k].filename = filename end
    end end
table.sort(new)

print (numlua._VERSION.."  Copyright (C) 2011 Luis Carvalho"..
    "\n-- sections: "..table.concat(new,", "))
print "-- try: help(SECTION), help(SECTION,'demo'), help[section,ITEM)"
print "-- dot syntax allowed, e.g. help 'matrix.new'"
 
local function wrap(s,delim)  -- like table.concat, but splits into lines
    delim = delim or ' '
    local lines={}
    local len = 0
    local start = 1
    local wrap_at = 70
    for k,v in ipairs(s) do
	v = tostring(v)
	if len + #v > wrap_at then 
	    lines[#lines+1]='  '..table.concat(s,delim,start,k-1)
	    start = k; len = 0  
	    end
	len = len + #v + #delim 
	end
    if len>0 then lines[#lines+1]='  '..table.concat(s,delim,start,#s) end
    return table.concat(lines,'\n')
    end

Help = print

function help(sect,item)
    if type(sect)~='string' then 
	print "section name must be a string"
	return
	end
    local a,b = sect:match("(.*)%.(.*)")
    if a then sect, item = a, b end
    if item and type(item)~='string' then 
	print "item name must be a string"
	return
	end
    local s = section[sect]
    if not s then 
	print("!! "..sect.." is not one of: "..table.concat(new,", ")) 
	return
	end
    if not item then 
        print("section '"..sect.."' contains\n"..wrap(section[sect]))
	return
    elseif item=="demo" then
	dofile(s.filename)
	return
	end
    local filename = s.filename:sub(1,-5)..delim..item..".lhp"
    local try=pcall(dofile,filename)
    if not try then 
	for _,k in ipairs(section[sect]) do if k==item then
	    print("no help available for "..sect.."."..item)
	    return
	    end end
	print("no such thing as "..sect.."."..item) 
	end
    end    

end
~~~~