How can I define a abstract odd function in mathematica?

你说的曾经没有我的故事 提交于 2020-01-04 01:52:32

问题


How can I define a abstract odd function, say f[x].

Whenever f[x]+f[-x] appears, mathematica simplifies it to zero.


回答1:


This can be done easily using upvalues

f[x_] + f[y_] /; x == -y ^:= 0

Normally Mathematica would try to assign the above rule to Plus, which of course does not work since that's protected. By using ^:= instead of := you can assign the rule to f. A quick check yields:

In[2]:=   f[3]+f[-3]
Out[2]:=  0

Edit: This, however, only works for Plus. It's probably better to use something more general, like:

f[x_?Negative] := -f[-x]

Now this also works with things like

In[4]:=  -f[3] - f[-3]
Out[4]:= 0

If you also want the function to work symbolically, you could add something like:

f[-a_] := -f[a]



回答2:


I am not good at this, but how about using the TransformationFunctions of Simplify ?

For example, suppose you have the expression 2 Sin[x] + f[x] + 3 + f[-x] + g[x] + g[-x] and you want to simplify it, assuming f[x] is odd function and g[x] is even. Then we want a rule to say f[x]+f[-x]->0 and a rule g[x]+g[-x]->2 g[x].

Hence write

myRules[e_]:=e/.f[x]+f[-x]->0/.g[x]+g[-x]->2 g[x]

Simplify[2 Sin[x]+ f[x]+ 3 +f[-x]+ g[x] + g[-x],
         TransformationFunctions->{Automatic,myRules}]

and this gives

3+2 g[x]+2 Sin[x]

Btw, in the above, I am using f[x] where it really should be a pattern f[x_], so that expression such as f[anything]+f[-anything] will also become zero. So, this needs to be improved to make myRules more general. Now it only works for the exact expression f[x]. I am not sure now how to improve this. Might need a delayed rule or so. Will think about it more. But you get the idea I hope.



来源:https://stackoverflow.com/questions/13342237/how-can-i-define-a-abstract-odd-function-in-mathematica

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!