0

As you can see,the original Differences will get {} when your parameter is a list with one element just.

Differences /@ {{1, 2, 3}, {4, 5}, {6}}

{{1, 1}, {1}, {}}

Then we can change it like this

Unprotect[Differences];
Differences[{a_}] := {a};
Differences /@ {{1, 2, 3}, {4, 5}, {6}}

{{1, 1}, {1}, {6}}

But the same method don't fit the Times and Divide

Unprotect[Times];
Times[0, 0] := "NAN"
Times[0, 0]
0*0
Times[2, 3]

enter image description here

Or like the Divide

Unprotect[Divide];
Divide[0, 0] := "NAN"
Divide[0, 0]
0/0
Divide[2, 3]

enter image description here

yode
  • 26,686
  • 4
  • 62
  • 167
  • 4
    Still a bit unclear - do you want to change Times etc? This is a really bad idea for such elementary functions, as it will mess up your system in unpredictable ways and bite you in the end. Is there a deeper reason not to use a custom function? – Yves Klett Jan 20 '16 at 14:21
  • Thanks for your advice very much,but I wanna change it still in my situation. – yode Jan 20 '16 at 14:24
  • 7
    There is a lot of indication that the built-in mathematical operations are deeply wired into the parser to speed up their operations, unlike Differences. So, adding DownValues for them is not very effective. – rcollyer Jan 20 '16 at 14:35
  • I am curious to see if/what @DanielLichtblau might report on messing with Times and friends. – Yves Klett Jan 20 '16 at 14:50
  • 3
    You should define new functions that deal with the special cases and then defer to Times, Divide, Differences etc. to process the non-special cases afterwards. There is simply no way to reliably change Times, for example. Its definition is hard-coded into the system. Even if you could, you shouldn't: do you really think Mathematica's own functions don't rely on a consistent concept of multiplication? – Oleksandr R. Jan 20 '16 at 22:07
  • Related: http://mathematica.stackexchange.com/questions/19126/why-does-this-pattern-with-plus-not-work-for-numbers – Michael E2 Jan 31 '16 at 12:51
  • It seems to work with Divide. If I read your output correctly, Divide[0, 0] yields "NAN". Note that 0/0 is translated to Times[0, Power[0, -1]] before being fed to the kernel for evaluation [(39200)]. – Michael E2 Jan 31 '16 at 12:54

0 Answers0