I would like to define some function $f$ that is odd, $f(-x)=-f(x)$, and can be used in symbolic computations. I am a bit more ambitious than this question and its answers. One way of achieving my goal is to use Mathematica's function OrderedQ to define

f[0] = 0;
f[x_] /; OrderedQ @ {x,-x} := -f[-x]

This does the job, giving e.g.

f /@ {-x, x-y, -x+y}
(* Out: {-f[x], -f[-x+y], f[-x+y]} *)

What I would really like, though, is to define an odd function $g$ that produces the aesthetically more pleasing

(* Out: {-g[x], g[x-y], -g[x-y]} *)

The reason for the mismatch must have to do with the internal ordering that Mathematica uses. How do I get a function yielding my preferred result?

NB. The above is adapted from Section 5.6 of Grozin's Introduction to Mathematica for Physicists, who instead sets

h[0] = 0;
h[x_] /; Not @ OrderedQ @ {-x,x} := -h[-x]

Like $f$ this yields

(* Out: {-h[x], -h[-x+y], h[-x+y]} *)
  • $\begingroup$ @MichaelE2: Thanks, I edited the text and renamed the second function to avoid possible confusion. Just out of curiosity, can you give me an example where $f$ and $h$ give different results? $\endgroup$ – Jules Lamers Sep 20 '16 at 13:40
  • $\begingroup$ It works now. I deleted my what I tried before. It could have been an error on my part, but I can't check. $\endgroup$ – Michael E2 Sep 20 '16 at 13:52

Maybe use Internal`SyntacticNegativeQ[]:

g[0] = 0;
g[x_] /; Internal`SyntacticNegativeQ[x] := -g[-x];

g /@ {-x, x - y, -x + y, -x - y, x + y, -2, 2, -2.}
(*  {-g[x], g[x - y], -g[x - y], -g[x + y], g[x + y], -g[2], g[2], -g[2.]}  *)

I'm not sure if it covers all use cases in the way desired, but it prevents the argument beginning with a minus sign. (It might not work on a negative symbolic constant, but I can't think of one. The constants Pi, E, and so forth are all positive.)

| improve this answer | |
  • $\begingroup$ That is wonderful! I was just trying to construct my own sort of Internal`SyntacticNegativeQ from CoefficientList. $\endgroup$ – Fred Simons Sep 20 '16 at 14:59
  • $\begingroup$ Thanks, that does seem to work and is elegant! Could you perhaps briefly explain what the function Internal`SyntacticNegativeQ does and why this works? $\endgroup$ – Jules Lamers Sep 20 '16 at 15:06
  • $\begingroup$ @JulesLamers What I know comes from the link I put in my answer, which is not a lot. It's an internal, undocumented function that seems to indicate whether the expression would start with a minus sign (in StandardForm, maybe). It works like this: If x starts with a minus, then negate it and return -g[-x]. $\endgroup$ – Michael E2 Sep 20 '16 at 15:15
  • 1
    $\begingroup$ This is cute-- You can evaluate g numerically, but not symbolically, over a symmetric interval: NIntegrate[g[x], {x, -2, 2}] evaluates to 0., but Integrate[g[x], {x, -2, 2}] returns unevaluated. $\endgroup$ – Michael E2 Sep 24 '16 at 13:58

Your definition of f forces that any argument z of f is the smallest of z and -z with respect to the default ordering. Therefore, with your definition of f, it is impossible to get an output f[x-y], since this automatically will evaluate to f[-x+y].

So I restricted your definition a bit and came to the following, not very elegant, solution:

f[ x_] /; Quiet[x[[1]]<0 || x[[1,1]]<0] := -f[Expand[-x]]


(* {-f[x],f[x-y],-f[x-y]} *)

I hope this also suits your needs. Otherwise, I think you have to use a different ordering in your definition of an odd function.

| improve this answer | |
  • $\begingroup$ Thanks, although as you say it is not very elegant, that does indeed seem to do what I want. Just to see how versatile this is: Does Mathematica have any other orderings available? And suppose I would like $x-y$ but $z-x$, is it hard to define one's own ordering where, say, $x$ comes before $y$ but after $z$? $\endgroup$ – Jules Lamers Sep 20 '16 at 14:25
  • $\begingroup$ This seems to be not so simple as I thought it was. Have a look at the documentation of MonomialList, where there is a survey of possible orderings. $\endgroup$ – Fred Simons Sep 20 '16 at 15:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.