# Define antisymmetric function [duplicate]

Assume I'm trying to naively define an antisymmetric functionf[x_, y_] := -f[y, x]and assign it a value at some point f[1, 2] = 1;. After this, calling {f[1, 2], f[2, 1]} gives {1,-1} as desired. However, evaluating f[1,3] leads to an infinite recursion with result Hold[f[1, 3]].

My goal is to write a definition of f[x,y] in such a way that it tries both variants f[x,y] and -f[y,x] to see if a value is assigned for any of them. If it is, the function should evaluate to this value. If it isn't, then it should stay unevaluated but avoid the infinite loop.

• Just use a Condition in your definition to avoid the infinite loop: f[x_, y_] /; (!OrderedQ[{x,y}]) := -f[y, x] Jul 5, 2018 at 15:53
• In this case if I define f[2,1]=-1 then f[1,2] will stay unevaluated. My whole purpose is to not care whether I should define f[x,y] or f[y,x]. Jul 5, 2018 at 15:57
• Perhaps this is what I'm looking for forums.wolfram.com/mathgroup/archive/2010/Feb/msg00383.html. The key is inspecting DownValues. Jul 5, 2018 at 16:06
• What should happen if you try f[1,1] = 1???
– Jens
Jul 5, 2018 at 16:33
• Possible duplicate of Skew-symmetric function. Also related: mathematica.stackexchange.com/questions/137862/… Jul 5, 2018 at 18:51

This works as requested:

Clear@f
Module[
{enabled = True},
f[x_, y_] /; enabled := Block[
{enabled = False},
With[
{res = f[y, x]},
-res /; res =!= Unevaluated@f[y, x]
]
]
]


Testing it:

f[1, 2]
(* f[1, 2] *)

f[2, 1] = 2
(* 2 *)

f[1, 2]
(* -2 *)

f[2, 1]
(* 2 *)


### How

There are a few things that make this work:

• The Module/Condition(/;)/Block combination ensures that the definition is not infinitely reinserted into itself (you can remove the Module if you don't worry about the enabled flag colliding with anything
• In this setting, we can safely evaluate f[y,x] is safe.
• The last part is the second Condition(res =!= Unevaluated@…), which only applies the "flipping" of arguments if it actually evaluates to something else
• This works in my actual problem, great! The fact that x==Unevaluated[x] gives True seems counter-intuitive to me. I was thinking about how to check whether some change in expression really occurs, and this is the way to go. Jul 6, 2018 at 6:36