Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining symbols in SymPy which hold numerical values

I am trying to define a constant in SymPy with the same functionality as constants like pi, e, and i. When exact answers are given, they remain as their symbols, instead of evaluating to their values. For instance if I ran

2*pi

it would return 2⋅π. If I wanted an approximate answer, I could run

N(2*pi)

which returns 6.28318530717959.

What I want is to define a constant, tau, where

tau

returns τ, while

N(tau)

returns 6.28318530717959.

Basically I'm just trying to define a new symbol, τ, which is equal to 2⋅π.

like image 616
Sotanaht Avatar asked Oct 18 '25 02:10

Sotanaht


1 Answers

You need to subclass NumberSymbol in the same way that Pi does it. Just copy-pasting the source code for Pi and adding *2 should be enough. Be aware that these objects are singleton classes and not instances.

You should also read the pi manifesto ;)

like image 103
Krastanov Avatar answered Oct 19 '25 17:10

Krastanov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!