5
$\begingroup$

I'm trying to add functionality to some of Mathematica's built-in functions in a package I'm building. I'm not trying to overwrite any functionality—just add it where it isn't already defined.

I start my package body with:

Unprotect["packagename`*"];
ClearAll["packagename`*"]

Clearly, though, I don't want to clear the system definition! However, I do want to reset the symbol to its default system definition before adding my definition, and I don't know how to do that.

I'd also appreciate any advice on best practices for adding this functionality! I'm willing to just use a typical :=, but if there are better ways, I'd certainly be interested.

$\endgroup$
6
  • 1
    $\begingroup$ An example of a built-in function, and how and why you want to modify it would be helpful. $\endgroup$
    – Carl Woll
    Mar 23, 2019 at 2:16
  • $\begingroup$ Sure! Concretely, I need to extend interval arithmetic to complex numbers. For example, something I might want to include in a package could be Interval[n_Complex] := (#1 + #2*I) & @@ (Interval /@ ReIm[n]). Without this, Interval[n] where n has head Complex doesn't evaluate to anything. Unprotecting Interval and evaluating this works—I just want to do it safely and repeatedly in a package, which I think would involve resetting Interval to the system default somehow. $\endgroup$
    – thorimur
    Mar 23, 2019 at 2:20
  • $\begingroup$ "I need to extend interval arithmetic to complex numbers" - this isn't your question anymore, but: look up disk arithmetic. Disks are more natural to operate on than rectangles for complex numbers. $\endgroup$ Mar 23, 2019 at 4:00
  • $\begingroup$ @J.M. Disk arithmetic may be more useful in delivering tighter error bounds, but there is a price in implementation difficulty. $\endgroup$ Mar 27, 2019 at 22:22
  • $\begingroup$ @Daniel, yes, I've seen from the literature that there are a lot of subtleties to fret about. I presume this is why Mathematica doesn't have it? $\endgroup$ Mar 27, 2019 at 23:07

1 Answer 1

7
$\begingroup$

If the System` symbol you want to modify is a kernel function, then you can just use Clear to reset it to its default state. For your Interval example:

Language`ExtendedDefinition[Interval]

Language`DefinitionList[Interval->{OwnValues->{},SubValues->{},UpValues->{},DownValues->{},NValues->{},FormatValues->{},DefaultValues->{},Messages->{},Attributes->{Protected}}]

shows that Interval has no *Values. Using Clear on Interval will only clear new definitions that you add. Example:

Unprotect[Interval];
Interval[n_Complex] := (#1 + #2*I) & @@ (Interval /@ ReIm[n])
Protect[Interval];

Now, let's check:

Language`ExtendedDefinition[Interval]

Language`DefinitionList[Interval->{OwnValues->{},SubValues->{},UpValues->{},DownValues->{HoldPattern[Interval[n_Complex]]:>(#1+#2 I&)@@Interval/@ReIm[n]},NValues->{},FormatValues->{},DefaultValues->{},Messages->{},Attributes->{Protected}}]

Notice that your definition shows up in the DownValues rule. Let's clear definitions:

Unprotect[Interval];
Clear[Interval]
Protect[Interval];

And the definition is gone:

Language`ExtendedDefinition[Interval]

Language`DefinitionList[Interval->{OwnValues->{},SubValues->{},UpValues->{},DownValues->{},NValues->{},FormatValues->{},DefaultValues->{},Messages->{},Attributes->{Protected}}]

Note the Interval still works:

Sin[Interval[{0, Pi}]]

Interval[{0, 1}]

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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