[Mathematica] Redefining a built-in symbol

  • Context: Mathematica 
  • Thread starter Thread starter guerom00
  • Start date Start date
  • Tags Tags
    Mathematica Symbol
Click For Summary
SUMMARY

This discussion focuses on redefining the built-in square root function in Mathematica. The user initially attempts to redefine the function using the syntax mySqrt[z_]:=√z and modifies the Sqrt function with an If statement. However, the symbol remains linked to the original Sqrt function. A solution is provided using MakeExpression to redefine how Mathematica interprets the square root symbol, while also cautioning against globally overwriting built-in functions to avoid potential bugs.

PREREQUISITES
  • Familiarity with Mathematica syntax and functions
  • Understanding of function definitions and scoping in Mathematica
  • Knowledge of complex numbers and their representation in Mathematica
  • Experience with the MakeExpression function in Mathematica
NEXT STEPS
  • Research how to use MakeExpression for custom input parsing in Mathematica
  • Explore the implications of redefining built-in functions in Mathematica
  • Learn about the Block function for temporary symbol redefinitions
  • Investigate best practices for handling complex inputs in Mathematica functions
USEFUL FOR

This discussion is beneficial for Mathematica users, particularly those interested in customizing function behavior, developers creating complex mathematical models, and anyone looking to enhance their understanding of function scoping and input parsing in Mathematica.

guerom00
Messages
90
Reaction score
0
Hello all :)

I would like to redefine the built-in square root function.
I have written this :

mySqrt[z_]:=√z;
Unprotect[Sqrt];
Sqrt[z_]:=If[Re[mySqrt[z]]+Im[mySqrt[z]]>0,mySqrt[z],-mySqrt[z]];
Protect[Sqrt]

This works fine and redefine Sqrt[] as I want it to be. But, the symbol √ (by typing Ctrl-2) still points to the original Sqrt[] function ! How can I redefine the behavior of the symbol √ ?

TIA :)
 
Physics news on Phys.org
guerom00 said:
Hello all :)

I would like to redefine the built-in square root function.
I have written this :

mySqrt[z_]:=√z;
Unprotect[Sqrt];
Sqrt[z_]:=If[Re[mySqrt[z]]+Im[mySqrt[z]]>0,mySqrt[z],-mySqrt[z]];
Protect[Sqrt]

This works fine and redefine Sqrt[] as I want it to be. But, the symbol √ (by typing Ctrl-2) still points to the original Sqrt[] function ! How can I redefine the behavior of the symbol √ ? :)

To introduce your own behaviour for complex inputs, you need to change the way that Mathematica parses input expressions. Try replacing your first definition with

Code:
MakeExpression[SqrtBox[expr_], form_] := 
 With[{mexpr = ReleaseHold@MakeExpression[expr, form]}, 
  Hold[mySqrt[mexpr]]]

As an aside, it's not normally a good idea to globally overwrite built-in functions.
Sqrt[] could be used inside many algorithms that will then break - causing hard to track down bugs. Best to just Block[] the symbol when you want to overwrite the normal definition.
 
Thank you for your answer, I'll try that.
And thanks for your warnings : I better define my own square root function and leave the built-in one untouched :)
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
19
Views
3K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 5 ·
Replies
5
Views
1K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
Replies
2
Views
2K