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)
可以将所需的表达式语法转换为一组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)
请告诉我这是否符合您的要求:
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} *)