问题
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