On 04/06/2020 00:04, James Bateman wrote:
Thank you. This has been extremely helpful. Sorry once again for
posting the wrong code initially.
James,
I found the creation of symbols in SymPy incredibly confusing and
counter-intuitive - it was almost spoiling my enjoyment of using the
wonderful SymPy system. I decided to work out a systematic way of using
SymPy for short calculations that eliminates this confusion.
I avoid all use of Symbol, symbols, or any of the related ways of
creating symbols, except for creating special symbols, such as undefined
function symbols. I start any use of SymPy thus:
import sympy
from sympy.abc import *
from sympy import *
The module sympy.abc contains symbols for all 52 single-letter variables
- which are obviously most useful for algebra - in upper and lower case.
Unfortunately a few upper case symbols are used by SymPy for specific
purposes - so the order of the above imports is important - SymPy
symbols should override those from abc so they are imported last. This
is described in the SymPy documentation:
"As of the time of writing this, the names |C|, |O|, |S|, |I|, |N|, |E|,
and |Q| are colliding with names defined in SymPy. If you import them
from both |sympy.abc| and |sympy|, the second import will “win”. This is
an issue only for * imports, which should only be used for short-lived
code such as interactive sessions and throwaway scripts that do not
survive until the next SymPy upgrade, where |sympy| may contain a
different set of names."
Using the above, I then choose a few (often zero) variables to use in
the traditional Python fashion (i.e. variables that will be assigned
values) and I have the rest available for algebra. If you need a lot of
Python variables - say in a complicated algorithm - it may be easier to
use multi-letter names for them - which totally avoids any clash with
symbols.
*I don't create any normal symbols explicitly.*
Note that the above quote from the SymPy documentation does not abhor
the use of 'import *' for "short lived code", which might be better
phrased as "small sized code". I suspect that typically most users only
want to write a small SymPy 'program' - sometimes just one line - and
for those uses 'import *' seems infinitely preferable. Of course, most
of the people who post here are seasoned SymPy developers, and they are
writing large amounts (hundreds or thousands of lines) of SymPy code -
symbolic integration algorithms or whatever - and for them I can see
that importing the entire contents of SymPy or other modules would be
dangerous.
I hope that helps, and doesn't create too much controversy!
David
--
You received this message because you are subscribed to the Google Groups
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to sympy+unsubscr...@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msgid/sympy/335197a0-2f6e-d32e-c9da-8e8a30c7d97c%40dbailey.co.uk.