使用 lpeg (luatex) 从方程 $ax^2+bx+c=0$ 中收集数字 a、b 和 c

使用 lpeg (luatex) 从方程 $ax^2+bx+c=0$ 中收集数字 a、b 和 c

这是我第一次尝试用创建宏。这里是我的代码,用于解决二次方程的 luatex\solveqax^2+bx+c=0

\documentclass[12pt]{article}
\usepackage{amssymb}
\usepackage{luacode}


\begin{luacode*}
function solve_equa(equa)
local a_here= string.find (equa,"x^2")
local b_here= string.find (equa,"x[^^]")
local c_here= string.find (equa,"x[-+](.-)=")

local a = string.match(equa,"%$(.-)x^2") or 0
local b = (b_here ~=nil) and  ((a_here ~=nil) and  string.match(equa,"x^2(.-)x") or string.match(equa,"%$(.-)x")) or 0
local c = (c_here ~=nil) and  ((a_here ~=nil) and  ((b_here ~=nil) and string.match(equa,"%b^x(.-)=") or string.match(equa,"%x^2(.-)="))  or
string.match(equa,"x(.-)=")) or 0

if a=="" or a=="+" then
 a=1
elseif a=="-" then
 a=-1
end

if b=="" or b=="+" then
 b=1
elseif b=="-" then
 b=-1
end

if a==0 then
local  temp =  (-c/b == -0) and 0 or -c/b
       solution = "x = " .. temp
else

D = b*b-4*a*c

if D==0 then
       solution = "$x = " ..  -b/2/a .. "$"
else if D>0 then
       solution  = "$x1 = " .. (-b+math.sqrt(D))/2/a .. "\\quad  x2 =" .. (-b-math.sqrt(D))/2/a ..  "$"
     else
       solution  =[[no solution in $\mathbb{R}$]]
     end
end
end
return solution
end

\end{luacode*}

\def\solveq#1{solution of equation $#1$:\par
{\centering \directlua{solve_equa("$#1$") tex.print(solution)}\par}}

\begin{document}

\solveq{-x^2+5x+4=0}

\end{document}

我希望用lpeg功能

答案1

下面我介绍一种 LPEG 语法,它可以满足您的要求,但重点在于简单性。对于更复杂的解析,您需要更复杂的解析器。您可能想看看,也许可以调整我这里的项目中的解析器:https://github.com/hmenke/boost_matheval/blob/master/include/matheval.lua

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{luacode}

\begin{luacode*}
local lpeg = require"lpeg"
local C, P, S = lpeg.C, lpeg.P, lpeg.S

local sgn = { [""] = 1, ["+"] = 1, ["-"] = -1 }

local w     = S" \t"^0
local eq    = P"="
local var   = P"x"
local pm    = C(S"+-"^0) / sgn
local num   = C((1 - var - eq)^0) / function(c) return c == "" and 1 or tonumber(c) end
local coeff = pm * w * num / function(a,b) return a * b end

local grammar = (
    w * coeff * w * var * P"^2" *
    w * coeff * w * var *
    w * coeff *
    w * eq *
    w * coeff
)

local parse = function(exp)
    return grammar:match(exp)
end

solve = function(exp)
    local a, b, c, d = parse(exp)
    c = c - d

    local D = b^2 - 4*a*c

    if D < 0 then
        tex.sprint("\\[\\text{no solution in $\\mathbb{R}$}\\]")
        return
    end

    if D == 0 then
        tex.sprint("\\[x_1 = " .. -b/(2*a) .."\\]")
        return
    end

    local x1 = (-b + math.sqrt(D))/(2*a)
    local x2 = (-b - math.sqrt(D))/(2*a)
    tex.sprint("\\[x_1 = " .. x1 .. " \\quad x_2 = " .. x2 .. "\\]")
end
\end{luacode*}

\def\solveq#1{\[ #1 \] has the following solutions: \directlua{solve("\luaescapestring{#1}")}}

\begin{document}

\solveq{-x^2+5x+4=0}

\solveq{- x^2 + 3 x + 2 = 2}

\solveq{x^2+ x +1=0}

\solveq{-13x^2+0x+1=0}

\end{document}

在此处输入图片描述

相关内容