Advertisement
LDDestroier

CC Linear Algebraic Solve Function

May 8th, 2017
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.34 KB | None | 0 0
  1. -- pastebin get bs4QNDyS solve
  2.  
  3. local strins = function(str,ins,pos)
  4.     return str:sub(1,pos-1)..tostring(ins)..str:sub(pos)
  5. end
  6.  
  7. --syntax:
  8. --solve(sting leftside, string rightside, string variablename, number maxiterations)
  9.  
  10. solve = function(left,right,varname,maxIterations) -- left part of equation, right part of equation. got it memorized?
  11.     local step, output, flips, diff, olddiff = 2^64, 0, 0
  12.     varname = varname or "x"
  13.     maxIterations = tonumber(maxIterations) or 2^16
  14.     local try = function()
  15.         local Ls,L = pcall( function() return  loadstring("return "..left:gsub("[%+%-]"..varname,function(s) return strins(s," ",2) end):gsub(varname,"("..output..")"))() end )
  16.         local Rs,R = pcall( function() return loadstring("return "..right:gsub("[%+%-]"..varname,function(s) return strins(s," ",2) end):gsub(varname,"("..output..")"))() end )
  17.         assert(type(L) == "number", "malformed equation (LEFT)")
  18.         assert(type(R) == "number", "malformed equation (RIGHT)")
  19.         return L - R, L, R
  20.     end
  21.     assert(type(left) == "string", "left equation must be string")
  22.     assert(type(right) == "string", "right equation must be string")
  23.     assert(type(varname) == "string", "variable name must be string")
  24.     assert(type(maxIterations) == "number", "max iterations must be number")
  25.     for a = 1, maxIterations do
  26.         olddiff = diff or 0
  27.         diff = try()
  28.         if diff == 0 then
  29.             return output, true
  30.         else
  31.             if math.abs(diff) > math.abs(olddiff) then
  32.                 if a == 1 then
  33.                     step = step * -1
  34.                 else
  35.                     step = step * -0.5
  36.                 end
  37.                 flips = flips + 1
  38.             end
  39.             output = output + step
  40.         end
  41.     end
  42.     if (flips <= 1) then
  43.         return math.huge%1, false --not a number, false
  44.     else
  45.         return output, false
  46.     end
  47. end
  48.  
  49.  
  50. --syntax:
  51. --strsolve(string equation, string variablename)
  52.  
  53. strsolve = function(equation,variable)
  54.     assert(type(equation) == "string", "equation must be string")
  55.     variable = variable or "x"
  56.     assert(equation:find("="), "malformed equation (NO '=')")
  57.     equation = equation:gsub("%d+["..variable.."]",function(str)
  58.         return str:sub( 1,str:find(variable)-1 ).."*"..variable
  59.     end):gsub("%)%(","%)%*%("):gsub("[%d("..variable..")]%(",function(s) return strins(s,"*",2) end)
  60.     return solve(equation:sub(1,equation:find("=")-1),equation:sub(equation:find("=")+1),variable)
  61. end
  62.  
  63. local tArg = {...}
  64. if tArg[1] then
  65.     print(strsolve(table.concat(tArg,""),"x"),"")
  66. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement