5

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]} *)
Jules Lamers
  • 1,074
  • 9
  • 19
  • @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? – Jules Lamers Sep 20 '16 at 13:40
  • It works now. I deleted my what I tried before. It could have been an error on my part, but I can't check. – Michael E2 Sep 20 '16 at 13:52

2 Answers2

11

Maybe use Internal`SyntacticNegativeQ[]:

ClearAll[g];
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.)

Michael E2
  • 235,386
  • 17
  • 334
  • 747
  • That is wonderful! I was just trying to construct my own sort of Internal`SyntacticNegativeQ from CoefficientList. – Fred Simons Sep 20 '16 at 14:59
  • Thanks, that does seem to work and is elegant! Could you perhaps briefly explain what the function Internal`SyntacticNegativeQ does and why this works? – Jules Lamers Sep 20 '16 at 15:06
  • @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]. – Michael E2 Sep 20 '16 at 15:15
  • 2
    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. – Michael E2 Sep 24 '16 at 13:58
1

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:

Clear[f];
f[0]=0;
f[x_?NumericQ]/;OrderedQ@{x,-x}:=-f[-x];
f[ x_] /; Quiet[x[[1]]<0 || x[[1,1]]<0] := -f[Expand[-x]]

f/@{-x,x-y,-x+y}

(* {-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.

Fred Simons
  • 10,181
  • 18
  • 49
  • 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$? – Jules Lamers Sep 20 '16 at 14:25
  • 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. – Fred Simons Sep 20 '16 at 15:15