Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Import to git

  • Loading branch information...
commit 8463d868d76e4bbe7d3558f633d6e87a1319db7a 0 parents
@drahosp drahosp authored
17 CMakeLists.txt
@@ -0,0 +1,17 @@
+# Copyright (C) 2007-2009 LuaDist.
+# Created by Peter Kapec
+# Redistribution and use of this file is allowed according to the terms of the MIT license.
+# For details see the COPYRIGHT file distributed with LuaDist.
+# Please note that the package source code is licensed under its own license.
+
+PROJECT(lcomplex C)
+CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
+INCLUDE(dist.cmake)
+
+# Build
+ADD_LUA_MODULE(complex lcomplex.c)
+
+# Install all files and documentation
+INSTALL (TARGETS complex DESTINATION ${INSTALL_CMOD})
+INSTALL (FILES README DESTINATION ${INSTALL_DATA})
+INSTALL (FILES test.lua DESTINATION ${INSTALL_TEST})
66 Makefile
@@ -0,0 +1,66 @@
+# makefile for complex library for Lua
+
+# change these to reflect your Lua installation
+LUA= /tmp/lhf/lua-5.1.4
+LUAINC= $(LUA)/src
+LUALIB= $(LUA)/src
+LUABIN= $(LUA)/src
+
+# these will probably work if Lua has been installed globally
+#LUA= /usr/local
+#LUAINC= $(LUA)/include
+#LUALIB= $(LUA)/lib
+#LUABIN= $(LUA)/bin
+
+# probably no need to change anything below here
+CC= gcc
+CFLAGS= $(INCS) $(WARN) -O2 $G
+WARN= -std=c99 -pedantic -Wall
+INCS= -I$(LUAINC)
+MAKESO= $(CC) -shared
+#MAKESO= env MACOSX_DEPLOYMENT_TARGET=10.3 $(CC) -bundle -undefined dynamic_lookup
+
+MYNAME= complex
+MYLIB= l$(MYNAME)
+T= $(MYNAME).so
+OBJS= $(MYLIB).o
+TEST= test.lua
+
+all: test
+
+test: $T
+ $(LUABIN)/lua $(TEST)
+
+o: $(MYLIB).o
+
+so: $T
+
+$T: $(OBJS)
+ $(MAKESO) -o $@ $(OBJS)
+
+clean:
+ rm -f $(OBJS) $T core core.*
+
+doc:
+ @echo "$(MYNAME) library:"
+ @fgrep '/**' $(MYLIB).c | cut -f2 -d/ | tr -d '*' | sort | column
+
+# distribution
+
+FTP= www:www/ftp/lua/5.1
+F= http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/5.1/$A
+D= $(MYNAME)
+A= $(MYLIB).tar.gz
+TOTAR= Makefile,README,$(MYLIB).c,test.lua
+
+distr: clean
+ tar zcvf $A -C .. $D/{$(TOTAR)}
+ touch -r $A .stamp
+ scp -p $A $(FTP)
+
+diff: clean
+ wget -q -N $F
+ tar zxf $A
+ diff $D .
+
+# eof
29 README
@@ -0,0 +1,29 @@
+This code provides support for complex numbers in Lua using the functions
+available in C99.
+
+To try the library, just edit Makefile to reflect your installation of Lua and
+then run make. This will build the library and run a simple test. For detailed
+installation instructions, see
+ http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/install.html
+
+There is no manual: see the summary below and a C99 reference manual, e.g.
+http://www.dinkumware.com/manuals/default.aspx?manual=compleat&page=complex2.html
+
+Read also test.lua, which shows the library in action.
+
+This code is hereby placed in the public domain.
+
+Please send comments, suggestions, and bug reports to lhf@tecgraf.puc-rio.br .
+
+-------------------------------------------------------------------------------
+
+complex library:
+ __add(z,w) __unm(z) atan(z) log(z) sqrt(z)
+ __div(z,w) abs(z) atanh(z) new(x,y) tan(z)
+ __eq(z,w) acos(z) conj(z) pow(z,w) tanh(z)
+ __mul(z,w) acosh(z) cos(z) proj(z) tostring(z)
+ __pow(z,w) arg(z) cosh(z) real(z) version
+ __sub(z,w) asin(z) exp(z) sin(z) I
+ __tostring(z) asinh(z) imag(z) sinh(z)
+
+-------------------------------------------------------------------------------
130 dist.cmake
@@ -0,0 +1,130 @@
+# LuaDist CMake utility library.
+# Provides variables and utility functions common to LuaDist CMake builds.
+#
+# Copyright (C) 2007-2010 LuaDist.
+# by David Manura, Peter Drahos
+# Redistribution and use of this file is allowed according to the terms of the MIT license.
+# For details see the COPYRIGHT file distributed with LuaDist.
+# Please note that the package source code is licensed under its own license.
+
+# Few convinence settings
+SET (CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS true)
+SET (CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_MODULE_PATH})
+
+# Where to install module parts:
+set(INSTALL_BIN bin CACHE PATH "Where to install binaries to.")
+set(INSTALL_LIB lib CACHE PATH "Where to install libraries to.")
+set(INSTALL_INC include CACHE PATH "Where to install headers to.")
+set(INSTALL_ETC etc CACHE PATH "Where to store configuration files")
+set(INSTALL_LMOD share/lua/lmod CACHE PATH "Directory to install Lua modules.")
+set(INSTALL_CMOD share/lua/cmod CACHE PATH "Directory to install Lua binary modules.")
+set(INSTALL_DATA share/${PROJECT_NAME} CACHE PATH "Directory the package can store documentation, tests or other data in.")
+set(INSTALL_DOC ${INSTALL_DATA}/doc CACHE PATH "Recommended directory to install documentation into.")
+set(INSTALL_EXAMPLE ${INSTALL_DATA}/example CACHE PATH "Recommended directory to install examples into.")
+set(INSTALL_TEST ${INSTALL_DATA}/test CACHE PATH "Recommended directory to install tests into.")
+set(INSTALL_FOO ${INSTALL_DATA}/etc CACHE PATH "Where to install additional files")
+
+
+# In MSVC, prevent warnings that can occur when using standard libraries.
+if(MSVC)
+ add_definitions(-D_CRT_SECURE_NO_WARNINGS)
+endif(MSVC)
+
+# Adds Lua shared library module target `_target`.
+# Additional sources to build the module are listed after `_target`.
+macro(add_lua_module _target)
+ find_package(Lua51 REQUIRED)
+ include_directories(${LUA_INCLUDE_DIR}) #2DO: somehow apply only to _target?
+
+ add_library(${_target} MODULE ${ARGN})
+ set_target_properties(${_target} PROPERTIES PREFIX "")
+ target_link_libraries(${_target} ${LUA_LIBRARY})
+
+ IF(WIN32)
+ set_target_properties(${_target} PROPERTIES LINK_FLAGS "-Wl,--enable-auto-import")
+ ENDIF()
+
+endmacro(add_lua_module)
+
+# Runs Lua script `_testfile` under CTest tester.
+# Optional argument `_testcurrentdir` is current working directory to run test under
+# (defaults to ${CMAKE_CURRENT_BINARY_DIR}).
+# Both paths, if relative, are relative to ${CMAKE_CURRENT_SOURCE_DIR}.
+# Under LuaDist, set test=true in config.lua to enable testing.
+macro(add_lua_test _testfile)
+ include(CTest)
+ if(BUILD_TESTING)
+ find_program(LUA NAMES lua lua.bat)
+ get_filename_component(TESTFILEABS ${_testfile} ABSOLUTE)
+ get_filename_component(TESTFILENAME ${_testfile} NAME)
+ get_filename_component(TESTFILEBASE ${_testfile} NAME_WE)
+
+ # Write wrapper script.
+ set(TESTWRAPPER ${CMAKE_CURRENT_BINARY_DIR}/${TESTFILENAME})
+ set(TESTWRAPPERSOURCE
+"package.path = '${CMAKE_CURRENT_BINARY_DIR}/?.lua\;${CMAKE_CURRENT_SOURCE_DIR}/?.lua\;' .. package.path
+package.cpath = '${CMAKE_CURRENT_BINARY_DIR}/?.so\;${CMAKE_CURRENT_BINARY_DIR}/?.dll\;' .. package.cpath
+return dofile '${TESTFILEABS}'
+" )
+ if(${ARGC} GREATER 1)
+ set(_testcurrentdir ${ARGV1})
+ get_filename_component(TESTCURRENTDIRABS ${_testcurrentdir} ABSOLUTE)
+ set(TESTWRAPPERSOURCE
+"require 'lfs'
+lfs.chdir('${TESTCURRENTDIRABS}')
+${TESTWRAPPERSOURCE}")
+ endif()
+ FILE(WRITE ${TESTWRAPPER} ${TESTWRAPPERSOURCE})
+
+ add_test(${TESTFILEBASE} ${LUA} ${TESTWRAPPER})
+ endif(BUILD_TESTING)
+
+ # see also http://gdcm.svn.sourceforge.net/viewvc/gdcm/Sandbox/CMakeModules/UsePythonTest.cmake
+endmacro(add_lua_test)
+
+# Converts Lua source file `_source` to binary string embedded in C source
+# file `_target`. Optionally compiles Lua source to byte code (not available
+# under LuaJIT2, which doesn't have a bytecode loader). Additionally, Lua
+# versions of bin2c [1] and luac [2] may be passed respectively as additional
+# arguments.
+#
+# [1] http://lua-users.org/wiki/BinToCee
+# [2] http://lua-users.org/wiki/LuaCompilerInLua
+function(add_lua_bin2c _target _source)
+ find_program(LUA NAMES lua lua.bat)
+ execute_process(COMMAND ${LUA} -e "string.dump(function()end)" RESULT_VARIABLE _LUA_DUMP_RESULT ERROR_QUIET)
+ if (NOT ${_LUA_DUMP_RESULT})
+ SET(HAVE_LUA_DUMP true)
+ endif()
+ message("-- string.dump=${HAVE_LUA_DUMP}")
+
+ if (ARGV2)
+ get_filename_component(BIN2C ${ARGV2} ABSOLUTE)
+ set(BIN2C ${LUA} ${BIN2C})
+ else()
+ find_program(BIN2C NAMES bin2c bin2c.bat)
+ endif()
+ if (HAVE_LUA_DUMP)
+ if (ARGV3)
+ get_filename_component(LUAC ${ARGV3} ABSOLUTE)
+ set(LUAC ${LUA} ${LUAC})
+ else()
+ find_program(LUAC NAMES luac luac.bat)
+ endif()
+ endif (HAVE_LUA_DUMP)
+ message("-- bin2c=${BIN2C}")
+ message("-- luac=${LUAC}")
+
+ get_filename_component(SOURCEABS ${_source} ABSOLUTE)
+ if (HAVE_LUA_DUMP)
+ get_filename_component(SOURCEBASE ${_source} NAME_WE)
+ add_custom_command(
+ OUTPUT ${_target} DEPENDS ${_source}
+ COMMAND ${LUAC} -o ${CMAKE_CURRENT_BINARY_DIR}/${SOURCEBASE}.lo ${SOURCEABS}
+ COMMAND ${BIN2C} ${CMAKE_CURRENT_BINARY_DIR}/${SOURCEBASE}.lo ">${_target}" )
+ else()
+ add_custom_command(
+ OUTPUT ${_target} DEPENDS ${SOURCEABS}
+ COMMAND ${BIN2C} ${_source} ">${_target}" )
+ endif()
+endfunction(add_lua_bin2c)
19 dist.info
@@ -0,0 +1,19 @@
+--- This file is part of LuaDist project
+--note: Conflicts with "matrix" package, which has "complex.lua" in LMOD !!!
+
+name = "lclomplex"
+version = "5.1"
+
+desc = "Provides support for complex numbers in Lua using the functions available in C99."
+author = "Luiz Henrique de Figueiredo"
+license = "Public Domain"
+maintainer = "Peter Kapec"
+url = "http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/"
+
+depends = {
+ "lua ~> 5.1"
+}
+
+conflicts = {
+ "matrix"
+}
174 lcomplex.c
@@ -0,0 +1,174 @@
+/*
+* lcomplex.c
+* C99 complex nummbers for Lua
+* Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br>
+* 02 Nov 2009 23:15:43
+* This code is hereby placed in the public domain.
+*/
+
+#include <complex.h>
+#define Complex double complex
+
+#include "lua.h"
+#include "lauxlib.h"
+
+#define MYNAME "complex"
+#define MYTYPE MYNAME " number"
+#define MYVERSION MYTYPE " library for " LUA_VERSION " / Nov 2009"
+
+#define Z(i) Pget(L,i)
+#define O(i) luaL_optnumber(L,i,0)
+
+#define cadd(z,w) ((z)+(w))
+#define csub(z,w) ((z)-(w))
+#define cmul(z,w) ((z)*(w))
+#define cdiv(z,w) ((z)/(w))
+#define cneg(z) (-(z))
+#define cconj conj
+
+static Complex Pget(lua_State *L, int i)
+{
+ switch (lua_type(L,i))
+ {
+ case LUA_TNUMBER:
+ case LUA_TSTRING:
+ return luaL_checknumber(L,i);
+ default:
+ return *((Complex*)luaL_checkudata(L,i,MYTYPE));
+ }
+}
+
+static int pushcomplex(lua_State *L, Complex z)
+{
+ Complex *p=lua_newuserdata(L,sizeof(Complex));
+ *p=z;
+ luaL_getmetatable(L,MYTYPE);
+ lua_setmetatable(L,-2);
+ return 1;
+}
+
+static int Leq(lua_State *L) /** __eq(z,w) */
+{
+ lua_pushboolean(L,Z(1)==Z(2));
+ return 1;
+}
+
+static int Ltostring(lua_State *L) /** tostring(z) */
+{
+ Complex z=Z(1);
+ double x=creal(z);
+ double y=cimag(z);
+ lua_settop(L,0);
+ if (x!=0) lua_pushnumber(L,x);
+ if (y!=0)
+ {
+ if (y==1)
+ {
+ if (x!=0) lua_pushliteral(L,"+");
+ }
+ else if (y==-1)
+ lua_pushliteral(L,"-");
+ else
+ {
+ if (y>0 && x!=0) lua_pushliteral(L,"+");
+ lua_pushnumber(L,y);
+ }
+ lua_pushliteral(L,"i");
+ }
+ lua_concat(L,lua_gettop(L));
+ return 1;
+}
+
+#define A(f,e) static int L##f(lua_State *L) { return pushcomplex(L,e); }
+#define B(f) A(f,c##f(Z(1),Z(2)))
+#define F(f) A(f,c##f(Z(1)))
+#define G(f) static int L##f(lua_State *L) { lua_pushnumber(L,c##f(Z(1))); return 1; }
+
+A(new,O(1)+O(2)*I) /** new(x,y) */
+B(add) /** __add(z,w) */
+B(div) /** __div(z,w) */
+B(mul) /** __mul(z,w) */
+B(sub) /** __sub(z,w) */
+F(neg) /** __unm(z) */
+G(abs) /** abs(z) */
+F(acos) /** acos(z) */
+F(acosh) /** acosh(z) */
+G(arg) /** arg(z) */
+F(asin) /** asin(z) */
+F(asinh) /** asinh(z) */
+F(atan) /** atan(z) */
+F(atanh) /** atanh(z) */
+F(conj) /** conj(z) */
+F(cos) /** cos(z) */
+F(cosh) /** cosh(z) */
+F(exp) /** exp(z) */
+G(imag) /** imag(z) */
+F(log) /** log(z) */
+B(pow) /** pow(z,w) */
+F(proj) /** proj(z) */
+G(real) /** real(z) */
+F(sin) /** sin(z) */
+F(sinh) /** sinh(z) */
+F(sqrt) /** sqrt(z) */
+F(tan) /** tan(z) */
+F(tanh) /** tanh(z) */
+
+static const luaL_Reg R[] =
+{
+ { "__add", Ladd },
+ { "__div", Ldiv },
+ { "__eq", Leq },
+ { "__mul", Lmul },
+ { "__sub", Lsub },
+ { "__unm", Lneg },
+ { "abs", Labs },
+ { "acos", Lacos },
+ { "acosh", Lacosh },
+ { "arg", Larg },
+ { "asin", Lasin },
+ { "asinh", Lasinh },
+ { "atan", Latan },
+ { "atanh", Latanh },
+ { "conj", Lconj },
+ { "cos", Lcos },
+ { "cosh", Lcosh },
+ { "exp", Lexp },
+ { "imag", Limag },
+ { "log", Llog },
+ { "new", Lnew },
+ { "pow", Lpow },
+ { "proj", Lproj },
+ { "real", Lreal },
+ { "sin", Lsin },
+ { "sinh", Lsinh },
+ { "sqrt", Lsqrt },
+ { "tan", Ltan },
+ { "tanh", Ltanh },
+ { "tostring", Ltostring},
+ { NULL, NULL }
+};
+
+LUALIB_API int luaopen_complex(lua_State *L)
+{
+ luaL_newmetatable(L,MYTYPE);
+ lua_setglobal(L,MYNAME);
+ luaL_register(L,MYNAME,R);
+ lua_pushliteral(L,"version"); /** version */
+ lua_pushliteral(L,MYVERSION);
+ lua_settable(L,-3);
+ lua_pushliteral(L,"__index");
+ lua_pushvalue(L,-2);
+ lua_settable(L,-3);
+ lua_pushliteral(L,"I"); /** I */
+ pushcomplex(L,I);
+ lua_settable(L,-3);
+ lua_pushliteral(L,"__pow"); /** __pow(z,w) */
+ lua_pushliteral(L,"pow");
+ lua_gettable(L,-3);
+ lua_settable(L,-3);
+ lua_pushliteral(L,"__tostring"); /** __tostring(z) */
+ lua_pushliteral(L,"tostring");
+ lua_gettable(L,-3);
+ lua_settable(L,-3);
+ return 1;
+}
41 test.lua
@@ -0,0 +1,41 @@
+-- test complex library
+
+------------------------------------------------------------------------------
+require"complex"
+
+print(complex.version)
+
+z=complex.new(3,4)
+print(z,"ABS",z:abs())
+print(z,"CONJ",z:conj())
+print(z,"CABS",z:conj():abs())
+print(z,"NEG",-z)
+
+z=complex.new(0,math.pi)
+print(z,"EXP",z:exp())
+
+z=complex.new(0,2*math.pi/3)
+print(z,"EXP",z:exp(),(2*z:exp():imag())^2)
+
+--I=complex.new(0,1)
+I=complex.I
+
+z=1
+for n=0,7 do
+ print(n,"MUL",z,"POW",I^n)
+ z=z*I
+end
+
+z=3+4*I
+print(z,"ABS",z:abs())
+print(z,"ARG",z:arg())
+print(z,"SQRT",z:sqrt())
+z=2+I
+print(z,"SQR",z^2)
+print(z,"CUB",z^3)
+
+z=2+11*I
+print(z,"CBRT",z^(1/3))
+
+print(complex.version)
+------------------------------------------------------------------------------

0 comments on commit 8463d86

Please sign in to comment.
Something went wrong with that request. Please try again.