是否有内置的Mathematica函数来查找运算符而不是方程中的数字?

Cet*_*ert 7 wolfram-mathematica equation-solving

如何在Mathematica中完成以下任务?

 In[1] := Solve[f[2,3]==5,f ? {Plus,Minus,Divide}]

Out[1] := Plus
Run Code Online (Sandbox Code Playgroud)

WRe*_*ach 8

可以将所需的表达式语法转换为一组Solve表达式:

fSolve[expr_, f_ ? functions_List] :=
  Map[Solve[(expr /. f -> #) && f == #, f] &, functions] // Flatten
Run Code Online (Sandbox Code Playgroud)

样品用途:

In[6]:= fSolve[f[2,3] == 5, f ? {Plus, Subtract, Divide}]
Out[6]= {f -> Plus}

In[7]:= fSolve[f[4,2] == 2, f ? {Plus, Subtract, Divide}]
Out[7]= {f -> Subtract, f -> Divide}
Run Code Online (Sandbox Code Playgroud)

这种方法的优点在于Solve可用于更复杂表达的遗留物的全部功能,例如

In[8]:= fSolve[D[f[x], x] < f[x], f ? {Log, Exp}]
Out[8]= {f -> ConditionalExpression[Log, x Log[x]?Reals && x>E^ProductLog[1]]}

In[9]:= fSolve[D[f[x], x] <= f[x], f ? {Log, Exp}]
Out[9]= {f -> ConditionalExpression[Log, x Log[x]?Reals && x>=E^ProductLog[1]],
         f -> ConditionalExpression[Exp, E^x ? Reals]}
Run Code Online (Sandbox Code Playgroud)

  • 也是非常好的解决方案,与标准Solve的良好集成 (2认同)

Mr.*_*ard 6

请告诉我这是否符合您的要求:

findFunction[expr_, head_ ? {ops__}] :=
    Quiet@Pick[{ops}, expr /. head -> # & /@ {ops}]

findFunction[f[2, 3] == 5, f ? {Plus, Minus, Divide}]
Run Code Online (Sandbox Code Playgroud)
(* Out[]= {Plus} *)

  • 顺便说一下,我知道答案是作为列表返回的,但是因为可以想象多个函数匹配,我认为它不应该只返回第一个函数. (2认同)