| 1 |
% The luatruthtable package |
| 2 |
% Authors: Chetan Shirore and Ajit Kumar |
| 3 |
% Version 1.3, Date=17-Nov-2023 |
| 4 |
% Licensed under LaTeX Project Public License v1.3c or later. The complete license text is available at http://www.latex-project.org/lppl.txt. |
| 5 |
|
| 6 |
\ProvidesPackage{luatruthtable}[1.3] |
| 7 |
\RequirePackage{xkeyval} |
| 8 |
\RequirePackage{amsmath} |
| 9 |
\RequirePackage{luacode} |
| 10 |
\begin{luacode*} |
| 11 |
-- Function to convert decimal number to binary number. |
| 12 |
local function toBinary(x,y) |
| 13 |
y = y or math.max(1, select(2, math.frexp(x))) |
| 14 |
local res = {} |
| 15 |
for i = y, 1, -1 do |
| 16 |
res[i] = math.fmod(x, 2) |
| 17 |
x = math.floor((x - res[i]) / 2) |
| 18 |
end |
| 19 |
return res |
| 20 |
end |
| 21 |
-- Define logical operators. |
| 22 |
local function _not(a) |
| 23 |
if a ==0 then return 1 |
| 24 |
else return 0 end |
| 25 |
end |
| 26 |
|
| 27 |
local lognot = {} |
| 28 |
|
| 29 |
local not_mt= { |
| 30 |
__mul = function(a,b) |
| 31 |
return _not(b) |
| 32 |
|
| 33 |
end |
| 34 |
} |
| 35 |
|
| 36 |
setmetatable(lognot, not_mt) |
| 37 |
_G.lognot = lognot |
| 38 |
|
| 39 |
local function _and(a, b) |
| 40 |
if a == 1 and b == 1 then return 1 |
| 41 |
else return 0 end |
| 42 |
end |
| 43 |
|
| 44 |
local tmp1 = {} |
| 45 |
local logand = {} |
| 46 |
local v1 = 0 |
| 47 |
|
| 48 |
local and_mt= { |
| 49 |
__mul = function(a,b) |
| 50 |
if b==logand then |
| 51 |
v1 = v1 + 1 |
| 52 |
tmp1[v1]=a |
| 53 |
return tmp1 |
| 54 |
elseif a == tmp1 then |
| 55 |
local w1 = _and(tmp1[v1], b) |
| 56 |
v1 = v1 - 1 |
| 57 |
return w1 |
| 58 |
end |
| 59 |
end |
| 60 |
} |
| 61 |
|
| 62 |
setmetatable(tmp1, and_mt) |
| 63 |
setmetatable(logand, and_mt) |
| 64 |
_G.logand = logand |
| 65 |
|
| 66 |
local function _or(a, b) |
| 67 |
if a == 0 and b == 0 then return 0 |
| 68 |
else return 1 end |
| 69 |
end |
| 70 |
|
| 71 |
local tmp2 = {} |
| 72 |
local logor = {} |
| 73 |
local v2 = 0 |
| 74 |
|
| 75 |
local or_mt= { |
| 76 |
__mul = function(a,b) |
| 77 |
if b==logor then |
| 78 |
v2 = v2 + 1 |
| 79 |
tmp2[v2] = a |
| 80 |
return tmp2 |
| 81 |
elseif a == tmp2 then |
| 82 |
local w2 = _or(tmp2[v2], b) |
| 83 |
v2 = v2 - 1 |
| 84 |
return w2 |
| 85 |
end |
| 86 |
end |
| 87 |
} |
| 88 |
|
| 89 |
setmetatable(tmp2, or_mt) |
| 90 |
setmetatable(logor, or_mt) |
| 91 |
_G.logor = logor |
| 92 |
|
| 93 |
local function _imp(a, b) |
| 94 |
if a == 1 and b == 0 then return 0 |
| 95 |
else return 1 end |
| 96 |
end |
| 97 |
|
| 98 |
local tmp3 = {} |
| 99 |
local imp = {} |
| 100 |
local v3 = 0 |
| 101 |
|
| 102 |
local imp_mt= { |
| 103 |
__mul = function(a,b) |
| 104 |
if b==imp then |
| 105 |
v3 = v3 + 1 |
| 106 |
tmp3[v3]=a |
| 107 |
return tmp3 |
| 108 |
elseif a == tmp3 then |
| 109 |
local w3 = _imp(tmp3[v3], b) |
| 110 |
v3 = v3 - 1 |
| 111 |
return w3 |
| 112 |
end |
| 113 |
end |
| 114 |
} |
| 115 |
|
| 116 |
setmetatable(tmp3, imp_mt) |
| 117 |
setmetatable(imp, imp_mt) |
| 118 |
_G.imp = imp |
| 119 |
|
| 120 |
local function _iff(a, b) |
| 121 |
if a == b then return 1 |
| 122 |
else return 0 end |
| 123 |
end |
| 124 |
|
| 125 |
local tmp4 = {} |
| 126 |
local iff = {} |
| 127 |
local v4 = 0 |
| 128 |
|
| 129 |
local iff_mt= { |
| 130 |
__mul = function(a,b) |
| 131 |
if b==iff then |
| 132 |
v4 = v4 + 1 |
| 133 |
tmp4[v4]=a |
| 134 |
return tmp4 |
| 135 |
elseif a == tmp4 then |
| 136 |
local w4 = _iff(tmp4[v4], b) |
| 137 |
v4 = v4 - 1 |
| 138 |
return w4 |
| 139 |
end |
| 140 |
end |
| 141 |
} |
| 142 |
|
| 143 |
setmetatable(tmp4, iff_mt) |
| 144 |
setmetatable(iff, iff_mt) |
| 145 |
_G.iff = iff |
| 146 |
|
| 147 |
local function _xor(a, b) |
| 148 |
if a ~= b then return 1 |
| 149 |
else return 0 end |
| 150 |
end |
| 151 |
|
| 152 |
local tmp5 = {} |
| 153 |
local logxor = {} |
| 154 |
local v5 = 0 |
| 155 |
|
| 156 |
local xor_mt= { |
| 157 |
__mul = function(a,b) |
| 158 |
if b==logxor then |
| 159 |
v5 = v5 + 1 |
| 160 |
tmp5[v5]=a |
| 161 |
return tmp5 |
| 162 |
elseif a == tmp5 then |
| 163 |
local w5 = _xor(tmp5[v5], b) |
| 164 |
v5 = v5 - 1 |
| 165 |
return w5 |
| 166 |
end |
| 167 |
end |
| 168 |
} |
| 169 |
|
| 170 |
setmetatable(tmp5, xor_mt) |
| 171 |
setmetatable(logxor, xor_mt) |
| 172 |
_G.logxor = logxor |
| 173 |
|
| 174 |
local function _nand(a, b) |
| 175 |
if a ==1 and b == 1 then return 0 |
| 176 |
else return 1 end |
| 177 |
end |
| 178 |
|
| 179 |
local tmp6 = {} |
| 180 |
local lognand = {} |
| 181 |
local v6 = 0 |
| 182 |
|
| 183 |
local nand_mt= { |
| 184 |
__mul = function(a,b) |
| 185 |
if b==lognand then |
| 186 |
v6 = v6 + 1 |
| 187 |
tmp6[v6]=a |
| 188 |
return tmp6 |
| 189 |
elseif a == tmp6 then |
| 190 |
local w6 = _nand(tmp6[v6], b) |
| 191 |
v6 = v6 - 1 |
| 192 |
return w6 |
| 193 |
end |
| 194 |
end |
| 195 |
} |
| 196 |
|
| 197 |
setmetatable(tmp6, nand_mt) |
| 198 |
setmetatable(lognand, nand_mt) |
| 199 |
_G.lognand = lognand |
| 200 |
|
| 201 |
local function _nor(a, b) |
| 202 |
if a ==0 and b == 0 then return 1 |
| 203 |
else return 0 end |
| 204 |
end |
| 205 |
|
| 206 |
local tmp7 = {} |
| 207 |
local lognor = {} |
| 208 |
local v7 = 0 |
| 209 |
|
| 210 |
local nor_mt= { |
| 211 |
__mul = function(a,b) |
| 212 |
if b==lognor then |
| 213 |
v7 = v7 + 1 |
| 214 |
tmp7[v7]=a |
| 215 |
return tmp7 |
| 216 |
elseif a == tmp7 then |
| 217 |
local w7 = _nor(tmp7[v7], b) |
| 218 |
v7 = v7 - 1 |
| 219 |
return w7 |
| 220 |
end |
| 221 |
end |
| 222 |
} |
| 223 |
|
| 224 |
setmetatable(tmp7, nor_mt) |
| 225 |
setmetatable(lognor, nor_mt) |
| 226 |
_G.lognor = lognor |
| 227 |
|
| 228 |
local function _xnor(a, b) |
| 229 |
if a == b then return 1 |
| 230 |
else return 0 end |
| 231 |
end |
| 232 |
|
| 233 |
local tmp8 = {} |
| 234 |
local logxnor = {} |
| 235 |
local v8 = 0 |
| 236 |
|
| 237 |
local xnor_mt= { |
| 238 |
__mul = function(a,b) |
| 239 |
if b==logxnor then |
| 240 |
v8 = v8 + 1 |
| 241 |
tmp8[v8]=a |
| 242 |
return tmp8 |
| 243 |
elseif a == tmp8 then |
| 244 |
local w8 = _xnor(tmp8[v8], b) |
| 245 |
v8 = v8 - 1 |
| 246 |
return w8 |
| 247 |
end |
| 248 |
end |
| 249 |
} |
| 250 |
|
| 251 |
setmetatable(tmp8, xnor_mt) |
| 252 |
setmetatable(logxnor, xnor_mt) |
| 253 |
_G.logxnor = logxnor |
| 254 |
|
| 255 |
function truthTable(str0,str,trtext,fltext) |
| 256 |
local eval="" |
| 257 |
local names={lognot=lognot,logand=logand,logor=logor, imp=imp, |
| 258 |
iff=iff, logxor=logxor,lognand=lognand,lognor=lognor, logxnor=logxnor} |
| 259 |
local vars={} |
| 260 |
local expr={} |
| 261 |
local countexp = 1 |
| 262 |
local countvars =1 |
| 263 |
local res="" |
| 264 |
local sep=" & " |
| 265 |
trtext = trtext or "$T$" |
| 266 |
fltext = fltext or "$F$" |
| 267 |
local str0 = string.gsub(str0, "%s+", "") |
| 268 |
local str = string.gsub(str, "%s+", "") |
| 269 |
for variables in string.gmatch(str0, '([^,]+)') do |
| 270 |
vars[countvars] = variables |
| 271 |
countvars = countvars + 1 |
| 272 |
end |
| 273 |
|
| 274 |
for subexp in string.gmatch(str, '([^,]+)') do |
| 275 |
expr[countexp] = subexp |
| 276 |
countexp = countexp + 1 |
| 277 |
end |
| 278 |
|
| 279 |
local n =#vars |
| 280 |
for i =1,2^n do |
| 281 |
itr = toBinary(i,n) |
| 282 |
for j=1, #itr do |
| 283 |
names[vars[j]] = itr[j] |
| 284 |
end |
| 285 |
for k = 1, #expr do |
| 286 |
res= res ..sep.. load("return " .. expr[k],nil,"t",names)() |
| 287 |
end |
| 288 |
if i~=2^n then |
| 289 |
eval = table.concat(toBinary(i,n)," & ") .. res .."\\\\" ..eval |
| 290 |
else |
| 291 |
eval = eval .. table.concat(toBinary(i,n)," & ") .. res |
| 292 |
end |
| 293 |
res="" |
| 294 |
end |
| 295 |
|
| 296 |
if trtext:gsub("%s+", "") =="0" or trtext:gsub("%s+", "") == "$0$" |
| 297 |
or fltext:gsub("%s+", "") =="1" or fltext:gsub("%s+", "") == "$1$" |
| 298 |
then |
| 299 |
eval=eval |
| 300 |
else |
| 301 |
eval=eval:gsub(0,fltext):gsub(1,trtext) |
| 302 |
end |
| 303 |
return eval |
| 304 |
end |
| 305 |
|
| 306 |
\end{luacode*} |
| 307 |
|
| 308 |
% ========= KEY DEFINITIONS ========= |
| 309 |
\define@key{luatruthtable}{trtext}{\def\luatrtbl@trtext{#1}}% |
| 310 |
\define@key{luatruthtable}{fltext}{\def\luatrtbl@fltext{#1}}% |
| 311 |
|
| 312 |
% ========= KEY DEFAULTS ========= |
| 313 |
\setkeys{luatruthtable}{trtext=$T$,fltext=$F$}% |
| 314 |
% ========= Defining Command ========= |
| 315 |
\newcommand{\luaTruthTable}[3][]{% |
| 316 |
\setkeys{luatruthtable}{#1}% |
| 317 |
\directlua{tex.sprint(truthTable(\luastringN{#2},\luastringN{#3},'\luatrtbl@trtext','\luatrtbl@fltext'))}} % |
| 318 |
|
| 319 |
\endinput |