Skip to content

Instantly share code, notes, and snippets.

View Yonaba's full-sized avatar
💭
I may be slow to respond.

Roland Yonaba

💭
I may be slow to respond.
View GitHub Profile
@Yonaba
Yonaba / newtonraphson.lua
Last active December 28, 2015 04:49
Newton Raphson Solver
local DOUBLE_IEEE_ACCURACY = 1e-8
local function areFuzzyEqual(a, b, accuracy)
return (math.abs(a-b) < (accuracy or DOUBLE_IEEE_ACCURACY))
end
local function derivative(f, x0, accuracy)
local h = 0.1
local xL, xR = x0 - h, x0 + h
local tAccr = (f(xR) - f(xL))/(2*h)
@Yonaba
Yonaba / IUPMessageExample.lua
Last active December 26, 2015 20:39
IUP Message Example
print(package.path:gsub(';','\n'))
require ('iuplua')
iup.Message('Hello', 'Hello from IUPLua')
@Yonaba
Yonaba / InventoryClassExample.lua
Created September 13, 2013 11:52
Plain Lua Inventory Class Example from MoonScript reference (http://moonscript.org/reference/#object_oriented_programming)
local Inventory = {}
Inventory.__index = Inventory
function Inventory:new()
local new_inventory = {}
new_inventory.items = {}
setmetatable(new_inventory, Inventory)
return new_inventory
end
@Yonaba
Yonaba / nextvsipairs.lua
Created September 13, 2013 09:55
Next Iterator versus Ipairs for iteration in Lua arrays.
-- Evaluates times
local function time(nm,times, f,...)
local st = os.clock()
for i = 1, times do
f(...)
end
local et = os.clock()
print(('(%s): Times (%d): %.2f ms')
:format(nm, times, (et-st)*1000))
end
@Yonaba
Yonaba / subvsmatch.lua
Last active December 22, 2015 23:39
string.gmatch vs string.sub
-- Calls f(...) n-times
local function time(nm,times, f,...)
local st = os.clock()
for i = 1, times do
f(...)
end
local et = os.clock()
print(('(%s): Times (%d): %.2f ms')
:format(nm, times, (et-st)*1000))
end
@Yonaba
Yonaba / Jumper_Stress_Test.lua
Last active December 11, 2015 21:48
A stress test for the pathfinding library Jumper (v1.8.1)
-- A stress test for the pathfinding library Jumper (v1.8.1)
-- Source: https://github.com/Yonaba/Jumper/tree/jumper-1.8.1-1
-- The purpose of this is to compare the relative performance of different
-- search algorithms implemented in Jumper. Time measurements uses Lua's
-- native os.clock with (milliseconds precision).
-- Library setup
local Grid = require 'jumper.grid'
@Yonaba
Yonaba / astar.lua
Created June 29, 2011 16:23
A-Star Pathfinding
--[[Improved A-Star Pathfinding Algorithm
Ported to by Roland Yonaba - aka SeanPaul223
28/01/2010]]
--Looks for a node listed in a table of nodes.
--Returns true on success, plus the index where it was found.
--Otherwise returns false plus nil.
function isListed(list,node)
local bool,pos
bool = false
@Yonaba
Yonaba / gausseidel.lua
Created June 29, 2011 16:21
Gauss-Seidel Algorithm
-- Gauss Seidel Algorithm implemented through lua
function Norm(m)
local n = 0
for i,v in ipairs(m) do
n = n + v^2
end
return math.sqrt(n)
end
@Yonaba
Yonaba / sor2.lua
Created June 29, 2011 16:20
Successive Over Relaxation (SOR) Algorithm : 2nd Approach
-- SOR Algorithm Implemented through Lua
function Norm(m)
local n = 0
for i,v in ipairs(m) do
n = n + v^2
end
return math.sqrt(n)
end
@Yonaba
Yonaba / sor.lua
Created June 29, 2011 16:18
Successive Over Relaxation (SOR) Algorithm
-- SOR Algorithm implemented through Lua
function Normalize(m)
local n = 0
for i,v in ipairs(m) do
n = n + v^2
end
return math.sqrt(n)
end