1
$\begingroup$

I often see in code by other people some standard functions like Sum, NonCommutativeMultiply etc get unprotected, supplemented by additional rules and then protected back. It is not clear to be that this is a good idea. How can I be sure that I will not mess up badly redefining something like Sum? Is not there a way to somehow clone the command say "mySum=Sum" and then make changes to the new command? Or maybe there is a way to modify an existing command locally, say within some scoping construct?

$\endgroup$
  • 5
    $\begingroup$ If you don't know what sort of consequences it could have, I recommend against it. In principle, there's no problem with modification of many built-in symbols, but some of them are crucial to the workings of the language. You can use Internal`InheritedBlock to temporarily change built-in functions without permanent damage: mathematica.stackexchange.com/q/25769/43522 $\endgroup$ – Sjoerd Smit Aug 11 at 14:09
  • $\begingroup$ There are many similar questions e.g. this. $\endgroup$ – Rohit Namjoshi Aug 11 at 14:11
  • 3
    $\begingroup$ I echo what @Sjoerd said, and add that sometimes you want to alter the internal workings. For instance, I wanted to place a time limit on Integrate inside DSolve or [to change the Method of Solve](https://mathematica.stackexchange.com/a/63676/4999) inside DSolve`. The alternative is that Mathematica fails. In their limited use-cases, altering built-ins seem worth the risk. But there is a risk. $\endgroup$ – Michael E2 Aug 11 at 14:20
  • 2
    $\begingroup$ One reason that functions are protected is to warn users who don't yet fully understand Mathematica syntax when their definitions are modifying built-in functions. A question to ask yourself, is my redefinition likely to be triggered in internal code? $\endgroup$ – mikado Aug 11 at 16:15
5
$\begingroup$

Is it a good idea to unprotect standard commands?

No, it is not usually a good idea to modify built-ins. If you modify a built-in, other built-ins (or packages) that rely on it may either break or may slow down significantly. Here's an example of such a slowdown:

If you are just looking to overload a built-in for your own custom expression type, consider using up-values instead.

Of course, there are some built-ins which are designed to be extended, e.g. MakeBoxes. That's an entirely different situation. You may also want to intercept the calls to some functions to better understand how the system works. That is also a reasonable application. However, if you were to publish a package in which you overload a fundamental function such as Sum, I would think twice before using your package.

Is not there a way to somehow clone the command say "mySum=Sum" and then make changes to the new command?

You don't need to "clone" it. Simply use Sum in the definition of mySum.

| improve this answer | |
$\endgroup$
  • $\begingroup$ Just to clarify. If I want to take standard NonCommutativeMultiply and then make it do whatever I want, I can just initialize myNCM[x___]:=NonCommutativeMultiply[x] and then do with myNCM whatever I want? $\endgroup$ – Weather Report Aug 11 at 17:01
  • 1
    $\begingroup$ @WeatherReport No. NonCommutativeMultiply does not do anything, so there is no point to calling it from another function. NonCommutativeMultiply is one of those symbols which is meant to be given a definition (it does not have one by default). Just use it. $\endgroup$ – Szabolcs Aug 11 at 21:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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