2

How can we override a Mathematica built-in function using itself in the definition?

For example, the DiracDelta function is not defined for complex numbers like:

In[1]:= N[DiracDelta[1 + 2 I]]

Out[1]= DiracDelta[1. + 2. I]

But how can we change its behavior for something similar to this?

Unprotect[DiracDelta];
DiracDelta[x_] := DiracDelta[Re[x] + Im[x]];
Protect[DiractDelta];

With this approach we receive an infinite recursion error.

We could define another function, like DiractDelta2[x_] with the expected behavior, but the problem is more because I'd needing to see some simplifications when I'm expanding my series, and for those cases it is ok to define it that way.

Attempts

OldDiracDelta = DiracDelta;
Unprotect[DiracDelta];
DiracDelta[x_] := OldDiracDelta[Re[x] + Im[x]];
Protect[DiractDelta];
(* But unfortunately this still incurs in the infinite recursion error. *)
J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
GarouDan
  • 1,536
  • 1
  • 12
  • 15
  • 1
    Could restrict the definition as: DiracDelta[Complex[x_,y_]]:=DiracDelta[x +y] (not commenting on the sensibility of such a definition). – Daniel Lichtblau May 06 '20 at 19:43
  • 1
    The answer to the question in the title is here: https://mathematica.stackexchange.com/q/39711/12 In this case you don't need such a heavyweight approach, just more specific patterns, as pointed out by Daniel – Szabolcs May 06 '20 at 20:01
  • Thanks a lot @DanielLichtblau, it works! =) DanielLichtblau, would you like to post it as an answer? I'd like to accept it. – GarouDan May 06 '20 at 21:42
  • @Szabolcs, I wasn't able to really understand the solution of your link, but I'll try to keep this in mind ;) – GarouDan May 06 '20 at 21:42
  • I've seen that this approach DiracDelta[Complex[x_,y_]]:=DiracDelta[x +y] do not always work as expected. If you have something like DiractDelta[I] it doesn't resolve. If you extend more, like adding DiractDelta[I*x_] := DiracDelta[x] it works but I've seen a lot of other cases appearing, unfortunately. Maybe I need to try something using ComplexExpand first. – GarouDan May 07 '20 at 02:03

0 Answers0