[Mathematica] Redefining a built-in symbol

In summary, the conversation discusses redefining the built-in square root function in Mathematica using a custom function and how to change the behavior of the symbol √. The solution involves changing the way Mathematica parses input expressions and using the Block[] function to avoid breaking the normal definition of Sqrt[].
  • #1
guerom00
93
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
  • #2
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.
 
  • #3
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 :)
 

What is a built-in symbol in Mathematica?

A built-in symbol in Mathematica is a predefined symbol that has a specific meaning and functionality within the software. These symbols are part of the core language and cannot be redefined or modified by the user.

Why would I want to redefine a built-in symbol in Mathematica?

Redefining a built-in symbol in Mathematica can be useful when you want to customize the behavior of a specific function or symbol for your specific needs. It allows you to change the default functionality of a symbol to better suit your purposes.

What precautions should I take when redefining a built-in symbol in Mathematica?

When redefining a built-in symbol in Mathematica, it is important to be cautious and fully understand the implications of your changes. This includes checking for any potential conflicts with other functions or symbols, as well as making sure your changes do not break any existing code.

Can I undo my changes to a built-in symbol in Mathematica?

Yes, you can undo your changes to a built-in symbol in Mathematica by restarting the software or by using the built-in function Remove to remove the new definition and restore the original functionality of the symbol.

Are there any alternatives to redefining a built-in symbol in Mathematica?

Yes, there are alternatives to redefining a built-in symbol in Mathematica. These include creating your own custom functions or using options and parameters to modify the behavior of existing functions. It is also possible to create custom packages to extend the functionality of the software without directly modifying built-in symbols.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
19
Views
1K
  • Linear and Abstract Algebra
Replies
1
Views
758
Replies
5
Views
695
  • Special and General Relativity
Replies
15
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Precalculus Mathematics Homework Help
Replies
7
Views
2K
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
275
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
Back
Top