Created
July 17, 2015 18:44
-
-
Save mitya57/f43720cba9ab12b07d92 to your computer and use it in GitHub Desktop.
sympy sphinx build
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mkdir -p _build/latex _build/doctrees | |
PYTHONPATH=..: sphinx-build -b latex -d _build/doctrees-latex -D latex_paper_size= src _build/latex | |
Running Sphinx v1.3.1 | |
loading pickled environment... not yet created | |
building [mo]: targets for 0 po files that are out of date | |
building [latex]: all documents | |
updating environment: 172 added, 0 changed, 0 removed | |
reading sources... [ 0%] aboutus | |
reading sources... [ 1%] gotchas | |
reading sources... [ 1%] guide | |
reading sources... [ 2%] index | |
reading sources... [ 2%] install | |
reading sources... [ 3%] modules/assumptions/ask | |
reading sources... [ 4%] modules/assumptions/assume | |
reading sources... [ 4%] modules/assumptions/handlers/calculus | |
reading sources... [ 5%] modules/assumptions/handlers/index | |
reading sources... [ 5%] modules/assumptions/handlers/ntheory | |
reading sources... [ 6%] modules/assumptions/handlers/order | |
reading sources... [ 6%] modules/assumptions/handlers/sets | |
reading sources... [ 7%] modules/assumptions/index | |
reading sources... [ 8%] modules/assumptions/refine | |
reading sources... [ 8%] modules/calculus/index | |
reading sources... [ 9%] modules/categories | |
reading sources... [ 9%] modules/combinatorics/graycode | |
reading sources... [ 10%] modules/combinatorics/group_constructs | |
reading sources... [ 11%] modules/combinatorics/index | |
reading sources... [ 11%] modules/combinatorics/named_groups | |
reading sources... [ 12%] modules/combinatorics/partitions | |
reading sources... [ 12%] modules/combinatorics/perm_groups | |
reading sources... [ 13%] modules/combinatorics/permutations | |
reading sources... [ 13%] modules/combinatorics/polyhedron | |
reading sources... [ 14%] modules/combinatorics/prufer | |
reading sources... [ 15%] modules/combinatorics/subsets | |
reading sources... [ 15%] modules/combinatorics/tensor_can | |
reading sources... [ 16%] modules/combinatorics/testutil | |
reading sources... [ 16%] modules/combinatorics/util | |
reading sources... [ 17%] modules/concrete | |
reading sources... [ 18%] modules/core | |
reading sources... [ 18%] modules/crypto | |
reading sources... [ 19%] modules/diffgeom | |
reading sources... [ 19%] modules/evalf | |
reading sources... [ 20%] modules/functions/combinatorial | |
reading sources... [ 20%] modules/functions/elementary | |
reading sources... [ 21%] modules/functions/index | |
reading sources... [ 22%] modules/functions/special | |
reading sources... [ 22%] modules/galgebra/debug | |
reading sources... [ 23%] modules/galgebra/ga | |
reading sources... [ 23%] modules/galgebra/index | |
reading sources... [ 24%] modules/galgebra/manifold | |
reading sources... [ 25%] modules/galgebra/ncutil | |
reading sources... [ 25%] modules/galgebra/precedence | |
reading sources... [ 26%] modules/galgebra/printing | |
reading sources... [ 26%] modules/galgebra/stringarrays | |
reading sources... [ 27%] modules/galgebra/vector | |
reading sources... [ 27%] modules/geometry | |
reading sources... [ 28%] modules/index | |
reading sources... [ 29%] modules/integrals/g-functions | |
reading sources... [ 29%] modules/integrals/integrals | |
reading sources... [ 30%] modules/logic | |
reading sources... [ 30%] modules/matrices/dense | |
reading sources... [ 31%] modules/matrices/expressions | |
reading sources... [ 31%] modules/matrices/immutablematrices | |
reading sources... [ 32%] modules/matrices/index | |
reading sources... [ 33%] modules/matrices/matrices | |
reading sources... [ 33%] modules/matrices/sparse | |
reading sources... [ 34%] modules/ntheory | |
reading sources... [ 34%] modules/numeric-computation | |
reading sources... [ 35%] modules/parsing | |
reading sources... [ 36%] modules/physics/gaussopt | |
reading sources... [ 36%] modules/physics/hep/gamma_matrices | |
reading sources... [ 37%] modules/physics/hep/index | |
reading sources... [ 37%] modules/physics/hydrogen | |
reading sources... [ 38%] modules/physics/index | |
reading sources... [ 38%] modules/physics/matrices | |
reading sources... [ 39%] modules/physics/mechanics/advanced | |
reading sources... [ 40%] modules/physics/mechanics/api/kane | |
reading sources... [ 40%] modules/physics/mechanics/api/part_bod | |
reading sources... [ 41%] modules/physics/mechanics/api/printing | |
reading sources... [ 41%] modules/physics/mechanics/bicycle_example | |
reading sources... [ 42%] modules/physics/mechanics/examples | |
reading sources... [ 43%] modules/physics/mechanics/index | |
reading sources... [ 43%] modules/physics/mechanics/kane | |
reading sources... [ 44%] modules/physics/mechanics/masses | |
reading sources... [ 44%] modules/physics/mechanics/reference | |
reading sources... [ 45%] modules/physics/mechanics/rollingdisc_example | |
reading sources... [ 45%] modules/physics/mechanics/rollingdisc_example_kane | |
reading sources... [ 46%] modules/physics/mechanics/rollingdisc_example_kane_constraints | |
reading sources... [ 47%] modules/physics/mechanics/rollingdisc_example_lagrange | |
reading sources... [ 47%] modules/physics/paulialgebra | |
reading sources... [ 48%] modules/physics/qho_1d | |
reading sources... [ 48%] modules/physics/quantum/anticommutator | |
reading sources... [ 49%] modules/physics/quantum/cartesian | |
reading sources... [ 50%] modules/physics/quantum/cg | |
reading sources... [ 50%] modules/physics/quantum/circuitplot | |
reading sources... [ 51%] modules/physics/quantum/commutator | |
reading sources... [ 51%] modules/physics/quantum/constants | |
reading sources... [ 52%] modules/physics/quantum/dagger | |
reading sources... [ 52%] modules/physics/quantum/gate | |
reading sources... [ 53%] modules/physics/quantum/grover | |
reading sources... [ 54%] modules/physics/quantum/hilbert | |
reading sources... [ 54%] modules/physics/quantum/index | |
reading sources... [ 55%] modules/physics/quantum/innerproduct | |
reading sources... [ 55%] modules/physics/quantum/operator | |
reading sources... [ 56%] modules/physics/quantum/operatorset | |
reading sources... [ 56%] modules/physics/quantum/piab | |
reading sources... [ 57%] modules/physics/quantum/qapply | |
reading sources... [ 58%] modules/physics/quantum/qft | |
reading sources... [ 58%] modules/physics/quantum/qubit | |
reading sources... [ 59%] modules/physics/quantum/represent | |
reading sources... [ 59%] modules/physics/quantum/shor | |
reading sources... [ 60%] modules/physics/quantum/spin | |
reading sources... [ 61%] modules/physics/quantum/state | |
reading sources... [ 61%] modules/physics/quantum/tensorproduct | |
reading sources... [ 62%] modules/physics/secondquant | |
reading sources... [ 62%] modules/physics/sho | |
reading sources... [ 63%] modules/physics/units | |
reading sources... [ 63%] modules/physics/vector/advanced | |
reading sources... [ 64%] modules/physics/vector/api/classes | |
reading sources... [ 65%] modules/physics/vector/api/functions | |
reading sources... [ 65%] modules/physics/vector/api/kinematics | |
reading sources... [ 66%] modules/physics/vector/api/printing | |
reading sources... [ 66%] modules/physics/vector/index | |
reading sources... [ 67%] modules/physics/vector/kinematics | |
reading sources... [ 68%] modules/physics/vector/vectors | |
reading sources... [ 68%] modules/physics/wigner | |
reading sources... [ 69%] modules/plotting | |
reading sources... [ 69%] modules/polys/agca | |
reading sources... [ 70%] modules/polys/basics | |
reading sources... [ 70%] modules/polys/index | |
reading sources... [ 71%] modules/polys/internals | |
reading sources... [ 72%] modules/polys/literature | |
reading sources... [ 72%] modules/polys/reference | |
reading sources... [ 73%] modules/polys/wester | |
reading sources... [ 73%] modules/printing | |
reading sources... [ 74%] modules/rewriting | |
reading sources... [ 75%] modules/series | |
reading sources... [ 75%] modules/sets | |
reading sources... [ 76%] modules/simplify/hyperexpand | |
reading sources... [ 76%] modules/simplify/simplify | |
reading sources... [ 77%] modules/solvers/diophantine | |
reading sources... [ 77%] modules/solvers/inequalities | |
reading sources... [ 78%] modules/solvers/ode | |
reading sources... [ 79%] modules/solvers/pde | |
reading sources... [ 79%] modules/solvers/solvers | |
reading sources... [ 80%] modules/statistics | |
/tmp/buildd/sympy-0.7.5/sympy/statistics/__init__.py:11: SymPyDeprecationWarning: | |
sympy.statistics has been deprecated since SymPy 0.7.2. Use | |
sympy.stats instead. See | |
http://code.google.com/p/sympy/issues/detail?id=3386 for more info. | |
deprecated_since_version="0.7.2", | |
reading sources... [ 80%] modules/stats | |
reading sources... [ 81%] modules/tensor/index | |
reading sources... [ 81%] modules/tensor/index_methods | |
reading sources... [ 82%] modules/tensor/indexed | |
reading sources... [ 83%] modules/tensor/tensor | |
reading sources... [ 83%] modules/utilities/autowrap | |
reading sources... [ 84%] modules/utilities/codegen | |
reading sources... [ 84%] modules/utilities/decorator | |
reading sources... [ 85%] modules/utilities/enumerative | |
reading sources... [ 86%] modules/utilities/index | |
reading sources... [ 86%] modules/utilities/iterables | |
reading sources... [ 87%] modules/utilities/lambdify | |
reading sources... [ 87%] modules/utilities/memoization | |
reading sources... [ 88%] modules/utilities/misc | |
reading sources... [ 88%] modules/utilities/pkgdata | |
reading sources... [ 89%] modules/utilities/pytest | |
reading sources... [ 90%] modules/utilities/randtest | |
reading sources... [ 90%] modules/utilities/runtests | |
reading sources... [ 91%] modules/utilities/source | |
reading sources... [ 91%] modules/utilities/timeutils | |
reading sources... [ 92%] outreach | |
reading sources... [ 93%] python-comparisons | |
reading sources... [ 93%] tutorial/basic_operations | |
reading sources... [ 94%] tutorial/calculus | |
reading sources... [ 94%] tutorial/gotchas | |
reading sources... [ 95%] tutorial/index | |
reading sources... [ 95%] tutorial/intro | |
reading sources... [ 96%] tutorial/manipulation | |
reading sources... [ 97%] tutorial/matrices | |
reading sources... [ 97%] tutorial/preliminaries | |
reading sources... [ 98%] tutorial/printing | |
reading sources... [ 98%] tutorial/simplification | |
reading sources... [ 99%] tutorial/solvers | |
reading sources... [100%] wiki | |
/tmp/buildd/sympy-0.7.5/doc/src/modules/core.rst:465: WARNING: error while formatting arguments for sympy.core.evalf.N: 'function' object has no attribute '__bases__' | |
/tmp/buildd/sympy-0.7.5/doc/src/modules/functions/elementary.rst:306: WARNING: error while formatting arguments for sympy.functions.elementary.miscellaneous.sqrt: 'function' object has no attribute '__bases__' | |
/tmp/buildd/sympy-0.7.5/doc/src/modules/polys/internals.rst:512: WARNING: error while formatting arguments for sympy.polys.modulargcd.modgcd_univariate: 'function' object has no attribute '__bases__' | |
/tmp/buildd/sympy-0.7.5/doc/src/modules/polys/internals.rst:513: WARNING: error while formatting arguments for sympy.polys.modulargcd.modgcd_bivariate: 'function' object has no attribute '__bases__' | |
/tmp/buildd/sympy-0.7.5/doc/src/modules/polys/internals.rst:514: WARNING: error while formatting arguments for sympy.polys.modulargcd.modgcd_multivariate: 'function' object has no attribute '__bases__' | |
/tmp/buildd/sympy-0.7.5/doc/src/modules/polys/internals.rst:515: WARNING: error while formatting arguments for sympy.polys.modulargcd.func_field_modgcd: 'function' object has no attribute '__bases__' | |
looking for now-outdated files... none found | |
pickling environment... done | |
checking consistency... done | |
processing sympy-0.7.5.tex... index install tutorial/index tutorial/preliminaries tutorial/intro tutorial/printing tutorial/gotchas tutorial/basic_operations tutorial/simplification tutorial/calculus tutorial/solvers tutorial/matrices tutorial/manipulation gotchas guide modules/index modules/core modules/combinatorics/index modules/combinatorics/partitions modules/combinatorics/permutations modules/combinatorics/perm_groups modules/combinatorics/polyhedron modules/combinatorics/prufer modules/combinatorics/subsets modules/combinatorics/graycode modules/combinatorics/named_groups modules/combinatorics/util modules/combinatorics/group_constructs modules/combinatorics/testutil modules/combinatorics/tensor_can modules/ntheory modules/crypto modules/concrete modules/evalf modules/numeric-computation modules/functions/index modules/functions/elementary modules/functions/combinatorial modules/functions/special modules/galgebra/index modules/galgebra/ga modules/galgebra/manifold modules/galgebra/vector modules/galgebra/precedence modules/galgebra/printing modules/galgebra/ncutil modules/galgebra/stringarrays modules/galgebra/debug modules/geometry modules/integrals/integrals modules/integrals/g-functions modules/logic modules/matrices/index modules/matrices/matrices modules/matrices/dense modules/matrices/sparse modules/matrices/immutablematrices modules/matrices/expressions modules/polys/index modules/polys/basics modules/polys/wester modules/polys/reference modules/polys/agca modules/polys/internals modules/polys/literature modules/printing modules/plotting modules/assumptions/index modules/assumptions/ask modules/assumptions/assume modules/assumptions/refine modules/assumptions/handlers/index modules/assumptions/handlers/calculus modules/assumptions/handlers/ntheory modules/assumptions/handlers/order modules/assumptions/handlers/sets modules/rewriting modules/series modules/sets modules/simplify/simplify modules/simplify/hyperexpand modules/statistics modules/stats modules/solvers/ode modules/solvers/pde modules/solvers/solvers modules/solvers/diophantine modules/solvers/inequalities modules/tensor/index modules/tensor/indexed modules/tensor/index_methods modules/tensor/tensor modules/utilities/index modules/utilities/autowrap modules/utilities/codegen modules/utilities/decorator modules/utilities/enumerative modules/utilities/iterables modules/utilities/lambdify modules/utilities/memoization modules/utilities/misc modules/utilities/pkgdata modules/utilities/pytest modules/utilities/randtest modules/utilities/runtests modules/utilities/source modules/utilities/timeutils modules/parsing modules/calculus/index modules/physics/index modules/physics/gaussopt modules/physics/hydrogen modules/physics/matrices modules/physics/paulialgebra modules/physics/qho_1d modules/physics/sho modules/physics/secondquant modules/physics/wigner modules/physics/units modules/physics/hep/index modules/physics/hep/gamma_matrices modules/physics/vector/index modules/physics/vector/vectors modules/physics/vector/kinematics modules/physics/vector/advanced modules/physics/vector/api/classes modules/physics/vector/api/kinematics modules/physics/vector/api/printing modules/physics/vector/api/functions modules/physics/mechanics/index modules/physics/mechanics/masses modules/physics/mechanics/kane modules/physics/mechanics/examples modules/physics/mechanics/rollingdisc_example modules/physics/mechanics/rollingdisc_example_kane modules/physics/mechanics/rollingdisc_example_kane_constraints modules/physics/mechanics/rollingdisc_example_lagrange modules/physics/mechanics/bicycle_example modules/physics/mechanics/advanced modules/physics/mechanics/reference modules/physics/mechanics/api/part_bod modules/physics/mechanics/api/kane modules/physics/mechanics/api/printing modules/physics/quantum/index modules/physics/quantum/anticommutator modules/physics/quantum/cg modules/physics/quantum/commutator modules/physics/quantum/constants modules/physics/quantum/dagger modules/physics/quantum/innerproduct modules/physics/quantum/tensorproduct modules/physics/quantum/cartesian modules/physics/quantum/hilbert modules/physics/quantum/operator modules/physics/quantum/operatorset modules/physics/quantum/qapply modules/physics/quantum/represent modules/physics/quantum/spin modules/physics/quantum/state modules/physics/quantum/circuitplot modules/physics/quantum/gate modules/physics/quantum/grover modules/physics/quantum/qft modules/physics/quantum/qubit modules/physics/quantum/shor modules/physics/quantum/piab modules/categories modules/diffgeom python-comparisons wiki outreach aboutus | |
resolving references... | |
writing... /tmp/buildd/sympy-0.7.5/doc/src/install.rst:: WARNING: unusable reference target found: irc://irc.freenode.net/sympy | |
done | |
copying images... modules/physics/vector/kin_1.pdf modules/physics/vector/vec_cross.pdf tutorial/../pics/consoleascii.png modules/physics/vector/kin_4.pdf modules/physics/vector/vec_add.pdf modules/physics/vector/kin_2pt.pdf pics/winpdb2.png modules/physics/vector/kin_angvel2.pdf modules/galgebra/simple_test_latex_1.png modules/physics/vector/simp_rot.pdf modules/physics/vector/vec_dot.pdf modules/physics/vector/kin_angvel3.pdf tutorial/../pics/consoleunicode.png tutorial/../pics/ipythonnotebook.png modules/physics/vector/vec_fix_notfix.pdf modules/galgebra/manifold_testlatex.png modules/physics/mechanics/rollingdisc.pdf modules/physics/vector/vec_simp_der.pdf tutorial/../pics/ipythonqtconsole.png modules/physics/vector/vec_mul.pdf pics/winpdb1.png modules/physics/vector/vec_rep.pdf modules/galgebra/simple_test_latex_2.png modules/physics/vector/kin_3.pdf modules/physics/vector/kin_rolling.pdf modules/physics/vector/kin_angvel1.pdf modules/physics/vector/kin_1pt.pdf modules/physics/vector/kin_2.pdf | |
copying TeX support files... | |
done | |
build succeeded, 7 warnings. | |
sed -i "s/pdflatex/xelatex/g" _build/latex/Makefile | |
Build finished; the LaTeX files are in _build/latex. | |
Run `make all' in that directory to run these through xelatex. | |
make[2]: Leaving directory '/tmp/buildd/sympy-0.7.5/doc' | |
make[2]: Entering directory '/tmp/buildd/sympy-0.7.5/doc/_build/latex' | |
xelatex 'sympy-0.7.5.tex' | |
This is XeTeX, Version 3.14159265-2.6-0.99992 (TeX Live 2015/dev/Debian) (preloaded format=xelatex) | |
restricted \write18 enabled. | |
entering extended mode | |
(./sympy-0.7.5.tex | |
LaTeX2e <2014/05/01> | |
Babel <3.9l> and hyphenation patterns for 2 languages loaded. | |
(./sphinxmanual.cls | |
Document Class: sphinxmanual 2009/06/02 Document class (Sphinx manual) | |
(/usr/share/texlive/texmf-dist/tex/latex/base/report.cls | |
Document Class: report 2014/09/29 v1.4h Standard LaTeX document class | |
(/usr/share/texlive/texmf-dist/tex/latex/base/size10.clo))) | |
(/usr/share/texlive/texmf-dist/tex/latex/cmap/cmap.sty | |
Package cmap Warning: pdftex not detected - exiting. | |
) (/usr/share/texlive/texmf-dist/tex/latex/tools/bm.sty) | |
(/usr/share/texlive/texmf-dist/tex/latex/amsfonts/amssymb.sty | |
(/usr/share/texlive/texmf-dist/tex/latex/amsfonts/amsfonts.sty)) | |
(/usr/share/texlive/texmf-dist/tex/latex/fontspec/fontspec.sty | |
(/usr/share/texlive/texmf-dist/tex/latex/l3kernel/expl3.sty | |
(/usr/share/texlive/texmf-dist/tex/latex/l3kernel/expl3-code.tex | |
(/usr/share/texlive/texmf-dist/tex/latex/etex-pkg/etex.sty)) | |
(/usr/share/texlive/texmf-dist/tex/latex/l3kernel/l3unicode-data.def) | |
(/usr/share/texlive/texmf-dist/tex/latex/l3kernel/l3xdvipdfmx.def)) | |
(/usr/share/texlive/texmf-dist/tex/latex/l3packages/xparse/xparse.sty) | |
(/usr/share/texlive/texmf-dist/tex/latex/fontspec/fontspec-patches.sty) | |
(/usr/share/texlive/texmf-dist/tex/latex/fontspec/fontspec-xetex.sty | |
(/usr/share/texlive/texmf-dist/tex/latex/base/fontenc.sty | |
(/usr/share/texlive/texmf-dist/tex/latex/euenc/eu1enc.def) | |
(/usr/share/texlive/texmf-dist/tex/latex/euenc/eu1lmr.fd)) | |
(/usr/share/texlive/texmf-dist/tex/xelatex/xunicode/xunicode.sty | |
(/usr/share/texmf/tex/latex/tipa/t3enc.def | |
(/usr/share/texlive/texmf-dist/tex/latex/euenc/eu1lmss.fd)) | |
(/usr/share/texlive/texmf-dist/tex/latex/graphics/graphicx.sty | |
(/usr/share/texlive/texmf-dist/tex/latex/graphics/keyval.sty) | |
(/usr/share/texlive/texmf-dist/tex/latex/graphics/graphics.sty | |
(/usr/share/texlive/texmf-dist/tex/latex/graphics/trig.sty) | |
(/usr/share/texlive/texmf-dist/tex/latex/latexconfig/graphics.cfg) | |
(/usr/share/texlive/texmf-dist/tex/xelatex/xetex-def/xetex.def)))) | |
(/usr/share/texlive/texmf-dist/tex/latex/fontspec/fontspec.cfg))) | |
(./fncychap.sty) (/usr/share/texlive/texmf-dist/tex/latex/tools/longtable.sty) | |
(./sphinx.sty (/usr/share/texlive/texmf-dist/tex/latex/fancyhdr/fancyhdr.sty) | |
(/usr/share/texlive/texmf-dist/tex/latex/base/textcomp.sty | |
(/usr/share/texlive/texmf-dist/tex/latex/base/ts1enc.def)) | |
(/usr/share/texlive/texmf-dist/tex/latex/fancybox/fancybox.sty | |
Style option: `fancybox' v1.4 <2010/05/15> (tvz) | |
) (/usr/share/texlive/texmf-dist/tex/latex/titlesec/titlesec.sty) | |
(./tabulary.sty (/usr/share/texlive/texmf-dist/tex/latex/tools/array.sty)) | |
(/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsmath.sty | |
For additional information on amsmath, use the `?' option. | |
(/usr/share/texlive/texmf-dist/tex/latex/amsmath/amstext.sty | |
(/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsgen.sty)) | |
(/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsbsy.sty) | |
(/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsopn.sty)) | |
(/usr/share/texlive/texmf-dist/tex/latex/base/makeidx.sty) | |
(/usr/share/texlive/texmf-dist/tex/latex/framed/framed.sty) | |
(/usr/share/texlive/texmf-dist/tex/latex/base/ifthen.sty) | |
(/usr/share/texlive/texmf-dist/tex/latex/graphics/color.sty | |
(/usr/share/texlive/texmf-dist/tex/latex/latexconfig/color.cfg)) | |
(/usr/share/texlive/texmf-dist/tex/latex/fancyvrb/fancyvrb.sty | |
Style option: `fancyvrb' v2.7a, with DG/SPQR fixes, and firstline=lastline fix | |
<2008/02/07> (tvz)) | |
(/usr/share/texlive/texmf-dist/tex/latex/threeparttable/threeparttable.sty) | |
(/usr/share/texlive/texmf-dist/tex/latex/mdwtools/footnote.sty) | |
(/usr/share/texlive/texmf-dist/tex/latex/wrapfig/wrapfig.sty) | |
(/usr/share/texlive/texmf-dist/tex/latex/parskip/parskip.sty) | |
(/usr/share/texlive/texmf-dist/tex/latex/base/alltt.sty) | |
(/usr/share/texlive/texmf-dist/tex/latex/upquote/upquote.sty) | |
(/usr/share/texlive/texmf-dist/tex/latex/hyperref/hyperref.sty | |
(/usr/share/texlive/texmf-dist/tex/generic/oberdiek/hobsub-hyperref.sty | |
(/usr/share/texlive/texmf-dist/tex/generic/oberdiek/hobsub-generic.sty)) | |
(/usr/share/texlive/texmf-dist/tex/generic/ifxetex/ifxetex.sty) | |
(/usr/share/texlive/texmf-dist/tex/latex/oberdiek/auxhook.sty) | |
(/usr/share/texlive/texmf-dist/tex/latex/oberdiek/kvoptions.sty) | |
(/usr/share/texlive/texmf-dist/tex/latex/hyperref/pd1enc.def) | |
(/usr/share/texlive/texmf-dist/tex/latex/latexconfig/hyperref.cfg) | |
(/usr/share/texlive/texmf-dist/tex/latex/url/url.sty)) | |
Package hyperref Message: Driver (autodetected): hxetex. | |
(/usr/share/texlive/texmf-dist/tex/latex/hyperref/hxetex.def | |
(/usr/share/texlive/texmf-dist/tex/latex/hyperref/puenc.def) | |
(/usr/share/texlive/texmf-dist/tex/generic/oberdiek/stringenc.sty) | |
(/usr/share/texlive/texmf-dist/tex/latex/oberdiek/rerunfilecheck.sty)) | |
(/usr/share/texlive/texmf-dist/tex/latex/oberdiek/hypcap.sty) | |
(/usr/share/texlive/texmf-dist/tex/latex/float/float.sty)) | |
(/usr/share/texlive/texmf-dist/tex/latex/multirow/multirow.sty) | |
! Undefined control sequence. | |
l.23 \addto | |
\captionsenglish{\renewcommand{\figurename}{Fig. }} | |
? | |
! Emergency stop. | |
l.23 \addto | |
\captionsenglish{\renewcommand{\figurename}{Fig. }} | |
No pages of output. | |
Transcript written on sympy-0.7.5.log. |
This file has been truncated, but you can view the full file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% Generated by Sphinx. | |
\def\sphinxdocclass{report} | |
\documentclass[letterpaper,10pt,english]{sphinxmanual} | |
\usepackage{cmap} | |
\usepackage{bm} | |
\usepackage{amssymb} | |
\usepackage{fontspec} | |
\defaultfontfeatures{Mapping=tex-text} | |
\setmainfont{DejaVu Serif} | |
\setsansfont{DejaVu Sans} | |
\setmonofont{DejaVu Sans Mono} | |
\usepackage[Bjarne]{fncychap} | |
\usepackage{longtable} | |
\usepackage{sphinx} | |
\usepackage{multirow} | |
\addto\captionsenglish{\renewcommand{\figurename}{Fig. }} | |
\addto\captionsenglish{\renewcommand{\tablename}{Table }} | |
\floatname{literal-block}{Listing } | |
% redefine \LaTeX to be usable in math mode | |
\expandafter\def\expandafter\LaTeX\expandafter{\expandafter\text\expandafter{\LaTeX}} | |
\title{SymPy Documentation} | |
\date{July 15, 2015} | |
\release{0.7.6} | |
\author{SymPy Development Team} | |
\newcommand{\sphinxlogo}{\includegraphics{sympylogo_big.png}\par} | |
\renewcommand{\releasename}{Release} | |
\makeindex | |
\makeatletter | |
\def\PYG@reset{\let\PYG@it=\relax \let\PYG@bf=\relax% | |
\let\PYG@ul=\relax \let\PYG@tc=\relax% | |
\let\PYG@bc=\relax \let\PYG@ff=\relax} | |
\def\PYG@tok#1{\csname PYG@tok@#1\endcsname} | |
\def\PYG@toks#1+{\ifx\relax#1\empty\else% | |
\PYG@tok{#1}\expandafter\PYG@toks\fi} | |
\def\PYG@do#1{\PYG@bc{\PYG@tc{\PYG@ul{% | |
\PYG@it{\PYG@bf{\PYG@ff{#1}}}}}}} | |
\def\PYG#1#2{\PYG@reset\PYG@toks#1+\relax+\PYG@do{#2}} | |
\expandafter\def\csname PYG@tok@gd\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.63,0.00,0.00}{##1}}} | |
\expandafter\def\csname PYG@tok@gu\endcsname{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.50,0.00,0.50}{##1}}} | |
\expandafter\def\csname PYG@tok@gt\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.00,0.27,0.87}{##1}}} | |
\expandafter\def\csname PYG@tok@gs\endcsname{\let\PYG@bf=\textbf} | |
\expandafter\def\csname PYG@tok@gr\endcsname{\def\PYG@tc##1{\textcolor[rgb]{1.00,0.00,0.00}{##1}}} | |
\expandafter\def\csname PYG@tok@cm\endcsname{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.25,0.50,0.56}{##1}}} | |
\expandafter\def\csname PYG@tok@vg\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.73,0.38,0.84}{##1}}} | |
\expandafter\def\csname PYG@tok@m\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.13,0.50,0.31}{##1}}} | |
\expandafter\def\csname PYG@tok@mh\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.13,0.50,0.31}{##1}}} | |
\expandafter\def\csname PYG@tok@cs\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.25,0.50,0.56}{##1}}\def\PYG@bc##1{\setlength{\fboxsep}{0pt}\colorbox[rgb]{1.00,0.94,0.94}{\strut ##1}}} | |
\expandafter\def\csname PYG@tok@ge\endcsname{\let\PYG@it=\textit} | |
\expandafter\def\csname PYG@tok@vc\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.73,0.38,0.84}{##1}}} | |
\expandafter\def\csname PYG@tok@il\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.13,0.50,0.31}{##1}}} | |
\expandafter\def\csname PYG@tok@go\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.20,0.20,0.20}{##1}}} | |
\expandafter\def\csname PYG@tok@cp\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.00,0.44,0.13}{##1}}} | |
\expandafter\def\csname PYG@tok@gi\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.00,0.63,0.00}{##1}}} | |
\expandafter\def\csname PYG@tok@gh\endcsname{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.00,0.50}{##1}}} | |
\expandafter\def\csname PYG@tok@ni\endcsname{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.84,0.33,0.22}{##1}}} | |
\expandafter\def\csname PYG@tok@nl\endcsname{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.13,0.44}{##1}}} | |
\expandafter\def\csname PYG@tok@nn\endcsname{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.05,0.52,0.71}{##1}}} | |
\expandafter\def\csname PYG@tok@no\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.38,0.68,0.84}{##1}}} | |
\expandafter\def\csname PYG@tok@na\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.25,0.44,0.63}{##1}}} | |
\expandafter\def\csname PYG@tok@nb\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.00,0.44,0.13}{##1}}} | |
\expandafter\def\csname PYG@tok@nc\endcsname{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.05,0.52,0.71}{##1}}} | |
\expandafter\def\csname PYG@tok@nd\endcsname{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.33,0.33,0.33}{##1}}} | |
\expandafter\def\csname PYG@tok@ne\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.00,0.44,0.13}{##1}}} | |
\expandafter\def\csname PYG@tok@nf\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.02,0.16,0.49}{##1}}} | |
\expandafter\def\csname PYG@tok@si\endcsname{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.44,0.63,0.82}{##1}}} | |
\expandafter\def\csname PYG@tok@s2\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.25,0.44,0.63}{##1}}} | |
\expandafter\def\csname PYG@tok@vi\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.73,0.38,0.84}{##1}}} | |
\expandafter\def\csname PYG@tok@nt\endcsname{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.02,0.16,0.45}{##1}}} | |
\expandafter\def\csname PYG@tok@nv\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.73,0.38,0.84}{##1}}} | |
\expandafter\def\csname PYG@tok@s1\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.25,0.44,0.63}{##1}}} | |
\expandafter\def\csname PYG@tok@gp\endcsname{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.78,0.36,0.04}{##1}}} | |
\expandafter\def\csname PYG@tok@sh\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.25,0.44,0.63}{##1}}} | |
\expandafter\def\csname PYG@tok@ow\endcsname{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.44,0.13}{##1}}} | |
\expandafter\def\csname PYG@tok@sx\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.78,0.36,0.04}{##1}}} | |
\expandafter\def\csname PYG@tok@bp\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.00,0.44,0.13}{##1}}} | |
\expandafter\def\csname PYG@tok@c1\endcsname{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.25,0.50,0.56}{##1}}} | |
\expandafter\def\csname PYG@tok@kc\endcsname{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.44,0.13}{##1}}} | |
\expandafter\def\csname PYG@tok@c\endcsname{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.25,0.50,0.56}{##1}}} | |
\expandafter\def\csname PYG@tok@mf\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.13,0.50,0.31}{##1}}} | |
\expandafter\def\csname PYG@tok@err\endcsname{\def\PYG@bc##1{\setlength{\fboxsep}{0pt}\fcolorbox[rgb]{1.00,0.00,0.00}{1,1,1}{\strut ##1}}} | |
\expandafter\def\csname PYG@tok@mb\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.13,0.50,0.31}{##1}}} | |
\expandafter\def\csname PYG@tok@ss\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.32,0.47,0.09}{##1}}} | |
\expandafter\def\csname PYG@tok@sr\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.14,0.33,0.53}{##1}}} | |
\expandafter\def\csname PYG@tok@mo\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.13,0.50,0.31}{##1}}} | |
\expandafter\def\csname PYG@tok@kd\endcsname{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.44,0.13}{##1}}} | |
\expandafter\def\csname PYG@tok@mi\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.13,0.50,0.31}{##1}}} | |
\expandafter\def\csname PYG@tok@kn\endcsname{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.44,0.13}{##1}}} | |
\expandafter\def\csname PYG@tok@o\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} | |
\expandafter\def\csname PYG@tok@kr\endcsname{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.44,0.13}{##1}}} | |
\expandafter\def\csname PYG@tok@s\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.25,0.44,0.63}{##1}}} | |
\expandafter\def\csname PYG@tok@kp\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.00,0.44,0.13}{##1}}} | |
\expandafter\def\csname PYG@tok@w\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.73,0.73,0.73}{##1}}} | |
\expandafter\def\csname PYG@tok@kt\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.56,0.13,0.00}{##1}}} | |
\expandafter\def\csname PYG@tok@sc\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.25,0.44,0.63}{##1}}} | |
\expandafter\def\csname PYG@tok@sb\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.25,0.44,0.63}{##1}}} | |
\expandafter\def\csname PYG@tok@k\endcsname{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.44,0.13}{##1}}} | |
\expandafter\def\csname PYG@tok@se\endcsname{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.25,0.44,0.63}{##1}}} | |
\expandafter\def\csname PYG@tok@sd\endcsname{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.25,0.44,0.63}{##1}}} | |
\def\PYGZbs{\char`\\} | |
\def\PYGZus{\char`\_} | |
\def\PYGZob{\char`\{} | |
\def\PYGZcb{\char`\}} | |
\def\PYGZca{\char`\^} | |
\def\PYGZam{\char`\&} | |
\def\PYGZlt{\char`\<} | |
\def\PYGZgt{\char`\>} | |
\def\PYGZsh{\char`\#} | |
\def\PYGZpc{\char`\%} | |
\def\PYGZdl{\char`\$} | |
\def\PYGZhy{\char`\-} | |
\def\PYGZsq{\char`\'} | |
\def\PYGZdq{\char`\"} | |
\def\PYGZti{\char`\~} | |
% for compatibility with earlier versions | |
\def\PYGZat{@} | |
\def\PYGZlb{[} | |
\def\PYGZrb{]} | |
\makeatother | |
\renewcommand\PYGZsq{\textquotesingle} | |
\begin{document} | |
\maketitle | |
\tableofcontents | |
\phantomsection\label{index::doc} | |
\chapter{Installation} | |
\label{install:installation}\label{install::doc}\label{install:id1} | |
The SymPy CAS can be installed on virtually any computer with Python 2.6 or | |
above. SymPy does not require any special Python modules: let us know if you | |
have any problems with SymPy on a standard Python install. The current | |
recommended method of installation is directly from the source files. | |
Alternatively, executables are available for Windows, and some Linux | |
distributions have SymPy packages available. | |
SymPy officially supports Python 2.6, 2.7, 3.2, 3.3, 3.4, and PyPy. | |
\section{Source} | |
\label{install:source} | |
SymPy currently recommends that users install directly from the source files. | |
You will first have to download the source files via the archive. Download the | |
latest release (tar.gz) from the \href{https://github.com/sympy/sympy/releases}{downloads site} and open it with your | |
operating system's standard decompression utility. | |
After the download is complete, you should have a folder called ``sympy''. From | |
your favorite command line terminal, change directory into that folder and | |
execute the following: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYGZdl{} python setup.py install | |
\end{Verbatim} | |
Alternatively, if you don't want to install the package onto your computer, you | |
may run SymPy with the ``isympy'' console (which automatically imports SymPy | |
packages and defines common symbols) by executing within the ``sympy'' folder: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYGZdl{} ./bin/isympy | |
\end{Verbatim} | |
You may now run SymPy statements directly within the Python shell: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{\PYGZus{}\PYGZus{}future\PYGZus{}\PYGZus{}} \PYG{k+kn}{import} \PYG{n}{division} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{o}{*} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{z}\PYG{p}{,} \PYG{n}{t} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x y z t}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{k}\PYG{p}{,} \PYG{n}{m}\PYG{p}{,} \PYG{n}{n} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{k m n}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{integer}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{f}\PYG{p}{,} \PYG{n}{g}\PYG{p}{,} \PYG{n}{h} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{f g h}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{cls}\PYG{o}{=}\PYG{n}{Function}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{diff}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{o}{/}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{x} | |
\end{Verbatim} | |
\section{Git} | |
\label{install:git} | |
If you are a developer or like to get the latest updates as they come, be sure | |
to install from git. To download the repository, execute the following from the | |
command line: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYGZdl{} git clone git://github.com/sympy/sympy.git | |
\end{Verbatim} | |
Then, execute either the \(setup.py\) or the \(bin/isympy\) scripts as demonstrated | |
above. | |
To update to the latest version, go into your repository and execute: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYGZdl{} git pull origin master | |
\end{Verbatim} | |
If you want to install SymPy, but still want to use the git version, you can run | |
from your repository: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYGZdl{} setupegg.py develop | |
\end{Verbatim} | |
This will cause the installed version to always point to the version in the git | |
directory. | |
\section{Anaconda} | |
\label{install:anaconda} | |
Although SymPy does not have any hard dependencies, many nice features are | |
only enabled when certain libraries are installed. For example, without | |
Matplotlib, only simple text-based plotting is enabled. With the IPython | |
notebook or qtconsole, you can get nicer \(\LaTeX\) printing by running | |
\code{init\_printing()}. An easy way to get all these libraries in addition to | |
SymPy is to install \href{http://continuum.io/downloads}{Anaconda}, which is | |
a free Python distribution from Continuum Analytics that includes SymPy, | |
Matplotlib, IPython, NumPy, and many more useful packages for scientific | |
computing. | |
\section{Other Methods} | |
\label{install:other-methods} | |
An installation executable (.exe) is available for Windows users at the | |
\href{https://github.com/sympy/sympy/releases}{downloads site}. In addition, various Linux distributions have SymPy | |
available as a package. Others are strongly encouraged to download from source | |
(details above). | |
\section{Run SymPy} | |
\label{install:run-sympy} | |
After installation, it is best to verify that your freshly-installed SymPy | |
works. To do this, start up Python and import the SymPy libraries: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYGZdl{} python | |
\PYGZgt{}\PYGZgt{}\PYGZgt{} from sympy import * | |
\end{Verbatim} | |
From here, execute some simple SymPy statements like the ones below: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x} \PYG{o}{=} \PYG{n}{Symbol}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{limit}\PYG{p}{(}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{/}\PYG{n}{x}\PYG{p}{,} \PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{)} | |
\PYG{g+go}{1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{integrate}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{o}{/}\PYG{n}{x}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{log(x)} | |
\end{Verbatim} | |
For a starter guide on using SymPy effectively, refer to the {\hyperref[tutorial/index:tutorial]{\emph{\DUspan{}{SymPy Tutorial}}}} (\autopageref*{tutorial/index:tutorial}). | |
\section{Questions} | |
\label{install:questions} | |
If you have a question about installation or SymPy in general, feel free to | |
visit our chat on \href{https://gitter.im/sympy/sympy}{Gitter}. In addition, our \href{http://groups.google.com/group/sympy}{mailing list} is an excellent | |
source of community support. | |
If you think there's a bug or you would like to request a feature, please open | |
an \href{https://github.com/sympy/sympy/issues}{issue ticket}. | |
\chapter{SymPy Tutorial} | |
\label{tutorial/index:mailing-list}\label{tutorial/index:sympy-tutorial}\label{tutorial/index::doc}\label{tutorial/index:tutorial} | |
\section{Preliminaries} | |
\label{tutorial/preliminaries::doc}\label{tutorial/preliminaries:preliminaries} | |
This tutorial assumes that the reader already knows the basics of the Python programming | |
language. If you do not, the \href{http://docs.python.org/3/tutorial/index.html}{official Python | |
tutorial} is excellent. | |
This tutorial assumes a decent mathematical background. Most examples require | |
knowledge lower than a calculus level, and some require knowledge at a | |
calculus level. Some of the advanced features require more than this. If you | |
come across a section that uses some mathematical function you are not | |
familiar with, you can probably skip over it, or replace it with a similar one | |
that you are more familiar with. Or look up the function on Wikipedia and | |
learn something new. Some important mathematical concepts that are not common | |
knowledge will be introduced as necessary. | |
\subsection{Installation} | |
\label{tutorial/preliminaries:installation}\setbox0\vbox{ | |
\begin{minipage}{0.95\linewidth} | |
\textbf{Quick Tip} | |
\medskip | |
You do not need to install SymPy to try it. You can use the online shell | |
at \href{http://live.sympy.org}{http://live.sympy.org}, or the shell at the bottom right of this | |
documentation page. | |
\end{minipage}} | |
\begin{center}\setlength{\fboxsep}{5pt}\shadowbox{\box0}\end{center} | |
You will need to install SymPy first. See the {\hyperref[install:installation]{\emph{\DUspan{}{installation guide}}}} (\autopageref*{install:installation}). | |
Alternately, you can just use the SymPy Live Sphinx extension to run the code | |
blocks in the browser. For example, click on the green ``Run code block in | |
SymPy Live'' button below | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{o}{*} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a} \PYG{o}{=} \PYG{n}{Integral}\PYG{p}{(}\PYG{n}{cos}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{n}{exp}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Eq}\PYG{p}{(}\PYG{n}{a}\PYG{p}{,} \PYG{n}{a}\PYG{o}{.}\PYG{n}{doit}\PYG{p}{(}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{Integral(exp(x)*cos(x), x) == exp(x)*sin(x)/2 + exp(x)*cos(x)/2} | |
\end{Verbatim} | |
The SymPy Live shell in the bottom corner will pop up and evaluate the code | |
block. You can also click any individual line to evaluate it one at a time. | |
The SymPy Live shell is a fully interactive Python shell. You can type any | |
expression in the input box to evaluate it. Feel free to use it throughout | |
the tutorial to experiment. | |
To show or hide the SymPy Live shell at any time, just click the green button | |
on the bottom right of the screen. | |
By default, the SymPy Live shell uses \(\LaTeX\) for output. If you want the | |
output to look more like the output in the documentation, change the | |
output format to \code{Str} or \code{Unicode} in the settings. | |
If you wish to modify an example before evaluating it, change the evaluation | |
mode to ``copy'' in the SymPy Live settings. This will cause clicking on an | |
example to copy the example to the SymPy Live shell, but not evaluate it, | |
allowing you to change it before execution. You can also use the up/down | |
arrow keys on your keyboard in the input box to move through the shell | |
history. | |
The SymPy Live shell is also available at \href{http://live.sympy.org}{http://live.sympy.org}, with extra | |
features, like a mobile phone enhanced version and saved history. | |
\subsection{Exercises} | |
\label{tutorial/preliminaries:exercises} | |
This tutorial was the basis for a tutorial given at the 2013 SciPy conference | |
in Austin, TX. The website for that tutorial is \href{http://certik.github.io/scipy-2013-tutorial/html/index.html}{here}. It has links | |
to videos, materials, and IPython notebook exercises. The IPython notebook | |
exercises in particular are recommended to anyone going through this tutorial. | |
\subsection{About This Tutorial} | |
\label{tutorial/preliminaries:about-this-tutorial} | |
This tutorial aims to give an introduction to SymPy for someone who has not | |
used the library before. Many features of SymPy will be introduced in this | |
tutorial, but they will not be exhaustive. In fact, virtually every | |
functionality shown in this tutorial will have more options or capabilities | |
than what will be shown. The rest of the SymPy documentation serves as API | |
documentation, which extensively lists every feature and option of each | |
function. | |
These are the goals of this tutorial: | |
\begin{itemize} | |
\item {} | |
To give a guide, suitable for someone who has never used SymPy (but who has | |
used Python and knows the necessary mathematics). | |
\item {} | |
To be written in a narrative format, which is both easy and fun to follow. | |
It should read like a book. | |
\item {} | |
To give insightful examples and exercises, to help the reader learn and to | |
make it entertaining to work through. | |
\item {} | |
To introduce concepts in a logical order. | |
\end{itemize} | |
\begin{itemize} | |
\item {} | |
To use good practices and idioms, and avoid antipatterns. Functions or | |
methodologies that tend to lead to antipatterns are avoided. Features that | |
are only useful to advanced users are not shown. | |
\item {} | |
To be consistent. If there are multiple ways to do it, only the best way is | |
shown. | |
\end{itemize} | |
\begin{itemize} | |
\item {} | |
To avoid unnecessary duplication, it is assumed that previous sections of | |
the tutorial have already been read. | |
\end{itemize} | |
Feedback on this tutorial, or on SymPy in general is always welcome. Just | |
write to our \href{https://groups.google.com/forum/?fromgroups\#!forum/sympy}{mailing list}. | |
\section{Introduction} | |
\label{tutorial/intro:introduction}\label{tutorial/intro::doc} | |
\subsection{What is Symbolic Computation?} | |
\label{tutorial/intro:what-is-symbolic-computation} | |
Symbolic computation deals with the computation of mathematical objects | |
symbolically. This means that the mathematical objects are represented | |
exactly, not approximately, and mathematical expressions with unevaluated | |
variables are left in symbolic form. | |
Let's take an example. Say we wanted to use the built-in Python functions to | |
compute square roots. We might do something like this | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{import} \PYG{n+nn}{math} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{math}\PYG{o}{.}\PYG{n}{sqrt}\PYG{p}{(}\PYG{l+m+mi}{9}\PYG{p}{)} | |
\PYG{g+go}{3.0} | |
\end{Verbatim} | |
9 is a perfect square, so we got the exact answer, 3. But suppose we computed | |
the square root of a number that isn't a perfect square | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{math}\PYG{o}{.}\PYG{n}{sqrt}\PYG{p}{(}\PYG{l+m+mi}{8}\PYG{p}{)} | |
\PYG{g+go}{2.82842712475} | |
\end{Verbatim} | |
Here we got an approximate result. 2.82842712475 is not the exact square root | |
of 8 (indeed, the actual square root of 8 cannot be represented by a finite | |
decimal, since it is an irrational number). If all we cared about was the | |
decimal form of the square root of 8, we would be done. | |
But suppose we want to go further. Recall that \(\sqrt{8} = \sqrt{4\cdot 2} = | |
2\sqrt{2}\). We would have a hard time deducing this from the above result. | |
This is where symbolic computation comes in. With a symbolic computation | |
system like SymPy, square roots of numbers that are not perfect squares are | |
left unevaluated by default | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{import} \PYG{n+nn}{sympy} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{sympy}\PYG{o}{.}\PYG{n}{sqrt}\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{p}{)} | |
\PYG{g+go}{sqrt(3)} | |
\end{Verbatim} | |
Furthermore---and this is where we start to see the real power of symbolic | |
computation---symbolic results can be symbolically simplified. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{sympy}\PYG{o}{.}\PYG{n}{sqrt}\PYG{p}{(}\PYG{l+m+mi}{8}\PYG{p}{)} | |
\PYG{g+go}{2*sqrt(2)} | |
\end{Verbatim} | |
\subsection{A More Interesting Example} | |
\label{tutorial/intro:a-more-interesting-example} | |
The above example starts to show how we can manipulate irrational numbers | |
exactly using SymPy. But it is much more powerful than that. Symbolic | |
computation systems (which by the way, are also often called computer algebra | |
systems, or just CASs) such as SymPy are capable of computing symbolic | |
expressions with variables. | |
As we will see later, in SymPy, variables are defined using \code{symbols}. | |
Unlike many symbolic manipulation systems, variables in SymPy must be defined | |
before they are used (the reason for this will be discussed in the {\hyperref[tutorial/gotchas:tutorial-gotchas-symbols]{\emph{\DUspan{}{next | |
section}}}} (\autopageref*{tutorial/gotchas:tutorial-gotchas-symbols})). | |
Let us define a symbolic expression, representing the mathematical expression | |
\(x + 2y\). | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{symbols} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x}\PYG{p}{,} \PYG{n}{y} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x y}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} \PYG{o}{=} \PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{y} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} | |
\PYG{g+go}{x + 2*y} | |
\end{Verbatim} | |
Note that we wrote \code{x + 2*y} just as we would if \code{x} and \code{y} were | |
ordinary Python variables. But in this case, instead of evaluating to | |
something, the expression remains as just \code{x + 2*y}. Now let us play around | |
with it: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} \PYG{o}{+} \PYG{l+m+mi}{1} | |
\PYG{g+go}{x + 2*y + 1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} \PYG{o}{\PYGZhy{}} \PYG{n}{x} | |
\PYG{g+go}{2*y} | |
\end{Verbatim} | |
Notice something in the above example. When we typed \code{expr - x}, we did not | |
get \code{x + 2*y - x}, but rather just \code{2*y}. The \code{x} and the \code{-x} | |
automatically canceled one another. This is similar to how \code{sqrt(8)} | |
automatically turned into \code{2*sqrt(2)} above. This isn't always the case in | |
SymPy, however: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x}\PYG{o}{*}\PYG{n}{expr} | |
\PYG{g+go}{x*(x + 2*y)} | |
\end{Verbatim} | |
Here, we might have expected \(x(x + 2y)\) to transform into \(x^2 + 2xy\), but | |
instead we see that the expression was left alone. This is a common theme in | |
SymPy. Aside from obvious simplifications like \(x - x = 0\) and \(\sqrt{8} = | |
2\sqrt{2}\), most simplifications are not performed automatically. This is | |
because we might prefer the factored form \(x(x + 2y)\), or we might prefer the | |
expanded form \(x^2 + 2xy\). Both forms are useful in different circumstances. | |
In SymPy, there are functions to go from one form to the other | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{expand}\PYG{p}{,} \PYG{n}{factor} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expanded\PYGZus{}expr} \PYG{o}{=} \PYG{n}{expand}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{n}{expr}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expanded\PYGZus{}expr} | |
\PYG{g+go}{x**2 + 2*x*y} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{factor}\PYG{p}{(}\PYG{n}{expanded\PYGZus{}expr}\PYG{p}{)} | |
\PYG{g+go}{x*(x + 2*y)} | |
\end{Verbatim} | |
\subsection{The Power of Symbolic Computation} | |
\label{tutorial/intro:the-power-of-symbolic-computation} | |
The real power of a symbolic computation system such as SymPy is the ability | |
to do all sorts of computations symbolically. SymPy can simplify expressions, | |
compute derivatives, integrals, and limits, solve equations, work with | |
matrices, and much, much more, and do it all symbolically. It includes | |
modules for plotting, printing (like 2D pretty printed output of math | |
formulas, or \(\LaTeX\)), code generation, physics, statistics, combinatorics, | |
number theory, geometry, logic, and more. Here is a small sampling of the sort | |
of symbolic power SymPy is capable of, to whet your appetite. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{o}{*} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x}\PYG{p}{,} \PYG{n}{t}\PYG{p}{,} \PYG{n}{z}\PYG{p}{,} \PYG{n}{nu} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x t z nu}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\end{Verbatim} | |
This will make all further examples pretty print with unicode characters. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{init\PYGZus{}printing}\PYG{p}{(}\PYG{n}{use\PYGZus{}unicode}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\end{Verbatim} | |
Take the derivative of \(\sin{(x)}e^x\). | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{diff}\PYG{p}{(}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{n}{exp}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{ x x} | |
\PYG{g+go}{e ⋅sin(x) + e ⋅cos(x)} | |
\end{Verbatim} | |
Compute \(\int(e^x\sin{(x)} + e^x\cos{(x)})\,dx\). | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{integrate}\PYG{p}{(}\PYG{n}{exp}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} \PYG{o}{+} \PYG{n}{exp}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{n}{cos}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{ x} | |
\PYG{g+go}{e ⋅sin(x)} | |
\end{Verbatim} | |
Compute \(\int_{-\infty}^\infty \sin{(x^2)}\,dx\). | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{integrate}\PYG{p}{(}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{,} \PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{o}{\PYGZhy{}}\PYG{n}{oo}\PYG{p}{,} \PYG{n}{oo}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{ \PYGZus{}\PYGZus{}\PYGZus{} \PYGZus{}\PYGZus{}\PYGZus{}} | |
\PYG{g+go}{\textbackslash{}╱ 2 ⋅\textbackslash{}╱ \(\pi\)} | |
\PYG{g+go}{-----------} | |
\PYG{g+go}{ 2} | |
\end{Verbatim} | |
Find \(\lim_{x\to 0}\frac{\sin{(x)}}{x}\). | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{limit}\PYG{p}{(}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{/}\PYG{n}{x}\PYG{p}{,} \PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{)} | |
\PYG{g+go}{1} | |
\end{Verbatim} | |
Solve \(x^2 - 2 = 0\). | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{solve}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{⎡ \PYGZus{}\PYGZus{}\PYGZus{} \PYGZus{}\PYGZus{}\PYGZus{}⎤} | |
\PYG{g+go}{⎣\PYGZhy{}\textbackslash{}╱ 2 , \textbackslash{}╱ 2 ⎦} | |
\end{Verbatim} | |
Solve the differential equation \(y'' - y = e^t\). | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{y} \PYG{o}{=} \PYG{n}{Function}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{y}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{dsolve}\PYG{p}{(}\PYG{n}{Eq}\PYG{p}{(}\PYG{n}{y}\PYG{p}{(}\PYG{n}{t}\PYG{p}{)}\PYG{o}{.}\PYG{n}{diff}\PYG{p}{(}\PYG{n}{t}\PYG{p}{,} \PYG{n}{t}\PYG{p}{)} \PYG{o}{\PYGZhy{}} \PYG{n}{y}\PYG{p}{(}\PYG{n}{t}\PYG{p}{)}\PYG{p}{,} \PYG{n}{exp}\PYG{p}{(}\PYG{n}{t}\PYG{p}{)}\PYG{p}{)}\PYG{p}{,} \PYG{n}{y}\PYG{p}{(}\PYG{n}{t}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{ \PYGZhy{}t ⎛ t⎞ t} | |
\PYG{g+go}{y(t) = C2⋅e + ⎜C1 + -⎟⋅e} | |
\PYG{g+go}{ ⎝ 2⎠} | |
\end{Verbatim} | |
Find the eigenvalues of \(\left[\begin{smallmatrix}1 & 2\\2 & | |
2\end{smallmatrix}\right]\). | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Matrix}\PYG{p}{(}\PYG{p}{[}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{]}\PYG{p}{]}\PYG{p}{)}\PYG{o}{.}\PYG{n}{eigenvals}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{⎧ \PYGZus{}\PYGZus{}\PYGZus{}\PYGZus{} \PYGZus{}\PYGZus{}\PYGZus{}\PYGZus{} ⎫} | |
\PYG{g+go}{⎪3 \textbackslash{}╱ 17 \textbackslash{}╱ 17 3 ⎪} | |
\PYG{g+go}{⎨- + ------: 1, \PYGZhy{} ------ + -: 1⎬} | |
\PYG{g+go}{⎪2 2 2 2 ⎪} | |
\PYG{g+go}{⎩ ⎭} | |
\end{Verbatim} | |
Rewrite the Bessel function \(J_{\nu}\left(z\right)\) in terms of the | |
spherical Bessel function \(j_\nu(z)\). | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{besselj}\PYG{p}{(}\PYG{n}{nu}\PYG{p}{,} \PYG{n}{z}\PYG{p}{)}\PYG{o}{.}\PYG{n}{rewrite}\PYG{p}{(}\PYG{n}{jn}\PYG{p}{)} | |
\PYG{g+go}{ \PYGZus{}\PYGZus{}\PYGZus{} \PYGZus{}\PYGZus{}\PYGZus{}} | |
\PYG{g+go}{\textbackslash{}╱ 2 ⋅\textbackslash{}╱ z ⋅jn(\(\nu\) \PYGZhy{} 1/2, z)} | |
\PYG{g+go}{--------------------------} | |
\PYG{g+go}{ \PYGZus{}\PYGZus{}\PYGZus{}} | |
\PYG{g+go}{ \textbackslash{}╱ \(\pi\)} | |
\end{Verbatim} | |
Print \(\int_{0}^{\pi} \cos^{2}{\left (x \right )}\, dx\) using \(\LaTeX\). | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{latex}\PYG{p}{(}\PYG{n}{Integral}\PYG{p}{(}\PYG{n}{cos}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{,} \PYG{n}{pi}\PYG{p}{)}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{\PYGZbs{}int\PYGZus{}\PYGZob{}0\PYGZcb{}\PYGZca{}\PYGZob{}\PYGZbs{}pi\PYGZcb{} \PYGZbs{}cos\PYGZca{}\PYGZob{}2\PYGZcb{}\PYGZob{}\PYGZbs{}left (x \PYGZbs{}right )\PYGZcb{}\PYGZbs{}, dx} | |
\end{Verbatim} | |
\subsection{Why SymPy?} | |
\label{tutorial/intro:why-sympy} | |
There are many computer algebra systems out there. \href{http://en.wikipedia.org/wiki/List\_of\_computer\_algebra\_systems}{This} Wikipedia | |
article lists many of them. What makes SymPy a better choice than the | |
alternatives? | |
First off, SymPy is completely free. It is open source, and licensed under the | |
liberal BSD license, so you can modify the source code and even sell it if you | |
want to. This contrasts with popular commercial systems like Maple or | |
Mathematica that cost hundreds of dollars in licenses. | |
Second, SymPy uses Python. Most computer algebra systems invent their own | |
language. Not SymPy. SymPy is written entirely in Python, and is executed | |
entirely in Python. This means that if you already know Python, it is much | |
easier to get started with SymPy, because you already know the syntax (and if | |
you don't know Python, it is really easy to learn). We already know that | |
Python is a well-designed, battle-tested language. The SymPy developers are | |
confident in their abilities in writing mathematical software, but programming | |
language design is a completely different thing. By reusing an existing | |
language, we are able to focus on those things that matter: the mathematics. | |
Another computer algebra system, Sage also uses Python as its language. But | |
Sage is large, with a download of over a gigabyte. An advantage of SymPy is | |
that it is lightweight. In addition to being relatively small, it has no | |
dependencies other than Python, so it can be used almost anywhere easily. | |
Furthermore, the goals of Sage and the goals of SymPy are different. Sage | |
aims to be a full featured system for mathematics, and aims to do so by | |
compiling all the major open source mathematical systems together into | |
one. When you call some function in Sage, such as \code{integrate}, it calls out | |
to one of the open source packages that it includes. In fact, SymPy is | |
included in Sage. SymPy on the other hand aims to be an independent system, | |
with all the features implemented in SymPy itself. | |
A final important feature of SymPy is that it can be used as a library. Many | |
computer algebra systems focus on being usable in interactive environments, but | |
if you wish to automate or extend them, it is difficult to do. With SymPy, | |
you can just as easily use it in an interactive Python environment or import | |
it in your own Python application. SymPy also provides APIs to make it easy | |
to extend it with your own custom functions. | |
\section{Gotchas} | |
\label{tutorial/gotchas:gotchas}\label{tutorial/gotchas::doc} | |
To begin, we should make something about SymPy clear. SymPy is nothing more | |
than a Python library, like \code{NumPy}, \code{Django}, or even modules in the | |
Python standard library \code{sys} or \code{re}. What this means is that SymPy does | |
not add anything to the Python language. Limitations that are inherent in the | |
Python language are also inherent in SymPy. It also means that SymPy tries to | |
use Python idioms whenever possible, making programming with SymPy easy for | |
those already familiar with programming with Python. As a simple example, | |
SymPy uses Python syntax to build expressions. Implicit multiplication (like | |
\code{3x} or \code{3 x}) is not allowed in Python, and thus not allowed in SymPy. | |
To multiply \code{3} and \code{x}, you must type \code{3*x} with the \code{*}. | |
\subsection{Symbols} | |
\label{tutorial/gotchas:symbols}\label{tutorial/gotchas:tutorial-gotchas-symbols} | |
One consequence of this fact is that SymPy can be used in any environment | |
where Python is available. We just import it, like we would any other | |
library: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{o}{*} | |
\end{Verbatim} | |
This imports all the functions and classes from SymPy into our interactive | |
Python session. Now, suppose we start to do a computation. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1} | |
\PYG{g+gt}{Traceback (most recent call last):} | |
\PYG{c}{...} | |
\PYG{g+gr}{NameError}: \PYG{n}{name \PYGZsq{}x\PYGZsq{} is not defined} | |
\PYG{g+gt}{Traceback (most recent call last):} | |
\PYG{c}{...} | |
\PYG{g+gr}{NameError}: \PYG{n}{name \PYGZsq{}x\PYGZsq{} is not defined} | |
\end{Verbatim} | |
Oops! What happened here? We tried to use the variable \code{x}, but it tells us | |
that \code{x} is not defined. In Python, variables have no meaning until they | |
are defined. SymPy is no different. Unlike many symbolic manipulation | |
systems you may have used, in SymPy, variables are not defined automatically. | |
To define variables, we must use \code{symbols}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1} | |
\PYG{g+go}{x + 1} | |
\end{Verbatim} | |
\code{symbols} takes a string of variable names separated by spaces or commas, | |
and creates Symbols out of them. We can then assign these to variable names. | |
Later, we will investigate some convenient ways we can work around this issue. | |
For now, let us just define the most common variable names, \code{x}, \code{y}, and | |
\code{z}, for use through the rest of this section | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{z} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x y z}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\end{Verbatim} | |
As a final note, we note that the name of a Symbol and the name of the | |
variable it is assigned to need not have anything to do with one another. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a}\PYG{p}{,} \PYG{n}{b} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{b a}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a} | |
\PYG{g+go}{b} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{b} | |
\PYG{g+go}{a} | |
\end{Verbatim} | |
Here we have done the very confusing thing of assigning a Symbol with the name | |
\code{a} to the variable \code{b}, and a Symbol of the name \code{b} to the variable | |
\code{a}. Now the Python variable named \code{a} points to the SymPy Symbol named | |
\code{b}, and visa versa. How confusing. We could have also done something like | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{crazy} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{unrelated}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{crazy} \PYG{o}{+} \PYG{l+m+mi}{1} | |
\PYG{g+go}{unrelated + 1} | |
\end{Verbatim} | |
This also shows that Symbols can have names longer than one character if we | |
want. | |
Usually, the best practice is to assign Symbols to Python variables of the | |
same name, although there are exceptions: Symbol names can contain characters | |
that are not allowed in Python variable names, or may just want to avoid | |
typing long names by assigning Symbols with long names to single letter Python | |
variables. | |
To avoid confusion, throughout this tutorial, Symbol names and Python variable | |
names will always coincide. Furthermore, the word ``Symbol'' will refer to a | |
SymPy Symbol and the word ``variable'' will refer to a Python variable. | |
Finally, let us be sure we understand the difference between SymPy Symbols and | |
Python variables. Consider the following: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{n}{x} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{n}{expr} \PYG{o}{=} \PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1} | |
\PYG{n}{x} \PYG{o}{=} \PYG{l+m+mi}{2} | |
\PYG{k}{print}\PYG{p}{(}\PYG{n}{expr}\PYG{p}{)} | |
\end{Verbatim} | |
What do you think the output of this code will be? If you thought \code{3}, | |
you're wrong. Let's see what really happens | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} \PYG{o}{=} \PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x} \PYG{o}{=} \PYG{l+m+mi}{2} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{n}{expr}\PYG{p}{)} | |
\PYG{g+go}{x + 1} | |
\end{Verbatim} | |
Changing \code{x} to \code{2} had no effect on \code{expr}. This is because \code{x = 2} | |
changes the Python variable \code{x} to \code{2}, but has no effect on the SymPy | |
Symbol \code{x}, which was what we used in creating \code{expr}. When we created | |
\code{expr}, the Python variable \code{x} was a Symbol. After we created, it, we | |
changed the Python variable \code{x} to 2. But \code{expr} remains the same. This | |
behavior is not unique to SymPy. All Python programs work this way: if a | |
variable is changed, expressions that were already created with that variable | |
do not change automatically. For example | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x} \PYG{o}{=} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{abc}\PYG{l+s}{\PYGZsq{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} \PYG{o}{=} \PYG{n}{x} \PYG{o}{+} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{def}\PYG{l+s}{\PYGZsq{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} | |
\PYG{g+go}{\PYGZsq{}abcdef\PYGZsq{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x} \PYG{o}{=} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{ABC}\PYG{l+s}{\PYGZsq{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} | |
\PYG{g+go}{\PYGZsq{}abcdef\PYGZsq{}} | |
\end{Verbatim} | |
\setbox0\vbox{ | |
\begin{minipage}{0.95\linewidth} | |
\textbf{Quick Tip} | |
\medskip | |
To change the value of a Symbol in an expression, use \code{subs} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} \PYG{o}{=} \PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr}\PYG{o}{.}\PYG{n}{subs}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+go}{3} | |
\end{Verbatim} | |
\end{minipage}} | |
\begin{center}\setlength{\fboxsep}{5pt}\shadowbox{\box0}\end{center} | |
In this example, if we want to know what \code{expr} is with the new value of | |
\code{x}, we need to reevaluate the code that created \code{expr}, namely, \code{expr = | |
x + 1}. This can be complicated if several lines created \code{expr}. One | |
advantage of using a symbolic computation system like SymPy is that we can | |
build a symbolic representation for \code{expr}, and then substitute \code{x} with | |
values. The correct way to do this in SymPy is to use \code{subs}, which will be | |
discussed in more detail later. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} \PYG{o}{=} \PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr}\PYG{o}{.}\PYG{n}{subs}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+go}{3} | |
\end{Verbatim} | |
\subsection{Equals signs} | |
\label{tutorial/gotchas:equals-signs}\label{tutorial/gotchas:tutorial-gotchas-equals} | |
Another very important consequence of the fact that SymPy does not extend | |
Python syntax is that \code{=} does not represent equality in SymPy. Rather it | |
is Python variable assignment. This is hard-coded into the Python language, | |
and SymPy makes no attempts to change that. | |
You may think, however, that \code{==}, which is used for equality testing in | |
Python, is used for SymPy as equality. This is not quite correct either. Let | |
us see what happens when we use \code{==}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1} \PYG{o}{==} \PYG{l+m+mi}{4} | |
\PYG{g+go}{False} | |
\end{Verbatim} | |
Instead of treating \code{x + 1 == 4} symbolically, we just got \code{False}. In | |
SymPy, \code{==} represents exact structural equality testing. This means that | |
\code{a == b} means that we are \emph{asking} if \(a = b\). We always get a \code{bool} as | |
the result of \code{==}. There is a separate object, called \code{Eq}, which can be | |
used to create symbolic equalities | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Eq}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{4}\PYG{p}{)} | |
\PYG{g+go}{x + 1 == 4} | |
\end{Verbatim} | |
There is one additional caveat about \code{==} as well. Suppose we want to know | |
if \((x + 1)^2 = x^2 + 2x + 1\). We might try something like this: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{==} \PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1} | |
\PYG{g+go}{False} | |
\end{Verbatim} | |
We got \code{False} again. However, \((x + 1)^2\) \emph{does} equal \(x^2 + 2x + 1\). What | |
is going on here? Did we find a bug in SymPy, or is it just not powerful | |
enough to recognize this basic algebraic fact? | |
Recall from above that \code{==} represents \emph{exact} structural equality testing. | |
``Exact'' here means that two expressions will compare equal with \code{==} only if | |
they are exactly equal structurally. Here, \((x + 1)^2\) and \(x^2 + 2x + 1\) are | |
not the same symbolically. One is the power of an addition of two terms, and | |
the other is the addition of three terms. | |
It turns out that when using SymPy as a library, having \code{==} test for exact | |
symbolic equality is far more useful than having it represent symbolic | |
equality, or having it test for mathematical equality. However, as a new | |
user, you will probably care more about the latter two. We have already seen | |
an alternative to representing equalities symbolically, \code{Eq}. To test if | |
two things are equal, it is best to recall the basic fact that if \(a = b\), | |
then \(a - b = 0\). Thus, the best way to check if \(a = b\) is to take \(a - b\) | |
and simplify it, and see if it goes to 0. We will learn {\hyperref[tutorial/simplification:tutorial-simplify]{\emph{\DUspan{}{later}}}} (\autopageref*{tutorial/simplification:tutorial-simplify}) that the function to do this is called \code{simplify}. This | |
method is not infallible---in fact, it can be \href{http://en.wikipedia.org/wiki/Richardson\%27s\_theorem}{theoretically proven} that it is impossible | |
to determine if two symbolic expressions are identically equal in | |
general---but for most common expressions, it works quite well. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a} \PYG{o}{=} \PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{b} \PYG{o}{=} \PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{simplify}\PYG{p}{(}\PYG{n}{a} \PYG{o}{\PYGZhy{}} \PYG{n}{b}\PYG{p}{)} | |
\PYG{g+go}{0} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{c} \PYG{o}{=} \PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{simplify}\PYG{p}{(}\PYG{n}{a} \PYG{o}{\PYGZhy{}} \PYG{n}{c}\PYG{p}{)} | |
\PYG{g+go}{4*x} | |
\end{Verbatim} | |
There is also a method called \code{equals} that tests if two expressions are | |
equal by evaluating them numerically at random points. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a} \PYG{o}{=} \PYG{n}{cos}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{\PYGZhy{}} \PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{b} \PYG{o}{=} \PYG{n}{cos}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a}\PYG{o}{.}\PYG{n}{equals}\PYG{p}{(}\PYG{n}{b}\PYG{p}{)} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
\subsection{Two Final Notes: \texttt{\textasciicircum{}} and \texttt{/}} | |
\label{tutorial/gotchas:two-final-notes-and} | |
You may have noticed that we have been using \code{**} for exponentiation instead | |
of the standard \code{\textasciicircum{}}. That's because SymPy follows Python's conventions. In | |
Python, \code{\textasciicircum{}} represents logical exclusive or. SymPy follows this convention: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb+bp}{True} \PYG{o}{\PYGZca{}} \PYG{n+nb+bp}{False} | |
\PYG{g+go}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb+bp}{True} \PYG{o}{\PYGZca{}} \PYG{n+nb+bp}{True} | |
\PYG{g+go}{False} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x}\PYG{o}{\PYGZca{}}\PYG{n}{y} | |
\PYG{g+go}{Xor(x, y)} | |
\end{Verbatim} | |
Finally, a small technical discussion on how SymPy works is in order. When | |
you type something like \code{x + 1}, the SymPy Symbol \code{x} is added to the | |
Python int \code{1}. Python's operator rules then allow SymPy to tell Python | |
that SymPy objects know how to be added to Python ints, and so \code{1} is | |
automatically converted to the SymPy Integer object. | |
This sort of operator magic happens automatically behind the scenes, and you | |
rarely need to even know that it is happening. However, there is one | |
exception. Whenever you combine a SymPy object and a SymPy object, or a SymPy | |
object and a Python object, you get a SymPy object, but whenever you combine | |
two Python objects, SymPy never comes into play, and so you get a Python | |
object. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb}{type}\PYG{p}{(}\PYG{n}{Integer}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{)} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+go}{\PYGZlt{}class \PYGZsq{}sympy.core.numbers.Integer\PYGZsq{}\PYGZgt{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb}{type}\PYG{p}{(}\PYG{l+m+mi}{1} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+go}{\PYGZlt{}... \PYGZsq{}int\PYGZsq{}\PYGZgt{}} | |
\end{Verbatim} | |
This is usually not a big deal. Python ints work much the same as SymPy | |
Integers, but there is one important exception: division. In SymPy, the | |
division of two Integers gives a Rational: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Integer}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{/}\PYG{n}{Integer}\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{p}{)} | |
\PYG{g+go}{1/3} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb}{type}\PYG{p}{(}\PYG{n}{Integer}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{/}\PYG{n}{Integer}\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{\PYGZlt{}class \PYGZsq{}sympy.core.numbers.Rational\PYGZsq{}\PYGZgt{}} | |
\end{Verbatim} | |
But in Python \code{/} represents either integer division or floating point | |
division, depending on whether you are in Python 2 or Python 3, and depending | |
on whether or not you have run \code{from \_\_future\_\_ import division}: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{\PYGZus{}\PYGZus{}future\PYGZus{}\PYGZus{}} \PYG{k+kn}{import} \PYG{n}{division} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{l+m+mi}{1}\PYG{o}{/}\PYG{l+m+mi}{2} | |
\PYG{g+go}{0.5} | |
\end{Verbatim} | |
To avoid this, we can construct the rational object explicitly | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Rational}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+go}{1/2} | |
\end{Verbatim} | |
This problem also comes up whenever we have a larger symbolic expression with | |
\code{int/int} in it. For example: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{o}{/}\PYG{l+m+mi}{2} | |
\PYG{g+go}{x + 0.5} | |
\end{Verbatim} | |
This happens because Python first evaluates \code{1/2} into \code{0.5}, and then | |
that is cast into a SymPy type when it is added to \code{x}. Again, we can get | |
around this by explicitly creating a Rational: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x} \PYG{o}{+} \PYG{n}{Rational}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+go}{x + 1/2} | |
\end{Verbatim} | |
There are several tips on avoiding this situation in the {\hyperref[gotchas:gotchas]{\emph{\DUspan{}{Gotchas and Pitfalls}}}} (\autopageref*{gotchas:gotchas}) | |
document. | |
\subsection{Further Reading} | |
\label{tutorial/gotchas:further-reading} | |
For more discussion on the topics covered in this section, see {\hyperref[gotchas:gotchas]{\emph{\DUspan{}{Gotchas and Pitfalls}}}} (\autopageref*{gotchas:gotchas}). | |
\section{Basic Operations} | |
\label{tutorial/basic_operations:tutorial-basic}\label{tutorial/basic_operations::doc}\label{tutorial/basic_operations:basic-operations} | |
Here we discuss some of the most basic operations needed for expression | |
manipulation in SymPy. Some more advanced operations will be discussed later | |
in the {\hyperref[tutorial/manipulation:tutorial-manipulation]{\emph{\DUspan{}{advanced expression manipulation}}}} (\autopageref*{tutorial/manipulation:tutorial-manipulation}) section. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{o}{*} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{z} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s}{x y z}\PYG{l+s}{\PYGZdq{}}\PYG{p}{)} | |
\end{Verbatim} | |
\subsection{Substitution} | |
\label{tutorial/basic_operations:substitution} | |
One of the most common things you might want to do with a mathematical | |
expression is substitution. Substitution replaces all instances of something | |
in an expression with something else. It is done using the \code{subs} method. | |
For example | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} \PYG{o}{=} \PYG{n}{cos}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} \PYG{o}{+} \PYG{l+m+mi}{1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr}\PYG{o}{.}\PYG{n}{subs}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{)} | |
\PYG{g+go}{cos(y) + 1} | |
\end{Verbatim} | |
Substitution is usually done for one of two reasons: | |
\begin{enumerate} | |
\item {} | |
Evaluating an expression at a point. For example, if our expression is | |
\code{cos(x) + 1} and we want to evaluate it at the point \code{x = 0}, so that | |
we get \code{cos(0) + 1}, which is 2. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr}\PYG{o}{.}\PYG{n}{subs}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{)} | |
\PYG{g+go}{2} | |
\end{Verbatim} | |
\item {} | |
Replacing a subexpression with another subexpression. There are two | |
reasons we might want to do this. The first is if we are trying to build | |
an expression that has some symmetry, such as \(x^{x^{x^x}}\). To build | |
this, we might start with \code{x**y}, and replace \code{y} with \code{x**y}. We | |
would then get \code{x**(x**y)}. If we replaced \code{y} in this new expression | |
with \code{x**x}, we would get \code{x**(x**(x**x))}, the desired expression. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} \PYG{o}{=} \PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{n}{y} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} | |
\PYG{g+go}{x**y} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} \PYG{o}{=} \PYG{n}{expr}\PYG{o}{.}\PYG{n}{subs}\PYG{p}{(}\PYG{n}{y}\PYG{p}{,} \PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} | |
\PYG{g+go}{x**(x**y)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} \PYG{o}{=} \PYG{n}{expr}\PYG{o}{.}\PYG{n}{subs}\PYG{p}{(}\PYG{n}{y}\PYG{p}{,} \PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} | |
\PYG{g+go}{x**(x**(x**x))} | |
\end{Verbatim} | |
The second is if we want to perform a very controlled simplification, or | |
perhaps a simplification that SymPy is otherwise unable to do. For | |
example, say we have \(\sin(2x) + \cos(2x)\), and we want to replace | |
\(\sin(2x)\) with \(2\sin(x)\cos(x)\). As we will learn later, the function | |
\code{expand\_trig} does this. However, this function will also expand | |
\(\cos(2x)\), which we may not want. While there are ways to perform such | |
precise simplification, and we will learn some of them in the | |
{\hyperref[tutorial/manipulation:tutorial-manipulation]{\emph{\DUspan{}{advanced expression manipulation}}}} (\autopageref*{tutorial/manipulation:tutorial-manipulation}) section, an | |
easy way is to just replace \(\sin(2x)\) with \(2\sin(x)\cos(x)\). | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} \PYG{o}{=} \PYG{n}{sin}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)} \PYG{o}{+} \PYG{n}{cos}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expand\PYGZus{}trig}\PYG{p}{(}\PYG{n}{expr}\PYG{p}{)} | |
\PYG{g+go}{2*sin(x)*cos(x) + 2*cos(x)**2 \PYGZhy{} 1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr}\PYG{o}{.}\PYG{n}{subs}\PYG{p}{(}\PYG{n}{sin}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{n}{cos}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{2*sin(x)*cos(x) + cos(2*x)} | |
\end{Verbatim} | |
\end{enumerate} | |
There are two important things to note about \code{subs}. First, it returns a | |
new expression. SymPy objects are immutable. That means that \code{subs} does | |
not modify it in-place. For example | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} \PYG{o}{=} \PYG{n}{cos}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr}\PYG{o}{.}\PYG{n}{subs}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{)} | |
\PYG{g+go}{1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} | |
\PYG{g+go}{cos(x)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x} | |
\PYG{g+go}{x} | |
\end{Verbatim} | |
\setbox0\vbox{ | |
\begin{minipage}{0.95\linewidth} | |
\textbf{Quick Tip} | |
\medskip | |
SymPy expressions are immutable. No function will change them in-place. | |
\end{minipage}} | |
\begin{center}\setlength{\fboxsep}{5pt}\shadowbox{\box0}\end{center} | |
Here, we see that performing \code{expr.subs(x, 0)} leaves \code{expr} unchanged. | |
In fact, since SymPy expressions are immutable, no function will change them | |
in-place. All functions will return new expressions. | |
To perform multiple substitutions at once, pass a list of \code{(old, new)} pairs | |
to \code{subs}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} \PYG{o}{=} \PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{3} \PYG{o}{+} \PYG{l+m+mi}{4}\PYG{o}{*}\PYG{n}{x}\PYG{o}{*}\PYG{n}{y} \PYG{o}{\PYGZhy{}} \PYG{n}{z} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr}\PYG{o}{.}\PYG{n}{subs}\PYG{p}{(}\PYG{p}{[}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{,} \PYG{p}{(}\PYG{n}{y}\PYG{p}{,} \PYG{l+m+mi}{4}\PYG{p}{)}\PYG{p}{,} \PYG{p}{(}\PYG{n}{z}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{)}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+go}{40} | |
\end{Verbatim} | |
It is often useful to combine this with a list comprehension to do a large set | |
of similar replacements all at once. For example, say we had \(x^4 - 4x^3 + 4x^2 - | |
2x + 3\) and we wanted to replace all instances of \(x\) that have an even power | |
with \(y\), to get \(y^4 - 4x^3 + 4y^2 - 2x + 3\). | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} \PYG{o}{=} \PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{4} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{4}\PYG{o}{*}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{3} \PYG{o}{+} \PYG{l+m+mi}{4}\PYG{o}{*}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{3} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{replacements} \PYG{o}{=} \PYG{p}{[}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{n}{i}\PYG{p}{,} \PYG{n}{y}\PYG{o}{*}\PYG{o}{*}\PYG{n}{i}\PYG{p}{)} \PYG{k}{for} \PYG{n}{i} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{l+m+mi}{5}\PYG{p}{)} \PYG{k}{if} \PYG{n}{i} \PYG{o}{\PYGZpc{}} \PYG{l+m+mi}{2} \PYG{o}{==} \PYG{l+m+mi}{0}\PYG{p}{]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr}\PYG{o}{.}\PYG{n}{subs}\PYG{p}{(}\PYG{n}{replacements}\PYG{p}{)} | |
\PYG{g+go}{\PYGZhy{}4*x**3 \PYGZhy{} 2*x + y**4 + 4*y**2 + 3} | |
\end{Verbatim} | |
\subsection{Converting Strings to SymPy Expressions} | |
\label{tutorial/basic_operations:converting-strings-to-sympy-expressions} | |
The \code{sympify} function (that's \code{sympify}, not to be confused with | |
\code{simplify}) can be used to convert strings into SymPy expressions. | |
For example | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{str\PYGZus{}expr} \PYG{o}{=} \PYG{l+s}{\PYGZdq{}}\PYG{l+s}{x**2 + 3*x \PYGZhy{} 1/2}\PYG{l+s}{\PYGZdq{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} \PYG{o}{=} \PYG{n}{sympify}\PYG{p}{(}\PYG{n}{str\PYGZus{}expr}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} | |
\PYG{g+go}{x**2 + 3*x \PYGZhy{} 1/2} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr}\PYG{o}{.}\PYG{n}{subs}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+go}{19/2} | |
\end{Verbatim} | |
\begin{notice}{warning}{Warning:} | |
\code{sympify} uses \code{eval}. Don't use it on unsanitized input. | |
\end{notice} | |
\subsection{\texttt{evalf}} | |
\label{tutorial/basic_operations:evalf} | |
To evaluate a numerical expression into a floating point number, use | |
\code{evalf}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} \PYG{o}{=} \PYG{n}{sqrt}\PYG{p}{(}\PYG{l+m+mi}{8}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr}\PYG{o}{.}\PYG{n}{evalf}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{2.82842712474619} | |
\end{Verbatim} | |
SymPy can evaluate floating point expressions to arbitrary precision. By | |
default, 15 digits of precision are used, but you can pass any number as the | |
argument to \code{evalf}. Let's compute the first 100 digits of \(\pi\). | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{pi}\PYG{o}{.}\PYG{n}{evalf}\PYG{p}{(}\PYG{l+m+mi}{100}\PYG{p}{)} | |
\PYG{g+go}{3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068} | |
\end{Verbatim} | |
To numerically evaluate an expression with a Symbol at a point, we might use | |
\code{subs} followed by \code{evalf}, but it is more efficient and numerically | |
stable to pass the substitution to \code{evalf} using the \code{subs} flag, which | |
takes a dictionary of \code{Symbol: point} pairs. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} \PYG{o}{=} \PYG{n}{cos}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr}\PYG{o}{.}\PYG{n}{evalf}\PYG{p}{(}\PYG{n}{subs}\PYG{o}{=}\PYG{p}{\PYGZob{}}\PYG{n}{x}\PYG{p}{:} \PYG{l+m+mf}{2.4}\PYG{p}{\PYGZcb{}}\PYG{p}{)} | |
\PYG{g+go}{0.0874989834394464} | |
\end{Verbatim} | |
Sometimes there are roundoff errors smaller than the desired precision that | |
remain after an expression is evaluated. Such numbers can be removed at the | |
user's discretion by setting the \code{chop} flag to True. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{one} \PYG{o}{=} \PYG{n}{cos}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{n}{sin}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{one} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{.}\PYG{n}{evalf}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{\PYGZhy{}0.e\PYGZhy{}124} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{one} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{.}\PYG{n}{evalf}\PYG{p}{(}\PYG{n}{chop}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{0} | |
\end{Verbatim} | |
\subsection{\texttt{lambdify}} | |
\label{tutorial/basic_operations:lambdify} | |
\code{subs} and \code{evalf} are good if you want to do simple evaluation, but if | |
you intend to evaluate an expression at many points, there are more efficient | |
ways. For example, if you wanted to evaluate an expression at a thousand | |
points, using SymPy would be far slower than it needs to be, especially if you | |
only care about machine precision. Instead, you should use libraries like | |
\href{http://www.numpy.org/}{NumPy} and \href{http://www.scipy.org/}{SciPy}. | |
The easiest way to convert a SymPy expression to an expression that can be | |
numerically evaluated is to use the \code{lambdify} function. \code{lambdify} acts | |
like a \code{lambda} function, except it converts the SymPy names to the names of | |
the given numerical library, usually NumPy. For example | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{import} \PYG{n+nn}{numpy} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a} \PYG{o}{=} \PYG{n}{numpy}\PYG{o}{.}\PYG{n}{arange}\PYG{p}{(}\PYG{l+m+mi}{10}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} \PYG{o}{=} \PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{f} \PYG{o}{=} \PYG{n}{lambdify}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{expr}\PYG{p}{,} \PYG{l+s}{\PYGZdq{}}\PYG{l+s}{numpy}\PYG{l+s}{\PYGZdq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{f}\PYG{p}{(}\PYG{n}{a}\PYG{p}{)} | |
\PYG{g+go}{[ 0. 0.84147098 0.90929743 0.14112001 \PYGZhy{}0.7568025 \PYGZhy{}0.95892427} | |
\PYG{g+go}{ \PYGZhy{}0.2794155 0.6569866 0.98935825 0.41211849]} | |
\end{Verbatim} | |
You can use other libraries than NumPy. For example, to use the standard | |
library math module, use \code{"math"}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{f} \PYG{o}{=} \PYG{n}{lambdify}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{expr}\PYG{p}{,} \PYG{l+s}{\PYGZdq{}}\PYG{l+s}{math}\PYG{l+s}{\PYGZdq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{f}\PYG{p}{(}\PYG{l+m+mf}{0.1}\PYG{p}{)} | |
\PYG{g+go}{0.0998334166468} | |
\end{Verbatim} | |
To use lambdify with numerical libraries that it does not know about, pass a | |
dictionary of \code{sympy\_name:numerical\_function} pairs. For example | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{def} \PYG{n+nf}{mysin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{:} | |
\PYG{g+gp}{... } \PYG{l+s+sd}{\PYGZdq{}\PYGZdq{}\PYGZdq{}} | |
\PYG{g+gp}{... }\PYG{l+s+sd}{ My sine. Not only accurate for small x.} | |
\PYG{g+gp}{... }\PYG{l+s+sd}{ \PYGZdq{}\PYGZdq{}\PYGZdq{}} | |
\PYG{g+gp}{... } \PYG{k}{return} \PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{f} \PYG{o}{=} \PYG{n}{lambdify}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{expr}\PYG{p}{,} \PYG{p}{\PYGZob{}}\PYG{l+s}{\PYGZdq{}}\PYG{l+s}{sin}\PYG{l+s}{\PYGZdq{}}\PYG{p}{:}\PYG{n}{mysin}\PYG{p}{\PYGZcb{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{f}\PYG{p}{(}\PYG{l+m+mf}{0.1}\PYG{p}{)} | |
\PYG{g+go}{0.1} | |
\end{Verbatim} | |
\section{Printing} | |
\label{tutorial/printing:tutorial-printing}\label{tutorial/printing:printing}\label{tutorial/printing::doc} | |
As we have already seen, SymPy can pretty print its output using Unicode | |
characters. This is a short introduction to the most common printing options | |
available in SymPy. | |
\subsection{Printers} | |
\label{tutorial/printing:printers} | |
There are several printers available in SymPy. The most common ones are | |
\begin{itemize} | |
\item {} | |
str | |
\item {} | |
repr | |
\item {} | |
ASCII pretty printer | |
\item {} | |
Unicode pretty printer | |
\item {} | |
LaTeX | |
\item {} | |
MathML | |
\item {} | |
Dot | |
\end{itemize} | |
In addition to these, there are also ``printers'' that can output SymPy objects | |
to code, such as C, Fortran, Javascript, Theano, and Python. These are not | |
discussed in this tutorial. | |
\subsection{Setting up Pretty Printing} | |
\label{tutorial/printing:setting-up-pretty-printing} | |
If all you want is the best pretty printing, use the \code{init\_printing()} | |
function. This will automatically enable the best printer available in your | |
environment. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{init\PYGZus{}printing} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{init\PYGZus{}printing}\PYG{p}{(}\PYG{p}{)} | |
\end{Verbatim} | |
\setbox0\vbox{ | |
\begin{minipage}{0.95\linewidth} | |
\textbf{Quick Tip} | |
\medskip | |
You an also change the printer used in SymPy Live. Just change the ``Output | |
Format'' in the settings. | |
\end{minipage}} | |
\begin{center}\setlength{\fboxsep}{5pt}\shadowbox{\box0}\end{center} | |
If you plan to work in an interactive calculator-type session, the | |
\code{init\_session()} function will automatically import everything in SymPy, | |
create some common Symbols, setup plotting, and run \code{init\_printing()}. | |
\begin{quote} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{init\PYGZus{}session} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{init\PYGZus{}session}\PYG{p}{(}\PYG{p}{)} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
Python console for SymPy 0.7.3 (Python 2.7.5\PYGZhy{}64\PYGZhy{}bit) (ground types: gmpy) | |
These commands were executed: | |
\PYGZgt{}\PYGZgt{}\PYGZgt{} from \PYGZus{}\PYGZus{}future\PYGZus{}\PYGZus{} import division | |
\PYGZgt{}\PYGZgt{}\PYGZgt{} from sympy import * | |
\PYGZgt{}\PYGZgt{}\PYGZgt{} x, y, z, t = symbols(\PYGZsq{}x y z t\PYGZsq{}) | |
\PYGZgt{}\PYGZgt{}\PYGZgt{} k, m, n = symbols(\PYGZsq{}k m n\PYGZsq{}, integer=True) | |
\PYGZgt{}\PYGZgt{}\PYGZgt{} f, g, h = symbols(\PYGZsq{}f g h\PYGZsq{}, cls=Function) | |
\PYGZgt{}\PYGZgt{}\PYGZgt{} init\PYGZus{}printing() \PYGZsh{} doctest: +SKIP | |
Documentation can be found at http://www.sympy.org | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+go}{\PYGZgt{}\PYGZgt{}\PYGZgt{}} | |
\end{Verbatim} | |
\end{quote} | |
In any case, this is what will happen: | |
\begin{itemize} | |
\item {} | |
In the IPython QTConsole, if \(\LaTeX\) is installed, it will enable a printer | |
that uses \(\LaTeX\). | |
\includegraphics{ipythonqtconsole.png} | |
If \(\LaTeX\) is not installed, but Matplotlib is installed, it will use the | |
Matplotlib rendering engine. If Matplotlib is not installed, it uses the | |
Unicode pretty printer. | |
\item {} | |
In the IPython notebook, it will use MathJax to render \(\LaTeX\). | |
\includegraphics{ipythonnotebook.png} | |
\item {} | |
In an IPython console session, or a regular Python session, it will use the | |
Unicode pretty printer if the terminal supports Unicode. | |
\includegraphics{consoleunicode.png} | |
\item {} | |
In a terminal that does not support Unicode, the ASCII pretty printer is | |
used. | |
\includegraphics{consoleascii.png} | |
\end{itemize} | |
To explicitly not use \(\LaTeX\), pass \code{use\_latex=False} to \code{init\_printing()} | |
or \code{init\_session()}. To explicitly not use Unicode, pass | |
\code{use\_unicode=False}. | |
\subsection{Printing Functions} | |
\label{tutorial/printing:printing-functions} | |
In addition to automatic printing, you can explicitly use any one of the | |
printers by calling the appropriate function. | |
\subsubsection{str} | |
\label{tutorial/printing:str} | |
To get a string form of an expression, use \code{str(expr)}. This is also the | |
form that is produced by \code{print(expr)}. String forms are designed to be | |
easy to read, but in a form that is correct Python syntax so that it can be | |
copied and pasted. The \code{str()} form of an expression will usually look | |
exactly the same as the expression as you would enter it. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{o}{*} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{z} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x y z}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb}{str}\PYG{p}{(}\PYG{n}{Integral}\PYG{p}{(}\PYG{n}{sqrt}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{o}{/}\PYG{n}{x}\PYG{p}{)}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{\PYGZsq{}Integral(sqrt(1/x), x)\PYGZsq{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{n}{Integral}\PYG{p}{(}\PYG{n}{sqrt}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{o}{/}\PYG{n}{x}\PYG{p}{)}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{Integral(sqrt(1/x), x)} | |
\end{Verbatim} | |
\subsubsection{repr} | |
\label{tutorial/printing:repr} | |
The repr form of an expression is designed to show the exact form of an | |
expression. It will be discussed more in the {\hyperref[tutorial/manipulation:tutorial-manipulation]{\emph{\DUspan{}{Advanced Expression Manipulation}}}} (\autopageref*{tutorial/manipulation:tutorial-manipulation}) | |
section. To get it, use \code{srepr()} \footnote{ | |
SymPy does not use the Python builtin \code{repr()} function for | |
repr printing, because in Python \code{str(list)} calls \code{repr()} on the | |
elements of the list, and some SymPy functions return lists (such as | |
\code{solve()}). Since \code{srepr()} is so verbose, it is unlikely that anyone | |
would want it called by default on the output of \code{solve()}. | |
}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{srepr}\PYG{p}{(}\PYG{n}{Integral}\PYG{p}{(}\PYG{n}{sqrt}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{o}{/}\PYG{n}{x}\PYG{p}{)}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{\PYGZdq{}Integral(Pow(Pow(Symbol(\PYGZsq{}x\PYGZsq{}), Integer(\PYGZhy{}1)), Rational(1, 2)), Tuple(Symbol(\PYGZsq{}x\PYGZsq{})))\PYGZdq{}} | |
\end{Verbatim} | |
The repr form is mostly useful for understanding how an expression is built | |
internally. | |
\subsubsection{ASCII Pretty Printer} | |
\label{tutorial/printing:ascii-pretty-printer} | |
The ASCII pretty printer is accessed from \code{pprint()}. If the terminal does | |
not support Unicode, the ASCII printer is used by default. Otherwise, you | |
must pass \code{use\_unicode=False}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{pprint}\PYG{p}{(}\PYG{n}{Integral}\PYG{p}{(}\PYG{n}{sqrt}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{o}{/}\PYG{n}{x}\PYG{p}{)}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)}\PYG{p}{,} \PYG{n}{use\PYGZus{}unicode}\PYG{o}{=}\PYG{n+nb+bp}{False}\PYG{p}{)} | |
\PYG{g+go}{ /} | |
\PYG{g+go}{ \textbar{}} | |
\PYG{g+go}{ \textbar{} \PYGZus{}\PYGZus{}\PYGZus{}} | |
\PYG{g+go}{ \textbar{} / 1} | |
\PYG{g+go}{ \textbar{} / \PYGZhy{} dx} | |
\PYG{g+go}{ \textbar{} \PYGZbs{}/ x} | |
\PYG{g+go}{ \textbar{}} | |
\PYG{g+go}{/} | |
\end{Verbatim} | |
\code{pprint()} prints the output to the screen. If you want the string form, | |
use \code{pretty()}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{pretty}\PYG{p}{(}\PYG{n}{Integral}\PYG{p}{(}\PYG{n}{sqrt}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{o}{/}\PYG{n}{x}\PYG{p}{)}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)}\PYG{p}{,} \PYG{n}{use\PYGZus{}unicode}\PYG{o}{=}\PYG{n+nb+bp}{False}\PYG{p}{)} | |
\PYG{g+go}{\PYGZsq{} / \PYGZbs{}n \textbar{} \PYGZbs{}n \textbar{} \PYGZus{}\PYGZus{}\PYGZus{} \PYGZbs{}n \textbar{} / 1 \PYGZbs{}n \textbar{} / \PYGZhy{} dx\PYGZbs{}n \textbar{} \PYGZbs{}\PYGZbs{}/ x \PYGZbs{}n \textbar{} \PYGZbs{}n/ \PYGZsq{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{n}{pretty}\PYG{p}{(}\PYG{n}{Integral}\PYG{p}{(}\PYG{n}{sqrt}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{o}{/}\PYG{n}{x}\PYG{p}{)}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)}\PYG{p}{,} \PYG{n}{use\PYGZus{}unicode}\PYG{o}{=}\PYG{n+nb+bp}{False}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{ /} | |
\PYG{g+go}{ \textbar{}} | |
\PYG{g+go}{ \textbar{} \PYGZus{}\PYGZus{}\PYGZus{}} | |
\PYG{g+go}{ \textbar{} / 1} | |
\PYG{g+go}{ \textbar{} / \PYGZhy{} dx} | |
\PYG{g+go}{ \textbar{} \PYGZbs{}/ x} | |
\PYG{g+go}{ \textbar{}} | |
\PYG{g+go}{/} | |
\end{Verbatim} | |
\subsubsection{Unicode Pretty Printer} | |
\label{tutorial/printing:unicode-pretty-printer} | |
The Unicode pretty printer is also accessed from \code{pprint()} and | |
\code{pretty()}. It the terminal supports Unicode, it is used automatically. If | |
\code{pprint()} is not able to detect that the terminal supports unicode, you can | |
pass \code{use\_unicode=True} to force it to use Unicode. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{pprint}\PYG{p}{(}\PYG{n}{Integral}\PYG{p}{(}\PYG{n}{sqrt}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{o}{/}\PYG{n}{x}\PYG{p}{)}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)}\PYG{p}{,} \PYG{n}{use\PYGZus{}unicode}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{⌠} | |
\PYG{g+go}{⎮ \PYGZus{}\PYGZus{}\PYGZus{}} | |
\PYG{g+go}{⎮ ╱ 1} | |
\PYG{g+go}{⎮ ╱ - dx} | |
\PYG{g+go}{⎮ \textbackslash{}╱ x} | |
\PYG{g+go}{⌡} | |
\end{Verbatim} | |
\subsubsection{\(\LaTeX\)} | |
\label{tutorial/printing:latex}\label{tutorial/printing:id2} | |
To get the \(\LaTeX\) form of an expression, use \code{latex()}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{n}{latex}\PYG{p}{(}\PYG{n}{Integral}\PYG{p}{(}\PYG{n}{sqrt}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{o}{/}\PYG{n}{x}\PYG{p}{)}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{\PYGZbs{}int \PYGZbs{}sqrt\PYGZob{}\PYGZbs{}frac\PYGZob{}1\PYGZcb{}\PYGZob{}x\PYGZcb{}\PYGZcb{}\PYGZbs{}, dx} | |
\end{Verbatim} | |
The \code{latex()} function has many options to change the formatting of | |
different things. See {\hyperref[modules/printing:sympy.printing.latex.latex]{\emph{\code{its documentation}}}} (\autopageref*{modules/printing:sympy.printing.latex.latex}) for more details. | |
\subsubsection{MathML} | |
\label{tutorial/printing:mathml} | |
There is also a printer to MathML, called \code{print\_mathml()}. It must be | |
imported from \code{sympy.printing.mathml}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.printing.mathml} \PYG{k+kn}{import} \PYG{n}{print\PYGZus{}mathml} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{print\PYGZus{}mathml}\PYG{p}{(}\PYG{n}{Integral}\PYG{p}{(}\PYG{n}{sqrt}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{o}{/}\PYG{n}{x}\PYG{p}{)}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{\PYGZlt{}apply\PYGZgt{}} | |
\PYG{g+go}{ \PYGZlt{}int/\PYGZgt{}} | |
\PYG{g+go}{ \PYGZlt{}bvar\PYGZgt{}} | |
\PYG{g+go}{ \PYGZlt{}ci\PYGZgt{}x\PYGZlt{}/ci\PYGZgt{}} | |
\PYG{g+go}{ \PYGZlt{}/bvar\PYGZgt{}} | |
\PYG{g+go}{ \PYGZlt{}apply\PYGZgt{}} | |
\PYG{g+go}{ \PYGZlt{}root/\PYGZgt{}} | |
\PYG{g+go}{ \PYGZlt{}apply\PYGZgt{}} | |
\PYG{g+go}{ \PYGZlt{}power/\PYGZgt{}} | |
\PYG{g+go}{ \PYGZlt{}ci\PYGZgt{}x\PYGZlt{}/ci\PYGZgt{}} | |
\PYG{g+go}{ \PYGZlt{}cn\PYGZgt{}\PYGZhy{}1\PYGZlt{}/cn\PYGZgt{}} | |
\PYG{g+go}{ \PYGZlt{}/apply\PYGZgt{}} | |
\PYG{g+go}{ \PYGZlt{}/apply\PYGZgt{}} | |
\PYG{g+go}{\PYGZlt{}/apply\PYGZgt{}} | |
\end{Verbatim} | |
\code{print\_mathml()} prints the output. If you want the string, use the | |
function \code{mathml()}. | |
\subsubsection{Dot} | |
\label{tutorial/printing:dot} | |
The \code{dotprint()} function in \code{sympy.printing.dot} prints output to dot | |
format, which can be rendered with Graphviz. See the | |
{\hyperref[tutorial/manipulation:tutorial-manipulation]{\emph{\DUspan{}{Advanced Expression Manipulation}}}} (\autopageref*{tutorial/manipulation:tutorial-manipulation}) section for some examples of the output of this | |
printer. | |
\section{Simplification} | |
\label{tutorial/simplification:simplification}\label{tutorial/simplification::doc}\label{tutorial/simplification:tutorial-simplify} | |
To make this document easier to read, we are going to enable pretty printing. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{o}{*} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{z} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x y z}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{init\PYGZus{}printing}\PYG{p}{(}\PYG{n}{use\PYGZus{}unicode}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\end{Verbatim} | |
\subsection{\texttt{simplify}} | |
\label{tutorial/simplification:simplify} | |
Now let's jump in and do some interesting mathematics. One of the most useful | |
features of a symbolic manipulation system is the ability to simplify | |
mathematical expressions. SymPy has dozens of functions to perform various | |
kinds of simplification. There is also one general function called | |
\code{simplify()} that attempts to apply all of these functions in an intelligent | |
way to arrive at the simplest form of an expression. Here are some examples | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{simplify}\PYG{p}{(}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{n}{cos}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+go}{1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{simplify}\PYG{p}{(}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{3} \PYG{o}{+} \PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{\PYGZhy{}} \PYG{n}{x} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{/}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{x \PYGZhy{} 1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{simplify}\PYG{p}{(}\PYG{n}{gamma}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{/}\PYG{n}{gamma}\PYG{p}{(}\PYG{n}{x} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{(x \PYGZhy{} 2)⋅(x \PYGZhy{} 1)} | |
\end{Verbatim} | |
Here, \code{gamma(x)} is \(\Gamma(x)\), the \href{http://en.wikipedia.org/wiki/Gamma\_function}{gamma function}. We see that \code{simplify()} | |
is capable of handling a large class of expressions. | |
But \code{simplify()} has a pitfall. It just applies all the major | |
simplification operations in SymPy, and uses heuristics to determine the | |
simplest result. But ``simplest'' is not a well-defined term. For example, say | |
we wanted to ``simplify'' \(x^2 + 2x + 1\) into \((x + 1)^2\): | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{simplify}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+go}{ 2} | |
\PYG{g+go}{x + 2⋅x + 1} | |
\end{Verbatim} | |
We did not get what we want. There is a function to perform this | |
simplification, called \code{factor()}, which will be discussed below. | |
Another pitfall to \code{simplify()} is that it can be unnecessarily slow, since | |
it tries many kinds of simplifications before picking the best one. If you | |
already know exactly what kind of simplification you are after, it is better | |
to apply the specific simplification function(s) that apply those | |
simplifications. | |
Applying specific simplification functions instead of \code{simplify()} also has | |
the advantage that specific functions have certain guarantees about the form | |
of their output. These will be discussed with each function below. For | |
example, \code{factor()}, when called on a polynomial with rational coefficients, | |
is guaranteed to factor the polynomial into irreducible factors. | |
\code{simplify()} has no guarantees. It is entirely heuristical, and, as we saw | |
above, it may even miss a possible type of simplification that SymPy is | |
capable of doing. | |
\code{simplify()} is best when used interactively, when you just want to whittle | |
down an expression to a simpler form. You may then choose to apply specific | |
functions once you see what \code{simplify()} returns, to get a more precise | |
result. It is also useful when you have no idea what form an expression will | |
take, and you need a catchall function to simplify it. | |
\subsection{Polynomial/Rational Function Simplification} | |
\label{tutorial/simplification:polynomial-rational-function-simplification} | |
\subsubsection{expand} | |
\label{tutorial/simplification:expand} | |
\code{expand()} is one of the most common simplification functions in SymPy. | |
Although it has a lot of scopes, for now, we will consider its function in | |
expanding polynomial expressions. For example: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expand}\PYG{p}{(}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+go}{ 2} | |
\PYG{g+go}{x + 2⋅x + 1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expand}\PYG{p}{(}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{*}\PYG{p}{(}\PYG{n}{x} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{3}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{ 2} | |
\PYG{g+go}{x \PYGZhy{} x \PYGZhy{} 6} | |
\end{Verbatim} | |
Given a polynomial, \code{expand()} will put it into a canonical form of a sum of | |
monomials. | |
\code{expand()} may not sound like a simplification function. After all, by its | |
very name, it makes expressions bigger, not smaller. Usually this is the | |
case, but often an expression will become smaller upon calling \code{expand()} on | |
it due to cancellation. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expand}\PYG{p}{(}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{*}\PYG{p}{(}\PYG{n}{x} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{2}\PYG{p}{)} \PYG{o}{\PYGZhy{}} \PYG{p}{(}\PYG{n}{x} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{\PYGZhy{}2} | |
\end{Verbatim} | |
\subsubsection{factor} | |
\label{tutorial/simplification:factor} | |
\code{factor()} takes a polynomial and factors it into irreducible factors over | |
the rational numbers. For example: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{factor}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{3} \PYG{o}{\PYGZhy{}} \PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{n}{x} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+go}{ ⎛ 2 ⎞} | |
\PYG{g+go}{(x \PYGZhy{} 1)⋅⎝x + 1⎠} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{factor}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{z} \PYG{o}{+} \PYG{l+m+mi}{4}\PYG{o}{*}\PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{o}{*}\PYG{n}{z} \PYG{o}{+} \PYG{l+m+mi}{4}\PYG{o}{*}\PYG{n}{y}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{z}\PYG{p}{)} | |
\PYG{g+go}{ 2} | |
\PYG{g+go}{z⋅(x + 2⋅y)} | |
\end{Verbatim} | |
For polynomials, \code{factor()} is the opposite of \code{expand()}. \code{factor()} | |
uses a complete multivariate factorization algorithm over the rational | |
numbers, which means that each of the factors returned by \code{factor()} is | |
guaranteed to be irreducible. | |
If you are interested in the factors themselves, \code{factor\_list} returns a | |
more structured output. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{factor\PYGZus{}list}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{z} \PYG{o}{+} \PYG{l+m+mi}{4}\PYG{o}{*}\PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{o}{*}\PYG{n}{z} \PYG{o}{+} \PYG{l+m+mi}{4}\PYG{o}{*}\PYG{n}{y}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{z}\PYG{p}{)} | |
\PYG{g+go}{(1, [(z, 1), (x + 2⋅y, 2)])} | |
\end{Verbatim} | |
Note that the input to \code{factor} and \code{expand} need not be polynomials in | |
the strict sense. They will intelligently factor or expand any kind of | |
expression (though note that the factors may not be irreducible if the input | |
is no longer a polynomial over the rationals). | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expand}\PYG{p}{(}\PYG{p}{(}\PYG{n}{cos}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} \PYG{o}{+} \PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+go}{ 2 2} | |
\PYG{g+go}{sin (x) + 2⋅sin(x)⋅cos(x) + cos (x)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{factor}\PYG{p}{(}\PYG{n}{cos}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{cos}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} \PYG{o}{+} \PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+go}{ 2} | |
\PYG{g+go}{(sin(x) + cos(x))} | |
\end{Verbatim} | |
\subsubsection{collect} | |
\label{tutorial/simplification:collect} | |
\code{collect()} collects common powers of a term in an expression. For example | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} \PYG{o}{=} \PYG{n}{x}\PYG{o}{*}\PYG{n}{y} \PYG{o}{+} \PYG{n}{x} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{3} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{\PYGZhy{}} \PYG{n}{z}\PYG{o}{*}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{3} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} | |
\PYG{g+go}{ 3 2 2} | |
\PYG{g+go}{x \PYGZhy{} x ⋅z + 2⋅x + x⋅y + x \PYGZhy{} 3} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{collected\PYGZus{}expr} \PYG{o}{=} \PYG{n}{collect}\PYG{p}{(}\PYG{n}{expr}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{collected\PYGZus{}expr} | |
\PYG{g+go}{ 3 2} | |
\PYG{g+go}{x + x ⋅(\PYGZhy{}z + 2) + x⋅(y + 1) \PYGZhy{} 3} | |
\end{Verbatim} | |
\code{collect()} is particularly useful in conjunction with the \code{.coeff()} | |
method. \code{expr.coeff(x, n)} gives the coefficient of \code{x**n} in \code{expr}: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{collected\PYGZus{}expr}\PYG{o}{.}\PYG{n}{coeff}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+go}{\PYGZhy{}z + 2} | |
\end{Verbatim} | |
\subsubsection{cancel} | |
\label{tutorial/simplification:cancel} | |
\code{cancel()} will take any rational function and put it into the standard | |
canonical form, \(\frac{p}{q}\), where \(p\) and \(q\) are expanded polynomials with | |
no common factors, and the leading coefficients of \(p\) and \(q\) do not have | |
denominators (i.e., are integers). | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{cancel}\PYG{p}{(}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{/}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{n}{x}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{x + 1} | |
\PYG{g+go}{-----} | |
\PYG{g+go}{ x} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} \PYG{o}{=} \PYG{l+m+mi}{1}\PYG{o}{/}\PYG{n}{x} \PYG{o}{+} \PYG{p}{(}\PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{x}\PYG{o}{/}\PYG{l+m+mi}{2} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{/}\PYG{p}{(}\PYG{n}{x} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{4}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} | |
\PYG{g+go}{3⋅x} | |
\PYG{g+go}{--- \PYGZhy{} 2} | |
\PYG{g+go}{ 2 1} | |
\PYG{g+go}{------- + -} | |
\PYG{g+go}{ x \PYGZhy{} 4 x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{cancel}\PYG{p}{(}\PYG{n}{expr}\PYG{p}{)} | |
\PYG{g+go}{ 2} | |
\PYG{g+go}{3⋅x \PYGZhy{} 2⋅x \PYGZhy{} 8} | |
\PYG{g+go}{--------------} | |
\PYG{g+go}{ 2} | |
\PYG{g+go}{ 2⋅x \PYGZhy{} 8⋅x} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} \PYG{o}{=} \PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{o}{*}\PYG{n}{z} \PYG{o}{+} \PYG{n}{x}\PYG{o}{*}\PYG{n}{z}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{n}{y}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{y}\PYG{o}{*}\PYG{n}{z} \PYG{o}{+} \PYG{n}{z}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{/}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} | |
\PYG{g+go}{ 2 2 2 2} | |
\PYG{g+go}{x⋅y \PYGZhy{} 2⋅x⋅y⋅z + x⋅z + y \PYGZhy{} 2⋅y⋅z + z} | |
\PYG{g+go}{---------------------------------------} | |
\PYG{g+go}{ 2} | |
\PYG{g+go}{ x \PYGZhy{} 1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{cancel}\PYG{p}{(}\PYG{n}{expr}\PYG{p}{)} | |
\PYG{g+go}{ 2 2} | |
\PYG{g+go}{y \PYGZhy{} 2⋅y⋅z + z} | |
\PYG{g+go}{---------------} | |
\PYG{g+go}{ x \PYGZhy{} 1} | |
\end{Verbatim} | |
Note that since \code{factor()} will completely factorize both the numerator and | |
the denominator of an expression, it can also be used to do the same thing: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{factor}\PYG{p}{(}\PYG{n}{expr}\PYG{p}{)} | |
\PYG{g+go}{ 2} | |
\PYG{g+go}{(y \PYGZhy{} z)} | |
\PYG{g+go}{--------} | |
\PYG{g+go}{ x \PYGZhy{} 1} | |
\end{Verbatim} | |
However, if you are only interested in making sure that the expression is in | |
canceled form, \code{cancel()} is more efficient than \code{factor()}. | |
\subsubsection{apart} | |
\label{tutorial/simplification:apart} | |
\code{apart()} performs a \href{http://en.wikipedia.org/wiki/Partial\_fraction\_decomposition}{partial fraction decomposition} on a rational | |
function. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} \PYG{o}{=} \PYG{p}{(}\PYG{l+m+mi}{4}\PYG{o}{*}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{3} \PYG{o}{+} \PYG{l+m+mi}{21}\PYG{o}{*}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{l+m+mi}{10}\PYG{o}{*}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{12}\PYG{p}{)}\PYG{o}{/}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{4} \PYG{o}{+} \PYG{l+m+mi}{5}\PYG{o}{*}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{3} \PYG{o}{+} \PYG{l+m+mi}{5}\PYG{o}{*}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{l+m+mi}{4}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} | |
\PYG{g+go}{ 3 2} | |
\PYG{g+go}{4⋅x + 21⋅x + 10⋅x + 12} | |
\PYG{g+go}{------------------------} | |
\PYG{g+go}{ 4 3 2} | |
\PYG{g+go}{ x + 5⋅x + 5⋅x + 4⋅x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{apart}\PYG{p}{(}\PYG{n}{expr}\PYG{p}{)} | |
\PYG{g+go}{ 2⋅x \PYGZhy{} 1 1 3} | |
\PYG{g+go}{---------- \PYGZhy{} ----- + -} | |
\PYG{g+go}{ 2 x + 4 x} | |
\PYG{g+go}{x + x + 1} | |
\end{Verbatim} | |
\subsection{Trigonometric Simplification} | |
\label{tutorial/simplification:trigonometric-simplification} | |
\begin{notice}{note}{Note:} | |
SymPy follows Python's naming conventions for inverse trigonometric | |
functions, which is to append an \code{a} to the front of the function's | |
name. For example, the inverse cosine, or arc cosine, is called \code{acos()}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{acos}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{acos(x)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{cos}\PYG{p}{(}\PYG{n}{acos}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{asin}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+go}{\(\pi\)} | |
\PYG{g+go}{-} | |
\PYG{g+go}{2} | |
\end{Verbatim} | |
\end{notice} | |
\subsubsection{trigsimp} | |
\label{tutorial/simplification:trigsimp} | |
To simplify expressions using trigonometric identities, use \code{trigsimp()}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{trigsimp}\PYG{p}{(}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{n}{cos}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+go}{1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{trigsimp}\PYG{p}{(}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{4} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{cos}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{n}{cos}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{4}\PYG{p}{)} | |
\PYG{g+go}{cos(4⋅x) 1} | |
\PYG{g+go}{-------- + -} | |
\PYG{g+go}{ 2 2} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{trigsimp}\PYG{p}{(}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{n}{tan}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{/}\PYG{n}{sec}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{ 2} | |
\PYG{g+go}{sin (x)} | |
\end{Verbatim} | |
\code{trigsimp()} also works with hyperbolic trig functions. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{trigsimp}\PYG{p}{(}\PYG{n}{cosh}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{n}{sinh}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+go}{cosh(2⋅x)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{trigsimp}\PYG{p}{(}\PYG{n}{sinh}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{/}\PYG{n}{tanh}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{cosh(x)} | |
\end{Verbatim} | |
Much like \code{simplify()}, \code{trigsimp()} applies various trigonometric identities to | |
the input expression, and then uses a heuristic to return the ``best'' one. | |
\subsubsection{expand\_trig} | |
\label{tutorial/simplification:expand-trig} | |
To expand trigonometric functions, that is, apply the sum or double angle | |
identities, use \code{expand\_trig()}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expand\PYGZus{}trig}\PYG{p}{(}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{n}{y}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{sin(x)⋅cos(y) + sin(y)⋅cos(x)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expand\PYGZus{}trig}\PYG{p}{(}\PYG{n}{tan}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{ 2⋅tan(x)} | |
\PYG{g+go}{-------------} | |
\PYG{g+go}{ 2} | |
\PYG{g+go}{\PYGZhy{} tan (x) + 1} | |
\end{Verbatim} | |
Because \code{expand\_trig()} tends to make trigonometric expressions larger, and | |
\code{trigsimp()} tends to make them smaller, these identities can be applied in | |
reverse using \code{trigsimp()} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{trigsimp}\PYG{p}{(}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{n}{cos}\PYG{p}{(}\PYG{n}{y}\PYG{p}{)} \PYG{o}{+} \PYG{n}{sin}\PYG{p}{(}\PYG{n}{y}\PYG{p}{)}\PYG{o}{*}\PYG{n}{cos}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{sin(x + y)} | |
\end{Verbatim} | |
\subsection{Powers} | |
\label{tutorial/simplification:powers} | |
Before we introduce the power simplification functions, a mathematical | |
discussion on the identities held by powers is in order. There are three | |
kinds of identities satisfied by exponents | |
\begin{enumerate} | |
\item {} | |
\(x^ax^b = x^{a + b}\) | |
\item {} | |
\(x^ay^a = (xy)^a\) | |
\item {} | |
\((x^a)^b = x^{ab}\) | |
\end{enumerate} | |
Identity 1 is always true. | |
Identity 2 is not always true. For example, if \(x = y = -1\) and \(a = | |
\frac{1}{2}\), then \(x^ay^a = \sqrt{-1}\sqrt{-1} = i\cdot i = -1\), whereas | |
\((xy)^a = \sqrt{-1\cdot-1} = \sqrt{1} = 1\). However, identity 2 is true at | |
least if \(x\) and \(y\) are nonnegative and \(a\) is real (it may also be true | |
under other conditions as well). A common consequence of the failure of | |
identity 2 is that \(\sqrt{x}\sqrt{y} \neq \sqrt{xy}\). | |
Identity 3 is not always true. For example, if \(x = -1\), \(a = 2\), and \(b = | |
\frac{1}{2}\), then \((x^a)^b = {\left ((-1)^2\right )}^{1/2} = \sqrt{1} = 1\) | |
and \(x^{ab} = (-1)^{2\cdot1/2} = (-1)^1 = -1\). However, identity 3 is true | |
when \(b\) is an integer (again, it may also hold in other cases as well). Two | |
common consequences of the failure of identity 3 are that \(\sqrt{x^2}\neq x\) | |
and that \(\sqrt{\frac{1}{x}} \neq \frac{1}{\sqrt{x}}\). | |
To summarize | |
\begin{tabular}{|p{0.237\linewidth}|p{0.237\linewidth}|p{0.237\linewidth}|p{0.237\linewidth}|} | |
\hline | |
\textsf{\relax | |
Identity | |
} & \textsf{\relax | |
Sufficient conditions to hold | |
} & \textsf{\relax | |
Counterexample when conditions are not met | |
} & \textsf{\relax | |
Important consequences | |
}\\ | |
\hline\begin{enumerate} | |
\item {} | |
\(x^ax^b = x^{a + b}\) | |
\end{enumerate} | |
& | |
Always true | |
& | |
None | |
& | |
None | |
\\ | |
\hline\begin{enumerate} | |
\setcounter{enumi}{1} | |
\item {} | |
\(x^ay^a = (xy)^a\) | |
\end{enumerate} | |
& | |
\(x, y \geq 0\) and \(a \in \mathbb{R}\) | |
& | |
\((-1)^{1/2}(-1)^{1/2} \neq (-1\cdot-1)^{1/2}\) | |
& | |
\(\sqrt{x}\sqrt{y} \neq \sqrt{xy}\) in general | |
\\ | |
\hline\begin{enumerate} | |
\setcounter{enumi}{2} | |
\item {} | |
\((x^a)^b = x^{ab}\) | |
\end{enumerate} | |
& | |
\(b \in \mathbb{Z}\) | |
& | |
\({\left((-1)^2\right )}^{1/2} \neq (-1)^{2\cdot1/2}\) | |
& | |
\(\sqrt{x^2}\neq x\) and \(\sqrt{\frac{1}{x}}\neq\frac{1}{\sqrt{x}}\) in general | |
\\ | |
\hline\end{tabular} | |
This is important to remember, because by default, SymPy will not perform | |
simplifications if they are not true in general. | |
In order to make SymPy perform simplifications involving identities that are | |
only true under certain assumptions, we need to put assumptions on our | |
Symbols. We will undertake a full discussion of the assumptions system later, | |
but for now, all we need to know are the following. | |
\begin{itemize} | |
\item {} | |
By default, SymPy Symbols are assumed to be complex (elements of | |
\(\mathbb{C}\)). That is, a simplification will not be applied to an | |
expression with a given Symbol unless it holds for all complex numbers. | |
\item {} | |
Symbols can be given different assumptions by passing the assumption to | |
\code{symbols()}. For the rest of this section, we will be assuming that \code{x} | |
and \code{y} are positive, and that \code{a} and \code{b} are real. We will leave | |
\code{z}, \code{t}, and \code{c} as arbitrary complex Symbols to demonstrate what | |
happens in that case. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x}\PYG{p}{,} \PYG{n}{y} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x y}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{positive}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a}\PYG{p}{,} \PYG{n}{b} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{a b}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{real}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{z}\PYG{p}{,} \PYG{n}{t}\PYG{p}{,} \PYG{n}{c} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{z t c}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\end{Verbatim} | |
\end{itemize} | |
\begin{notice}{note}{Note:} | |
In SymPy, \code{sqrt(x)} is just a shortcut to \code{x**Rational(1, 2)}. They | |
are exactly the same object. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{sqrt}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} \PYG{o}{==} \PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{n}{Rational}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
\end{notice} | |
\subsubsection{powsimp} | |
\label{tutorial/simplification:powsimp} | |
\code{powsimp()} applies identities 1 and 2 from above, from left to right. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{powsimp}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{n}{a}\PYG{o}{*}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{n}{b}\PYG{p}{)} | |
\PYG{g+go}{ a + b} | |
\PYG{g+go}{ x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{powsimp}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{n}{a}\PYG{o}{*}\PYG{n}{y}\PYG{o}{*}\PYG{o}{*}\PYG{n}{a}\PYG{p}{)} | |
\PYG{g+go}{ a} | |
\PYG{g+go}{(x⋅y)} | |
\end{Verbatim} | |
Notice that \code{powsimp()} refuses to do the simplification if it is not valid. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{powsimp}\PYG{p}{(}\PYG{n}{t}\PYG{o}{*}\PYG{o}{*}\PYG{n}{c}\PYG{o}{*}\PYG{n}{z}\PYG{o}{*}\PYG{o}{*}\PYG{n}{c}\PYG{p}{)} | |
\PYG{g+go}{ c c} | |
\PYG{g+go}{t ⋅z} | |
\end{Verbatim} | |
If you know that you want to apply this simplification, but you don't want to | |
mess with assumptions, you can pass the \code{force=True} flag. This will force | |
the simplification to take place, regardless of assumptions. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{powsimp}\PYG{p}{(}\PYG{n}{t}\PYG{o}{*}\PYG{o}{*}\PYG{n}{c}\PYG{o}{*}\PYG{n}{z}\PYG{o}{*}\PYG{o}{*}\PYG{n}{c}\PYG{p}{,} \PYG{n}{force}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{ c} | |
\PYG{g+go}{(t⋅z)} | |
\end{Verbatim} | |
Note that in some instances, in particular, when the exponents are integers or | |
rational numbers, and identity 2 holds, it will be applied automatically | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{z}\PYG{o}{*}\PYG{n}{t}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} | |
\PYG{g+go}{ 2 2} | |
\PYG{g+go}{ t ⋅z} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{sqrt}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)} | |
\PYG{g+go}{ \PYGZus{}\PYGZus{}\PYGZus{} \PYGZus{}\PYGZus{}\PYGZus{}} | |
\PYG{g+go}{ \textbackslash{}╱ x ⋅\textbackslash{}╱ y} | |
\end{Verbatim} | |
This means that it will be impossible to undo this identity with | |
\code{powsimp()}, because even if \code{powsimp()} were to put the bases together, | |
they would be automatically split apart again. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{powsimp}\PYG{p}{(}\PYG{n}{z}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{t}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+go}{ 2 2} | |
\PYG{g+go}{ t ⋅z} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{powsimp}\PYG{p}{(}\PYG{n}{sqrt}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{n}{sqrt}\PYG{p}{(}\PYG{n}{y}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{ \PYGZus{}\PYGZus{}\PYGZus{} \PYGZus{}\PYGZus{}\PYGZus{}} | |
\PYG{g+go}{ \textbackslash{}╱ x ⋅\textbackslash{}╱ y} | |
\end{Verbatim} | |
\subsubsection{expand\_power\_exp / expand\_power\_base} | |
\label{tutorial/simplification:expand-power-exp-expand-power-base} | |
\code{expand\_power\_exp()} and \code{expand\_power\_base()} apply identities 1 and 2 | |
from right to left, respectively. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expand\PYGZus{}power\PYGZus{}exp}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{p}{(}\PYG{n}{a} \PYG{o}{+} \PYG{n}{b}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{ a b} | |
\PYG{g+go}{x ⋅x} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expand\PYGZus{}power\PYGZus{}base}\PYG{p}{(}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{n}{a}\PYG{p}{)} | |
\PYG{g+go}{ a a} | |
\PYG{g+go}{x ⋅y} | |
\end{Verbatim} | |
As with \code{powsimp()}, identity 2 is not applied if it is not valid. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expand\PYGZus{}power\PYGZus{}base}\PYG{p}{(}\PYG{p}{(}\PYG{n}{z}\PYG{o}{*}\PYG{n}{t}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{n}{c}\PYG{p}{)} | |
\PYG{g+go}{ c} | |
\PYG{g+go}{(t⋅z)} | |
\end{Verbatim} | |
And as with \code{powsimp()}, you can force the expansion to happen without | |
fiddling with assumptions by using \code{force=True}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expand\PYGZus{}power\PYGZus{}base}\PYG{p}{(}\PYG{p}{(}\PYG{n}{z}\PYG{o}{*}\PYG{n}{t}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{n}{c}\PYG{p}{,} \PYG{n}{force}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{ c c} | |
\PYG{g+go}{ t ⋅z} | |
\end{Verbatim} | |
As with identity 2, identity 1 is applied automatically if the power is a | |
number, and hence cannot be undone with \code{expand\_power\_exp()}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{3} | |
\PYG{g+go}{ 5} | |
\PYG{g+go}{ x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expand\PYGZus{}power\PYGZus{}exp}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{5}\PYG{p}{)} | |
\PYG{g+go}{ 5} | |
\PYG{g+go}{ x} | |
\end{Verbatim} | |
\subsubsection{powdenest} | |
\label{tutorial/simplification:powdenest} | |
\code{powdenest()} applies identity 3, from left to right. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{powdenest}\PYG{p}{(}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{n}{a}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{n}{b}\PYG{p}{)} | |
\PYG{g+go}{ a⋅b} | |
\PYG{g+go}{x} | |
\end{Verbatim} | |
As before, the identity is not applied if it is not true under the given | |
assumptions. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{powdenest}\PYG{p}{(}\PYG{p}{(}\PYG{n}{z}\PYG{o}{*}\PYG{o}{*}\PYG{n}{a}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{n}{b}\PYG{p}{)} | |
\PYG{g+go}{ b} | |
\PYG{g+go}{⎛ a⎞} | |
\PYG{g+go}{⎝z ⎠} | |
\end{Verbatim} | |
And as before, this can be manually overridden with \code{force=True}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{powdenest}\PYG{p}{(}\PYG{p}{(}\PYG{n}{z}\PYG{o}{*}\PYG{o}{*}\PYG{n}{a}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{n}{b}\PYG{p}{,} \PYG{n}{force}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{ a⋅b} | |
\PYG{g+go}{z} | |
\end{Verbatim} | |
\subsection{Exponentials and logarithms} | |
\label{tutorial/simplification:exponentials-and-logarithms} | |
\begin{notice}{note}{Note:} | |
In SymPy, as in Python and most programming languages, \code{log} is the | |
natural logarithm, also known as \code{ln}. SymPy automatically provides an | |
alias \code{ln = log} in case you forget this. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{ln}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{log(x)} | |
\end{Verbatim} | |
\end{notice} | |
Logarithms have similar issues as powers. There are two main identities | |
\begin{enumerate} | |
\item {} | |
\(\log{(xy)} = \log{(x)} + \log{(y)}\) | |
\item {} | |
\(\log{(x^n)} = n\log{(x)}\) | |
\end{enumerate} | |
Neither identity is true for arbitrary complex \(x\) and \(y\), due to the branch | |
cut in the complex plane for the complex logarithm. However, sufficient | |
conditions for the identities to hold are if \(x\) and \(y\) are positive and \(n\) | |
is real. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x}\PYG{p}{,} \PYG{n}{y} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x y}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{positive}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{n} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{n}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{real}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\end{Verbatim} | |
As before, \code{z} and \code{t} will be Symbols with no additional assumptions. | |
Note that the identity \(\log{\left (\frac{x}{y}\right )} = \log(x) - \log(y)\) | |
is a special case of identities 1 and 2 by \(\log{\left (\frac{x}{y}\right )} | |
=\) \(\log{\left (x\cdot\frac{1}{y}\right )} =\) \(\log(x) + \log{\left( | |
y^{-1}\right )} =\) \(\log(x) - \log(y)\), and thus it also holds if \(x\) and \(y\) | |
are positive, but may not hold in general. | |
We also see that \(\log{\left( e^x \right)} = x\) comes from \(\log{\left ( e^x | |
\right)} = x\log(e) = x\), and thus holds when \(x\) is real (and it can be | |
verified that it does not hold in general for arbitrary complex \(x\), for | |
example, \(\log{\left (e^{x + 2\pi i}\right)} = \log{\left (e^x\right )} = x | |
\neq x + 2\pi i\)). | |
\subsubsection{expand\_log} | |
\label{tutorial/simplification:expand-log} | |
To apply identities 1 and 2 from left to right, use \code{expand\_log()}. As | |
always, the identities will not be applied unless they are valid. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expand\PYGZus{}log}\PYG{p}{(}\PYG{n}{log}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{log(x) + log(y)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expand\PYGZus{}log}\PYG{p}{(}\PYG{n}{log}\PYG{p}{(}\PYG{n}{x}\PYG{o}{/}\PYG{n}{y}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{log(x) \PYGZhy{} log(y)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expand\PYGZus{}log}\PYG{p}{(}\PYG{n}{log}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{2⋅log(x)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expand\PYGZus{}log}\PYG{p}{(}\PYG{n}{log}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{n}{n}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{n⋅log(x)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expand\PYGZus{}log}\PYG{p}{(}\PYG{n}{log}\PYG{p}{(}\PYG{n}{z}\PYG{o}{*}\PYG{n}{t}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{log(t⋅z)} | |
\end{Verbatim} | |
As with \code{powsimp()} and \code{powdenest()}, \code{expand\_log()} has a \code{force} | |
option that can be used to ignore assumptions. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expand\PYGZus{}log}\PYG{p}{(}\PYG{n}{log}\PYG{p}{(}\PYG{n}{z}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{ ⎛ 2⎞} | |
\PYG{g+go}{log⎝z ⎠} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expand\PYGZus{}log}\PYG{p}{(}\PYG{n}{log}\PYG{p}{(}\PYG{n}{z}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{,} \PYG{n}{force}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{2⋅log(z)} | |
\end{Verbatim} | |
\subsubsection{logcombine} | |
\label{tutorial/simplification:logcombine} | |
To apply identities 1 and 2 from right to left, use \code{logcombine()}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{logcombine}\PYG{p}{(}\PYG{n}{log}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} \PYG{o}{+} \PYG{n}{log}\PYG{p}{(}\PYG{n}{y}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{log(x⋅y)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{logcombine}\PYG{p}{(}\PYG{n}{n}\PYG{o}{*}\PYG{n}{log}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{ ⎛ n⎞} | |
\PYG{g+go}{log⎝x ⎠} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{logcombine}\PYG{p}{(}\PYG{n}{n}\PYG{o}{*}\PYG{n}{log}\PYG{p}{(}\PYG{n}{z}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{n⋅log(z)} | |
\end{Verbatim} | |
\code{logcombine()} also has a \code{force} option that can be used to ignore | |
assumptions. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{logcombine}\PYG{p}{(}\PYG{n}{n}\PYG{o}{*}\PYG{n}{log}\PYG{p}{(}\PYG{n}{z}\PYG{p}{)}\PYG{p}{,} \PYG{n}{force}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{ ⎛ n⎞} | |
\PYG{g+go}{log⎝z ⎠} | |
\end{Verbatim} | |
\subsection{Special Functions} | |
\label{tutorial/simplification:special-functions} | |
SymPy implements dozens of special functions, ranging from functions in | |
combinatorics to mathematical physics. | |
An extensive list of the special functions included with SymPy and their | |
documentation is at the {\hyperref[modules/functions/index:functions-contents]{\emph{\DUspan{}{Functions Module}}}} (\autopageref*{modules/functions/index:functions-contents}) page. | |
For the purposes of this tutorial, let's introduce a few special functions in | |
SymPy. | |
Let's define \code{x}, \code{y}, and \code{z} as regular, complex Symbols, removing any | |
assumptions we put on them in the previous section. We will also define \code{k}, | |
\code{m}, and \code{n}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{z} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x y z}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{k}\PYG{p}{,} \PYG{n}{m}\PYG{p}{,} \PYG{n}{n} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{k m n}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\end{Verbatim} | |
The \href{http://en.wikipedia.org/wiki/Factorial}{factorial} function is | |
\code{factorial}. \code{factorial(n)} represents \(n!= 1\cdot2\cdots(n - 1)\cdot | |
n\). \(n!\) represents the number of permutations of \(n\) distinct items. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{factorial}\PYG{p}{(}\PYG{n}{n}\PYG{p}{)} | |
\PYG{g+go}{n!} | |
\end{Verbatim} | |
The \href{http://en.wikipedia.org/wiki/Binomial\_coefficient}{binomial coefficient} function is | |
\code{binomial}. \code{binomial(n, k)} represents \(\binom{n}{k}\), the number of | |
ways to choose \(k\) items from a set of \(n\) distinct items. It is also often | |
written as \(nCk\), and is pronounced ``\(n\) choose \(k\)''. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{binomial}\PYG{p}{(}\PYG{n}{n}\PYG{p}{,} \PYG{n}{k}\PYG{p}{)} | |
\PYG{g+go}{⎛n⎞} | |
\PYG{g+go}{⎜ ⎟} | |
\PYG{g+go}{⎝k⎠} | |
\end{Verbatim} | |
The factorial function is closely related to the \href{http://en.wikipedia.org/wiki/Gamma\_function}{gamma function}, \code{gamma}. \code{gamma(z)} | |
represents \(\Gamma(z) = \int_0^\infty t^{z - 1}e^{-t}\,dt\), which for positive integer | |
\(z\) is the same as \((z - 1)!\). | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{gamma}\PYG{p}{(}\PYG{n}{z}\PYG{p}{)} | |
\PYG{g+go}{\(\Gamma\)(z)} | |
\end{Verbatim} | |
The \href{http://en.wikipedia.org/wiki/Generalized\_hypergeometric\_function}{generalized hypergeometric function} is | |
\code{hyper}. \code{hyper({[}a\_1, ..., a\_p{]}, {[}b\_1, ..., b\_q{]}, z)} represents | |
\({}_pF_q\left(\begin{matrix} a_1, \dots, a_p \\ b_1, \dots, b_q \end{matrix} | |
\middle| z \right)\). The most common case is \({}_2F_1\), which is often | |
referred to as the \href{http://en.wikipedia.org/wiki/Hypergeometric\_function}{ordinary hypergeometric function}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{hyper}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mi}{3}\PYG{p}{]}\PYG{p}{,} \PYG{n}{z}\PYG{p}{)} | |
\PYG{g+go}{ ┌- ⎛1, 2 \textbar{} ⎞} | |
\PYG{g+go}{ ├- ⎜ \textbar{} z⎟} | |
\PYG{g+go}{2╵ 1 ⎝ 3 \textbar{} ⎠} | |
\end{Verbatim} | |
\subsubsection{rewrite} | |
\label{tutorial/simplification:rewrite} | |
A common way to deal with special functions is to rewrite them in terms of one | |
another. This works for any function in SymPy, not just special functions. | |
To rewrite an expression in terms of a function, use | |
\code{expr.rewrite(function)}. For example, | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{tan}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{rewrite}\PYG{p}{(}\PYG{n}{sin}\PYG{p}{)} | |
\PYG{g+go}{ 2} | |
\PYG{g+go}{2⋅sin (x)} | |
\PYG{g+go}{---------} | |
\PYG{g+go}{ sin(2⋅x)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{factorial}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{rewrite}\PYG{p}{(}\PYG{n}{gamma}\PYG{p}{)} | |
\PYG{g+go}{\(\Gamma\)(x + 1)} | |
\end{Verbatim} | |
For some tips on applying more targeted rewriting, see the | |
{\hyperref[tutorial/manipulation:tutorial-manipulation]{\emph{\DUspan{}{Advanced Expression Manipulation}}}} (\autopageref*{tutorial/manipulation:tutorial-manipulation}) section. | |
\subsubsection{expand\_func} | |
\label{tutorial/simplification:expand-func} | |
To expand special functions in terms of some identities, use | |
\code{expand\_func()}. For example | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expand\PYGZus{}func}\PYG{p}{(}\PYG{n}{gamma}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{3}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{x⋅(x + 1)⋅(x + 2)⋅\(\Gamma\)(x)} | |
\end{Verbatim} | |
\subsubsection{hyperexpand} | |
\label{tutorial/simplification:hyperexpand} | |
To rewrite \code{hyper} in terms of more standard functions, use | |
\code{hyperexpand()}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{hyperexpand}\PYG{p}{(}\PYG{n}{hyper}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mi}{2}\PYG{p}{]}\PYG{p}{,} \PYG{n}{z}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{\PYGZhy{}log(\PYGZhy{}z + 1)} | |
\PYG{g+go}{-------------} | |
\PYG{g+go}{ z} | |
\end{Verbatim} | |
\code{hyperexpand()} also works on the more general Meijer G-function (see | |
{\hyperref[modules/functions/special:sympy.functions.special.hyper.meijerg]{\emph{\code{its documentation}}}} (\autopageref*{modules/functions/special:sympy.functions.special.hyper.meijerg}) for more | |
information). | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} \PYG{o}{=} \PYG{n}{meijerg}\PYG{p}{(}\PYG{p}{[}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{,}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{,}\PYG{p}{[}\PYG{p}{]}\PYG{p}{]}\PYG{p}{,} \PYG{o}{\PYGZhy{}}\PYG{n}{z}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} | |
\PYG{g+go}{╭-╮1, 1 ⎛1 1 \textbar{} ⎞} | |
\PYG{g+go}{\textbar{}╶┐ ⎜ \textbar{} \PYGZhy{}z⎟} | |
\PYG{g+go}{╰-╯2, 1 ⎝1 \textbar{} ⎠} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{hyperexpand}\PYG{p}{(}\PYG{n}{expr}\PYG{p}{)} | |
\PYG{g+go}{ 1} | |
\PYG{g+go}{ -} | |
\PYG{g+go}{ z} | |
\PYG{g+go}{e} | |
\end{Verbatim} | |
\subsubsection{combsimp} | |
\label{tutorial/simplification:combsimp} | |
To simplify combinatorial expressions, use \code{combsimp()}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{combsimp}\PYG{p}{(}\PYG{n}{factorial}\PYG{p}{(}\PYG{n}{n}\PYG{p}{)}\PYG{o}{/}\PYG{n}{factorial}\PYG{p}{(}\PYG{n}{n} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{3}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{n⋅(n \PYGZhy{} 2)⋅(n \PYGZhy{} 1)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{combsimp}\PYG{p}{(}\PYG{n}{binomial}\PYG{p}{(}\PYG{n}{n}\PYG{o}{+}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{k}\PYG{o}{+}\PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{/}\PYG{n}{binomial}\PYG{p}{(}\PYG{n}{n}\PYG{p}{,} \PYG{n}{k}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{n + 1} | |
\PYG{g+go}{-----} | |
\PYG{g+go}{k + 1} | |
\end{Verbatim} | |
\code{combsimp()} also simplifies expressions with \code{gamma}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{combsimp}\PYG{p}{(}\PYG{n}{gamma}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{n}{gamma}\PYG{p}{(}\PYG{l+m+mi}{1} \PYG{o}{\PYGZhy{}} \PYG{n}{x}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{ \(\pi\)} | |
\PYG{g+go}{--------} | |
\PYG{g+go}{sin(\(\pi\)⋅x)} | |
\end{Verbatim} | |
\subsection{Example: Continued Fractions} | |
\label{tutorial/simplification:example-continued-fractions} | |
Let's use SymPy to explore continued fractions. A \href{http://en.wikipedia.org/wiki/Continued\_fraction}{continued fraction} is an expression of the | |
form | |
\begin{gather} | |
\begin{split}a_0 + \cfrac{1}{a_1 + \cfrac{1}{a_2 + \cfrac{1}{ \ddots + \cfrac{1}{a_n} | |
}}}\end{split}\notag | |
\end{gather} | |
where \(a_0, \ldots, a_n\) are integers, and \(a_1, \ldots, a_n\) are positive. A | |
continued fraction can also be infinite, but infinite objects are more | |
difficult to represent in computers, so we will only examine the finite case | |
here. | |
A continued fraction of the above form is often represented as a list \([a_0; | |
a_1, \ldots, a_n]\). Let's write a simple function that converts such a list | |
to its continued fraction form. The easiest way to construct a continued | |
fraction from a list is to work backwards. Note that despite the apparent | |
symmetry of the definition, the first element, \(a_0\), must usually be handled | |
differently from the rest. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{def} \PYG{n+nf}{list\PYGZus{}to\PYGZus{}frac}\PYG{p}{(}\PYG{n}{l}\PYG{p}{)}\PYG{p}{:} | |
\PYG{g+gp}{... } \PYG{n}{expr} \PYG{o}{=} \PYG{n}{Integer}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{)} | |
\PYG{g+gp}{... } \PYG{k}{for} \PYG{n}{i} \PYG{o+ow}{in} \PYG{n+nb}{reversed}\PYG{p}{(}\PYG{n}{l}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{:}\PYG{p}{]}\PYG{p}{)}\PYG{p}{:} | |
\PYG{g+gp}{... } \PYG{n}{expr} \PYG{o}{+}\PYG{o}{=} \PYG{n}{i} | |
\PYG{g+gp}{... } \PYG{n}{expr} \PYG{o}{=} \PYG{l+m+mi}{1}\PYG{o}{/}\PYG{n}{expr} | |
\PYG{g+gp}{... } \PYG{k}{return} \PYG{n}{l}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{]} \PYG{o}{+} \PYG{n}{expr} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{list\PYGZus{}to\PYGZus{}frac}\PYG{p}{(}\PYG{p}{[}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{z}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+go}{ 1} | |
\PYG{g+go}{x + -----} | |
\PYG{g+go}{ 1} | |
\PYG{g+go}{ y + -} | |
\PYG{g+go}{ z} | |
\end{Verbatim} | |
We use \code{Integer(0)} in \code{list\_to\_frac} so that the result will always be a | |
SymPy object, even if we only pass in Python ints. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{list\PYGZus{}to\PYGZus{}frac}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{4}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+go}{43} | |
\PYG{g+go}{--} | |
\PYG{g+go}{30} | |
\end{Verbatim} | |
Every finite continued fraction is a rational number, but we are interested in | |
symbolics here, so let's create a symbolic continued fraction. The | |
\code{symbols()} function that we have been using has a shortcut to create | |
numbered symbols. \code{symbols('a0:5')} will create the symbols \code{a0}, \code{a1}, | |
..., \code{a5}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{syms} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{a0:5}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{syms} | |
\PYG{g+go}{(a₀, a1, a2, a₃, a₄)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a0}\PYG{p}{,} \PYG{n}{a1}\PYG{p}{,} \PYG{n}{a2}\PYG{p}{,} \PYG{n}{a3}\PYG{p}{,} \PYG{n}{a4} \PYG{o}{=} \PYG{n}{syms} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{frac} \PYG{o}{=} \PYG{n}{list\PYGZus{}to\PYGZus{}frac}\PYG{p}{(}\PYG{n}{syms}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{frac} | |
\PYG{g+go}{ 1} | |
\PYG{g+go}{a₀ + -----------------} | |
\PYG{g+go}{ 1} | |
\PYG{g+go}{ a1 + ------------} | |
\PYG{g+go}{ 1} | |
\PYG{g+go}{ a2 + -------} | |
\PYG{g+go}{ 1} | |
\PYG{g+go}{ a₃ + --} | |
\PYG{g+go}{ a₄} | |
\end{Verbatim} | |
This form is useful for understanding continued fractions, but lets put it | |
into standard rational function form using \code{cancel()}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{frac} \PYG{o}{=} \PYG{n}{cancel}\PYG{p}{(}\PYG{n}{frac}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{frac} | |
\PYG{g+go}{a₀⋅a1⋅a2⋅a₃⋅a₄ + a₀⋅a1⋅a2 + a₀⋅a1⋅a₄ + a₀⋅a₃⋅a₄ + a₀ + a2⋅a₃⋅a₄ + a2 + a₄} | |
\PYG{g+go}{-------------------------------------------------------------------------} | |
\PYG{g+go}{ a1⋅a2⋅a₃⋅a₄ + a1⋅a2 + a1⋅a₄ + a₃⋅a₄ + 1} | |
\end{Verbatim} | |
Now suppose we were given \code{frac} in the above canceled form. In fact, we | |
might be given the fraction in any form, but we can always put it into the | |
above canonical form with \code{cancel()}. Suppose that we knew that it could be | |
rewritten as a continued fraction. How could we do this with SymPy? A | |
continued fraction is recursively \(c + \frac{1}{f}\), where \(c\) is an integer | |
and \(f\) is a (smaller) continued fraction. If we could write the expression | |
in this form, we could pull out each \(c\) recursively and add it to a list. We | |
could then get a continued fraction with our \code{list\_to\_frac()} function. | |
The key observation here is that we can convert an expression to the form \(c + | |
\frac{1}{f}\) by doing a partial fraction decomposition with respect to | |
\(c\). This is because \(f\) does not contain \(c\). This means we need to use the | |
\code{apart()} function. We use \code{apart()} to pull the term out, then subtract | |
it from the expression, and take the reciprocal to get the \(f\) part. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{l} \PYG{o}{=} \PYG{p}{[}\PYG{p}{]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{frac} \PYG{o}{=} \PYG{n}{apart}\PYG{p}{(}\PYG{n}{frac}\PYG{p}{,} \PYG{n}{a0}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{frac} | |
\PYG{g+go}{ a2⋅a₃⋅a₄ + a2 + a₄} | |
\PYG{g+go}{a₀ + ---------------------------------------} | |
\PYG{g+go}{ a1⋅a2⋅a₃⋅a₄ + a1⋅a2 + a1⋅a₄ + a₃⋅a₄ + 1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{l}\PYG{o}{.}\PYG{n}{append}\PYG{p}{(}\PYG{n}{a0}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{frac} \PYG{o}{=} \PYG{l+m+mi}{1}\PYG{o}{/}\PYG{p}{(}\PYG{n}{frac} \PYG{o}{\PYGZhy{}} \PYG{n}{a0}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{frac} | |
\PYG{g+go}{a1⋅a2⋅a₃⋅a₄ + a1⋅a2 + a1⋅a₄ + a₃⋅a₄ + 1} | |
\PYG{g+go}{---------------------------------------} | |
\PYG{g+go}{ a2⋅a₃⋅a₄ + a2 + a₄} | |
\end{Verbatim} | |
Now we repeat this process | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{frac} \PYG{o}{=} \PYG{n}{apart}\PYG{p}{(}\PYG{n}{frac}\PYG{p}{,} \PYG{n}{a1}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{frac} | |
\PYG{g+go}{ a₃⋅a₄ + 1} | |
\PYG{g+go}{a1 + ------------------} | |
\PYG{g+go}{ a2⋅a₃⋅a₄ + a2 + a₄} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{l}\PYG{o}{.}\PYG{n}{append}\PYG{p}{(}\PYG{n}{a1}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{frac} \PYG{o}{=} \PYG{l+m+mi}{1}\PYG{o}{/}\PYG{p}{(}\PYG{n}{frac} \PYG{o}{\PYGZhy{}} \PYG{n}{a1}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{frac} \PYG{o}{=} \PYG{n}{apart}\PYG{p}{(}\PYG{n}{frac}\PYG{p}{,} \PYG{n}{a2}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{frac} | |
\PYG{g+go}{ a₄} | |
\PYG{g+go}{a2 + ---------} | |
\PYG{g+go}{ a₃⋅a₄ + 1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{l}\PYG{o}{.}\PYG{n}{append}\PYG{p}{(}\PYG{n}{a2}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{frac} \PYG{o}{=} \PYG{l+m+mi}{1}\PYG{o}{/}\PYG{p}{(}\PYG{n}{frac} \PYG{o}{\PYGZhy{}} \PYG{n}{a2}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{frac} \PYG{o}{=} \PYG{n}{apart}\PYG{p}{(}\PYG{n}{frac}\PYG{p}{,} \PYG{n}{a3}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{frac} | |
\PYG{g+go}{ 1} | |
\PYG{g+go}{a₃ + --} | |
\PYG{g+go}{ a₄} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{l}\PYG{o}{.}\PYG{n}{append}\PYG{p}{(}\PYG{n}{a3}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{frac} \PYG{o}{=} \PYG{l+m+mi}{1}\PYG{o}{/}\PYG{p}{(}\PYG{n}{frac} \PYG{o}{\PYGZhy{}} \PYG{n}{a3}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{frac} \PYG{o}{=} \PYG{n}{apart}\PYG{p}{(}\PYG{n}{frac}\PYG{p}{,} \PYG{n}{a4}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{frac} | |
\PYG{g+go}{a₄} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{l}\PYG{o}{.}\PYG{n}{append}\PYG{p}{(}\PYG{n}{a4}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{list\PYGZus{}to\PYGZus{}frac}\PYG{p}{(}\PYG{n}{l}\PYG{p}{)} | |
\PYG{g+go}{ 1} | |
\PYG{g+go}{a₀ + -----------------} | |
\PYG{g+go}{ 1} | |
\PYG{g+go}{ a1 + ------------} | |
\PYG{g+go}{ 1} | |
\PYG{g+go}{ a2 + -------} | |
\PYG{g+go}{ 1} | |
\PYG{g+go}{ a₃ + --} | |
\PYG{g+go}{ a₄} | |
\end{Verbatim} | |
\setbox0\vbox{ | |
\begin{minipage}{0.95\linewidth} | |
\textbf{Quick Tip} | |
\medskip | |
You can execute multiple lines at once in SymPy Live. Typing | |
\code{Shift-Enter} instead of \code{Enter} will enter a newline instead of | |
executing. | |
\end{minipage}} | |
\begin{center}\setlength{\fboxsep}{5pt}\shadowbox{\box0}\end{center} | |
Of course, this exercise seems pointless, because we already know that our | |
\code{frac} is \code{list\_to\_frac({[}a0, a1, a2, a3, a4{]})}. So try the following | |
exercise. Take a list of symbols and randomize them, and create the canceled | |
continued fraction, and see if you can reproduce the original list. For | |
example | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{import} \PYG{n+nn}{random} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{l} \PYG{o}{=} \PYG{n+nb}{list}\PYG{p}{(}\PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{a0:5}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{random}\PYG{o}{.}\PYG{n}{shuffle}\PYG{p}{(}\PYG{n}{l}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{orig\PYGZus{}frac} \PYG{o}{=} \PYG{n}{frac} \PYG{o}{=} \PYG{n}{cancel}\PYG{p}{(}\PYG{n}{list\PYGZus{}to\PYGZus{}frac}\PYG{p}{(}\PYG{n}{l}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{del} \PYG{n}{l} | |
\end{Verbatim} | |
Click on ``Run code block in SymPy Live'' on the definition of \code{list\_to\_frac)} | |
above, and then on the above example, and try to reproduce \code{l} from | |
\code{frac}. I have deleted \code{l} at the end to remove the temptation for | |
peeking (you can check your answer at the end by calling | |
\code{cancel(list\_to\_frac(l))} on the list that you generate at the end, and | |
comparing it to \code{orig\_frac}. | |
See if you can think of a way to figure out what symbol to pass to \code{apart()} | |
at each stage (hint: think of what happens to \(a_0\) in the formula \(a_0 + | |
\frac{1}{a_1 + \cdots}\) when it is canceled). | |
\section{Calculus} | |
\label{tutorial/calculus:calculus}\label{tutorial/calculus::doc} | |
This section covers how to do basic calculus tasks such as derivatives, | |
integrals, limits, and series expansions in SymPy. If you are not familiar | |
with the math of any part of this section, you may safely skip it. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{o}{*} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{z} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x y z}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{init\PYGZus{}printing}\PYG{p}{(}\PYG{n}{use\PYGZus{}unicode}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\end{Verbatim} | |
\subsection{Derivatives} | |
\label{tutorial/calculus:derivatives}\label{tutorial/calculus:tutorial-derivatives} | |
To take derivatives, use the \code{diff} function. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{diff}\PYG{p}{(}\PYG{n}{cos}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{\PYGZhy{}sin(x)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{diff}\PYG{p}{(}\PYG{n}{exp}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{ ⎛ 2⎞} | |
\PYG{g+go}{ ⎝x ⎠} | |
\PYG{g+go}{2⋅x⋅e} | |
\end{Verbatim} | |
\code{diff} can take multiple derivatives at once. To take multiple derivatives, | |
pass the variable as many times as you wish to differentiate, or pass a number | |
after the variable. For example, both of the following find the third | |
derivative of \(x^4\). | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{diff}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{4}\PYG{p}{,} \PYG{n}{x}\PYG{p}{,} \PYG{n}{x}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{24⋅x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{diff}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{4}\PYG{p}{,} \PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{)} | |
\PYG{g+go}{24⋅x} | |
\end{Verbatim} | |
You can also take derivatives with respect to many variables at once. Just | |
pass each derivative in order, using the same syntax as for single variable | |
derivatives. For example, each of the following will compute | |
\(\frac{\partial^7}{\partial x\partial y^2\partial z^4} e^{x y z}\). | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} \PYG{o}{=} \PYG{n}{exp}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{o}{*}\PYG{n}{z}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{diff}\PYG{p}{(}\PYG{n}{expr}\PYG{p}{,} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{z}\PYG{p}{,} \PYG{n}{z}\PYG{p}{,} \PYG{n}{z}\PYG{p}{,} \PYG{n}{z}\PYG{p}{)} | |
\PYG{g+go}{ 3 2 ⎛ 3 3 3 2 2 2 ⎞ x⋅y⋅z} | |
\PYG{g+go}{x ⋅y ⋅⎝x ⋅y ⋅z + 14⋅x ⋅y ⋅z + 52⋅x⋅y⋅z + 48⎠⋅e} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{diff}\PYG{p}{(}\PYG{n}{expr}\PYG{p}{,} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{n}{z}\PYG{p}{,} \PYG{l+m+mi}{4}\PYG{p}{)} | |
\PYG{g+go}{ 3 2 ⎛ 3 3 3 2 2 2 ⎞ x⋅y⋅z} | |
\PYG{g+go}{x ⋅y ⋅⎝x ⋅y ⋅z + 14⋅x ⋅y ⋅z + 52⋅x⋅y⋅z + 48⎠⋅e} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{diff}\PYG{p}{(}\PYG{n}{expr}\PYG{p}{,} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{z}\PYG{p}{,} \PYG{l+m+mi}{4}\PYG{p}{)} | |
\PYG{g+go}{ 3 2 ⎛ 3 3 3 2 2 2 ⎞ x⋅y⋅z} | |
\PYG{g+go}{x ⋅y ⋅⎝x ⋅y ⋅z + 14⋅x ⋅y ⋅z + 52⋅x⋅y⋅z + 48⎠⋅e} | |
\end{Verbatim} | |
\code{diff} can also be called as a method. The two ways of calling \code{diff} are | |
exactly the same, and are provided only for convenience. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr}\PYG{o}{.}\PYG{n}{diff}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{z}\PYG{p}{,} \PYG{l+m+mi}{4}\PYG{p}{)} | |
\PYG{g+go}{ 3 2 ⎛ 3 3 3 2 2 2 ⎞ x⋅y⋅z} | |
\PYG{g+go}{x ⋅y ⋅⎝x ⋅y ⋅z + 14⋅x ⋅y ⋅z + 52⋅x⋅y⋅z + 48⎠⋅e} | |
\end{Verbatim} | |
To create an unevaluated derivative, use the \code{Derivative} class. It has the | |
same syntax as \code{diff}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{deriv} \PYG{o}{=} \PYG{n}{Derivative}\PYG{p}{(}\PYG{n}{expr}\PYG{p}{,} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{z}\PYG{p}{,} \PYG{l+m+mi}{4}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{deriv} | |
\PYG{g+go}{ 7} | |
\PYG{g+go}{ ∂ ⎛ x⋅y⋅z⎞} | |
\PYG{g+go}{----------⎝e ⎠} | |
\PYG{g+go}{ 4 2} | |
\PYG{g+go}{∂z ∂y ∂x} | |
\end{Verbatim} | |
To evaluate an unevaluated derivative, use the \code{doit} method. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{deriv}\PYG{o}{.}\PYG{n}{doit}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{ 3 2 ⎛ 3 3 3 2 2 2 ⎞ x⋅y⋅z} | |
\PYG{g+go}{x ⋅y ⋅⎝x ⋅y ⋅z + 14⋅x ⋅y ⋅z + 52⋅x⋅y⋅z + 48⎠⋅e} | |
\end{Verbatim} | |
These unevaluated objects are useful for delaying the evaluation of the | |
derivative, or for printing purposes. They are also used when SymPy does not | |
know how to compute the derivative of an expression (for example, if it | |
contains an undefined function, which are described in the {\hyperref[tutorial/solvers:tutorial-dsolve]{\emph{\DUspan{}{Solving | |
Differential Equations}}}} (\autopageref*{tutorial/solvers:tutorial-dsolve}) section). | |
\subsection{Integrals} | |
\label{tutorial/calculus:integrals} | |
To compute an integral, use the \code{integrate} function. There are two kinds | |
of integrals, definite and indefinite. To compute an indefinite integral, | |
that is, an antiderivative, or primitive, just pass the variable after the | |
expression. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{integrate}\PYG{p}{(}\PYG{n}{cos}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{sin(x)} | |
\end{Verbatim} | |
Note that SymPy does not include the constant of integration. If you want it, | |
you can add one yourself, or rephrase your problem as a differential equation | |
and use \code{dsolve} to solve it, which does add the constant (see {\hyperref[tutorial/solvers:tutorial-dsolve]{\emph{\DUspan{}{Solving Differential Equations}}}} (\autopageref*{tutorial/solvers:tutorial-dsolve})). | |
\setbox0\vbox{ | |
\begin{minipage}{0.95\linewidth} | |
\textbf{Quick Tip} | |
\medskip | |
\(\infty\) in SymPy is \code{oo} (that's the lowercase letter ``oh'' twice). This | |
is because \code{oo} looks like \(\infty\), and is easy to type. | |
\end{minipage}} | |
\begin{center}\setlength{\fboxsep}{5pt}\shadowbox{\box0}\end{center} | |
To compute a definite integral, pass the argument \code{(integration\_variable, | |
lower\_limit, upper\_limit)}. For example, to compute | |
\begin{gather} | |
\begin{split}\int_0^\infty e^{-x}\,dx,\end{split}\notag | |
\end{gather} | |
we would do | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{integrate}\PYG{p}{(}\PYG{n}{exp}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{n}{x}\PYG{p}{)}\PYG{p}{,} \PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{,} \PYG{n}{oo}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{1} | |
\end{Verbatim} | |
As with indefinite integrals, you can pass multiple limit tuples to perform a | |
multiple integral. For example, to compute | |
\begin{gather} | |
\begin{split}\int_{-\infty}^{\infty}\int_{-\infty}^{\infty} e^{- x^{2} - y^{2}}\, dx\, dy,\end{split}\notag | |
\end{gather} | |
do | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{integrate}\PYG{p}{(}\PYG{n}{exp}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{\PYGZhy{}} \PYG{n}{y}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{,} \PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{o}{\PYGZhy{}}\PYG{n}{oo}\PYG{p}{,} \PYG{n}{oo}\PYG{p}{)}\PYG{p}{,} \PYG{p}{(}\PYG{n}{y}\PYG{p}{,} \PYG{o}{\PYGZhy{}}\PYG{n}{oo}\PYG{p}{,} \PYG{n}{oo}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{\(\pi\)} | |
\end{Verbatim} | |
If \code{integrate} is unable to compute an integral, it returns an unevaluated | |
\code{Integral} object. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} \PYG{o}{=} \PYG{n}{integrate}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{n}{x}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{n}{expr}\PYG{p}{)} | |
\PYG{g+go}{Integral(x**x, x)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} | |
\PYG{g+go}{⌠} | |
\PYG{g+go}{⎮ x} | |
\PYG{g+go}{⎮ x dx} | |
\PYG{g+go}{⌡} | |
\end{Verbatim} | |
As with \code{Derivative}, you can create an unevaluated integral using | |
\code{Integral}. To later evaluate this integral, call \code{doit}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} \PYG{o}{=} \PYG{n}{Integral}\PYG{p}{(}\PYG{n}{log}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} | |
\PYG{g+go}{⌠} | |
\PYG{g+go}{⎮ 2} | |
\PYG{g+go}{⎮ log (x) dx} | |
\PYG{g+go}{⌡} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr}\PYG{o}{.}\PYG{n}{doit}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{ 2} | |
\PYG{g+go}{x⋅log (x) \PYGZhy{} 2⋅x⋅log(x) + 2⋅x} | |
\end{Verbatim} | |
\code{integrate} uses powerful algorithms that are always improving to compute | |
both definite and indefinite integrals, including heuristic pattern matching | |
type algorithms, a partial implementation of the \href{http://en.wikipedia.org/wiki/Risch\_algorithm}{Risch algorithm}, and an algorithm using | |
\href{http://en.wikipedia.org/wiki/Meijer\_g-function}{Meijer G-functions} that is | |
useful for computing integrals in terms of special functions, especially | |
definite integrals. Here is a sampling of some of the power of \code{integrate}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{integ} \PYG{o}{=} \PYG{n}{Integral}\PYG{p}{(}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{4} \PYG{o}{+} \PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{exp}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} \PYG{o}{\PYGZhy{}} \PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{o}{*}\PYG{n}{exp}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x} \PYG{o}{\PYGZhy{}} | |
\PYG{g+gp}{... } \PYG{n}{exp}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{)}\PYG{o}{*}\PYG{n}{exp}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{/}\PYG{p}{(}\PYG{p}{(}\PYG{n}{x} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{p}{(}\PYG{n}{exp}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{p}{)}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{integ} | |
\PYG{g+go}{⌠} | |
\PYG{g+go}{⎮ ⎛ 4 2 x 2 x x⎞ x} | |
\PYG{g+go}{⎮ ⎝x + x ⋅e \PYGZhy{} x \PYGZhy{} 2⋅x⋅e \PYGZhy{} 2⋅x \PYGZhy{} e ⎠⋅e} | |
\PYG{g+go}{⎮ ---------------------------------------- dx} | |
\PYG{g+go}{⎮ 2 2 ⎛ x ⎞} | |
\PYG{g+go}{⎮ (x \PYGZhy{} 1) ⋅(x + 1) ⋅⎝e + 1⎠} | |
\PYG{g+go}{⌡} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{integ}\PYG{o}{.}\PYG{n}{doit}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{ x} | |
\PYG{g+go}{ ⎛ x ⎞ e} | |
\PYG{g+go}{log⎝e + 1⎠ + ------} | |
\PYG{g+go}{ 2} | |
\PYG{g+go}{ x \PYGZhy{} 1} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{integ} \PYG{o}{=} \PYG{n}{Integral}\PYG{p}{(}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{integ} | |
\PYG{g+go}{⌠} | |
\PYG{g+go}{⎮ ⎛ 2⎞} | |
\PYG{g+go}{⎮ sin⎝x ⎠ dx} | |
\PYG{g+go}{⌡} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{integ}\PYG{o}{.}\PYG{n}{doit}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{ ⎛ \PYGZus{}\PYGZus{}\PYGZus{} ⎞} | |
\PYG{g+go}{ \PYGZus{}\PYGZus{}\PYGZus{} \PYGZus{}\PYGZus{}\PYGZus{} ⎜\textbackslash{}╱ 2 ⋅x⎟} | |
\PYG{g+go}{3⋅\textbackslash{}╱ 2 ⋅\textbackslash{}╱ \(\pi\) ⋅fresnels⎜-------⎟⋅\(\Gamma\)(3/4)} | |
\PYG{g+go}{ ⎜ \PYGZus{}\PYGZus{}\PYGZus{} ⎟} | |
\PYG{g+go}{ ⎝ \textbackslash{}╱ \(\pi\) ⎠} | |
\PYG{g+go}{--------------------------------------} | |
\PYG{g+go}{ 8⋅\(\Gamma\)(7/4)} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{integ} \PYG{o}{=} \PYG{n}{Integral}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{n}{y}\PYG{o}{*}\PYG{n}{exp}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{n}{x}\PYG{p}{)}\PYG{p}{,} \PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{,} \PYG{n}{oo}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{integ} | |
\PYG{g+go}{\(\infty\)} | |
\PYG{g+go}{⌠} | |
\PYG{g+go}{⎮ y \PYGZhy{}x} | |
\PYG{g+go}{⎮ x ⋅e dx} | |
\PYG{g+go}{⌡} | |
\PYG{g+go}{0} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{integ}\PYG{o}{.}\PYG{n}{doit}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{⎧ \(\Gamma\)(y + 1) for \PYGZhy{}re(y) \PYGZlt{} 1} | |
\PYG{g+go}{⎪} | |
\PYG{g+go}{⎪\(\infty\)} | |
\PYG{g+go}{⎪⌠} | |
\PYG{g+go}{⎨⎮ y \PYGZhy{}x} | |
\PYG{g+go}{⎪⎮ x ⋅e dx otherwise} | |
\PYG{g+go}{⎪⌡} | |
\PYG{g+go}{⎪0} | |
\PYG{g+go}{⎩} | |
\end{Verbatim} | |
This last example returned a \code{Piecewise} expression because the integral | |
does not converge unless \(\Re(y) > 1.\) | |
\subsection{Limits} | |
\label{tutorial/calculus:limits} | |
SymPy can compute symbolic limits with the \code{limit} function. The syntax to compute | |
\begin{gather} | |
\begin{split}\lim_{x\to x_0} f(x)\end{split}\notag | |
\end{gather} | |
is \code{limit(f(x), x, x0)}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{limit}\PYG{p}{(}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{/}\PYG{n}{x}\PYG{p}{,} \PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{)} | |
\PYG{g+go}{1} | |
\end{Verbatim} | |
\code{limit} should be used instead of \code{subs} whenever the point of evaluation | |
is a singularity. Even though SymPy has objects to represent \(\infty\), using | |
them for evaluation is not reliable because they do not keep track of things | |
like rate of growth. Also, things like \(\infty - \infty\) and | |
\(\frac{\infty}{\infty}\) return \(\mathrm{nan}\) (not-a-number). For example | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} \PYG{o}{=} \PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{o}{/}\PYG{n}{exp}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr}\PYG{o}{.}\PYG{n}{subs}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{oo}\PYG{p}{)} | |
\PYG{g+go}{nan} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{limit}\PYG{p}{(}\PYG{n}{expr}\PYG{p}{,} \PYG{n}{x}\PYG{p}{,} \PYG{n}{oo}\PYG{p}{)} | |
\PYG{g+go}{0} | |
\end{Verbatim} | |
Like \code{Derivative} and \code{Integral}, \code{limit} has an unevaluated | |
counterpart, \code{Limit}. To evaluate it, use \code{doit}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} \PYG{o}{=} \PYG{n}{Limit}\PYG{p}{(}\PYG{p}{(}\PYG{n}{cos}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{/}\PYG{n}{x}\PYG{p}{,} \PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} | |
\PYG{g+go}{ cos(x) \PYGZhy{} 1} | |
\PYG{g+go}{ lim ----------} | |
\PYG{g+go}{x-\(\rightarrow\)0⁺ x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr}\PYG{o}{.}\PYG{n}{doit}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{0} | |
\end{Verbatim} | |
To evaluate a limit at one side only, pass \code{'+'} or \code{'-'} as a third | |
argument to \code{limit}. For example, to compute | |
\begin{gather} | |
\begin{split}\lim_{x\to 0^+}\frac{1}{x},\end{split}\notag | |
\end{gather} | |
do | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{limit}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{o}{/}\PYG{n}{x}\PYG{p}{,} \PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{+}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+go}{\(\infty\)} | |
\end{Verbatim} | |
As opposed to | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{limit}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{o}{/}\PYG{n}{x}\PYG{p}{,} \PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{\PYGZhy{}}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+go}{\PYGZhy{}\(\infty\)} | |
\end{Verbatim} | |
\subsection{Series Expansion} | |
\label{tutorial/calculus:series-expansion} | |
SymPy can compute asymptotic series expansions of functions around a point. To | |
compute the expansion of \(f(x)\) around the point \(x = x_0\) terms of order | |
\(x^n\), use \code{f(x).series(x, x0, n)}. \code{x0} and \code{n} can be omitted, in | |
which case the defaults \code{x0=0} and \code{n=6} will be used. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} \PYG{o}{=} \PYG{n}{exp}\PYG{p}{(}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr}\PYG{o}{.}\PYG{n}{series}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{4}\PYG{p}{)} | |
\PYG{g+go}{ 2} | |
\PYG{g+go}{ x ⎛ 4⎞} | |
\PYG{g+go}{1 + x + -- + O⎝x ⎠} | |
\PYG{g+go}{ 2} | |
\end{Verbatim} | |
The \(O\left (x^4\right )\) term at the end represents the Landau order term at | |
\(x=0\) (not to be confused with big O notation used in computer science, which | |
generally represents the Landau order term at \(x=\infty\)). It means that all | |
x terms with power greater than or equal to \(x^4\) are omitted. Order terms | |
can be created and manipulated outside of \code{series}. They automatically | |
absorb higher order terms. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x} \PYG{o}{+} \PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{3} \PYG{o}{+} \PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{6} \PYG{o}{+} \PYG{n}{O}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{4}\PYG{p}{)} | |
\PYG{g+go}{ 3 ⎛ 4⎞} | |
\PYG{g+go}{x + x + O⎝x ⎠} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x}\PYG{o}{*}\PYG{n}{O}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+go}{O(x)} | |
\end{Verbatim} | |
If you do not want the order term, use the \code{removeO} method. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr}\PYG{o}{.}\PYG{n}{series}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{4}\PYG{p}{)}\PYG{o}{.}\PYG{n}{removeO}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{ 2} | |
\PYG{g+go}{x} | |
\PYG{g+go}{-- + x + 1} | |
\PYG{g+go}{2} | |
\end{Verbatim} | |
The \code{O} notation supports arbitrary limit points (other than 0): | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{exp}\PYG{p}{(}\PYG{n}{x} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{6}\PYG{p}{)}\PYG{o}{.}\PYG{n}{series}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{x0}\PYG{o}{=}\PYG{l+m+mi}{6}\PYG{p}{)} | |
\PYG{g+go}{ 2 3 4 5} | |
\PYG{g+go}{ (x \PYGZhy{} 6) (x \PYGZhy{} 6) (x \PYGZhy{} 6) (x \PYGZhy{} 6) ⎛ 6 ⎞} | |
\PYG{g+go}{\PYGZhy{}5 + -------- + -------- + -------- + -------- + x + O⎝(x \PYGZhy{} 6) ; x \(\rightarrow\) 6⎠} | |
\PYG{g+go}{ 2 6 24 120} | |
\end{Verbatim} | |
\subsection{Finite differences} | |
\label{tutorial/calculus:finite-differences} | |
So far we have looked at expressions with analytical derivatives | |
and primitive functions respectively. But what if we want to have an | |
expression to estimate a derivative of a curve for which we lack a | |
closed form representation, or for which we don't know the functional | |
values for yet. One approach would be to use a finite difference | |
approach. | |
You can use the \code{as\_finite\_diff} method of on any \code{Derivative} | |
instance to generate approximations to derivatives of arbitrary order: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{f} \PYG{o}{=} \PYG{n}{Function}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{f}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{dfdx} \PYG{o}{=} \PYG{n}{f}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{diff}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{as\PYGZus{}finite\PYGZus{}diff}\PYG{p}{(}\PYG{n}{dfdx}\PYG{p}{)} | |
\PYG{g+go}{\PYGZhy{}f(x \PYGZhy{} 1/2) + f(x + 1/2)} | |
\end{Verbatim} | |
here the first order derivative was approximated around x using a | |
minimum number of points (2 for 1st order derivative) evaluated | |
equidistantly using a step-size of 1. We can use arbitrary steps | |
(possibly containing symbolic expressions): | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{f} \PYG{o}{=} \PYG{n}{Function}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{f}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{d2fdx2} \PYG{o}{=} \PYG{n}{f}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{diff}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{h} \PYG{o}{=} \PYG{n}{Symbol}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{h}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{as\PYGZus{}finite\PYGZus{}diff}\PYG{p}{(}\PYG{n}{d2fdx2}\PYG{p}{,} \PYG{p}{[}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{h}\PYG{p}{,}\PYG{o}{\PYGZhy{}}\PYG{n}{h}\PYG{p}{,}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{h}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+go}{f(\PYGZhy{}3⋅h) f(\PYGZhy{}h) 2⋅f(2⋅h)} | |
\PYG{g+go}{------- \PYGZhy{} ----- + --------} | |
\PYG{g+go}{ 2 2 2} | |
\PYG{g+go}{ 5⋅h 3⋅h 15⋅h} | |
\end{Verbatim} | |
If you are just interested in evaluating the weights, you can do so | |
manually: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{finite\PYGZus{}diff\PYGZus{}weights}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{p}{[}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{3}\PYG{p}{,} \PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{]}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{)}\PYG{p}{[}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{[}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{1}\PYG{p}{]} | |
\PYG{g+go}{[1/5, \PYGZhy{}1/3, 2/15]} | |
\end{Verbatim} | |
note that we only need the last element in the last sublist | |
returned from finite\_diff\_weights. The reason for this is that | |
finite\_diff\_weights also generates weights for lower derivatives and | |
using fewer points (see the documentation of \code{finite\_diff\_weights} | |
for more details). | |
if using \code{finite\_diff\_weights} directly looks complicated and the | |
\code{as\_finite\_diff} function operating on \code{Derivative} instances | |
is not flexible enough, you can use \code{apply\_finite\_diff} which | |
takes order, x\_list, y\_list and x0 as parameters: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x\PYGZus{}list} \PYG{o}{=} \PYG{p}{[}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{y\PYGZus{}list} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{a b c}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{apply\PYGZus{}finite\PYGZus{}diff}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{x\PYGZus{}list}\PYG{p}{,} \PYG{n}{y\PYGZus{}list}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{)} | |
\PYG{g+go}{ 3⋅a b 2⋅c} | |
\PYG{g+go}{\PYGZhy{} --- \PYGZhy{} - + ---} | |
\PYG{g+go}{ 20 4 5} | |
\end{Verbatim} | |
\section{Solvers} | |
\label{tutorial/solvers::doc}\label{tutorial/solvers:solvers} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{o}{*} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{z} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x y z}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{init\PYGZus{}printing}\PYG{p}{(}\PYG{n}{use\PYGZus{}unicode}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\end{Verbatim} | |
\subsection{A Note about Equations} | |
\label{tutorial/solvers:a-note-about-equations} | |
Recall from the {\hyperref[tutorial/gotchas:tutorial-gotchas-equals]{\emph{\DUspan{}{gotchas}}}} (\autopageref*{tutorial/gotchas:tutorial-gotchas-equals}) section of this | |
tutorial that symbolic equations in SymPy are not represented by \code{=} or | |
\code{==}, but by \code{Eq}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Eq}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{)} | |
\PYG{g+go}{x = y} | |
\end{Verbatim} | |
However, there is an even easier way. In SymPy, any expression is not in an | |
\code{Eq} is automatically assumed to equal 0 by the solving functions. Since \(a | |
= b\) if and only if \(a - b = 0\), this means that instead of using \code{x == y}, | |
you can just use \code{x - y}. For example | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{solve}\PYG{p}{(}\PYG{n}{Eq}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{[\PYGZhy{}1, 1]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{solve}\PYG{p}{(}\PYG{n}{Eq}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{)}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{[\PYGZhy{}1, 1]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{solve}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{[\PYGZhy{}1, 1]} | |
\end{Verbatim} | |
This is particularly useful if the equation you wish to solve is already equal | |
to 0. Instead of typing \code{solve(Eq(expr, 0), x)}, you can just use | |
\code{solve(expr, x)}. | |
\subsection{Solving Equations Algebraically} | |
\label{tutorial/solvers:solving-equations-algebraically} | |
The main function for solving algebraic equations, as we saw above, is | |
\code{solve}. The syntax is \code{solve(equations, variables)}, where, as we saw | |
above, \code{equations} may be in the form of \code{Eq} instances or expressions | |
that are assumed to be equal to zero. | |
When solving a single equation, the output of \code{solve} is a list of the | |
solutions. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{solve}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{\PYGZhy{}} \PYG{n}{x}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{[0, 1]} | |
\end{Verbatim} | |
If no solutions are found, an empty list is returned, or | |
\code{NotImplementedError} is raised. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{solve}\PYG{p}{(}\PYG{n}{exp}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{[]} | |
\end{Verbatim} | |
\begin{notice}{note}{Note:} | |
If \code{solve} returns \code{{[}{]}} or raises \code{NotImplementedError}, it doesn't | |
mean that the equation has no solutions. It just means that it couldn't | |
find any. Often this means that the solutions cannot be represented | |
symbolically. For example, the equation \(x = \cos(x)\) has a solution, but | |
it cannot be represented symbolically using standard functions. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{solve}\PYG{p}{(}\PYG{n}{x} \PYG{o}{\PYGZhy{}} \PYG{n}{cos}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)} | |
\PYG{g+gt}{Traceback (most recent call last):} | |
\PYG{c}{...} | |
\PYG{g+gr}{NotImplementedError}: \PYG{n}{multiple generators [x, exp(I*x)]} | |
\PYG{g+go}{No algorithms are implemented to solve equation exp(I*x)} | |
\PYG{g+gt}{Traceback (most recent call last):} | |
\PYG{c}{...} | |
\PYG{g+gr}{NotImplementedError}: \PYG{n}{multiple generators [x, exp(I*x)]} | |
\end{Verbatim} | |
In fact, \code{solve} makes \emph{no guarantees whatsoever} about the completeness | |
of the solutions it finds. Much of \code{solve} is heuristics, which may find | |
some solutions to an equation or system of equations, but not all of them. | |
\end{notice} | |
\code{solve} can also solve systems of equations. Pass a list of equations and a | |
list of variables to solve for. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{solve}\PYG{p}{(}\PYG{p}{[}\PYG{n}{x} \PYG{o}{\PYGZhy{}} \PYG{n}{y} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{n}{x} \PYG{o}{+} \PYG{n}{y} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{3}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+go}{\PYGZob{}x: 1/2, y: 5/2\PYGZcb{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{solve}\PYG{p}{(}\PYG{p}{[}\PYG{n}{x}\PYG{o}{*}\PYG{n}{y} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{7}\PYG{p}{,} \PYG{n}{x} \PYG{o}{+} \PYG{n}{y} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{6}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+go}{⎡⎛ \PYGZus{}\PYGZus{}\PYGZus{} \PYGZus{}\PYGZus{}\PYGZus{} ⎞ ⎛ \PYGZus{}\PYGZus{}\PYGZus{} \PYGZus{}\PYGZus{}\PYGZus{} ⎞⎤} | |
\PYG{g+go}{⎣⎝\PYGZhy{} \textbackslash{}╱ 2 + 3, \textbackslash{}╱ 2 + 3⎠, ⎝\textbackslash{}╱ 2 + 3, \PYGZhy{} \textbackslash{}╱ 2 + 3⎠⎦} | |
\end{Verbatim} | |
\begin{notice}{note}{Note:} | |
The type of the output of \code{solve} when solving systems of equations | |
varies depending on the type of the input. If you want a consistent | |
interface, pass \code{dict=True}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{solve}\PYG{p}{(}\PYG{p}{[}\PYG{n}{x} \PYG{o}{\PYGZhy{}} \PYG{n}{y} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{n}{x} \PYG{o}{+} \PYG{n}{y} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{3}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{]}\PYG{p}{,} \PYG{n+nb}{dict}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{[\PYGZob{}x: 1/2, y: 5/2\PYGZcb{}]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{solve}\PYG{p}{(}\PYG{p}{[}\PYG{n}{x}\PYG{o}{*}\PYG{n}{y} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{7}\PYG{p}{,} \PYG{n}{x} \PYG{o}{+} \PYG{n}{y} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{6}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{]}\PYG{p}{,} \PYG{n+nb}{dict}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{⎡⎧ \PYGZus{}\PYGZus{}\PYGZus{} \PYGZus{}\PYGZus{}\PYGZus{} ⎫ ⎧ \PYGZus{}\PYGZus{}\PYGZus{} \PYGZus{}\PYGZus{}\PYGZus{} ⎫⎤} | |
\PYG{g+go}{⎢⎨x: \PYGZhy{} \textbackslash{}╱ 2 + 3, y: \textbackslash{}╱ 2 + 3⎬, ⎨x: \textbackslash{}╱ 2 + 3, y: \PYGZhy{} \textbackslash{}╱ 2 + 3⎬⎥} | |
\PYG{g+go}{⎣⎩ ⎭ ⎩ ⎭⎦} | |
\end{Verbatim} | |
\end{notice} | |
\phantomsection\label{tutorial/solvers:tutorial-roots} | |
\code{solve} reports each solution only once. To get the solutions of a | |
polynomial including multiplicity use \code{roots}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{solve}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{3} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{6}\PYG{o}{*}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{l+m+mi}{9}\PYG{o}{*}\PYG{n}{x}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{[0, 3]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{roots}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{3} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{6}\PYG{o}{*}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{l+m+mi}{9}\PYG{o}{*}\PYG{n}{x}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{\PYGZob{}0: 1, 3: 2\PYGZcb{}} | |
\end{Verbatim} | |
The output \code{\{0: 1, 3: 2\}} of \code{roots} means that \code{0} is a root of | |
multiplicity 1 and \code{3} is a root of multiplicity 2. | |
\subsection{Solving Differential Equations} | |
\label{tutorial/solvers:tutorial-dsolve}\label{tutorial/solvers:solving-differential-equations} | |
To solve differential equations, use \code{dsolve}. First, create an undefined | |
function by passing \code{cls=Function} to the \code{symbols} function. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{f}\PYG{p}{,} \PYG{n}{g} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{f g}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{cls}\PYG{o}{=}\PYG{n}{Function}\PYG{p}{)} | |
\end{Verbatim} | |
\code{f} and \code{g} are now undefined functions. We can call \code{f(x)}, and it | |
will represent an unknown function. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{f}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{f(x)} | |
\end{Verbatim} | |
Derivatives of \code{f(x)} are unevaluated. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{f}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{diff}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{d} | |
\PYG{g+go}{--(f(x))} | |
\PYG{g+go}{dx} | |
\end{Verbatim} | |
(see the {\hyperref[tutorial/calculus:tutorial-derivatives]{\emph{\DUspan{}{Derivatives}}}} (\autopageref*{tutorial/calculus:tutorial-derivatives}) section for more on | |
derivatives). | |
To represent the differential equation \(f''(x) - 2f'(x) + f(x) = \sin(x)\), we | |
would thus use | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{diffeq} \PYG{o}{=} \PYG{n}{Eq}\PYG{p}{(}\PYG{n}{f}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{diff}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{f}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{diff}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} \PYG{o}{+} \PYG{n}{f}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{,} \PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{diffeq} | |
\PYG{g+go}{ 2} | |
\PYG{g+go}{ d d} | |
\PYG{g+go}{f(x) \PYGZhy{} 2⋅--(f(x)) + ---(f(x)) = sin(x)} | |
\PYG{g+go}{ dx 2} | |
\PYG{g+go}{ dx} | |
\end{Verbatim} | |
To solve the ODE, pass it and the function to solve for to \code{dsolve}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{dsolve}\PYG{p}{(}\PYG{n}{diffeq}\PYG{p}{,} \PYG{n}{f}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{ x cos(x)} | |
\PYG{g+go}{f(x) = (C1 + C2⋅x)⋅e + ------} | |
\PYG{g+go}{ 2} | |
\end{Verbatim} | |
\code{dsolve} returns an instance of \code{Eq}. This is because in general, | |
solutions to differential equations cannot be solved explicitly for the | |
function. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{dsolve}\PYG{p}{(}\PYG{n}{f}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{diff}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{p}{(}\PYG{l+m+mi}{1} \PYG{o}{\PYGZhy{}} \PYG{n}{sin}\PYG{p}{(}\PYG{n}{f}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{)}\PYG{p}{)}\PYG{p}{,} \PYG{n}{f}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{f(x) + cos(f(x)) = C1} | |
\end{Verbatim} | |
The arbitrary constants in the solutions from dsolve are symbols of the form | |
\code{C1}, \code{C2}, \code{C3}, and so on. | |
\section{Matrices} | |
\label{tutorial/matrices:matrices}\label{tutorial/matrices::doc} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{o}{*} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{init\PYGZus{}printing}\PYG{p}{(}\PYG{n}{use\PYGZus{}unicode}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\end{Verbatim} | |
To make a matrix in SymPy, use the \code{Matrix} object. A matrix is constructed | |
by providing a list of row vectors that make up the matrix. For example, | |
to construct the matrix | |
\begin{gather} | |
\begin{split}\left[\begin{array}{cc}1 & -1\\3 & 4\\0 & 2\end{array}\right]\end{split}\notag | |
\end{gather} | |
use | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Matrix}\PYG{p}{(}\PYG{p}{[}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{4}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{]}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+go}{⎡1 \PYGZhy{}1⎤} | |
\PYG{g+go}{⎢ ⎥} | |
\PYG{g+go}{⎢3 4 ⎥} | |
\PYG{g+go}{⎢ ⎥} | |
\PYG{g+go}{⎣0 2 ⎦} | |
\end{Verbatim} | |
To make it easy to make column vectors, a list of elements is considered to be | |
a column vector. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Matrix}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+go}{⎡1⎤} | |
\PYG{g+go}{⎢ ⎥} | |
\PYG{g+go}{⎢2⎥} | |
\PYG{g+go}{⎢ ⎥} | |
\PYG{g+go}{⎣3⎦} | |
\end{Verbatim} | |
Matrices are manipulated just like any other object in SymPy or Python. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{M} \PYG{o}{=} \PYG{n}{Matrix}\PYG{p}{(}\PYG{p}{[}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{N} \PYG{o}{=} \PYG{n}{Matrix}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{M}\PYG{o}{*}\PYG{n}{N} | |
\PYG{g+go}{⎡5⎤} | |
\PYG{g+go}{⎢ ⎥} | |
\PYG{g+go}{⎣3⎦} | |
\end{Verbatim} | |
One important thing to note about SymPy matrices is that, unlike every other | |
object in SymPy, they are mutable. This means that they can be modified in | |
place, as we will see below. The downside to this is that \code{Matrix} cannot | |
be used in places that require immutability, such as inside other SymPy | |
expressions or as keys to dictionaries. If you need an immutable version of | |
\code{Matrix}, use \code{ImmutableMatrix}. | |
\subsection{Basic Operations} | |
\label{tutorial/matrices:basic-operations} | |
\subsubsection{Shape} | |
\label{tutorial/matrices:shape} | |
Here are some basic operations on \code{Matrix}. To get the shape of a matrix | |
use \code{shape} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{M} \PYG{o}{=} \PYG{n}{Matrix}\PYG{p}{(}\PYG{p}{[}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{4}\PYG{p}{]}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{M} | |
\PYG{g+go}{⎡1 2 3⎤} | |
\PYG{g+go}{⎢ ⎥} | |
\PYG{g+go}{⎣\PYGZhy{}2 0 4⎦} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{M}\PYG{o}{.}\PYG{n}{shape} | |
\PYG{g+go}{(2, 3)} | |
\end{Verbatim} | |
\subsubsection{Accessing Rows and Columns} | |
\label{tutorial/matrices:accessing-rows-and-columns} | |
To get an individual row or column of a matrix, use \code{row} or \code{col}. For | |
example, \code{M.row(0)} will get the first row. \code{M.col(-1)} will get the last | |
column. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{M}\PYG{o}{.}\PYG{n}{row}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{)} | |
\PYG{g+go}{[1 2 3]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{M}\PYG{o}{.}\PYG{n}{col}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+go}{⎡3⎤} | |
\PYG{g+go}{⎢ ⎥} | |
\PYG{g+go}{⎣4⎦} | |
\end{Verbatim} | |
\subsubsection{Deleting and Inserting Rows and Columns} | |
\label{tutorial/matrices:deleting-and-inserting-rows-and-columns} | |
To delete a row or column, use \code{row\_del} or \code{col\_del}. These operations | |
will modify the Matrix \textbf{in place}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{M}\PYG{o}{.}\PYG{n}{col\PYGZus{}del}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{M} | |
\PYG{g+go}{⎡2 3⎤} | |
\PYG{g+go}{⎢ ⎥} | |
\PYG{g+go}{⎣0 4⎦} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{M}\PYG{o}{.}\PYG{n}{row\PYGZus{}del}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{M} | |
\PYG{g+go}{[2 3]} | |
\end{Verbatim} | |
To insert rows or columns, use \code{row\_insert} or \code{col\_insert}. These | |
operations \textbf{do not} operate in place. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{M} | |
\PYG{g+go}{[2 3]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{M} \PYG{o}{=} \PYG{n}{M}\PYG{o}{.}\PYG{n}{row\PYGZus{}insert}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{Matrix}\PYG{p}{(}\PYG{p}{[}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{4}\PYG{p}{]}\PYG{p}{]}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{M} | |
\PYG{g+go}{⎡2 3⎤} | |
\PYG{g+go}{⎢ ⎥} | |
\PYG{g+go}{⎣0 4⎦} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{M} \PYG{o}{=} \PYG{n}{M}\PYG{o}{.}\PYG{n}{col\PYGZus{}insert}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{n}{Matrix}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{2}\PYG{p}{]}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{M} | |
\PYG{g+go}{⎡1 2 3⎤} | |
\PYG{g+go}{⎢ ⎥} | |
\PYG{g+go}{⎣\PYGZhy{}2 0 4⎦} | |
\end{Verbatim} | |
Unless explicitly stated, the methods mentioned below do not operate in | |
place. In general, a method that does not operate in place will return a new | |
\code{Matrix} and a method that does operate in place will return \code{None}. | |
\subsection{Basic Methods} | |
\label{tutorial/matrices:basic-methods} | |
As noted above, simple operations like addition and multiplication are done | |
just by using \code{+}, \code{*}, and \code{**}. To find the inverse of a matrix, just | |
raise it to the \code{-1} power. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{M} \PYG{o}{=} \PYG{n}{Matrix}\PYG{p}{(}\PYG{p}{[}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{]}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{N} \PYG{o}{=} \PYG{n}{Matrix}\PYG{p}{(}\PYG{p}{[}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{7}\PYG{p}{]}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{M} \PYG{o}{+} \PYG{n}{N} | |
\PYG{g+go}{⎡1 6 ⎤} | |
\PYG{g+go}{⎢ ⎥} | |
\PYG{g+go}{⎣\PYGZhy{}2 10⎦} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{M}\PYG{o}{*}\PYG{n}{N} | |
\PYG{g+go}{⎡0 24⎤} | |
\PYG{g+go}{⎢ ⎥} | |
\PYG{g+go}{⎣0 15⎦} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{M} | |
\PYG{g+go}{⎡3 9⎤} | |
\PYG{g+go}{⎢ ⎥} | |
\PYG{g+go}{⎣\PYGZhy{}6 9⎦} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{M}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} | |
\PYG{g+go}{⎡\PYGZhy{}5 12⎤} | |
\PYG{g+go}{⎢ ⎥} | |
\PYG{g+go}{⎣\PYGZhy{}8 3 ⎦} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{M}\PYG{o}{*}\PYG{o}{*}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{1} | |
\PYG{g+go}{⎡1/3 \PYGZhy{}1/3⎤} | |
\PYG{g+go}{⎢ ⎥} | |
\PYG{g+go}{⎣2/9 1/9 ⎦} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{N}\PYG{o}{*}\PYG{o}{*}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{1} | |
\PYG{g+gt}{Traceback (most recent call last):} | |
\PYG{c}{...} | |
\PYG{g+gr}{ValueError}: \PYG{n}{Matrix det == 0; not invertible.} | |
\PYG{g+gt}{Traceback (most recent call last):} | |
\PYG{c}{...} | |
\PYG{g+gr}{ValueError}: \PYG{n}{Matrix det == 0; not invertible.} | |
\end{Verbatim} | |
To take the transpose of a Matrix, use \code{T}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{M} \PYG{o}{=} \PYG{n}{Matrix}\PYG{p}{(}\PYG{p}{[}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mi}{4}\PYG{p}{,} \PYG{l+m+mi}{5}\PYG{p}{,} \PYG{l+m+mi}{6}\PYG{p}{]}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{M} | |
\PYG{g+go}{⎡1 2 3⎤} | |
\PYG{g+go}{⎢ ⎥} | |
\PYG{g+go}{⎣4 5 6⎦} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{M}\PYG{o}{.}\PYG{n}{T} | |
\PYG{g+go}{⎡1 4⎤} | |
\PYG{g+go}{⎢ ⎥} | |
\PYG{g+go}{⎢2 5⎥} | |
\PYG{g+go}{⎢ ⎥} | |
\PYG{g+go}{⎣3 6⎦} | |
\end{Verbatim} | |
\subsection{Matrix Constructors} | |
\label{tutorial/matrices:matrix-constructors} | |
Several constructors exist for creating common matrices. To create an | |
identity matrix, use \code{eye}. \code{eye(n)} will create an \(n\times n\) identity matrix. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{eye}\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{p}{)} | |
\PYG{g+go}{⎡1 0 0⎤} | |
\PYG{g+go}{⎢ ⎥} | |
\PYG{g+go}{⎢0 1 0⎥} | |
\PYG{g+go}{⎢ ⎥} | |
\PYG{g+go}{⎣0 0 1⎦} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{eye}\PYG{p}{(}\PYG{l+m+mi}{4}\PYG{p}{)} | |
\PYG{g+go}{⎡1 0 0 0⎤} | |
\PYG{g+go}{⎢ ⎥} | |
\PYG{g+go}{⎢0 1 0 0⎥} | |
\PYG{g+go}{⎢ ⎥} | |
\PYG{g+go}{⎢0 0 1 0⎥} | |
\PYG{g+go}{⎢ ⎥} | |
\PYG{g+go}{⎣0 0 0 1⎦} | |
\end{Verbatim} | |
To create a matrix of all zeros, use \code{zeros}. \code{zeros(n, m)} creates an | |
\(n\times m\) matrix of \(0\)s. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{zeros}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{)} | |
\PYG{g+go}{⎡0 0 0⎤} | |
\PYG{g+go}{⎢ ⎥} | |
\PYG{g+go}{⎣0 0 0⎦} | |
\end{Verbatim} | |
Similarly, \code{ones} creates a matrix of ones. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{ones}\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+go}{⎡1 1⎤} | |
\PYG{g+go}{⎢ ⎥} | |
\PYG{g+go}{⎢1 1⎥} | |
\PYG{g+go}{⎢ ⎥} | |
\PYG{g+go}{⎣1 1⎦} | |
\end{Verbatim} | |
To create diagonal matrices, use \code{diag}. The arguments to \code{diag} can be | |
either numbers or matrices. A number is interpreted as a \(1\times 1\) | |
matrix. The matrices are stacked diagonally. The remaining elements are | |
filled with \(0\)s. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{diag}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{)} | |
\PYG{g+go}{⎡1 0 0⎤} | |
\PYG{g+go}{⎢ ⎥} | |
\PYG{g+go}{⎢0 2 0⎥} | |
\PYG{g+go}{⎢ ⎥} | |
\PYG{g+go}{⎣0 0 3⎦} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{diag}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{ones}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{,} \PYG{n}{Matrix}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{5}\PYG{p}{,} \PYG{l+m+mi}{7}\PYG{p}{,} \PYG{l+m+mi}{5}\PYG{p}{]}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{⎡\PYGZhy{}1 0 0 0⎤} | |
\PYG{g+go}{⎢ ⎥} | |
\PYG{g+go}{⎢0 1 1 0⎥} | |
\PYG{g+go}{⎢ ⎥} | |
\PYG{g+go}{⎢0 1 1 0⎥} | |
\PYG{g+go}{⎢ ⎥} | |
\PYG{g+go}{⎢0 0 0 5⎥} | |
\PYG{g+go}{⎢ ⎥} | |
\PYG{g+go}{⎢0 0 0 7⎥} | |
\PYG{g+go}{⎢ ⎥} | |
\PYG{g+go}{⎣0 0 0 5⎦} | |
\end{Verbatim} | |
\subsection{Advanced Methods} | |
\label{tutorial/matrices:advanced-methods} | |
\subsubsection{Determinant} | |
\label{tutorial/matrices:determinant} | |
To compute the determinant of a matrix, use \code{det}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{M} \PYG{o}{=} \PYG{n}{Matrix}\PYG{p}{(}\PYG{p}{[}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mi}{4}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{]}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{M} | |
\PYG{g+go}{⎡1 0 1⎤} | |
\PYG{g+go}{⎢ ⎥} | |
\PYG{g+go}{⎢2 \PYGZhy{}1 3⎥} | |
\PYG{g+go}{⎢ ⎥} | |
\PYG{g+go}{⎣4 3 2⎦} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{M}\PYG{o}{.}\PYG{n}{det}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{\PYGZhy{}1} | |
\end{Verbatim} | |
\subsubsection{RREF} | |
\label{tutorial/matrices:rref} | |
To put a matrix into reduced row echelon form, use \code{rref}. \code{rref} returns | |
a tuple of two elements. The first is the reduced row echelon form, and the | |
second is a list of indices of the pivot columns. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{M} \PYG{o}{=} \PYG{n}{Matrix}\PYG{p}{(}\PYG{p}{[}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{4}\PYG{p}{,} \PYG{l+m+mi}{7}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{3}\PYG{p}{,} \PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{3}\PYG{p}{,} \PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{4}\PYG{p}{]}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{M} | |
\PYG{g+go}{⎡1 0 1 3 ⎤} | |
\PYG{g+go}{⎢ ⎥} | |
\PYG{g+go}{⎢2 3 4 7 ⎥} | |
\PYG{g+go}{⎢ ⎥} | |
\PYG{g+go}{⎣\PYGZhy{}1 \PYGZhy{}3 \PYGZhy{}3 \PYGZhy{}4⎦} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{M}\PYG{o}{.}\PYG{n}{rref}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{⎛⎡1 0 1 3 ⎤, [0, 1]⎞} | |
\PYG{g+go}{⎜⎢ ⎥ ⎟} | |
\PYG{g+go}{⎜⎢0 1 2/3 1/3⎥ ⎟} | |
\PYG{g+go}{⎜⎢ ⎥ ⎟} | |
\PYG{g+go}{⎝⎣0 0 0 0 ⎦ ⎠} | |
\end{Verbatim} | |
\begin{notice}{note}{Note:} | |
The first element of the tuple returned by \code{rref} is of type | |
\code{Matrix}. The second is of type \code{list}. | |
\end{notice} | |
\subsubsection{Nullspace} | |
\label{tutorial/matrices:nullspace} | |
To find the nullspace of a matrix, use \code{nullspace}. \code{nullspace} returns a | |
\code{list} of column vectors that span the nullspace of the matrix. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{M} \PYG{o}{=} \PYG{n}{Matrix}\PYG{p}{(}\PYG{p}{[}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mi}{4}\PYG{p}{,} \PYG{l+m+mi}{10}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{M} | |
\PYG{g+go}{⎡1 2 3 0 0⎤} | |
\PYG{g+go}{⎢ ⎥} | |
\PYG{g+go}{⎣4 10 0 0 1⎦} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{M}\PYG{o}{.}\PYG{n}{nullspace}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{⎡⎡\PYGZhy{}15⎤, ⎡0⎤, ⎡ 1 ⎤⎤} | |
\PYG{g+go}{⎢⎢ ⎥ ⎢ ⎥ ⎢ ⎥⎥} | |
\PYG{g+go}{⎢⎢ 6 ⎥ ⎢0⎥ ⎢\PYGZhy{}1/2⎥⎥} | |
\PYG{g+go}{⎢⎢ ⎥ ⎢ ⎥ ⎢ ⎥⎥} | |
\PYG{g+go}{⎢⎢ 1 ⎥ ⎢0⎥ ⎢ 0 ⎥⎥} | |
\PYG{g+go}{⎢⎢ ⎥ ⎢ ⎥ ⎢ ⎥⎥} | |
\PYG{g+go}{⎢⎢ 0 ⎥ ⎢1⎥ ⎢ 0 ⎥⎥} | |
\PYG{g+go}{⎢⎢ ⎥ ⎢ ⎥ ⎢ ⎥⎥} | |
\PYG{g+go}{⎣⎣ 0 ⎦ ⎣0⎦ ⎣ 1 ⎦⎦} | |
\end{Verbatim} | |
\subsubsection{Eigenvalues, Eigenvectors, and Diagonalization} | |
\label{tutorial/matrices:eigenvalues-eigenvectors-and-diagonalization} | |
To find the eigenvalues of a matrix, use \code{eigenvals}. \code{eigenvals} | |
returns a dictionary of \code{eigenvalue:algebraic multiplicity} pairs (similar to the | |
output of {\hyperref[tutorial/solvers:tutorial-roots]{\emph{\DUspan{}{roots}}}} (\autopageref*{tutorial/solvers:tutorial-roots})). | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{M} \PYG{o}{=} \PYG{n}{Matrix}\PYG{p}{(}\PYG{p}{[}\PYG{p}{[}\PYG{l+m+mi}{3}\PYG{p}{,} \PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{4}\PYG{p}{,} \PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{2}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mi}{5}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{,} \PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{3}\PYG{p}{,} \PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{2}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mi}{5}\PYG{p}{,} \PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{2}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mi}{5}\PYG{p}{,} \PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{]}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{M} | |
\PYG{g+go}{⎡3 \PYGZhy{}2 4 \PYGZhy{}2⎤} | |
\PYG{g+go}{⎢ ⎥} | |
\PYG{g+go}{⎢5 3 \PYGZhy{}3 \PYGZhy{}2⎥} | |
\PYG{g+go}{⎢ ⎥} | |
\PYG{g+go}{⎢5 \PYGZhy{}2 2 \PYGZhy{}2⎥} | |
\PYG{g+go}{⎢ ⎥} | |
\PYG{g+go}{⎣5 \PYGZhy{}2 \PYGZhy{}3 3 ⎦} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{M}\PYG{o}{.}\PYG{n}{eigenvals}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{\PYGZob{}\PYGZhy{}2: 1, 3: 1, 5: 2\PYGZcb{}} | |
\end{Verbatim} | |
This means that \code{M} has eigenvalues -2, 3, and 5, and that the | |
eigenvalues -2 and 3 have algebraic multiplicity 1 and that the eigenvalue 5 | |
has algebraic multiplicity 2. | |
To find the eigenvectors of a matrix, use \code{eigenvects}. \code{eigenvects} | |
returns a list of tuples of the form \code{(eigenvalue:algebraic multiplicity, | |
{[}eigenvectors{]})}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{M}\PYG{o}{.}\PYG{n}{eigenvects}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{⎡⎛\PYGZhy{}2, 1, ⎡⎡0⎤⎤⎞, ⎛3, 1, ⎡⎡1⎤⎤⎞, ⎛5, 2, ⎡⎡1⎤, ⎡0 ⎤⎤⎞⎤} | |
\PYG{g+go}{⎢⎜ ⎢⎢ ⎥⎥⎟ ⎜ ⎢⎢ ⎥⎥⎟ ⎜ ⎢⎢ ⎥ ⎢ ⎥⎥⎟⎥} | |
\PYG{g+go}{⎢⎜ ⎢⎢1⎥⎥⎟ ⎜ ⎢⎢1⎥⎥⎟ ⎜ ⎢⎢1⎥ ⎢\PYGZhy{}1⎥⎥⎟⎥} | |
\PYG{g+go}{⎢⎜ ⎢⎢ ⎥⎥⎟ ⎜ ⎢⎢ ⎥⎥⎟ ⎜ ⎢⎢ ⎥ ⎢ ⎥⎥⎟⎥} | |
\PYG{g+go}{⎢⎜ ⎢⎢1⎥⎥⎟ ⎜ ⎢⎢1⎥⎥⎟ ⎜ ⎢⎢1⎥ ⎢0 ⎥⎥⎟⎥} | |
\PYG{g+go}{⎢⎜ ⎢⎢ ⎥⎥⎟ ⎜ ⎢⎢ ⎥⎥⎟ ⎜ ⎢⎢ ⎥ ⎢ ⎥⎥⎟⎥} | |
\PYG{g+go}{⎣⎝ ⎣⎣1⎦⎦⎠ ⎝ ⎣⎣1⎦⎦⎠ ⎝ ⎣⎣0⎦ ⎣1 ⎦⎦⎠⎦} | |
\end{Verbatim} | |
This shows us that, for example, the eigenvalue 5 also has geometric | |
multiplicity 2, because it has two eigenvectors. Because the algebraic and | |
geometric multiplicities are the same for all the eigenvalues, \code{M} is | |
diagonalizable. | |
To diagonalize a matrix, use \code{diagonalize}. \code{diagonalize} returns a tuple | |
\((P, D)\), where \(D\) is diagonal and \(M = PDP^{-1}\). | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{P}\PYG{p}{,} \PYG{n}{D} \PYG{o}{=} \PYG{n}{M}\PYG{o}{.}\PYG{n}{diagonalize}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{P} | |
\PYG{g+go}{⎡0 1 1 0 ⎤} | |
\PYG{g+go}{⎢ ⎥} | |
\PYG{g+go}{⎢1 1 1 \PYGZhy{}1⎥} | |
\PYG{g+go}{⎢ ⎥} | |
\PYG{g+go}{⎢1 1 1 0 ⎥} | |
\PYG{g+go}{⎢ ⎥} | |
\PYG{g+go}{⎣1 1 0 1 ⎦} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{D} | |
\PYG{g+go}{⎡\PYGZhy{}2 0 0 0⎤} | |
\PYG{g+go}{⎢ ⎥} | |
\PYG{g+go}{⎢0 3 0 0⎥} | |
\PYG{g+go}{⎢ ⎥} | |
\PYG{g+go}{⎢0 0 5 0⎥} | |
\PYG{g+go}{⎢ ⎥} | |
\PYG{g+go}{⎣0 0 0 5⎦} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{P}\PYG{o}{*}\PYG{n}{D}\PYG{o}{*}\PYG{n}{P}\PYG{o}{*}\PYG{o}{*}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{1} | |
\PYG{g+go}{⎡3 \PYGZhy{}2 4 \PYGZhy{}2⎤} | |
\PYG{g+go}{⎢ ⎥} | |
\PYG{g+go}{⎢5 3 \PYGZhy{}3 \PYGZhy{}2⎥} | |
\PYG{g+go}{⎢ ⎥} | |
\PYG{g+go}{⎢5 \PYGZhy{}2 2 \PYGZhy{}2⎥} | |
\PYG{g+go}{⎢ ⎥} | |
\PYG{g+go}{⎣5 \PYGZhy{}2 \PYGZhy{}3 3 ⎦} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{P}\PYG{o}{*}\PYG{n}{D}\PYG{o}{*}\PYG{n}{P}\PYG{o}{*}\PYG{o}{*}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{1} \PYG{o}{==} \PYG{n}{M} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
\setbox0\vbox{ | |
\begin{minipage}{0.95\linewidth} | |
\textbf{Quick Tip} | |
\medskip | |
\code{lambda} is a reserved keyword in Python, so to create a Symbol called | |
\(\lambda\), while using the same names for SymPy Symbols and Python | |
variables, use \code{lamda} (without the \code{b}). It will still pretty print | |
as \(\lambda\). | |
\end{minipage}} | |
\begin{center}\setlength{\fboxsep}{5pt}\shadowbox{\box0}\end{center} | |
Note that since \code{eigenvects} also includes the eigenvalues, you should use | |
it instead of \code{eigenvals} if you also want the eigenvectors. However, as | |
computing the eigenvectors may often be costly, \code{eigenvals} should be | |
preferred if you only wish to find the eigenvalues. | |
If all you want is the characteristic polynomial, use \code{charpoly}. This is | |
more efficient than \code{eigenvals}, because sometimes symbolic roots can be | |
expensive to calculate. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{lamda} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{lamda}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{M}\PYG{o}{.}\PYG{n}{charpoly}\PYG{p}{(}\PYG{n}{lamda}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{factor}\PYG{p}{(}\PYG{n}{p}\PYG{p}{)} | |
\PYG{g+go}{ 2} | |
\PYG{g+go}{(\(\lambda\) \PYGZhy{} 5) ⋅(\(\lambda\) \PYGZhy{} 3)⋅(\(\lambda\) + 2)} | |
\end{Verbatim} | |
\section{Advanced Expression Manipulation} | |
\label{tutorial/manipulation:tutorial-manipulation}\label{tutorial/manipulation::doc}\label{tutorial/manipulation:advanced-expression-manipulation} | |
In this section, we discuss some ways that we can perform advanced | |
manipulation of expressions. | |
\subsection{Understanding Expression Trees} | |
\label{tutorial/manipulation:understanding-expression-trees}\setbox0\vbox{ | |
\begin{minipage}{0.95\linewidth} | |
\textbf{Quick Tip} | |
\medskip | |
To play with the \code{srepr} form of expressions in the SymPy Live shell, | |
change the output format to \code{Repr} in the settings. | |
\end{minipage}} | |
\begin{center}\setlength{\fboxsep}{5pt}\shadowbox{\box0}\end{center} | |
Before we can do this, we need to understand how expressions are represented | |
in SymPy. A mathematical expression is represented as a tree. Let us take | |
the expression \(x^2 + xy\), i.e., \code{x**2 + x*y}. We can see what this | |
expression looks like internally by using \code{srepr} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{o}{*} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{z} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x y z}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} \PYG{o}{=} \PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{n}{x}\PYG{o}{*}\PYG{n}{y} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{srepr}\PYG{p}{(}\PYG{n}{expr}\PYG{p}{)} | |
\PYG{g+go}{\PYGZdq{}Add(Pow(Symbol(\PYGZsq{}x\PYGZsq{}), Integer(2)), Mul(Symbol(\PYGZsq{}x\PYGZsq{}), Symbol(\PYGZsq{}y\PYGZsq{})))\PYGZdq{}} | |
\end{Verbatim} | |
The easiest way to tear this apart is to look at a diagram of the expression | |
tree: | |
\includegraphics{graphviz-d8d6268439273f5c9659310fcb25b50fd18c6260.pdf} | |
\begin{notice}{note}{Note:} | |
The above diagram was made using \href{http://www.graphviz.org/}{Graphviz} and | |
the {\hyperref[modules/printing:sympy.printing.dot.dotprint]{\emph{\code{dotprint}}}} (\autopageref*{modules/printing:sympy.printing.dot.dotprint}) function. | |
\end{notice} | |
First, let's look at the leaves of this tree. Symbols are instances of the | |
class Symbol. While we have been doing | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\end{Verbatim} | |
we could have also done | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x} \PYG{o}{=} \PYG{n}{Symbol}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\end{Verbatim} | |
Either way, we get a Symbol with the name ``x'' \footnote{ | |
We have been using \code{symbols} instead of \code{Symbol} because it | |
automatically splits apart strings into multiple \code{Symbol}s. | |
\code{symbols('x y z')} returns a tuple of three \code{Symbol}s. \code{Symbol('x y | |
z')} returns a single \code{Symbol} called \code{x y z}. | |
}. For the number in the | |
expression, 2, we got \code{Integer(2)}. \code{Integer} is the SymPy class for | |
integers. It is similar to the Python built-in type \code{int}, except that | |
\code{Integer} plays nicely with other SymPy types. | |
When we write \code{x**2}, this creates a \code{Pow} object. \code{Pow} is short for | |
``power''. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{srepr}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+go}{\PYGZdq{}Pow(Symbol(\PYGZsq{}x\PYGZsq{}), Integer(2))\PYGZdq{}} | |
\end{Verbatim} | |
We could have created the same object by calling \code{Pow(x, 2)} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Pow}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+go}{x**2} | |
\end{Verbatim} | |
Note that in the \code{srepr} output, we see \code{Integer(2)}, the SymPy version of | |
integers, even though technically, we input \code{2}, a Python int. In general, | |
whenever you combine a SymPy object with a non-SymPy object via some function | |
or operation, the non-SymPy object will be converted into a SymPy object. The | |
function that does this is \code{sympify} \footnote{ | |
Technically, it is an internal function called \code{\_sympify}, | |
which differs from \code{sympify} in that it does not convert strings. \code{x + | |
'2'} is not allowed. | |
}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb}{type}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+go}{\PYGZlt{}... \PYGZsq{}int\PYGZsq{}\PYGZgt{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb}{type}\PYG{p}{(}\PYG{n}{sympify}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{\PYGZlt{}class \PYGZsq{}sympy.core.numbers.Integer\PYGZsq{}\PYGZgt{}} | |
\end{Verbatim} | |
We have seen that \code{x**2} is represented as \code{Pow(x, 2)}. What about | |
\code{x*y}? As we might expect, this is the multiplication of \code{x} and \code{y}. | |
The SymPy class for multiplication is \code{Mul}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{srepr}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)} | |
\PYG{g+go}{\PYGZdq{}Mul(Symbol(\PYGZsq{}x\PYGZsq{}), Symbol(\PYGZsq{}y\PYGZsq{}))\PYGZdq{}} | |
\end{Verbatim} | |
Thus, we could have created the same object by writing \code{Mul(x, y)}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Mul}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{)} | |
\PYG{g+go}{x*y} | |
\end{Verbatim} | |
Now we get to our final expression, \code{x**2 + x*y}. This is the addition of | |
our last two objects, \code{Pow(x, 2)}, and \code{Mul(x, y)}. The SymPy class for | |
addition is \code{Add}, so, as you might expect, to create this object, we use | |
\code{Add(Pow(x, 2), Mul(x, y))}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Add}\PYG{p}{(}\PYG{n}{Pow}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{,} \PYG{n}{Mul}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{x**2 + x*y} | |
\end{Verbatim} | |
SymPy expression trees can have many branches, and can be quite deep or quite | |
broad. Here is a more complicated example | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} \PYG{o}{=} \PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{/}\PYG{l+m+mi}{2} \PYG{o}{\PYGZhy{}} \PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{o}{/}\PYG{n}{y} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{srepr}\PYG{p}{(}\PYG{n}{expr}\PYG{p}{)} | |
\PYG{g+go}{\PYGZdq{}Add(Mul(Integer(\PYGZhy{}1), Pow(Symbol(\PYGZsq{}x\PYGZsq{}), Integer(2))), Mul(Rational(1, 2),} | |
\PYG{g+go}{sin(Mul(Symbol(\PYGZsq{}x\PYGZsq{}), Symbol(\PYGZsq{}y\PYGZsq{})))), Pow(Symbol(\PYGZsq{}y\PYGZsq{}), Integer(\PYGZhy{}1)))\PYGZdq{}} | |
\end{Verbatim} | |
Here is a diagram | |
\includegraphics{graphviz-a15fbf4039d3f5488f10cf31b136816de047baa7.pdf} | |
This expression reveals some interesting things about SymPy expression | |
trees. Let's go through them one by one. | |
Let's first look at the term \code{x**2}. As we expected, we see \code{Pow(x, 2)}. | |
One level up, we see we have \code{Mul(-1, Pow(x, 2))}. There is no subtraction | |
class in SymPy. \code{x - y} is represented as \code{x + -y}, or, more completely, | |
\code{x + -1*y}, i.e., \code{Add(x, Mul(-1, y))}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} \PYG{o}{=} \PYG{n}{x} \PYG{o}{\PYGZhy{}} \PYG{n}{y} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{srepr}\PYG{p}{(}\PYG{n}{x} \PYG{o}{\PYGZhy{}} \PYG{n}{y}\PYG{p}{)} | |
\PYG{g+go}{\PYGZdq{}Add(Symbol(\PYGZsq{}x\PYGZsq{}), Mul(Integer(\PYGZhy{}1), Symbol(\PYGZsq{}y\PYGZsq{})))\PYGZdq{}} | |
\end{Verbatim} | |
\includegraphics{graphviz-751d8866c8b62cfbeb5393714bd4f17241679175.pdf} | |
Next, look at \code{1/y}. We might expect to see something like \code{Div(1, y)}, | |
but similar to subtraction, there is no class in SymPy for division. Rather, | |
division is represented by a power of -1. Hence, we have \code{Pow(y, -1)}. | |
What if we had divided something other than 1 by \code{y}, like \code{x/y}? Let's | |
see. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} \PYG{o}{=} \PYG{n}{x}\PYG{o}{/}\PYG{n}{y} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{srepr}\PYG{p}{(}\PYG{n}{expr}\PYG{p}{)} | |
\PYG{g+go}{\PYGZdq{}Mul(Symbol(\PYGZsq{}x\PYGZsq{}), Pow(Symbol(\PYGZsq{}y\PYGZsq{}), Integer(\PYGZhy{}1)))\PYGZdq{}} | |
\end{Verbatim} | |
\includegraphics{graphviz-e878bf0458393c212427a889ec15220a360a91b4.pdf} | |
We see that \code{x/y} is represented as \code{x*y**-1}, i.e., \code{Mul(x, Pow(y, | |
-1))}. | |
Finally, let's look at the \code{sin(x*y)/2} term. Following the pattern of the | |
previous example, we might expect to see \code{Mul(sin(x*y), Pow(Integer(2), | |
-1))}. But instead, we have \code{Mul(Rational(1, 2), sin(x*y))}. Rational | |
numbers are always combined into a single term in a multiplication, so that | |
when we divide by 2, it is represented as multiplying by 1/2. | |
Finally, one last note. You may have noticed that the order we entered our | |
expression and the order that it came out from \code{srepr} or in the graph were | |
different. You may have also noticed this phenonemon earlier in the | |
tutorial. For example | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{l+m+mi}{1} \PYG{o}{+} \PYG{n}{x} | |
\PYG{g+go}{x + 1} | |
\end{Verbatim} | |
This because in SymPy, the arguments of the commutative operations \code{Add} and | |
\code{Mul} are stored in an arbitrary (but consistent!) order, which is | |
independent of the order inputted (if you're worried about noncommutative | |
multiplication, don't be. In SymPy, you can create noncommutative Symbols | |
using \code{Symbol('A', commutative=False)}, and the order of multiplication for | |
noncommutative Symbols is kept the same as the input). Furthermore, as we | |
shall see in the next section, the printing order and the order in which | |
things are stored internally need not be the same either. | |
\setbox0\vbox{ | |
\begin{minipage}{0.95\linewidth} | |
\textbf{Quick Tip} | |
\medskip | |
The way an expression is represented internally and the way it is printed | |
are often not the same. | |
\end{minipage}} | |
\begin{center}\setlength{\fboxsep}{5pt}\shadowbox{\box0}\end{center} | |
In general, an important thing to keep in mind when working with SymPy expression | |
trees is this: the internal representation of an expression and the way it is | |
printed need not be the same. The same is true for the input form. If some | |
expression manipulation algorithm is not working in the way you expected it | |
to, chances are, the internal representation of the object is different from | |
what you thought it was. | |
\subsection{Recursing through an Expression Tree} | |
\label{tutorial/manipulation:recursing-through-an-expression-tree} | |
Now that you know how expression trees work in SymPy, let's look at how to dig | |
our way through an expression tree. Every object in SymPy has two very | |
important attributes, \code{func}, and \code{args}. | |
\subsubsection{func} | |
\label{tutorial/manipulation:func} | |
\code{func} is the head of the object. For example, \code{(x*y).func} is \code{Mul}. | |
Usually it is the same as the class of the object (though there are exceptions | |
to this rule). | |
Two notes about \code{func}. First, the class of an object need not be the same | |
as the one used to create it. For example | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} \PYG{o}{=} \PYG{n}{Add}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr}\PYG{o}{.}\PYG{n}{func} | |
\PYG{g+go}{\PYGZlt{}class \PYGZsq{}sympy.core.mul.Mul\PYGZsq{}\PYGZgt{}} | |
\end{Verbatim} | |
We created \code{Add(x, x)}, so we might expect \code{expr.func} to be \code{Add}, but | |
instead we got \code{Mul}. Why is that? Let's take a closer look at \code{expr}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} | |
\PYG{g+go}{2*x} | |
\end{Verbatim} | |
\code{Add(x, x)}, i.e., \code{x + x}, was automatically converted into \code{Mul(2, | |
x)}, i.e., \code{2*x}, which is a \code{Mul}. SymPy classes make heavy use of the | |
\code{\_\_new\_\_} class constructor, which, unlike \code{\_\_init\_\_}, allows a different | |
class to be returned from the constructor. | |
Second, some classes are special-cased, usually for efficiency reasons | |
\footnote{ | |
Classes like \code{One} and \code{Zero} are singletonized, meaning | |
that only one object is ever created, no matter how many times the class is | |
called. This is done for space efficiency, as these classes are very | |
common. For example, \code{Zero} might occur very often in a sparse matrix | |
represented densely. As we have seen, \code{NegativeOne} occurs any time we | |
have \code{-x} or \code{1/x}. It is also done for speed efficiency because | |
singletonized objects can be compared by \code{is}. The unique objects for | |
each singletonized class can be accessed from the \code{S} object. | |
}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Integer}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{.}\PYG{n}{func} | |
\PYG{g+go}{\PYGZlt{}class \PYGZsq{}sympy.core.numbers.Integer\PYGZsq{}\PYGZgt{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Integer}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{)}\PYG{o}{.}\PYG{n}{func} | |
\PYG{g+go}{\PYGZlt{}class \PYGZsq{}sympy.core.numbers.Zero\PYGZsq{}\PYGZgt{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Integer}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{.}\PYG{n}{func} | |
\PYG{g+go}{\PYGZlt{}class \PYGZsq{}sympy.core.numbers.NegativeOne\PYGZsq{}\PYGZgt{}} | |
\end{Verbatim} | |
For the most part, these issues will not bother us. The special classes | |
\code{Zero}, \code{One}, \code{NegativeOne}, and so on are subclasses of \code{Integer}, | |
so as long as you use \code{isinstance}, it will not be an issue. | |
\subsubsection{args} | |
\label{tutorial/manipulation:args} | |
\code{args} are the top-level arguments of the object. \code{(x*y).args} would be | |
\code{(x, y)}. Let's look at some examples | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} \PYG{o}{=} \PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{y}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr}\PYG{o}{.}\PYG{n}{func} | |
\PYG{g+go}{\PYGZlt{}class \PYGZsq{}sympy.core.mul.Mul\PYGZsq{}\PYGZgt{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr}\PYG{o}{.}\PYG{n}{args} | |
\PYG{g+go}{(3, x, y**2)} | |
\end{Verbatim} | |
From this, we can see that \code{expr == Mul(3, y**2, x)}. In fact, we can see | |
that we can completely reconstruct \code{expr} from its \code{func} and its | |
\code{args}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr}\PYG{o}{.}\PYG{n}{func}\PYG{p}{(}\PYG{o}{*}\PYG{n}{expr}\PYG{o}{.}\PYG{n}{args}\PYG{p}{)} | |
\PYG{g+go}{3*x*y**2} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} \PYG{o}{==} \PYG{n}{expr}\PYG{o}{.}\PYG{n}{func}\PYG{p}{(}\PYG{o}{*}\PYG{n}{expr}\PYG{o}{.}\PYG{n}{args}\PYG{p}{)} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
Note that although we entered \code{3*y**2*x}, the \code{args} are \code{(3, x, y**2)}. | |
In a \code{Mul}, the Rational coefficient will come first in the \code{args}, but | |
other than that, the order of everything else follows no special pattern. To | |
be sure, though, there is an order. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} \PYG{o}{=} \PYG{n}{y}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr}\PYG{o}{.}\PYG{n}{args} | |
\PYG{g+go}{(3, x, y**2)} | |
\end{Verbatim} | |
Mul's \code{args} are sorted, so that the same \code{Mul} will have the same | |
\code{args}. But the sorting is based on some criteria designed to make the | |
sorting unique and efficient that has no mathematical significance. | |
The \code{srepr} form of our \code{expr} is \code{Mul(3, x, Pow(y, 2))}. What if we | |
want to get at the \code{args} of \code{Pow(y, 2)}. Notice that the \code{y**2} is in | |
the third slot of \code{expr.args}, i.e., \code{expr.args{[}2{]}}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr}\PYG{o}{.}\PYG{n}{args}\PYG{p}{[}\PYG{l+m+mi}{2}\PYG{p}{]} | |
\PYG{g+go}{y**2} | |
\end{Verbatim} | |
So to get the \code{args} of this, we call \code{expr.args{[}2{]}.args}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr}\PYG{o}{.}\PYG{n}{args}\PYG{p}{[}\PYG{l+m+mi}{2}\PYG{p}{]}\PYG{o}{.}\PYG{n}{args} | |
\PYG{g+go}{(y, 2)} | |
\end{Verbatim} | |
Now what if we try to go deeper. What are the args of \code{y}. Or \code{2}. | |
Let's see. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{y}\PYG{o}{.}\PYG{n}{args} | |
\PYG{g+go}{()} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Integer}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{.}\PYG{n}{args} | |
\PYG{g+go}{()} | |
\end{Verbatim} | |
They both have empty \code{args}. In SymPy, empty \code{args} signal that we have | |
hit a leaf of the expression tree. | |
So there are two possibilities for a SymPy expression. Either it has empty | |
\code{args}, in which case it is a leaf node in any expression tree, or it has | |
\code{args}, in which case, it is a branch node of any expression tree. When it | |
has \code{args}, it can be completely rebuilt from its \code{func} and its \code{args}. | |
This is expressed in the key invariant. | |
\setbox0\vbox{ | |
\begin{minipage}{0.95\linewidth} | |
\textbf{Key Invariant} | |
\medskip | |
Every well-formed SymPy expression must either have empty \code{args} or | |
satisfy \code{expr == expr.func(*expr.args)}. | |
\end{minipage}} | |
\begin{center}\setlength{\fboxsep}{5pt}\shadowbox{\box0}\end{center} | |
(Recall that in Python if \code{a} is a tuple, then \code{f(*a)} means to call \code{f} | |
with arguments from the elements of \code{a}, e.g., \code{f(*(1, 2, 3))} is the same | |
as \code{f(1, 2, 3)}.) | |
This key invariant allows us to write simple algorithms that walk expression | |
trees, change them, and rebuild them into new expressions. | |
\subsubsection{Walking the Tree} | |
\label{tutorial/manipulation:walking-the-tree} | |
With this knowledge, let's look at how we can recurse through an expression | |
tree. The nested nature of \code{args} is a perfect fit for recursive functions. | |
The base case will be empty \code{args}. Let's write a simple function that goes | |
through an expression and prints all the \code{args} at each level. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{def} \PYG{n+nf}{pre}\PYG{p}{(}\PYG{n}{expr}\PYG{p}{)}\PYG{p}{:} | |
\PYG{g+gp}{... } \PYG{k}{print}\PYG{p}{(}\PYG{n}{expr}\PYG{p}{)} | |
\PYG{g+gp}{... } \PYG{k}{for} \PYG{n}{arg} \PYG{o+ow}{in} \PYG{n}{expr}\PYG{o}{.}\PYG{n}{args}\PYG{p}{:} | |
\PYG{g+gp}{... } \PYG{n}{pre}\PYG{p}{(}\PYG{n}{arg}\PYG{p}{)} | |
\end{Verbatim} | |
See how nice it is that \code{()} signals leaves in the expression tree. We | |
don't even have to write a base case for our recursion; it is handled | |
automatically by the for loop. | |
Let's test our function. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} \PYG{o}{=} \PYG{n}{x}\PYG{o}{*}\PYG{n}{y} \PYG{o}{+} \PYG{l+m+mi}{1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{pre}\PYG{p}{(}\PYG{n}{expr}\PYG{p}{)} | |
\PYG{g+go}{x*y + 1} | |
\PYG{g+go}{1} | |
\PYG{g+go}{x*y} | |
\PYG{g+go}{x} | |
\PYG{g+go}{y} | |
\end{Verbatim} | |
Can you guess why we called our function \code{pre}? We just wrote a pre-order | |
traversal function for our expression tree. See if you can write a | |
post-order traversal function. | |
Such traversals are so common in SymPy that the generator functions | |
\code{preorder\_traversal} and \code{postorder\_traversal} are provided to make such | |
traversals easy. We could have also written our algorithm as | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{for} \PYG{n}{arg} \PYG{o+ow}{in} \PYG{n}{preorder\PYGZus{}traversal}\PYG{p}{(}\PYG{n}{expr}\PYG{p}{)}\PYG{p}{:} | |
\PYG{g+gp}{... } \PYG{k}{print}\PYG{p}{(}\PYG{n}{arg}\PYG{p}{)} | |
\PYG{g+go}{x*y + 1} | |
\PYG{g+go}{1} | |
\PYG{g+go}{x*y} | |
\PYG{g+go}{x} | |
\PYG{g+go}{y} | |
\end{Verbatim} | |
\chapter{Gotchas and Pitfalls} | |
\label{gotchas:gotchas}\label{gotchas:gotchas-and-pitfalls}\label{gotchas::doc} | |
\section{Introduction} | |
\label{gotchas:introduction} | |
SymPy runs under the \href{https://www.python.org/}{Python Programming Language}, so there are some things that may behave | |
differently than they do in other, independent computer algebra systems | |
like Maple or Mathematica. These are some of the gotchas and pitfalls | |
that you may encounter when using SymPy. See also the \href{https://github.com/sympy/sympy/wiki/Faq}{FAQ}, the {\hyperref[tutorial/index:tutorial]{\emph{\DUspan{}{Tutorial}}}} (\autopageref*{tutorial/index:tutorial}), the | |
remainder of the SymPy Docs, and the \href{https://docs.python.org/3/tutorial/}{official Python Tutorial}. | |
If you are already familiar with C or Java, you might also want to look | |
at this \href{http://www.nerdparadise.com/tech/python/4minutecrashcourse/}{4 minute Python tutorial}. | |
Ignore \code{\#doctest: +SKIP} in the examples. That has to do with | |
internal testing of the examples. | |
\section{Equals Signs (=)} | |
\label{gotchas:equals-signs}\label{gotchas:id1} | |
\subsection{Single Equals Sign} | |
\label{gotchas:single-equals-sign} | |
The equals sign (\code{=}) is the assignment operator, not equality. If | |
you want to do \(x = y\), use \code{Eq(x, y)} for equality. | |
Alternatively, all expressions are assumed to equal zero, so you can | |
just subtract one side and use \code{x - y}. | |
The proper use of the equals sign is to assign expressions to variables. | |
For example: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a} \PYG{o}{=} \PYG{n}{x} \PYG{o}{\PYGZhy{}} \PYG{n}{y} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{n}{a}\PYG{p}{)} | |
\PYG{g+go}{x \PYGZhy{} y} | |
\end{Verbatim} | |
\subsection{Double Equals Signs} | |
\label{gotchas:double-equals-signs} | |
Double equals signs (\code{==}) are used to test equality. However, this | |
tests expressions exactly, not symbolically. For example: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{==} \PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1} | |
\PYG{g+go}{False} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{==} \PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
If you want to test for symbolic equality, one way is to subtract one | |
expression from the other and run it through functions like | |
\code{expand()}, \code{simplify()}, and \code{trigsimp()} and see if the | |
equation reduces to 0. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{simplify}\PYG{p}{,} \PYG{n}{cos}\PYG{p}{,} \PYG{n}{sin}\PYG{p}{,} \PYG{n}{expand} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{simplify}\PYG{p}{(}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{\PYGZhy{}} \PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{0} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{eq} \PYG{o}{=} \PYG{n}{sin}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{n}{cos}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{simplify}\PYG{p}{(}\PYG{n}{eq}\PYG{p}{)} | |
\PYG{g+go}{0} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expand}\PYG{p}{(}\PYG{n}{eq}\PYG{p}{,} \PYG{n}{trig}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{0} | |
\end{Verbatim} | |
\begin{notice}{note}{Note:} | |
See also \href{https://github.com/sympy/sympy/wiki/Faq}{Why does SymPy say that two equal expressions are unequal?} in the FAQ. | |
\end{notice} | |
\section{Variables} | |
\label{gotchas:variables} | |
\subsection{Variables Assignment does not Create a Relation Between Expressions} | |
\label{gotchas:variables-assignment-does-not-create-a-relation-between-expressions} | |
When you use \code{=} to do assignment, remember that in Python, as in most | |
programming languages, the variable does not change if you change the | |
value you assigned to it. The equations you are typing use the values | |
present at the time of creation to ``fill in'' values, just like regular | |
Python definitions. They are not altered by changes made afterwards. | |
Consider the following: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{Symbol} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a} \PYG{o}{=} \PYG{n}{Symbol}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{a}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} \PYG{c}{\PYGZsh{} Symbol, {}`a{}`, stored as variable \PYGZdq{}a\PYGZdq{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{b} \PYG{o}{=} \PYG{n}{a} \PYG{o}{+} \PYG{l+m+mi}{1} \PYG{c}{\PYGZsh{} an expression involving {}`a{}` stored as variable \PYGZdq{}b\PYGZdq{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{n}{b}\PYG{p}{)} | |
\PYG{g+go}{a + 1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a} \PYG{o}{=} \PYG{l+m+mi}{4} \PYG{c}{\PYGZsh{} \PYGZdq{}a\PYGZdq{} now points to literal integer 4, not Symbol(\PYGZsq{}a\PYGZsq{})} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{n}{a}\PYG{p}{)} | |
\PYG{g+go}{4} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{n}{b}\PYG{p}{)} \PYG{c}{\PYGZsh{} \PYGZdq{}b\PYGZdq{} is still pointing at the expression involving {}`a{}`} | |
\PYG{g+go}{a + 1} | |
\end{Verbatim} | |
Changing quantity \code{a} does not change \code{b}; you are not working | |
with a set of simultaneous equations. It might be helpful to remember | |
that the string that gets printed when you print a variable referring to | |
a SymPy object is the string that was given to it when it was created; | |
that string does not have to be the same as the variable that you assign | |
it to. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{var} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{r}\PYG{p}{,} \PYG{n}{t}\PYG{p}{,} \PYG{n}{d} \PYG{o}{=} \PYG{n}{var}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{rate time short\PYGZus{}life}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{d} \PYG{o}{=} \PYG{n}{r}\PYG{o}{*}\PYG{n}{t} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{n}{d}\PYG{p}{)} | |
\PYG{g+go}{rate*time} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{r} \PYG{o}{=} \PYG{l+m+mi}{80} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{t} \PYG{o}{=} \PYG{l+m+mi}{2} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{n}{d}\PYG{p}{)} \PYG{c}{\PYGZsh{} We haven\PYGZsq{}t changed d, only r and t} | |
\PYG{g+go}{rate*time} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{d} \PYG{o}{=} \PYG{n}{r}\PYG{o}{*}\PYG{n}{t} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{n}{d}\PYG{p}{)} \PYG{c}{\PYGZsh{} Now d is using the current values of r and t} | |
\PYG{g+go}{160} | |
\end{Verbatim} | |
If you need variables that have dependence on each other, you can define | |
functions. Use the \code{def} operator. Indent the body of the function. | |
See the Python docs for more information on defining functions. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{c}\PYG{p}{,} \PYG{n}{d} \PYG{o}{=} \PYG{n}{var}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{c d}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{n}{c}\PYG{p}{)} | |
\PYG{g+go}{c} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{n}{d}\PYG{p}{)} | |
\PYG{g+go}{d} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{def} \PYG{n+nf}{ctimesd}\PYG{p}{(}\PYG{p}{)}\PYG{p}{:} | |
\PYG{g+gp}{... } \PYG{l+s+sd}{\PYGZdq{}\PYGZdq{}\PYGZdq{}} | |
\PYG{g+gp}{... }\PYG{l+s+sd}{ This function returns whatever c is times whatever d is.} | |
\PYG{g+gp}{... }\PYG{l+s+sd}{ \PYGZdq{}\PYGZdq{}\PYGZdq{}} | |
\PYG{g+gp}{... } \PYG{k}{return} \PYG{n}{c}\PYG{o}{*}\PYG{n}{d} | |
\PYG{g+gp}{...} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{ctimesd}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{c*d} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{c} \PYG{o}{=} \PYG{l+m+mi}{2} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{n}{c}\PYG{p}{)} | |
\PYG{g+go}{2} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{ctimesd}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{2*d} | |
\end{Verbatim} | |
If you define a circular relationship, you will get a | |
\code{RuntimeError}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{def} \PYG{n+nf}{a}\PYG{p}{(}\PYG{p}{)}\PYG{p}{:} | |
\PYG{g+gp}{... } \PYG{k}{return} \PYG{n}{b}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+gp}{...} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{def} \PYG{n+nf}{b}\PYG{p}{(}\PYG{p}{)}\PYG{p}{:} | |
\PYG{g+gp}{... } \PYG{k}{return} \PYG{n}{a}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+gp}{...} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+gt}{Traceback (most recent call last):} | |
\PYG{g+gr}{ File \PYGZdq{}...\PYGZdq{}, line ..., in ...} | |
\PYG{g+gr}{ compileflags, 1) in test.globs} | |
\PYG{g+gr}{ File \PYGZdq{}\PYGZlt{}...\PYGZgt{}\PYGZdq{}, line 1, in \PYGZlt{}module\PYGZgt{}} | |
\PYG{g+gr}{ a()} | |
\PYG{g+gr}{ File \PYGZdq{}\PYGZlt{}...\PYGZgt{}\PYGZdq{}, line 2, in a} | |
\PYG{g+gr}{ return b()} | |
\PYG{g+gr}{ File \PYGZdq{}\PYGZlt{}...\PYGZgt{}\PYGZdq{}, line 2, in b} | |
\PYG{g+gr}{ return a()} | |
\PYG{g+gr}{ File \PYGZdq{}\PYGZlt{}...\PYGZgt{}\PYGZdq{}, line 2, in a} | |
\PYG{g+gr}{ return b()} | |
\PYG{g+gr}{...} | |
\PYG{g+gr}{RuntimeError}: \PYG{n}{maximum recursion depth exceeded} | |
\PYG{g+gt}{Traceback (most recent call last):} | |
\PYG{g+gr}{ File \PYGZdq{}...\PYGZdq{}, line ..., in ...} | |
\PYG{g+gr}{ compileflags, 1) in test.globs} | |
\PYG{g+gr}{ File \PYGZdq{}\PYGZlt{}...\PYGZgt{}\PYGZdq{}, line 1, in \PYGZlt{}module\PYGZgt{}} | |
\PYG{g+gr}{ a()} | |
\PYG{g+gr}{ File \PYGZdq{}\PYGZlt{}...\PYGZgt{}\PYGZdq{}, line 2, in a} | |
\PYG{g+gr}{ return b()} | |
\PYG{g+gr}{ File \PYGZdq{}\PYGZlt{}...\PYGZgt{}\PYGZdq{}, line 2, in b} | |
\PYG{g+gr}{ return a()} | |
\PYG{g+gr}{ File \PYGZdq{}\PYGZlt{}...\PYGZgt{}\PYGZdq{}, line 2, in a} | |
\PYG{g+gr}{ return b()} | |
\PYG{g+gr}{...} | |
\PYG{g+gr}{RuntimeError}: \PYG{n}{maximum recursion depth exceeded} | |
\end{Verbatim} | |
\begin{notice}{note}{Note:} | |
See also \href{https://github.com/sympy/sympy/wiki/Faq}{Why doesn't changing one variable change another that depends on it?} in the FAQ. | |
\end{notice} | |
\subsection{Symbols} | |
\label{gotchas:symbols}\label{gotchas:id2} | |
Symbols are variables, and like all other variables, they need to be | |
assigned before you can use them. For example: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{import} \PYG{n+nn}{sympy} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{z}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{c}{\PYGZsh{} z is not defined yet } | |
\PYG{g+gt}{Traceback (most recent call last):} | |
File \PYG{n+nb}{\PYGZdq{}\PYGZlt{}stdin\PYGZgt{}\PYGZdq{}}, line \PYG{l+m}{1}, in \PYG{n}{\PYGZlt{}module\PYGZgt{}} | |
\PYG{g+gr}{NameError}: \PYG{n}{name \PYGZsq{}z\PYGZsq{} is not defined} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{sympy}\PYG{o}{.}\PYG{n}{var}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{z}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} \PYG{c}{\PYGZsh{} This is the easiest way to define z as a standard symbol} | |
\PYG{g+go}{z} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{z}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} | |
\PYG{g+go}{z**2} | |
\PYG{g+gt}{Traceback (most recent call last):} | |
File \PYG{n+nb}{\PYGZdq{}\PYGZlt{}stdin\PYGZgt{}\PYGZdq{}}, line \PYG{l+m}{1}, in \PYG{n}{\PYGZlt{}module\PYGZgt{}} | |
\PYG{g+gr}{NameError}: \PYG{n}{name \PYGZsq{}z\PYGZsq{} is not defined} | |
\end{Verbatim} | |
If you use \textbf{\texttt{isympy}}, it runs the following commands for you, | |
giving you some default Symbols and Functions. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{\PYGZus{}\PYGZus{}future\PYGZus{}\PYGZus{}} \PYG{k+kn}{import} \PYG{n}{division} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{o}{*} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{z}\PYG{p}{,} \PYG{n}{t} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x y z t}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{k}\PYG{p}{,} \PYG{n}{m}\PYG{p}{,} \PYG{n}{n} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{k m n}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{integer}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{f}\PYG{p}{,} \PYG{n}{g}\PYG{p}{,} \PYG{n}{h} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{f g h}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{cls}\PYG{o}{=}\PYG{n}{Function}\PYG{p}{)} | |
\end{Verbatim} | |
You can also import common symbol names from \code{sympy.abc}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{w} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{w} | |
\PYG{g+go}{w} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{import} \PYG{n+nn}{sympy} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb}{dir}\PYG{p}{(}\PYG{n}{sympy}\PYG{o}{.}\PYG{n}{abc}\PYG{p}{)} | |
\PYG{g+go}{[\PYGZsq{}A\PYGZsq{}, \PYGZsq{}B\PYGZsq{}, \PYGZsq{}C\PYGZsq{}, \PYGZsq{}D\PYGZsq{}, \PYGZsq{}E\PYGZsq{}, \PYGZsq{}F\PYGZsq{}, \PYGZsq{}G\PYGZsq{}, \PYGZsq{}H\PYGZsq{}, \PYGZsq{}I\PYGZsq{}, \PYGZsq{}J\PYGZsq{}, \PYGZsq{}K\PYGZsq{}, \PYGZsq{}L\PYGZsq{}, \PYGZsq{}M\PYGZsq{}, \PYGZsq{}N\PYGZsq{}, \PYGZsq{}O\PYGZsq{},} | |
\PYG{g+go}{\PYGZsq{}P\PYGZsq{}, \PYGZsq{}Q\PYGZsq{}, \PYGZsq{}R\PYGZsq{}, \PYGZsq{}S\PYGZsq{}, \PYGZsq{}Symbol\PYGZsq{}, \PYGZsq{}T\PYGZsq{}, \PYGZsq{}U\PYGZsq{}, \PYGZsq{}V\PYGZsq{}, \PYGZsq{}W\PYGZsq{}, \PYGZsq{}X\PYGZsq{}, \PYGZsq{}Y\PYGZsq{}, \PYGZsq{}Z\PYGZsq{},} | |
\PYG{g+go}{\PYGZsq{}\PYGZus{}\PYGZus{}builtins\PYGZus{}\PYGZus{}\PYGZsq{}, \PYGZsq{}\PYGZus{}\PYGZus{}doc\PYGZus{}\PYGZus{}\PYGZsq{}, \PYGZsq{}\PYGZus{}\PYGZus{}file\PYGZus{}\PYGZus{}\PYGZsq{}, \PYGZsq{}\PYGZus{}\PYGZus{}name\PYGZus{}\PYGZus{}\PYGZsq{}, \PYGZsq{}\PYGZus{}\PYGZus{}package\PYGZus{}\PYGZus{}\PYGZsq{}, \PYGZsq{}\PYGZus{}greek\PYGZsq{},} | |
\PYG{g+go}{\PYGZsq{}\PYGZus{}latin\PYGZsq{}, \PYGZsq{}a\PYGZsq{}, \PYGZsq{}alpha\PYGZsq{}, \PYGZsq{}b\PYGZsq{}, \PYGZsq{}beta\PYGZsq{}, \PYGZsq{}c\PYGZsq{}, \PYGZsq{}chi\PYGZsq{}, \PYGZsq{}d\PYGZsq{}, \PYGZsq{}delta\PYGZsq{}, \PYGZsq{}e\PYGZsq{},} | |
\PYG{g+go}{\PYGZsq{}epsilon\PYGZsq{}, \PYGZsq{}eta\PYGZsq{}, \PYGZsq{}f\PYGZsq{}, \PYGZsq{}g\PYGZsq{}, \PYGZsq{}gamma\PYGZsq{}, \PYGZsq{}h\PYGZsq{}, \PYGZsq{}i\PYGZsq{}, \PYGZsq{}iota\PYGZsq{}, \PYGZsq{}j\PYGZsq{}, \PYGZsq{}k\PYGZsq{}, \PYGZsq{}kappa\PYGZsq{},} | |
\PYG{g+go}{\PYGZsq{}l\PYGZsq{}, \PYGZsq{}m\PYGZsq{}, \PYGZsq{}mu\PYGZsq{}, \PYGZsq{}n\PYGZsq{}, \PYGZsq{}nu\PYGZsq{}, \PYGZsq{}o\PYGZsq{}, \PYGZsq{}omega\PYGZsq{}, \PYGZsq{}omicron\PYGZsq{}, \PYGZsq{}p\PYGZsq{}, \PYGZsq{}phi\PYGZsq{}, \PYGZsq{}pi\PYGZsq{},} | |
\PYG{g+go}{\PYGZsq{}psi\PYGZsq{}, \PYGZsq{}q\PYGZsq{}, \PYGZsq{}r\PYGZsq{}, \PYGZsq{}rho\PYGZsq{}, \PYGZsq{}s\PYGZsq{}, \PYGZsq{}sigma\PYGZsq{}, \PYGZsq{}t\PYGZsq{}, \PYGZsq{}tau\PYGZsq{}, \PYGZsq{}theta\PYGZsq{}, \PYGZsq{}u\PYGZsq{}, \PYGZsq{}upsilon\PYGZsq{},} | |
\PYG{g+go}{\PYGZsq{}v\PYGZsq{}, \PYGZsq{}w\PYGZsq{}, \PYGZsq{}x\PYGZsq{}, \PYGZsq{}xi\PYGZsq{}, \PYGZsq{}y\PYGZsq{}, \PYGZsq{}z\PYGZsq{}, \PYGZsq{}zeta\PYGZsq{}]} | |
\end{Verbatim} | |
If you want control over the assumptions of the variables, use | |
\code{Symbol()} and \code{symbols()}. See {\hyperref[gotchas:keyword-arguments]{\emph{\DUspan{}{Keyword | |
Arguments}}}} (\autopageref*{gotchas:keyword-arguments}) below. | |
Lastly, it is recommended that you not use \code{I}, \code{E}, \code{S}, | |
\code{N}, \code{C}, \code{O}, or \code{Q} for variable or symbol names, as those | |
are used for the imaginary unit (\(i\)), the base of the natural | |
logarithm (\(e\)), the \code{sympify()} function (see {\hyperref[gotchas:symbolic-expressions]{\emph{\DUspan{}{Symbolic | |
Expressions}}}} (\autopageref*{gotchas:symbolic-expressions}) below), numeric evaluation (\code{N()} | |
is equivalent to {\hyperref[modules/evalf:evalf-label]{\emph{\DUspan{}{evalf()}}}} (\autopageref*{modules/evalf:evalf-label}) ), the class registry (for | |
things like \code{C.cos()}, to prevent cyclic imports in some code), | |
the \href{http://en.wikipedia.org/wiki/Big\_O\_notation}{big O} order symbol | |
(as in \(O(n\log{n})\)), and the assumptions object that holds a list of | |
supported ask keys (such as \code{Q.real}), respectively. You can use the | |
mnemonic \code{QCOSINE} to remember what Symbols are defined by default in SymPy. | |
Or better yet, always use lowercase letters for Symbol names. Python will | |
not prevent you from overriding default SymPy names or functions, so be | |
careful. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{cos}\PYG{p}{(}\PYG{n}{pi}\PYG{p}{)} \PYG{c}{\PYGZsh{} cos and pi are a built\PYGZhy{}in sympy names.} | |
\PYG{g+go}{\PYGZhy{}1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{pi} \PYG{o}{=} \PYG{l+m+mi}{3} \PYG{c}{\PYGZsh{} Notice that there is no warning for overriding pi.} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{cos}\PYG{p}{(}\PYG{n}{pi}\PYG{p}{)} | |
\PYG{g+go}{cos(3)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{def} \PYG{n+nf}{cos}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{:} \PYG{c}{\PYGZsh{} No warning for overriding built\PYGZhy{}in functions either.} | |
\PYG{g+gp}{... } \PYG{k}{return} \PYG{l+m+mi}{5}\PYG{o}{*}\PYG{n}{x} | |
\PYG{g+gp}{...} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{cos}\PYG{p}{(}\PYG{n}{pi}\PYG{p}{)} | |
\PYG{g+go}{15} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{cos} \PYG{c}{\PYGZsh{} reimport to restore normal behavior} | |
\end{Verbatim} | |
To get a full list of all default names in SymPy do: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{import} \PYG{n+nn}{sympy} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb}{dir}\PYG{p}{(}\PYG{n}{sympy}\PYG{p}{)} | |
\PYG{g+go}{\PYGZsh{} A big list of all default sympy names and functions follows.} | |
\PYG{g+go}{\PYGZsh{} Ignore everything that starts and ends with \PYGZus{}\PYGZus{}.} | |
\end{Verbatim} | |
If you have \href{http://ipython.org/}{IPython} installed and | |
use \textbf{\texttt{isympy}}, you can also press the TAB key to get a list of | |
all built-in names and to autocomplete. Also, see \href{http://kogs-www.informatik.uni-hamburg.de/~meine/python\_tricks}{this page} for a | |
trick for getting tab completion in the regular Python console. | |
\begin{notice}{note}{Note:} | |
See also \href{https://github.com/sympy/sympy/wiki/Faq}{What is the best way to create symbols?} in the FAQ. | |
\end{notice} | |
\section{Symbolic Expressions} | |
\label{gotchas:symbolic-expressions}\label{gotchas:id3} | |
\subsection{Python numbers vs. SymPy Numbers} | |
\label{gotchas:python-vs-sympy-numbers}\label{gotchas:python-numbers-vs-sympy-numbers} | |
SymPy uses its own classes for integers, rational numbers, and floating | |
point numbers instead of the default Python \code{int} and \code{float} | |
types because it allows for more control. But you have to be careful. | |
If you type an expression that just has numbers in it, it will default | |
to a Python expression. Use the \code{sympify()} function, or just | |
\code{S()}, to ensure that something is a SymPy expression. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{l+m+mf}{6.2} \PYG{c}{\PYGZsh{} Python float. Notice the floating point accuracy problems.} | |
\PYG{g+go}{6.2000000000000002} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb}{type}\PYG{p}{(}\PYG{l+m+mf}{6.2}\PYG{p}{)} \PYG{c}{\PYGZsh{} \PYGZlt{}type \PYGZsq{}float\PYGZsq{}\PYGZgt{} in Python 2.x, \PYGZlt{}class \PYGZsq{}float\PYGZsq{}\PYGZgt{} in Py3k} | |
\PYG{g+go}{\PYGZlt{}... \PYGZsq{}float\PYGZsq{}\PYGZgt{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{S}\PYG{p}{(}\PYG{l+m+mf}{6.2}\PYG{p}{)} \PYG{c}{\PYGZsh{} SymPy Float has no such problems because of arbitrary precision.} | |
\PYG{g+go}{6.20000000000000} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb}{type}\PYG{p}{(}\PYG{n}{S}\PYG{p}{(}\PYG{l+m+mf}{6.2}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{\PYGZlt{}class \PYGZsq{}sympy.core.numbers.Float\PYGZsq{}\PYGZgt{}} | |
\end{Verbatim} | |
If you include numbers in a SymPy expression, they will be sympified | |
automatically, but there is one gotcha you should be aware of. If you | |
do \code{\textless{}number\textgreater{}/\textless{}number\textgreater{}} inside of a SymPy expression, Python will | |
evaluate the two numbers before SymPy has a chance to get | |
to them. The solution is to \code{sympify()} one of the numbers, or use | |
\code{Rational}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{o}{/}\PYG{l+m+mi}{2}\PYG{p}{)} \PYG{c}{\PYGZsh{} evaluates to x**0 or x**0.5} | |
\PYG{g+go}{x**0.5} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{p}{(}\PYG{n}{S}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{/}\PYG{l+m+mi}{2}\PYG{p}{)} \PYG{c}{\PYGZsh{} sympyify one of the ints} | |
\PYG{g+go}{sqrt(x)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{n}{Rational}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{)} \PYG{c}{\PYGZsh{} use the Rational class} | |
\PYG{g+go}{sqrt(x)} | |
\end{Verbatim} | |
With a power of \code{1/2} you can also use \code{sqrt} shorthand: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{sqrt}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} \PYG{o}{==} \PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{n}{Rational}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
If the two integers are not directly separated by a division sign then | |
you don't have to worry about this problem: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{o}{/}\PYG{l+m+mi}{3}\PYG{p}{)} | |
\PYG{g+go}{x**(2*x/3)} | |
\end{Verbatim} | |
\begin{notice}{note}{Note:} | |
A common mistake is copying an expression that is printed and | |
reusing it. If the expression has a \code{Rational} (i.e., | |
\code{\textless{}number\textgreater{}/\textless{}number\textgreater{}}) in it, you will not get the same result, | |
obtaining the Python result for the division rather than a SymPy | |
Rational. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x} \PYG{o}{=} \PYG{n}{Symbol}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{n}{solve}\PYG{p}{(}\PYG{l+m+mi}{7}\PYG{o}{*}\PYG{n}{x} \PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{22}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{[22/7]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{l+m+mi}{22}\PYG{o}{/}\PYG{l+m+mi}{7} \PYG{c}{\PYGZsh{} If we just copy and paste we get int 3 or a float} | |
\PYG{g+go}{3.142857142857143} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{c}{\PYGZsh{} One solution is to just assign the expression to a variable} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{c}{\PYGZsh{} if we need to use it again.} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a} \PYG{o}{=} \PYG{n}{solve}\PYG{p}{(}\PYG{l+m+mi}{7}\PYG{o}{*}\PYG{n}{x} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{22}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a} | |
\PYG{g+go}{[22/7]} | |
\end{Verbatim} | |
The other solution is to put quotes around the expression | |
and run it through S() (i.e., sympify it): | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{S}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s}{22/7}\PYG{l+s}{\PYGZdq{}}\PYG{p}{)} | |
\PYG{g+go}{22/7} | |
\end{Verbatim} | |
\end{notice} | |
Also, if you do not use \textbf{\texttt{isympy}}, you could use \code{from | |
\_\_future\_\_ import division} to prevent the \code{/} sign from performing | |
\href{http://en.wikipedia.org/wiki/Integer\_division}{integer division}. | |
\begin{quote} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{\PYGZus{}\PYGZus{}future\PYGZus{}\PYGZus{}} \PYG{k+kn}{import} \PYG{n}{division} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{l+m+mi}{1}\PYG{o}{/}\PYG{l+m+mi}{2} \PYG{c}{\PYGZsh{} With division imported it evaluates to a python float} | |
\PYG{g+go}{0.5} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{l+m+mi}{1}\PYG{o}{/}\PYG{o}{/}\PYG{l+m+mi}{2} \PYG{c}{\PYGZsh{} You can still achieve integer division with //} | |
\PYG{g+go}{0} | |
\end{Verbatim} | |
But be careful: you will now receive floats where you might have desired | |
a Rational: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{o}{/}\PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+go}{x**0.5} | |
\end{Verbatim} | |
\end{quote} | |
\code{Rational} only works for number/number and is only meant for | |
rational numbers. If you want a fraction with symbols or expressions in | |
it, just use \code{/}. If you do number/expression or expression/number, | |
then the number will automatically be converted into a SymPy Number. | |
You only need to be careful with number/number. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Rational}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)} | |
\PYG{g+gt}{Traceback (most recent call last):} | |
\PYG{c}{...} | |
\PYG{g+gr}{TypeError}: \PYG{n}{invalid input: x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{l+m+mi}{2}\PYG{o}{/}\PYG{n}{x} | |
\PYG{g+go}{2/x} | |
\PYG{g+gt}{Traceback (most recent call last):} | |
\PYG{c}{...} | |
\PYG{g+gr}{TypeError}: \PYG{n}{invalid input: x} | |
\end{Verbatim} | |
\subsection{Evaluating Expressions with Floats and Rationals} | |
\label{gotchas:evaluating-expressions-with-floats-and-rationals} | |
SymPy keeps track of the precision of \code{Float} objects. The default precision is | |
15 digits. When an expression involving a \code{Float} is evaluated, the result | |
will be expressed to 15 digits of precision but those digits (depending | |
on the numbers involved with the calculation) may not all be significant. | |
The first issue to keep in mind is how the \code{Float} is created: it is created | |
with a value and a precision. The precision indicates how precise of a value | |
to use when that \code{Float} (or an expression it appears in) is evaluated. | |
The values can be given as strings, integers, floats, or rationals. | |
\begin{quote} | |
\begin{itemize} | |
\item {} | |
strings and integers are interpreted as exact | |
\end{itemize} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Float}\PYG{p}{(}\PYG{l+m+mi}{100}\PYG{p}{)} | |
\PYG{g+go}{100.000000000000} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Float}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{100}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{l+m+mi}{5}\PYG{p}{)} | |
\PYG{g+go}{100.00} | |
\end{Verbatim} | |
\begin{itemize} | |
\item {} | |
to have the precision match the number of digits, the null string | |
can be used for the precision | |
\end{itemize} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Float}\PYG{p}{(}\PYG{l+m+mi}{100}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+go}{100.} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Float}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{12.34}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+go}{12.3400000000000} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Float}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{12.34}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+go}{12.34} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{s}\PYG{p}{,} \PYG{n}{r} \PYG{o}{=} \PYG{p}{[}\PYG{n}{Float}\PYG{p}{(}\PYG{n}{j}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{)} \PYG{k}{for} \PYG{n}{j} \PYG{o+ow}{in} \PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{0.25}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{Rational}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{7}\PYG{p}{)}\PYG{p}{)}\PYG{p}{]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{for} \PYG{n}{f} \PYG{o+ow}{in} \PYG{p}{[}\PYG{n}{s}\PYG{p}{,} \PYG{n}{r}\PYG{p}{]}\PYG{p}{:} | |
\PYG{g+gp}{... } \PYG{k}{print}\PYG{p}{(}\PYG{n}{f}\PYG{p}{)} | |
\PYG{g+go}{0.250} | |
\PYG{g+go}{0.143} | |
\end{Verbatim} | |
\end{quote} | |
Next, notice that each of those values looks correct to 3 digits. But if we try | |
to evaluate them to 20 digits, a difference will become apparent: | |
\begin{quote} | |
The 0.25 (with precision of 3) represents a number that has a non-repeating | |
binary decimal; 1/7 is repeating in binary and decimal -- it cannot be | |
represented accurately too far past those first 3 digits (the correct | |
decimal is a repeating 142857): | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{s}\PYG{o}{.}\PYG{n}{n}\PYG{p}{(}\PYG{l+m+mi}{20}\PYG{p}{)} | |
\PYG{g+go}{0.25000000000000000000} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{r}\PYG{o}{.}\PYG{n}{n}\PYG{p}{(}\PYG{l+m+mi}{20}\PYG{p}{)} | |
\PYG{g+go}{0.14285278320312500000} | |
\end{Verbatim} | |
It is important to realize that although a Float is being displayed in | |
decimal at aritrary precision, it is actually stored in binary. Once the | |
Float is created, its binary information is set at the given precision. | |
The accuracy of that value cannot be subsequently changed; so 1/7, at a | |
precision of 3 digits, can be padded with binary zeros, but these will | |
not make it a more accurate value of 1/7. | |
\end{quote} | |
If inexact, low-precision numbers are involved in a calculation with | |
with higher precision values, the evalf engine will increase the precision | |
of the low precision values and inexact results will be obtained. This is | |
feature of calculations with limited precision: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Float}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{0.1}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{l+m+mi}{10}\PYG{p}{)} \PYG{o}{+} \PYG{n}{Float}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{0.1}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{)} | |
\PYG{g+go}{0.2000061035} | |
\end{Verbatim} | |
Although the \code{evalf} engine tried to maintain 10 digits of precision (since | |
that was the highest precision represented) the 3-digit precision used | |
limits the accuracy to about 4 digits -- not all the digits you see | |
are significant. evalf doesn't try to keep track of the number of | |
significant digits. | |
That very simple expression involving the addition of two numbers with | |
different precisions will hopefully be instructive in helping you | |
understand why more complicated expressions (like trig expressions that | |
may not be simplified) will not evaluate to an exact zero even though, | |
with the right simplification, they should be zero. Consider this | |
unsimplified trig identity, multiplied by a big number: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{big} \PYG{o}{=} \PYG{l+m+mi}{12345678901234567890} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{big\PYGZus{}trig\PYGZus{}identity} \PYG{o}{=} \PYG{n}{big}\PYG{o}{*}\PYG{n}{cos}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{n}{big}\PYG{o}{*}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{\PYGZhy{}} \PYG{n}{big}\PYG{o}{*}\PYG{l+m+mi}{1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb}{abs}\PYG{p}{(}\PYG{n}{big\PYGZus{}trig\PYGZus{}identity}\PYG{o}{.}\PYG{n}{subs}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{o}{.}\PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{.}\PYG{n}{n}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{)} \PYG{o}{\PYGZgt{}} \PYG{l+m+mi}{1000} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
When the \(\cos\) and \(\sin\) terms were evaluated to 15 digits of precision and | |
multiplied by the big number, they gave a large number that was only | |
precise to 15 digits (approximately) and when the 20 digit big number | |
was subtracted the result was not zero. | |
There are three things that will help you obtain more precise numerical | |
values for expressions: | |
\begin{quote} | |
1) Pass the desired substitutions with the call to evaluate. By doing | |
the subs first, the \code{Float} values can not be updated as necessary. By | |
passing the desired substitutions with the call to evalf the ability | |
to re-evaluate as necessary is gained and the results are impressively | |
better: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{big\PYGZus{}trig\PYGZus{}identity}\PYG{o}{.}\PYG{n}{n}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{p}{\PYGZob{}}\PYG{n}{x}\PYG{p}{:} \PYG{l+m+mf}{0.1}\PYG{p}{\PYGZcb{}}\PYG{p}{)} | |
\PYG{g+go}{\PYGZhy{}0.e\PYGZhy{}91} | |
\end{Verbatim} | |
2) Use Rationals, not Floats. During the evaluation process, the | |
Rational can be computed to an arbitrary precision while the Float, | |
once created -- at a default of 15 digits -- cannot. Compare the | |
value of \code{-1.4e+3} above with the nearly zero value obtained when | |
replacing x with a Rational representing 1/10 -- before the call | |
to evaluate: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{big\PYGZus{}trig\PYGZus{}identity}\PYG{o}{.}\PYG{n}{subs}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{S}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{1/10}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{n}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+go}{0.e\PYGZhy{}91} | |
\end{Verbatim} | |
3) Try to simplify the expression. In this case, SymPy will recognize | |
the trig identity and simplify it to zero so you don't even have to | |
evaluate it numerically: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{big\PYGZus{}trig\PYGZus{}identity}\PYG{o}{.}\PYG{n}{simplify}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{0} | |
\end{Verbatim} | |
\end{quote} | |
\subsection{Immutability of Expressions} | |
\label{gotchas:immutability-of-expressions}\label{gotchas:id4} | |
Expressions in SymPy are immutable, and cannot be modified by an in-place | |
operation. This means that a function will always return an object, and the | |
original expression will not be modified. The following example snippet | |
demonstrates how this works: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{k}{def} \PYG{n+nf}{main}\PYG{p}{(}\PYG{p}{)}\PYG{p}{:} | |
\PYG{n}{var}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x y a b}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{n}{expr} \PYG{o}{=} \PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{4}\PYG{o}{*}\PYG{n}{y} | |
\PYG{k}{print}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{original =}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{expr}\PYG{p}{)} | |
\PYG{n}{expr\PYGZus{}modified} \PYG{o}{=} \PYG{n}{expr}\PYG{o}{.}\PYG{n}{subs}\PYG{p}{(}\PYG{p}{\PYGZob{}}\PYG{n}{x}\PYG{p}{:} \PYG{n}{a}\PYG{p}{,} \PYG{n}{y}\PYG{p}{:} \PYG{n}{b}\PYG{p}{\PYGZcb{}}\PYG{p}{)} | |
\PYG{k}{print}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{modified =}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{expr\PYGZus{}modified}\PYG{p}{)} | |
\PYG{k}{if} \PYG{n}{\PYGZus{}\PYGZus{}name\PYGZus{}\PYGZus{}} \PYG{o}{==} \PYG{l+s}{\PYGZdq{}}\PYG{l+s}{\PYGZus{}\PYGZus{}main\PYGZus{}\PYGZus{}}\PYG{l+s}{\PYGZdq{}}\PYG{p}{:} | |
\PYG{n}{main}\PYG{p}{(}\PYG{p}{)} | |
\end{Verbatim} | |
The output shows that the \code{subs()} function has replaced variable | |
\code{x} with variable \code{a}, and variable \code{y} with variable \code{b}: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{n}{original} \PYG{o}{=} \PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{4}\PYG{o}{*}\PYG{n}{y} | |
\PYG{n}{modified} \PYG{o}{=} \PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{a} \PYG{o}{+} \PYG{l+m+mi}{4}\PYG{o}{*}\PYG{n}{b} | |
\end{Verbatim} | |
The \code{subs()} function does not modify the original expression \code{expr}. | |
Rather, a modified copy of the expression is returned. This returned object | |
is stored in the variable \code{expr\_modified}. Note that unlike C/C++ and | |
other high-level languages, Python does not require you to declare a variable | |
before it is used. | |
\subsection{Mathematical Operators} | |
\label{gotchas:mathematical-operators} | |
SymPy uses the same default operators as Python. Most of these, like | |
\code{*/+-}, are standard. Aside from integer division discussed in | |
{\hyperref[gotchas:python-vs-sympy-numbers]{\emph{\DUspan{}{Python numbers vs. SymPy Numbers}}}} (\autopageref*{gotchas:python-vs-sympy-numbers}) above, | |
you should also be aware that implied multiplication is not allowed. You | |
need to use \code{*} whenever you wish to multiply something. Also, to | |
raise something to a power, use \code{**}, not \code{\textasciicircum{}} as many computer | |
algebra systems use. Parentheses \code{()} change operator precedence as | |
you would normally expect. | |
In \textbf{\texttt{isympy}}, with the \textbf{\texttt{ipython}} shell: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{l+m+mi}{2}\PYG{n}{x} | |
\PYG{g+gt}{Traceback (most recent call last):} | |
\PYG{c}{...} | |
\PYG{g+gr}{SyntaxError}: \PYG{n}{invalid syntax} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x} | |
\PYG{g+go}{2*x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{\PYGZca{}}\PYG{l+m+mi}{2} \PYG{c}{\PYGZsh{} This is not power. Use ** instead.} | |
\PYG{g+gt}{Traceback (most recent call last):} | |
\PYG{c}{...} | |
\PYG{g+gr}{TypeError}: \PYG{n}{unsupported operand type(s) for \PYGZca{}: \PYGZsq{}Add\PYGZsq{} and \PYGZsq{}int\PYGZsq{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} | |
\PYG{g+go}{(x + 1)**2} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{pprint}\PYG{p}{(}\PYG{l+m+mi}{3} \PYG{o}{\PYGZhy{}} \PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)}\PYG{o}{/}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{ 2*x} | |
\PYG{g+go}{ x} | |
\PYG{g+go}{\PYGZhy{} \PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{} + 3} | |
\PYG{g+go}{ x + 1} | |
\PYG{g+gt}{Traceback (most recent call last):} | |
\PYG{c}{...} | |
\PYG{g+gr}{TypeError}: \PYG{n}{unsupported operand type(s) for \PYGZca{}: \PYGZsq{}Add\PYGZsq{} and \PYGZsq{}int\PYGZsq{}} | |
\end{Verbatim} | |
\subsection{Inverse Trig Functions} | |
\label{gotchas:inverse-trig-functions} | |
SymPy uses different names for some functions than most computer algebra | |
systems. In particular, the inverse trig functions use the python names | |
of \code{asin()}, \code{acos()} and so on instead of the usual \code{arcsin} | |
and \code{arccos}. Use the methods described in {\hyperref[gotchas:symbols]{\emph{\DUspan{}{Symbols}}}} (\autopageref*{gotchas:symbols}) | |
above to see the names of all SymPy functions. | |
\section{Special Symbols} | |
\label{gotchas:special-symbols} | |
The symbols \code{{[}{]}}, \code{\{\}}, \code{=}, and \code{()} have special meanings in | |
Python, and thus in SymPy. See the Python docs linked to above for | |
additional information. | |
\subsection{Lists} | |
\label{gotchas:id5}\label{gotchas:lists} | |
Square brackets \code{{[}{]}} denote a list. A list is a container that holds | |
any number of different objects. A list can contain anything, including | |
items of different types. Lists are mutable, which means that you can | |
change the elements of a list after it has been created. You access the | |
items of a list also using square brackets, placing them after the list | |
or list variable. Items are numbered using the space before the item. | |
\begin{notice}{note}{Note:} | |
List indexes begin at 0. | |
\end{notice} | |
Example: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a} \PYG{o}{=} \PYG{p}{[}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{]} \PYG{c}{\PYGZsh{} A simple list of two items} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a} | |
\PYG{g+go}{[x, 1]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{]} \PYG{c}{\PYGZsh{} This is the first item} | |
\PYG{g+go}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{]} \PYG{o}{=} \PYG{l+m+mi}{2} \PYG{c}{\PYGZsh{} You can change values of lists after they have been created} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{n}{a}\PYG{p}{)} | |
\PYG{g+go}{[2, 1]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{n}{solve}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)}\PYG{p}{)} \PYG{c}{\PYGZsh{} Some functions return lists} | |
\PYG{g+go}{[\PYGZhy{}1 + sqrt(2), \PYGZhy{}sqrt(2) \PYGZhy{} 1]} | |
\end{Verbatim} | |
\begin{notice}{note}{Note:} | |
See the Python docs for more information on lists and the square | |
bracket notation for accessing elements of a list. | |
\end{notice} | |
\subsection{Dictionaries} | |
\label{gotchas:dictionaries} | |
Curly brackets \code{\{\}} denote a dictionary, or a dict for short. A | |
dictionary is an unordered list of non-duplicate keys and values. The | |
syntax is \code{\{key: value\}}. You can access values of keys using square | |
bracket notation. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{d} \PYG{o}{=} \PYG{p}{\PYGZob{}}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{a}\PYG{l+s}{\PYGZsq{}}\PYG{p}{:} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{b}\PYG{l+s}{\PYGZsq{}}\PYG{p}{:} \PYG{l+m+mi}{2}\PYG{p}{\PYGZcb{}} \PYG{c}{\PYGZsh{} A dictionary.} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{d} | |
\PYG{g+go}{\PYGZob{}\PYGZsq{}a\PYGZsq{}: 1, \PYGZsq{}b\PYGZsq{}: 2\PYGZcb{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{d}\PYG{p}{[}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{a}\PYG{l+s}{\PYGZsq{}}\PYG{p}{]} \PYG{c}{\PYGZsh{} How to access items in a dict} | |
\PYG{g+go}{1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{roots}\PYG{p}{(}\PYG{p}{(}\PYG{n}{x} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{p}{(}\PYG{n}{x} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)} \PYG{c}{\PYGZsh{} Some functions return dicts} | |
\PYG{g+go}{\PYGZob{}1: 2, 2: 1\PYGZcb{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{c}{\PYGZsh{} Some SymPy functions return dictionaries. For example,} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{c}{\PYGZsh{} roots returns a dictionary of root:multiplicity items.} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{roots}\PYG{p}{(}\PYG{p}{(}\PYG{n}{x} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{5}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{3}\PYG{p}{)}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{\PYGZob{}\PYGZhy{}3: 1, 5: 2\PYGZcb{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{c}{\PYGZsh{} This means that the root \PYGZhy{}3 occurs once and the root 5 occurs twice.} | |
\end{Verbatim} | |
\begin{notice}{note}{Note:} | |
See the Python docs for more information on dictionaries. | |
\end{notice} | |
\subsection{Tuples} | |
\label{gotchas:tuples} | |
Parentheses \code{()}, aside from changing operator precedence and their | |
use in function calls, (like \code{cos(x)}), are also used for tuples. A | |
\code{tuple} is identical to a {\hyperref[gotchas:lists]{\emph{\DUspan{}{list}}}} (\autopageref*{gotchas:lists}), except that it is not | |
mutable. That means that you can not change their values after they | |
have been created. In general, you will not need tuples in SymPy, but | |
sometimes it can be more convenient to type parentheses instead of | |
square brackets. | |
\begin{quote} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{t} \PYG{o}{=} \PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)} \PYG{c}{\PYGZsh{} Tuples are like lists} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{t} | |
\PYG{g+go}{(1, 2, x)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{t}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{]} | |
\PYG{g+go}{1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{t}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{]} \PYG{o}{=} \PYG{l+m+mi}{4} \PYG{c}{\PYGZsh{} Except you can not change them after they have been created} | |
\PYG{g+gt}{Traceback (most recent call last):} | |
File \PYG{n+nb}{\PYGZdq{}\PYGZlt{}console\PYGZgt{}\PYGZdq{}}, line \PYG{l+m}{1}, in \PYG{n}{\PYGZlt{}module\PYGZgt{}} | |
\PYG{g+gr}{TypeError}: \PYG{n}{\PYGZsq{}tuple\PYGZsq{} object does not support item assignment} | |
\PYG{g+gt}{Traceback (most recent call last):} | |
File \PYG{n+nb}{\PYGZdq{}\PYGZlt{}console\PYGZgt{}\PYGZdq{}}, line \PYG{l+m}{1}, in \PYG{n}{\PYGZlt{}module\PYGZgt{}} | |
\PYG{g+gr}{TypeError}: \PYG{n}{\PYGZsq{}tuple\PYGZsq{} object does not support item assignment} | |
\end{Verbatim} | |
Single element tuples, unlike lists, must have a comma in them: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{x}\PYG{p}{,}\PYG{p}{)} | |
\PYG{g+go}{(x,)} | |
\end{Verbatim} | |
Without the comma, a single expression without a comma is not a tuple: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{x} | |
\end{Verbatim} | |
integrate takes a sequence as the second argument if you want to integrate | |
with limits (and a tuple or list will work): | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{integrate}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{1/3} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{integrate}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{p}{[}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+go}{1/3} | |
\end{Verbatim} | |
\end{quote} | |
\begin{notice}{note}{Note:} | |
See the Python docs for more information on tuples. | |
\end{notice} | |
\subsection{Keyword Arguments} | |
\label{gotchas:id6}\label{gotchas:keyword-arguments} | |
Aside from the usage described {\hyperref[gotchas:equals-signs]{\emph{\DUspan{}{above}}}} (\autopageref*{gotchas:equals-signs}), equals signs | |
(\code{=}) are also used to give named arguments to functions. Any | |
function that has \code{key=value} in its parameters list (see below on how | |
to find this out), then \code{key} is set to \code{value} by default. You can | |
change the value of the key by supplying your own value using the equals | |
sign in the function call. Also, functions that have \code{**} followed by | |
a name in the parameters list (usually \code{**kwargs} or | |
\code{**assumptions}) allow you to add any number of \code{key=value} pairs | |
that you want, and they will all be evaluated according to the function. | |
\begin{quote} | |
\code{sqrt(x**2)} doesn't auto simplify to x because x is assumed to be | |
complex by default, and, for example, \code{sqrt((-1)**2) == sqrt(1) == 1 != -1}: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{sqrt}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+go}{sqrt(x**2)} | |
\end{Verbatim} | |
Giving assumptions to Symbols is an example of using the keyword argument: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x} \PYG{o}{=} \PYG{n}{Symbol}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{positive}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\end{Verbatim} | |
The square root will now simplify since it knows that \code{x \textgreater{}= 0}: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{sqrt}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+go}{x} | |
\end{Verbatim} | |
powsimp has a default argument of \code{combine='all'}: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{pprint}\PYG{p}{(}\PYG{n}{powsimp}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{n}{n}\PYG{o}{*}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{n}{m}\PYG{o}{*}\PYG{n}{y}\PYG{o}{*}\PYG{o}{*}\PYG{n}{n}\PYG{o}{*}\PYG{n}{y}\PYG{o}{*}\PYG{o}{*}\PYG{n}{m}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{ m + n} | |
\PYG{g+go}{(x*y)} | |
\end{Verbatim} | |
Setting combine to the default value is the same as not setting it. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{pprint}\PYG{p}{(}\PYG{n}{powsimp}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{n}{n}\PYG{o}{*}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{n}{m}\PYG{o}{*}\PYG{n}{y}\PYG{o}{*}\PYG{o}{*}\PYG{n}{n}\PYG{o}{*}\PYG{n}{y}\PYG{o}{*}\PYG{o}{*}\PYG{n}{m}\PYG{p}{,} \PYG{n}{combine}\PYG{o}{=}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{all}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{ m + n} | |
\PYG{g+go}{(x*y)} | |
\end{Verbatim} | |
The non-default options are \code{'exp'}, which combines exponents... | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{pprint}\PYG{p}{(}\PYG{n}{powsimp}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{n}{n}\PYG{o}{*}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{n}{m}\PYG{o}{*}\PYG{n}{y}\PYG{o}{*}\PYG{o}{*}\PYG{n}{n}\PYG{o}{*}\PYG{n}{y}\PYG{o}{*}\PYG{o}{*}\PYG{n}{m}\PYG{p}{,} \PYG{n}{combine}\PYG{o}{=}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{exp}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{ m + n m + n} | |
\PYG{g+go}{x *y} | |
\end{Verbatim} | |
...and `base', which combines bases. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{pprint}\PYG{p}{(}\PYG{n}{powsimp}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{n}{n}\PYG{o}{*}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{n}{m}\PYG{o}{*}\PYG{n}{y}\PYG{o}{*}\PYG{o}{*}\PYG{n}{n}\PYG{o}{*}\PYG{n}{y}\PYG{o}{*}\PYG{o}{*}\PYG{n}{m}\PYG{p}{,} \PYG{n}{combine}\PYG{o}{=}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{base}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{ m n} | |
\PYG{g+go}{(x*y) *(x*y)} | |
\end{Verbatim} | |
\end{quote} | |
\begin{notice}{note}{Note:} | |
See the Python docs for more information on function parameters. | |
\end{notice} | |
\section{Getting help from within SymPy} | |
\label{gotchas:getting-help-from-within-sympy} | |
\subsection{help()} | |
\label{gotchas:help} | |
Although all docs are available at \href{http://docs.sympy.org/}{docs.sympy.org} or on the | |
\href{http://wiki.sympy.org/}{SymPy Wiki}, you can also get info on functions from within the | |
Python interpreter that runs SymPy. The easiest way to do this is to do | |
\code{help(function)}, or \code{function?} if you are using \textbf{\texttt{ipython}}: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
In [1]: help(powsimp) \PYGZsh{} help() works everywhere | |
In [2]: \PYGZsh{} But in ipython, you can also use ?, which is better because it | |
In [3]: \PYGZsh{} it gives you more information | |
In [4]: powsimp? | |
\end{Verbatim} | |
These will give you the function parameters and docstring for | |
\code{powsimp()}. The output will look something like this: | |
\phantomsection\label{gotchas:module-sympy.simplify.simplify}\index{sympy.simplify.simplify (module)} | |
\subsection{source()} | |
\label{gotchas:source} | |
Another useful option is the \code{source()} function. This will print | |
the source code of a function, including any docstring that it may have. | |
You can also do \code{function??} in \textbf{\texttt{ipython}}. For example, | |
from SymPy 0.6.5: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{source}\PYG{p}{(}\PYG{n}{simplify}\PYG{p}{)} \PYG{c}{\PYGZsh{} simplify() is actually only 2 lines of code. } | |
\PYG{g+go}{In file: ./sympy/simplify/simplify.py} | |
\PYG{g+go}{def simplify(expr):} | |
\PYG{g+go}{ \PYGZdq{}\PYGZdq{}\PYGZdq{}Naively simplifies the given expression.} | |
\PYG{g+go}{ ...} | |
\PYG{g+go}{ Simplification is not a well defined term and the exact strategies} | |
\PYG{g+go}{ this function tries can change in the future versions of SymPy. If} | |
\PYG{g+go}{ your algorithm relies on \PYGZdq{}simplification\PYGZdq{} (whatever it is), try to} | |
\PYG{g+go}{ determine what you need exactly \PYGZhy{} is it powsimp()? radsimp()?} | |
\PYG{g+go}{ together()?, logcombine()?, or something else? And use this particular} | |
\PYG{g+go}{ function directly, because those are well defined and thus your algorithm} | |
\PYG{g+go}{ will be robust.} | |
\PYG{g+go}{ ...} | |
\PYG{g+go}{ \PYGZdq{}\PYGZdq{}\PYGZdq{}} | |
\PYG{g+go}{ expr = Poly.cancel(powsimp(expr))} | |
\PYG{g+go}{ return powsimp(together(expr.expand()), combine=\PYGZsq{}exp\PYGZsq{}, deep=True)} | |
\end{Verbatim} | |
\chapter{SymPy User's Guide} | |
\label{guide:sympy-user-s-guide}\label{guide::doc}\label{guide:guide} | |
\section{Introduction} | |
\label{guide:introduction} | |
If you are new to SymPy, start with the {\hyperref[tutorial/index:tutorial]{\emph{\DUspan{}{Tutorial}}}} (\autopageref*{tutorial/index:tutorial}). If you went | |
through it, now | |
it's time to learn how SymPy works internally, which is what this guide is | |
about. Once you grasp the ideas behind SymPy, you will be able to use it | |
effectively and also know how to extend it and fix it. | |
You may also be just interested in {\hyperref[modules/index:module-docs]{\emph{\DUspan{}{SymPy Modules Reference}}}} (\autopageref*{modules/index:module-docs}). | |
\section{Learning SymPy} | |
\label{guide:learning-sympy} | |
Everyone has different ways of understanding the code written by others. | |
\subsection{Ondřej's approach} | |
\label{guide:ondrej-s-approach} | |
Let's say I'd like to understand how \code{x + y + x} works and how it is possible | |
that it gets simplified to \code{2*x + y}. | |
I write a simple script, I usually call it \code{t.py} (I don't remember anymore | |
why I call it that way): | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y} | |
\PYG{n}{e} \PYG{o}{=} \PYG{n}{x} \PYG{o}{+} \PYG{n}{y} \PYG{o}{+} \PYG{n}{x} | |
\PYG{k}{print} \PYG{n}{e} | |
\end{Verbatim} | |
And I try if it works | |
\begin{alltt} | |
\$ \textbf{python t.py} | |
y + 2*x | |
\end{alltt} | |
Now I start \href{http://winpdb.org/}{winpdb} on it (if you've never used winpdb | |
-- it's an excellent multiplatform debugger, works on Linux, Windows and Mac OS | |
X): | |
\begin{alltt} | |
\$ \textbf{winpdb t.py} | |
y + 2*x | |
\end{alltt} | |
and a winpdb window will popup, I move to the next line using F6: | |
\includegraphics{winpdb1.png} | |
Then I step into (F7) and after a little debugging I get for example: | |
\includegraphics{winpdb2.png} | |
\begin{notice}{tip}{Tip:} | |
Make the winpdb window larger on your screen, it was just made smaller | |
to fit in this guide. | |
\end{notice} | |
I see values of all local variables in the left panel, so it's very easy to see | |
what's happening. You can see, that the \code{y + 2*x} is emerging in the \code{obj} | |
variable. Observing that \code{obj} is constructed from \code{c\_part} and \code{nc\_part} | |
and seeing what \code{c\_part} contains (\code{y} and \code{2*x}). So looking at the line | |
28 (the whole line is not visible on the screenshot, so here it is): | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{n}{c\PYGZus{}part}\PYG{p}{,} \PYG{n}{nc\PYGZus{}part}\PYG{p}{,} \PYG{n}{lambda\PYGZus{}args}\PYG{p}{,} \PYG{n}{order\PYGZus{}symbols} \PYG{o}{=} \PYG{n}{cls}\PYG{o}{.}\PYG{n}{flatten}\PYG{p}{(}\PYG{n+nb}{map}\PYG{p}{(}\PYG{n}{\PYGZus{}sympify}\PYG{p}{,} \PYG{n}{args}\PYG{p}{)}\PYG{p}{)} | |
\end{Verbatim} | |
you can see that the simplification happens in \code{cls.flatten}. Now you can set | |
the breakpoint on the line 28, quit winpdb (it will remember the breakpoint), | |
start it again, hit F5, this will stop at this breakpoint, hit F7, this will go | |
into the function \code{Add.flatten()}: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{n+nd}{@classmethod} | |
\PYG{k}{def} \PYG{n+nf}{flatten}\PYG{p}{(}\PYG{n}{cls}\PYG{p}{,} \PYG{n}{seq}\PYG{p}{)}\PYG{p}{:} | |
\PYG{l+s+sd}{\PYGZdq{}\PYGZdq{}\PYGZdq{}} | |
\PYG{l+s+sd}{ Takes the sequence \PYGZdq{}seq\PYGZdq{} of nested Adds and returns a flatten list.} | |
\PYG{l+s+sd}{ Returns: (commutative\PYGZus{}part, noncommutative\PYGZus{}part, lambda\PYGZus{}args,} | |
\PYG{l+s+sd}{ order\PYGZus{}symbols)} | |
\PYG{l+s+sd}{ Applies associativity, all terms are commutable with respect to} | |
\PYG{l+s+sd}{ addition.} | |
\PYG{l+s+sd}{ \PYGZdq{}\PYGZdq{}\PYGZdq{}} | |
\PYG{n}{terms} \PYG{o}{=} \PYG{p}{\PYGZob{}}\PYG{p}{\PYGZcb{}} \PYG{c}{\PYGZsh{} term \PYGZhy{}\PYGZgt{} coeff} | |
\PYG{c}{\PYGZsh{} e.g. x**2 \PYGZhy{}\PYGZgt{} 5 for ... + 5*x**2 + ...} | |
\PYG{n}{coeff} \PYG{o}{=} \PYG{n}{S}\PYG{o}{.}\PYG{n}{Zero} \PYG{c}{\PYGZsh{} standalone term} | |
\PYG{c}{\PYGZsh{} e.g. 3 + ...} | |
\PYG{n}{lambda\PYGZus{}args} \PYG{o}{=} \PYG{n+nb+bp}{None} | |
\PYG{n}{order\PYGZus{}factors} \PYG{o}{=} \PYG{p}{[}\PYG{p}{]} | |
\PYG{k}{while} \PYG{n}{seq}\PYG{p}{:} | |
\PYG{n}{o} \PYG{o}{=} \PYG{n}{seq}\PYG{o}{.}\PYG{n}{pop}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{)} | |
\end{Verbatim} | |
and then you can study how it works. I am going to stop here, this should be | |
enough to get you going -- with the above technique, I am able to understand | |
almost any Python code. | |
\section{SymPy's Architecture} | |
\label{guide:sympy-s-architecture} | |
We try to make the sources easily understandable, so you can look into the | |
sources and read the doctests, it should be well documented and if you don't | |
understand something, ask on the \href{https://groups.google.com/forum/\#!forum/sympy}{mailinglist}. | |
You can find all the decisions archived in the \href{https://github.com/sympy/sympy/issues}{issues}, to see rationale for | |
doing this and that. | |
\subsection{Basics} | |
\label{guide:basics} | |
All symbolic things are implemented using subclasses of the \code{Basic} class. | |
First, you need to create symbols using \code{Symbol("x")} or numbers using | |
\code{Integer(5)} or \code{Float(34.3)}. Then you construct the expression using any | |
class from SymPy. For example \code{Add(Symbol("a"), Symbol("b"))} gives an | |
instance of the \code{Add} class. You can call all methods, which the particular | |
class supports. | |
For easier use, there is a syntactic sugar for expressions like: | |
\code{cos(x) + 1} is equal to \code{cos(x).\_\_add\_\_(1)} is equal to | |
\code{Add(cos(x), Integer(1))} | |
or | |
\code{2/cos(x)} is equal to \code{cos(x).\_\_rdiv\_\_(2)} is equal to | |
\code{Mul(Rational(2), Pow(cos(x), Rational(-1)))}. | |
So, you can write normal expressions using python arithmetics like this: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{n}{a} \PYG{o}{=} \PYG{n}{Symbol}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s}{a}\PYG{l+s}{\PYGZdq{}}\PYG{p}{)} | |
\PYG{n}{b} \PYG{o}{=} \PYG{n}{Symbol}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s}{b}\PYG{l+s}{\PYGZdq{}}\PYG{p}{)} | |
\PYG{n}{e} \PYG{o}{=} \PYG{p}{(}\PYG{n}{a} \PYG{o}{+} \PYG{n}{b}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} | |
\PYG{k}{print} \PYG{n}{e} | |
\end{Verbatim} | |
but from the SymPy point of view, we just need the classes \code{Add}, \code{Mul}, | |
\code{Pow}, \code{Rational}, \code{Integer}. | |
\subsection{Automatic evaluation to canonical form} | |
\label{guide:automatic-evaluation-to-canonical-form} | |
For computation, all expressions need to be in a | |
canonical form, this is done during the creation of the particular instance | |
and only inexpensive operations are performed, necessary to put the expression | |
in the | |
canonical form. So the canonical form doesn't mean the simplest possible | |
expression. The exact list of operations performed depend on the | |
implementation. Obviously, the definition of the canonical form is arbitrary, | |
the only requirement is that all equivalent expressions must have the same | |
canonical form. We tried the conversion to a canonical (standard) form to be | |
as fast as possible and also in a way so that the result is what you would | |
write by hand - so for example \code{b*a + -4 + b + a*b + 4 + (a + b)**2} becomes | |
\code{2*a*b + b + (a + b)**2}. | |
Whenever you construct an expression, for example \code{Add(x, x)}, the | |
\code{Add.\_\_new\_\_()} is called and it determines what to return. In this case: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{Add} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e} \PYG{o}{=} \PYG{n}{Add}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e} | |
\PYG{g+go}{2*x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb}{type}\PYG{p}{(}\PYG{n}{e}\PYG{p}{)} | |
\PYG{g+go}{\PYGZlt{}class \PYGZsq{}sympy.core.mul.Mul\PYGZsq{}\PYGZgt{}} | |
\end{Verbatim} | |
\code{e} is actually an instance of \code{Mul(2, x)}, because \code{Add.\_\_new\_\_()} | |
returned \code{Mul}. | |
\subsection{Comparisons} | |
\label{guide:comparisons} | |
Expressions can be compared using a regular python syntax: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x} \PYG{o}{+} \PYG{n}{y} \PYG{o}{==} \PYG{n}{y} \PYG{o}{+} \PYG{n}{x} | |
\PYG{g+go}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x} \PYG{o}{+} \PYG{n}{y} \PYG{o}{==} \PYG{n}{y} \PYG{o}{\PYGZhy{}} \PYG{n}{x} | |
\PYG{g+go}{False} | |
\end{Verbatim} | |
We made the following decision in SymPy: \code{a = Symbol("x")} and another | |
\code{b = Symbol("x")} (with the same string ``x'') is the same thing, i.e \code{a == b} is | |
\code{True}. We chose \code{a == b}, because it is more natural - \code{exp(x) == exp(x)} is | |
also \code{True} for the same instance of \code{x} but different instances of \code{exp}, | |
so we chose to have \code{exp(x) == exp(x)} even for different instances of \code{x}. | |
Sometimes, you need to have a unique symbol, for example as a temporary one in | |
some calculation, which is going to be substituted for something else at the | |
end anyway. This is achieved using \code{Dummy("x")}. So, to sum it | |
up: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{Symbol}\PYG{p}{,} \PYG{n}{Dummy} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Symbol}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s}{x}\PYG{l+s}{\PYGZdq{}}\PYG{p}{)} \PYG{o}{==} \PYG{n}{Symbol}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s}{x}\PYG{l+s}{\PYGZdq{}}\PYG{p}{)} | |
\PYG{g+go}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Dummy}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s}{x}\PYG{l+s}{\PYGZdq{}}\PYG{p}{)} \PYG{o}{==} \PYG{n}{Dummy}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s}{x}\PYG{l+s}{\PYGZdq{}}\PYG{p}{)} | |
\PYG{g+go}{False} | |
\end{Verbatim} | |
\subsection{Debugging} | |
\label{guide:debugging} | |
Starting with 0.6.4, you can turn on/off debug messages with the environment | |
variable \code{SYMPY\_DEBUG}, which is expected to have the values True or False. For | |
example, to turn on debugging, you would issue: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
[user@localhost]: SYMPY\PYGZus{}DEBUG=True ./bin/isympy | |
\end{Verbatim} | |
\subsection{Functionality} | |
\label{guide:functionality} | |
There are no given requirements on classes in the library. For example, if they | |
don't implement the \code{fdiff()} method and you construct an expression using | |
such a class, then trying to use the \code{Basic.series()} method will raise an | |
exception of not finding the \code{fdiff()} method in your class. This ``duck | |
typing'' has an advantage that you just implement the functionality which you | |
need. | |
You can define the \code{cos} class like this: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{k}{class} \PYG{n+nc}{cos}\PYG{p}{(}\PYG{n}{Function}\PYG{p}{)}\PYG{p}{:} | |
\PYG{k}{pass} | |
\end{Verbatim} | |
and use it like \code{1 + cos(x)}, but if you don't implement the \code{fdiff()} method, | |
you will not be able to call \code{(1 + cos(x)).series()}. | |
The symbolic object is characterized (defined) by the things which it can do, | |
so implementing more methods like \code{fdiff()}, \code{subs()} etc., you are creating | |
a ``shape'' of the symbolic object. Useful things to implement in new classes are: | |
\code{hash()} (to use the class in comparisons), \code{fdiff()} (to use it in series | |
expansion), \code{subs()} (to use it in expressions, where some parts are being | |
substituted) and \code{series()} (if the series cannot be computed using the | |
general \code{Basic.series()} method). When you create a new class, don't worry | |
about this too much - just try to use it in your code and you will realize | |
immediately which methods need to be implemented in each situation. | |
All objects in sympy are immutable - in the sense that any operation just | |
returns a new instance (it can return the same instance only if it didn't | |
change). This is a common mistake to change the current instance, like | |
\code{self.arg = self.arg + 1} (wrong!). Use \code{arg = self.arg + 1; return arg} instead. | |
The object is immutable in the | |
sense of the symbolic expression it represents. It can modify itself to keep | |
track of, for example, its hash. Or it can recalculate anything regarding the | |
expression it contains. But the expression cannot be changed. So you can pass | |
any instance to other objects, because you don't have to worry that it will | |
change, or that this would break anything. | |
\subsection{Conclusion} | |
\label{guide:conclusion} | |
Above are the main ideas behind SymPy that we try to obey. The rest | |
depends on the current implementation and may possibly change in the future. | |
The point of all of this is that the interdependencies inside SymPy should be | |
kept to a minimum. If one wants to add new functionality to SymPy, all that is | |
necessary is to create a subclass of \code{Basic} and implement what you want. | |
\subsection{Functions} | |
\label{guide:functions} | |
How to create a new function with one variable: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{k}{class} \PYG{n+nc}{sign}\PYG{p}{(}\PYG{n}{Function}\PYG{p}{)}\PYG{p}{:} | |
\PYG{n}{nargs} \PYG{o}{=} \PYG{l+m+mi}{1} | |
\PYG{n+nd}{@classmethod} | |
\PYG{k}{def} \PYG{n+nf}{eval}\PYG{p}{(}\PYG{n}{cls}\PYG{p}{,} \PYG{n}{arg}\PYG{p}{)}\PYG{p}{:} | |
\PYG{k}{if} \PYG{n+nb}{isinstance}\PYG{p}{(}\PYG{n}{arg}\PYG{p}{,} \PYG{n}{Basic}\PYG{o}{.}\PYG{n}{NaN}\PYG{p}{)}\PYG{p}{:} | |
\PYG{k}{return} \PYG{n}{S}\PYG{o}{.}\PYG{n}{NaN} | |
\PYG{k}{if} \PYG{n+nb}{isinstance}\PYG{p}{(}\PYG{n}{arg}\PYG{p}{,} \PYG{n}{Basic}\PYG{o}{.}\PYG{n}{Zero}\PYG{p}{)}\PYG{p}{:} | |
\PYG{k}{return} \PYG{n}{S}\PYG{o}{.}\PYG{n}{Zero} | |
\PYG{k}{if} \PYG{n}{arg}\PYG{o}{.}\PYG{n}{is\PYGZus{}positive}\PYG{p}{:} | |
\PYG{k}{return} \PYG{n}{S}\PYG{o}{.}\PYG{n}{One} | |
\PYG{k}{if} \PYG{n}{arg}\PYG{o}{.}\PYG{n}{is\PYGZus{}negative}\PYG{p}{:} | |
\PYG{k}{return} \PYG{n}{S}\PYG{o}{.}\PYG{n}{NegativeOne} | |
\PYG{k}{if} \PYG{n+nb}{isinstance}\PYG{p}{(}\PYG{n}{arg}\PYG{p}{,} \PYG{n}{Basic}\PYG{o}{.}\PYG{n}{Mul}\PYG{p}{)}\PYG{p}{:} | |
\PYG{n}{coeff}\PYG{p}{,} \PYG{n}{terms} \PYG{o}{=} \PYG{n}{arg}\PYG{o}{.}\PYG{n}{as\PYGZus{}coeff\PYGZus{}mul}\PYG{p}{(}\PYG{p}{)} | |
\PYG{k}{if} \PYG{o+ow}{not} \PYG{n+nb}{isinstance}\PYG{p}{(}\PYG{n}{coeff}\PYG{p}{,} \PYG{n}{Basic}\PYG{o}{.}\PYG{n}{One}\PYG{p}{)}\PYG{p}{:} | |
\PYG{k}{return} \PYG{n}{cls}\PYG{p}{(}\PYG{n}{coeff}\PYG{p}{)} \PYG{o}{*} \PYG{n}{cls}\PYG{p}{(}\PYG{n}{Basic}\PYG{o}{.}\PYG{n}{Mul}\PYG{p}{(}\PYG{o}{*}\PYG{n}{terms}\PYG{p}{)}\PYG{p}{)} | |
\PYG{n}{is\PYGZus{}finite} \PYG{o}{=} \PYG{n+nb+bp}{True} | |
\PYG{k}{def} \PYG{n+nf}{\PYGZus{}eval\PYGZus{}conjugate}\PYG{p}{(}\PYG{n+nb+bp}{self}\PYG{p}{)}\PYG{p}{:} | |
\PYG{k}{return} \PYG{n+nb+bp}{self} | |
\PYG{k}{def} \PYG{n+nf}{\PYGZus{}eval\PYGZus{}is\PYGZus{}zero}\PYG{p}{(}\PYG{n+nb+bp}{self}\PYG{p}{)}\PYG{p}{:} | |
\PYG{k}{return} \PYG{n+nb}{isinstance}\PYG{p}{(}\PYG{n+nb+bp}{self}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{]}\PYG{p}{,} \PYG{n}{Basic}\PYG{o}{.}\PYG{n}{Zero}\PYG{p}{)} | |
\end{Verbatim} | |
and that's it. The \code{\_eval\_*} functions are called when something is needed. | |
The \code{eval} is called when the class is about to be instantiated and it | |
should return either some simplified instance of some other class or if the | |
class should be unmodified, return \code{None} (see \code{core/function.py} in | |
\code{Function.\_\_new\_\_} for implementation details). See also tests in | |
\href{https://github.com/sympy/sympy/blob/master/sympy/functions/elementary/tests/test\_interface.py}{sympy/functions/elementary/tests/test\_interface.py} that test this interface. You can use them to create your own new functions. | |
The applied function \code{sign(x)} is constructed using | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{n}{sign}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} | |
\end{Verbatim} | |
both inside and outside of SymPy. Unapplied functions \code{sign} is just the class | |
itself: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{n}{sign} | |
\end{Verbatim} | |
both inside and outside of SymPy. This is the current structure of classes in | |
SymPy: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{k}{class} \PYG{n+nc}{BasicType}\PYG{p}{(}\PYG{n+nb}{type}\PYG{p}{)}\PYG{p}{:} | |
\PYG{k}{pass} | |
\PYG{k}{class} \PYG{n+nc}{MetaBasicMeths}\PYG{p}{(}\PYG{n}{BasicType}\PYG{p}{)}\PYG{p}{:} | |
\PYG{o}{.}\PYG{o}{.}\PYG{o}{.} | |
\PYG{k}{class} \PYG{n+nc}{BasicMeths}\PYG{p}{(}\PYG{n}{AssumeMeths}\PYG{p}{)}\PYG{p}{:} | |
\PYG{n}{\PYGZus{}\PYGZus{}metaclass\PYGZus{}\PYGZus{}} \PYG{o}{=} \PYG{n}{MetaBasicMeths} | |
\PYG{o}{.}\PYG{o}{.}\PYG{o}{.} | |
\PYG{k}{class} \PYG{n+nc}{Basic}\PYG{p}{(}\PYG{n}{BasicMeths}\PYG{p}{)}\PYG{p}{:} | |
\PYG{o}{.}\PYG{o}{.}\PYG{o}{.} | |
\PYG{k}{class} \PYG{n+nc}{FunctionClass}\PYG{p}{(}\PYG{n}{MetaBasicMeths}\PYG{p}{)}\PYG{p}{:} | |
\PYG{o}{.}\PYG{o}{.}\PYG{o}{.} | |
\PYG{k}{class} \PYG{n+nc}{Function}\PYG{p}{(}\PYG{n}{Basic}\PYG{p}{,} \PYG{n}{RelMeths}\PYG{p}{,} \PYG{n}{ArithMeths}\PYG{p}{)}\PYG{p}{:} | |
\PYG{n}{\PYGZus{}\PYGZus{}metaclass\PYGZus{}\PYGZus{}} \PYG{o}{=} \PYG{n}{FunctionClass} | |
\PYG{o}{.}\PYG{o}{.}\PYG{o}{.} | |
\end{Verbatim} | |
The exact names of the classes and the names of the methods and how they work | |
can be changed in the future. | |
This is how to create a function with two variables: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{k}{class} \PYG{n+nc}{chebyshevt\PYGZus{}root}\PYG{p}{(}\PYG{n}{Function}\PYG{p}{)}\PYG{p}{:} | |
\PYG{n}{nargs} \PYG{o}{=} \PYG{l+m+mi}{2} | |
\PYG{n+nd}{@classmethod} | |
\PYG{k}{def} \PYG{n+nf}{eval}\PYG{p}{(}\PYG{n}{cls}\PYG{p}{,} \PYG{n}{n}\PYG{p}{,} \PYG{n}{k}\PYG{p}{)}\PYG{p}{:} | |
\PYG{k}{if} \PYG{o+ow}{not} \PYG{l+m+mi}{0} \PYG{o}{\PYGZlt{}}\PYG{o}{=} \PYG{n}{k} \PYG{o}{\PYGZlt{}} \PYG{n}{n}\PYG{p}{:} | |
\PYG{k}{raise} \PYG{n+ne}{ValueError}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s}{must have 0 \PYGZlt{}= k \PYGZlt{} n}\PYG{l+s}{\PYGZdq{}}\PYG{p}{)} | |
\PYG{k}{return} \PYG{n}{C}\PYG{o}{.}\PYG{n}{cos}\PYG{p}{(}\PYG{n}{S}\PYG{o}{.}\PYG{n}{Pi}\PYG{o}{*}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{k} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{/}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{n}\PYG{p}{)}\PYG{p}{)} | |
\end{Verbatim} | |
\begin{notice}{note}{Note:} | |
the first argument of a @classmethod should be \code{cls} (i.e. not | |
\code{self}). | |
\end{notice} | |
Here it's how to define a derivative of the function: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{Function}\PYG{p}{,} \PYG{n}{sympify}\PYG{p}{,} \PYG{n}{cos} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{class} \PYG{n+nc}{my\PYGZus{}function}\PYG{p}{(}\PYG{n}{Function}\PYG{p}{)}\PYG{p}{:} | |
\PYG{g+gp}{... } \PYG{n}{nargs} \PYG{o}{=} \PYG{l+m+mi}{1} | |
\PYG{g+gp}{...} | |
\PYG{g+gp}{... } \PYG{k}{def} \PYG{n+nf}{fdiff}\PYG{p}{(}\PYG{n+nb+bp}{self}\PYG{p}{,} \PYG{n}{argindex} \PYG{o}{=} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{p}{:} | |
\PYG{g+gp}{... } \PYG{k}{return} \PYG{n}{cos}\PYG{p}{(}\PYG{n+nb+bp}{self}\PYG{o}{.}\PYG{n}{args}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{...} | |
\PYG{g+gp}{... } \PYG{n+nd}{@classmethod} | |
\PYG{g+gp}{... } \PYG{k}{def} \PYG{n+nf}{eval}\PYG{p}{(}\PYG{n}{cls}\PYG{p}{,} \PYG{n}{arg}\PYG{p}{)}\PYG{p}{:} | |
\PYG{g+gp}{... } \PYG{n}{arg} \PYG{o}{=} \PYG{n}{sympify}\PYG{p}{(}\PYG{n}{arg}\PYG{p}{)} | |
\PYG{g+gp}{... } \PYG{k}{if} \PYG{n}{arg} \PYG{o}{==} \PYG{l+m+mi}{0}\PYG{p}{:} | |
\PYG{g+gp}{... } \PYG{k}{return} \PYG{n}{sympify}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{)} | |
\end{Verbatim} | |
So guess what this \code{my\_function} is going to be? Well, it's derivative is | |
\code{cos} and the function value at 0 is 0, but let's pretend we don't know: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{pprint} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{pprint}\PYG{p}{(}\PYG{n}{my\PYGZus{}function}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{series}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{10}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{ 3 5 7 9} | |
\PYG{g+go}{ x x x x / 10\PYGZbs{}} | |
\PYG{g+go}{x \PYGZhy{} \PYGZhy{}\PYGZhy{} + \PYGZhy{}\PYGZhy{}\PYGZhy{} \PYGZhy{} \PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{} + \PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{} + O\PYGZbs{}x /} | |
\PYG{g+go}{ 6 120 5040 362880} | |
\end{Verbatim} | |
Looks familiar indeed: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{sin} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{pprint}\PYG{p}{(}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{series}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{10}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{ 3 5 7 9} | |
\PYG{g+go}{ x x x x / 10\PYGZbs{}} | |
\PYG{g+go}{x \PYGZhy{} \PYGZhy{}\PYGZhy{} + \PYGZhy{}\PYGZhy{}\PYGZhy{} \PYGZhy{} \PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{} + \PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{} + O\PYGZbs{}x /} | |
\PYG{g+go}{ 6 120 5040 362880} | |
\end{Verbatim} | |
Let's try a more complicated example. Let's define the derivative in terms of | |
the function itself: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{class} \PYG{n+nc}{what\PYGZus{}am\PYGZus{}i}\PYG{p}{(}\PYG{n}{Function}\PYG{p}{)}\PYG{p}{:} | |
\PYG{g+gp}{... } \PYG{n}{nargs} \PYG{o}{=} \PYG{l+m+mi}{1} | |
\PYG{g+gp}{...} | |
\PYG{g+gp}{... } \PYG{k}{def} \PYG{n+nf}{fdiff}\PYG{p}{(}\PYG{n+nb+bp}{self}\PYG{p}{,} \PYG{n}{argindex} \PYG{o}{=} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{p}{:} | |
\PYG{g+gp}{... } \PYG{k}{return} \PYG{l+m+mi}{1} \PYG{o}{\PYGZhy{}} \PYG{n}{what\PYGZus{}am\PYGZus{}i}\PYG{p}{(}\PYG{n+nb+bp}{self}\PYG{o}{.}\PYG{n}{args}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{]}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} | |
\PYG{g+gp}{...} | |
\PYG{g+gp}{... } \PYG{n+nd}{@classmethod} | |
\PYG{g+gp}{... } \PYG{k}{def} \PYG{n+nf}{eval}\PYG{p}{(}\PYG{n}{cls}\PYG{p}{,} \PYG{n}{arg}\PYG{p}{)}\PYG{p}{:} | |
\PYG{g+gp}{... } \PYG{n}{arg} \PYG{o}{=} \PYG{n}{sympify}\PYG{p}{(}\PYG{n}{arg}\PYG{p}{)} | |
\PYG{g+gp}{... } \PYG{k}{if} \PYG{n}{arg} \PYG{o}{==} \PYG{l+m+mi}{0}\PYG{p}{:} | |
\PYG{g+gp}{... } \PYG{k}{return} \PYG{n}{sympify}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{)} | |
\end{Verbatim} | |
So what is \code{what\_am\_i}? Let's try it: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{pprint}\PYG{p}{(}\PYG{n}{what\PYGZus{}am\PYGZus{}i}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{series}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{10}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{ 3 5 7 9} | |
\PYG{g+go}{ x 2*x 17*x 62*x / 10\PYGZbs{}} | |
\PYG{g+go}{x \PYGZhy{} \PYGZhy{}\PYGZhy{} + \PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{} \PYGZhy{} \PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{} + \PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{} + O\PYGZbs{}x /} | |
\PYG{g+go}{ 3 15 315 2835} | |
\end{Verbatim} | |
Well, it's \code{tanh}: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{tanh} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{pprint}\PYG{p}{(}\PYG{n}{tanh}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{series}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{10}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{ 3 5 7 9} | |
\PYG{g+go}{ x 2*x 17*x 62*x / 10\PYGZbs{}} | |
\PYG{g+go}{x \PYGZhy{} \PYGZhy{}\PYGZhy{} + \PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{} \PYGZhy{} \PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{} + \PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{}\PYGZhy{} + O\PYGZbs{}x /} | |
\PYG{g+go}{ 3 15 315 2835} | |
\end{Verbatim} | |
The new functions we just defined are regular SymPy objects, you | |
can use them all over SymPy, e.g.: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{limit} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{limit}\PYG{p}{(}\PYG{n}{what\PYGZus{}am\PYGZus{}i}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{/}\PYG{n}{x}\PYG{p}{,} \PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{)} | |
\PYG{g+go}{1} | |
\end{Verbatim} | |
\subsection{Common tasks} | |
\label{guide:common-tasks} | |
Please use the same way as is shown below all across SymPy. | |
\textbf{accessing parameters}: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{sign}\PYG{p}{,} \PYG{n}{sin} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{z} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e} \PYG{o}{=} \PYG{n}{sign}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e}\PYG{o}{.}\PYG{n}{args} | |
\PYG{g+go}{(x**2,)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e}\PYG{o}{.}\PYG{n}{args}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{]} | |
\PYG{g+go}{x**2} | |
\PYG{g+go}{Number arguments (in Adds and Muls) will always be the first argument;} | |
\PYG{g+go}{other arguments might be in arbitrary order:} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{1} \PYG{o}{+} \PYG{n}{x} \PYG{o}{+} \PYG{n}{y}\PYG{o}{*}\PYG{n}{z}\PYG{p}{)}\PYG{o}{.}\PYG{n}{args}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{]} | |
\PYG{g+go}{1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{1} \PYG{o}{+} \PYG{n}{x} \PYG{o}{+} \PYG{n}{y}\PYG{o}{*}\PYG{n}{z}\PYG{p}{)}\PYG{o}{.}\PYG{n}{args}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{]} \PYG{o+ow}{in} \PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{o}{*}\PYG{n}{z}\PYG{p}{)} | |
\PYG{g+go}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{y}\PYG{o}{*}\PYG{n}{z}\PYG{p}{)}\PYG{o}{.}\PYG{n}{args} | |
\PYG{g+go}{(y, z)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{sin}\PYG{p}{(}\PYG{n}{y}\PYG{o}{*}\PYG{n}{z}\PYG{p}{)}\PYG{o}{.}\PYG{n}{args} | |
\PYG{g+go}{(y*z,)} | |
\end{Verbatim} | |
Never use internal methods or variables, prefixed with ``\code{\_}'' (example: don't | |
use \code{\_args}, use \code{.args} instead). | |
\textbf{testing the structure of a SymPy expression} | |
Applied functions: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{sign}\PYG{p}{,} \PYG{n}{exp}\PYG{p}{,} \PYG{n}{Function} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e} \PYG{o}{=} \PYG{n}{sign}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb}{isinstance}\PYG{p}{(}\PYG{n}{e}\PYG{p}{,} \PYG{n}{sign}\PYG{p}{)} | |
\PYG{g+go}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb}{isinstance}\PYG{p}{(}\PYG{n}{e}\PYG{p}{,} \PYG{n}{exp}\PYG{p}{)} | |
\PYG{g+go}{False} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb}{isinstance}\PYG{p}{(}\PYG{n}{e}\PYG{p}{,} \PYG{n}{Function}\PYG{p}{)} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
So \code{e} is a \code{sign(z)} function, but not \code{exp(z)} function. | |
Unapplied functions: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{sign}\PYG{p}{,} \PYG{n}{exp}\PYG{p}{,} \PYG{n}{FunctionClass} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e} \PYG{o}{=} \PYG{n}{sign} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{f} \PYG{o}{=} \PYG{n}{exp} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{g} \PYG{o}{=} \PYG{n}{Add} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb}{isinstance}\PYG{p}{(}\PYG{n}{e}\PYG{p}{,} \PYG{n}{FunctionClass}\PYG{p}{)} | |
\PYG{g+go}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb}{isinstance}\PYG{p}{(}\PYG{n}{f}\PYG{p}{,} \PYG{n}{FunctionClass}\PYG{p}{)} | |
\PYG{g+go}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb}{isinstance}\PYG{p}{(}\PYG{n}{g}\PYG{p}{,} \PYG{n}{FunctionClass}\PYG{p}{)} | |
\PYG{g+go}{False} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{g} \PYG{o+ow}{is} \PYG{n}{Add} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
So \code{e} and \code{f} are functions, \code{g} is not a function. | |
\section{Contributing} | |
\label{guide:contributing} | |
We welcome every SymPy user to participate in it's development. Don't worry if | |
you've never contributed to any open source project, we'll help you learn | |
anything necessary, just ask on our \href{https://groups.google.com/forum/\#!forum/sympy}{mailinglist}. | |
Don't be afraid to ask anything and don't worry that you are wasting our time | |
if you are new to SymPy and ask questions that maybe most of the people know | |
the answer to -- you are not, because that's exactly what the \href{https://groups.google.com/forum/\#!forum/sympy}{mailinglist} is | |
for and people answer your emails because they want to. Also we try hard to | |
answer every email, so you'll always get some feedback and pointers what to do | |
next. | |
\subsection{Improving the code} | |
\label{guide:improving-the-code} | |
Go to \href{https://github.com/sympy/sympy/issues}{issues} that are sorted by priority and simply find something that you | |
would like to get fixed and fix it. If you find something odd, please report it | |
into \href{https://github.com/sympy/sympy/issues}{issues} first before fixing it. Feel free to consult with us on the | |
\href{https://groups.google.com/forum/\#!forum/sympy}{mailinglist}. Then send your patch either to the \href{https://github.com/sympy/sympy/issues}{issues} or the \href{https://groups.google.com/forum/\#!forum/sympy}{mailinglist}. | |
Please read our excellent \href{https://github.com/sympy/sympy/wiki/Development-workflow}{SymPy Patches Tutorial} at our | |
wiki for a guide on how to write patches to SymPy, how to work with Git, | |
and how to make your life easier as you get started with SymPy. | |
\subsection{Improving the docs} | |
\label{guide:improving-the-docs}\label{guide:mailinglist} | |
Please see {\hyperref[modules/index:module-docs]{\emph{\DUspan{}{the documentation}}}} (\autopageref*{modules/index:module-docs}) how to fix and improve | |
SymPy's documentation. All contribution is very welcome. | |
\chapter{SymPy Modules Reference} | |
\label{modules/index:module-docs}\label{modules/index::doc}\label{modules/index:sympy-modules-reference} | |
Because every feature of SymPy must have a test case, when you are not sure how | |
to use something, just look into the \code{tests/} directories, find that feature | |
and read the tests for it, that will tell you everything you need to know. | |
Most of the things are already documented though in this document, that is | |
automatically generated using SymPy's docstrings. | |
Click the ``modules'' (\DUspan{xref,std,std-ref}{modindex}) link in the top right corner to easily | |
access any SymPy module, or use this contens: | |
\section{SymPy Core} | |
\label{modules/core:sympy-core}\label{modules/core::doc} | |
\subsection{sympify} | |
\label{modules/core:sympify}\label{modules/core:module-sympy.core.sympify}\index{sympy.core.sympify (module)} | |
\subsubsection{sympify} | |
\label{modules/core:id1}\index{sympify() (in module sympy.core.sympify)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.sympify.sympify}\pysiglinewithargsret{\code{sympy.core.sympify.}\bfcode{sympify}}{\emph{a}, \emph{locals=None}, \emph{convert\_xor=True}, \emph{strict=False}, \emph{rational=False}, \emph{evaluate=None}}{} | |
Converts an arbitrary expression to a type that can be used inside SymPy. | |
For example, it will convert Python ints into instance of sympy.Rational, | |
floats into instances of sympy.Float, etc. It is also able to coerce symbolic | |
expressions which inherit from Basic. This can be useful in cooperation | |
with SAGE. | |
\begin{description} | |
\item[{It currently accepts as arguments:}] \leavevmode\begin{itemize} | |
\item {} | |
any object defined in sympy | |
\item {} | |
standard numeric python types: int, long, float, Decimal | |
\item {} | |
strings (like ``0.09'' or ``2e-19'') | |
\item {} | |
booleans, including \code{None} (will leave \code{None} unchanged) | |
\item {} | |
lists, sets or tuples containing any of the above | |
\end{itemize} | |
\end{description} | |
If the argument is already a type that SymPy understands, it will do | |
nothing but return that value. This can be used at the beginning of a | |
function to ensure you are working with the correct type. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{sympify} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{sympify}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{.}\PYG{n}{is\PYGZus{}integer} | |
\PYG{g+go}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{sympify}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{.}\PYG{n}{is\PYGZus{}real} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{sympify}\PYG{p}{(}\PYG{l+m+mf}{2.0}\PYG{p}{)}\PYG{o}{.}\PYG{n}{is\PYGZus{}real} | |
\PYG{g+go}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{sympify}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s}{2.0}\PYG{l+s}{\PYGZdq{}}\PYG{p}{)}\PYG{o}{.}\PYG{n}{is\PYGZus{}real} | |
\PYG{g+go}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{sympify}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s}{2e\PYGZhy{}45}\PYG{l+s}{\PYGZdq{}}\PYG{p}{)}\PYG{o}{.}\PYG{n}{is\PYGZus{}real} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
If the expression could not be converted, a SympifyError is raised. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{sympify}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s}{x***2}\PYG{l+s}{\PYGZdq{}}\PYG{p}{)} | |
\PYG{g+gt}{Traceback (most recent call last):} | |
\PYG{c}{...} | |
\PYG{g+gr}{SympifyError}: \PYG{n}{SympifyError: \PYGZdq{}could not parse u\PYGZsq{}x***2\PYGZsq{}\PYGZdq{}} | |
\PYG{g+gt}{Traceback (most recent call last):} | |
\PYG{c}{...} | |
\PYG{g+gr}{SympifyError}: \PYG{n}{SympifyError: \PYGZdq{}could not parse u\PYGZsq{}x***2\PYGZsq{}\PYGZdq{}} | |
\end{Verbatim} | |
\paragraph{Notes} | |
Sometimes autosimplification during sympification results in expressions | |
that are very different in structure than what was entered. Until such | |
autosimplification is no longer done, the \code{kernS} function might be of | |
some use. In the example below you can see how an expression reduces to | |
-1 by autosimplification, but does not do so when \code{kernS} is used. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.core.sympify} \PYG{k+kn}{import} \PYG{n}{kernS} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{o}{/}\PYG{n}{x}\PYG{p}{)}\PYG{o}{/}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{p}{(}\PYG{n}{x} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{1}\PYG{o}{/}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{1}\PYG{o}{/}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{p}{(}\PYG{n}{x} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{1}\PYG{o}{/}\PYG{n}{x}\PYG{p}{)}\PYG{p}{)}\PYG{p}{)} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{1} | |
\PYG{g+go}{\PYGZhy{}1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{s} \PYG{o}{=} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{\PYGZhy{}2*(\PYGZhy{}(\PYGZhy{}x + 1/x)/(x*(x \PYGZhy{} 1/x)**2) \PYGZhy{} 1/(x*(x \PYGZhy{} 1/x))) \PYGZhy{} 1}\PYG{l+s}{\PYGZsq{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{sympify}\PYG{p}{(}\PYG{n}{s}\PYG{p}{)} | |
\PYG{g+go}{\PYGZhy{}1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{kernS}\PYG{p}{(}\PYG{n}{s}\PYG{p}{)} | |
\PYG{g+go}{\PYGZhy{}2*(\PYGZhy{}(\PYGZhy{}x + 1/x)/(x*(x \PYGZhy{} 1/x)**2) \PYGZhy{} 1/(x*(x \PYGZhy{} 1/x))) \PYGZhy{} 1} | |
\end{Verbatim} | |
\paragraph{Locals} | |
The sympification happens with access to everything that is loaded | |
by \code{from sympy import *}; anything used in a string that is not | |
defined by that import will be converted to a symbol. In the following, | |
the \code{bitcount} function is treated as a symbol and the \code{O} is | |
interpreted as the Order object (used with series) and it raises | |
an error when used improperly: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{s} \PYG{o}{=} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{bitcount(42)}\PYG{l+s}{\PYGZsq{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{sympify}\PYG{p}{(}\PYG{n}{s}\PYG{p}{)} | |
\PYG{g+go}{bitcount(42)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{sympify}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s}{O(x)}\PYG{l+s}{\PYGZdq{}}\PYG{p}{)} | |
\PYG{g+go}{O(x)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{sympify}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s}{O + 1}\PYG{l+s}{\PYGZdq{}}\PYG{p}{)} | |
\PYG{g+gt}{Traceback (most recent call last):} | |
\PYG{c}{...} | |
\PYG{g+gr}{TypeError}: \PYG{n}{unbound method...} | |
\PYG{g+gt}{Traceback (most recent call last):} | |
\PYG{c}{...} | |
\PYG{g+gr}{TypeError}: \PYG{n}{unbound method...} | |
\end{Verbatim} | |
In order to have \code{bitcount} be recognized it can be imported into a | |
namespace dictionary and passed as locals: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.core.compatibility} \PYG{k+kn}{import} \PYG{n}{exec\PYGZus{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{ns} \PYG{o}{=} \PYG{p}{\PYGZob{}}\PYG{p}{\PYGZcb{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{exec\PYGZus{}}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{from sympy.core.evalf import bitcount}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{ns}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{sympify}\PYG{p}{(}\PYG{n}{s}\PYG{p}{,} \PYG{n+nb}{locals}\PYG{o}{=}\PYG{n}{ns}\PYG{p}{)} | |
\PYG{g+go}{6} | |
\end{Verbatim} | |
In order to have the \code{O} interpreted as a Symbol, identify it as such | |
in the namespace dictionary. This can be done in a variety of ways; all | |
three of the following are possibilities: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{Symbol} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{ns}\PYG{p}{[}\PYG{l+s}{\PYGZdq{}}\PYG{l+s}{O}\PYG{l+s}{\PYGZdq{}}\PYG{p}{]} \PYG{o}{=} \PYG{n}{Symbol}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s}{O}\PYG{l+s}{\PYGZdq{}}\PYG{p}{)} \PYG{c}{\PYGZsh{} method 1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{exec\PYGZus{}}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{from sympy.abc import O}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{ns}\PYG{p}{)} \PYG{c}{\PYGZsh{} method 2} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{ns}\PYG{o}{.}\PYG{n}{update}\PYG{p}{(}\PYG{n+nb}{dict}\PYG{p}{(}\PYG{n}{O}\PYG{o}{=}\PYG{n}{Symbol}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s}{O}\PYG{l+s}{\PYGZdq{}}\PYG{p}{)}\PYG{p}{)}\PYG{p}{)} \PYG{c}{\PYGZsh{} method 3} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{sympify}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s}{O + 1}\PYG{l+s}{\PYGZdq{}}\PYG{p}{,} \PYG{n+nb}{locals}\PYG{o}{=}\PYG{n}{ns}\PYG{p}{)} | |
\PYG{g+go}{O + 1} | |
\end{Verbatim} | |
If you want \emph{all} single-letter and Greek-letter variables to be symbols | |
then you can use the clashing-symbols dictionaries that have been defined | |
there as private variables: \_clash1 (single-letter variables), \_clash2 | |
(the multi-letter Greek names) or \_clash (both single and multi-letter | |
names that are defined in abc). | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{\PYGZus{}clash1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{\PYGZus{}clash1} | |
\PYG{g+go}{\PYGZob{}\PYGZsq{}C\PYGZsq{}: C, \PYGZsq{}E\PYGZsq{}: E, \PYGZsq{}I\PYGZsq{}: I, \PYGZsq{}N\PYGZsq{}: N, \PYGZsq{}O\PYGZsq{}: O, \PYGZsq{}Q\PYGZsq{}: Q, \PYGZsq{}S\PYGZsq{}: S\PYGZcb{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{sympify}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{C \PYGZam{} Q}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{\PYGZus{}clash1}\PYG{p}{)} | |
\PYG{g+go}{And(C, Q)} | |
\end{Verbatim} | |
\paragraph{Strict} | |
If the option \code{strict} is set to \code{True}, only the types for which an | |
explicit conversion has been defined are converted. In the other | |
cases, a SympifyError is raised. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{n}{sympify}\PYG{p}{(}\PYG{n+nb+bp}{None}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{None} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{sympify}\PYG{p}{(}\PYG{n+nb+bp}{None}\PYG{p}{,} \PYG{n}{strict}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+gt}{Traceback (most recent call last):} | |
\PYG{c}{...} | |
\PYG{g+gr}{SympifyError}: \PYG{n}{SympifyError: None} | |
\PYG{g+gt}{Traceback (most recent call last):} | |
\PYG{c}{...} | |
\PYG{g+gr}{SympifyError}: \PYG{n}{SympifyError: None} | |
\end{Verbatim} | |
\paragraph{Evaluation} | |
If the option \code{evaluate} is set to \code{False}, then arithmetic and | |
operators will be converted into their SymPy equivalents and the | |
\code{evaluate=False} option will be added. Nested \code{Add} or \code{Mul} will | |
be denested first. This is done via an AST transformation that replaces | |
operators with their SymPy equivalents, so if an operand redefines any | |
of those operations, the redefined operators will not be used. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{sympify}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{2**2 / 3 + 5}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+go}{19/3} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{sympify}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{2**2 / 3 + 5}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{evaluate}\PYG{o}{=}\PYG{n+nb+bp}{False}\PYG{p}{)} | |
\PYG{g+go}{2**2/3 + 5} | |
\end{Verbatim} | |
\paragraph{Extending} | |
To extend \code{sympify} to convert custom objects (not derived from \code{Basic}), | |
just define a \code{\_sympy\_} method to your class. You can do that even to | |
classes that you do not own by subclassing or adding the method at runtime. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{Matrix} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{class} \PYG{n+nc}{MyList1}\PYG{p}{(}\PYG{n+nb}{object}\PYG{p}{)}\PYG{p}{:} | |
\PYG{g+gp}{... } \PYG{k}{def} \PYG{n+nf}{\PYGZus{}\PYGZus{}iter\PYGZus{}\PYGZus{}}\PYG{p}{(}\PYG{n+nb+bp}{self}\PYG{p}{)}\PYG{p}{:} | |
\PYG{g+gp}{... } \PYG{k}{yield} \PYG{l+m+mi}{1} | |
\PYG{g+gp}{... } \PYG{k}{yield} \PYG{l+m+mi}{2} | |
\PYG{g+gp}{... } \PYG{k}{raise} \PYG{n+ne}{StopIteration} | |
\PYG{g+gp}{... } \PYG{k}{def} \PYG{n+nf}{\PYGZus{}\PYGZus{}getitem\PYGZus{}\PYGZus{}}\PYG{p}{(}\PYG{n+nb+bp}{self}\PYG{p}{,} \PYG{n}{i}\PYG{p}{)}\PYG{p}{:} \PYG{k}{return} \PYG{n+nb}{list}\PYG{p}{(}\PYG{n+nb+bp}{self}\PYG{p}{)}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]} | |
\PYG{g+gp}{... } \PYG{k}{def} \PYG{n+nf}{\PYGZus{}sympy\PYGZus{}}\PYG{p}{(}\PYG{n+nb+bp}{self}\PYG{p}{)}\PYG{p}{:} \PYG{k}{return} \PYG{n}{Matrix}\PYG{p}{(}\PYG{n+nb+bp}{self}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{sympify}\PYG{p}{(}\PYG{n}{MyList1}\PYG{p}{(}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{Matrix([} | |
\PYG{g+go}{[1],} | |
\PYG{g+go}{[2]])} | |
\end{Verbatim} | |
If you do not have control over the class definition you could also use the | |
\code{converter} global dictionary. The key is the class and the value is a | |
function that takes a single argument and returns the desired SymPy | |
object, e.g. \code{converter{[}MyList{]} = lambda x: Matrix(x)}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{class} \PYG{n+nc}{MyList2}\PYG{p}{(}\PYG{n+nb}{object}\PYG{p}{)}\PYG{p}{:} \PYG{c}{\PYGZsh{} XXX Do not do this if you control the class!} | |
\PYG{g+gp}{... } \PYG{k}{def} \PYG{n+nf}{\PYGZus{}\PYGZus{}iter\PYGZus{}\PYGZus{}}\PYG{p}{(}\PYG{n+nb+bp}{self}\PYG{p}{)}\PYG{p}{:} \PYG{c}{\PYGZsh{} Use \PYGZus{}sympy\PYGZus{}!} | |
\PYG{g+gp}{... } \PYG{k}{yield} \PYG{l+m+mi}{1} | |
\PYG{g+gp}{... } \PYG{k}{yield} \PYG{l+m+mi}{2} | |
\PYG{g+gp}{... } \PYG{k}{raise} \PYG{n+ne}{StopIteration} | |
\PYG{g+gp}{... } \PYG{k}{def} \PYG{n+nf}{\PYGZus{}\PYGZus{}getitem\PYGZus{}\PYGZus{}}\PYG{p}{(}\PYG{n+nb+bp}{self}\PYG{p}{,} \PYG{n}{i}\PYG{p}{)}\PYG{p}{:} \PYG{k}{return} \PYG{n+nb}{list}\PYG{p}{(}\PYG{n+nb+bp}{self}\PYG{p}{)}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.core.sympify} \PYG{k+kn}{import} \PYG{n}{converter} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{converter}\PYG{p}{[}\PYG{n}{MyList2}\PYG{p}{]} \PYG{o}{=} \PYG{k}{lambda} \PYG{n}{x}\PYG{p}{:} \PYG{n}{Matrix}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{sympify}\PYG{p}{(}\PYG{n}{MyList2}\PYG{p}{(}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{Matrix([} | |
\PYG{g+go}{[1],} | |
\PYG{g+go}{[2]])} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\subsection{assumptions} | |
\label{modules/core:assumptions}\label{modules/core:module-sympy.core.assumptions}\index{sympy.core.assumptions (module)} | |
This module contains the machinery handling assumptions. | |
All symbolic objects have assumption attributes that can be accessed via | |
.is\_\textless{}assumption name\textgreater{} attribute. | |
Assumptions determine certain properties of symbolic objects. Assumptions | |
can have 3 possible values: True, False, None. None is returned when it is | |
impossible to say something about the property. For example, a generic Symbol | |
is not known beforehand to be positive. | |
By default, all symbolic values are in the largest set in the given context | |
without specifying the property. For example, a symbol that has a property | |
being integer, is also real, complex, etc. | |
Here follows a list of possible assumption names: | |
\begin{description} | |
\item[{\index{commutative|textbf}commutative}] \leavevmode\phantomsection\label{modules/core:term-commutative} | |
object commutes with any other object with | |
respect to multiplication operation. | |
\item[{\index{complex|textbf}complex}] \leavevmode\phantomsection\label{modules/core:term-complex} | |
object can have only values from the set | |
of complex numbers. | |
\item[{\index{imaginary|textbf}imaginary}] \leavevmode\phantomsection\label{modules/core:term-imaginary} | |
object value is a number that can be written as a real | |
number multiplied by the imaginary unit \code{I}. See | |
\phantomsection\label{modules/core:id2}{\hyperref[modules/core:r26]{\emph{{[}R26{]}}}} (\autopageref*{modules/core:r26}). Please note, that \code{0} is not considered to be an | |
imaginary number, see | |
\href{https://github.com/sympy/sympy/issues/7649}{issue \#7649}. | |
\item[{\index{real|textbf}real}] \leavevmode\phantomsection\label{modules/core:term-real} | |
object can have only values from the set | |
of real numbers. | |
\item[{\index{integer|textbf}integer}] \leavevmode\phantomsection\label{modules/core:term-integer} | |
object can have only values from the set | |
of integers. | |
\item[{\index{odd|textbf}odd, \index{even|textbf}even}] \leavevmode\phantomsection\label{modules/core:term-odd} | |
object can have only values from the set of | |
odd (even) integers \phantomsection\label{modules/core:id3}{\hyperref[modules/core:r25]{\emph{{[}R25{]}}}} (\autopageref*{modules/core:r25}). | |
\item[{\index{prime|textbf}prime}] \leavevmode\phantomsection\label{modules/core:term-prime} | |
object is a natural number greater than \code{1} that has | |
no positive divisors other than \code{1} and itself. See \phantomsection\label{modules/core:id4}{\hyperref[modules/core:r29]{\emph{{[}R29{]}}}} (\autopageref*{modules/core:r29}). | |
\item[{\index{composite|textbf}composite}] \leavevmode\phantomsection\label{modules/core:term-composite} | |
object is a positive integer that has at least one positive | |
divisor other than \code{1} or the number itself. See \phantomsection\label{modules/core:id5}{\hyperref[modules/core:r27]{\emph{{[}R27{]}}}} (\autopageref*{modules/core:r27}). | |
\item[{\index{zero|textbf}zero, \index{nonzero|textbf}nonzero}] \leavevmode\phantomsection\label{modules/core:term-zero} | |
object is zero (not zero). | |
\item[{\index{rational|textbf}rational}] \leavevmode\phantomsection\label{modules/core:term-rational} | |
object can have only values from the set | |
of rationals. | |
\item[{\index{algebraic|textbf}algebraic}] \leavevmode\phantomsection\label{modules/core:term-algebraic} | |
object can have only values from the set | |
of algebraic numbers \footnote{ | |
\href{http://en.wikipedia.org/wiki/Algebraic\_number}{http://en.wikipedia.org/wiki/Algebraic\_number} | |
}. | |
\item[{\index{transcendental|textbf}transcendental}] \leavevmode\phantomsection\label{modules/core:term-transcendental} | |
object can have only values from the set | |
of transcendental numbers \footnote{ | |
\href{http://en.wikipedia.org/wiki/Transcendental\_number}{http://en.wikipedia.org/wiki/Transcendental\_number} | |
}. | |
\item[{\index{irrational|textbf}irrational}] \leavevmode\phantomsection\label{modules/core:term-irrational} | |
object value cannot be represented exactly by Rational, see \phantomsection\label{modules/core:id8}{\hyperref[modules/core:r28]{\emph{{[}R28{]}}}} (\autopageref*{modules/core:r28}). | |
\item[{\index{finite|textbf}finite, \index{infinite|textbf}infinite}] \leavevmode\phantomsection\label{modules/core:term-finite} | |
object absolute value is bounded (is value is | |
arbitrarily large). See \phantomsection\label{modules/core:id9}{\hyperref[modules/core:r30]{\emph{{[}R30{]}}}} (\autopageref*{modules/core:r30}), \phantomsection\label{modules/core:id10}{\hyperref[modules/core:r31]{\emph{{[}R31{]}}}} (\autopageref*{modules/core:r31}), \phantomsection\label{modules/core:id11}{\hyperref[modules/core:r32]{\emph{{[}R32{]}}}} (\autopageref*{modules/core:r32}). | |
\item[{\index{negative|textbf}negative, \index{nonnegative|textbf}nonnegative}] \leavevmode\phantomsection\label{modules/core:term-negative} | |
object can have only negative (only | |
nonnegative) values \phantomsection\label{modules/core:id12}{\hyperref[modules/core:r24]{\emph{{[}R24{]}}}} (\autopageref*{modules/core:r24}). | |
\item[{\index{positive|textbf}positive, \index{nonpositive|textbf}nonpositive}] \leavevmode\phantomsection\label{modules/core:term-positive} | |
object can have only positive (only | |
nonpositive) values. | |
\item[{\index{hermitian|textbf}hermitian, \index{antihermitian|textbf}antihermitian}] \leavevmode\phantomsection\label{modules/core:term-hermitian} | |
object belongs to the field of hermitian | |
(antihermitian) operators. | |
\end{description} | |
\subsubsection{Examples} | |
\label{modules/core:examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{Symbol} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x} \PYG{o}{=} \PYG{n}{Symbol}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{real} \PYG{o}{=} \PYG{n+nb+bp}{True}\PYG{p}{)}\PYG{p}{;} \PYG{n}{x} | |
\PYG{g+go}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x}\PYG{o}{.}\PYG{n}{is\PYGZus{}real} | |
\PYG{g+go}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x}\PYG{o}{.}\PYG{n}{is\PYGZus{}complex} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
\subsubsection{See Also} | |
\label{modules/core:see-also} | |
\strong{See also:} | |
{\hyperref[modules/core:sympy.core.numbers.ImaginaryUnit]{\emph{\code{sympy.core.numbers.ImaginaryUnit}}}} (\autopageref*{modules/core:sympy.core.numbers.ImaginaryUnit}) | |
{\hyperref[modules/core:sympy.core.numbers.Zero]{\emph{\code{sympy.core.numbers.Zero}}}} (\autopageref*{modules/core:sympy.core.numbers.Zero}) | |
{\hyperref[modules/core:sympy.core.numbers.One]{\emph{\code{sympy.core.numbers.One}}}} (\autopageref*{modules/core:sympy.core.numbers.One}) | |
\subsubsection{Notes} | |
\label{modules/core:notes} | |
Assumption values are stored in obj.\_assumptions dictionary or | |
are returned by getter methods (with property decorators) or are | |
attributes of objects/classes. | |
\subsubsection{References} | |
\label{modules/core:references} | |
\subsection{cache} | |
\label{modules/core:module-sympy.core.cache}\label{modules/core:cache}\index{sympy.core.cache (module)} | |
\subsubsection{cacheit} | |
\label{modules/core:cacheit}\index{cacheit() (in module sympy.core.cache)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.cache.cacheit}\pysiglinewithargsret{\code{sympy.core.cache.}\bfcode{cacheit}}{\emph{func}}{} | |
\end{fulllineitems} | |
\subsection{basic} | |
\label{modules/core:module-sympy.core.basic}\label{modules/core:basic}\index{sympy.core.basic (module)} | |
\subsubsection{Basic} | |
\label{modules/core:id15}\index{Basic (class in sympy.core.basic)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.basic.Basic}\pysigline{\strong{class }\code{sympy.core.basic.}\bfcode{Basic}} | |
Base class for all objects in SymPy. | |
Conventions: | |
\begin{enumerate} | |
\item {} | |
Always use \code{.args}, when accessing parameters of some instance: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{cot} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{cot}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{args} | |
\PYG{g+go}{(x,)} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{cot}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{args}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{]} | |
\PYG{g+go}{x} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{args} | |
\PYG{g+go}{(x, y)} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{args}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{]} | |
\PYG{g+go}{y} | |
\end{Verbatim} | |
\item {} | |
Never use internal methods or variables (the ones prefixed with \code{\_}): | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{cot}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{\PYGZus{}args} \PYG{c}{\PYGZsh{} do not use this, use cot(x).args instead} | |
\PYG{g+go}{(x,)} | |
\end{Verbatim} | |
\end{enumerate} | |
\index{args (sympy.core.basic.Basic attribute)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.basic.Basic.args}\pysigline{\bfcode{args}} | |
Returns a tuple of arguments of `self'. | |
\paragraph{Notes} | |
Never use self.\_args, always use self.args. | |
Only use \_args in \_\_new\_\_ when creating a new function. | |
Don't override .args() from Basic (so that it's easy to | |
change the interface in the future if needed). | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{cot} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{cot}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{args} | |
\PYG{g+go}{(x,)} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{cot}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{args}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{]} | |
\PYG{g+go}{x} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{args} | |
\PYG{g+go}{(x, y)} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{args}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{]} | |
\PYG{g+go}{y} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{as\_content\_primitive() (sympy.core.basic.Basic method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.basic.Basic.as_content_primitive}\pysiglinewithargsret{\bfcode{as\_content\_primitive}}{\emph{radical=False}}{} | |
A stub to allow Basic args (like Tuple) to be skipped when computing | |
the content and primitive components of an expression. | |
See docstring of Expr.as\_content\_primitive | |
\end{fulllineitems} | |
\index{as\_poly() (sympy.core.basic.Basic method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.basic.Basic.as_poly}\pysiglinewithargsret{\bfcode{as\_poly}}{\emph{*gens}, \emph{**args}}{} | |
Converts \code{self} to a polynomial or returns \code{None}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{sin} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}poly}\PYG{p}{(}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{Poly(x**2 + x*y, x, y, domain=\PYGZsq{}ZZ\PYGZsq{})} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}poly}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{Poly(x**2 + x*y, x, y, domain=\PYGZsq{}ZZ\PYGZsq{})} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{n}{sin}\PYG{p}{(}\PYG{n}{y}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}poly}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{None} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{assumptions0 (sympy.core.basic.Basic attribute)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.basic.Basic.assumptions0}\pysigline{\bfcode{assumptions0}} | |
Return object \(type\) assumptions. | |
For example: | |
\begin{quote} | |
Symbol(`x', real=True) | |
Symbol(`x', integer=True) | |
\end{quote} | |
are different objects. In other words, besides Python type (Symbol in | |
this case), the initial assumptions are also forming their typeinfo. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{Symbol} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x}\PYG{o}{.}\PYG{n}{assumptions0} | |
\PYG{g+go}{\PYGZob{}\PYGZsq{}commutative\PYGZsq{}: True\PYGZcb{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x} \PYG{o}{=} \PYG{n}{Symbol}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s}{x}\PYG{l+s}{\PYGZdq{}}\PYG{p}{,} \PYG{n}{positive}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x}\PYG{o}{.}\PYG{n}{assumptions0} | |
\PYG{g+go}{\PYGZob{}\PYGZsq{}commutative\PYGZsq{}: True, \PYGZsq{}complex\PYGZsq{}: True, \PYGZsq{}hermitian\PYGZsq{}: True,} | |
\PYG{g+go}{\PYGZsq{}imaginary\PYGZsq{}: False, \PYGZsq{}negative\PYGZsq{}: False, \PYGZsq{}nonnegative\PYGZsq{}: True,} | |
\PYG{g+go}{\PYGZsq{}nonpositive\PYGZsq{}: False, \PYGZsq{}nonzero\PYGZsq{}: True, \PYGZsq{}positive\PYGZsq{}: True, \PYGZsq{}real\PYGZsq{}: True,} | |
\PYG{g+go}{\PYGZsq{}zero\PYGZsq{}: False\PYGZcb{}} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{atoms() (sympy.core.basic.Basic method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.basic.Basic.atoms}\pysiglinewithargsret{\bfcode{atoms}}{\emph{*types}}{} | |
Returns the atoms that form the current object. | |
By default, only objects that are truly atomic and can't | |
be divided into smaller pieces are returned: symbols, numbers, | |
and number symbols like I and pi. It is possible to request | |
atoms of any type, however, as demonstrated below. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{Number}\PYG{p}{,} \PYG{n}{NumberSymbol}\PYG{p}{,} \PYG{n}{Symbol} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{1} \PYG{o}{+} \PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{y} \PYG{o}{+} \PYG{n}{I}\PYG{o}{*}\PYG{n}{pi}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{atoms}\PYG{p}{(}\PYG{n}{Symbol}\PYG{p}{)} | |
\PYG{g+go}{set([x, y])} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{1} \PYG{o}{+} \PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{y} \PYG{o}{+} \PYG{n}{I}\PYG{o}{*}\PYG{n}{pi}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{atoms}\PYG{p}{(}\PYG{n}{Number}\PYG{p}{)} | |
\PYG{g+go}{set([1, 2])} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{1} \PYG{o}{+} \PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{y} \PYG{o}{+} \PYG{n}{I}\PYG{o}{*}\PYG{n}{pi}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{atoms}\PYG{p}{(}\PYG{n}{Number}\PYG{p}{,} \PYG{n}{NumberSymbol}\PYG{p}{)} | |
\PYG{g+go}{set([1, 2, pi])} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{1} \PYG{o}{+} \PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{y} \PYG{o}{+} \PYG{n}{I}\PYG{o}{*}\PYG{n}{pi}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{atoms}\PYG{p}{(}\PYG{n}{Number}\PYG{p}{,} \PYG{n}{NumberSymbol}\PYG{p}{,} \PYG{n}{I}\PYG{p}{)} | |
\PYG{g+go}{set([1, 2, I, pi])} | |
\end{Verbatim} | |
Note that I (imaginary unit) and zoo (complex infinity) are special | |
types of number symbols and are not part of the NumberSymbol class. | |
The type can be given implicitly, too: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{1} \PYG{o}{+} \PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{y} \PYG{o}{+} \PYG{n}{I}\PYG{o}{*}\PYG{n}{pi}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{atoms}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} \PYG{c}{\PYGZsh{} x is a Symbol} | |
\PYG{g+go}{set([x, y])} | |
\end{Verbatim} | |
Be careful to check your assumptions when using the implicit option | |
since \code{S(1).is\_Integer = True} but \code{type(S(1))} is \code{One}, a special type | |
of sympy atom, while \code{type(S(2))} is type \code{Integer} and will find all | |
integers in an expression: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{S} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{1} \PYG{o}{+} \PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{y} \PYG{o}{+} \PYG{n}{I}\PYG{o}{*}\PYG{n}{pi}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{atoms}\PYG{p}{(}\PYG{n}{S}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{set([1])} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{1} \PYG{o}{+} \PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{y} \PYG{o}{+} \PYG{n}{I}\PYG{o}{*}\PYG{n}{pi}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{atoms}\PYG{p}{(}\PYG{n}{S}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{set([1, 2])} | |
\end{Verbatim} | |
Finally, arguments to atoms() can select more than atomic atoms: any | |
sympy type (loaded in core/\_\_init\_\_.py) can be listed as an argument | |
and those types of ``atoms'' as found in scanning the arguments of the | |
expression recursively: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{Function}\PYG{p}{,} \PYG{n}{Mul} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.core.function} \PYG{k+kn}{import} \PYG{n}{AppliedUndef} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{f} \PYG{o}{=} \PYG{n}{Function}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{f}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{1} \PYG{o}{+} \PYG{n}{f}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{y} \PYG{o}{+} \PYG{n}{I}\PYG{o}{*}\PYG{n}{pi}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{atoms}\PYG{p}{(}\PYG{n}{Function}\PYG{p}{)} | |
\PYG{g+go}{set([f(x), sin(y + I*pi)])} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{1} \PYG{o}{+} \PYG{n}{f}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{y} \PYG{o}{+} \PYG{n}{I}\PYG{o}{*}\PYG{n}{pi}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{atoms}\PYG{p}{(}\PYG{n}{AppliedUndef}\PYG{p}{)} | |
\PYG{g+go}{set([f(x)])} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{1} \PYG{o}{+} \PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{y} \PYG{o}{+} \PYG{n}{I}\PYG{o}{*}\PYG{n}{pi}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{atoms}\PYG{p}{(}\PYG{n}{Mul}\PYG{p}{)} | |
\PYG{g+go}{set([I*pi, 2*sin(y + I*pi)])} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{canonical\_variables (sympy.core.basic.Basic attribute)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.basic.Basic.canonical_variables}\pysigline{\bfcode{canonical\_variables}} | |
Return a dictionary mapping any variable defined in | |
\code{self.variables} as underscore-suffixed numbers | |
corresponding to their position in \code{self.variables}. Enough | |
underscores are added to ensure that there will be no clash with | |
existing free symbols. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{Lambda} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Lambda}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{canonical\PYGZus{}variables} | |
\PYG{g+go}{\PYGZob{}x: 0\PYGZus{}\PYGZcb{}} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{class\_key() (sympy.core.basic.Basic class method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.basic.Basic.class_key}\pysiglinewithargsret{\strong{classmethod }\bfcode{class\_key}}{}{} | |
Nice order of classes. | |
\end{fulllineitems} | |
\index{compare() (sympy.core.basic.Basic method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.basic.Basic.compare}\pysiglinewithargsret{\bfcode{compare}}{\emph{other}}{} | |
Return -1, 0, 1 if the object is smaller, equal, or greater than other. | |
Not in the mathematical sense. If the object is of a different type | |
from the ``other'' then their classes are ordered according to | |
the sorted\_classes list. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x}\PYG{o}{.}\PYG{n}{compare}\PYG{p}{(}\PYG{n}{y}\PYG{p}{)} | |
\PYG{g+go}{\PYGZhy{}1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x}\PYG{o}{.}\PYG{n}{compare}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{0} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{y}\PYG{o}{.}\PYG{n}{compare}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{1} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{count() (sympy.core.basic.Basic method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.basic.Basic.count}\pysiglinewithargsret{\bfcode{count}}{\emph{query}}{} | |
Count the number of matching subexpressions. | |
\end{fulllineitems} | |
\index{count\_ops() (sympy.core.basic.Basic method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.basic.Basic.count_ops}\pysiglinewithargsret{\bfcode{count\_ops}}{\emph{visual=None}}{} | |
wrapper for count\_ops that returns the operation count. | |
\end{fulllineitems} | |
\index{doit() (sympy.core.basic.Basic method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.basic.Basic.doit}\pysiglinewithargsret{\bfcode{doit}}{\emph{**hints}}{} | |
Evaluate objects that are not evaluated by default like limits, | |
integrals, sums and products. All objects of this kind will be | |
evaluated recursively, unless some species were excluded via `hints' | |
or unless the `deep' hint was set to `False'. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{Integral} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{Integral}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{2*Integral(x, x)} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{Integral}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{doit}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{x**2} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{Integral}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{doit}\PYG{p}{(}\PYG{n}{deep} \PYG{o}{=} \PYG{n+nb+bp}{False}\PYG{p}{)} | |
\PYG{g+go}{2*Integral(x, x)} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{dummy\_eq() (sympy.core.basic.Basic method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.basic.Basic.dummy_eq}\pysiglinewithargsret{\bfcode{dummy\_eq}}{\emph{other}, \emph{symbol=None}}{} | |
Compare two expressions and handle dummy symbols. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{Dummy} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{u} \PYG{o}{=} \PYG{n}{Dummy}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{u}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{u}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{.}\PYG{n}{dummy\PYGZus{}eq}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+go}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{u}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)} \PYG{o}{==} \PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+go}{False} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{u}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{dummy\PYGZus{}eq}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{n}{y}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{u}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{dummy\PYGZus{}eq}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{n}{y}\PYG{p}{,} \PYG{n}{y}\PYG{p}{)} | |
\PYG{g+go}{False} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{find() (sympy.core.basic.Basic method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.basic.Basic.find}\pysiglinewithargsret{\bfcode{find}}{\emph{query}, \emph{group=False}}{} | |
Find all subexpressions matching a query. | |
\end{fulllineitems} | |
\index{free\_symbols (sympy.core.basic.Basic attribute)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.basic.Basic.free_symbols}\pysigline{\bfcode{free\_symbols}} | |
Return from the atoms of self those which are free symbols. | |
For most expressions, all symbols are free symbols. For some classes | |
this is not true. e.g. Integrals use Symbols for the dummy variables | |
which are bound variables, so Integral has a method to return all symbols | |
except those. Derivative keeps track of symbols with respect to which it | |
will perform a derivative; those are bound variables, too, so it has | |
its own symbols method. | |
Any other method that uses bound variables should implement a symbols | |
method. | |
\end{fulllineitems} | |
\index{fromiter() (sympy.core.basic.Basic class method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.basic.Basic.fromiter}\pysiglinewithargsret{\strong{classmethod }\bfcode{fromiter}}{\emph{args}, \emph{**assumptions}}{} | |
Create a new object from an iterable. | |
This is a convenience function that allows one to create objects from | |
any iterable, without having to convert to a list or tuple first. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{Tuple} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Tuple}\PYG{o}{.}\PYG{n}{fromiter}\PYG{p}{(}\PYG{n}{i} \PYG{k}{for} \PYG{n}{i} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{l+m+mi}{5}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{(0, 1, 2, 3, 4)} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{func (sympy.core.basic.Basic attribute)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.basic.Basic.func}\pysigline{\bfcode{func}} | |
The top-level function in an expression. | |
The following should hold for all objects: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYGZgt{}\PYGZgt{} x == x.func(*x.args) | |
\end{Verbatim} | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a} \PYG{o}{=} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a}\PYG{o}{.}\PYG{n}{func} | |
\PYG{g+go}{\PYGZlt{}class \PYGZsq{}sympy.core.mul.Mul\PYGZsq{}\PYGZgt{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a}\PYG{o}{.}\PYG{n}{args} | |
\PYG{g+go}{(2, x)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a}\PYG{o}{.}\PYG{n}{func}\PYG{p}{(}\PYG{o}{*}\PYG{n}{a}\PYG{o}{.}\PYG{n}{args}\PYG{p}{)} | |
\PYG{g+go}{2*x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a} \PYG{o}{==} \PYG{n}{a}\PYG{o}{.}\PYG{n}{func}\PYG{p}{(}\PYG{o}{*}\PYG{n}{a}\PYG{o}{.}\PYG{n}{args}\PYG{p}{)} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{has() (sympy.core.basic.Basic method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.basic.Basic.has}\pysiglinewithargsret{\bfcode{has}}{\emph{*args}, \emph{**kwargs}}{} | |
Test whether any subexpression matches any of the patterns. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{sin} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{z} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{has}\PYG{p}{(}\PYG{n}{z}\PYG{p}{)} | |
\PYG{g+go}{False} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{has}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{z}\PYG{p}{)} | |
\PYG{g+go}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x}\PYG{o}{.}\PYG{n}{has}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
Note that \code{expr.has(*patterns)} is exactly equivalent to | |
\code{any(expr.has(p) for p in patterns)}. In particular, \code{False} is | |
returned when the list of patterns is empty. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x}\PYG{o}{.}\PYG{n}{has}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{False} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{is\_comparable (sympy.core.basic.Basic attribute)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.basic.Basic.is_comparable}\pysigline{\bfcode{is\_comparable}} | |
Return True if self can be computed to a real number | |
with precision, else False. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{exp\PYGZus{}polar}\PYG{p}{,} \PYG{n}{pi}\PYG{p}{,} \PYG{n}{I} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{I}\PYG{o}{*}\PYG{n}{exp\PYGZus{}polar}\PYG{p}{(}\PYG{n}{I}\PYG{o}{*}\PYG{n}{pi}\PYG{o}{/}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{is\PYGZus{}comparable} | |
\PYG{g+go}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{I}\PYG{o}{*}\PYG{n}{exp\PYGZus{}polar}\PYG{p}{(}\PYG{n}{I}\PYG{o}{*}\PYG{n}{pi}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{is\PYGZus{}comparable} | |
\PYG{g+go}{False} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{iter\_basic\_args() (sympy.core.basic.Basic method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.basic.Basic.iter_basic_args}\pysiglinewithargsret{\bfcode{iter\_basic\_args}}{\emph{*args}, \emph{**kwargs}}{} | |
Iterates arguments of \code{self}. | |
\end{fulllineitems} | |
\index{match() (sympy.core.basic.Basic method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.basic.Basic.match}\pysiglinewithargsret{\bfcode{match}}{\emph{pattern}, \emph{old=False}}{} | |
Pattern matching. | |
Wild symbols match all. | |
Return \code{None} when expression (self) does not match | |
with pattern. Otherwise return a dictionary such that: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{n}{pattern}\PYG{o}{.}\PYG{n}{xreplace}\PYG{p}{(}\PYG{n+nb+bp}{self}\PYG{o}{.}\PYG{n}{match}\PYG{p}{(}\PYG{n}{pattern}\PYG{p}{)}\PYG{p}{)} \PYG{o}{==} \PYG{n+nb+bp}{self} | |
\end{Verbatim} | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{Wild} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{Wild}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s}{p}\PYG{l+s}{\PYGZdq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{q} \PYG{o}{=} \PYG{n}{Wild}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s}{q}\PYG{l+s}{\PYGZdq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{r} \PYG{o}{=} \PYG{n}{Wild}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s}{r}\PYG{l+s}{\PYGZdq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e} \PYG{o}{=} \PYG{p}{(}\PYG{n}{x}\PYG{o}{+}\PYG{n}{y}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{p}{(}\PYG{n}{x}\PYG{o}{+}\PYG{n}{y}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e}\PYG{o}{.}\PYG{n}{match}\PYG{p}{(}\PYG{n}{p}\PYG{o}{*}\PYG{o}{*}\PYG{n}{p}\PYG{p}{)} | |
\PYG{g+go}{\PYGZob{}p\PYGZus{}: x + y\PYGZcb{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e}\PYG{o}{.}\PYG{n}{match}\PYG{p}{(}\PYG{n}{p}\PYG{o}{*}\PYG{o}{*}\PYG{n}{q}\PYG{p}{)} | |
\PYG{g+go}{\PYGZob{}p\PYGZus{}: x + y, q\PYGZus{}: x + y\PYGZcb{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e} \PYG{o}{=} \PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e}\PYG{o}{.}\PYG{n}{match}\PYG{p}{(}\PYG{n}{p}\PYG{o}{*}\PYG{n}{q}\PYG{o}{*}\PYG{o}{*}\PYG{n}{r}\PYG{p}{)} | |
\PYG{g+go}{\PYGZob{}p\PYGZus{}: 4, q\PYGZus{}: x, r\PYGZus{}: 2\PYGZcb{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{p}\PYG{o}{*}\PYG{n}{q}\PYG{o}{*}\PYG{o}{*}\PYG{n}{r}\PYG{p}{)}\PYG{o}{.}\PYG{n}{xreplace}\PYG{p}{(}\PYG{n}{e}\PYG{o}{.}\PYG{n}{match}\PYG{p}{(}\PYG{n}{p}\PYG{o}{*}\PYG{n}{q}\PYG{o}{*}\PYG{o}{*}\PYG{n}{r}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{4*x**2} | |
\end{Verbatim} | |
The \code{old} flag will give the old-style pattern matching where | |
expressions and patterns are essentially solved to give the | |
match. Both of the following give None unless \code{old=True}: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{x} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{.}\PYG{n}{match}\PYG{p}{(}\PYG{n}{p} \PYG{o}{\PYGZhy{}} \PYG{n}{x}\PYG{p}{,} \PYG{n}{old}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{\PYGZob{}p\PYGZus{}: 2*x \PYGZhy{} 2\PYGZcb{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{/}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{match}\PYG{p}{(}\PYG{n}{p}\PYG{o}{*}\PYG{n}{x}\PYG{p}{,} \PYG{n}{old}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{\PYGZob{}p\PYGZus{}: 2/x**2\PYGZcb{}} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{matches() (sympy.core.basic.Basic method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.basic.Basic.matches}\pysiglinewithargsret{\bfcode{matches}}{\emph{expr}, \emph{repl\_dict=\{\}}, \emph{old=False}}{} | |
Helper method for match() that looks for a match between Wild symbols | |
in self and expressions in expr. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{symbols}\PYG{p}{,} \PYG{n}{Wild}\PYG{p}{,} \PYG{n}{Basic} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a}\PYG{p}{,} \PYG{n}{b}\PYG{p}{,} \PYG{n}{c} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{a b c}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x} \PYG{o}{=} \PYG{n}{Wild}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Basic}\PYG{p}{(}\PYG{n}{a} \PYG{o}{+} \PYG{n}{x}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{matches}\PYG{p}{(}\PYG{n}{Basic}\PYG{p}{(}\PYG{n}{a} \PYG{o}{+} \PYG{n}{b}\PYG{p}{,} \PYG{n}{c}\PYG{p}{)}\PYG{p}{)} \PYG{o+ow}{is} \PYG{n+nb+bp}{None} | |
\PYG{g+go}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Basic}\PYG{p}{(}\PYG{n}{a} \PYG{o}{+} \PYG{n}{x}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{matches}\PYG{p}{(}\PYG{n}{Basic}\PYG{p}{(}\PYG{n}{a} \PYG{o}{+} \PYG{n}{b} \PYG{o}{+} \PYG{n}{c}\PYG{p}{,} \PYG{n}{b} \PYG{o}{+} \PYG{n}{c}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{\PYGZob{}x\PYGZus{}: b + c\PYGZcb{}} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{rcall() (sympy.core.basic.Basic method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.basic.Basic.rcall}\pysiglinewithargsret{\bfcode{rcall}}{\emph{*args}}{} | |
Apply on the argument recursively through the expression tree. | |
This method is used to simulate a common abuse of notation for | |
operators. For instance in SymPy the the following will not work: | |
\code{(x+Lambda(y, 2*y))(z) == x+2*z}, | |
however you can use | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{Lambda} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x}\PYG{p}{,}\PYG{n}{y}\PYG{p}{,}\PYG{n}{z} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{n}{Lambda}\PYG{p}{(}\PYG{n}{y}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{rcall}\PYG{p}{(}\PYG{n}{z}\PYG{p}{)} | |
\PYG{g+go}{x + 2*z} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{replace() (sympy.core.basic.Basic method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.basic.Basic.replace}\pysiglinewithargsret{\bfcode{replace}}{\emph{query}, \emph{value}, \emph{map=False}, \emph{simultaneous=True}, \emph{exact=False}}{} | |
Replace matching subexpressions of \code{self} with \code{value}. | |
If \code{map = True} then also return the mapping \{old: new\} where \code{old} | |
was a sub-expression found with query and \code{new} is the replacement | |
value for it. If the expression itself doesn't match the query, then | |
the returned value will be \code{self.xreplace(map)} otherwise it should | |
be \code{self.subs(ordered(map.items()))}. | |
Traverses an expression tree and performs replacement of matching | |
subexpressions from the bottom to the top of the tree. The default | |
approach is to do the replacement in a simultaneous fashion so | |
changes made are targeted only once. If this is not desired or causes | |
problems, \code{simultaneous} can be set to False. In addition, if an | |
expression containing more than one Wild symbol is being used to match | |
subexpressions and the \code{exact} flag is True, then the match will only | |
succeed if non-zero values are received for each Wild that appears in | |
the match pattern. | |
The list of possible combinations of queries and replacement values | |
is listed below: | |
\strong{See also:} | |
\begin{description} | |
\item[{{\hyperref[modules/core:sympy.core.basic.Basic.subs]{\emph{\code{subs}}}} (\autopageref*{modules/core:sympy.core.basic.Basic.subs})}] \leavevmode | |
substitution of subexpressions as defined by the objects themselves. | |
\item[{{\hyperref[modules/core:sympy.core.basic.Basic.xreplace]{\emph{\code{xreplace}}}} (\autopageref*{modules/core:sympy.core.basic.Basic.xreplace})}] \leavevmode | |
exact node replacement in expr tree; also capable of using matching rules | |
\end{description} | |
\paragraph{Examples} | |
Initial setup | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{log}\PYG{p}{,} \PYG{n}{sin}\PYG{p}{,} \PYG{n}{cos}\PYG{p}{,} \PYG{n}{tan}\PYG{p}{,} \PYG{n}{Wild}\PYG{p}{,} \PYG{n}{Mul}\PYG{p}{,} \PYG{n}{Add} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{f} \PYG{o}{=} \PYG{n}{log}\PYG{p}{(}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{)} \PYG{o}{+} \PYG{n}{tan}\PYG{p}{(}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{)} | |
\end{Verbatim} | |
\begin{description} | |
\item[{1.1. type -\textgreater{} type}] \leavevmode | |
obj.replace(type, newtype) | |
When object of type \code{type} is found, replace it with the | |
result of passing its argument(s) to \code{newtype}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{f}\PYG{o}{.}\PYG{n}{replace}\PYG{p}{(}\PYG{n}{sin}\PYG{p}{,} \PYG{n}{cos}\PYG{p}{)} | |
\PYG{g+go}{log(cos(x)) + tan(cos(x**2))} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{replace}\PYG{p}{(}\PYG{n}{sin}\PYG{p}{,} \PYG{n}{cos}\PYG{p}{,} \PYG{n+nb}{map}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{(cos(x), \PYGZob{}sin(x): cos(x)\PYGZcb{})} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{replace}\PYG{p}{(}\PYG{n}{Mul}\PYG{p}{,} \PYG{n}{Add}\PYG{p}{)} | |
\PYG{g+go}{x + y} | |
\end{Verbatim} | |
\item[{1.2. type -\textgreater{} func}] \leavevmode | |
obj.replace(type, func) | |
When object of type \code{type} is found, apply \code{func} to its | |
argument(s). \code{func} must be written to handle the number | |
of arguments of \code{type}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{f}\PYG{o}{.}\PYG{n}{replace}\PYG{p}{(}\PYG{n}{sin}\PYG{p}{,} \PYG{k}{lambda} \PYG{n}{arg}\PYG{p}{:} \PYG{n}{sin}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{arg}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{log(sin(2*x)) + tan(sin(2*x**2))} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{replace}\PYG{p}{(}\PYG{n}{Mul}\PYG{p}{,} \PYG{k}{lambda} \PYG{o}{*}\PYG{n}{args}\PYG{p}{:} \PYG{n}{sin}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{Mul}\PYG{p}{(}\PYG{o}{*}\PYG{n}{args}\PYG{p}{)}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{sin(2*x*y)} | |
\end{Verbatim} | |
\item[{2.1. pattern -\textgreater{} expr}] \leavevmode | |
obj.replace(pattern(wild), expr(wild)) | |
Replace subexpressions matching \code{pattern} with the expression | |
written in terms of the Wild symbols in \code{pattern}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a} \PYG{o}{=} \PYG{n}{Wild}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{a}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{f}\PYG{o}{.}\PYG{n}{replace}\PYG{p}{(}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{a}\PYG{p}{)}\PYG{p}{,} \PYG{n}{tan}\PYG{p}{(}\PYG{n}{a}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{log(tan(x)) + tan(tan(x**2))} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{f}\PYG{o}{.}\PYG{n}{replace}\PYG{p}{(}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{a}\PYG{p}{)}\PYG{p}{,} \PYG{n}{tan}\PYG{p}{(}\PYG{n}{a}\PYG{o}{/}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{log(tan(x/2)) + tan(tan(x**2/2))} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{f}\PYG{o}{.}\PYG{n}{replace}\PYG{p}{(}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{a}\PYG{p}{)}\PYG{p}{,} \PYG{n}{a}\PYG{p}{)} | |
\PYG{g+go}{log(x) + tan(x**2)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{replace}\PYG{p}{(}\PYG{n}{a}\PYG{o}{*}\PYG{n}{x}\PYG{p}{,} \PYG{n}{a}\PYG{p}{)} | |
\PYG{g+go}{y} | |
\end{Verbatim} | |
When the default value of False is used with patterns that have | |
more than one Wild symbol, non-intuitive results may be obtained: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{b} \PYG{o}{=} \PYG{n}{Wild}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{b}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{replace}\PYG{p}{(}\PYG{n}{a}\PYG{o}{*}\PYG{n}{x} \PYG{o}{+} \PYG{n}{b}\PYG{p}{,} \PYG{n}{b} \PYG{o}{\PYGZhy{}} \PYG{n}{a}\PYG{p}{)} | |
\PYG{g+go}{2/x} | |
\end{Verbatim} | |
For this reason, the \code{exact} option can be used to make the | |
replacement only when the match gives non-zero values for all | |
Wild symbols: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x} \PYG{o}{+} \PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{replace}\PYG{p}{(}\PYG{n}{a}\PYG{o}{*}\PYG{n}{x} \PYG{o}{+} \PYG{n}{b}\PYG{p}{,} \PYG{n}{b} \PYG{o}{\PYGZhy{}} \PYG{n}{a}\PYG{p}{,} \PYG{n}{exact}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{y \PYGZhy{} 2} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{replace}\PYG{p}{(}\PYG{n}{a}\PYG{o}{*}\PYG{n}{x} \PYG{o}{+} \PYG{n}{b}\PYG{p}{,} \PYG{n}{b} \PYG{o}{\PYGZhy{}} \PYG{n}{a}\PYG{p}{,} \PYG{n}{exact}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{2*x} | |
\end{Verbatim} | |
\item[{2.2. pattern -\textgreater{} func}] \leavevmode | |
obj.replace(pattern(wild), lambda wild: expr(wild)) | |
All behavior is the same as in 2.1 but now a function in terms of | |
pattern variables is used rather than an expression: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{f}\PYG{o}{.}\PYG{n}{replace}\PYG{p}{(}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{a}\PYG{p}{)}\PYG{p}{,} \PYG{k}{lambda} \PYG{n}{a}\PYG{p}{:} \PYG{n}{sin}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{a}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{log(sin(2*x)) + tan(sin(2*x**2))} | |
\end{Verbatim} | |
\item[{3.1. func -\textgreater{} func}] \leavevmode | |
obj.replace(filter, func) | |
Replace subexpression \code{e} with \code{func(e)} if \code{filter(e)} | |
is True. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{g} \PYG{o}{=} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{3}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{g}\PYG{o}{.}\PYG{n}{replace}\PYG{p}{(}\PYG{k}{lambda} \PYG{n}{expr}\PYG{p}{:} \PYG{n}{expr}\PYG{o}{.}\PYG{n}{is\PYGZus{}Number}\PYG{p}{,} \PYG{k}{lambda} \PYG{n}{expr}\PYG{p}{:} \PYG{n}{expr}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+go}{4*sin(x**9)} | |
\end{Verbatim} | |
\end{description} | |
The expression itself is also targeted by the query but is done in | |
such a fashion that changes are not made twice. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e} \PYG{o}{=} \PYG{n}{x}\PYG{o}{*}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{n}{y} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e}\PYG{o}{.}\PYG{n}{replace}\PYG{p}{(}\PYG{k}{lambda} \PYG{n}{x}\PYG{p}{:} \PYG{n}{x}\PYG{o}{.}\PYG{n}{is\PYGZus{}Mul}\PYG{p}{,} \PYG{k}{lambda} \PYG{n}{x}\PYG{p}{:} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{2*x*(2*x*y + 1)} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{rewrite() (sympy.core.basic.Basic method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.basic.Basic.rewrite}\pysiglinewithargsret{\bfcode{rewrite}}{\emph{*args}, \emph{**hints}}{} | |
Rewrite functions in terms of other functions. | |
Rewrites expression containing applications of functions | |
of one kind in terms of functions of different kind. For | |
example you can rewrite trigonometric functions as complex | |
exponentials or combinatorial functions as gamma function. | |
As a pattern this function accepts a list of functions to | |
to rewrite (instances of DefinedFunction class). As rule | |
you can use string or a destination function instance (in | |
this case rewrite() will use the str() function). | |
There is also the possibility to pass hints on how to rewrite | |
the given expressions. For now there is only one such hint | |
defined called `deep'. When `deep' is set to False it will | |
forbid functions to rewrite their contents. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{sin}\PYG{p}{,} \PYG{n}{exp} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x} | |
\end{Verbatim} | |
Unspecified pattern: | |
\textgreater{}\textgreater{}\textgreater{} sin(x).rewrite(exp) | |
-I*(exp(I*x) - exp(-I*x))/2 | |
Pattern as a single function: | |
\textgreater{}\textgreater{}\textgreater{} sin(x).rewrite(sin, exp) | |
-I*(exp(I*x) - exp(-I*x))/2 | |
Pattern as a list of functions: | |
\textgreater{}\textgreater{}\textgreater{} sin(x).rewrite({[}sin, {]}, exp) | |
-I*(exp(I*x) - exp(-I*x))/2 | |
\end{fulllineitems} | |
\index{sort\_key() (sympy.core.basic.Basic method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.basic.Basic.sort_key}\pysiglinewithargsret{\bfcode{sort\_key}}{\emph{*args}, \emph{**kwargs}}{} | |
Return a sort key. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.core} \PYG{k+kn}{import} \PYG{n}{S}\PYG{p}{,} \PYG{n}{I} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb}{sorted}\PYG{p}{(}\PYG{p}{[}\PYG{n}{S}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{/}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{n}{I}\PYG{p}{,} \PYG{o}{\PYGZhy{}}\PYG{n}{I}\PYG{p}{]}\PYG{p}{,} \PYG{n}{key}\PYG{o}{=}\PYG{k}{lambda} \PYG{n}{x}\PYG{p}{:} \PYG{n}{x}\PYG{o}{.}\PYG{n}{sort\PYGZus{}key}\PYG{p}{(}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{[1/2, \PYGZhy{}I, I]} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{S}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s}{[x, 1/x, 1/x**2, x**2, x**(1/2), x**(1/4), x**(3/2)]}\PYG{l+s}{\PYGZdq{}}\PYG{p}{)} | |
\PYG{g+go}{[x, 1/x, x**(\PYGZhy{}2), x**2, sqrt(x), x**(1/4), x**(3/2)]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb}{sorted}\PYG{p}{(}\PYG{n}{\PYGZus{}}\PYG{p}{,} \PYG{n}{key}\PYG{o}{=}\PYG{k}{lambda} \PYG{n}{x}\PYG{p}{:} \PYG{n}{x}\PYG{o}{.}\PYG{n}{sort\PYGZus{}key}\PYG{p}{(}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{[x**(\PYGZhy{}2), 1/x, x**(1/4), sqrt(x), x, x**(3/2), x**2]} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{subs() (sympy.core.basic.Basic method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.basic.Basic.subs}\pysiglinewithargsret{\bfcode{subs}}{\emph{*args}, \emph{**kwargs}}{} | |
Substitutes old for new in an expression after sympifying args. | |
\begin{description} | |
\item[{\(args\) is either:}] \leavevmode\begin{itemize} | |
\item {} | |
two arguments, e.g. foo.subs(old, new) | |
\item {} \begin{description} | |
\item[{one iterable argument, e.g. foo.subs(iterable). The iterable may be}] \leavevmode\begin{description} | |
\item[{o an iterable container with (old, new) pairs. In this case the}] \leavevmode | |
replacements are processed in the order given with successive | |
patterns possibly affecting replacements already made. | |
\item[{o a dict or set whose key/value items correspond to old/new pairs.}] \leavevmode | |
In this case the old/new pairs will be sorted by op count and in | |
case of a tie, by number of args and the default\_sort\_key. The | |
resulting sorted list is then processed as an iterable container | |
(see previous). | |
\end{description} | |
\end{description} | |
\end{itemize} | |
\end{description} | |
If the keyword \code{simultaneous} is True, the subexpressions will not be | |
evaluated until all the substitutions have been made. | |
\strong{See also:} | |
\begin{description} | |
\item[{{\hyperref[modules/core:sympy.core.basic.Basic.replace]{\emph{\code{replace}}}} (\autopageref*{modules/core:sympy.core.basic.Basic.replace})}] \leavevmode | |
replacement capable of doing wildcard-like matching, parsing of match, and conditional replacements | |
\item[{{\hyperref[modules/core:sympy.core.basic.Basic.xreplace]{\emph{\code{xreplace}}}} (\autopageref*{modules/core:sympy.core.basic.Basic.xreplace})}] \leavevmode | |
exact node replacement in expr tree; also capable of using matching rules | |
\item[{\code{evalf}}] \leavevmode | |
calculates the given formula to a desired level of precision | |
\end{description} | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{pi}\PYG{p}{,} \PYG{n}{exp}\PYG{p}{,} \PYG{n}{limit}\PYG{p}{,} \PYG{n}{oo} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{1} \PYG{o}{+} \PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{subs}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{pi}\PYG{p}{)} | |
\PYG{g+go}{pi*y + 1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{1} \PYG{o}{+} \PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{subs}\PYG{p}{(}\PYG{p}{\PYGZob{}}\PYG{n}{x}\PYG{p}{:}\PYG{n}{pi}\PYG{p}{,} \PYG{n}{y}\PYG{p}{:}\PYG{l+m+mi}{2}\PYG{p}{\PYGZcb{}}\PYG{p}{)} | |
\PYG{g+go}{1 + 2*pi} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{1} \PYG{o}{+} \PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{subs}\PYG{p}{(}\PYG{p}{[}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{pi}\PYG{p}{)}\PYG{p}{,} \PYG{p}{(}\PYG{n}{y}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+go}{1 + 2*pi} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{reps} \PYG{o}{=} \PYG{p}{[}\PYG{p}{(}\PYG{n}{y}\PYG{p}{,} \PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{,} \PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{subs}\PYG{p}{(}\PYG{n}{reps}\PYG{p}{)} | |
\PYG{g+go}{6} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{subs}\PYG{p}{(}\PYG{n+nb}{reversed}\PYG{p}{(}\PYG{n}{reps}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{x**2 + 2} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{4}\PYG{p}{)}\PYG{o}{.}\PYG{n}{subs}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{n}{y}\PYG{p}{)} | |
\PYG{g+go}{y**2 + y} | |
\end{Verbatim} | |
To replace only the x**2 but not the x**4, use xreplace: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{4}\PYG{p}{)}\PYG{o}{.}\PYG{n}{xreplace}\PYG{p}{(}\PYG{p}{\PYGZob{}}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{:} \PYG{n}{y}\PYG{p}{\PYGZcb{}}\PYG{p}{)} | |
\PYG{g+go}{x**4 + y} | |
\end{Verbatim} | |
To delay evaluation until all substitutions have been made, | |
set the keyword \code{simultaneous} to True: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{x}\PYG{o}{/}\PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{subs}\PYG{p}{(}\PYG{p}{[}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{)}\PYG{p}{,} \PYG{p}{(}\PYG{n}{y}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{)}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+go}{0} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{x}\PYG{o}{/}\PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{subs}\PYG{p}{(}\PYG{p}{[}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{)}\PYG{p}{,} \PYG{p}{(}\PYG{n}{y}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{)}\PYG{p}{]}\PYG{p}{,} \PYG{n}{simultaneous}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{nan} | |
\end{Verbatim} | |
This has the added feature of not allowing subsequent substitutions | |
to affect those already made: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{n}{y}\PYG{p}{)}\PYG{o}{/}\PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{subs}\PYG{p}{(}\PYG{p}{\PYGZob{}}\PYG{n}{x} \PYG{o}{+} \PYG{n}{y}\PYG{p}{:} \PYG{n}{y}\PYG{p}{,} \PYG{n}{y}\PYG{p}{:} \PYG{n}{x} \PYG{o}{+} \PYG{n}{y}\PYG{p}{\PYGZcb{}}\PYG{p}{)} | |
\PYG{g+go}{1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{n}{y}\PYG{p}{)}\PYG{o}{/}\PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{subs}\PYG{p}{(}\PYG{p}{\PYGZob{}}\PYG{n}{x} \PYG{o}{+} \PYG{n}{y}\PYG{p}{:} \PYG{n}{y}\PYG{p}{,} \PYG{n}{y}\PYG{p}{:} \PYG{n}{x} \PYG{o}{+} \PYG{n}{y}\PYG{p}{\PYGZcb{}}\PYG{p}{,} \PYG{n}{simultaneous}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{y/(x + y)} | |
\end{Verbatim} | |
In order to obtain a canonical result, unordered iterables are | |
sorted by count\_op length, number of arguments and by the | |
default\_sort\_key to break any ties. All other iterables are left | |
unsorted. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{sqrt}\PYG{p}{,} \PYG{n}{sin}\PYG{p}{,} \PYG{n}{cos} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{a}\PYG{p}{,} \PYG{n}{b}\PYG{p}{,} \PYG{n}{c}\PYG{p}{,} \PYG{n}{d}\PYG{p}{,} \PYG{n}{e} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{A} \PYG{o}{=} \PYG{p}{(}\PYG{n}{sqrt}\PYG{p}{(}\PYG{n}{sin}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)}\PYG{p}{)}\PYG{p}{,} \PYG{n}{a}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{B} \PYG{o}{=} \PYG{p}{(}\PYG{n}{sin}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)}\PYG{p}{,} \PYG{n}{b}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{C} \PYG{o}{=} \PYG{p}{(}\PYG{n}{cos}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)}\PYG{p}{,} \PYG{n}{c}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{D} \PYG{o}{=} \PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{d}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{E} \PYG{o}{=} \PYG{p}{(}\PYG{n}{exp}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{,} \PYG{n}{e}\PYG{p}{)} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr} \PYG{o}{=} \PYG{n}{sqrt}\PYG{p}{(}\PYG{n}{sin}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)}\PYG{p}{)}\PYG{o}{*}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{exp}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{n}{cos}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)} \PYG{o}{+} \PYG{n}{sin}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expr}\PYG{o}{.}\PYG{n}{subs}\PYG{p}{(}\PYG{n+nb}{dict}\PYG{p}{(}\PYG{p}{[}\PYG{n}{A}\PYG{p}{,}\PYG{n}{B}\PYG{p}{,}\PYG{n}{C}\PYG{p}{,}\PYG{n}{D}\PYG{p}{,}\PYG{n}{E}\PYG{p}{]}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{a*c*sin(d*e) + b} | |
\end{Verbatim} | |
The resulting expression represents a literal replacement of the | |
old arguments with the new arguments. This may not reflect the | |
limiting behavior of the expression: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{3} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{subs}\PYG{p}{(}\PYG{p}{\PYGZob{}}\PYG{n}{x}\PYG{p}{:} \PYG{n}{oo}\PYG{p}{\PYGZcb{}}\PYG{p}{)} | |
\PYG{g+go}{nan} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{limit}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{3} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{x}\PYG{p}{,} \PYG{n}{x}\PYG{p}{,} \PYG{n}{oo}\PYG{p}{)} | |
\PYG{g+go}{oo} | |
\end{Verbatim} | |
If the substitution will be followed by numerical | |
evaluation, it is better to pass the substitution to | |
evalf as | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{o}{/}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{evalf}\PYG{p}{(}\PYG{n}{subs}\PYG{o}{=}\PYG{p}{\PYGZob{}}\PYG{n}{x}\PYG{p}{:} \PYG{l+m+mf}{3.0}\PYG{p}{\PYGZcb{}}\PYG{p}{,} \PYG{n}{n}\PYG{o}{=}\PYG{l+m+mi}{21}\PYG{p}{)} | |
\PYG{g+go}{0.333333333333333333333} | |
\end{Verbatim} | |
rather than | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{o}{/}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{subs}\PYG{p}{(}\PYG{p}{\PYGZob{}}\PYG{n}{x}\PYG{p}{:} \PYG{l+m+mf}{3.0}\PYG{p}{\PYGZcb{}}\PYG{p}{)}\PYG{o}{.}\PYG{n}{evalf}\PYG{p}{(}\PYG{l+m+mi}{21}\PYG{p}{)} | |
\PYG{g+go}{0.333333333333333314830} | |
\end{Verbatim} | |
as the former will ensure that the desired level of precision is | |
obtained. | |
\end{fulllineitems} | |
\index{xreplace() (sympy.core.basic.Basic method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.basic.Basic.xreplace}\pysiglinewithargsret{\bfcode{xreplace}}{\emph{rule}}{} | |
Replace occurrences of objects within the expression. | |
\begin{quote}\begin{description} | |
\item[{Parameters}] \leavevmode | |
\textbf{rule} : dict-like | |
\begin{quote} | |
Expresses a replacement rule | |
\end{quote} | |
\item[{Returns}] \leavevmode | |
\textbf{xreplace} : the result of the replacement | |
\end{description}\end{quote} | |
\strong{See also:} | |
\begin{description} | |
\item[{{\hyperref[modules/core:sympy.core.basic.Basic.replace]{\emph{\code{replace}}}} (\autopageref*{modules/core:sympy.core.basic.Basic.replace})}] \leavevmode | |
replacement capable of doing wildcard-like matching, parsing of match, and conditional replacements | |
\item[{{\hyperref[modules/core:sympy.core.basic.Basic.subs]{\emph{\code{subs}}}} (\autopageref*{modules/core:sympy.core.basic.Basic.subs})}] \leavevmode | |
substitution of subexpressions as defined by the objects themselves. | |
\end{description} | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{symbols}\PYG{p}{,} \PYG{n}{pi}\PYG{p}{,} \PYG{n}{exp} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{z} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x y z}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{1} \PYG{o}{+} \PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{xreplace}\PYG{p}{(}\PYG{p}{\PYGZob{}}\PYG{n}{x}\PYG{p}{:} \PYG{n}{pi}\PYG{p}{\PYGZcb{}}\PYG{p}{)} | |
\PYG{g+go}{pi*y + 1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{1} \PYG{o}{+} \PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{xreplace}\PYG{p}{(}\PYG{p}{\PYGZob{}}\PYG{n}{x}\PYG{p}{:}\PYG{n}{pi}\PYG{p}{,} \PYG{n}{y}\PYG{p}{:}\PYG{l+m+mi}{2}\PYG{p}{\PYGZcb{}}\PYG{p}{)} | |
\PYG{g+go}{1 + 2*pi} | |
\end{Verbatim} | |
Replacements occur only if an entire node in the expression tree is | |
matched: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{n}{y} \PYG{o}{+} \PYG{n}{z}\PYG{p}{)}\PYG{o}{.}\PYG{n}{xreplace}\PYG{p}{(}\PYG{p}{\PYGZob{}}\PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{p}{:} \PYG{n}{pi}\PYG{p}{\PYGZcb{}}\PYG{p}{)} | |
\PYG{g+go}{z + pi} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{o}{*}\PYG{n}{z}\PYG{p}{)}\PYG{o}{.}\PYG{n}{xreplace}\PYG{p}{(}\PYG{p}{\PYGZob{}}\PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{p}{:} \PYG{n}{pi}\PYG{p}{\PYGZcb{}}\PYG{p}{)} | |
\PYG{g+go}{x*y*z} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{xreplace}\PYG{p}{(}\PYG{p}{\PYGZob{}}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{p}{:} \PYG{n}{y}\PYG{p}{,} \PYG{n}{x}\PYG{p}{:} \PYG{n}{z}\PYG{p}{\PYGZcb{}}\PYG{p}{)} | |
\PYG{g+go}{y} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{xreplace}\PYG{p}{(}\PYG{p}{\PYGZob{}}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{p}{:} \PYG{n}{y}\PYG{p}{,} \PYG{n}{x}\PYG{p}{:} \PYG{n}{z}\PYG{p}{\PYGZcb{}}\PYG{p}{)} | |
\PYG{g+go}{4*z} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{n}{y} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{.}\PYG{n}{xreplace}\PYG{p}{(}\PYG{p}{\PYGZob{}}\PYG{n}{x} \PYG{o}{+} \PYG{n}{y}\PYG{p}{:} \PYG{l+m+mi}{2}\PYG{p}{\PYGZcb{}}\PYG{p}{)} | |
\PYG{g+go}{x + y + 2} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{2} \PYG{o}{+} \PYG{n}{exp}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{xreplace}\PYG{p}{(}\PYG{p}{\PYGZob{}}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{p}{:} \PYG{n}{y}\PYG{p}{\PYGZcb{}}\PYG{p}{)} | |
\PYG{g+go}{x + exp(y) + 2} | |
\end{Verbatim} | |
xreplace doesn't differentiate between free and bound symbols. In the | |
following, subs(x, y) would not change x since it is a bound symbol, | |
but xreplace does: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{Integral} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Integral}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{xreplace}\PYG{p}{(}\PYG{p}{\PYGZob{}}\PYG{n}{x}\PYG{p}{:} \PYG{n}{y}\PYG{p}{\PYGZcb{}}\PYG{p}{)} | |
\PYG{g+go}{Integral(y, (y, 1, 2*y))} | |
\end{Verbatim} | |
Trying to replace x with an expression raises an error: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Integral}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{xreplace}\PYG{p}{(}\PYG{p}{\PYGZob{}}\PYG{n}{x}\PYG{p}{:} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{y}\PYG{p}{\PYGZcb{}}\PYG{p}{)} | |
\PYG{g+go}{ValueError: Invalid limits given: ((2*y, 1, 4*y),)} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\end{fulllineitems} | |
\subsubsection{Atom} | |
\label{modules/core:atom}\index{Atom (class in sympy.core.basic)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.basic.Atom}\pysigline{\strong{class }\code{sympy.core.basic.}\bfcode{Atom}} | |
A parent class for atomic things. An atom is an expression with no subexpressions. | |
\paragraph{Examples} | |
Symbol, Number, Rational, Integer, ... | |
But not: Add, Mul, Pow, ... | |
\end{fulllineitems} | |
\subsubsection{C} | |
\label{modules/core:c}\index{C (in module sympy.core.basic)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.basic.C}\pysigline{\code{sympy.core.basic.}\bfcode{C}} | |
\end{fulllineitems} | |
\subsection{singleton} | |
\label{modules/core:module-sympy.core.singleton}\label{modules/core:singleton}\index{sympy.core.singleton (module)} | |
\subsubsection{S} | |
\label{modules/core:s}\index{S (in module sympy.core.singleton)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.singleton.S}\pysigline{\code{sympy.core.singleton.}\bfcode{S}} | |
\end{fulllineitems} | |
\subsection{expr} | |
\label{modules/core:expr}\label{modules/core:module-sympy.core.expr}\index{sympy.core.expr (module)} | |
\subsection{Expr} | |
\label{modules/core:id16}\index{Expr (class in sympy.core.expr)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr}\pysigline{\strong{class }\code{sympy.core.expr.}\bfcode{Expr}} | |
Base class for algebraic expressions. | |
Everything that requires arithmetic operations to be defined | |
should subclass this class, instead of Basic (which should be | |
used only for argument storage and expression manipulation, i.e. | |
pattern matching, substitutions, etc). | |
\strong{See also:} | |
{\hyperref[modules/core:sympy.core.basic.Basic]{\emph{\code{sympy.core.basic.Basic}}}} (\autopageref*{modules/core:sympy.core.basic.Basic}) | |
\index{apart() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.apart}\pysiglinewithargsret{\bfcode{apart}}{\emph{x=None}, \emph{**args}}{} | |
See the apart function in sympy.polys | |
\end{fulllineitems} | |
\index{args\_cnc() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.args_cnc}\pysiglinewithargsret{\bfcode{args\_cnc}}{\emph{cset=False}, \emph{warn=True}, \emph{split\_1=True}}{} | |
Return {[}commutative factors, non-commutative factors{]} of self. | |
self is treated as a Mul and the ordering of the factors is maintained. | |
If \code{cset} is True the commutative factors will be returned in a set. | |
If there were repeated factors (as may happen with an unevaluated Mul) | |
then an error will be raised unless it is explicitly supressed by | |
setting \code{warn} to False. | |
Note: -1 is always separated from a Number unless split\_1 is False. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{symbols}\PYG{p}{,} \PYG{n}{oo} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{A}\PYG{p}{,} \PYG{n}{B} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{A B}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{commutative}\PYG{o}{=}\PYG{l+m+mi}{0}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x}\PYG{p}{,} \PYG{n}{y} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x y}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{args\PYGZus{}cnc}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{[[\PYGZhy{}1, 2, x, y], []]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mf}{2.5}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{args\PYGZus{}cnc}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{[[\PYGZhy{}1, 2.5, x], []]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{o}{*}\PYG{n}{A}\PYG{o}{*}\PYG{n}{B}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{args\PYGZus{}cnc}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{[[\PYGZhy{}1, 2, x, y], [A, B]]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{o}{*}\PYG{n}{A}\PYG{o}{*}\PYG{n}{B}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{args\PYGZus{}cnc}\PYG{p}{(}\PYG{n}{split\PYGZus{}1}\PYG{o}{=}\PYG{n+nb+bp}{False}\PYG{p}{)} | |
\PYG{g+go}{[[\PYGZhy{}2, x, y], [A, B]]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{args\PYGZus{}cnc}\PYG{p}{(}\PYG{n}{cset}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{[set([\PYGZhy{}1, 2, x, y]), []]} | |
\end{Verbatim} | |
The arg is always treated as a Mul: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{n}{x} \PYG{o}{+} \PYG{n}{A}\PYG{p}{)}\PYG{o}{.}\PYG{n}{args\PYGZus{}cnc}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{[[], [x \PYGZhy{} 2 + A]]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{n}{oo}\PYG{p}{)}\PYG{o}{.}\PYG{n}{args\PYGZus{}cnc}\PYG{p}{(}\PYG{p}{)} \PYG{c}{\PYGZsh{} \PYGZhy{}oo is a singleton} | |
\PYG{g+go}{[[\PYGZhy{}1, oo], []]} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{as\_coeff\_Add() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.as_coeff_Add}\pysiglinewithargsret{\bfcode{as\_coeff\_Add}}{}{} | |
Efficiently extract the coefficient of a summation. | |
\end{fulllineitems} | |
\index{as\_coeff\_Mul() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.as_coeff_Mul}\pysiglinewithargsret{\bfcode{as\_coeff\_Mul}}{\emph{rational=False}}{} | |
Efficiently extract the coefficient of a product. | |
\end{fulllineitems} | |
\index{as\_coeff\_add() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.as_coeff_add}\pysiglinewithargsret{\bfcode{as\_coeff\_add}}{\emph{*deps}}{} | |
Return the tuple (c, args) where self is written as an Add, \code{a}. | |
c should be a Rational added to any terms of the Add that are | |
independent of deps. | |
args should be a tuple of all other terms of \code{a}; args is empty | |
if self is a Number or if self is independent of deps (when given). | |
This should be used when you don't know if self is an Add or not but | |
you want to treat self as an Add or if you want to process the | |
individual arguments of the tail of self as an Add. | |
\begin{itemize} | |
\item {} | |
if you know self is an Add and want only the head, use self.args{[}0{]}; | |
\item {} | |
if you don't want to process the arguments of the tail but need the | |
tail then use self.as\_two\_terms() which gives the head and tail. | |
\item {} | |
if you want to split self into an independent and dependent parts | |
use \code{self.as\_independent(*deps)} | |
\end{itemize} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{S} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{S}\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}coeff\PYGZus{}add}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{(3, ())} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{3} \PYG{o}{+} \PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}coeff\PYGZus{}add}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{(3, (x,))} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{3} \PYG{o}{+} \PYG{n}{x} \PYG{o}{+} \PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}coeff\PYGZus{}add}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{(y + 3, (x,))} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{3} \PYG{o}{+} \PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}coeff\PYGZus{}add}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{(y + 3, ())} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{as\_coeff\_exponent() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.as_coeff_exponent}\pysiglinewithargsret{\bfcode{as\_coeff\_exponent}}{\emph{x}}{} | |
\code{c*x**e -\textgreater{} c,e} where x can be any symbolic expression. | |
\end{fulllineitems} | |
\index{as\_coeff\_mul() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.as_coeff_mul}\pysiglinewithargsret{\bfcode{as\_coeff\_mul}}{\emph{*deps}}{} | |
Return the tuple (c, args) where self is written as a Mul, \code{m}. | |
c should be a Rational multiplied by any terms of the Mul that are | |
independent of deps. | |
args should be a tuple of all other terms of m; args is empty | |
if self is a Number or if self is independent of deps (when given). | |
This should be used when you don't know if self is a Mul or not but | |
you want to treat self as a Mul or if you want to process the | |
individual arguments of the tail of self as a Mul. | |
\begin{itemize} | |
\item {} | |
if you know self is a Mul and want only the head, use self.args{[}0{]}; | |
\item {} | |
if you don't want to process the arguments of the tail but need the | |
tail then use self.as\_two\_terms() which gives the head and tail; | |
\item {} | |
if you want to split self into an independent and dependent parts | |
use \code{self.as\_independent(*deps)} | |
\end{itemize} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{S} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{S}\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}coeff\PYGZus{}mul}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{(3, ())} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}coeff\PYGZus{}mul}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{(3, (x, y))} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}coeff\PYGZus{}mul}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{(3*y, (x,))} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}coeff\PYGZus{}mul}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{(3*y, ())} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{as\_coefficient() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.as_coefficient}\pysiglinewithargsret{\bfcode{as\_coefficient}}{\emph{expr}}{} | |
Extracts symbolic coefficient at the given expression. In | |
other words, this functions separates `self' into the product | |
of `expr' and `expr'-free coefficient. If such separation | |
is not possible it will return None. | |
\strong{See also:} | |
\begin{description} | |
\item[{{\hyperref[modules/core:sympy.core.expr.Expr.coeff]{\emph{\code{coeff}}}} (\autopageref*{modules/core:sympy.core.expr.Expr.coeff})}] \leavevmode | |
return sum of terms have a given factor | |
\item[{{\hyperref[modules/core:sympy.core.expr.Expr.as_coeff_Add]{\emph{\code{as\_coeff\_Add}}}} (\autopageref*{modules/core:sympy.core.expr.Expr.as_coeff_Add})}] \leavevmode | |
separate the additive constant from an expression | |
\item[{{\hyperref[modules/core:sympy.core.expr.Expr.as_coeff_Mul]{\emph{\code{as\_coeff\_Mul}}}} (\autopageref*{modules/core:sympy.core.expr.Expr.as_coeff_Mul})}] \leavevmode | |
separate the multiplicative constant from an expression | |
\item[{{\hyperref[modules/core:sympy.core.expr.Expr.as_independent]{\emph{\code{as\_independent}}}} (\autopageref*{modules/core:sympy.core.expr.Expr.as_independent})}] \leavevmode | |
separate x-dependent terms/factors from others | |
\item[{\code{sympy.polys.polytools.coeff\_monomial}}] \leavevmode | |
efficiently find the single coefficient of a monomial in Poly | |
\item[{\code{sympy.polys.polytools.nth}}] \leavevmode | |
like coeff\_monomial but powers of monomial terms are used | |
\end{description} | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{E}\PYG{p}{,} \PYG{n}{pi}\PYG{p}{,} \PYG{n}{sin}\PYG{p}{,} \PYG{n}{I}\PYG{p}{,} \PYG{n}{Poly} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{E}\PYG{o}{.}\PYG{n}{as\PYGZus{}coefficient}\PYG{p}{(}\PYG{n}{E}\PYG{p}{)} | |
\PYG{g+go}{1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{E}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}coefficient}\PYG{p}{(}\PYG{n}{E}\PYG{p}{)} | |
\PYG{g+go}{2} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{E}\PYG{p}{)}\PYG{o}{*}\PYG{n}{E}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}coefficient}\PYG{p}{(}\PYG{n}{E}\PYG{p}{)} | |
\end{Verbatim} | |
Two terms have E in them so a sum is returned. (If one were | |
desiring the coefficient of the term exactly matching E then | |
the constant from the returned expression could be selected. | |
Or, for greater precision, a method of Poly can be used to | |
indicate the desired term from which the coefficient is | |
desired.) | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{E} \PYG{o}{+} \PYG{n}{x}\PYG{o}{*}\PYG{n}{E}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}coefficient}\PYG{p}{(}\PYG{n}{E}\PYG{p}{)} | |
\PYG{g+go}{x + 2} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{\PYGZus{}}\PYG{o}{.}\PYG{n}{args}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{]} \PYG{c}{\PYGZsh{} just want the exact match} | |
\PYG{g+go}{2} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{Poly}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{E} \PYG{o}{+} \PYG{n}{x}\PYG{o}{*}\PYG{n}{E}\PYG{p}{)}\PYG{p}{;} \PYG{n}{p} | |
\PYG{g+go}{Poly(x*E + 2*E, x, E, domain=\PYGZsq{}ZZ\PYGZsq{})} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{coeff\PYGZus{}monomial}\PYG{p}{(}\PYG{n}{E}\PYG{p}{)} | |
\PYG{g+go}{2} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{nth}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+go}{2} | |
\end{Verbatim} | |
Since the following cannot be written as a product containing | |
E as a factor, None is returned. (If the coefficient \code{2*x} is | |
desired then the \code{coeff} method should be used.) | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{E}\PYG{o}{*}\PYG{n}{x} \PYG{o}{+} \PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}coefficient}\PYG{p}{(}\PYG{n}{E}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{E}\PYG{o}{*}\PYG{n}{x} \PYG{o}{+} \PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{coeff}\PYG{p}{(}\PYG{n}{E}\PYG{p}{)} | |
\PYG{g+go}{2*x} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{E}\PYG{o}{*}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)} \PYG{o}{+} \PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}coefficient}\PYG{p}{(}\PYG{n}{E}\PYG{p}{)} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{pi}\PYG{o}{*}\PYG{n}{I}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}coefficient}\PYG{p}{(}\PYG{n}{pi}\PYG{o}{*}\PYG{n}{I}\PYG{p}{)} | |
\PYG{g+go}{2} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{I}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}coefficient}\PYG{p}{(}\PYG{n}{pi}\PYG{o}{*}\PYG{n}{I}\PYG{p}{)} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{as\_coefficients\_dict() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.as_coefficients_dict}\pysiglinewithargsret{\bfcode{as\_coefficients\_dict}}{}{} | |
Return a dictionary mapping terms to their Rational coefficient. | |
Since the dictionary is a defaultdict, inquiries about terms which | |
were not present will return a coefficient of 0. If an expression is | |
not an Add it is considered to have a single term. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{a}\PYG{p}{,} \PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{x} \PYG{o}{+} \PYG{n}{a}\PYG{o}{*}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{4}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}coefficients\PYGZus{}dict}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{\PYGZob{}1: 4, x: 3, a*x: 1\PYGZcb{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{\PYGZus{}}\PYG{p}{[}\PYG{n}{a}\PYG{p}{]} | |
\PYG{g+go}{0} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{a}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}coefficients\PYGZus{}dict}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{\PYGZob{}a*x: 3\PYGZcb{}} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{as\_content\_primitive() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.as_content_primitive}\pysiglinewithargsret{\bfcode{as\_content\_primitive}}{\emph{radical=False}}{} | |
This method should recursively remove a Rational from all arguments | |
and return that (content) and the new self (primitive). The content | |
should always be positive and \code{Mul(*foo.as\_content\_primitive()) == foo}. | |
The primitive need no be in canonical form and should try to preserve | |
the underlying structure if possible (i.e. expand\_mul should not be | |
applied to self). | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{sqrt} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{z} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{eq} \PYG{o}{=} \PYG{l+m+mi}{2} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{y}\PYG{o}{*}\PYG{p}{(}\PYG{l+m+mi}{3} \PYG{o}{+} \PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)} | |
\end{Verbatim} | |
The as\_content\_primitive function is recursive and retains structure: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{eq}\PYG{o}{.}\PYG{n}{as\PYGZus{}content\PYGZus{}primitive}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{(2, x + 3*y*(y + 1) + 1)} | |
\end{Verbatim} | |
Integer powers will have Rationals extracted from the base: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{p}{(}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{l+m+mi}{6}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}content\PYGZus{}primitive}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{(4, (3*x + 1)**2)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{p}{(}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{l+m+mi}{6}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}content\PYGZus{}primitive}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{(1, (2*(3*x + 1))**(2*y))} | |
\end{Verbatim} | |
Terms may end up joining once their as\_content\_primitives are added: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{p}{(}\PYG{l+m+mi}{5}\PYG{o}{*}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{p}{(}\PYG{l+m+mi}{1} \PYG{o}{+} \PYG{n}{y}\PYG{p}{)}\PYG{p}{)} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{o}{*}\PYG{p}{(}\PYG{l+m+mi}{3} \PYG{o}{+} \PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}content\PYGZus{}primitive}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{(11, x*(y + 1))} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{o}{*}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{p}{(}\PYG{l+m+mi}{1} \PYG{o}{+} \PYG{n}{y}\PYG{p}{)}\PYG{p}{)} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{o}{*}\PYG{p}{(}\PYG{l+m+mi}{3} \PYG{o}{+} \PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}content\PYGZus{}primitive}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{(9, x*(y + 1))} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{o}{*}\PYG{p}{(}\PYG{n}{z}\PYG{o}{*}\PYG{p}{(}\PYG{l+m+mi}{1} \PYG{o}{+} \PYG{n}{y}\PYG{p}{)}\PYG{p}{)} \PYG{o}{+} \PYG{l+m+mf}{2.0}\PYG{o}{*}\PYG{n}{x}\PYG{o}{*}\PYG{p}{(}\PYG{l+m+mi}{3} \PYG{o}{+} \PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}content\PYGZus{}primitive}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{(1, 6.0*x*(y + 1) + 3*z*(y + 1))} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{p}{(}\PYG{l+m+mi}{5}\PYG{o}{*}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{p}{(}\PYG{l+m+mi}{1} \PYG{o}{+} \PYG{n}{y}\PYG{p}{)}\PYG{p}{)} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{o}{*}\PYG{p}{(}\PYG{l+m+mi}{3} \PYG{o}{+} \PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}content\PYGZus{}primitive}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{(121, x**2*(y + 1)**2)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{p}{(}\PYG{l+m+mi}{5}\PYG{o}{*}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{p}{(}\PYG{l+m+mi}{1} \PYG{o}{+} \PYG{n}{y}\PYG{p}{)}\PYG{p}{)} \PYG{o}{+} \PYG{l+m+mf}{2.0}\PYG{o}{*}\PYG{n}{x}\PYG{o}{*}\PYG{p}{(}\PYG{l+m+mi}{3} \PYG{o}{+} \PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}content\PYGZus{}primitive}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{(1, 121.0*x**2*(y + 1)**2)} | |
\end{Verbatim} | |
Radical content can also be factored out of the primitive: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{sqrt}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{)} \PYG{o}{+} \PYG{l+m+mi}{4}\PYG{o}{*}\PYG{n}{sqrt}\PYG{p}{(}\PYG{l+m+mi}{10}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}content\PYGZus{}primitive}\PYG{p}{(}\PYG{n}{radical}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{(2, sqrt(2)*(1 + 2*sqrt(5)))} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{as\_expr() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.as_expr}\pysiglinewithargsret{\bfcode{as\_expr}}{\emph{*gens}}{} | |
Convert a polynomial to a SymPy expression. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{sin} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{f} \PYG{o}{=} \PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}poly}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{f}\PYG{o}{.}\PYG{n}{as\PYGZus{}expr}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{x**2 + x*y} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}expr}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{sin(x)} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{as\_independent() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.as_independent}\pysiglinewithargsret{\bfcode{as\_independent}}{\emph{*deps}, \emph{**hint}}{} | |
A mostly naive separation of a Mul or Add into arguments that are not | |
are dependent on deps. To obtain as complete a separation of variables | |
as possible, use a separation method first, e.g.: | |
\begin{itemize} | |
\item {} | |
separatevars() to change Mul, Add and Pow (including exp) into Mul | |
\item {} | |
.expand(mul=True) to change Add or Mul into Add | |
\item {} | |
.expand(log=True) to change log expr into an Add | |
\end{itemize} | |
The only non-naive thing that is done here is to respect noncommutative | |
ordering of variables. | |
The returned tuple (i, d) has the following interpretation: | |
\begin{itemize} | |
\item {} | |
i will has no variable that appears in deps | |
\item {} | |
d will be 1 or else have terms that contain variables that are in deps | |
\item {} | |
if self is an Add then self = i + d | |
\item {} | |
if self is a Mul then self = i*d | |
\item {} | |
if self is anything else, either tuple (self, S.One) or (S.One, self) | |
is returned. | |
\end{itemize} | |
To force the expression to be treated as an Add, use the hint as\_Add=True | |
\paragraph{Examples} | |
-- self is an Add | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{sin}\PYG{p}{,} \PYG{n}{cos}\PYG{p}{,} \PYG{n}{exp} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{z} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}independent}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{(0, x*y + x)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}independent}\PYG{p}{(}\PYG{n}{y}\PYG{p}{)} | |
\PYG{g+go}{(x, x*y)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{o}{*}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} \PYG{o}{+} \PYG{n}{y} \PYG{o}{+} \PYG{n}{x} \PYG{o}{+} \PYG{n}{z}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}independent}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{(y + z, 2*x*sin(x) + x)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{o}{*}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} \PYG{o}{+} \PYG{n}{y} \PYG{o}{+} \PYG{n}{x} \PYG{o}{+} \PYG{n}{z}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}independent}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{)} | |
\PYG{g+go}{(z, 2*x*sin(x) + x + y)} | |
\end{Verbatim} | |
-- self is a Mul | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{n}{cos}\PYG{p}{(}\PYG{n}{y}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}independent}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{(cos(y), x*sin(x))} | |
\end{Verbatim} | |
non-commutative terms cannot always be separated out when self is a Mul | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{symbols} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{n1}\PYG{p}{,} \PYG{n}{n2}\PYG{p}{,} \PYG{n}{n3} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{n1 n2 n3}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{commutative}\PYG{o}{=}\PYG{n+nb+bp}{False}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{n1} \PYG{o}{+} \PYG{n}{n1}\PYG{o}{*}\PYG{n}{n2}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}independent}\PYG{p}{(}\PYG{n}{n2}\PYG{p}{)} | |
\PYG{g+go}{(n1, n1*n2)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{n2}\PYG{o}{*}\PYG{n}{n1} \PYG{o}{+} \PYG{n}{n1}\PYG{o}{*}\PYG{n}{n2}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}independent}\PYG{p}{(}\PYG{n}{n2}\PYG{p}{)} | |
\PYG{g+go}{(0, n1*n2 + n2*n1)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{n1}\PYG{o}{*}\PYG{n}{n2}\PYG{o}{*}\PYG{n}{n3}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}independent}\PYG{p}{(}\PYG{n}{n1}\PYG{p}{)} | |
\PYG{g+go}{(1, n1*n2*n3)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{n1}\PYG{o}{*}\PYG{n}{n2}\PYG{o}{*}\PYG{n}{n3}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}independent}\PYG{p}{(}\PYG{n}{n2}\PYG{p}{)} | |
\PYG{g+go}{(n1, n2*n3)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{p}{(}\PYG{n}{x}\PYG{o}{\PYGZhy{}}\PYG{n}{n1}\PYG{p}{)}\PYG{o}{*}\PYG{p}{(}\PYG{n}{x}\PYG{o}{\PYGZhy{}}\PYG{n}{y}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}independent}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{(1, (x \PYGZhy{} y)*(x \PYGZhy{} n1))} | |
\end{Verbatim} | |
-- self is anything else: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}independent}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{(1, sin(x))} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}independent}\PYG{p}{(}\PYG{n}{y}\PYG{p}{)} | |
\PYG{g+go}{(sin(x), 1)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{exp}\PYG{p}{(}\PYG{n}{x}\PYG{o}{+}\PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}independent}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{(1, exp(x + y))} | |
\end{Verbatim} | |
-- force self to be treated as an Add: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}independent}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{as\PYGZus{}Add}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{(0, 3*x)} | |
\end{Verbatim} | |
-- force self to be treated as a Mul: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{o}{+}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}independent}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{as\PYGZus{}Add}\PYG{o}{=}\PYG{n+nb+bp}{False}\PYG{p}{)} | |
\PYG{g+go}{(1, x + 3)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{3}\PYG{o}{+}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}independent}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{as\PYGZus{}Add}\PYG{o}{=}\PYG{n+nb+bp}{False}\PYG{p}{)} | |
\PYG{g+go}{(1, x \PYGZhy{} 3)} | |
\end{Verbatim} | |
Note how the below differs from the above in making the | |
constant on the dep term positive. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{y}\PYG{o}{*}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{3}\PYG{o}{+}\PYG{n}{x}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}independent}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{(y, x \PYGZhy{} 3)} | |
\end{Verbatim} | |
\begin{description} | |
\item[{-- use .as\_independent() for true independence testing instead}] \leavevmode | |
of .has(). The former considers only symbols in the free | |
symbols while the latter considers all symbols | |
\end{description} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{Integral} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{I} \PYG{o}{=} \PYG{n}{Integral}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{I}\PYG{o}{.}\PYG{n}{has}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x} \PYG{o+ow}{in} \PYG{n}{I}\PYG{o}{.}\PYG{n}{free\PYGZus{}symbols} | |
\PYG{g+go}{False} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{I}\PYG{o}{.}\PYG{n}{as\PYGZus{}independent}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} \PYG{o}{==} \PYG{p}{(}\PYG{n}{I}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+go}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{I} \PYG{o}{+} \PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}independent}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} \PYG{o}{==} \PYG{p}{(}\PYG{n}{I}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
Note: when trying to get independent terms, a separation method | |
might need to be used first. In this case, it is important to keep | |
track of what you send to this routine so you know how to interpret | |
the returned values | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{separatevars}\PYG{p}{,} \PYG{n}{log} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{separatevars}\PYG{p}{(}\PYG{n}{exp}\PYG{p}{(}\PYG{n}{x}\PYG{o}{+}\PYG{n}{y}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}independent}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{(exp(y), exp(x))} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}independent}\PYG{p}{(}\PYG{n}{y}\PYG{p}{)} | |
\PYG{g+go}{(x, x*y)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{separatevars}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}independent}\PYG{p}{(}\PYG{n}{y}\PYG{p}{)} | |
\PYG{g+go}{(x, y + 1)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{p}{(}\PYG{l+m+mi}{1} \PYG{o}{+} \PYG{n}{y}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}independent}\PYG{p}{(}\PYG{n}{y}\PYG{p}{)} | |
\PYG{g+go}{(x, y + 1)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{p}{(}\PYG{l+m+mi}{1} \PYG{o}{+} \PYG{n}{y}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{expand}\PYG{p}{(}\PYG{n}{mul}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}independent}\PYG{p}{(}\PYG{n}{y}\PYG{p}{)} | |
\PYG{g+go}{(x, x*y)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a}\PYG{p}{,} \PYG{n}{b}\PYG{o}{=}\PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{a b}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,}\PYG{n}{positive}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{log}\PYG{p}{(}\PYG{n}{a}\PYG{o}{*}\PYG{n}{b}\PYG{p}{)}\PYG{o}{.}\PYG{n}{expand}\PYG{p}{(}\PYG{n}{log}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}independent}\PYG{p}{(}\PYG{n}{b}\PYG{p}{)} | |
\PYG{g+go}{(log(a), log(b))} | |
\end{Verbatim} | |
\begin{description} | |
\item[{See also: .separatevars(), .expand(log=True),}] \leavevmode | |
.as\_two\_terms(), .as\_coeff\_add(), .as\_coeff\_mul() | |
\end{description} | |
\end{fulllineitems} | |
\index{as\_leading\_term() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.as_leading_term}\pysiglinewithargsret{\bfcode{as\_leading\_term}}{\emph{*args}, \emph{**kwargs}}{} | |
Returns the leading (nonzero) term of the series expansion of self. | |
The \_eval\_as\_leading\_term routines are used to do this, and they must | |
always return a non-zero value. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{1} \PYG{o}{+} \PYG{n}{x} \PYG{o}{+} \PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}leading\PYGZus{}term}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{o}{/}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{n}{x} \PYG{o}{+} \PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}leading\PYGZus{}term}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{x**(\PYGZhy{}2)} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{as\_numer\_denom() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.as_numer_denom}\pysiglinewithargsret{\bfcode{as\_numer\_denom}}{}{} | |
expression -\textgreater{} a/b -\textgreater{} a, b | |
This is just a stub that should be defined by | |
an object's class methods to get anything else. | |
\strong{See also:} | |
\begin{description} | |
\item[{\code{normal}}] \leavevmode | |
return a/b instead of a, b | |
\end{description} | |
\end{fulllineitems} | |
\index{as\_ordered\_factors() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.as_ordered_factors}\pysiglinewithargsret{\bfcode{as\_ordered\_factors}}{\emph{order=None}}{} | |
Return list of ordered factors (if Mul) else {[}self{]}. | |
\end{fulllineitems} | |
\index{as\_ordered\_terms() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.as_ordered_terms}\pysiglinewithargsret{\bfcode{as\_ordered\_terms}}{\emph{order=None}, \emph{data=False}}{} | |
Transform an expression to an ordered list of terms. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{sin}\PYG{p}{,} \PYG{n}{cos} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{cos}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} \PYG{o}{+} \PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}ordered\PYGZus{}terms}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{[sin(x)**2*cos(x), sin(x)**2, 1]} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{as\_powers\_dict() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.as_powers_dict}\pysiglinewithargsret{\bfcode{as\_powers\_dict}}{}{} | |
Return self as a dictionary of factors with each factor being | |
treated as a power. The keys are the bases of the factors and the | |
values, the corresponding exponents. The resulting dictionary should | |
be used with caution if the expression is a Mul and contains non- | |
commutative factors since the order that they appeared will be lost in | |
the dictionary. | |
\end{fulllineitems} | |
\index{as\_real\_imag() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.as_real_imag}\pysiglinewithargsret{\bfcode{as\_real\_imag}}{\emph{deep=True}, \emph{**hints}}{} | |
Performs complex expansion on `self' and returns a tuple | |
containing collected both real and imaginary parts. This | |
method can't be confused with re() and im() functions, | |
which does not perform complex expansion at evaluation. | |
However it is possible to expand both re() and im() | |
functions and get exactly the same results as with | |
a single call to this function. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{symbols}\PYG{p}{,} \PYG{n}{I} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x}\PYG{p}{,} \PYG{n}{y} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x,y}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{real}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{n}{y}\PYG{o}{*}\PYG{n}{I}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}real\PYGZus{}imag}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{(x, y)} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{z}\PYG{p}{,} \PYG{n}{w} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{z} \PYG{o}{+} \PYG{n}{w}\PYG{o}{*}\PYG{n}{I}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}real\PYGZus{}imag}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{(re(z) \PYGZhy{} im(w), re(w) + im(z))} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{as\_terms() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.as_terms}\pysiglinewithargsret{\bfcode{as\_terms}}{}{} | |
Transform an expression to a list of terms. | |
\end{fulllineitems} | |
\index{cancel() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.cancel}\pysiglinewithargsret{\bfcode{cancel}}{\emph{*gens}, \emph{**args}}{} | |
See the cancel function in sympy.polys | |
\end{fulllineitems} | |
\index{coeff() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.coeff}\pysiglinewithargsret{\bfcode{coeff}}{\emph{x}, \emph{n=1}, \emph{right=False}}{} | |
Returns the coefficient from the term(s) containing \code{x**n} or None. If \code{n} | |
is zero then all terms independent of \code{x} will be returned. | |
When x is noncommutative, the coeff to the left (default) or right of x | |
can be returned. The keyword `right' is ignored when x is commutative. | |
\strong{See also:} | |
\begin{description} | |
\item[{{\hyperref[modules/core:sympy.core.expr.Expr.as_coefficient]{\emph{\code{as\_coefficient}}}} (\autopageref*{modules/core:sympy.core.expr.Expr.as_coefficient})}] \leavevmode | |
separate the expression into a coefficient and factor | |
\item[{{\hyperref[modules/core:sympy.core.expr.Expr.as_coeff_Add]{\emph{\code{as\_coeff\_Add}}}} (\autopageref*{modules/core:sympy.core.expr.Expr.as_coeff_Add})}] \leavevmode | |
separate the additive constant from an expression | |
\item[{{\hyperref[modules/core:sympy.core.expr.Expr.as_coeff_Mul]{\emph{\code{as\_coeff\_Mul}}}} (\autopageref*{modules/core:sympy.core.expr.Expr.as_coeff_Mul})}] \leavevmode | |
separate the multiplicative constant from an expression | |
\item[{{\hyperref[modules/core:sympy.core.expr.Expr.as_independent]{\emph{\code{as\_independent}}}} (\autopageref*{modules/core:sympy.core.expr.Expr.as_independent})}] \leavevmode | |
separate x-dependent terms/factors from others | |
\item[{\code{sympy.polys.polytools.coeff\_monomial}}] \leavevmode | |
efficiently find the single coefficient of a monomial in Poly | |
\item[{\code{sympy.polys.polytools.nth}}] \leavevmode | |
like coeff\_monomial but powers of monomial terms are used | |
\end{description} | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{symbols} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{z} | |
\end{Verbatim} | |
You can select terms that have an explicit negative in front of them: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{coeff}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+go}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{x} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{coeff}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+go}{2*y} | |
\end{Verbatim} | |
You can select terms with no Rational coefficient: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{coeff}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+go}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{3} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{4}\PYG{o}{*}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{.}\PYG{n}{coeff}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+go}{0} | |
\end{Verbatim} | |
You can select terms independent of x by making n=0; in this case | |
expr.as\_independent(x){[}0{]} is returned (and 0 will be returned instead | |
of None): | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{3} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{4}\PYG{o}{*}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{.}\PYG{n}{coeff}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{)} | |
\PYG{g+go}{3} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{eq} \PYG{o}{=} \PYG{p}{(}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{3}\PYG{p}{)}\PYG{o}{.}\PYG{n}{expand}\PYG{p}{(}\PYG{p}{)} \PYG{o}{+} \PYG{l+m+mi}{1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{eq} | |
\PYG{g+go}{x**3 + 3*x**2 + 3*x + 2} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{[}\PYG{n}{eq}\PYG{o}{.}\PYG{n}{coeff}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{i}\PYG{p}{)} \PYG{k}{for} \PYG{n}{i} \PYG{o+ow}{in} \PYG{n+nb}{reversed}\PYG{p}{(}\PYG{n+nb}{range}\PYG{p}{(}\PYG{l+m+mi}{4}\PYG{p}{)}\PYG{p}{)}\PYG{p}{]} | |
\PYG{g+go}{[1, 3, 3, 2]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{eq} \PYG{o}{\PYGZhy{}}\PYG{o}{=} \PYG{l+m+mi}{2} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{[}\PYG{n}{eq}\PYG{o}{.}\PYG{n}{coeff}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{i}\PYG{p}{)} \PYG{k}{for} \PYG{n}{i} \PYG{o+ow}{in} \PYG{n+nb}{reversed}\PYG{p}{(}\PYG{n+nb}{range}\PYG{p}{(}\PYG{l+m+mi}{4}\PYG{p}{)}\PYG{p}{)}\PYG{p}{]} | |
\PYG{g+go}{[1, 3, 3, 0]} | |
\end{Verbatim} | |
You can select terms that have a numerical term in front of them: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{n}{x} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{coeff}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+go}{\PYGZhy{}y} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{sqrt} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{n}{sqrt}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{coeff}\PYG{p}{(}\PYG{n}{sqrt}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{x} | |
\end{Verbatim} | |
The matching is exact: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{3} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{4}\PYG{o}{*}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{.}\PYG{n}{coeff}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{2} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{3} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{4}\PYG{o}{*}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{.}\PYG{n}{coeff}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+go}{4} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{3} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{4}\PYG{o}{*}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{.}\PYG{n}{coeff}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{3}\PYG{p}{)} | |
\PYG{g+go}{0} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{z}\PYG{o}{*}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{n}{y}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{.}\PYG{n}{coeff}\PYG{p}{(}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{n}{y}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+go}{z} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{z}\PYG{o}{*}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{n}{y}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{.}\PYG{n}{coeff}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{n}{y}\PYG{p}{)} | |
\PYG{g+go}{0} | |
\end{Verbatim} | |
In addition, no factoring is done, so 1 + z*(1 + y) is not obtained | |
from the following: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{n}{z}\PYG{o}{*}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{coeff}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{1} | |
\end{Verbatim} | |
If such factoring is desired, factor\_terms can be used first: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{factor\PYGZus{}terms} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{factor\PYGZus{}terms}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{n}{z}\PYG{o}{*}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{coeff}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{z*(y + 1) + 1} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{n}\PYG{p}{,} \PYG{n}{m}\PYG{p}{,} \PYG{n}{o} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{n m o}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{commutative}\PYG{o}{=}\PYG{n+nb+bp}{False}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{n}\PYG{o}{.}\PYG{n}{coeff}\PYG{p}{(}\PYG{n}{n}\PYG{p}{)} | |
\PYG{g+go}{1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{n}\PYG{p}{)}\PYG{o}{.}\PYG{n}{coeff}\PYG{p}{(}\PYG{n}{n}\PYG{p}{)} | |
\PYG{g+go}{3} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{n}\PYG{o}{*}\PYG{n}{m} \PYG{o}{+} \PYG{n}{m}\PYG{o}{*}\PYG{n}{n}\PYG{o}{*}\PYG{n}{m}\PYG{p}{)}\PYG{o}{.}\PYG{n}{coeff}\PYG{p}{(}\PYG{n}{n}\PYG{p}{)} \PYG{c}{\PYGZsh{} = (1 + m)*n*m} | |
\PYG{g+go}{1 + m} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{n}\PYG{o}{*}\PYG{n}{m} \PYG{o}{+} \PYG{n}{m}\PYG{o}{*}\PYG{n}{n}\PYG{o}{*}\PYG{n}{m}\PYG{p}{)}\PYG{o}{.}\PYG{n}{coeff}\PYG{p}{(}\PYG{n}{n}\PYG{p}{,} \PYG{n}{right}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} \PYG{c}{\PYGZsh{} = (1 + m)*n*m} | |
\PYG{g+go}{m} | |
\end{Verbatim} | |
If there is more than one possible coefficient 0 is returned: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{n}\PYG{o}{*}\PYG{n}{m} \PYG{o}{+} \PYG{n}{m}\PYG{o}{*}\PYG{n}{n}\PYG{p}{)}\PYG{o}{.}\PYG{n}{coeff}\PYG{p}{(}\PYG{n}{n}\PYG{p}{)} | |
\PYG{g+go}{0} | |
\end{Verbatim} | |
If there is only one possible coefficient, it is returned: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{n}\PYG{o}{*}\PYG{n}{m} \PYG{o}{+} \PYG{n}{x}\PYG{o}{*}\PYG{n}{m}\PYG{o}{*}\PYG{n}{n}\PYG{p}{)}\PYG{o}{.}\PYG{n}{coeff}\PYG{p}{(}\PYG{n}{m}\PYG{o}{*}\PYG{n}{n}\PYG{p}{)} | |
\PYG{g+go}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{n}\PYG{o}{*}\PYG{n}{m} \PYG{o}{+} \PYG{n}{x}\PYG{o}{*}\PYG{n}{m}\PYG{o}{*}\PYG{n}{n}\PYG{p}{)}\PYG{o}{.}\PYG{n}{coeff}\PYG{p}{(}\PYG{n}{m}\PYG{o}{*}\PYG{n}{n}\PYG{p}{,} \PYG{n}{right}\PYG{o}{=}\PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+go}{1} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{collect() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.collect}\pysiglinewithargsret{\bfcode{collect}}{\emph{syms}, \emph{func=None}, \emph{evaluate=True}, \emph{exact=False}, \emph{distribute\_order\_term=True}}{} | |
See the collect function in sympy.simplify | |
\end{fulllineitems} | |
\index{combsimp() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.combsimp}\pysiglinewithargsret{\bfcode{combsimp}}{}{} | |
See the combsimp function in sympy.simplify | |
\end{fulllineitems} | |
\index{compute\_leading\_term() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.compute_leading_term}\pysiglinewithargsret{\bfcode{compute\_leading\_term}}{\emph{x}, \emph{logx=None}}{} | |
as\_leading\_term is only allowed for results of .series() | |
This is a wrapper to compute a series first. | |
\end{fulllineitems} | |
\index{could\_extract\_minus\_sign() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.could_extract_minus_sign}\pysiglinewithargsret{\bfcode{could\_extract\_minus\_sign}}{}{} | |
Canonical way to choose an element in the set \{e, -e\} where | |
e is any expression. If the canonical element is e, we have | |
e.could\_extract\_minus\_sign() == True, else | |
e.could\_extract\_minus\_sign() == False. | |
For any expression, the set \code{\{e.could\_extract\_minus\_sign(), | |
(-e).could\_extract\_minus\_sign()\}} must be \code{\{True, False\}}. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{x}\PYG{o}{\PYGZhy{}}\PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{could\PYGZus{}extract\PYGZus{}minus\PYGZus{}sign}\PYG{p}{(}\PYG{p}{)} \PYG{o}{!=} \PYG{p}{(}\PYG{n}{y}\PYG{o}{\PYGZhy{}}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{could\PYGZus{}extract\PYGZus{}minus\PYGZus{}sign}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{count\_ops() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.count_ops}\pysiglinewithargsret{\bfcode{count\_ops}}{\emph{visual=None}}{} | |
wrapper for count\_ops that returns the operation count. | |
\end{fulllineitems} | |
\index{equals() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.equals}\pysiglinewithargsret{\bfcode{equals}}{\emph{other}, \emph{failing\_expression=False}}{} | |
Return True if self == other, False if it doesn't, or None. If | |
failing\_expression is True then the expression which did not simplify | |
to a 0 will be returned instead of None. | |
If \code{self} is a Number (or complex number) that is not zero, then | |
the result is False. | |
If \code{self} is a number and has not evaluated to zero, evalf will be | |
used to test whether the expression evaluates to zero. If it does so | |
and the result has significance (i.e. the precision is either -1, for | |
a Rational result, or is greater than 1) then the evalf value will be | |
used to return True or False. | |
\end{fulllineitems} | |
\index{expand() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.expand}\pysiglinewithargsret{\bfcode{expand}}{\emph{*args}, \emph{**kwargs}}{} | |
Expand an expression using hints. | |
See the docstring of the expand() function in sympy.core.function for | |
more information. | |
\end{fulllineitems} | |
\index{extract\_additively() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.extract_additively}\pysiglinewithargsret{\bfcode{extract\_additively}}{\emph{c}}{} | |
Return self - c if it's possible to subtract c from self and | |
make all matching coefficients move towards zero, else return None. | |
\strong{See also:} | |
{\hyperref[modules/core:sympy.core.expr.Expr.extract_multiplicatively]{\emph{\code{extract\_multiplicatively}}}} (\autopageref*{modules/core:sympy.core.expr.Expr.extract_multiplicatively}), {\hyperref[modules/core:sympy.core.expr.Expr.coeff]{\emph{\code{coeff}}}} (\autopageref*{modules/core:sympy.core.expr.Expr.coeff}), {\hyperref[modules/core:sympy.core.expr.Expr.as_coefficient]{\emph{\code{as\_coefficient}}}} (\autopageref*{modules/core:sympy.core.expr.Expr.as_coefficient}) | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e} \PYG{o}{=} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{3} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e}\PYG{o}{.}\PYG{n}{extract\PYGZus{}additively}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+go}{x + 2} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e}\PYG{o}{.}\PYG{n}{extract\PYGZus{}additively}\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e}\PYG{o}{.}\PYG{n}{extract\PYGZus{}additively}\PYG{p}{(}\PYG{l+m+mi}{4}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{y}\PYG{o}{*}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{extract\PYGZus{}additively}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{*}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{y} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)} \PYG{o}{+} \PYG{l+m+mi}{3}\PYG{p}{)}\PYG{o}{.}\PYG{n}{extract\PYGZus{}additively}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+go}{(x + 1)*(x + 2*y) + 3} | |
\end{Verbatim} | |
Sometimes auto-expansion will return a less simplified result | |
than desired; gcd\_terms might be used in such cases: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{gcd\PYGZus{}terms} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{4}\PYG{o}{*}\PYG{n}{x}\PYG{o}{*}\PYG{p}{(}\PYG{n}{y} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)} \PYG{o}{+} \PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{extract\PYGZus{}additively}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{4*x*(y + 1) + x*(4*y + 3) \PYGZhy{} x*(4*y + 4) + y} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{gcd\PYGZus{}terms}\PYG{p}{(}\PYG{n}{\PYGZus{}}\PYG{p}{)} | |
\PYG{g+go}{x*(4*y + 3) + y} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{extract\_branch\_factor() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.extract_branch_factor}\pysiglinewithargsret{\bfcode{extract\_branch\_factor}}{\emph{allow\_half=False}}{} | |
Try to write self as \code{exp\_polar(2*pi*I*n)*z} in a nice way. | |
Return (z, n). | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{exp\PYGZus{}polar}\PYG{p}{,} \PYG{n}{I}\PYG{p}{,} \PYG{n}{pi} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{exp\PYGZus{}polar}\PYG{p}{(}\PYG{n}{I}\PYG{o}{*}\PYG{n}{pi}\PYG{p}{)}\PYG{o}{.}\PYG{n}{extract\PYGZus{}branch\PYGZus{}factor}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{(exp\PYGZus{}polar(I*pi), 0)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{exp\PYGZus{}polar}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{I}\PYG{o}{*}\PYG{n}{pi}\PYG{p}{)}\PYG{o}{.}\PYG{n}{extract\PYGZus{}branch\PYGZus{}factor}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{(1, 1)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{exp\PYGZus{}polar}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{n}{pi}\PYG{o}{*}\PYG{n}{I}\PYG{p}{)}\PYG{o}{.}\PYG{n}{extract\PYGZus{}branch\PYGZus{}factor}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{(exp\PYGZus{}polar(I*pi), \PYGZhy{}1)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{exp\PYGZus{}polar}\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{pi}\PYG{o}{*}\PYG{n}{I} \PYG{o}{+} \PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{extract\PYGZus{}branch\PYGZus{}factor}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{(exp\PYGZus{}polar(x + I*pi), 1)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{y}\PYG{o}{*}\PYG{n}{exp\PYGZus{}polar}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{5}\PYG{o}{*}\PYG{n}{pi}\PYG{o}{*}\PYG{n}{I}\PYG{p}{)}\PYG{o}{*}\PYG{n}{exp\PYGZus{}polar}\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{pi}\PYG{o}{*}\PYG{n}{I} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{pi}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{extract\PYGZus{}branch\PYGZus{}factor}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{(y*exp\PYGZus{}polar(2*pi*x), \PYGZhy{}1)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{exp\PYGZus{}polar}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{n}{I}\PYG{o}{*}\PYG{n}{pi}\PYG{o}{/}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{.}\PYG{n}{extract\PYGZus{}branch\PYGZus{}factor}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{(exp\PYGZus{}polar(\PYGZhy{}I*pi/2), 0)} | |
\end{Verbatim} | |
If allow\_half is True, also extract exp\_polar(I*pi): | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{exp\PYGZus{}polar}\PYG{p}{(}\PYG{n}{I}\PYG{o}{*}\PYG{n}{pi}\PYG{p}{)}\PYG{o}{.}\PYG{n}{extract\PYGZus{}branch\PYGZus{}factor}\PYG{p}{(}\PYG{n}{allow\PYGZus{}half}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{(1, 1/2)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{exp\PYGZus{}polar}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{I}\PYG{o}{*}\PYG{n}{pi}\PYG{p}{)}\PYG{o}{.}\PYG{n}{extract\PYGZus{}branch\PYGZus{}factor}\PYG{p}{(}\PYG{n}{allow\PYGZus{}half}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{(1, 1)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{exp\PYGZus{}polar}\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{I}\PYG{o}{*}\PYG{n}{pi}\PYG{p}{)}\PYG{o}{.}\PYG{n}{extract\PYGZus{}branch\PYGZus{}factor}\PYG{p}{(}\PYG{n}{allow\PYGZus{}half}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{(1, 3/2)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{exp\PYGZus{}polar}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{n}{I}\PYG{o}{*}\PYG{n}{pi}\PYG{p}{)}\PYG{o}{.}\PYG{n}{extract\PYGZus{}branch\PYGZus{}factor}\PYG{p}{(}\PYG{n}{allow\PYGZus{}half}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{(1, \PYGZhy{}1/2)} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{extract\_multiplicatively() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.extract_multiplicatively}\pysiglinewithargsret{\bfcode{extract\_multiplicatively}}{\emph{c}}{} | |
Return None if it's not possible to make self in the form | |
c * something in a nice way, i.e. preserving the properties | |
of arguments of self. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{symbols}\PYG{p}{,} \PYG{n}{Rational} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x}\PYG{p}{,} \PYG{n}{y} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x,y}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{real}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{3}\PYG{p}{)}\PYG{o}{.}\PYG{n}{extract\PYGZus{}multiplicatively}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{*} \PYG{n}{y}\PYG{p}{)} | |
\PYG{g+go}{x*y**2} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{3}\PYG{p}{)}\PYG{o}{.}\PYG{n}{extract\PYGZus{}multiplicatively}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{4} \PYG{o}{*} \PYG{n}{y}\PYG{p}{)} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{extract\PYGZus{}multiplicatively}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+go}{x} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{extract\PYGZus{}multiplicatively}\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{p}{)} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{Rational}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{extract\PYGZus{}multiplicatively}\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{p}{)} | |
\PYG{g+go}{x/6} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{factor() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.factor}\pysiglinewithargsret{\bfcode{factor}}{\emph{*gens}, \emph{**args}}{} | |
See the factor() function in sympy.polys.polytools | |
\end{fulllineitems} | |
\index{getO() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.getO}\pysiglinewithargsret{\bfcode{getO}}{}{} | |
Returns the additive O(..) symbol if there is one, else None. | |
\end{fulllineitems} | |
\index{getn() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.getn}\pysiglinewithargsret{\bfcode{getn}}{}{} | |
Returns the order of the expression. | |
The order is determined either from the O(...) term. If there | |
is no O(...) term, it returns None. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{O} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{1} \PYG{o}{+} \PYG{n}{x} \PYG{o}{+} \PYG{n}{O}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{getn}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{2} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{1} \PYG{o}{+} \PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{getn}\PYG{p}{(}\PYG{p}{)} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{integrate() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.integrate}\pysiglinewithargsret{\bfcode{integrate}}{\emph{*args}, \emph{**kwargs}}{} | |
See the integrate function in sympy.integrals | |
\end{fulllineitems} | |
\index{invert() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.invert}\pysiglinewithargsret{\bfcode{invert}}{\emph{g}}{} | |
See the invert function in sympy.polys | |
\end{fulllineitems} | |
\index{is\_algebraic\_expr() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.is_algebraic_expr}\pysiglinewithargsret{\bfcode{is\_algebraic\_expr}}{\emph{*syms}}{} | |
This tests whether a given expression is algebraic or not, in the | |
given symbols, syms. When syms is not given, all free symbols | |
will be used. The rational function does not have to be in expanded | |
or in any kind of canonical form. | |
This function returns False for expressions that are ``algebraic | |
expressions'' with symbolic exponents. This is a simple extension to the | |
is\_rational\_function, including rational exponentiation. | |
\strong{See also:} | |
{\hyperref[modules/core:sympy.core.expr.Expr.is_rational_function]{\emph{\code{is\_rational\_function}}}} (\autopageref*{modules/core:sympy.core.expr.Expr.is_rational_function}) | |
\paragraph{References} | |
\begin{itemize} | |
\item {} | |
\href{http://en.wikipedia.org/wiki/Algebraic\_expression}{http://en.wikipedia.org/wiki/Algebraic\_expression} | |
\end{itemize} | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{Symbol}\PYG{p}{,} \PYG{n}{sqrt} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x} \PYG{o}{=} \PYG{n}{Symbol}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{real}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{sqrt}\PYG{p}{(}\PYG{l+m+mi}{1} \PYG{o}{+} \PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{is\PYGZus{}rational\PYGZus{}function}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{False} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{sqrt}\PYG{p}{(}\PYG{l+m+mi}{1} \PYG{o}{+} \PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{is\PYGZus{}algebraic\PYGZus{}expr}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
This function does not attempt any nontrivial simplifications that may | |
result in an expression that does not appear to be an algebraic | |
expression to become one. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{exp}\PYG{p}{,} \PYG{n}{factor} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a} \PYG{o}{=} \PYG{n}{sqrt}\PYG{p}{(}\PYG{n}{exp}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{exp}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{/}\PYG{p}{(}\PYG{n}{exp}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a}\PYG{o}{.}\PYG{n}{is\PYGZus{}algebraic\PYGZus{}expr}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{False} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{factor}\PYG{p}{(}\PYG{n}{a}\PYG{p}{)}\PYG{o}{.}\PYG{n}{is\PYGZus{}algebraic\PYGZus{}expr}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{is\_constant() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.is_constant}\pysiglinewithargsret{\bfcode{is\_constant}}{\emph{*wrt}, \emph{**flags}}{} | |
Return True if self is constant, False if not, or None if | |
the constancy could not be determined conclusively. | |
If an expression has no free symbols then it is a constant. If | |
there are free symbols it is possible that the expression is a | |
constant, perhaps (but not necessarily) zero. To test such | |
expressions, two strategies are tried: | |
1) numerical evaluation at two random points. If two such evaluations | |
give two different values and the values have a precision greater than | |
1 then self is not constant. If the evaluations agree or could not be | |
obtained with any precision, no decision is made. The numerical testing | |
is done only if \code{wrt} is different than the free symbols. | |
2) differentiation with respect to variables in `wrt' (or all free | |
symbols if omitted) to see if the expression is constant or not. This | |
will not always lead to an expression that is zero even though an | |
expression is constant (see added test in test\_expr.py). If | |
all derivatives are zero then self is constant with respect to the | |
given symbols. | |
If neither evaluation nor differentiation can prove the expression is | |
constant, None is returned unless two numerical values happened to be | |
the same and the flag \code{failing\_number} is True -- in that case the | |
numerical value will be returned. | |
If flag simplify=False is passed, self will not be simplified; | |
the default is True since self should be simplified before testing. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{cos}\PYG{p}{,} \PYG{n}{sin}\PYG{p}{,} \PYG{n}{Sum}\PYG{p}{,} \PYG{n}{S}\PYG{p}{,} \PYG{n}{pi} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{a}\PYG{p}{,} \PYG{n}{n}\PYG{p}{,} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x}\PYG{o}{.}\PYG{n}{is\PYGZus{}constant}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{False} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{S}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{.}\PYG{n}{is\PYGZus{}constant}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Sum}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{10}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{is\PYGZus{}constant}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Sum}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{n}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{is\PYGZus{}constant}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{False} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Sum}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{n}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{is\PYGZus{}constant}\PYG{p}{(}\PYG{n}{y}\PYG{p}{)} | |
\PYG{g+go}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Sum}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{n}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{is\PYGZus{}constant}\PYG{p}{(}\PYG{n}{n}\PYG{p}{)} | |
\PYG{g+go}{False} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Sum}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{n}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{is\PYGZus{}constant}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{eq} \PYG{o}{=} \PYG{n}{a}\PYG{o}{*}\PYG{n}{cos}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{n}{a}\PYG{o}{*}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{\PYGZhy{}} \PYG{n}{a} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{eq}\PYG{o}{.}\PYG{n}{is\PYGZus{}constant}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{eq}\PYG{o}{.}\PYG{n}{subs}\PYG{p}{(}\PYG{p}{\PYGZob{}}\PYG{n}{x}\PYG{p}{:}\PYG{n}{pi}\PYG{p}{,} \PYG{n}{a}\PYG{p}{:}\PYG{l+m+mi}{2}\PYG{p}{\PYGZcb{}}\PYG{p}{)} \PYG{o}{==} \PYG{n}{eq}\PYG{o}{.}\PYG{n}{subs}\PYG{p}{(}\PYG{p}{\PYGZob{}}\PYG{n}{x}\PYG{p}{:}\PYG{n}{pi}\PYG{p}{,} \PYG{n}{a}\PYG{p}{:}\PYG{l+m+mi}{3}\PYG{p}{\PYGZcb{}}\PYG{p}{)} \PYG{o}{==} \PYG{l+m+mi}{0} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{o}{*}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{is\PYGZus{}constant}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{False} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x}\PYG{o}{.}\PYG{n}{is\PYGZus{}constant}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{False} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{is\PYGZus{}constant}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{False} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{one} \PYG{o}{=} \PYG{n}{cos}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{one}\PYG{o}{.}\PYG{n}{is\PYGZus{}constant}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{p}{(}\PYG{n}{one} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{is\PYGZus{}constant}\PYG{p}{(}\PYG{p}{)} \PYG{o+ow}{in} \PYG{p}{(}\PYG{n+nb+bp}{True}\PYG{p}{,} \PYG{n+nb+bp}{False}\PYG{p}{)} \PYG{c}{\PYGZsh{} could be 0 or 1} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{is\_number (sympy.core.expr.Expr attribute)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.is_number}\pysigline{\bfcode{is\_number}} | |
Returns True if `self' has no free symbols. | |
It will be faster than \(if not self.free_symbols\), however, since | |
\(is_number\) will fail as soon as it hits a free symbol. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{log}\PYG{p}{,} \PYG{n}{Integral} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x}\PYG{o}{.}\PYG{n}{is\PYGZus{}number} | |
\PYG{g+go}{False} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{is\PYGZus{}number} | |
\PYG{g+go}{False} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{n}{log}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{is\PYGZus{}number} | |
\PYG{g+go}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{n}{Integral}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{is\PYGZus{}number} | |
\PYG{g+go}{False} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{n}{Integral}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{is\PYGZus{}number} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{is\_polynomial() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.is_polynomial}\pysiglinewithargsret{\bfcode{is\_polynomial}}{\emph{*syms}}{} | |
Return True if self is a polynomial in syms and False otherwise. | |
This checks if self is an exact polynomial in syms. This function | |
returns False for expressions that are ``polynomials'' with symbolic | |
exponents. Thus, you should be able to apply polynomial algorithms to | |
expressions for which this returns True, and Poly(expr, *syms) should | |
work if and only if expr.is\_polynomial(*syms) returns True. The | |
polynomial does not have to be in expanded form. If no symbols are | |
given, all free symbols in the expression will be used. | |
This is not part of the assumptions system. You cannot do | |
Symbol(`z', polynomial=True). | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{Symbol} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x} \PYG{o}{=} \PYG{n}{Symbol}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{4}\PYG{p}{)}\PYG{o}{.}\PYG{n}{is\PYGZus{}polynomial}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{4}\PYG{p}{)}\PYG{o}{.}\PYG{n}{is\PYGZus{}polynomial}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{o}{*}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{.}\PYG{n}{is\PYGZus{}polynomial}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{False} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{n} \PYG{o}{=} \PYG{n}{Symbol}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{n}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{nonnegative}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{,} \PYG{n}{integer}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{n}{n} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{.}\PYG{n}{is\PYGZus{}polynomial}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{False} | |
\end{Verbatim} | |
This function does not attempt any nontrivial simplifications that may | |
result in an expression that does not appear to be a polynomial to | |
become one. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{sqrt}\PYG{p}{,} \PYG{n}{factor}\PYG{p}{,} \PYG{n}{cancel} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{y} \PYG{o}{=} \PYG{n}{Symbol}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{y}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{positive}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a} \PYG{o}{=} \PYG{n}{sqrt}\PYG{p}{(}\PYG{n}{y}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{y} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a}\PYG{o}{.}\PYG{n}{is\PYGZus{}polynomial}\PYG{p}{(}\PYG{n}{y}\PYG{p}{)} | |
\PYG{g+go}{False} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{factor}\PYG{p}{(}\PYG{n}{a}\PYG{p}{)} | |
\PYG{g+go}{y + 1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{factor}\PYG{p}{(}\PYG{n}{a}\PYG{p}{)}\PYG{o}{.}\PYG{n}{is\PYGZus{}polynomial}\PYG{p}{(}\PYG{n}{y}\PYG{p}{)} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{b} \PYG{o}{=} \PYG{p}{(}\PYG{n}{y}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{y} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{/}\PYG{p}{(}\PYG{n}{y} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{b}\PYG{o}{.}\PYG{n}{is\PYGZus{}polynomial}\PYG{p}{(}\PYG{n}{y}\PYG{p}{)} | |
\PYG{g+go}{False} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{cancel}\PYG{p}{(}\PYG{n}{b}\PYG{p}{)} | |
\PYG{g+go}{y + 1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{cancel}\PYG{p}{(}\PYG{n}{b}\PYG{p}{)}\PYG{o}{.}\PYG{n}{is\PYGZus{}polynomial}\PYG{p}{(}\PYG{n}{y}\PYG{p}{)} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
See also .is\_rational\_function() | |
\end{fulllineitems} | |
\index{is\_rational\_function() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.is_rational_function}\pysiglinewithargsret{\bfcode{is\_rational\_function}}{\emph{*syms}}{} | |
Test whether function is a ratio of two polynomials in the given | |
symbols, syms. When syms is not given, all free symbols will be used. | |
The rational function does not have to be in expanded or in any kind of | |
canonical form. | |
This function returns False for expressions that are ``rational | |
functions'' with symbolic exponents. Thus, you should be able to call | |
.as\_numer\_denom() and apply polynomial algorithms to the result for | |
expressions for which this returns True. | |
This is not part of the assumptions system. You cannot do | |
Symbol(`z', rational\_function=True). | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{Symbol}\PYG{p}{,} \PYG{n}{sin} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{x}\PYG{o}{/}\PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{is\PYGZus{}rational\PYGZus{}function}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{.}\PYG{n}{is\PYGZus{}rational\PYGZus{}function}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{x}\PYG{o}{/}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{y}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{is\PYGZus{}rational\PYGZus{}function}\PYG{p}{(}\PYG{n}{y}\PYG{p}{)} | |
\PYG{g+go}{False} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{n} \PYG{o}{=} \PYG{n}{Symbol}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{n}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{integer}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{n}{n} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{.}\PYG{n}{is\PYGZus{}rational\PYGZus{}function}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{False} | |
\end{Verbatim} | |
This function does not attempt any nontrivial simplifications that may | |
result in an expression that does not appear to be a rational function | |
to become one. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{sqrt}\PYG{p}{,} \PYG{n}{factor} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{y} \PYG{o}{=} \PYG{n}{Symbol}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{y}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{positive}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a} \PYG{o}{=} \PYG{n}{sqrt}\PYG{p}{(}\PYG{n}{y}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{y} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{/}\PYG{n}{y} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a}\PYG{o}{.}\PYG{n}{is\PYGZus{}rational\PYGZus{}function}\PYG{p}{(}\PYG{n}{y}\PYG{p}{)} | |
\PYG{g+go}{False} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{factor}\PYG{p}{(}\PYG{n}{a}\PYG{p}{)} | |
\PYG{g+go}{(y + 1)/y} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{factor}\PYG{p}{(}\PYG{n}{a}\PYG{p}{)}\PYG{o}{.}\PYG{n}{is\PYGZus{}rational\PYGZus{}function}\PYG{p}{(}\PYG{n}{y}\PYG{p}{)} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
See also is\_algebraic\_expr(). | |
\end{fulllineitems} | |
\index{leadterm() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.leadterm}\pysiglinewithargsret{\bfcode{leadterm}}{\emph{x}}{} | |
Returns the leading term a*x**b as a tuple (a, b). | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{o}{+}\PYG{n}{x}\PYG{o}{+}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{.}\PYG{n}{leadterm}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{(1, 0)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{o}{/}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{o}{+}\PYG{n}{x}\PYG{o}{+}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{.}\PYG{n}{leadterm}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{(1, \PYGZhy{}2)} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{limit() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.limit}\pysiglinewithargsret{\bfcode{limit}}{\emph{x}, \emph{xlim}, \emph{dir='+'}}{} | |
Compute limit x-\textgreater{}xlim. | |
\end{fulllineitems} | |
\index{lseries() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.lseries}\pysiglinewithargsret{\bfcode{lseries}}{\emph{x=None}, \emph{x0=0}, \emph{dir='+'}, \emph{logx=None}}{} | |
Wrapper for series yielding an iterator of the terms of the series. | |
Note: an infinite series will yield an infinite iterator. The following, | |
for exaxmple, will never terminate. It will just keep printing terms | |
of the sin(x) series: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{k}{for} \PYG{n}{term} \PYG{o+ow}{in} \PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{lseries}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{:} | |
\PYG{k}{print} \PYG{n}{term} | |
\end{Verbatim} | |
The advantage of lseries() over nseries() is that many times you are | |
just interested in the next term in the series (i.e. the first term for | |
example), but you don't know how many you should ask for in nseries() | |
using the ``n'' parameter. | |
See also nseries(). | |
\end{fulllineitems} | |
\index{nseries() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.nseries}\pysiglinewithargsret{\bfcode{nseries}}{\emph{x=None}, \emph{x0=0}, \emph{n=6}, \emph{dir='+'}, \emph{logx=None}}{} | |
Wrapper to \_eval\_nseries if assumptions allow, else to series. | |
If x is given, x0 is 0, dir='+', and self has x, then \_eval\_nseries is | |
called. This calculates ``n'' terms in the innermost expressions and | |
then builds up the final series just by ``cross-multiplying'' everything | |
out. | |
The optional \code{logx} parameter can be used to replace any log(x) in the | |
returned series with a symbolic value to avoid evaluating log(x) at 0. A | |
symbol to use in place of log(x) should be provided. | |
Advantage -- it's fast, because we don't have to determine how many | |
terms we need to calculate in advance. | |
Disadvantage -- you may end up with less terms than you may have | |
expected, but the O(x**n) term appended will always be correct and | |
so the result, though perhaps shorter, will also be correct. | |
If any of those assumptions is not met, this is treated like a | |
wrapper to series which will try harder to return the correct | |
number of terms. | |
See also lseries(). | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{sin}\PYG{p}{,} \PYG{n}{log}\PYG{p}{,} \PYG{n}{Symbol} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{nseries}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{6}\PYG{p}{)} | |
\PYG{g+go}{x \PYGZhy{} x**3/6 + x**5/120 + O(x**6)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{log}\PYG{p}{(}\PYG{n}{x}\PYG{o}{+}\PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{.}\PYG{n}{nseries}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{5}\PYG{p}{)} | |
\PYG{g+go}{x \PYGZhy{} x**2/2 + x**3/3 \PYGZhy{} x**4/4 + O(x**5)} | |
\end{Verbatim} | |
Handling of the \code{logx} parameter --- in the following example the | |
expansion fails since \code{sin} does not have an asymptotic expansion | |
at -oo (the limit of log(x) as x approaches 0): | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e} \PYG{o}{=} \PYG{n}{sin}\PYG{p}{(}\PYG{n}{log}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e}\PYG{o}{.}\PYG{n}{nseries}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{6}\PYG{p}{)} | |
\PYG{g+gt}{Traceback (most recent call last):} | |
\PYG{c}{...} | |
\PYG{g+gr}{PoleError}: \PYG{n}{...} | |
\PYG{g+gp}{...} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{logx} \PYG{o}{=} \PYG{n}{Symbol}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{logx}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e}\PYG{o}{.}\PYG{n}{nseries}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{6}\PYG{p}{,} \PYG{n}{logx}\PYG{o}{=}\PYG{n}{logx}\PYG{p}{)} | |
\PYG{g+go}{sin(logx)} | |
\PYG{g+gt}{Traceback (most recent call last):} | |
\PYG{c}{...} | |
\PYG{g+gr}{PoleError}: \PYG{n}{...} | |
\end{Verbatim} | |
In the following example, the expansion works but gives only an Order term | |
unless the \code{logx} parameter is used: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e} \PYG{o}{=} \PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{n}{y} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e}\PYG{o}{.}\PYG{n}{nseries}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+go}{O(log(x)**2)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e}\PYG{o}{.}\PYG{n}{nseries}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{n}{logx}\PYG{o}{=}\PYG{n}{logx}\PYG{p}{)} | |
\PYG{g+go}{exp(logx*y)} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{nsimplify() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.nsimplify}\pysiglinewithargsret{\bfcode{nsimplify}}{\emph{constants={[}{]}}, \emph{tolerance=None}, \emph{full=False}}{} | |
See the nsimplify function in sympy.simplify | |
\end{fulllineitems} | |
\index{powsimp() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.powsimp}\pysiglinewithargsret{\bfcode{powsimp}}{\emph{deep=False}, \emph{combine='all'}}{} | |
See the powsimp function in sympy.simplify | |
\end{fulllineitems} | |
\index{primitive() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.primitive}\pysiglinewithargsret{\bfcode{primitive}}{}{} | |
Return the positive Rational that can be extracted non-recursively | |
from every term of self (i.e., self is treated like an Add). This is | |
like the as\_coeff\_Mul() method but primitive always extracts a positive | |
Rational (never a negative or a Float). | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{o}{*}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{.}\PYG{n}{primitive}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{(3, (x + 1)**2)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a} \PYG{o}{=} \PYG{p}{(}\PYG{l+m+mi}{6}\PYG{o}{*}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{;} \PYG{n}{a}\PYG{o}{.}\PYG{n}{primitive}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{(2, 3*x + 1)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{b} \PYG{o}{=} \PYG{p}{(}\PYG{n}{x}\PYG{o}{/}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{l+m+mi}{3}\PYG{p}{)}\PYG{p}{;} \PYG{n}{b}\PYG{o}{.}\PYG{n}{primitive}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{(1/2, x + 6)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{a}\PYG{o}{*}\PYG{n}{b}\PYG{p}{)}\PYG{o}{.}\PYG{n}{primitive}\PYG{p}{(}\PYG{p}{)} \PYG{o}{==} \PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{a}\PYG{o}{*}\PYG{n}{b}\PYG{p}{)} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{radsimp() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.radsimp}\pysiglinewithargsret{\bfcode{radsimp}}{}{} | |
See the radsimp function in sympy.simplify | |
\end{fulllineitems} | |
\index{ratsimp() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.ratsimp}\pysiglinewithargsret{\bfcode{ratsimp}}{}{} | |
See the ratsimp function in sympy.simplify | |
\end{fulllineitems} | |
\index{refine() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.refine}\pysiglinewithargsret{\bfcode{refine}}{\emph{assumption=True}}{} | |
See the refine function in sympy.assumptions | |
\end{fulllineitems} | |
\index{removeO() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.removeO}\pysiglinewithargsret{\bfcode{removeO}}{}{} | |
Removes the additive O(..) symbol if there is one | |
\end{fulllineitems} | |
\index{round() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.round}\pysiglinewithargsret{\bfcode{round}}{\emph{p=0}}{} | |
Return x rounded to the given decimal place. | |
If a complex number would results, apply round to the real | |
and imaginary components of the number. | |
\paragraph{Notes} | |
Do not confuse the Python builtin function, round, with the | |
SymPy method of the same name. The former always returns a float | |
(or raises an error if applied to a complex value) while the | |
latter returns either a Number or a complex number: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb}{isinstance}\PYG{p}{(}\PYG{n+nb}{round}\PYG{p}{(}\PYG{n}{S}\PYG{p}{(}\PYG{l+m+mi}{123}\PYG{p}{)}\PYG{p}{,} \PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{,} \PYG{n}{Number}\PYG{p}{)} | |
\PYG{g+go}{False} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb}{isinstance}\PYG{p}{(}\PYG{n}{S}\PYG{p}{(}\PYG{l+m+mi}{123}\PYG{p}{)}\PYG{o}{.}\PYG{n}{round}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{,} \PYG{n}{Number}\PYG{p}{)} | |
\PYG{g+go}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb}{isinstance}\PYG{p}{(}\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{I}\PYG{p}{)}\PYG{o}{.}\PYG{n}{round}\PYG{p}{(}\PYG{p}{)}\PYG{p}{,} \PYG{n}{Mul}\PYG{p}{)} | |
\PYG{g+go}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb}{isinstance}\PYG{p}{(}\PYG{p}{(}\PYG{l+m+mi}{1} \PYG{o}{+} \PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{I}\PYG{p}{)}\PYG{o}{.}\PYG{n}{round}\PYG{p}{(}\PYG{p}{)}\PYG{p}{,} \PYG{n}{Add}\PYG{p}{)} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{pi}\PYG{p}{,} \PYG{n}{E}\PYG{p}{,} \PYG{n}{I}\PYG{p}{,} \PYG{n}{S}\PYG{p}{,} \PYG{n}{Add}\PYG{p}{,} \PYG{n}{Mul}\PYG{p}{,} \PYG{n}{Number} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{S}\PYG{p}{(}\PYG{l+m+mf}{10.5}\PYG{p}{)}\PYG{o}{.}\PYG{n}{round}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{11.} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{pi}\PYG{o}{.}\PYG{n}{round}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{3.} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{pi}\PYG{o}{.}\PYG{n}{round}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+go}{3.14} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{pi} \PYG{o}{+} \PYG{n}{E}\PYG{o}{*}\PYG{n}{I}\PYG{p}{)}\PYG{o}{.}\PYG{n}{round}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{6. + 3.*I} | |
\end{Verbatim} | |
The round method has a chopping effect: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{pi} \PYG{o}{+} \PYG{n}{I}\PYG{o}{/}\PYG{l+m+mi}{10}\PYG{p}{)}\PYG{o}{.}\PYG{n}{round}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{6.} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{pi}\PYG{o}{/}\PYG{l+m+mi}{10} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{I}\PYG{p}{)}\PYG{o}{.}\PYG{n}{round}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{2.*I} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{pi}\PYG{o}{/}\PYG{l+m+mi}{10} \PYG{o}{+} \PYG{n}{E}\PYG{o}{*}\PYG{n}{I}\PYG{p}{)}\PYG{o}{.}\PYG{n}{round}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+go}{0.31 + 2.72*I} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{separate() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.separate}\pysiglinewithargsret{\bfcode{separate}}{\emph{deep=False}, \emph{force=False}}{} | |
See the separate function in sympy.simplify | |
\end{fulllineitems} | |
\index{series() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.series}\pysiglinewithargsret{\bfcode{series}}{\emph{x=None}, \emph{x0=0}, \emph{n=6}, \emph{dir='+'}, \emph{logx=None}}{} | |
Series expansion of ``self'' around \code{x = x0} yielding either terms of | |
the series one by one (the lazy series given when n=None), else | |
all the terms at once when n != None. | |
Returns the series expansion of ``self'' around the point \code{x = x0} | |
with respect to \code{x} up to \code{O((x - x0)**n, x, x0)} (default n is 6). | |
If \code{x=None} and \code{self} is univariate, the univariate symbol will | |
be supplied, otherwise an error will be raised. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{cos}\PYG{p}{,} \PYG{n}{exp} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{cos}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{series}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{1 \PYGZhy{} x**2/2 + x**4/24 + O(x**6)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{cos}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{series}\PYG{p}{(}\PYG{n}{n}\PYG{o}{=}\PYG{l+m+mi}{4}\PYG{p}{)} | |
\PYG{g+go}{1 \PYGZhy{} x**2/2 + O(x**4)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{cos}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{series}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{x0}\PYG{o}{=}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{n}\PYG{o}{=}\PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+go}{cos(1) \PYGZhy{} (x \PYGZhy{} 1)*sin(1) + O((x \PYGZhy{} 1)**2, (x, 1))} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e} \PYG{o}{=} \PYG{n}{cos}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{n}{exp}\PYG{p}{(}\PYG{n}{y}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e}\PYG{o}{.}\PYG{n}{series}\PYG{p}{(}\PYG{n}{y}\PYG{p}{,} \PYG{n}{n}\PYG{o}{=}\PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+go}{cos(x + 1) \PYGZhy{} y*sin(x + 1) + O(y**2)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e}\PYG{o}{.}\PYG{n}{series}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{n}\PYG{o}{=}\PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+go}{cos(exp(y)) \PYGZhy{} x*sin(exp(y)) + O(x**2)} | |
\end{Verbatim} | |
If \code{n=None} then a generator of the series terms will be returned. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{term}\PYG{o}{=}\PYG{n}{cos}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{series}\PYG{p}{(}\PYG{n}{n}\PYG{o}{=}\PYG{n+nb+bp}{None}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{[}\PYG{n+nb}{next}\PYG{p}{(}\PYG{n}{term}\PYG{p}{)} \PYG{k}{for} \PYG{n}{i} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{]} | |
\PYG{g+go}{[1, \PYGZhy{}x**2/2]} | |
\end{Verbatim} | |
For \code{dir=+} (default) the series is calculated from the right and | |
for \code{dir=-} the series from the left. For smooth functions this | |
flag will not alter the results. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb}{abs}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{series}\PYG{p}{(}\PYG{n+nb}{dir}\PYG{o}{=}\PYG{l+s}{\PYGZdq{}}\PYG{l+s}{+}\PYG{l+s}{\PYGZdq{}}\PYG{p}{)} | |
\PYG{g+go}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb}{abs}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{series}\PYG{p}{(}\PYG{n+nb}{dir}\PYG{o}{=}\PYG{l+s}{\PYGZdq{}}\PYG{l+s}{\PYGZhy{}}\PYG{l+s}{\PYGZdq{}}\PYG{p}{)} | |
\PYG{g+go}{\PYGZhy{}x} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{simplify() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.simplify}\pysiglinewithargsret{\bfcode{simplify}}{\emph{ratio=1.7}, \emph{measure=None}}{} | |
See the simplify function in sympy.simplify | |
\end{fulllineitems} | |
\index{taylor\_term() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.taylor_term}\pysiglinewithargsret{\bfcode{taylor\_term}}{\emph{n}, \emph{x}, \emph{*previous\_terms}}{} | |
General method for the taylor term. | |
This method is slow, because it differentiates n-times. Subclasses can | |
redefine it to make it faster by using the ``previous\_terms''. | |
\end{fulllineitems} | |
\index{together() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.together}\pysiglinewithargsret{\bfcode{together}}{\emph{*args}, \emph{**kwargs}}{} | |
See the together function in sympy.polys | |
\end{fulllineitems} | |
\index{trigsimp() (sympy.core.expr.Expr method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.Expr.trigsimp}\pysiglinewithargsret{\bfcode{trigsimp}}{\emph{**args}}{} | |
See the trigsimp function in sympy.simplify | |
\end{fulllineitems} | |
\end{fulllineitems} | |
\subsection{AtomicExpr} | |
\label{modules/core:atomicexpr}\index{AtomicExpr (class in sympy.core.expr)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.expr.AtomicExpr}\pysigline{\strong{class }\code{sympy.core.expr.}\bfcode{AtomicExpr}} | |
A parent class for object which are both atoms and Exprs. | |
For example: Symbol, Number, Rational, Integer, ... | |
But not: Add, Mul, Pow, ... | |
\end{fulllineitems} | |
\subsection{symbol} | |
\label{modules/core:symbol}\label{modules/core:module-sympy.core.symbol}\index{sympy.core.symbol (module)} | |
\subsubsection{Symbol} | |
\label{modules/core:id17}\index{Symbol (class in sympy.core.symbol)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.symbol.Symbol}\pysigline{\strong{class }\code{sympy.core.symbol.}\bfcode{Symbol}}~\begin{description} | |
\item[{Assumptions:}] \leavevmode | |
commutative = True | |
\end{description} | |
You can override the default assumptions in the constructor: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{symbols} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{A}\PYG{p}{,}\PYG{n}{B} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{A,B}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{commutative} \PYG{o}{=} \PYG{n+nb+bp}{False}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb}{bool}\PYG{p}{(}\PYG{n}{A}\PYG{o}{*}\PYG{n}{B} \PYG{o}{!=} \PYG{n}{B}\PYG{o}{*}\PYG{n}{A}\PYG{p}{)} | |
\PYG{g+go}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb}{bool}\PYG{p}{(}\PYG{n}{A}\PYG{o}{*}\PYG{n}{B}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{==} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{A}\PYG{o}{*}\PYG{n}{B}\PYG{p}{)} \PYG{o}{==} \PYG{n+nb+bp}{True} \PYG{c}{\PYGZsh{} multiplication by scalars is commutative} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
\index{as\_dummy() (sympy.core.symbol.Symbol method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.symbol.Symbol.as_dummy}\pysiglinewithargsret{\bfcode{as\_dummy}}{}{} | |
Return a Dummy having the same name and same assumptions as self. | |
\end{fulllineitems} | |
\end{fulllineitems} | |
\subsubsection{Wild} | |
\label{modules/core:wild}\index{Wild (class in sympy.core.symbol)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.symbol.Wild}\pysigline{\strong{class }\code{sympy.core.symbol.}\bfcode{Wild}} | |
A Wild symbol matches anything, or anything | |
without whatever is explicitly excluded. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{Wild}\PYG{p}{,} \PYG{n}{WildFunction}\PYG{p}{,} \PYG{n}{cos}\PYG{p}{,} \PYG{n}{pi} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{z} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a} \PYG{o}{=} \PYG{n}{Wild}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{a}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x}\PYG{o}{.}\PYG{n}{match}\PYG{p}{(}\PYG{n}{a}\PYG{p}{)} | |
\PYG{g+go}{\PYGZob{}a\PYGZus{}: x\PYGZcb{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{pi}\PYG{o}{.}\PYG{n}{match}\PYG{p}{(}\PYG{n}{a}\PYG{p}{)} | |
\PYG{g+go}{\PYGZob{}a\PYGZus{}: pi\PYGZcb{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{.}\PYG{n}{match}\PYG{p}{(}\PYG{n}{a}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{\PYGZob{}a\PYGZus{}: 3*x\PYGZcb{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{cos}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{match}\PYG{p}{(}\PYG{n}{a}\PYG{p}{)} | |
\PYG{g+go}{\PYGZob{}a\PYGZus{}: cos(x)\PYGZcb{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{b} \PYG{o}{=} \PYG{n}{Wild}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{b}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{exclude}\PYG{o}{=}\PYG{p}{[}\PYG{n}{x}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{.}\PYG{n}{match}\PYG{p}{(}\PYG{n}{b}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{b}\PYG{o}{.}\PYG{n}{match}\PYG{p}{(}\PYG{n}{a}\PYG{p}{)} | |
\PYG{g+go}{\PYGZob{}a\PYGZus{}: b\PYGZus{}\PYGZcb{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{A} \PYG{o}{=} \PYG{n}{WildFunction}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{A}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{A}\PYG{o}{.}\PYG{n}{match}\PYG{p}{(}\PYG{n}{a}\PYG{p}{)} | |
\PYG{g+go}{\PYGZob{}a\PYGZus{}: A\PYGZus{}\PYGZcb{}} | |
\end{Verbatim} | |
\paragraph{Tips} | |
When using Wild, be sure to use the exclude | |
keyword to make the pattern more precise. | |
Without the exclude pattern, you may get matches | |
that are technically correct, but not what you | |
wanted. For example, using the above without | |
exclude: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{symbols} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a}\PYG{p}{,} \PYG{n}{b} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{a b}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{cls}\PYG{o}{=}\PYG{n}{Wild}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{match}\PYG{p}{(}\PYG{n}{a}\PYG{o}{*}\PYG{n}{x} \PYG{o}{+} \PYG{n}{b}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)} | |
\PYG{g+go}{\PYGZob{}a\PYGZus{}: 2/x, b\PYGZus{}: 3\PYGZcb{}} | |
\end{Verbatim} | |
This is technically correct, because | |
(2/x)*x + 3*y == 2 + 3*y, but you probably | |
wanted it to not match at all. The issue is that | |
you really didn't want a and b to include x and y, | |
and the exclude parameter lets you specify exactly | |
this. With the exclude parameter, the pattern will | |
not match. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a} \PYG{o}{=} \PYG{n}{Wild}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{a}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{exclude}\PYG{o}{=}\PYG{p}{[}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{b} \PYG{o}{=} \PYG{n}{Wild}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{b}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{exclude}\PYG{o}{=}\PYG{p}{[}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{match}\PYG{p}{(}\PYG{n}{a}\PYG{o}{*}\PYG{n}{x} \PYG{o}{+} \PYG{n}{b}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)} | |
\end{Verbatim} | |
Exclude also helps remove ambiguity from matches. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{E} \PYG{o}{=} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{y}\PYG{o}{*}\PYG{n}{z} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a}\PYG{p}{,} \PYG{n}{b} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{a b}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{cls}\PYG{o}{=}\PYG{n}{Wild}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{E}\PYG{o}{.}\PYG{n}{match}\PYG{p}{(}\PYG{n}{a}\PYG{o}{*}\PYG{n}{b}\PYG{p}{)} | |
\PYG{g+go}{\PYGZob{}a\PYGZus{}: 2*y*z, b\PYGZus{}: x**3\PYGZcb{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a} \PYG{o}{=} \PYG{n}{Wild}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{a}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{exclude}\PYG{o}{=}\PYG{p}{[}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{E}\PYG{o}{.}\PYG{n}{match}\PYG{p}{(}\PYG{n}{a}\PYG{o}{*}\PYG{n}{b}\PYG{p}{)} | |
\PYG{g+go}{\PYGZob{}a\PYGZus{}: z, b\PYGZus{}: 2*x**3*y\PYGZcb{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a} \PYG{o}{=} \PYG{n}{Wild}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{a}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{exclude}\PYG{o}{=}\PYG{p}{[}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{z}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{E}\PYG{o}{.}\PYG{n}{match}\PYG{p}{(}\PYG{n}{a}\PYG{o}{*}\PYG{n}{b}\PYG{p}{)} | |
\PYG{g+go}{\PYGZob{}a\PYGZus{}: 2, b\PYGZus{}: x**3*y*z\PYGZcb{}} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\subsubsection{Dummy} | |
\label{modules/core:dummy}\index{Dummy (class in sympy.core.symbol)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.symbol.Dummy}\pysigline{\strong{class }\code{sympy.core.symbol.}\bfcode{Dummy}} | |
Dummy symbols are each unique, identified by an internal count index: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{Dummy} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb}{bool}\PYG{p}{(}\PYG{n}{Dummy}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s}{x}\PYG{l+s}{\PYGZdq{}}\PYG{p}{)} \PYG{o}{==} \PYG{n}{Dummy}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s}{x}\PYG{l+s}{\PYGZdq{}}\PYG{p}{)}\PYG{p}{)} \PYG{o}{==} \PYG{n+nb+bp}{True} | |
\PYG{g+go}{False} | |
\end{Verbatim} | |
If a name is not supplied then a string value of the count index will be | |
used. This is useful when a temporary variable is needed and the name | |
of the variable used in the expression is not important. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Dummy}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{\PYGZus{}Dummy\PYGZus{}10} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\subsubsection{symbols} | |
\label{modules/core:symbols}\index{symbols() (in module sympy.core.symbol)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.symbol.symbols}\pysiglinewithargsret{\code{sympy.core.symbol.}\bfcode{symbols}}{\emph{names}, \emph{**args}}{} | |
Transform strings into instances of {\hyperref[modules/core:sympy.core.symbol.Symbol]{\emph{\code{Symbol}}}} (\autopageref*{modules/core:sympy.core.symbol.Symbol}) class. | |
{\hyperref[modules/core:sympy.core.symbol.symbols]{\emph{\code{symbols()}}}} (\autopageref*{modules/core:sympy.core.symbol.symbols}) function returns a sequence of symbols with names taken | |
from \code{names} argument, which can be a comma or whitespace delimited | |
string, or a sequence of strings: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{symbols}\PYG{p}{,} \PYG{n}{Function} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{z} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x,y,z}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a}\PYG{p}{,} \PYG{n}{b}\PYG{p}{,} \PYG{n}{c} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{a b c}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\end{Verbatim} | |
The type of output is dependent on the properties of input arguments: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+go}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x,}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+go}{(x,)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x,y}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+go}{(x, y)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{symbols}\PYG{p}{(}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{a}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{b}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{c}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{(a, b, c)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{symbols}\PYG{p}{(}\PYG{p}{[}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{a}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{b}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{c}\PYG{l+s}{\PYGZsq{}}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+go}{[a, b, c]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{symbols}\PYG{p}{(}\PYG{n+nb}{set}\PYG{p}{(}\PYG{p}{[}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{a}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{b}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{c}\PYG{l+s}{\PYGZsq{}}\PYG{p}{]}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{set([a, b, c])} | |
\end{Verbatim} | |
If an iterable container is needed for a single symbol, set the \code{seq} | |
argument to \code{True} or terminate the symbol name with a comma: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{seq}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{(x,)} | |
\end{Verbatim} | |
To reduce typing, range syntax is supported to create indexed symbols. | |
Ranges are indicated by a colon and the type of range is determined by | |
the character to the right of the colon. If the character is a digit | |
then all contiguous digits to the left are taken as the nonnegative | |
starting value (or 0 if there is no digit left of the colon) and all | |
contiguous digits to the right are taken as 1 greater than the ending | |
value: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x:10}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+go}{(x0, x1, x2, x3, x4, x5, x6, x7, x8, x9)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x5:10}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+go}{(x5, x6, x7, x8, x9)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x5(:2)}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+go}{(x50, x51)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x5:10,y:5}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+go}{(x5, x6, x7, x8, x9, y0, y1, y2, y3, y4)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{symbols}\PYG{p}{(}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x5:10}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{y:5}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{((x5, x6, x7, x8, x9), (y0, y1, y2, y3, y4))} | |
\end{Verbatim} | |
If the character to the right of the colon is a letter, then the single | |
letter to the left (or `a' if there is none) is taken as the start | |
and all characters in the lexicographic range \emph{through} the letter to | |
the right are used as the range: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x:z}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+go}{(x, y, z)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x:c}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} \PYG{c}{\PYGZsh{} null range} | |
\PYG{g+go}{()} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x(:c)}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+go}{(xa, xb, xc)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{:c}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+go}{(a, b, c)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{a:d, x:z}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+go}{(a, b, c, d, x, y, z)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{symbols}\PYG{p}{(}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{a:d}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x:z}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{((a, b, c, d), (x, y, z))} | |
\end{Verbatim} | |
Multiple ranges are supported; contiguous numerical ranges should be | |
separated by parentheses to disambiguate the ending number of one | |
range from the starting number of the next: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x:2(1:3)}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+go}{(x01, x02, x11, x12)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{:3:2}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} \PYG{c}{\PYGZsh{} parsing is from left to right} | |
\PYG{g+go}{(00, 01, 10, 11, 20, 21)} | |
\end{Verbatim} | |
Only one pair of parentheses surrounding ranges are removed, so to | |
include parentheses around ranges, double them. And to include spaces, | |
commas, or colons, escape them with a backslash: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x((a:b))}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+go}{(x(a), x(b))} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x(:1}\PYG{l+s}{\PYGZbs{}}\PYG{l+s}{,:2)}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} \PYG{c}{\PYGZsh{} or \PYGZsq{}x((:1)\PYGZbs{},(:2))\PYGZsq{}} | |
\PYG{g+go}{(x(0,0), x(0,1))} | |
\end{Verbatim} | |
All newly created symbols have assumptions set according to \code{args}: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{a}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{integer}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a}\PYG{o}{.}\PYG{n}{is\PYGZus{}integer} | |
\PYG{g+go}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{z} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x,y,z}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{real}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x}\PYG{o}{.}\PYG{n}{is\PYGZus{}real} \PYG{o+ow}{and} \PYG{n}{y}\PYG{o}{.}\PYG{n}{is\PYGZus{}real} \PYG{o+ow}{and} \PYG{n}{z}\PYG{o}{.}\PYG{n}{is\PYGZus{}real} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
Despite its name, {\hyperref[modules/core:sympy.core.symbol.symbols]{\emph{\code{symbols()}}}} (\autopageref*{modules/core:sympy.core.symbol.symbols}) can create symbol-like objects like | |
instances of Function or Wild classes. To achieve this, set \code{cls} | |
keyword argument to the desired type: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{f,g,h}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{cls}\PYG{o}{=}\PYG{n}{Function}\PYG{p}{)} | |
\PYG{g+go}{(f, g, h)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb}{type}\PYG{p}{(}\PYG{n}{\PYGZus{}}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+go}{\PYGZlt{}class \PYGZsq{}sympy.core.function.UndefinedFunction\PYGZsq{}\PYGZgt{}} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\subsubsection{var} | |
\label{modules/core:var}\index{var() (in module sympy.core.symbol)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.symbol.var}\pysiglinewithargsret{\code{sympy.core.symbol.}\bfcode{var}}{\emph{names}, \emph{**args}}{} | |
Create symbols and inject them into the global namespace. | |
This calls {\hyperref[modules/core:sympy.core.symbol.symbols]{\emph{\code{symbols()}}}} (\autopageref*{modules/core:sympy.core.symbol.symbols}) with the same arguments and puts the results | |
into the \emph{global} namespace. It's recommended not to use {\hyperref[modules/core:sympy.core.symbol.var]{\emph{\code{var()}}}} (\autopageref*{modules/core:sympy.core.symbol.var}) in | |
library code, where {\hyperref[modules/core:sympy.core.symbol.symbols]{\emph{\code{symbols()}}}} (\autopageref*{modules/core:sympy.core.symbol.symbols}) has to be used: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
.. rubric:: Examples | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{var} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{var}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+go}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x} | |
\PYG{g+go}{x} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{var}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{a,ab,abc}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+go}{(a, ab, abc)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{abc} | |
\PYG{g+go}{abc} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{var}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x,y}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{real}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{(x, y)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x}\PYG{o}{.}\PYG{n}{is\PYGZus{}real} \PYG{o+ow}{and} \PYG{n}{y}\PYG{o}{.}\PYG{n}{is\PYGZus{}real} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
See \code{symbol()} documentation for more details on what kinds of | |
arguments can be passed to {\hyperref[modules/core:sympy.core.symbol.var]{\emph{\code{var()}}}} (\autopageref*{modules/core:sympy.core.symbol.var}). | |
\end{fulllineitems} | |
\subsection{numbers} | |
\label{modules/core:module-sympy.core.numbers}\label{modules/core:numbers}\index{sympy.core.numbers (module)} | |
\subsubsection{Number} | |
\label{modules/core:number}\index{Number (class in sympy.core.numbers)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.numbers.Number}\pysigline{\strong{class }\code{sympy.core.numbers.}\bfcode{Number}} | |
Represents any kind of number in sympy. | |
Floating point numbers are represented by the Float class. | |
Integer numbers (of any size), together with rational numbers (again, | |
there is no limit on their size) are represented by the Rational class. | |
If you want to represent, for example, \code{1+sqrt(2)}, then you need to do: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{n}{Rational}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{)} \PYG{o}{+} \PYG{n}{sqrt}\PYG{p}{(}\PYG{n}{Rational}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{)} | |
\end{Verbatim} | |
\index{as\_coeff\_Add() (sympy.core.numbers.Number method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.numbers.Number.as_coeff_Add}\pysiglinewithargsret{\bfcode{as\_coeff\_Add}}{}{} | |
Efficiently extract the coefficient of a summation. | |
\end{fulllineitems} | |
\index{as\_coeff\_Mul() (sympy.core.numbers.Number method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.numbers.Number.as_coeff_Mul}\pysiglinewithargsret{\bfcode{as\_coeff\_Mul}}{\emph{rational=False}}{} | |
Efficiently extract the coefficient of a product. | |
\end{fulllineitems} | |
\index{cofactors() (sympy.core.numbers.Number method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.numbers.Number.cofactors}\pysiglinewithargsret{\bfcode{cofactors}}{\emph{other}}{} | |
Compute GCD and cofactors of \(self\) and \(other\). | |
\end{fulllineitems} | |
\index{gcd() (sympy.core.numbers.Number method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.numbers.Number.gcd}\pysiglinewithargsret{\bfcode{gcd}}{\emph{other}}{} | |
Compute GCD of \(self\) and \(other\). | |
\end{fulllineitems} | |
\index{lcm() (sympy.core.numbers.Number method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.numbers.Number.lcm}\pysiglinewithargsret{\bfcode{lcm}}{\emph{other}}{} | |
Compute LCM of \(self\) and \(other\). | |
\end{fulllineitems} | |
\end{fulllineitems} | |
\subsubsection{Float} | |
\label{modules/core:float}\index{Float (class in sympy.core.numbers)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.numbers.Float}\pysigline{\strong{class }\code{sympy.core.numbers.}\bfcode{Float}} | |
Represents a floating point number. It is capable of representing | |
arbitrary-precision floating-point numbers. | |
\paragraph{Notes} | |
Floats are inexact by their nature unless their value is a binary-exact | |
value. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{approx}\PYG{p}{,} \PYG{n}{exact} \PYG{o}{=} \PYG{n}{Float}\PYG{p}{(}\PYG{o}{.}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{p}{,} \PYG{n}{Float}\PYG{p}{(}\PYG{o}{.}\PYG{l+m+mi}{125}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{)} | |
\end{Verbatim} | |
For calculation purposes, evalf needs to be able to change the precision | |
but this will not increase the accuracy of the inexact value. The | |
following is the most accurate 5-digit approximation of a value of 0.1 | |
that had only 1 digit of precision: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{approx}\PYG{o}{.}\PYG{n}{evalf}\PYG{p}{(}\PYG{l+m+mi}{5}\PYG{p}{)} | |
\PYG{g+go}{0.099609} | |
\end{Verbatim} | |
By contrast, 0.125 is exact in binary (as it is in base 10) and so it | |
can be passed to Float or evalf to obtain an arbitrary precision with | |
matching accuracy: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Float}\PYG{p}{(}\PYG{n}{exact}\PYG{p}{,} \PYG{l+m+mi}{5}\PYG{p}{)} | |
\PYG{g+go}{0.12500} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{exact}\PYG{o}{.}\PYG{n}{evalf}\PYG{p}{(}\PYG{l+m+mi}{20}\PYG{p}{)} | |
\PYG{g+go}{0.12500000000000000000} | |
\end{Verbatim} | |
Trying to make a high-precision Float from a float is not disallowed, | |
but one must keep in mind that the \emph{underlying float} (not the apparent | |
decimal value) is being obtained with high precision. For example, 0.3 | |
does not have a finite binary representation. The closest rational is | |
the fraction 5404319552844595/2**54. So if you try to obtain a Float of | |
0.3 to 20 digits of precision you will not see the same thing as 0.3 | |
followed by 19 zeros: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Float}\PYG{p}{(}\PYG{l+m+mf}{0.3}\PYG{p}{,} \PYG{l+m+mi}{20}\PYG{p}{)} | |
\PYG{g+go}{0.29999999999999998890} | |
\end{Verbatim} | |
If you want a 20-digit value of the decimal 0.3 (not the floating point | |
approximation of 0.3) you should send the 0.3 as a string. The underlying | |
representation is still binary but a higher precision than Python's float | |
is used: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Float}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{0.3}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{l+m+mi}{20}\PYG{p}{)} | |
\PYG{g+go}{0.30000000000000000000} | |
\end{Verbatim} | |
Although you can increase the precision of an existing Float using Float | |
it will not increase the accuracy -- the underlying value is not changed: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{def} \PYG{n+nf}{show}\PYG{p}{(}\PYG{n}{f}\PYG{p}{)}\PYG{p}{:} \PYG{c}{\PYGZsh{} binary rep of Float} | |
\PYG{g+gp}{... } \PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{Mul}\PYG{p}{,} \PYG{n}{Pow} | |
\PYG{g+gp}{... } \PYG{n}{s}\PYG{p}{,} \PYG{n}{m}\PYG{p}{,} \PYG{n}{e}\PYG{p}{,} \PYG{n}{b} \PYG{o}{=} \PYG{n}{f}\PYG{o}{.}\PYG{n}{\PYGZus{}mpf\PYGZus{}} | |
\PYG{g+gp}{... } \PYG{n}{v} \PYG{o}{=} \PYG{n}{Mul}\PYG{p}{(}\PYG{n+nb}{int}\PYG{p}{(}\PYG{n}{m}\PYG{p}{)}\PYG{p}{,} \PYG{n}{Pow}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{n+nb}{int}\PYG{p}{(}\PYG{n}{e}\PYG{p}{)}\PYG{p}{,} \PYG{n}{evaluate}\PYG{o}{=}\PYG{n+nb+bp}{False}\PYG{p}{)}\PYG{p}{,} \PYG{n}{evaluate}\PYG{o}{=}\PYG{n+nb+bp}{False}\PYG{p}{)} | |
\PYG{g+gp}{... } \PYG{k}{print}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{ at prec=}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{\PYGZsq{}} \PYG{o}{\PYGZpc{}} \PYG{p}{(}\PYG{n}{v}\PYG{p}{,} \PYG{n}{f}\PYG{o}{.}\PYG{n}{\PYGZus{}prec}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+gp}{...} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{t} \PYG{o}{=} \PYG{n}{Float}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{0.3}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{show}\PYG{p}{(}\PYG{n}{t}\PYG{p}{)} | |
\PYG{g+go}{4915/2**14 at prec=13} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{show}\PYG{p}{(}\PYG{n}{Float}\PYG{p}{(}\PYG{n}{t}\PYG{p}{,} \PYG{l+m+mi}{20}\PYG{p}{)}\PYG{p}{)} \PYG{c}{\PYGZsh{} higher prec, not higher accuracy} | |
\PYG{g+go}{4915/2**14 at prec=70} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{show}\PYG{p}{(}\PYG{n}{Float}\PYG{p}{(}\PYG{n}{t}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{)} \PYG{c}{\PYGZsh{} lower prec} | |
\PYG{g+go}{307/2**10 at prec=10} | |
\end{Verbatim} | |
The same thing happens when evalf is used on a Float: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{show}\PYG{p}{(}\PYG{n}{t}\PYG{o}{.}\PYG{n}{evalf}\PYG{p}{(}\PYG{l+m+mi}{20}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{4915/2**14 at prec=70} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{show}\PYG{p}{(}\PYG{n}{t}\PYG{o}{.}\PYG{n}{evalf}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{307/2**10 at prec=10} | |
\end{Verbatim} | |
Finally, Floats can be instantiated with an mpf tuple (n, c, p) to | |
produce the number (-1)**n*c*2**p: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{n}\PYG{p}{,} \PYG{n}{c}\PYG{p}{,} \PYG{n}{p} \PYG{o}{=} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{5}\PYG{p}{,} \PYG{l+m+mi}{0} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{n}{n}\PYG{o}{*}\PYG{n}{c}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{o}{*}\PYG{n}{p} | |
\PYG{g+go}{\PYGZhy{}5} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Float}\PYG{p}{(}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{5}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{\PYGZhy{}5.00000000000000} | |
\end{Verbatim} | |
An actual mpf tuple also contains the number of bits in c as the last | |
element of the tuple: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{\PYGZus{}}\PYG{o}{.}\PYG{n}{\PYGZus{}mpf\PYGZus{}} | |
\PYG{g+go}{(1, 5, 0, 3)} | |
\end{Verbatim} | |
This is not needed for instantiation and is not the same thing as the | |
precision. The mpf tuple and the precision are two separate quantities | |
that Float tracks. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{Float} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Float}\PYG{p}{(}\PYG{l+m+mf}{3.5}\PYG{p}{)} | |
\PYG{g+go}{3.50000000000000} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Float}\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{p}{)} | |
\PYG{g+go}{3.00000000000000} | |
\end{Verbatim} | |
Floats can be created from a string representations of Python floats | |
to force ints to Float or to enter high-precision (\textgreater{} 15 significant | |
digits) values: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Float}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{.0010}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+go}{0.00100000000000000} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Float}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{1e\PYGZhy{}3}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+go}{0.00100000000000000} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Float}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{1e\PYGZhy{}3}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{)} | |
\PYG{g+go}{0.00100} | |
\end{Verbatim} | |
Float can automatically count significant figures if a null string | |
is sent for the precision; space are also allowed in the string. (Auto- | |
counting is only allowed for strings, ints and longs). | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Float}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{123 456 789 . 123 456}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+go}{123456789.123456} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Float}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{12e\PYGZhy{}3}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+go}{0.012} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Float}\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+go}{3.} | |
\end{Verbatim} | |
If a number is written in scientific notation, only the digits before the | |
exponent are considered significant if a decimal appears, otherwise the | |
``e'' signifies only how to move the decimal: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Float}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{60.e2}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} \PYG{c}{\PYGZsh{} 2 digits significant} | |
\PYG{g+go}{6.0e+3} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Float}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{60e2}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} \PYG{c}{\PYGZsh{} 4 digits significant} | |
\PYG{g+go}{6000.} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Float}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{600e\PYGZhy{}2}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} \PYG{c}{\PYGZsh{} 3 digits significant} | |
\PYG{g+go}{6.00} | |
\end{Verbatim} | |
\paragraph{Attributes} | |
\begin{tabulary}{\linewidth}{|L|L|} | |
\hline | |
is\_irrational | |
& \\ | |
\hline | |
is\_rational | |
& \\ | |
\hline\end{tabulary} | |
\end{fulllineitems} | |
\subsubsection{Rational} | |
\label{modules/core:rational}\index{Rational (class in sympy.core.numbers)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.numbers.Rational}\pysigline{\strong{class }\code{sympy.core.numbers.}\bfcode{Rational}} | |
Represents integers and rational numbers (p/q) of any size. | |
\strong{See also:} | |
\code{sympify}, {\hyperref[modules/simplify/simplify:sympy.simplify.simplify.nsimplify]{\emph{\code{sympy.simplify.simplify.nsimplify}}}} (\autopageref*{modules/simplify/simplify:sympy.simplify.simplify.nsimplify}) | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{Rational}\PYG{p}{,} \PYG{n}{nsimplify}\PYG{p}{,} \PYG{n}{S}\PYG{p}{,} \PYG{n}{pi} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Rational}\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{p}{)} | |
\PYG{g+go}{3} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Rational}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+go}{1/2} | |
\end{Verbatim} | |
Rational is unprejudiced in accepting input. If a float is passed, the | |
underlying value of the binary representation will be returned: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Rational}\PYG{p}{(}\PYG{o}{.}\PYG{l+m+mi}{5}\PYG{p}{)} | |
\PYG{g+go}{1/2} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Rational}\PYG{p}{(}\PYG{o}{.}\PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+go}{3602879701896397/18014398509481984} | |
\end{Verbatim} | |
If the simpler representation of the float is desired then consider | |
limiting the denominator to the desired value or convert the float to | |
a string (which is roughly equivalent to limiting the denominator to | |
10**12): | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Rational}\PYG{p}{(}\PYG{n+nb}{str}\PYG{p}{(}\PYG{o}{.}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{1/5} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Rational}\PYG{p}{(}\PYG{o}{.}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{.}\PYG{n}{limit\PYGZus{}denominator}\PYG{p}{(}\PYG{l+m+mi}{10}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{12}\PYG{p}{)} | |
\PYG{g+go}{1/5} | |
\end{Verbatim} | |
An arbitrarily precise Rational is obtained when a string literal is | |
passed: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Rational}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s}{1.23}\PYG{l+s}{\PYGZdq{}}\PYG{p}{)} | |
\PYG{g+go}{123/100} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Rational}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{1e\PYGZhy{}2}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+go}{1/100} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Rational}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s}{.1}\PYG{l+s}{\PYGZdq{}}\PYG{p}{)} | |
\PYG{g+go}{1/10} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Rational}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{1e\PYGZhy{}2/3.2}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+go}{1/320} | |
\end{Verbatim} | |
The conversion of other types of strings can be handled by | |
the sympify() function, and conversion of floats to expressions | |
or simple fractions can be handled with nsimplify: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{S}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{.[3]}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} \PYG{c}{\PYGZsh{} repeating digits in brackets} | |
\PYG{g+go}{1/3} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{S}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{3**2/10}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} \PYG{c}{\PYGZsh{} general expressions} | |
\PYG{g+go}{9/10} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{nsimplify}\PYG{p}{(}\PYG{o}{.}\PYG{l+m+mi}{3}\PYG{p}{)} \PYG{c}{\PYGZsh{} numbers that have a simple form} | |
\PYG{g+go}{3/10} | |
\end{Verbatim} | |
But if the input does not reduce to a literal Rational, an error will | |
be raised: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Rational}\PYG{p}{(}\PYG{n}{pi}\PYG{p}{)} | |
\PYG{g+gt}{Traceback (most recent call last):} | |
\PYG{c}{...} | |
\PYG{g+gr}{TypeError}: \PYG{n}{invalid input: pi} | |
\PYG{g+gt}{Traceback (most recent call last):} | |
\PYG{c}{...} | |
\PYG{g+gr}{TypeError}: \PYG{n}{invalid input: pi} | |
\end{Verbatim} | |
\paragraph{Low-level} | |
Access numerator and denominator as .p and .q: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{r} \PYG{o}{=} \PYG{n}{Rational}\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{4}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{r} | |
\PYG{g+go}{3/4} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{r}\PYG{o}{.}\PYG{n}{p} | |
\PYG{g+go}{3} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{r}\PYG{o}{.}\PYG{n}{q} | |
\PYG{g+go}{4} | |
\end{Verbatim} | |
Note that p and q return integers (not SymPy Integers) so some care | |
is needed when using them in expressions: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{r}\PYG{o}{.}\PYG{n}{p}\PYG{o}{/}\PYG{n}{r}\PYG{o}{.}\PYG{n}{q} | |
\PYG{g+go}{0.75} | |
\end{Verbatim} | |
\index{as\_content\_primitive() (sympy.core.numbers.Rational method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.numbers.Rational.as_content_primitive}\pysiglinewithargsret{\bfcode{as\_content\_primitive}}{\emph{radical=False}}{} | |
Return the tuple (R, self/R) where R is the positive Rational | |
extracted from self. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{S} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{S}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{3}\PYG{p}{)}\PYG{o}{/}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}content\PYGZus{}primitive}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{(3/2, \PYGZhy{}1)} | |
\end{Verbatim} | |
See docstring of Expr.as\_content\_primitive for more examples. | |
\end{fulllineitems} | |
\index{factors() (sympy.core.numbers.Rational method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.numbers.Rational.factors}\pysiglinewithargsret{\bfcode{factors}}{\emph{limit=None}, \emph{use\_trial=True}, \emph{use\_rho=False}, \emph{use\_pm1=False}, \emph{verbose=False}, \emph{visual=False}}{} | |
A wrapper to factorint which return factors of self that are | |
smaller than limit (or cheap to compute). Special methods of | |
factoring are disabled by default so that only trial division is used. | |
\end{fulllineitems} | |
\index{limit\_denominator() (sympy.core.numbers.Rational method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.numbers.Rational.limit_denominator}\pysiglinewithargsret{\bfcode{limit\_denominator}}{\emph{max\_denominator=1000000}}{} | |
Closest Rational to self with denominator at most max\_denominator. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{Rational} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Rational}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{3.141592653589793}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)}\PYG{o}{.}\PYG{n}{limit\PYGZus{}denominator}\PYG{p}{(}\PYG{l+m+mi}{10}\PYG{p}{)} | |
\PYG{g+go}{22/7} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Rational}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{3.141592653589793}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)}\PYG{o}{.}\PYG{n}{limit\PYGZus{}denominator}\PYG{p}{(}\PYG{l+m+mi}{100}\PYG{p}{)} | |
\PYG{g+go}{311/99} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\end{fulllineitems} | |
\subsubsection{Integer} | |
\label{modules/core:integer}\index{Integer (class in sympy.core.numbers)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.numbers.Integer}\pysigline{\strong{class }\code{sympy.core.numbers.}\bfcode{Integer}} | |
\end{fulllineitems} | |
\subsubsection{NumberSymbol} | |
\label{modules/core:numbersymbol}\index{NumberSymbol (class in sympy.core.numbers)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.numbers.NumberSymbol}\pysigline{\strong{class }\code{sympy.core.numbers.}\bfcode{NumberSymbol}}~\index{approximation() (sympy.core.numbers.NumberSymbol method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.numbers.NumberSymbol.approximation}\pysiglinewithargsret{\bfcode{approximation}}{\emph{number\_cls}}{} | |
Return an interval with number\_cls endpoints | |
that contains the value of NumberSymbol. | |
If not implemented, then return None. | |
\end{fulllineitems} | |
\end{fulllineitems} | |
\subsubsection{RealNumber} | |
\label{modules/core:realnumber}\index{RealNumber (in module sympy.core.numbers)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.numbers.RealNumber}\pysigline{\code{sympy.core.numbers.}\bfcode{RealNumber}} | |
alias of {\hyperref[modules/core:sympy.core.numbers.Float]{\emph{\code{Float}}}} (\autopageref*{modules/core:sympy.core.numbers.Float}) | |
\end{fulllineitems} | |
\subsubsection{igcd} | |
\label{modules/core:igcd}\index{igcd() (in module sympy.core.numbers)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.numbers.igcd}\pysiglinewithargsret{\code{sympy.core.numbers.}\bfcode{igcd}}{\emph{*args}}{} | |
Computes positive integer greatest common divisor. | |
The algorithm is based on the well known Euclid's algorithm. To | |
improve speed, igcd() has its own caching mechanism implemented. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.core.numbers} \PYG{k+kn}{import} \PYG{n}{igcd} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{igcd}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{4}\PYG{p}{)} | |
\PYG{g+go}{2} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{igcd}\PYG{p}{(}\PYG{l+m+mi}{5}\PYG{p}{,} \PYG{l+m+mi}{10}\PYG{p}{,} \PYG{l+m+mi}{15}\PYG{p}{)} | |
\PYG{g+go}{5} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\subsubsection{ilcm} | |
\label{modules/core:ilcm}\index{ilcm() (in module sympy.core.numbers)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.numbers.ilcm}\pysiglinewithargsret{\code{sympy.core.numbers.}\bfcode{ilcm}}{\emph{*args}}{} | |
Computes integer least common multiple. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.core.numbers} \PYG{k+kn}{import} \PYG{n}{ilcm} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{ilcm}\PYG{p}{(}\PYG{l+m+mi}{5}\PYG{p}{,} \PYG{l+m+mi}{10}\PYG{p}{)} | |
\PYG{g+go}{10} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{ilcm}\PYG{p}{(}\PYG{l+m+mi}{7}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{)} | |
\PYG{g+go}{21} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{ilcm}\PYG{p}{(}\PYG{l+m+mi}{5}\PYG{p}{,} \PYG{l+m+mi}{10}\PYG{p}{,} \PYG{l+m+mi}{15}\PYG{p}{)} | |
\PYG{g+go}{30} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\subsubsection{seterr} | |
\label{modules/core:seterr}\index{seterr() (in module sympy.core.numbers)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.numbers.seterr}\pysiglinewithargsret{\code{sympy.core.numbers.}\bfcode{seterr}}{\emph{divide=False}}{} | |
Should sympy raise an exception on 0/0 or return a nan? | |
divide == True .... raise an exception | |
divide == False ... return nan | |
\end{fulllineitems} | |
\subsubsection{Zero} | |
\label{modules/core:zero}\index{Zero (class in sympy.core.numbers)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.numbers.Zero}\pysigline{\strong{class }\code{sympy.core.numbers.}\bfcode{Zero}} | |
The number zero. | |
Zero is a singleton, and can be accessed by \code{S.Zero} | |
\paragraph{References} | |
\phantomsection\label{modules/core:id18}{\hyperref[modules/core:r33]{\emph{{[}R33{]}}}} (\autopageref*{modules/core:r33}) | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{S}\PYG{p}{,} \PYG{n}{Integer}\PYG{p}{,} \PYG{n}{zoo} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Integer}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{)} \PYG{o+ow}{is} \PYG{n}{S}\PYG{o}{.}\PYG{n}{Zero} | |
\PYG{g+go}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{l+m+mi}{1}\PYG{o}{/}\PYG{n}{S}\PYG{o}{.}\PYG{n}{Zero} | |
\PYG{g+go}{zoo} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\subsubsection{One} | |
\label{modules/core:one}\index{One (class in sympy.core.numbers)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.numbers.One}\pysigline{\strong{class }\code{sympy.core.numbers.}\bfcode{One}} | |
The number one. | |
One is a singleton, and can be accessed by \code{S.One}. | |
\paragraph{References} | |
\phantomsection\label{modules/core:id19}{\hyperref[modules/core:r34]{\emph{{[}R34{]}}}} (\autopageref*{modules/core:r34}) | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{S}\PYG{p}{,} \PYG{n}{Integer} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Integer}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{)} \PYG{o+ow}{is} \PYG{n}{S}\PYG{o}{.}\PYG{n}{One} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\subsubsection{NegativeOne} | |
\label{modules/core:negativeone}\index{NegativeOne (class in sympy.core.numbers)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.numbers.NegativeOne}\pysigline{\strong{class }\code{sympy.core.numbers.}\bfcode{NegativeOne}} | |
The number negative one. | |
NegativeOne is a singleton, and can be accessed by \code{S.NegativeOne}. | |
\strong{See also:} | |
{\hyperref[modules/core:sympy.core.numbers.One]{\emph{\code{One}}}} (\autopageref*{modules/core:sympy.core.numbers.One}) | |
\paragraph{References} | |
\phantomsection\label{modules/core:id20}{\hyperref[modules/core:r35]{\emph{{[}R35{]}}}} (\autopageref*{modules/core:r35}) | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{S}\PYG{p}{,} \PYG{n}{Integer} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Integer}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{1}\PYG{p}{)} \PYG{o+ow}{is} \PYG{n}{S}\PYG{o}{.}\PYG{n}{NegativeOne} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\subsubsection{Half} | |
\label{modules/core:half}\index{Half (class in sympy.core.numbers)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.numbers.Half}\pysigline{\strong{class }\code{sympy.core.numbers.}\bfcode{Half}} | |
The rational number 1/2. | |
Half is a singleton, and can be accessed by \code{S.Half}. | |
\paragraph{References} | |
\phantomsection\label{modules/core:id21}{\hyperref[modules/core:r36]{\emph{{[}R36{]}}}} (\autopageref*{modules/core:r36}) | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{S}\PYG{p}{,} \PYG{n}{Rational} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Rational}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{)} \PYG{o+ow}{is} \PYG{n}{S}\PYG{o}{.}\PYG{n}{Half} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\subsubsection{NaN} | |
\label{modules/core:nan}\index{NaN (class in sympy.core.numbers)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.numbers.NaN}\pysigline{\strong{class }\code{sympy.core.numbers.}\bfcode{NaN}} | |
Not a Number. | |
This represents the corresponding data type to floating point nan, which | |
is defined in the IEEE 754 floating point standard, and corresponds to the | |
Python \code{float('nan')}. | |
NaN serves as a place holder for numeric values that are indeterminate. | |
Most operations on NaN, produce another NaN. Most indeterminate forms, | |
such as \code{0/0} or \code{oo - oo{}` produce NaN. Two exceptions are {}`{}`0**0} | |
and \code{oo**0}, which all produce \code{1} (this is consistent with Python's | |
float). | |
NaN is mathematically not equal to anything else, even NaN itself. This | |
explains the initially counter-intuitive results with \code{Eq} and \code{==} in | |
the examples below. | |
NaN is a singleton, and can be accessed by \code{S.NaN}, or can be imported | |
as \code{nan}. | |
\paragraph{References} | |
\phantomsection\label{modules/core:id22}{\hyperref[modules/core:r37]{\emph{{[}R37{]}}}} (\autopageref*{modules/core:r37}) | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{nan}\PYG{p}{,} \PYG{n}{S}\PYG{p}{,} \PYG{n}{oo}\PYG{p}{,} \PYG{n}{Eq} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{nan} \PYG{o+ow}{is} \PYG{n}{S}\PYG{o}{.}\PYG{n}{NaN} | |
\PYG{g+go}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{oo} \PYG{o}{\PYGZhy{}} \PYG{n}{oo} | |
\PYG{g+go}{nan} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{nan} \PYG{o}{+} \PYG{l+m+mi}{1} | |
\PYG{g+go}{nan} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Eq}\PYG{p}{(}\PYG{n}{nan}\PYG{p}{,} \PYG{n}{nan}\PYG{p}{)} \PYG{c}{\PYGZsh{} mathematical equality} | |
\PYG{g+go}{False} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{nan} \PYG{o}{==} \PYG{n}{nan} \PYG{c}{\PYGZsh{} structural equality} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
\paragraph{Attributes} | |
\begin{tabulary}{\linewidth}{|L|L|} | |
\hline | |
is\_algebraic | |
& \\ | |
\hline | |
is\_finite | |
& \\ | |
\hline | |
is\_integer | |
& \\ | |
\hline | |
is\_negative | |
& \\ | |
\hline | |
is\_positive | |
& \\ | |
\hline | |
is\_prime | |
& \\ | |
\hline | |
is\_rational | |
& \\ | |
\hline | |
is\_real | |
& \\ | |
\hline | |
is\_transcendental | |
& \\ | |
\hline | |
is\_zero | |
& \\ | |
\hline\end{tabulary} | |
\end{fulllineitems} | |
\subsubsection{Infinity} | |
\label{modules/core:infinity}\index{Infinity (class in sympy.core.numbers)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.numbers.Infinity}\pysigline{\strong{class }\code{sympy.core.numbers.}\bfcode{Infinity}} | |
Positive infinite quantity. | |
In real analysis the symbol \(\infty\) denotes an unbounded | |
limit: \(x\to\infty\) means that \(x\) grows without bound. | |
Infinity is often used not only to define a limit but as a value | |
in the affinely extended real number system. Points labeled \(+\infty\) | |
and \(-\infty\) can be added to the topological space of the real numbers, | |
producing the two-point compactification of the real numbers. Adding | |
algebraic properties to this gives us the extended real numbers. | |
Infinity is a singleton, and can be accessed by \code{S.Infinity}, | |
or can be imported as \code{oo}. | |
\strong{See also:} | |
{\hyperref[modules/core:sympy.core.numbers.NegativeInfinity]{\emph{\code{NegativeInfinity}}}} (\autopageref*{modules/core:sympy.core.numbers.NegativeInfinity}), {\hyperref[modules/core:sympy.core.numbers.NaN]{\emph{\code{NaN}}}} (\autopageref*{modules/core:sympy.core.numbers.NaN}) | |
\paragraph{References} | |
\phantomsection\label{modules/core:id23}{\hyperref[modules/core:r38]{\emph{{[}R38{]}}}} (\autopageref*{modules/core:r38}) | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{oo}\PYG{p}{,} \PYG{n}{exp}\PYG{p}{,} \PYG{n}{limit}\PYG{p}{,} \PYG{n}{Symbol} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{l+m+mi}{1} \PYG{o}{+} \PYG{n}{oo} | |
\PYG{g+go}{oo} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{l+m+mi}{42}\PYG{o}{/}\PYG{n}{oo} | |
\PYG{g+go}{0} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x} \PYG{o}{=} \PYG{n}{Symbol}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{limit}\PYG{p}{(}\PYG{n}{exp}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{,} \PYG{n}{x}\PYG{p}{,} \PYG{n}{oo}\PYG{p}{)} | |
\PYG{g+go}{oo} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\subsubsection{NegativeInfinity} | |
\label{modules/core:negativeinfinity}\index{NegativeInfinity (class in sympy.core.numbers)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.numbers.NegativeInfinity}\pysigline{\strong{class }\code{sympy.core.numbers.}\bfcode{NegativeInfinity}} | |
Negative infinite quantity. | |
NegativeInfinity is a singleton, and can be accessed | |
by \code{S.NegativeInfinity}. | |
\strong{See also:} | |
{\hyperref[modules/core:sympy.core.numbers.Infinity]{\emph{\code{Infinity}}}} (\autopageref*{modules/core:sympy.core.numbers.Infinity}) | |
\end{fulllineitems} | |
\subsubsection{ComplexInfinity} | |
\label{modules/core:complexinfinity}\index{ComplexInfinity (class in sympy.core.numbers)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.numbers.ComplexInfinity}\pysigline{\strong{class }\code{sympy.core.numbers.}\bfcode{ComplexInfinity}} | |
Complex infinity. | |
In complex analysis the symbol \(\tilde\infty\), called ``complex | |
infinity'', represents a quantity with infinite magnitude, but | |
undetermined complex phase. | |
ComplexInfinity is a singleton, and can be accessed by | |
\code{S.ComplexInfinity}, or can be imported as \code{zoo}. | |
\strong{See also:} | |
{\hyperref[modules/core:sympy.core.numbers.Infinity]{\emph{\code{Infinity}}}} (\autopageref*{modules/core:sympy.core.numbers.Infinity}) | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{zoo}\PYG{p}{,} \PYG{n}{oo} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{zoo} \PYG{o}{+} \PYG{l+m+mi}{42} | |
\PYG{g+go}{zoo} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{l+m+mi}{42}\PYG{o}{/}\PYG{n}{zoo} | |
\PYG{g+go}{0} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{zoo} \PYG{o}{+} \PYG{n}{zoo} | |
\PYG{g+go}{nan} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{zoo}\PYG{o}{*}\PYG{n}{zoo} | |
\PYG{g+go}{zoo} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\subsubsection{Exp1} | |
\label{modules/core:exp1}\index{Exp1 (class in sympy.core.numbers)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.numbers.Exp1}\pysigline{\strong{class }\code{sympy.core.numbers.}\bfcode{Exp1}} | |
The \(e\) constant. | |
The transcendental number \(e = 2.718281828\dots\) is the base of the | |
natural logarithm and of the exponential function, \(e = \exp(1)\). | |
Sometimes called Euler's number or Napier's constant. | |
Exp1 is a singleton, and can be accessed by \code{S.Exp1}, | |
or can be imported as \code{E}. | |
\paragraph{References} | |
\phantomsection\label{modules/core:id24}{\hyperref[modules/core:r39]{\emph{{[}R39{]}}}} (\autopageref*{modules/core:r39}) | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{exp}\PYG{p}{,} \PYG{n}{log}\PYG{p}{,} \PYG{n}{E} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{E} \PYG{o+ow}{is} \PYG{n}{exp}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+go}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{log}\PYG{p}{(}\PYG{n}{E}\PYG{p}{)} | |
\PYG{g+go}{1} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\subsubsection{ImaginaryUnit} | |
\label{modules/core:imaginaryunit}\index{ImaginaryUnit (class in sympy.core.numbers)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.numbers.ImaginaryUnit}\pysigline{\strong{class }\code{sympy.core.numbers.}\bfcode{ImaginaryUnit}} | |
The imaginary unit, \(i = \sqrt{-1}\). | |
I is a singleton, and can be accessed by \code{S.I}, or can be | |
imported as \code{I}. | |
\paragraph{References} | |
\phantomsection\label{modules/core:id25}{\hyperref[modules/core:r40]{\emph{{[}R40{]}}}} (\autopageref*{modules/core:r40}) | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{I}\PYG{p}{,} \PYG{n}{sqrt} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{sqrt}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+go}{I} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{I}\PYG{o}{*}\PYG{n}{I} | |
\PYG{g+go}{\PYGZhy{}1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{l+m+mi}{1}\PYG{o}{/}\PYG{n}{I} | |
\PYG{g+go}{\PYGZhy{}I} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\subsubsection{Pi} | |
\label{modules/core:pi}\index{Pi (class in sympy.core.numbers)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.numbers.Pi}\pysigline{\strong{class }\code{sympy.core.numbers.}\bfcode{Pi}} | |
The \(\pi\) constant. | |
The transcendental number \(\pi = 3.141592654\dots\) represents the ratio | |
of a circle's circumference to its diameter, the area of the unit circle, | |
the half-period of trigonometric functions, and many other things | |
in mathematics. | |
Pi is a singleton, and can be accessed by \code{S.Pi}, or can | |
be imported as \code{pi}. | |
\paragraph{References} | |
\phantomsection\label{modules/core:id26}{\hyperref[modules/core:r41]{\emph{{[}R41{]}}}} (\autopageref*{modules/core:r41}) | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{S}\PYG{p}{,} \PYG{n}{pi}\PYG{p}{,} \PYG{n}{oo}\PYG{p}{,} \PYG{n}{sin}\PYG{p}{,} \PYG{n}{exp}\PYG{p}{,} \PYG{n}{integrate}\PYG{p}{,} \PYG{n}{Symbol} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{S}\PYG{o}{.}\PYG{n}{Pi} | |
\PYG{g+go}{pi} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{pi} \PYG{o}{\PYGZgt{}} \PYG{l+m+mi}{3} | |
\PYG{g+go}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{pi}\PYG{o}{.}\PYG{n}{is\PYGZus{}irrational} | |
\PYG{g+go}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x} \PYG{o}{=} \PYG{n}{Symbol}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{sin}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{pi}\PYG{p}{)} | |
\PYG{g+go}{sin(x)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{integrate}\PYG{p}{(}\PYG{n}{exp}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{,} \PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{o}{\PYGZhy{}}\PYG{n}{oo}\PYG{p}{,} \PYG{n}{oo}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{sqrt(pi)} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\subsubsection{EulerGamma} | |
\label{modules/core:eulergamma}\index{EulerGamma (class in sympy.core.numbers)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.numbers.EulerGamma}\pysigline{\strong{class }\code{sympy.core.numbers.}\bfcode{EulerGamma}} | |
The Euler-Mascheroni constant. | |
\(\gamma = 0.5772157\dots\) (also called Euler's constant) is a mathematical | |
constant recurring in analysis and number theory. It is defined as the | |
limiting difference between the harmonic series and the | |
natural logarithm: | |
\begin{gather} | |
\begin{split}\gamma = \lim\limits_{n\to\infty} | |
\left(\sum\limits_{k=1}^n\frac{1}{k} - \ln n\right)\end{split}\notag | |
\end{gather} | |
EulerGamma is a singleton, and can be accessed by \code{S.EulerGamma}. | |
\paragraph{References} | |
\phantomsection\label{modules/core:id27}{\hyperref[modules/core:r42]{\emph{{[}R42{]}}}} (\autopageref*{modules/core:r42}) | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{S} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{S}\PYG{o}{.}\PYG{n}{EulerGamma}\PYG{o}{.}\PYG{n}{is\PYGZus{}irrational} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{S}\PYG{o}{.}\PYG{n}{EulerGamma} \PYG{o}{\PYGZgt{}} \PYG{l+m+mi}{0} | |
\PYG{g+go}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{S}\PYG{o}{.}\PYG{n}{EulerGamma} \PYG{o}{\PYGZgt{}} \PYG{l+m+mi}{1} | |
\PYG{g+go}{False} | |
\end{Verbatim} | |
\paragraph{Attributes} | |
\begin{tabulary}{\linewidth}{|L|L|} | |
\hline | |
is\_irrational | |
& \\ | |
\hline\end{tabulary} | |
\end{fulllineitems} | |
\subsubsection{Catalan} | |
\label{modules/core:catalan}\index{Catalan (class in sympy.core.numbers)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.numbers.Catalan}\pysigline{\strong{class }\code{sympy.core.numbers.}\bfcode{Catalan}} | |
Catalan's constant. | |
\(K = 0.91596559\dots\) is given by the infinite series | |
\begin{gather} | |
\begin{split}K = \sum_{k=0}^{\infty} \frac{(-1)^k}{(2k+1)^2}\end{split}\notag | |
\end{gather} | |
Catalan is a singleton, and can be accessed by \code{S.Catalan}. | |
\paragraph{References} | |
\phantomsection\label{modules/core:id28}{\hyperref[modules/core:r43]{\emph{{[}R43{]}}}} (\autopageref*{modules/core:r43}) | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{S} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{S}\PYG{o}{.}\PYG{n}{Catalan}\PYG{o}{.}\PYG{n}{is\PYGZus{}irrational} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{S}\PYG{o}{.}\PYG{n}{Catalan} \PYG{o}{\PYGZgt{}} \PYG{l+m+mi}{0} | |
\PYG{g+go}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{S}\PYG{o}{.}\PYG{n}{Catalan} \PYG{o}{\PYGZgt{}} \PYG{l+m+mi}{1} | |
\PYG{g+go}{False} | |
\end{Verbatim} | |
\paragraph{Attributes} | |
\begin{tabulary}{\linewidth}{|L|L|} | |
\hline | |
is\_irrational | |
& \\ | |
\hline\end{tabulary} | |
\end{fulllineitems} | |
\subsubsection{GoldenRatio} | |
\label{modules/core:goldenratio}\index{GoldenRatio (class in sympy.core.numbers)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.numbers.GoldenRatio}\pysigline{\strong{class }\code{sympy.core.numbers.}\bfcode{GoldenRatio}} | |
The golden ratio, \(\phi\). | |
\(\phi = \frac{1 + \sqrt{5}}{2}\) is algebraic number. Two quantities | |
are in the golden ratio if their ratio is the same as the ratio of | |
their sum to the larger of the two quantities, i.e. their maximum. | |
GoldenRatio is a singleton, and can be accessed by \code{S.GoldenRatio}. | |
\paragraph{References} | |
\phantomsection\label{modules/core:id29}{\hyperref[modules/core:r44]{\emph{{[}R44{]}}}} (\autopageref*{modules/core:r44}) | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{S} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{S}\PYG{o}{.}\PYG{n}{GoldenRatio} \PYG{o}{\PYGZgt{}} \PYG{l+m+mi}{1} | |
\PYG{g+go}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{S}\PYG{o}{.}\PYG{n}{GoldenRatio}\PYG{o}{.}\PYG{n}{expand}\PYG{p}{(}\PYG{n}{func}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{1/2 + sqrt(5)/2} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{S}\PYG{o}{.}\PYG{n}{GoldenRatio}\PYG{o}{.}\PYG{n}{is\PYGZus{}irrational} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\subsection{power} | |
\label{modules/core:module-sympy.core.power}\label{modules/core:power}\index{sympy.core.power (module)} | |
\subsubsection{Pow} | |
\label{modules/core:pow}\index{Pow (class in sympy.core.power)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.power.Pow}\pysigline{\strong{class }\code{sympy.core.power.}\bfcode{Pow}} | |
Defines the expression x**y as ``x raised to a power y'' | |
Singleton definitions involving (0, 1, -1, oo, -oo): | |
\begin{tabulary}{\linewidth}{|L|L|L|} | |
\hline | |
\textsf{\relax | |
expr | |
} & \textsf{\relax | |
value | |
} & \textsf{\relax | |
reason | |
}\\ | |
\hline | |
z**0 | |
& | |
1 | |
& | |
Although arguments over 0**0 exist, see {[}2{]}. | |
\\ | |
\hline | |
z**1 | |
& | |
z | |
& \\ | |
\hline | |
(-oo)**(-1) | |
& | |
0 | |
& \\ | |
\hline | |
(-1)**-1 | |
& | |
-1 | |
& \\ | |
\hline | |
S.Zero**-1 | |
& | |
oo | |
& | |
This is not strictly true, as 0**-1 may be | |
undefined, but is convenient is some contexts | |
where the base is assumed to be positive. | |
\\ | |
\hline | |
1**-1 | |
& | |
1 | |
& \\ | |
\hline | |
oo**-1 | |
& | |
0 | |
& \\ | |
\hline | |
0**oo | |
& | |
0 | |
& | |
Because for all complex numbers z near | |
0, z**oo -\textgreater{} 0. | |
\\ | |
\hline | |
0**-oo | |
& | |
oo | |
& | |
This is not strictly true, as 0**oo may be | |
oscillating between positive and negative | |
values or rotating in the complex plane. | |
It is convenient, however, when the base | |
is positive. | |
\\ | |
\hline | |
1**oo | |
1**-oo | |
& | |
nan | |
& | |
Because there are various cases where | |
lim(x(t),t)=1, lim(y(t),t)=oo (or -oo), | |
but lim( x(t)**y(t), t) != 1. See {[}3{]}. | |
\\ | |
\hline | |
(-1)**oo | |
(-1)**(-oo) | |
& | |
nan | |
& | |
Because of oscillations in the limit. | |
\\ | |
\hline | |
oo**oo | |
& | |
oo | |
& \\ | |
\hline | |
oo**-oo | |
& | |
0 | |
& \\ | |
\hline | |
(-oo)**oo | |
(-oo)**-oo | |
& | |
nan | |
& \\ | |
\hline\end{tabulary} | |
Because symbolic computations are more flexible that floating point | |
calculations and we prefer to never return an incorrect answer, | |
we choose not to conform to all IEEE 754 conventions. This helps | |
us avoid extra test-case code in the calculation of limits. | |
\strong{See also:} | |
\code{Infinity}, \code{NegativeInfinity}, \code{NaN} | |
\paragraph{References} | |
\phantomsection\label{modules/core:id30}{\hyperref[modules/core:r45]{\emph{{[}R45{]}}}} (\autopageref*{modules/core:r45}), \phantomsection\label{modules/core:id31}{\hyperref[modules/core:r46]{\emph{{[}R46{]}}}} (\autopageref*{modules/core:r46}), \phantomsection\label{modules/core:id32}{\hyperref[modules/core:r47]{\emph{{[}R47{]}}}} (\autopageref*{modules/core:r47}) | |
\index{as\_base\_exp() (sympy.core.power.Pow method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.power.Pow.as_base_exp}\pysiglinewithargsret{\bfcode{as\_base\_exp}}{}{} | |
Return base and exp of self. | |
If base is 1/Integer, then return Integer, -exp. If this extra | |
processing is not needed, the base and exp properties will | |
give the raw arguments | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{Pow}\PYG{p}{,} \PYG{n}{S} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{Pow}\PYG{p}{(}\PYG{n}{S}\PYG{o}{.}\PYG{n}{Half}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{n}{evaluate}\PYG{o}{=}\PYG{n+nb+bp}{False}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{as\PYGZus{}base\PYGZus{}exp}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{(2, \PYGZhy{}2)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{args} | |
\PYG{g+go}{(1/2, 2)} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{as\_content\_primitive() (sympy.core.power.Pow method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.power.Pow.as_content_primitive}\pysiglinewithargsret{\bfcode{as\_content\_primitive}}{\emph{radical=False}}{} | |
Return the tuple (R, self/R) where R is the positive Rational | |
extracted from self. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{sqrt} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{sqrt}\PYG{p}{(}\PYG{l+m+mi}{4} \PYG{o}{+} \PYG{l+m+mi}{4}\PYG{o}{*}\PYG{n}{sqrt}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}content\PYGZus{}primitive}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{(2, sqrt(1 + sqrt(2)))} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{sqrt}\PYG{p}{(}\PYG{l+m+mi}{3} \PYG{o}{+} \PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{sqrt}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}content\PYGZus{}primitive}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{(1, sqrt(3)*sqrt(1 + sqrt(2)))} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{expand\PYGZus{}power\PYGZus{}base}\PYG{p}{,} \PYG{n}{powsimp}\PYG{p}{,} \PYG{n}{Mul} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}content\PYGZus{}primitive}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{(4, (x + 1)**2)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{4}\PYG{o}{*}\PYG{o}{*}\PYG{p}{(}\PYG{p}{(}\PYG{l+m+mi}{1} \PYG{o}{+} \PYG{n}{y}\PYG{p}{)}\PYG{o}{/}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}content\PYGZus{}primitive}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{(2, 4**(y/2))} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{o}{*}\PYG{o}{*}\PYG{p}{(}\PYG{p}{(}\PYG{l+m+mi}{1} \PYG{o}{+} \PYG{n}{y}\PYG{p}{)}\PYG{o}{/}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}content\PYGZus{}primitive}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{(1, 3**((y + 1)/2))} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{o}{*}\PYG{o}{*}\PYG{p}{(}\PYG{p}{(}\PYG{l+m+mi}{5} \PYG{o}{+} \PYG{n}{y}\PYG{p}{)}\PYG{o}{/}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}content\PYGZus{}primitive}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{(9, 3**((y + 1)/2))} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{eq} \PYG{o}{=} \PYG{l+m+mi}{3}\PYG{o}{*}\PYG{o}{*}\PYG{p}{(}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{powsimp}\PYG{p}{(}\PYG{n}{eq}\PYG{p}{)} \PYG{o}{==} \PYG{n}{eq} | |
\PYG{g+go}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{eq}\PYG{o}{.}\PYG{n}{as\PYGZus{}content\PYGZus{}primitive}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{(9, 3**(2*x))} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{powsimp}\PYG{p}{(}\PYG{n}{Mul}\PYG{p}{(}\PYG{o}{*}\PYG{n}{\PYGZus{}}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{3**(2*x + 2)} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{eq} \PYG{o}{=} \PYG{p}{(}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{n}{y} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{s} \PYG{o}{=} \PYG{n}{expand\PYGZus{}power\PYGZus{}base}\PYG{p}{(}\PYG{n}{eq}\PYG{p}{)}\PYG{p}{;} \PYG{n}{s}\PYG{o}{.}\PYG{n}{is\PYGZus{}Mul}\PYG{p}{,} \PYG{n}{s} | |
\PYG{g+go}{(False, (2*x + 2)**y)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{eq}\PYG{o}{.}\PYG{n}{as\PYGZus{}content\PYGZus{}primitive}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{(1, (2*(x + 1))**y)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{s} \PYG{o}{=} \PYG{n}{expand\PYGZus{}power\PYGZus{}base}\PYG{p}{(}\PYG{n}{\PYGZus{}}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{)}\PYG{p}{;} \PYG{n}{s}\PYG{o}{.}\PYG{n}{is\PYGZus{}Mul}\PYG{p}{,} \PYG{n}{s} | |
\PYG{g+go}{(True, 2**y*(x + 1)**y)} | |
\end{Verbatim} | |
See docstring of Expr.as\_content\_primitive for more examples. | |
\end{fulllineitems} | |
\end{fulllineitems} | |
\subsubsection{integer\_nthroot} | |
\label{modules/core:integer-nthroot}\index{integer\_nthroot() (in module sympy.core.power)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.power.integer_nthroot}\pysiglinewithargsret{\code{sympy.core.power.}\bfcode{integer\_nthroot}}{\emph{y}, \emph{n}}{} | |
Return a tuple containing x = floor(y**(1/n)) | |
and a boolean indicating whether the result is exact (that is, | |
whether x**n == y). | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{integer\PYGZus{}nthroot} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{integer\PYGZus{}nthroot}\PYG{p}{(}\PYG{l+m+mi}{16}\PYG{p}{,}\PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+go}{(4, True)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{integer\PYGZus{}nthroot}\PYG{p}{(}\PYG{l+m+mi}{26}\PYG{p}{,}\PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+go}{(5, False)} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\subsection{mul} | |
\label{modules/core:mul}\label{modules/core:module-sympy.core.mul}\index{sympy.core.mul (module)} | |
\subsubsection{Mul} | |
\label{modules/core:id33}\index{Mul (class in sympy.core.mul)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.mul.Mul}\pysigline{\strong{class }\code{sympy.core.mul.}\bfcode{Mul}}~\index{as\_coeff\_Mul() (sympy.core.mul.Mul method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.mul.Mul.as_coeff_Mul}\pysiglinewithargsret{\bfcode{as\_coeff\_Mul}}{\emph{rational=False}}{} | |
Efficiently extract the coefficient of a product. | |
\end{fulllineitems} | |
\index{as\_content\_primitive() (sympy.core.mul.Mul method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.mul.Mul.as_content_primitive}\pysiglinewithargsret{\bfcode{as\_content\_primitive}}{\emph{radical=False}}{} | |
Return the tuple (R, self/R) where R is the positive Rational | |
extracted from self. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{sqrt} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{sqrt}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{*}\PYG{p}{(}\PYG{l+m+mi}{2} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{sqrt}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}content\PYGZus{}primitive}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{(6, \PYGZhy{}sqrt(2)*(\PYGZhy{}sqrt(2) + 1))} | |
\end{Verbatim} | |
See docstring of Expr.as\_content\_primitive for more examples. | |
\end{fulllineitems} | |
\index{as\_ordered\_factors() (sympy.core.mul.Mul method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.mul.Mul.as_ordered_factors}\pysiglinewithargsret{\bfcode{as\_ordered\_factors}}{\emph{order=None}}{} | |
Transform an expression into an ordered list of factors. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{sin}\PYG{p}{,} \PYG{n}{cos} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{o}{*}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{n}{cos}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}ordered\PYGZus{}factors}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{[2, x, y, sin(x), cos(x)]} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{as\_two\_terms() (sympy.core.mul.Mul method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.mul.Mul.as_two_terms}\pysiglinewithargsret{\bfcode{as\_two\_terms}}{\emph{*args}, \emph{**kwargs}}{} | |
Return head and tail of self. | |
This is the most efficient way to get the head and tail of an | |
expression. | |
\begin{itemize} | |
\item {} | |
if you want only the head, use self.args{[}0{]}; | |
\item {} | |
if you want to process the arguments of the tail then use | |
self.as\_coef\_mul() which gives the head and a tuple containing | |
the arguments of the tail when treated as a Mul. | |
\item {} | |
if you want the coefficient when self is treated as an Add | |
then use self.as\_coeff\_add(){[}0{]} | |
\end{itemize} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}two\PYGZus{}terms}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{(3, x*y)} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{flatten() (sympy.core.mul.Mul class method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.mul.Mul.flatten}\pysiglinewithargsret{\strong{classmethod }\bfcode{flatten}}{\emph{seq}}{} | |
Return commutative, noncommutative and order arguments by | |
combining related terms. | |
\paragraph{Notes} | |
\begin{itemize} | |
\item {} | |
In an expression like \code{a*b*c}, python process this through sympy | |
as \code{Mul(Mul(a, b), c)}. This can have undesirable consequences. | |
\begin{itemize} | |
\item {} | |
Sometimes terms are not combined as one would like: | |
\{c.f. \href{https://github.com/sympy/sympy/issues/4596}{https://github.com/sympy/sympy/issues/4596}\} | |
\end{itemize} | |
\begin{quote} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{Mul}\PYG{p}{,} \PYG{n}{sqrt} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{z} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)} \PYG{c}{\PYGZsh{} this is the 2\PYGZhy{}arg Mul behavior} | |
\PYG{g+go}{2*x + 2} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{y}\PYG{o}{*}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{*}\PYG{l+m+mi}{2} | |
\PYG{g+go}{2*y*(x + 1)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{*}\PYG{n}{y} \PYG{c}{\PYGZsh{} 2\PYGZhy{}arg result will be obtained first} | |
\PYG{g+go}{y*(2*x + 2)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Mul}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{y}\PYG{p}{)} \PYG{c}{\PYGZsh{} all 3 args simultaneously processed} | |
\PYG{g+go}{2*y*(x + 1)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{p}{(}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)} \PYG{c}{\PYGZsh{} parentheses can control this behavior} | |
\PYG{g+go}{2*y*(x + 1)} | |
\end{Verbatim} | |
Powers with compound bases may not find a single base to | |
combine with unless all arguments are processed at once. | |
Post-processing may be necessary in such cases. | |
\{c.f. \href{https://github.com/sympy/sympy/issues/5728}{https://github.com/sympy/sympy/issues/5728}\} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a} \PYG{o}{=} \PYG{n}{sqrt}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{n}{sqrt}\PYG{p}{(}\PYG{n}{y}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{3} | |
\PYG{g+go}{(x*sqrt(y))**(3/2)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Mul}\PYG{p}{(}\PYG{n}{a}\PYG{p}{,}\PYG{n}{a}\PYG{p}{,}\PYG{n}{a}\PYG{p}{)} | |
\PYG{g+go}{(x*sqrt(y))**(3/2)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a}\PYG{o}{*}\PYG{n}{a}\PYG{o}{*}\PYG{n}{a} | |
\PYG{g+go}{x*sqrt(y)*sqrt(x*sqrt(y))} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{\PYGZus{}}\PYG{o}{.}\PYG{n}{subs}\PYG{p}{(}\PYG{n}{a}\PYG{o}{.}\PYG{n}{base}\PYG{p}{,} \PYG{n}{z}\PYG{p}{)}\PYG{o}{.}\PYG{n}{subs}\PYG{p}{(}\PYG{n}{z}\PYG{p}{,} \PYG{n}{a}\PYG{o}{.}\PYG{n}{base}\PYG{p}{)} | |
\PYG{g+go}{(x*sqrt(y))**(3/2)} | |
\end{Verbatim} | |
\end{quote} | |
\begin{itemize} | |
\item {} | |
If more than two terms are being multiplied then all the | |
previous terms will be re-processed for each new argument. | |
So if each of \code{a}, \code{b} and \code{c} were {\hyperref[modules/core:sympy.core.mul.Mul]{\emph{\code{Mul}}}} (\autopageref*{modules/core:sympy.core.mul.Mul}) | |
expression, then \code{a*b*c} (or building up the product | |
with \code{*=}) will process all the arguments of \code{a} and | |
\code{b} twice: once when \code{a*b} is computed and again when | |
\code{c} is multiplied. | |
Using \code{Mul(a, b, c)} will process all arguments once. | |
\end{itemize} | |
\item {} | |
The results of Mul are cached according to arguments, so flatten | |
will only be called once for \code{Mul(a, b, c)}. If you can | |
structure a calculation so the arguments are most likely to be | |
repeats then this can save time in computing the answer. For | |
example, say you had a Mul, M, that you wished to divide by \code{d{[}i{]}} | |
and multiply by \code{n{[}i{]}} and you suspect there are many repeats | |
in \code{n}. It would be better to compute \code{M*n{[}i{]}/d{[}i{]}} rather | |
than \code{M/d{[}i{]}*n{[}i{]}} since every time n{[}i{]} is a repeat, the | |
product, \code{M*n{[}i{]}} will be returned without flattening -- the | |
cached value will be returned. If you divide by the \code{d{[}i{]}} | |
first (and those are more unique than the \code{n{[}i{]}}) then that will | |
create a new Mul, \code{M/d{[}i{]}} the args of which will be traversed | |
again when it is multiplied by \code{n{[}i{]}}. | |
\{c.f. \href{https://github.com/sympy/sympy/issues/5706}{https://github.com/sympy/sympy/issues/5706}\} | |
This consideration is moot if the cache is turned off. | |
\end{itemize} | |
\paragraph{Nb} | |
The validity of the above notes depends on the implementation | |
details of Mul and flatten which may change at any time. Therefore, | |
you should only consider them when your code is highly performance | |
sensitive. | |
Removal of 1 from the sequence is already handled by AssocOp.\_\_new\_\_. | |
\end{fulllineitems} | |
\end{fulllineitems} | |
\subsubsection{prod} | |
\label{modules/core:prod}\index{prod() (in module sympy.core.mul)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.mul.prod}\pysiglinewithargsret{\code{sympy.core.mul.}\bfcode{prod}}{\emph{a}, \emph{start=1}}{}~\begin{description} | |
\item[{Return product of elements of a. Start with int 1 so if only}] \leavevmode | |
ints are included then an int result is returned. | |
\end{description} | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{prod}\PYG{p}{,} \PYG{n}{S} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{prod}\PYG{p}{(}\PYG{n+nb}{range}\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{0} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb}{type}\PYG{p}{(}\PYG{n}{\PYGZus{}}\PYG{p}{)} \PYG{o+ow}{is} \PYG{n+nb}{int} | |
\PYG{g+go}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{prod}\PYG{p}{(}\PYG{p}{[}\PYG{n}{S}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+go}{6} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{\PYGZus{}}\PYG{o}{.}\PYG{n}{is\PYGZus{}Integer} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
You can start the product at something other than 1: | |
\textgreater{}\textgreater{}\textgreater{} prod({[}1, 2{]}, 3) | |
6 | |
\end{fulllineitems} | |
\subsection{add} | |
\label{modules/core:add}\label{modules/core:module-sympy.core.add}\index{sympy.core.add (module)} | |
\subsubsection{Add} | |
\label{modules/core:id34}\index{Add (class in sympy.core.add)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.add.Add}\pysigline{\strong{class }\code{sympy.core.add.}\bfcode{Add}}~\index{as\_coeff\_Add() (sympy.core.add.Add method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.add.Add.as_coeff_Add}\pysiglinewithargsret{\bfcode{as\_coeff\_Add}}{}{} | |
Efficiently extract the coefficient of a summation. | |
\end{fulllineitems} | |
\index{as\_coeff\_add() (sympy.core.add.Add method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.add.Add.as_coeff_add}\pysiglinewithargsret{\bfcode{as\_coeff\_add}}{\emph{*args}, \emph{**kwargs}}{} | |
Returns a tuple (coeff, args) where self is treated as an Add and coeff | |
is the Number term and args is a tuple of all other terms. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{7} \PYG{o}{+} \PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}coeff\PYGZus{}add}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{(7, (3*x,))} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{7}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}coeff\PYGZus{}add}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{(0, (7*x,))} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{as\_coefficients\_dict() (sympy.core.add.Add method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.add.Add.as_coefficients_dict}\pysiglinewithargsret{\bfcode{as\_coefficients\_dict}}{\emph{a}}{} | |
Return a dictionary mapping terms to their Rational coefficient. | |
Since the dictionary is a defaultdict, inquiries about terms which | |
were not present will return a coefficient of 0. If an expression is | |
not an Add it is considered to have a single term. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{a}\PYG{p}{,} \PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{x} \PYG{o}{+} \PYG{n}{a}\PYG{o}{*}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{4}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}coefficients\PYGZus{}dict}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{\PYGZob{}1: 4, x: 3, a*x: 1\PYGZcb{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{\PYGZus{}}\PYG{p}{[}\PYG{n}{a}\PYG{p}{]} | |
\PYG{g+go}{0} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{a}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}coefficients\PYGZus{}dict}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{\PYGZob{}a*x: 3\PYGZcb{}} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{as\_content\_primitive() (sympy.core.add.Add method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.add.Add.as_content_primitive}\pysiglinewithargsret{\bfcode{as\_content\_primitive}}{\emph{radical=False}}{} | |
Return the tuple (R, self/R) where R is the positive Rational | |
extracted from self. If radical is True (default is False) then | |
common radicals will be removed and included as a factor of the | |
primitive expression. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{sqrt} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{3} \PYG{o}{+} \PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{sqrt}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}content\PYGZus{}primitive}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{(3, 1 + sqrt(2))} | |
\end{Verbatim} | |
Radical content can also be factored out of the primitive: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{sqrt}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{)} \PYG{o}{+} \PYG{l+m+mi}{4}\PYG{o}{*}\PYG{n}{sqrt}\PYG{p}{(}\PYG{l+m+mi}{10}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}content\PYGZus{}primitive}\PYG{p}{(}\PYG{n}{radical}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{(2, sqrt(2)*(1 + 2*sqrt(5)))} | |
\end{Verbatim} | |
See docstring of Expr.as\_content\_primitive for more examples. | |
\end{fulllineitems} | |
\index{as\_real\_imag() (sympy.core.add.Add method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.add.Add.as_real_imag}\pysiglinewithargsret{\bfcode{as\_real\_imag}}{\emph{deep=True}, \emph{**hints}}{} | |
returns a tuple representing a complex number | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{I} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{7} \PYG{o}{+} \PYG{l+m+mi}{9}\PYG{o}{*}\PYG{n}{I}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}real\PYGZus{}imag}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{(7, 9)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{p}{(}\PYG{l+m+mi}{1} \PYG{o}{+} \PYG{n}{I}\PYG{p}{)}\PYG{o}{/}\PYG{p}{(}\PYG{l+m+mi}{1} \PYG{o}{\PYGZhy{}} \PYG{n}{I}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}real\PYGZus{}imag}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{(0, 1)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{p}{(}\PYG{l+m+mi}{1} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{I}\PYG{p}{)}\PYG{o}{*}\PYG{p}{(}\PYG{l+m+mi}{1} \PYG{o}{+} \PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{I}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}real\PYGZus{}imag}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{(\PYGZhy{}5, 5)} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{as\_two\_terms() (sympy.core.add.Add method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.add.Add.as_two_terms}\pysiglinewithargsret{\bfcode{as\_two\_terms}}{\emph{*args}, \emph{**kwargs}}{} | |
Return head and tail of self. | |
This is the most efficient way to get the head and tail of an | |
expression. | |
\begin{itemize} | |
\item {} | |
if you want only the head, use self.args{[}0{]}; | |
\item {} | |
if you want to process the arguments of the tail then use | |
self.as\_coef\_add() which gives the head and a tuple containing | |
the arguments of the tail when treated as an Add. | |
\item {} | |
if you want the coefficient when self is treated as a Mul | |
then use self.as\_coeff\_mul(){[}0{]} | |
\end{itemize} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}two\PYGZus{}terms}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{(3, x*y)} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{class\_key() (sympy.core.add.Add class method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.add.Add.class_key}\pysiglinewithargsret{\strong{classmethod }\bfcode{class\_key}}{}{} | |
Nice order of classes | |
\end{fulllineitems} | |
\index{extract\_leading\_order() (sympy.core.add.Add method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.add.Add.extract_leading_order}\pysiglinewithargsret{\bfcode{extract\_leading\_order}}{\emph{*args}, \emph{**kwargs}}{} | |
Returns the leading term and its order. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{o}{/}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{5}\PYG{p}{)}\PYG{o}{.}\PYG{n}{extract\PYGZus{}leading\PYGZus{}order}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{((x**(\PYGZhy{}5), O(x**(\PYGZhy{}5))),)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{1} \PYG{o}{+} \PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{extract\PYGZus{}leading\PYGZus{}order}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{((1, O(1)),)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{.}\PYG{n}{extract\PYGZus{}leading\PYGZus{}order}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{((x, O(x)),)} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{flatten() (sympy.core.add.Add class method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.add.Add.flatten}\pysiglinewithargsret{\strong{classmethod }\bfcode{flatten}}{\emph{seq}}{} | |
Takes the sequence ``seq'' of nested Adds and returns a flatten list. | |
Returns: (commutative\_part, noncommutative\_part, order\_symbols) | |
Applies associativity, all terms are commutable with respect to | |
addition. | |
NB: the removal of 0 is already handled by AssocOp.\_\_new\_\_ | |
\strong{See also:} | |
{\hyperref[modules/core:sympy.core.mul.Mul.flatten]{\emph{\code{sympy.core.mul.Mul.flatten}}}} (\autopageref*{modules/core:sympy.core.mul.Mul.flatten}) | |
\end{fulllineitems} | |
\index{primitive() (sympy.core.add.Add method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.add.Add.primitive}\pysiglinewithargsret{\bfcode{primitive}}{}{} | |
Return \code{(R, self/R)} where \code{R{}`} is the Rational GCD of \code{self{}`}. | |
\code{R} is collected only from the leading coefficient of each term. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{4}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{primitive}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{(2, x + 2*y)} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{o}{/}\PYG{l+m+mi}{3} \PYG{o}{+} \PYG{l+m+mi}{4}\PYG{o}{*}\PYG{n}{y}\PYG{o}{/}\PYG{l+m+mi}{9}\PYG{p}{)}\PYG{o}{.}\PYG{n}{primitive}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{(2/9, 3*x + 2*y)} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{o}{/}\PYG{l+m+mi}{3} \PYG{o}{+} \PYG{l+m+mf}{4.2}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{primitive}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{(1/3, 2*x + 12.6*y)} | |
\end{Verbatim} | |
No subprocessing of term factors is performed: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{p}{(}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{.}\PYG{n}{primitive}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{(1, x*(2*x + 2) + 2)} | |
\end{Verbatim} | |
Recursive subprocessing can be done with the as\_content\_primitive() | |
method: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{p}{(}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}content\PYGZus{}primitive}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{(2, x*(x + 1) + 1)} | |
\end{Verbatim} | |
See also: primitive() function in polytools.py | |
\end{fulllineitems} | |
\end{fulllineitems} | |
\subsection{mod} | |
\label{modules/core:module-sympy.core.mod}\label{modules/core:mod}\index{sympy.core.mod (module)} | |
\subsubsection{Mod} | |
\label{modules/core:id35}\index{Mod (class in sympy.core.mod)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.mod.Mod}\pysigline{\strong{class }\code{sympy.core.mod.}\bfcode{Mod}} | |
Represents a modulo operation on symbolic expressions. | |
Receives two arguments, dividend p and divisor q. | |
The convention used is the same as Python's: the remainder always has the | |
same sign as the divisor. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{\PYGZpc{}} \PYG{n}{y} | |
\PYG{g+go}{Mod(x**2, y)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{\PYGZus{}}\PYG{o}{.}\PYG{n}{subs}\PYG{p}{(}\PYG{p}{\PYGZob{}}\PYG{n}{x}\PYG{p}{:} \PYG{l+m+mi}{5}\PYG{p}{,} \PYG{n}{y}\PYG{p}{:} \PYG{l+m+mi}{6}\PYG{p}{\PYGZcb{}}\PYG{p}{)} | |
\PYG{g+go}{1} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\subsection{relational} | |
\label{modules/core:relational}\label{modules/core:module-sympy.core.relational}\index{sympy.core.relational (module)} | |
\subsubsection{Rel} | |
\label{modules/core:rel}\index{Rel (in module sympy.core.relational)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.relational.Rel}\pysigline{\code{sympy.core.relational.}\bfcode{Rel}} | |
alias of \code{Relational} | |
\end{fulllineitems} | |
\subsubsection{Eq} | |
\label{modules/core:eq}\index{Eq (in module sympy.core.relational)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.relational.Eq}\pysigline{\code{sympy.core.relational.}\bfcode{Eq}} | |
alias of {\hyperref[modules/core:sympy.core.relational.Equality]{\emph{\code{Equality}}}} (\autopageref*{modules/core:sympy.core.relational.Equality}) | |
\end{fulllineitems} | |
\subsubsection{Ne} | |
\label{modules/core:ne}\index{Ne (in module sympy.core.relational)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.relational.Ne}\pysigline{\code{sympy.core.relational.}\bfcode{Ne}} | |
alias of {\hyperref[modules/core:sympy.core.relational.Unequality]{\emph{\code{Unequality}}}} (\autopageref*{modules/core:sympy.core.relational.Unequality}) | |
\end{fulllineitems} | |
\subsubsection{Lt} | |
\label{modules/core:lt}\index{Lt (in module sympy.core.relational)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.relational.Lt}\pysigline{\code{sympy.core.relational.}\bfcode{Lt}} | |
alias of {\hyperref[modules/core:sympy.core.relational.StrictLessThan]{\emph{\code{StrictLessThan}}}} (\autopageref*{modules/core:sympy.core.relational.StrictLessThan}) | |
\end{fulllineitems} | |
\subsubsection{Le} | |
\label{modules/core:le}\index{Le (in module sympy.core.relational)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.relational.Le}\pysigline{\code{sympy.core.relational.}\bfcode{Le}} | |
alias of {\hyperref[modules/core:sympy.core.relational.LessThan]{\emph{\code{LessThan}}}} (\autopageref*{modules/core:sympy.core.relational.LessThan}) | |
\end{fulllineitems} | |
\subsubsection{Gt} | |
\label{modules/core:gt}\index{Gt (in module sympy.core.relational)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.relational.Gt}\pysigline{\code{sympy.core.relational.}\bfcode{Gt}} | |
alias of {\hyperref[modules/core:sympy.core.relational.StrictGreaterThan]{\emph{\code{StrictGreaterThan}}}} (\autopageref*{modules/core:sympy.core.relational.StrictGreaterThan}) | |
\end{fulllineitems} | |
\subsubsection{Ge} | |
\label{modules/core:ge}\index{Ge (in module sympy.core.relational)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.relational.Ge}\pysigline{\code{sympy.core.relational.}\bfcode{Ge}} | |
alias of {\hyperref[modules/core:sympy.core.relational.GreaterThan]{\emph{\code{GreaterThan}}}} (\autopageref*{modules/core:sympy.core.relational.GreaterThan}) | |
\end{fulllineitems} | |
\subsubsection{Equality} | |
\label{modules/core:equality}\index{Equality (class in sympy.core.relational)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.relational.Equality}\pysigline{\strong{class }\code{sympy.core.relational.}\bfcode{Equality}} | |
An equal relation between two objects. | |
Represents that two objects are equal. If they can be easily shown | |
to be definitively equal (or unequal), this will reduce to True (or | |
False). Otherwise, the relation is maintained as an unevaluated | |
Equality object. Use the \code{simplify} function on this object for | |
more nontrivial evaluation of the equality relation. | |
\strong{See also:} | |
\begin{description} | |
\item[{{\hyperref[modules/logic:sympy.logic.boolalg.Equivalent]{\emph{\code{sympy.logic.boolalg.Equivalent}}}} (\autopageref*{modules/logic:sympy.logic.boolalg.Equivalent})}] \leavevmode | |
for representing equality between two boolean expressions | |
\end{description} | |
\paragraph{Notes} | |
This class is not the same as the == operator. The == operator tests | |
for exact structural equality between two expressions; this class | |
compares expressions mathematically. | |
If either object defines an \(_eval_Eq\) method, it can be used in place of | |
the default algorithm. If \(lhs._eval_Eq(rhs)\) or \(rhs._eval_Eq(lhs)\) | |
returns anything other than None, that return value will be substituted for | |
the Equality. If None is returned by \(_eval_Eq\), an Equality object will | |
be created as usual. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{Eq} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Eq}\PYG{p}{(}\PYG{n}{y}\PYG{p}{,} \PYG{n}{x}\PYG{o}{+}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+go}{y == x**2 + x} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\subsubsection{GreaterThan} | |
\label{modules/core:greaterthan}\index{GreaterThan (class in sympy.core.relational)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.relational.GreaterThan}\pysigline{\strong{class }\code{sympy.core.relational.}\bfcode{GreaterThan}} | |
Class representations of inequalities. | |
The \code{*Than} classes represent inequal relationships, where the left-hand | |
side is generally bigger or smaller than the right-hand side. For example, | |
the GreaterThan class represents an inequal relationship where the | |
left-hand side is at least as big as the right side, if not bigger. In | |
mathematical notation: | |
lhs \textgreater{}= rhs | |
In total, there are four \code{*Than} classes, to represent the four | |
inequalities: | |
\begin{tabulary}{\linewidth}{|L|L|} | |
\hline | |
\textsf{\relax | |
Class Name | |
} & \textsf{\relax | |
Symbol | |
}\\ | |
\hline | |
GreaterThan | |
& | |
(\textgreater{}=) | |
\\ | |
\hline | |
LessThan | |
& | |
(\textless{}=) | |
\\ | |
\hline | |
StrictGreaterThan | |
& | |
(\textgreater{}) | |
\\ | |
\hline | |
StrictLessThan | |
& | |
(\textless{}) | |
\\ | |
\hline\end{tabulary} | |
All classes take two arguments, lhs and rhs. | |
\begin{tabulary}{\linewidth}{|L|L|} | |
\hline | |
\textsf{\relax | |
Signature Example | |
} & \textsf{\relax | |
Math equivalent | |
}\\ | |
\hline | |
GreaterThan(lhs, rhs) | |
& | |
lhs \textgreater{}= rhs | |
\\ | |
\hline | |
LessThan(lhs, rhs) | |
& | |
lhs \textless{}= rhs | |
\\ | |
\hline | |
StrictGreaterThan(lhs, rhs) | |
& | |
lhs \textgreater{} rhs | |
\\ | |
\hline | |
StrictLessThan(lhs, rhs) | |
& | |
lhs \textless{} rhs | |
\\ | |
\hline\end{tabulary} | |
In addition to the normal .lhs and .rhs of Relations, \code{*Than} inequality | |
objects also have the .lts and .gts properties, which represent the ``less | |
than side'' and ``greater than side'' of the operator. Use of .lts and .gts | |
in an algorithm rather than .lhs and .rhs as an assumption of inequality | |
direction will make more explicit the intent of a certain section of code, | |
and will make it similarly more robust to client code changes: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{GreaterThan}\PYG{p}{,} \PYG{n}{StrictGreaterThan} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{LessThan}\PYG{p}{,} \PYG{n}{StrictLessThan} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{And}\PYG{p}{,} \PYG{n}{Ge}\PYG{p}{,} \PYG{n}{Gt}\PYG{p}{,} \PYG{n}{Le}\PYG{p}{,} \PYG{n}{Lt}\PYG{p}{,} \PYG{n}{Rel}\PYG{p}{,} \PYG{n}{S} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{z} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.core.relational} \PYG{k+kn}{import} \PYG{n}{Relational} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e} \PYG{o}{=} \PYG{n}{GreaterThan}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e} | |
\PYG{g+go}{x \PYGZgt{}= 1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{l+s}{\PYGZsq{}}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{ \PYGZgt{}= }\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{ is the same as }\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{ \PYGZlt{}= }\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{\PYGZsq{}} \PYG{o}{\PYGZpc{}} \PYG{p}{(}\PYG{n}{e}\PYG{o}{.}\PYG{n}{gts}\PYG{p}{,} \PYG{n}{e}\PYG{o}{.}\PYG{n}{lts}\PYG{p}{,} \PYG{n}{e}\PYG{o}{.}\PYG{n}{lts}\PYG{p}{,} \PYG{n}{e}\PYG{o}{.}\PYG{n}{gts}\PYG{p}{)} | |
\PYG{g+go}{\PYGZsq{}x \PYGZgt{}= 1 is the same as 1 \PYGZlt{}= x\PYGZsq{}} | |
\end{Verbatim} | |
\paragraph{Notes} | |
There are a couple of ``gotchas'' when using Python's operators. | |
The first enters the mix when comparing against a literal number as the lhs | |
argument. Due to the order that Python decides to parse a statement, it may | |
not immediately find two objects comparable. For example, to evaluate the | |
statement (1 \textless{} x), Python will first recognize the number 1 as a native | |
number, and then that x is \emph{not} a native number. At this point, because a | |
native Python number does not know how to compare itself with a SymPy object | |
Python will try the reflective operation, (x \textgreater{} 1). Unfortunately, there is | |
no way available to SymPy to recognize this has happened, so the statement | |
(1 \textless{} x) will turn silently into (x \textgreater{} 1). | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e1} \PYG{o}{=} \PYG{n}{x} \PYG{o}{\PYGZgt{}} \PYG{l+m+mi}{1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e2} \PYG{o}{=} \PYG{n}{x} \PYG{o}{\PYGZgt{}}\PYG{o}{=} \PYG{l+m+mi}{1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e3} \PYG{o}{=} \PYG{n}{x} \PYG{o}{\PYGZlt{}} \PYG{l+m+mi}{1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e4} \PYG{o}{=} \PYG{n}{x} \PYG{o}{\PYGZlt{}}\PYG{o}{=} \PYG{l+m+mi}{1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e5} \PYG{o}{=} \PYG{l+m+mi}{1} \PYG{o}{\PYGZgt{}} \PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e6} \PYG{o}{=} \PYG{l+m+mi}{1} \PYG{o}{\PYGZgt{}}\PYG{o}{=} \PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e7} \PYG{o}{=} \PYG{l+m+mi}{1} \PYG{o}{\PYGZlt{}} \PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e8} \PYG{o}{=} \PYG{l+m+mi}{1} \PYG{o}{\PYGZlt{}}\PYG{o}{=} \PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{ }\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s}{\PYGZdq{}}\PYG{o}{*}\PYG{l+m+mi}{4} \PYG{o}{\PYGZpc{}} \PYG{p}{(}\PYG{n}{e1}\PYG{p}{,} \PYG{n}{e2}\PYG{p}{,} \PYG{n}{e3}\PYG{p}{,} \PYG{n}{e4}\PYG{p}{,} \PYG{n}{e5}\PYG{p}{,} \PYG{n}{e6}\PYG{p}{,} \PYG{n}{e7}\PYG{p}{,} \PYG{n}{e8}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{x \PYGZgt{} 1 x \PYGZgt{}= 1} | |
\PYG{g+go}{x \PYGZlt{} 1 x \PYGZlt{}= 1} | |
\PYG{g+go}{x \PYGZlt{} 1 x \PYGZlt{}= 1} | |
\PYG{g+go}{x \PYGZgt{} 1 x \PYGZgt{}= 1} | |
\end{Verbatim} | |
If the order of the statement is important (for visual output to the | |
console, perhaps), one can work around this annoyance in a couple ways: (1) | |
``sympify'' the literal before comparison, (2) use one of the wrappers, or (3) | |
use the less succinct methods described above: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e1} \PYG{o}{=} \PYG{n}{S}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{)} \PYG{o}{\PYGZgt{}} \PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e2} \PYG{o}{=} \PYG{n}{S}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{)} \PYG{o}{\PYGZgt{}}\PYG{o}{=} \PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e3} \PYG{o}{=} \PYG{n}{S}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{)} \PYG{o}{\PYGZlt{}} \PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e4} \PYG{o}{=} \PYG{n}{S}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{)} \PYG{o}{\PYGZlt{}}\PYG{o}{=} \PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e5} \PYG{o}{=} \PYG{n}{Gt}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e6} \PYG{o}{=} \PYG{n}{Ge}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e7} \PYG{o}{=} \PYG{n}{Lt}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e8} \PYG{o}{=} \PYG{n}{Le}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{ }\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s}{\PYGZdq{}}\PYG{o}{*}\PYG{l+m+mi}{4} \PYG{o}{\PYGZpc{}} \PYG{p}{(}\PYG{n}{e1}\PYG{p}{,} \PYG{n}{e2}\PYG{p}{,} \PYG{n}{e3}\PYG{p}{,} \PYG{n}{e4}\PYG{p}{,} \PYG{n}{e5}\PYG{p}{,} \PYG{n}{e6}\PYG{p}{,} \PYG{n}{e7}\PYG{p}{,} \PYG{n}{e8}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{1 \PYGZgt{} x 1 \PYGZgt{}= x} | |
\PYG{g+go}{1 \PYGZlt{} x 1 \PYGZlt{}= x} | |
\PYG{g+go}{1 \PYGZgt{} x 1 \PYGZgt{}= x} | |
\PYG{g+go}{1 \PYGZlt{} x 1 \PYGZlt{}= x} | |
\end{Verbatim} | |
The other gotcha is with chained inequalities. Occasionally, one may be | |
tempted to write statements like: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e} \PYG{o}{=} \PYG{n}{x} \PYG{o}{\PYGZlt{}} \PYG{n}{y} \PYG{o}{\PYGZlt{}} \PYG{n}{z} | |
\PYG{g+gt}{Traceback (most recent call last):} | |
\PYG{c}{...} | |
\PYG{g+gr}{TypeError}: \PYG{n}{symbolic boolean expression has no truth value.} | |
\PYG{g+gt}{Traceback (most recent call last):} | |
\PYG{c}{...} | |
\PYG{g+gr}{TypeError}: \PYG{n}{symbolic boolean expression has no truth value.} | |
\end{Verbatim} | |
Due to an implementation detail or decision of Python \phantomsection\label{modules/core:id36}{\hyperref[modules/core:r48]{\emph{{[}R48{]}}}} (\autopageref*{modules/core:r48}), there is no way | |
for SymPy to reliably create that as a chained inequality. To create a | |
chained inequality, the only method currently available is to make use of | |
And: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e} \PYG{o}{=} \PYG{n}{And}\PYG{p}{(}\PYG{n}{x} \PYG{o}{\PYGZlt{}} \PYG{n}{y}\PYG{p}{,} \PYG{n}{y} \PYG{o}{\PYGZlt{}} \PYG{n}{z}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb}{type}\PYG{p}{(} \PYG{n}{e} \PYG{p}{)} | |
\PYG{g+go}{And} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e} | |
\PYG{g+go}{And(x \PYGZlt{} y, y \PYGZlt{} z)} | |
\end{Verbatim} | |
Note that this is different than chaining an equality directly via use of | |
parenthesis (this is currently an open bug in SymPy \phantomsection\label{modules/core:id37}{\hyperref[modules/core:r49]{\emph{{[}R49{]}}}} (\autopageref*{modules/core:r49})): | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e} \PYG{o}{=} \PYG{p}{(}\PYG{n}{x} \PYG{o}{\PYGZlt{}} \PYG{n}{y}\PYG{p}{)} \PYG{o}{\PYGZlt{}} \PYG{n}{z} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb}{type}\PYG{p}{(} \PYG{n}{e} \PYG{p}{)} | |
\PYG{g+go}{\PYGZlt{}class \PYGZsq{}sympy.core.relational.StrictLessThan\PYGZsq{}\PYGZgt{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e} | |
\PYG{g+go}{(x \PYGZlt{} y) \PYGZlt{} z} | |
\end{Verbatim} | |
Any code that explicitly relies on this latter functionality will not be | |
robust as this behaviour is completely wrong and will be corrected at some | |
point. For the time being (circa Jan 2012), use And to create chained | |
inequalities. | |
\paragraph{Examples} | |
One generally does not instantiate these classes directly, but uses various | |
convenience methods: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e1} \PYG{o}{=} \PYG{n}{Ge}\PYG{p}{(} \PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{2} \PYG{p}{)} \PYG{c}{\PYGZsh{} Ge is a convenience wrapper} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{n}{e1}\PYG{p}{)} | |
\PYG{g+go}{x \PYGZgt{}= 2} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{rels} \PYG{o}{=} \PYG{n}{Ge}\PYG{p}{(} \PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{2} \PYG{p}{)}\PYG{p}{,} \PYG{n}{Gt}\PYG{p}{(} \PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{2} \PYG{p}{)}\PYG{p}{,} \PYG{n}{Le}\PYG{p}{(} \PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{2} \PYG{p}{)}\PYG{p}{,} \PYG{n}{Lt}\PYG{p}{(} \PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{2} \PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{\PYGZsq{}} \PYG{o}{\PYGZpc{}} \PYG{n}{rels}\PYG{p}{)} | |
\PYG{g+go}{x \PYGZgt{}= 2} | |
\PYG{g+go}{x \PYGZgt{} 2} | |
\PYG{g+go}{x \PYGZlt{}= 2} | |
\PYG{g+go}{x \PYGZlt{} 2} | |
\end{Verbatim} | |
Another option is to use the Python inequality operators (\textgreater{}=, \textgreater{}, \textless{}=, \textless{}) | |
directly. Their main advantage over the Ge, Gt, Le, and Lt counterparts, is | |
that one can write a more ``mathematical looking'' statement rather than | |
littering the math with oddball function calls. However there are certain | |
(minor) caveats of which to be aware (search for `gotcha', below). | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e2} \PYG{o}{=} \PYG{n}{x} \PYG{o}{\PYGZgt{}}\PYG{o}{=} \PYG{l+m+mi}{2} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{n}{e2}\PYG{p}{)} | |
\PYG{g+go}{x \PYGZgt{}= 2} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s}{e1: }\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{, e2: }\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{\PYGZdq{}} \PYG{o}{\PYGZpc{}} \PYG{p}{(}\PYG{n}{e1}\PYG{p}{,} \PYG{n}{e2}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{e1: x \PYGZgt{}= 2, e2: x \PYGZgt{}= 2} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e1} \PYG{o}{==} \PYG{n}{e2} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
However, it is also perfectly valid to instantiate a \code{*Than} class less | |
succinctly and less conveniently: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{rels} \PYG{o}{=} \PYG{n}{Rel}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{\PYGZgt{}=}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)}\PYG{p}{,} \PYG{n}{Relational}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{\PYGZgt{}=}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)}\PYG{p}{,} \PYG{n}{GreaterThan}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{\PYGZsq{}} \PYG{o}{\PYGZpc{}} \PYG{n}{rels}\PYG{p}{)} | |
\PYG{g+go}{x \PYGZgt{}= 1} | |
\PYG{g+go}{x \PYGZgt{}= 1} | |
\PYG{g+go}{x \PYGZgt{}= 1} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{rels} \PYG{o}{=} \PYG{n}{Rel}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{\PYGZgt{}}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)}\PYG{p}{,} \PYG{n}{Relational}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{\PYGZgt{}}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)}\PYG{p}{,} \PYG{n}{StrictGreaterThan}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{\PYGZsq{}} \PYG{o}{\PYGZpc{}} \PYG{n}{rels}\PYG{p}{)} | |
\PYG{g+go}{x \PYGZgt{} 1} | |
\PYG{g+go}{x \PYGZgt{} 1} | |
\PYG{g+go}{x \PYGZgt{} 1} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{rels} \PYG{o}{=} \PYG{n}{Rel}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{\PYGZlt{}=}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)}\PYG{p}{,} \PYG{n}{Relational}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{\PYGZlt{}=}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)}\PYG{p}{,} \PYG{n}{LessThan}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{\PYGZdq{}} \PYG{o}{\PYGZpc{}} \PYG{n}{rels}\PYG{p}{)} | |
\PYG{g+go}{x \PYGZlt{}= 1} | |
\PYG{g+go}{x \PYGZlt{}= 1} | |
\PYG{g+go}{x \PYGZlt{}= 1} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{rels} \PYG{o}{=} \PYG{n}{Rel}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{\PYGZlt{}}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)}\PYG{p}{,} \PYG{n}{Relational}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{\PYGZlt{}}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)}\PYG{p}{,} \PYG{n}{StrictLessThan}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{\PYGZsq{}} \PYG{o}{\PYGZpc{}} \PYG{n}{rels}\PYG{p}{)} | |
\PYG{g+go}{x \PYGZlt{} 1} | |
\PYG{g+go}{x \PYGZlt{} 1} | |
\PYG{g+go}{x \PYGZlt{} 1} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\subsubsection{LessThan} | |
\label{modules/core:lessthan}\index{LessThan (class in sympy.core.relational)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.relational.LessThan}\pysigline{\strong{class }\code{sympy.core.relational.}\bfcode{LessThan}} | |
Class representations of inequalities. | |
The \code{*Than} classes represent inequal relationships, where the left-hand | |
side is generally bigger or smaller than the right-hand side. For example, | |
the GreaterThan class represents an inequal relationship where the | |
left-hand side is at least as big as the right side, if not bigger. In | |
mathematical notation: | |
lhs \textgreater{}= rhs | |
In total, there are four \code{*Than} classes, to represent the four | |
inequalities: | |
\begin{tabulary}{\linewidth}{|L|L|} | |
\hline | |
\textsf{\relax | |
Class Name | |
} & \textsf{\relax | |
Symbol | |
}\\ | |
\hline | |
GreaterThan | |
& | |
(\textgreater{}=) | |
\\ | |
\hline | |
LessThan | |
& | |
(\textless{}=) | |
\\ | |
\hline | |
StrictGreaterThan | |
& | |
(\textgreater{}) | |
\\ | |
\hline | |
StrictLessThan | |
& | |
(\textless{}) | |
\\ | |
\hline\end{tabulary} | |
All classes take two arguments, lhs and rhs. | |
\begin{tabulary}{\linewidth}{|L|L|} | |
\hline | |
\textsf{\relax | |
Signature Example | |
} & \textsf{\relax | |
Math equivalent | |
}\\ | |
\hline | |
GreaterThan(lhs, rhs) | |
& | |
lhs \textgreater{}= rhs | |
\\ | |
\hline | |
LessThan(lhs, rhs) | |
& | |
lhs \textless{}= rhs | |
\\ | |
\hline | |
StrictGreaterThan(lhs, rhs) | |
& | |
lhs \textgreater{} rhs | |
\\ | |
\hline | |
StrictLessThan(lhs, rhs) | |
& | |
lhs \textless{} rhs | |
\\ | |
\hline\end{tabulary} | |
In addition to the normal .lhs and .rhs of Relations, \code{*Than} inequality | |
objects also have the .lts and .gts properties, which represent the ``less | |
than side'' and ``greater than side'' of the operator. Use of .lts and .gts | |
in an algorithm rather than .lhs and .rhs as an assumption of inequality | |
direction will make more explicit the intent of a certain section of code, | |
and will make it similarly more robust to client code changes: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{GreaterThan}\PYG{p}{,} \PYG{n}{StrictGreaterThan} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{LessThan}\PYG{p}{,} \PYG{n}{StrictLessThan} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{And}\PYG{p}{,} \PYG{n}{Ge}\PYG{p}{,} \PYG{n}{Gt}\PYG{p}{,} \PYG{n}{Le}\PYG{p}{,} \PYG{n}{Lt}\PYG{p}{,} \PYG{n}{Rel}\PYG{p}{,} \PYG{n}{S} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{z} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.core.relational} \PYG{k+kn}{import} \PYG{n}{Relational} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e} \PYG{o}{=} \PYG{n}{GreaterThan}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e} | |
\PYG{g+go}{x \PYGZgt{}= 1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{l+s}{\PYGZsq{}}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{ \PYGZgt{}= }\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{ is the same as }\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{ \PYGZlt{}= }\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{\PYGZsq{}} \PYG{o}{\PYGZpc{}} \PYG{p}{(}\PYG{n}{e}\PYG{o}{.}\PYG{n}{gts}\PYG{p}{,} \PYG{n}{e}\PYG{o}{.}\PYG{n}{lts}\PYG{p}{,} \PYG{n}{e}\PYG{o}{.}\PYG{n}{lts}\PYG{p}{,} \PYG{n}{e}\PYG{o}{.}\PYG{n}{gts}\PYG{p}{)} | |
\PYG{g+go}{\PYGZsq{}x \PYGZgt{}= 1 is the same as 1 \PYGZlt{}= x\PYGZsq{}} | |
\end{Verbatim} | |
\paragraph{Notes} | |
There are a couple of ``gotchas'' when using Python's operators. | |
The first enters the mix when comparing against a literal number as the lhs | |
argument. Due to the order that Python decides to parse a statement, it may | |
not immediately find two objects comparable. For example, to evaluate the | |
statement (1 \textless{} x), Python will first recognize the number 1 as a native | |
number, and then that x is \emph{not} a native number. At this point, because a | |
native Python number does not know how to compare itself with a SymPy object | |
Python will try the reflective operation, (x \textgreater{} 1). Unfortunately, there is | |
no way available to SymPy to recognize this has happened, so the statement | |
(1 \textless{} x) will turn silently into (x \textgreater{} 1). | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e1} \PYG{o}{=} \PYG{n}{x} \PYG{o}{\PYGZgt{}} \PYG{l+m+mi}{1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e2} \PYG{o}{=} \PYG{n}{x} \PYG{o}{\PYGZgt{}}\PYG{o}{=} \PYG{l+m+mi}{1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e3} \PYG{o}{=} \PYG{n}{x} \PYG{o}{\PYGZlt{}} \PYG{l+m+mi}{1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e4} \PYG{o}{=} \PYG{n}{x} \PYG{o}{\PYGZlt{}}\PYG{o}{=} \PYG{l+m+mi}{1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e5} \PYG{o}{=} \PYG{l+m+mi}{1} \PYG{o}{\PYGZgt{}} \PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e6} \PYG{o}{=} \PYG{l+m+mi}{1} \PYG{o}{\PYGZgt{}}\PYG{o}{=} \PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e7} \PYG{o}{=} \PYG{l+m+mi}{1} \PYG{o}{\PYGZlt{}} \PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e8} \PYG{o}{=} \PYG{l+m+mi}{1} \PYG{o}{\PYGZlt{}}\PYG{o}{=} \PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{ }\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s}{\PYGZdq{}}\PYG{o}{*}\PYG{l+m+mi}{4} \PYG{o}{\PYGZpc{}} \PYG{p}{(}\PYG{n}{e1}\PYG{p}{,} \PYG{n}{e2}\PYG{p}{,} \PYG{n}{e3}\PYG{p}{,} \PYG{n}{e4}\PYG{p}{,} \PYG{n}{e5}\PYG{p}{,} \PYG{n}{e6}\PYG{p}{,} \PYG{n}{e7}\PYG{p}{,} \PYG{n}{e8}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{x \PYGZgt{} 1 x \PYGZgt{}= 1} | |
\PYG{g+go}{x \PYGZlt{} 1 x \PYGZlt{}= 1} | |
\PYG{g+go}{x \PYGZlt{} 1 x \PYGZlt{}= 1} | |
\PYG{g+go}{x \PYGZgt{} 1 x \PYGZgt{}= 1} | |
\end{Verbatim} | |
If the order of the statement is important (for visual output to the | |
console, perhaps), one can work around this annoyance in a couple ways: (1) | |
``sympify'' the literal before comparison, (2) use one of the wrappers, or (3) | |
use the less succinct methods described above: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e1} \PYG{o}{=} \PYG{n}{S}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{)} \PYG{o}{\PYGZgt{}} \PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e2} \PYG{o}{=} \PYG{n}{S}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{)} \PYG{o}{\PYGZgt{}}\PYG{o}{=} \PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e3} \PYG{o}{=} \PYG{n}{S}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{)} \PYG{o}{\PYGZlt{}} \PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e4} \PYG{o}{=} \PYG{n}{S}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{)} \PYG{o}{\PYGZlt{}}\PYG{o}{=} \PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e5} \PYG{o}{=} \PYG{n}{Gt}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e6} \PYG{o}{=} \PYG{n}{Ge}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e7} \PYG{o}{=} \PYG{n}{Lt}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e8} \PYG{o}{=} \PYG{n}{Le}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{ }\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s}{\PYGZdq{}}\PYG{o}{*}\PYG{l+m+mi}{4} \PYG{o}{\PYGZpc{}} \PYG{p}{(}\PYG{n}{e1}\PYG{p}{,} \PYG{n}{e2}\PYG{p}{,} \PYG{n}{e3}\PYG{p}{,} \PYG{n}{e4}\PYG{p}{,} \PYG{n}{e5}\PYG{p}{,} \PYG{n}{e6}\PYG{p}{,} \PYG{n}{e7}\PYG{p}{,} \PYG{n}{e8}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{1 \PYGZgt{} x 1 \PYGZgt{}= x} | |
\PYG{g+go}{1 \PYGZlt{} x 1 \PYGZlt{}= x} | |
\PYG{g+go}{1 \PYGZgt{} x 1 \PYGZgt{}= x} | |
\PYG{g+go}{1 \PYGZlt{} x 1 \PYGZlt{}= x} | |
\end{Verbatim} | |
The other gotcha is with chained inequalities. Occasionally, one may be | |
tempted to write statements like: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e} \PYG{o}{=} \PYG{n}{x} \PYG{o}{\PYGZlt{}} \PYG{n}{y} \PYG{o}{\PYGZlt{}} \PYG{n}{z} | |
\PYG{g+gt}{Traceback (most recent call last):} | |
\PYG{c}{...} | |
\PYG{g+gr}{TypeError}: \PYG{n}{symbolic boolean expression has no truth value.} | |
\PYG{g+gt}{Traceback (most recent call last):} | |
\PYG{c}{...} | |
\PYG{g+gr}{TypeError}: \PYG{n}{symbolic boolean expression has no truth value.} | |
\end{Verbatim} | |
Due to an implementation detail or decision of Python \phantomsection\label{modules/core:id38}{\hyperref[modules/core:r50]{\emph{{[}R50{]}}}} (\autopageref*{modules/core:r50}), there is no way | |
for SymPy to reliably create that as a chained inequality. To create a | |
chained inequality, the only method currently available is to make use of | |
And: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e} \PYG{o}{=} \PYG{n}{And}\PYG{p}{(}\PYG{n}{x} \PYG{o}{\PYGZlt{}} \PYG{n}{y}\PYG{p}{,} \PYG{n}{y} \PYG{o}{\PYGZlt{}} \PYG{n}{z}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb}{type}\PYG{p}{(} \PYG{n}{e} \PYG{p}{)} | |
\PYG{g+go}{And} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e} | |
\PYG{g+go}{And(x \PYGZlt{} y, y \PYGZlt{} z)} | |
\end{Verbatim} | |
Note that this is different than chaining an equality directly via use of | |
parenthesis (this is currently an open bug in SymPy \phantomsection\label{modules/core:id39}{\hyperref[modules/core:r51]{\emph{{[}R51{]}}}} (\autopageref*{modules/core:r51})): | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e} \PYG{o}{=} \PYG{p}{(}\PYG{n}{x} \PYG{o}{\PYGZlt{}} \PYG{n}{y}\PYG{p}{)} \PYG{o}{\PYGZlt{}} \PYG{n}{z} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb}{type}\PYG{p}{(} \PYG{n}{e} \PYG{p}{)} | |
\PYG{g+go}{\PYGZlt{}class \PYGZsq{}sympy.core.relational.StrictLessThan\PYGZsq{}\PYGZgt{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e} | |
\PYG{g+go}{(x \PYGZlt{} y) \PYGZlt{} z} | |
\end{Verbatim} | |
Any code that explicitly relies on this latter functionality will not be | |
robust as this behaviour is completely wrong and will be corrected at some | |
point. For the time being (circa Jan 2012), use And to create chained | |
inequalities. | |
\paragraph{Examples} | |
One generally does not instantiate these classes directly, but uses various | |
convenience methods: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e1} \PYG{o}{=} \PYG{n}{Ge}\PYG{p}{(} \PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{2} \PYG{p}{)} \PYG{c}{\PYGZsh{} Ge is a convenience wrapper} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{n}{e1}\PYG{p}{)} | |
\PYG{g+go}{x \PYGZgt{}= 2} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{rels} \PYG{o}{=} \PYG{n}{Ge}\PYG{p}{(} \PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{2} \PYG{p}{)}\PYG{p}{,} \PYG{n}{Gt}\PYG{p}{(} \PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{2} \PYG{p}{)}\PYG{p}{,} \PYG{n}{Le}\PYG{p}{(} \PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{2} \PYG{p}{)}\PYG{p}{,} \PYG{n}{Lt}\PYG{p}{(} \PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{2} \PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{\PYGZsq{}} \PYG{o}{\PYGZpc{}} \PYG{n}{rels}\PYG{p}{)} | |
\PYG{g+go}{x \PYGZgt{}= 2} | |
\PYG{g+go}{x \PYGZgt{} 2} | |
\PYG{g+go}{x \PYGZlt{}= 2} | |
\PYG{g+go}{x \PYGZlt{} 2} | |
\end{Verbatim} | |
Another option is to use the Python inequality operators (\textgreater{}=, \textgreater{}, \textless{}=, \textless{}) | |
directly. Their main advantage over the Ge, Gt, Le, and Lt counterparts, is | |
that one can write a more ``mathematical looking'' statement rather than | |
littering the math with oddball function calls. However there are certain | |
(minor) caveats of which to be aware (search for `gotcha', below). | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e2} \PYG{o}{=} \PYG{n}{x} \PYG{o}{\PYGZgt{}}\PYG{o}{=} \PYG{l+m+mi}{2} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{n}{e2}\PYG{p}{)} | |
\PYG{g+go}{x \PYGZgt{}= 2} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s}{e1: }\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{, e2: }\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{\PYGZdq{}} \PYG{o}{\PYGZpc{}} \PYG{p}{(}\PYG{n}{e1}\PYG{p}{,} \PYG{n}{e2}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{e1: x \PYGZgt{}= 2, e2: x \PYGZgt{}= 2} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e1} \PYG{o}{==} \PYG{n}{e2} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
However, it is also perfectly valid to instantiate a \code{*Than} class less | |
succinctly and less conveniently: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{rels} \PYG{o}{=} \PYG{n}{Rel}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{\PYGZgt{}=}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)}\PYG{p}{,} \PYG{n}{Relational}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{\PYGZgt{}=}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)}\PYG{p}{,} \PYG{n}{GreaterThan}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{\PYGZsq{}} \PYG{o}{\PYGZpc{}} \PYG{n}{rels}\PYG{p}{)} | |
\PYG{g+go}{x \PYGZgt{}= 1} | |
\PYG{g+go}{x \PYGZgt{}= 1} | |
\PYG{g+go}{x \PYGZgt{}= 1} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{rels} \PYG{o}{=} \PYG{n}{Rel}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{\PYGZgt{}}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)}\PYG{p}{,} \PYG{n}{Relational}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{\PYGZgt{}}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)}\PYG{p}{,} \PYG{n}{StrictGreaterThan}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{\PYGZsq{}} \PYG{o}{\PYGZpc{}} \PYG{n}{rels}\PYG{p}{)} | |
\PYG{g+go}{x \PYGZgt{} 1} | |
\PYG{g+go}{x \PYGZgt{} 1} | |
\PYG{g+go}{x \PYGZgt{} 1} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{rels} \PYG{o}{=} \PYG{n}{Rel}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{\PYGZlt{}=}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)}\PYG{p}{,} \PYG{n}{Relational}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{\PYGZlt{}=}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)}\PYG{p}{,} \PYG{n}{LessThan}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{\PYGZdq{}} \PYG{o}{\PYGZpc{}} \PYG{n}{rels}\PYG{p}{)} | |
\PYG{g+go}{x \PYGZlt{}= 1} | |
\PYG{g+go}{x \PYGZlt{}= 1} | |
\PYG{g+go}{x \PYGZlt{}= 1} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{rels} \PYG{o}{=} \PYG{n}{Rel}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{\PYGZlt{}}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)}\PYG{p}{,} \PYG{n}{Relational}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{\PYGZlt{}}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)}\PYG{p}{,} \PYG{n}{StrictLessThan}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{\PYGZsq{}} \PYG{o}{\PYGZpc{}} \PYG{n}{rels}\PYG{p}{)} | |
\PYG{g+go}{x \PYGZlt{} 1} | |
\PYG{g+go}{x \PYGZlt{} 1} | |
\PYG{g+go}{x \PYGZlt{} 1} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\subsubsection{Unequality} | |
\label{modules/core:unequality}\index{Unequality (class in sympy.core.relational)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.relational.Unequality}\pysigline{\strong{class }\code{sympy.core.relational.}\bfcode{Unequality}} | |
An unequal relation between two objects. | |
Represents that two objects are not equal. If they can be shown to be | |
definitively equal, this will reduce to False; if definitively unequal, | |
this will reduce to True. Otherwise, the relation is maintained as an | |
Unequality object. | |
\strong{See also:} | |
{\hyperref[modules/core:sympy.core.relational.Equality]{\emph{\code{Equality}}}} (\autopageref*{modules/core:sympy.core.relational.Equality}) | |
\paragraph{Notes} | |
This class is not the same as the != operator. The != operator tests | |
for exact structural equality between two expressions; this class | |
compares expressions mathematically. | |
This class is effectively the inverse of Equality. As such, it uses the | |
same algorithms, including any available \(_eval_Eq\) methods. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{Ne} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Ne}\PYG{p}{(}\PYG{n}{y}\PYG{p}{,} \PYG{n}{x}\PYG{o}{+}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+go}{y != x**2 + x} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\subsubsection{StrictGreaterThan} | |
\label{modules/core:strictgreaterthan}\index{StrictGreaterThan (class in sympy.core.relational)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.relational.StrictGreaterThan}\pysigline{\strong{class }\code{sympy.core.relational.}\bfcode{StrictGreaterThan}} | |
Class representations of inequalities. | |
The \code{*Than} classes represent inequal relationships, where the left-hand | |
side is generally bigger or smaller than the right-hand side. For example, | |
the GreaterThan class represents an inequal relationship where the | |
left-hand side is at least as big as the right side, if not bigger. In | |
mathematical notation: | |
lhs \textgreater{}= rhs | |
In total, there are four \code{*Than} classes, to represent the four | |
inequalities: | |
\begin{tabulary}{\linewidth}{|L|L|} | |
\hline | |
\textsf{\relax | |
Class Name | |
} & \textsf{\relax | |
Symbol | |
}\\ | |
\hline | |
GreaterThan | |
& | |
(\textgreater{}=) | |
\\ | |
\hline | |
LessThan | |
& | |
(\textless{}=) | |
\\ | |
\hline | |
StrictGreaterThan | |
& | |
(\textgreater{}) | |
\\ | |
\hline | |
StrictLessThan | |
& | |
(\textless{}) | |
\\ | |
\hline\end{tabulary} | |
All classes take two arguments, lhs and rhs. | |
\begin{tabulary}{\linewidth}{|L|L|} | |
\hline | |
\textsf{\relax | |
Signature Example | |
} & \textsf{\relax | |
Math equivalent | |
}\\ | |
\hline | |
GreaterThan(lhs, rhs) | |
& | |
lhs \textgreater{}= rhs | |
\\ | |
\hline | |
LessThan(lhs, rhs) | |
& | |
lhs \textless{}= rhs | |
\\ | |
\hline | |
StrictGreaterThan(lhs, rhs) | |
& | |
lhs \textgreater{} rhs | |
\\ | |
\hline | |
StrictLessThan(lhs, rhs) | |
& | |
lhs \textless{} rhs | |
\\ | |
\hline\end{tabulary} | |
In addition to the normal .lhs and .rhs of Relations, \code{*Than} inequality | |
objects also have the .lts and .gts properties, which represent the ``less | |
than side'' and ``greater than side'' of the operator. Use of .lts and .gts | |
in an algorithm rather than .lhs and .rhs as an assumption of inequality | |
direction will make more explicit the intent of a certain section of code, | |
and will make it similarly more robust to client code changes: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{GreaterThan}\PYG{p}{,} \PYG{n}{StrictGreaterThan} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{LessThan}\PYG{p}{,} \PYG{n}{StrictLessThan} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{And}\PYG{p}{,} \PYG{n}{Ge}\PYG{p}{,} \PYG{n}{Gt}\PYG{p}{,} \PYG{n}{Le}\PYG{p}{,} \PYG{n}{Lt}\PYG{p}{,} \PYG{n}{Rel}\PYG{p}{,} \PYG{n}{S} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{z} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.core.relational} \PYG{k+kn}{import} \PYG{n}{Relational} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e} \PYG{o}{=} \PYG{n}{GreaterThan}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e} | |
\PYG{g+go}{x \PYGZgt{}= 1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{l+s}{\PYGZsq{}}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{ \PYGZgt{}= }\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{ is the same as }\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{ \PYGZlt{}= }\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{\PYGZsq{}} \PYG{o}{\PYGZpc{}} \PYG{p}{(}\PYG{n}{e}\PYG{o}{.}\PYG{n}{gts}\PYG{p}{,} \PYG{n}{e}\PYG{o}{.}\PYG{n}{lts}\PYG{p}{,} \PYG{n}{e}\PYG{o}{.}\PYG{n}{lts}\PYG{p}{,} \PYG{n}{e}\PYG{o}{.}\PYG{n}{gts}\PYG{p}{)} | |
\PYG{g+go}{\PYGZsq{}x \PYGZgt{}= 1 is the same as 1 \PYGZlt{}= x\PYGZsq{}} | |
\end{Verbatim} | |
\paragraph{Notes} | |
There are a couple of ``gotchas'' when using Python's operators. | |
The first enters the mix when comparing against a literal number as the lhs | |
argument. Due to the order that Python decides to parse a statement, it may | |
not immediately find two objects comparable. For example, to evaluate the | |
statement (1 \textless{} x), Python will first recognize the number 1 as a native | |
number, and then that x is \emph{not} a native number. At this point, because a | |
native Python number does not know how to compare itself with a SymPy object | |
Python will try the reflective operation, (x \textgreater{} 1). Unfortunately, there is | |
no way available to SymPy to recognize this has happened, so the statement | |
(1 \textless{} x) will turn silently into (x \textgreater{} 1). | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e1} \PYG{o}{=} \PYG{n}{x} \PYG{o}{\PYGZgt{}} \PYG{l+m+mi}{1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e2} \PYG{o}{=} \PYG{n}{x} \PYG{o}{\PYGZgt{}}\PYG{o}{=} \PYG{l+m+mi}{1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e3} \PYG{o}{=} \PYG{n}{x} \PYG{o}{\PYGZlt{}} \PYG{l+m+mi}{1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e4} \PYG{o}{=} \PYG{n}{x} \PYG{o}{\PYGZlt{}}\PYG{o}{=} \PYG{l+m+mi}{1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e5} \PYG{o}{=} \PYG{l+m+mi}{1} \PYG{o}{\PYGZgt{}} \PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e6} \PYG{o}{=} \PYG{l+m+mi}{1} \PYG{o}{\PYGZgt{}}\PYG{o}{=} \PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e7} \PYG{o}{=} \PYG{l+m+mi}{1} \PYG{o}{\PYGZlt{}} \PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e8} \PYG{o}{=} \PYG{l+m+mi}{1} \PYG{o}{\PYGZlt{}}\PYG{o}{=} \PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{ }\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s}{\PYGZdq{}}\PYG{o}{*}\PYG{l+m+mi}{4} \PYG{o}{\PYGZpc{}} \PYG{p}{(}\PYG{n}{e1}\PYG{p}{,} \PYG{n}{e2}\PYG{p}{,} \PYG{n}{e3}\PYG{p}{,} \PYG{n}{e4}\PYG{p}{,} \PYG{n}{e5}\PYG{p}{,} \PYG{n}{e6}\PYG{p}{,} \PYG{n}{e7}\PYG{p}{,} \PYG{n}{e8}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{x \PYGZgt{} 1 x \PYGZgt{}= 1} | |
\PYG{g+go}{x \PYGZlt{} 1 x \PYGZlt{}= 1} | |
\PYG{g+go}{x \PYGZlt{} 1 x \PYGZlt{}= 1} | |
\PYG{g+go}{x \PYGZgt{} 1 x \PYGZgt{}= 1} | |
\end{Verbatim} | |
If the order of the statement is important (for visual output to the | |
console, perhaps), one can work around this annoyance in a couple ways: (1) | |
``sympify'' the literal before comparison, (2) use one of the wrappers, or (3) | |
use the less succinct methods described above: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e1} \PYG{o}{=} \PYG{n}{S}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{)} \PYG{o}{\PYGZgt{}} \PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e2} \PYG{o}{=} \PYG{n}{S}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{)} \PYG{o}{\PYGZgt{}}\PYG{o}{=} \PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e3} \PYG{o}{=} \PYG{n}{S}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{)} \PYG{o}{\PYGZlt{}} \PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e4} \PYG{o}{=} \PYG{n}{S}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{)} \PYG{o}{\PYGZlt{}}\PYG{o}{=} \PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e5} \PYG{o}{=} \PYG{n}{Gt}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e6} \PYG{o}{=} \PYG{n}{Ge}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e7} \PYG{o}{=} \PYG{n}{Lt}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e8} \PYG{o}{=} \PYG{n}{Le}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{ }\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s}{\PYGZdq{}}\PYG{o}{*}\PYG{l+m+mi}{4} \PYG{o}{\PYGZpc{}} \PYG{p}{(}\PYG{n}{e1}\PYG{p}{,} \PYG{n}{e2}\PYG{p}{,} \PYG{n}{e3}\PYG{p}{,} \PYG{n}{e4}\PYG{p}{,} \PYG{n}{e5}\PYG{p}{,} \PYG{n}{e6}\PYG{p}{,} \PYG{n}{e7}\PYG{p}{,} \PYG{n}{e8}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{1 \PYGZgt{} x 1 \PYGZgt{}= x} | |
\PYG{g+go}{1 \PYGZlt{} x 1 \PYGZlt{}= x} | |
\PYG{g+go}{1 \PYGZgt{} x 1 \PYGZgt{}= x} | |
\PYG{g+go}{1 \PYGZlt{} x 1 \PYGZlt{}= x} | |
\end{Verbatim} | |
The other gotcha is with chained inequalities. Occasionally, one may be | |
tempted to write statements like: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e} \PYG{o}{=} \PYG{n}{x} \PYG{o}{\PYGZlt{}} \PYG{n}{y} \PYG{o}{\PYGZlt{}} \PYG{n}{z} | |
\PYG{g+gt}{Traceback (most recent call last):} | |
\PYG{c}{...} | |
\PYG{g+gr}{TypeError}: \PYG{n}{symbolic boolean expression has no truth value.} | |
\PYG{g+gt}{Traceback (most recent call last):} | |
\PYG{c}{...} | |
\PYG{g+gr}{TypeError}: \PYG{n}{symbolic boolean expression has no truth value.} | |
\end{Verbatim} | |
Due to an implementation detail or decision of Python \phantomsection\label{modules/core:id42}{\hyperref[modules/core:r52]{\emph{{[}R52{]}}}} (\autopageref*{modules/core:r52}), there is no way | |
for SymPy to reliably create that as a chained inequality. To create a | |
chained inequality, the only method currently available is to make use of | |
And: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e} \PYG{o}{=} \PYG{n}{And}\PYG{p}{(}\PYG{n}{x} \PYG{o}{\PYGZlt{}} \PYG{n}{y}\PYG{p}{,} \PYG{n}{y} \PYG{o}{\PYGZlt{}} \PYG{n}{z}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb}{type}\PYG{p}{(} \PYG{n}{e} \PYG{p}{)} | |
\PYG{g+go}{And} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e} | |
\PYG{g+go}{And(x \PYGZlt{} y, y \PYGZlt{} z)} | |
\end{Verbatim} | |
Note that this is different than chaining an equality directly via use of | |
parenthesis (this is currently an open bug in SymPy \phantomsection\label{modules/core:id43}{\hyperref[modules/core:r53]{\emph{{[}R53{]}}}} (\autopageref*{modules/core:r53})): | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e} \PYG{o}{=} \PYG{p}{(}\PYG{n}{x} \PYG{o}{\PYGZlt{}} \PYG{n}{y}\PYG{p}{)} \PYG{o}{\PYGZlt{}} \PYG{n}{z} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb}{type}\PYG{p}{(} \PYG{n}{e} \PYG{p}{)} | |
\PYG{g+go}{\PYGZlt{}class \PYGZsq{}sympy.core.relational.StrictLessThan\PYGZsq{}\PYGZgt{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e} | |
\PYG{g+go}{(x \PYGZlt{} y) \PYGZlt{} z} | |
\end{Verbatim} | |
Any code that explicitly relies on this latter functionality will not be | |
robust as this behaviour is completely wrong and will be corrected at some | |
point. For the time being (circa Jan 2012), use And to create chained | |
inequalities. | |
\paragraph{Examples} | |
One generally does not instantiate these classes directly, but uses various | |
convenience methods: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e1} \PYG{o}{=} \PYG{n}{Ge}\PYG{p}{(} \PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{2} \PYG{p}{)} \PYG{c}{\PYGZsh{} Ge is a convenience wrapper} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{n}{e1}\PYG{p}{)} | |
\PYG{g+go}{x \PYGZgt{}= 2} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{rels} \PYG{o}{=} \PYG{n}{Ge}\PYG{p}{(} \PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{2} \PYG{p}{)}\PYG{p}{,} \PYG{n}{Gt}\PYG{p}{(} \PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{2} \PYG{p}{)}\PYG{p}{,} \PYG{n}{Le}\PYG{p}{(} \PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{2} \PYG{p}{)}\PYG{p}{,} \PYG{n}{Lt}\PYG{p}{(} \PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{2} \PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{\PYGZsq{}} \PYG{o}{\PYGZpc{}} \PYG{n}{rels}\PYG{p}{)} | |
\PYG{g+go}{x \PYGZgt{}= 2} | |
\PYG{g+go}{x \PYGZgt{} 2} | |
\PYG{g+go}{x \PYGZlt{}= 2} | |
\PYG{g+go}{x \PYGZlt{} 2} | |
\end{Verbatim} | |
Another option is to use the Python inequality operators (\textgreater{}=, \textgreater{}, \textless{}=, \textless{}) | |
directly. Their main advantage over the Ge, Gt, Le, and Lt counterparts, is | |
that one can write a more ``mathematical looking'' statement rather than | |
littering the math with oddball function calls. However there are certain | |
(minor) caveats of which to be aware (search for `gotcha', below). | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e2} \PYG{o}{=} \PYG{n}{x} \PYG{o}{\PYGZgt{}}\PYG{o}{=} \PYG{l+m+mi}{2} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{n}{e2}\PYG{p}{)} | |
\PYG{g+go}{x \PYGZgt{}= 2} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s}{e1: }\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{, e2: }\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{\PYGZdq{}} \PYG{o}{\PYGZpc{}} \PYG{p}{(}\PYG{n}{e1}\PYG{p}{,} \PYG{n}{e2}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{e1: x \PYGZgt{}= 2, e2: x \PYGZgt{}= 2} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e1} \PYG{o}{==} \PYG{n}{e2} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
However, it is also perfectly valid to instantiate a \code{*Than} class less | |
succinctly and less conveniently: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{rels} \PYG{o}{=} \PYG{n}{Rel}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{\PYGZgt{}=}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)}\PYG{p}{,} \PYG{n}{Relational}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{\PYGZgt{}=}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)}\PYG{p}{,} \PYG{n}{GreaterThan}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{\PYGZsq{}} \PYG{o}{\PYGZpc{}} \PYG{n}{rels}\PYG{p}{)} | |
\PYG{g+go}{x \PYGZgt{}= 1} | |
\PYG{g+go}{x \PYGZgt{}= 1} | |
\PYG{g+go}{x \PYGZgt{}= 1} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{rels} \PYG{o}{=} \PYG{n}{Rel}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{\PYGZgt{}}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)}\PYG{p}{,} \PYG{n}{Relational}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{\PYGZgt{}}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)}\PYG{p}{,} \PYG{n}{StrictGreaterThan}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{\PYGZsq{}} \PYG{o}{\PYGZpc{}} \PYG{n}{rels}\PYG{p}{)} | |
\PYG{g+go}{x \PYGZgt{} 1} | |
\PYG{g+go}{x \PYGZgt{} 1} | |
\PYG{g+go}{x \PYGZgt{} 1} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{rels} \PYG{o}{=} \PYG{n}{Rel}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{\PYGZlt{}=}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)}\PYG{p}{,} \PYG{n}{Relational}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{\PYGZlt{}=}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)}\PYG{p}{,} \PYG{n}{LessThan}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{\PYGZdq{}} \PYG{o}{\PYGZpc{}} \PYG{n}{rels}\PYG{p}{)} | |
\PYG{g+go}{x \PYGZlt{}= 1} | |
\PYG{g+go}{x \PYGZlt{}= 1} | |
\PYG{g+go}{x \PYGZlt{}= 1} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{rels} \PYG{o}{=} \PYG{n}{Rel}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{\PYGZlt{}}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)}\PYG{p}{,} \PYG{n}{Relational}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{\PYGZlt{}}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)}\PYG{p}{,} \PYG{n}{StrictLessThan}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{\PYGZsq{}} \PYG{o}{\PYGZpc{}} \PYG{n}{rels}\PYG{p}{)} | |
\PYG{g+go}{x \PYGZlt{} 1} | |
\PYG{g+go}{x \PYGZlt{} 1} | |
\PYG{g+go}{x \PYGZlt{} 1} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\subsubsection{StrictLessThan} | |
\label{modules/core:strictlessthan}\index{StrictLessThan (class in sympy.core.relational)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.relational.StrictLessThan}\pysigline{\strong{class }\code{sympy.core.relational.}\bfcode{StrictLessThan}} | |
Class representations of inequalities. | |
The \code{*Than} classes represent inequal relationships, where the left-hand | |
side is generally bigger or smaller than the right-hand side. For example, | |
the GreaterThan class represents an inequal relationship where the | |
left-hand side is at least as big as the right side, if not bigger. In | |
mathematical notation: | |
lhs \textgreater{}= rhs | |
In total, there are four \code{*Than} classes, to represent the four | |
inequalities: | |
\begin{tabulary}{\linewidth}{|L|L|} | |
\hline | |
\textsf{\relax | |
Class Name | |
} & \textsf{\relax | |
Symbol | |
}\\ | |
\hline | |
GreaterThan | |
& | |
(\textgreater{}=) | |
\\ | |
\hline | |
LessThan | |
& | |
(\textless{}=) | |
\\ | |
\hline | |
StrictGreaterThan | |
& | |
(\textgreater{}) | |
\\ | |
\hline | |
StrictLessThan | |
& | |
(\textless{}) | |
\\ | |
\hline\end{tabulary} | |
All classes take two arguments, lhs and rhs. | |
\begin{tabulary}{\linewidth}{|L|L|} | |
\hline | |
\textsf{\relax | |
Signature Example | |
} & \textsf{\relax | |
Math equivalent | |
}\\ | |
\hline | |
GreaterThan(lhs, rhs) | |
& | |
lhs \textgreater{}= rhs | |
\\ | |
\hline | |
LessThan(lhs, rhs) | |
& | |
lhs \textless{}= rhs | |
\\ | |
\hline | |
StrictGreaterThan(lhs, rhs) | |
& | |
lhs \textgreater{} rhs | |
\\ | |
\hline | |
StrictLessThan(lhs, rhs) | |
& | |
lhs \textless{} rhs | |
\\ | |
\hline\end{tabulary} | |
In addition to the normal .lhs and .rhs of Relations, \code{*Than} inequality | |
objects also have the .lts and .gts properties, which represent the ``less | |
than side'' and ``greater than side'' of the operator. Use of .lts and .gts | |
in an algorithm rather than .lhs and .rhs as an assumption of inequality | |
direction will make more explicit the intent of a certain section of code, | |
and will make it similarly more robust to client code changes: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{GreaterThan}\PYG{p}{,} \PYG{n}{StrictGreaterThan} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{LessThan}\PYG{p}{,} \PYG{n}{StrictLessThan} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{And}\PYG{p}{,} \PYG{n}{Ge}\PYG{p}{,} \PYG{n}{Gt}\PYG{p}{,} \PYG{n}{Le}\PYG{p}{,} \PYG{n}{Lt}\PYG{p}{,} \PYG{n}{Rel}\PYG{p}{,} \PYG{n}{S} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{z} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.core.relational} \PYG{k+kn}{import} \PYG{n}{Relational} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e} \PYG{o}{=} \PYG{n}{GreaterThan}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e} | |
\PYG{g+go}{x \PYGZgt{}= 1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{l+s}{\PYGZsq{}}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{ \PYGZgt{}= }\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{ is the same as }\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{ \PYGZlt{}= }\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{\PYGZsq{}} \PYG{o}{\PYGZpc{}} \PYG{p}{(}\PYG{n}{e}\PYG{o}{.}\PYG{n}{gts}\PYG{p}{,} \PYG{n}{e}\PYG{o}{.}\PYG{n}{lts}\PYG{p}{,} \PYG{n}{e}\PYG{o}{.}\PYG{n}{lts}\PYG{p}{,} \PYG{n}{e}\PYG{o}{.}\PYG{n}{gts}\PYG{p}{)} | |
\PYG{g+go}{\PYGZsq{}x \PYGZgt{}= 1 is the same as 1 \PYGZlt{}= x\PYGZsq{}} | |
\end{Verbatim} | |
\paragraph{Notes} | |
There are a couple of ``gotchas'' when using Python's operators. | |
The first enters the mix when comparing against a literal number as the lhs | |
argument. Due to the order that Python decides to parse a statement, it may | |
not immediately find two objects comparable. For example, to evaluate the | |
statement (1 \textless{} x), Python will first recognize the number 1 as a native | |
number, and then that x is \emph{not} a native number. At this point, because a | |
native Python number does not know how to compare itself with a SymPy object | |
Python will try the reflective operation, (x \textgreater{} 1). Unfortunately, there is | |
no way available to SymPy to recognize this has happened, so the statement | |
(1 \textless{} x) will turn silently into (x \textgreater{} 1). | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e1} \PYG{o}{=} \PYG{n}{x} \PYG{o}{\PYGZgt{}} \PYG{l+m+mi}{1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e2} \PYG{o}{=} \PYG{n}{x} \PYG{o}{\PYGZgt{}}\PYG{o}{=} \PYG{l+m+mi}{1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e3} \PYG{o}{=} \PYG{n}{x} \PYG{o}{\PYGZlt{}} \PYG{l+m+mi}{1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e4} \PYG{o}{=} \PYG{n}{x} \PYG{o}{\PYGZlt{}}\PYG{o}{=} \PYG{l+m+mi}{1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e5} \PYG{o}{=} \PYG{l+m+mi}{1} \PYG{o}{\PYGZgt{}} \PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e6} \PYG{o}{=} \PYG{l+m+mi}{1} \PYG{o}{\PYGZgt{}}\PYG{o}{=} \PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e7} \PYG{o}{=} \PYG{l+m+mi}{1} \PYG{o}{\PYGZlt{}} \PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e8} \PYG{o}{=} \PYG{l+m+mi}{1} \PYG{o}{\PYGZlt{}}\PYG{o}{=} \PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{ }\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s}{\PYGZdq{}}\PYG{o}{*}\PYG{l+m+mi}{4} \PYG{o}{\PYGZpc{}} \PYG{p}{(}\PYG{n}{e1}\PYG{p}{,} \PYG{n}{e2}\PYG{p}{,} \PYG{n}{e3}\PYG{p}{,} \PYG{n}{e4}\PYG{p}{,} \PYG{n}{e5}\PYG{p}{,} \PYG{n}{e6}\PYG{p}{,} \PYG{n}{e7}\PYG{p}{,} \PYG{n}{e8}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{x \PYGZgt{} 1 x \PYGZgt{}= 1} | |
\PYG{g+go}{x \PYGZlt{} 1 x \PYGZlt{}= 1} | |
\PYG{g+go}{x \PYGZlt{} 1 x \PYGZlt{}= 1} | |
\PYG{g+go}{x \PYGZgt{} 1 x \PYGZgt{}= 1} | |
\end{Verbatim} | |
If the order of the statement is important (for visual output to the | |
console, perhaps), one can work around this annoyance in a couple ways: (1) | |
``sympify'' the literal before comparison, (2) use one of the wrappers, or (3) | |
use the less succinct methods described above: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e1} \PYG{o}{=} \PYG{n}{S}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{)} \PYG{o}{\PYGZgt{}} \PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e2} \PYG{o}{=} \PYG{n}{S}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{)} \PYG{o}{\PYGZgt{}}\PYG{o}{=} \PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e3} \PYG{o}{=} \PYG{n}{S}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{)} \PYG{o}{\PYGZlt{}} \PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e4} \PYG{o}{=} \PYG{n}{S}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{)} \PYG{o}{\PYGZlt{}}\PYG{o}{=} \PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e5} \PYG{o}{=} \PYG{n}{Gt}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e6} \PYG{o}{=} \PYG{n}{Ge}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e7} \PYG{o}{=} \PYG{n}{Lt}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e8} \PYG{o}{=} \PYG{n}{Le}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{ }\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s}{\PYGZdq{}}\PYG{o}{*}\PYG{l+m+mi}{4} \PYG{o}{\PYGZpc{}} \PYG{p}{(}\PYG{n}{e1}\PYG{p}{,} \PYG{n}{e2}\PYG{p}{,} \PYG{n}{e3}\PYG{p}{,} \PYG{n}{e4}\PYG{p}{,} \PYG{n}{e5}\PYG{p}{,} \PYG{n}{e6}\PYG{p}{,} \PYG{n}{e7}\PYG{p}{,} \PYG{n}{e8}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{1 \PYGZgt{} x 1 \PYGZgt{}= x} | |
\PYG{g+go}{1 \PYGZlt{} x 1 \PYGZlt{}= x} | |
\PYG{g+go}{1 \PYGZgt{} x 1 \PYGZgt{}= x} | |
\PYG{g+go}{1 \PYGZlt{} x 1 \PYGZlt{}= x} | |
\end{Verbatim} | |
The other gotcha is with chained inequalities. Occasionally, one may be | |
tempted to write statements like: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e} \PYG{o}{=} \PYG{n}{x} \PYG{o}{\PYGZlt{}} \PYG{n}{y} \PYG{o}{\PYGZlt{}} \PYG{n}{z} | |
\PYG{g+gt}{Traceback (most recent call last):} | |
\PYG{c}{...} | |
\PYG{g+gr}{TypeError}: \PYG{n}{symbolic boolean expression has no truth value.} | |
\PYG{g+gt}{Traceback (most recent call last):} | |
\PYG{c}{...} | |
\PYG{g+gr}{TypeError}: \PYG{n}{symbolic boolean expression has no truth value.} | |
\end{Verbatim} | |
Due to an implementation detail or decision of Python \phantomsection\label{modules/core:id46}{\hyperref[modules/core:r54]{\emph{{[}R54{]}}}} (\autopageref*{modules/core:r54}), there is no way | |
for SymPy to reliably create that as a chained inequality. To create a | |
chained inequality, the only method currently available is to make use of | |
And: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e} \PYG{o}{=} \PYG{n}{And}\PYG{p}{(}\PYG{n}{x} \PYG{o}{\PYGZlt{}} \PYG{n}{y}\PYG{p}{,} \PYG{n}{y} \PYG{o}{\PYGZlt{}} \PYG{n}{z}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb}{type}\PYG{p}{(} \PYG{n}{e} \PYG{p}{)} | |
\PYG{g+go}{And} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e} | |
\PYG{g+go}{And(x \PYGZlt{} y, y \PYGZlt{} z)} | |
\end{Verbatim} | |
Note that this is different than chaining an equality directly via use of | |
parenthesis (this is currently an open bug in SymPy \phantomsection\label{modules/core:id47}{\hyperref[modules/core:r55]{\emph{{[}R55{]}}}} (\autopageref*{modules/core:r55})): | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e} \PYG{o}{=} \PYG{p}{(}\PYG{n}{x} \PYG{o}{\PYGZlt{}} \PYG{n}{y}\PYG{p}{)} \PYG{o}{\PYGZlt{}} \PYG{n}{z} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb}{type}\PYG{p}{(} \PYG{n}{e} \PYG{p}{)} | |
\PYG{g+go}{\PYGZlt{}class \PYGZsq{}sympy.core.relational.StrictLessThan\PYGZsq{}\PYGZgt{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e} | |
\PYG{g+go}{(x \PYGZlt{} y) \PYGZlt{} z} | |
\end{Verbatim} | |
Any code that explicitly relies on this latter functionality will not be | |
robust as this behaviour is completely wrong and will be corrected at some | |
point. For the time being (circa Jan 2012), use And to create chained | |
inequalities. | |
\paragraph{Examples} | |
One generally does not instantiate these classes directly, but uses various | |
convenience methods: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e1} \PYG{o}{=} \PYG{n}{Ge}\PYG{p}{(} \PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{2} \PYG{p}{)} \PYG{c}{\PYGZsh{} Ge is a convenience wrapper} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{n}{e1}\PYG{p}{)} | |
\PYG{g+go}{x \PYGZgt{}= 2} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{rels} \PYG{o}{=} \PYG{n}{Ge}\PYG{p}{(} \PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{2} \PYG{p}{)}\PYG{p}{,} \PYG{n}{Gt}\PYG{p}{(} \PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{2} \PYG{p}{)}\PYG{p}{,} \PYG{n}{Le}\PYG{p}{(} \PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{2} \PYG{p}{)}\PYG{p}{,} \PYG{n}{Lt}\PYG{p}{(} \PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{2} \PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{\PYGZsq{}} \PYG{o}{\PYGZpc{}} \PYG{n}{rels}\PYG{p}{)} | |
\PYG{g+go}{x \PYGZgt{}= 2} | |
\PYG{g+go}{x \PYGZgt{} 2} | |
\PYG{g+go}{x \PYGZlt{}= 2} | |
\PYG{g+go}{x \PYGZlt{} 2} | |
\end{Verbatim} | |
Another option is to use the Python inequality operators (\textgreater{}=, \textgreater{}, \textless{}=, \textless{}) | |
directly. Their main advantage over the Ge, Gt, Le, and Lt counterparts, is | |
that one can write a more ``mathematical looking'' statement rather than | |
littering the math with oddball function calls. However there are certain | |
(minor) caveats of which to be aware (search for `gotcha', below). | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e2} \PYG{o}{=} \PYG{n}{x} \PYG{o}{\PYGZgt{}}\PYG{o}{=} \PYG{l+m+mi}{2} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{n}{e2}\PYG{p}{)} | |
\PYG{g+go}{x \PYGZgt{}= 2} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s}{e1: }\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{, e2: }\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{\PYGZdq{}} \PYG{o}{\PYGZpc{}} \PYG{p}{(}\PYG{n}{e1}\PYG{p}{,} \PYG{n}{e2}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{e1: x \PYGZgt{}= 2, e2: x \PYGZgt{}= 2} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e1} \PYG{o}{==} \PYG{n}{e2} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
However, it is also perfectly valid to instantiate a \code{*Than} class less | |
succinctly and less conveniently: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{rels} \PYG{o}{=} \PYG{n}{Rel}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{\PYGZgt{}=}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)}\PYG{p}{,} \PYG{n}{Relational}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{\PYGZgt{}=}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)}\PYG{p}{,} \PYG{n}{GreaterThan}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{\PYGZsq{}} \PYG{o}{\PYGZpc{}} \PYG{n}{rels}\PYG{p}{)} | |
\PYG{g+go}{x \PYGZgt{}= 1} | |
\PYG{g+go}{x \PYGZgt{}= 1} | |
\PYG{g+go}{x \PYGZgt{}= 1} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{rels} \PYG{o}{=} \PYG{n}{Rel}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{\PYGZgt{}}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)}\PYG{p}{,} \PYG{n}{Relational}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{\PYGZgt{}}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)}\PYG{p}{,} \PYG{n}{StrictGreaterThan}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{\PYGZsq{}} \PYG{o}{\PYGZpc{}} \PYG{n}{rels}\PYG{p}{)} | |
\PYG{g+go}{x \PYGZgt{} 1} | |
\PYG{g+go}{x \PYGZgt{} 1} | |
\PYG{g+go}{x \PYGZgt{} 1} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{rels} \PYG{o}{=} \PYG{n}{Rel}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{\PYGZlt{}=}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)}\PYG{p}{,} \PYG{n}{Relational}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{\PYGZlt{}=}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)}\PYG{p}{,} \PYG{n}{LessThan}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{\PYGZdq{}} \PYG{o}{\PYGZpc{}} \PYG{n}{rels}\PYG{p}{)} | |
\PYG{g+go}{x \PYGZlt{}= 1} | |
\PYG{g+go}{x \PYGZlt{}= 1} | |
\PYG{g+go}{x \PYGZlt{}= 1} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{rels} \PYG{o}{=} \PYG{n}{Rel}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{\PYGZlt{}}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)}\PYG{p}{,} \PYG{n}{Relational}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{\PYGZlt{}}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)}\PYG{p}{,} \PYG{n}{StrictLessThan}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{\PYGZsq{}} \PYG{o}{\PYGZpc{}} \PYG{n}{rels}\PYG{p}{)} | |
\PYG{g+go}{x \PYGZlt{} 1} | |
\PYG{g+go}{x \PYGZlt{} 1} | |
\PYG{g+go}{x \PYGZlt{} 1} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\subsection{multidimensional} | |
\label{modules/core:module-sympy.core.multidimensional}\label{modules/core:multidimensional}\index{sympy.core.multidimensional (module)} | |
\subsubsection{vectorize} | |
\label{modules/core:vectorize}\index{vectorize (class in sympy.core.multidimensional)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.multidimensional.vectorize}\pysiglinewithargsret{\strong{class }\code{sympy.core.multidimensional.}\bfcode{vectorize}}{\emph{*mdargs}}{} | |
Generalizes a function taking scalars to accept multidimensional arguments. | |
For example | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{diff}\PYG{p}{,} \PYG{n}{sin}\PYG{p}{,} \PYG{n}{symbols}\PYG{p}{,} \PYG{n}{Function} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.core.multidimensional} \PYG{k+kn}{import} \PYG{n}{vectorize} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{z} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x y z}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{f}\PYG{p}{,} \PYG{n}{g}\PYG{p}{,} \PYG{n}{h} \PYG{o}{=} \PYG{n+nb}{list}\PYG{p}{(}\PYG{n+nb}{map}\PYG{p}{(}\PYG{n}{Function}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{fgh}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)}\PYG{p}{)} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nd}{@vectorize}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{)} | |
\PYG{g+gp}{... }\PYG{k}{def} \PYG{n+nf}{vsin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{:} | |
\PYG{g+gp}{... } \PYG{k}{return} \PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{vsin}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+go}{[sin(1), sin(x), sin(y)]} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nd}{@vectorize}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+gp}{... }\PYG{k}{def} \PYG{n+nf}{vdiff}\PYG{p}{(}\PYG{n}{f}\PYG{p}{,} \PYG{n}{y}\PYG{p}{)}\PYG{p}{:} | |
\PYG{g+gp}{... } \PYG{k}{return} \PYG{n}{diff}\PYG{p}{(}\PYG{n}{f}\PYG{p}{,} \PYG{n}{y}\PYG{p}{)} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{vdiff}\PYG{p}{(}\PYG{p}{[}\PYG{n}{f}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{z}\PYG{p}{)}\PYG{p}{,} \PYG{n}{g}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{z}\PYG{p}{)}\PYG{p}{,} \PYG{n}{h}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{z}\PYG{p}{)}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{z}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+go}{[[Derivative(f(x, y, z), x), Derivative(f(x, y, z), y), Derivative(f(x, y, z), z)], [Derivative(g(x, y, z), x), Derivative(g(x, y, z), y), Derivative(g(x, y, z), z)], [Derivative(h(x, y, z), x), Derivative(h(x, y, z), y), Derivative(h(x, y, z), z)]]} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\subsection{function} | |
\label{modules/core:function}\label{modules/core:module-sympy.core.function}\index{sympy.core.function (module)} | |
\subsubsection{Lambda} | |
\label{modules/core:lambda}\index{Lambda (class in sympy.core.function)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.function.Lambda}\pysigline{\strong{class }\code{sympy.core.function.}\bfcode{Lambda}} | |
Lambda(x, expr) represents a lambda function similar to Python's | |
`lambda x: expr'. A function of several variables is written as | |
Lambda((x, y, ...), expr). | |
A simple example: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{Lambda} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{f} \PYG{o}{=} \PYG{n}{Lambda}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{f}\PYG{p}{(}\PYG{l+m+mi}{4}\PYG{p}{)} | |
\PYG{g+go}{16} | |
\end{Verbatim} | |
For multivariate functions, use: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{y}\PYG{p}{,} \PYG{n}{z}\PYG{p}{,} \PYG{n}{t} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{f2} \PYG{o}{=} \PYG{n}{Lambda}\PYG{p}{(}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{z}\PYG{p}{,} \PYG{n}{t}\PYG{p}{)}\PYG{p}{,} \PYG{n}{x} \PYG{o}{+} \PYG{n}{y}\PYG{o}{*}\PYG{o}{*}\PYG{n}{z} \PYG{o}{+} \PYG{n}{t}\PYG{o}{*}\PYG{o}{*}\PYG{n}{z}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{f2}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{4}\PYG{p}{)} | |
\PYG{g+go}{73} | |
\end{Verbatim} | |
A handy shortcut for lots of arguments: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{z} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{f} \PYG{o}{=} \PYG{n}{Lambda}\PYG{p}{(}\PYG{n}{p}\PYG{p}{,} \PYG{n}{x} \PYG{o}{+} \PYG{n}{y}\PYG{o}{*}\PYG{n}{z}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{f}\PYG{p}{(}\PYG{o}{*}\PYG{n}{p}\PYG{p}{)} | |
\PYG{g+go}{x + y*z} | |
\end{Verbatim} | |
\index{expr (sympy.core.function.Lambda attribute)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.function.Lambda.expr}\pysigline{\bfcode{expr}} | |
The return value of the function | |
\end{fulllineitems} | |
\index{is\_identity (sympy.core.function.Lambda attribute)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.function.Lambda.is_identity}\pysigline{\bfcode{is\_identity}} | |
Return \code{True} if this \code{Lambda} is an identity function. | |
\end{fulllineitems} | |
\index{variables (sympy.core.function.Lambda attribute)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.function.Lambda.variables}\pysigline{\bfcode{variables}} | |
The variables used in the internal representation of the function | |
\end{fulllineitems} | |
\end{fulllineitems} | |
\subsubsection{WildFunction} | |
\label{modules/core:wildfunction}\index{WildFunction (class in sympy.core.function)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.function.WildFunction}\pysiglinewithargsret{\strong{class }\code{sympy.core.function.}\bfcode{WildFunction}}{\emph{name}, \emph{**assumptions}}{} | |
A WildFunction function matches any function (with its arguments). | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{WildFunction}\PYG{p}{,} \PYG{n}{Function}\PYG{p}{,} \PYG{n}{cos} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{F} \PYG{o}{=} \PYG{n}{WildFunction}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{F}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{f} \PYG{o}{=} \PYG{n}{Function}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{f}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{F}\PYG{o}{.}\PYG{n}{nargs} | |
\PYG{g+go}{Naturals0()} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x}\PYG{o}{.}\PYG{n}{match}\PYG{p}{(}\PYG{n}{F}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{F}\PYG{o}{.}\PYG{n}{match}\PYG{p}{(}\PYG{n}{F}\PYG{p}{)} | |
\PYG{g+go}{\PYGZob{}F\PYGZus{}: F\PYGZus{}\PYGZcb{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{f}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{match}\PYG{p}{(}\PYG{n}{F}\PYG{p}{)} | |
\PYG{g+go}{\PYGZob{}F\PYGZus{}: f(x)\PYGZcb{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{cos}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{match}\PYG{p}{(}\PYG{n}{F}\PYG{p}{)} | |
\PYG{g+go}{\PYGZob{}F\PYGZus{}: cos(x)\PYGZcb{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{f}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{match}\PYG{p}{(}\PYG{n}{F}\PYG{p}{)} | |
\PYG{g+go}{\PYGZob{}F\PYGZus{}: f(x, y)\PYGZcb{}} | |
\end{Verbatim} | |
To match functions with a given number of arguments, set \code{nargs} to the | |
desired value at instantiation: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{F} \PYG{o}{=} \PYG{n}{WildFunction}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{F}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{nargs}\PYG{o}{=}\PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{F}\PYG{o}{.}\PYG{n}{nargs} | |
\PYG{g+go}{\PYGZob{}2\PYGZcb{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{f}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{match}\PYG{p}{(}\PYG{n}{F}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{f}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{match}\PYG{p}{(}\PYG{n}{F}\PYG{p}{)} | |
\PYG{g+go}{\PYGZob{}F\PYGZus{}: f(x, y)\PYGZcb{}} | |
\end{Verbatim} | |
To match functions with a range of arguments, set \code{nargs} to a tuple | |
containing the desired number of arguments, e.g. if \code{nargs = (1, 2)} | |
then functions with 1 or 2 arguments will be matched. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{F} \PYG{o}{=} \PYG{n}{WildFunction}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{F}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{nargs}\PYG{o}{=}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{F}\PYG{o}{.}\PYG{n}{nargs} | |
\PYG{g+go}{\PYGZob{}1, 2\PYGZcb{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{f}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{match}\PYG{p}{(}\PYG{n}{F}\PYG{p}{)} | |
\PYG{g+go}{\PYGZob{}F\PYGZus{}: f(x)\PYGZcb{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{f}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{match}\PYG{p}{(}\PYG{n}{F}\PYG{p}{)} | |
\PYG{g+go}{\PYGZob{}F\PYGZus{}: f(x, y)\PYGZcb{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{f}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{.}\PYG{n}{match}\PYG{p}{(}\PYG{n}{F}\PYG{p}{)} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\subsubsection{Derivative} | |
\label{modules/core:derivative}\index{Derivative (class in sympy.core.function)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.function.Derivative}\pysigline{\strong{class }\code{sympy.core.function.}\bfcode{Derivative}} | |
Carries out differentiation of the given expression with respect to symbols. | |
expr must define .\_eval\_derivative(symbol) method that returns | |
the differentiation result. This function only needs to consider the | |
non-trivial case where expr contains symbol and it should call the diff() | |
method internally (not \_eval\_derivative); Derivative should be the only | |
one to call \_eval\_derivative. | |
Simplification of high-order derivatives: | |
Because there can be a significant amount of simplification that can be | |
done when multiple differentiations are performed, results will be | |
automatically simplified in a fairly conservative fashion unless the | |
keyword \code{simplify} is set to False. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{sqrt}\PYG{p}{,} \PYG{n}{diff} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e} \PYG{o}{=} \PYG{n}{sqrt}\PYG{p}{(}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{n}{x}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{diff}\PYG{p}{(}\PYG{n}{e}\PYG{p}{,} \PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{5}\PYG{p}{,} \PYG{n}{simplify}\PYG{o}{=}\PYG{n+nb+bp}{False}\PYG{p}{)}\PYG{o}{.}\PYG{n}{count\PYGZus{}ops}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{136} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{diff}\PYG{p}{(}\PYG{n}{e}\PYG{p}{,} \PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{5}\PYG{p}{)}\PYG{o}{.}\PYG{n}{count\PYGZus{}ops}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{30} | |
\end{Verbatim} | |
Ordering of variables: | |
If evaluate is set to True and the expression can not be evaluated, the | |
list of differentiation symbols will be sorted, that is, the expression is | |
assumed to have continuous derivatives up to the order asked. This sorting | |
assumes that derivatives wrt Symbols commute, derivatives wrt non-Symbols | |
commute, but Symbol and non-Symbol derivatives don't commute with each | |
other. | |
Derivative wrt non-Symbols: | |
This class also allows derivatives wrt non-Symbols that have \_diff\_wrt | |
set to True, such as Function and Derivative. When a derivative wrt a non- | |
Symbol is attempted, the non-Symbol is temporarily converted to a Symbol | |
while the differentiation is performed. | |
Note that this may seem strange, that Derivative allows things like | |
f(g(x)).diff(g(x)), or even f(cos(x)).diff(cos(x)). The motivation for | |
allowing this syntax is to make it easier to work with variational calculus | |
(i.e., the Euler-Lagrange method). The best way to understand this is that | |
the action of derivative with respect to a non-Symbol is defined by the | |
above description: the object is substituted for a Symbol and the | |
derivative is taken with respect to that. This action is only allowed for | |
objects for which this can be done unambiguously, for example Function and | |
Derivative objects. Note that this leads to what may appear to be | |
mathematically inconsistent results. For example: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{cos}\PYG{p}{,} \PYG{n}{sin}\PYG{p}{,} \PYG{n}{sqrt} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{cos}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{diff}\PYG{p}{(}\PYG{n}{cos}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{2} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{sqrt}\PYG{p}{(}\PYG{l+m+mi}{1} \PYG{o}{\PYGZhy{}} \PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{diff}\PYG{p}{(}\PYG{n}{cos}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{0} | |
\end{Verbatim} | |
This appears wrong because in fact 2*cos(x) and 2*sqrt(1 - sin(x)**2) are | |
identically equal. However this is the wrong way to think of this. Think | |
of it instead as if we have something like this: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{c}\PYG{p}{,} \PYG{n}{s} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{def} \PYG{n+nf}{F}\PYG{p}{(}\PYG{n}{u}\PYG{p}{)}\PYG{p}{:} | |
\PYG{g+gp}{... } \PYG{k}{return} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{u} | |
\PYG{g+gp}{...} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{def} \PYG{n+nf}{G}\PYG{p}{(}\PYG{n}{u}\PYG{p}{)}\PYG{p}{:} | |
\PYG{g+gp}{... } \PYG{k}{return} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{sqrt}\PYG{p}{(}\PYG{l+m+mi}{1} \PYG{o}{\PYGZhy{}} \PYG{n}{u}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+gp}{...} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{F}\PYG{p}{(}\PYG{n}{cos}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{2*cos(x)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{G}\PYG{p}{(}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{2*sqrt(\PYGZhy{}sin(x)**2 + 1)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{F}\PYG{p}{(}\PYG{n}{c}\PYG{p}{)}\PYG{o}{.}\PYG{n}{diff}\PYG{p}{(}\PYG{n}{c}\PYG{p}{)} | |
\PYG{g+go}{2} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{F}\PYG{p}{(}\PYG{n}{c}\PYG{p}{)}\PYG{o}{.}\PYG{n}{diff}\PYG{p}{(}\PYG{n}{c}\PYG{p}{)} | |
\PYG{g+go}{2} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{G}\PYG{p}{(}\PYG{n}{s}\PYG{p}{)}\PYG{o}{.}\PYG{n}{diff}\PYG{p}{(}\PYG{n}{c}\PYG{p}{)} | |
\PYG{g+go}{0} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{G}\PYG{p}{(}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{diff}\PYG{p}{(}\PYG{n}{cos}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{0} | |
\end{Verbatim} | |
Here, the Symbols c and s act just like the functions cos(x) and sin(x), | |
respectively. Think of 2*cos(x) as f(c).subs(c, cos(x)) (or f(c) \emph{at} | |
c = cos(x)) and 2*sqrt(1 - sin(x)**2) as g(s).subs(s, sin(x)) (or g(s) \emph{at} | |
s = sin(x)), where f(u) == 2*u and g(u) == 2*sqrt(1 - u**2). Here, we | |
define the function first and evaluate it at the function, but we can | |
actually unambiguously do this in reverse in SymPy, because | |
expr.subs(Function, Symbol) is well-defined: just structurally replace the | |
function everywhere it appears in the expression. | |
This is actually the same notational convenience used in the Euler-Lagrange | |
method when one says F(t, f(t), f'(t)).diff(f(t)). What is actually meant | |
is that the expression in question is represented by some F(t, u, v) at | |
u = f(t) and v = f'(t), and F(t, f(t), f'(t)).diff(f(t)) simply means | |
F(t, u, v).diff(u) at u = f(t). | |
We do not allow derivatives to be taken with respect to expressions where this | |
is not so well defined. For example, we do not allow expr.diff(x*y) | |
because there are multiple ways of structurally defining where x*y appears | |
in an expression, some of which may surprise the reader (for example, a | |
very strict definition would have that (x*y*z).diff(x*y) == 0). | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{z} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{o}{*}\PYG{n}{z}\PYG{p}{)}\PYG{o}{.}\PYG{n}{diff}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)} | |
\PYG{g+gt}{Traceback (most recent call last):} | |
\PYG{c}{...} | |
\PYG{g+gr}{ValueError}: \PYG{n}{Can\PYGZsq{}t differentiate wrt the variable: x*y, 1} | |
\PYG{g+gt}{Traceback (most recent call last):} | |
\PYG{c}{...} | |
\PYG{g+gr}{ValueError}: \PYG{n}{Can\PYGZsq{}t differentiate wrt the variable: x*y, 1} | |
\end{Verbatim} | |
Note that this definition also fits in nicely with the definition of the | |
chain rule. Note how the chain rule in SymPy is defined using unevaluated | |
Subs objects: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{symbols}\PYG{p}{,} \PYG{n}{Function} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{f}\PYG{p}{,} \PYG{n}{g} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{f g}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{cls}\PYG{o}{=}\PYG{n}{Function}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{f}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{g}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{diff}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{2*Derivative(g(x), x)*Subs(Derivative(f(\PYGZus{}xi\PYGZus{}1), \PYGZus{}xi\PYGZus{}1),} | |
\PYG{g+go}{ (\PYGZus{}xi\PYGZus{}1,), (2*g(x),))} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{f}\PYG{p}{(}\PYG{n}{g}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{diff}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{Derivative(g(x), x)*Subs(Derivative(f(\PYGZus{}xi\PYGZus{}1), \PYGZus{}xi\PYGZus{}1),} | |
\PYG{g+go}{ (\PYGZus{}xi\PYGZus{}1,), (g(x),))} | |
\end{Verbatim} | |
Finally, note that, to be consistent with variational calculus, and to | |
ensure that the definition of substituting a Function for a Symbol in an | |
expression is well-defined, derivatives of functions are assumed to not be | |
related to the function. In other words, we have: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{diff} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{diff}\PYG{p}{(}\PYG{n}{f}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{diff}\PYG{p}{(}\PYG{n}{f}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{0} | |
\end{Verbatim} | |
The same is actually true for derivatives of different orders: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{diff}\PYG{p}{(}\PYG{n}{f}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{,} \PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{.}\PYG{n}{diff}\PYG{p}{(}\PYG{n}{diff}\PYG{p}{(}\PYG{n}{f}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{,} \PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{0} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{diff}\PYG{p}{(}\PYG{n}{f}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{,} \PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{.}\PYG{n}{diff}\PYG{p}{(}\PYG{n}{diff}\PYG{p}{(}\PYG{n}{f}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{,} \PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{0} | |
\end{Verbatim} | |
Note, any class can allow derivatives to be taken with respect to itself. | |
See the docstring of Expr.\_diff\_wrt. | |
\paragraph{Examples} | |
Some basic examples: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{Derivative}\PYG{p}{,} \PYG{n}{Symbol}\PYG{p}{,} \PYG{n}{Function} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{f} \PYG{o}{=} \PYG{n}{Function}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{f}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{g} \PYG{o}{=} \PYG{n}{Function}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{g}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x} \PYG{o}{=} \PYG{n}{Symbol}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{y} \PYG{o}{=} \PYG{n}{Symbol}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{y}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Derivative}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{n}{x}\PYG{p}{,} \PYG{n}{evaluate}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{2*x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Derivative}\PYG{p}{(}\PYG{n}{Derivative}\PYG{p}{(}\PYG{n}{f}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,}\PYG{n}{y}\PYG{p}{)}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)}\PYG{p}{,} \PYG{n}{y}\PYG{p}{)} | |
\PYG{g+go}{Derivative(f(x, y), x, y)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Derivative}\PYG{p}{(}\PYG{n}{f}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{,} \PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{)} | |
\PYG{g+go}{Derivative(f(x), x, x, x)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Derivative}\PYG{p}{(}\PYG{n}{f}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{)}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{x}\PYG{p}{,} \PYG{n}{evaluate}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{Derivative(f(x, y), x, y)} | |
\end{Verbatim} | |
Now some derivatives wrt functions: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Derivative}\PYG{p}{(}\PYG{n}{f}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{n}{f}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{,} \PYG{n}{evaluate}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{2*f(x)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Derivative}\PYG{p}{(}\PYG{n}{f}\PYG{p}{(}\PYG{n}{g}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{)}\PYG{p}{,} \PYG{n}{x}\PYG{p}{,} \PYG{n}{evaluate}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{Derivative(g(x), x)*Subs(Derivative(f(\PYGZus{}xi\PYGZus{}1), \PYGZus{}xi\PYGZus{}1),} | |
\PYG{g+go}{ (\PYGZus{}xi\PYGZus{}1,), (g(x),))} | |
\end{Verbatim} | |
\index{doit\_numerically() (sympy.core.function.Derivative method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.function.Derivative.doit_numerically}\pysiglinewithargsret{\bfcode{doit\_numerically}}{\emph{a}, \emph{b}}{} | |
Evaluate the derivative at z numerically. | |
When we can represent derivatives at a point, this should be folded | |
into the normal evalf. For now, we need a special method. | |
\end{fulllineitems} | |
\end{fulllineitems} | |
\subsubsection{diff} | |
\label{modules/core:diff}\index{diff() (in module sympy.core.function)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.function.diff}\pysiglinewithargsret{\code{sympy.core.function.}\bfcode{diff}}{\emph{f}, \emph{*symbols}, \emph{**kwargs}}{} | |
Differentiate f with respect to symbols. | |
This is just a wrapper to unify .diff() and the Derivative class; its | |
interface is similar to that of integrate(). You can use the same | |
shortcuts for multiple variables as with Derivative. For example, | |
diff(f(x), x, x, x) and diff(f(x), x, 3) both return the third derivative | |
of f(x). | |
You can pass evaluate=False to get an unevaluated Derivative class. Note | |
that if there are 0 symbols (such as diff(f(x), x, 0), then the result will | |
be the function (the zeroth derivative), even if evaluate=False. | |
\strong{See also:} | |
{\hyperref[modules/core:sympy.core.function.Derivative]{\emph{\code{Derivative}}}} (\autopageref*{modules/core:sympy.core.function.Derivative}) | |
\begin{description} | |
\item[{\code{sympy.geometry.util.idiff}}] \leavevmode | |
computes the derivative implicitly | |
\end{description} | |
\paragraph{References} | |
\href{http://reference.wolfram.com/legacy/v5\_2/Built-inFunctions/AlgebraicComputation/Calculus/D.html}{http://reference.wolfram.com/legacy/v5\_2/Built-inFunctions/AlgebraicComputation/Calculus/D.html} | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{sin}\PYG{p}{,} \PYG{n}{cos}\PYG{p}{,} \PYG{n}{Function}\PYG{p}{,} \PYG{n}{diff} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{f} \PYG{o}{=} \PYG{n}{Function}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{f}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{diff}\PYG{p}{(}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{cos(x)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{diff}\PYG{p}{(}\PYG{n}{f}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{,} \PYG{n}{x}\PYG{p}{,} \PYG{n}{x}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{Derivative(f(x), x, x, x)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{diff}\PYG{p}{(}\PYG{n}{f}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{,} \PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{)} | |
\PYG{g+go}{Derivative(f(x), x, x, x)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{diff}\PYG{p}{(}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{n}{cos}\PYG{p}{(}\PYG{n}{y}\PYG{p}{)}\PYG{p}{,} \PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+go}{sin(x)*cos(y)} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb}{type}\PYG{p}{(}\PYG{n}{diff}\PYG{p}{(}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{cos} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb}{type}\PYG{p}{(}\PYG{n}{diff}\PYG{p}{(}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{,} \PYG{n}{x}\PYG{p}{,} \PYG{n}{evaluate}\PYG{o}{=}\PYG{n+nb+bp}{False}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{\PYGZlt{}class \PYGZsq{}sympy.core.function.Derivative\PYGZsq{}\PYGZgt{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb}{type}\PYG{p}{(}\PYG{n}{diff}\PYG{p}{(}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{,} \PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{sin} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb}{type}\PYG{p}{(}\PYG{n}{diff}\PYG{p}{(}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{,} \PYG{n}{x}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{,} \PYG{n}{evaluate}\PYG{o}{=}\PYG{n+nb+bp}{False}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{sin} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{diff}\PYG{p}{(}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{cos(x)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{diff}\PYG{p}{(}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+gt}{Traceback (most recent call last):} | |
\PYG{c}{...} | |
\PYG{g+gr}{ValueError}: \PYG{n}{specify differentiation variables to differentiate sin(x*y)} | |
\PYG{g+gt}{Traceback (most recent call last):} | |
\PYG{c}{...} | |
\PYG{g+gr}{ValueError}: \PYG{n}{specify differentiation variables to differentiate sin(x*y)} | |
\end{Verbatim} | |
Note that \code{diff(sin(x))} syntax is meant only for convenience | |
in interactive sessions and should be avoided in library code. | |
\end{fulllineitems} | |
\subsubsection{FunctionClass} | |
\label{modules/core:functionclass}\index{FunctionClass (class in sympy.core.function)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.function.FunctionClass}\pysiglinewithargsret{\strong{class }\code{sympy.core.function.}\bfcode{FunctionClass}}{\emph{*args}, \emph{**kwargs}}{} | |
Base class for function classes. FunctionClass is a subclass of type. | |
Use Function(`\textless{}function name\textgreater{}' {[} , signature {]}) to create | |
undefined function classes. | |
\index{nargs (sympy.core.function.FunctionClass attribute)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.function.FunctionClass.nargs}\pysigline{\bfcode{nargs}} | |
Return a set of the allowed number of arguments for the function. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.core.function} \PYG{k+kn}{import} \PYG{n}{Function} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{f} \PYG{o}{=} \PYG{n}{Function}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{f}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\end{Verbatim} | |
If the function can take any number of arguments, the set of whole | |
numbers is returned: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Function}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{f}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)}\PYG{o}{.}\PYG{n}{nargs} | |
\PYG{g+go}{Naturals0()} | |
\end{Verbatim} | |
If the function was initialized to accept one or more arguments, a | |
corresponding set will be returned: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Function}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{f}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{nargs}\PYG{o}{=}\PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{.}\PYG{n}{nargs} | |
\PYG{g+go}{\PYGZob{}1\PYGZcb{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Function}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{f}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{nargs}\PYG{o}{=}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{nargs} | |
\PYG{g+go}{\PYGZob{}1, 2\PYGZcb{}} | |
\end{Verbatim} | |
The undefined function, after application, also has the nargs | |
attribute; the actual number of arguments is always available by | |
checking the \code{args} attribute: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{f} \PYG{o}{=} \PYG{n}{Function}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{f}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{f}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{.}\PYG{n}{nargs} | |
\PYG{g+go}{Naturals0()} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{f}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{.}\PYG{n}{args}\PYG{p}{)} | |
\PYG{g+go}{1} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\end{fulllineitems} | |
\subsubsection{Function} | |
\label{modules/core:id50}\index{Function (class in sympy.core.function)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.function.Function}\pysigline{\strong{class }\code{sympy.core.function.}\bfcode{Function}} | |
Base class for applied mathematical functions. | |
It also serves as a constructor for undefined function classes. | |
\paragraph{Examples} | |
First example shows how to use Function as a constructor for undefined | |
function classes: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{Function}\PYG{p}{,} \PYG{n}{Symbol} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x} \PYG{o}{=} \PYG{n}{Symbol}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{f} \PYG{o}{=} \PYG{n}{Function}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{f}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{g} \PYG{o}{=} \PYG{n}{Function}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{g}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{f} | |
\PYG{g+go}{f} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{f}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{f(x)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{g} | |
\PYG{g+go}{g(x)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{f}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{diff}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{Derivative(f(x), x)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{g}\PYG{o}{.}\PYG{n}{diff}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{Derivative(g(x), x)} | |
\end{Verbatim} | |
In the following example Function is used as a base class for | |
\code{my\_func} that represents a mathematical function \emph{my\_func}. Suppose | |
that it is well known, that \emph{my\_func(0)} is \emph{1} and \emph{my\_func} at infinity | |
goes to \emph{0}, so we want those two simplifications to occur automatically. | |
Suppose also that \emph{my\_func(x)} is real exactly when \emph{x} is real. Here is | |
an implementation that honours those requirements: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{Function}\PYG{p}{,} \PYG{n}{S}\PYG{p}{,} \PYG{n}{oo}\PYG{p}{,} \PYG{n}{I}\PYG{p}{,} \PYG{n}{sin} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{class} \PYG{n+nc}{my\PYGZus{}func}\PYG{p}{(}\PYG{n}{Function}\PYG{p}{)}\PYG{p}{:} | |
\PYG{g+gp}{...} | |
\PYG{g+gp}{... } \PYG{n+nd}{@classmethod} | |
\PYG{g+gp}{... } \PYG{k}{def} \PYG{n+nf}{eval}\PYG{p}{(}\PYG{n}{cls}\PYG{p}{,} \PYG{n}{x}\PYG{p}{)}\PYG{p}{:} | |
\PYG{g+gp}{... } \PYG{k}{if} \PYG{n}{x}\PYG{o}{.}\PYG{n}{is\PYGZus{}Number}\PYG{p}{:} | |
\PYG{g+gp}{... } \PYG{k}{if} \PYG{n}{x} \PYG{o+ow}{is} \PYG{n}{S}\PYG{o}{.}\PYG{n}{Zero}\PYG{p}{:} | |
\PYG{g+gp}{... } \PYG{k}{return} \PYG{n}{S}\PYG{o}{.}\PYG{n}{One} | |
\PYG{g+gp}{... } \PYG{k}{elif} \PYG{n}{x} \PYG{o+ow}{is} \PYG{n}{S}\PYG{o}{.}\PYG{n}{Infinity}\PYG{p}{:} | |
\PYG{g+gp}{... } \PYG{k}{return} \PYG{n}{S}\PYG{o}{.}\PYG{n}{Zero} | |
\PYG{g+gp}{...} | |
\PYG{g+gp}{... } \PYG{k}{def} \PYG{n+nf}{\PYGZus{}eval\PYGZus{}is\PYGZus{}real}\PYG{p}{(}\PYG{n+nb+bp}{self}\PYG{p}{)}\PYG{p}{:} | |
\PYG{g+gp}{... } \PYG{k}{return} \PYG{n+nb+bp}{self}\PYG{o}{.}\PYG{n}{args}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{]}\PYG{o}{.}\PYG{n}{is\PYGZus{}real} | |
\PYG{g+gp}{...} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x} \PYG{o}{=} \PYG{n}{S}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{my\PYGZus{}func}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{)} \PYG{o}{+} \PYG{n}{sin}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{)} | |
\PYG{g+go}{1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{my\PYGZus{}func}\PYG{p}{(}\PYG{n}{oo}\PYG{p}{)} | |
\PYG{g+go}{0} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{my\PYGZus{}func}\PYG{p}{(}\PYG{l+m+mf}{3.54}\PYG{p}{)}\PYG{o}{.}\PYG{n}{n}\PYG{p}{(}\PYG{p}{)} \PYG{c}{\PYGZsh{} Not yet implemented for my\PYGZus{}func.} | |
\PYG{g+go}{my\PYGZus{}func(3.54)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{my\PYGZus{}func}\PYG{p}{(}\PYG{n}{I}\PYG{p}{)}\PYG{o}{.}\PYG{n}{is\PYGZus{}real} | |
\PYG{g+go}{False} | |
\end{Verbatim} | |
In order for \code{my\_func} to become useful, several other methods would | |
need to be implemented. See source code of some of the already | |
implemented functions for more complete examples. | |
Also, if the function can take more than one argument, then \code{nargs} | |
must be defined, e.g. if \code{my\_func} can take one or two arguments | |
then, | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{class} \PYG{n+nc}{my\PYGZus{}func}\PYG{p}{(}\PYG{n}{Function}\PYG{p}{)}\PYG{p}{:} | |
\PYG{g+gp}{... } \PYG{n}{nargs} \PYG{o}{=} \PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+gp}{...} | |
\PYG{g+go}{\PYGZgt{}\PYGZgt{}\PYGZgt{}} | |
\end{Verbatim} | |
\index{as\_base\_exp() (sympy.core.function.Function method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.function.Function.as_base_exp}\pysiglinewithargsret{\bfcode{as\_base\_exp}}{}{} | |
Returns the method as the 2-tuple (base, exponent). | |
\end{fulllineitems} | |
\index{fdiff() (sympy.core.function.Function method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.function.Function.fdiff}\pysiglinewithargsret{\bfcode{fdiff}}{\emph{argindex=1}}{} | |
Returns the first derivative of the function. | |
\end{fulllineitems} | |
\index{is\_commutative (sympy.core.function.Function attribute)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.function.Function.is_commutative}\pysigline{\bfcode{is\_commutative}} | |
Returns whether the functon is commutative. | |
\end{fulllineitems} | |
\end{fulllineitems} | |
\begin{notice}{note}{Note:} | |
Not all functions are the same | |
SymPy defines many functions (like \code{cos} and \code{factorial}). It also | |
allows the user to create generic functions which act as argument | |
holders. Such functions are created just like symbols: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{Function}\PYG{p}{,} \PYG{n}{cos} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{f} \PYG{o}{=} \PYG{n}{Function}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{f}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{f}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{)} \PYG{o}{+} \PYG{n}{f}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{f(2) + f(x)} | |
\end{Verbatim} | |
If you want to see which functions appear in an expression you can use | |
the atoms method: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e} \PYG{o}{=} \PYG{p}{(}\PYG{n}{f}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} \PYG{o}{+} \PYG{n}{cos}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e}\PYG{o}{.}\PYG{n}{atoms}\PYG{p}{(}\PYG{n}{Function}\PYG{p}{)} | |
\PYG{g+go}{set([f(x), cos(x)])} | |
\end{Verbatim} | |
If you just want the function you defined, not SymPy functions, the | |
thing to search for is AppliedUndef: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.core.function} \PYG{k+kn}{import} \PYG{n}{AppliedUndef} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e}\PYG{o}{.}\PYG{n}{atoms}\PYG{p}{(}\PYG{n}{AppliedUndef}\PYG{p}{)} | |
\PYG{g+go}{set([f(x)])} | |
\end{Verbatim} | |
\end{notice} | |
\subsubsection{Subs} | |
\label{modules/core:subs}\index{Subs (class in sympy.core.function)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.function.Subs}\pysigline{\strong{class }\code{sympy.core.function.}\bfcode{Subs}} | |
Represents unevaluated substitutions of an expression. | |
\code{Subs(expr, x, x0)} receives 3 arguments: an expression, a variable or | |
list of distinct variables and a point or list of evaluation points | |
corresponding to those variables. | |
\code{Subs} objects are generally useful to represent unevaluated derivatives | |
calculated at a point. | |
The variables may be expressions, but they are subjected to the limitations | |
of subs(), so it is usually a good practice to use only symbols for | |
variables, since in that case there can be no ambiguity. | |
There's no automatic expansion - use the method .doit() to effect all | |
possible substitutions of the object and also of objects inside the | |
expression. | |
When evaluating derivatives at a point that is not a symbol, a Subs object | |
is returned. One is also able to calculate derivatives of Subs objects - in | |
this case the expression is always expanded (for the unevaluated form, use | |
Derivative()). | |
A simple example: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{Subs}\PYG{p}{,} \PYG{n}{Function}\PYG{p}{,} \PYG{n}{sin} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{z} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{f} \PYG{o}{=} \PYG{n}{Function}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{f}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e} \PYG{o}{=} \PYG{n}{Subs}\PYG{p}{(}\PYG{n}{f}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{diff}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{,} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e}\PYG{o}{.}\PYG{n}{subs}\PYG{p}{(}\PYG{n}{y}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{)} | |
\PYG{g+go}{Subs(Derivative(f(x), x), (x,), (0,))} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{e}\PYG{o}{.}\PYG{n}{subs}\PYG{p}{(}\PYG{n}{f}\PYG{p}{,} \PYG{n}{sin}\PYG{p}{)}\PYG{o}{.}\PYG{n}{doit}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{cos(y)} | |
\end{Verbatim} | |
An example with several variables: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Subs}\PYG{p}{(}\PYG{n}{f}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{y}\PYG{p}{)} \PYG{o}{+} \PYG{n}{z}\PYG{p}{,} \PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{)}\PYG{p}{,} \PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{Subs(z + f(x)*sin(y), (x, y), (0, 1))} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{\PYGZus{}}\PYG{o}{.}\PYG{n}{doit}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{z + f(0)*sin(1)} | |
\end{Verbatim} | |
\index{expr (sympy.core.function.Subs attribute)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.function.Subs.expr}\pysigline{\bfcode{expr}} | |
The expression on which the substitution operates | |
\end{fulllineitems} | |
\index{point (sympy.core.function.Subs attribute)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.function.Subs.point}\pysigline{\bfcode{point}} | |
The values for which the variables are to be substituted | |
\end{fulllineitems} | |
\index{variables (sympy.core.function.Subs attribute)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.function.Subs.variables}\pysigline{\bfcode{variables}} | |
The variables to be evaluated | |
\end{fulllineitems} | |
\end{fulllineitems} | |
\subsubsection{expand} | |
\label{modules/core:expand}\index{expand() (in module sympy.core.function)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.function.expand}\pysiglinewithargsret{\code{sympy.core.function.}\bfcode{expand}}{\emph{e}, \emph{deep=True}, \emph{modulus=None}, \emph{power\_base=True}, \emph{power\_exp=True}, \emph{mul=True}, \emph{log=True}, \emph{multinomial=True}, \emph{basic=True}, \emph{**hints}}{} | |
Expand an expression using methods given as hints. | |
Hints evaluated unless explicitly set to False are: \code{basic}, \code{log}, | |
\code{multinomial}, \code{mul}, \code{power\_base}, and \code{power\_exp} The following | |
hints are supported but not applied unless set to True: \code{complex}, | |
\code{func}, and \code{trig}. In addition, the following meta-hints are | |
supported by some or all of the other hints: \code{frac}, \code{numer}, | |
\code{denom}, \code{modulus}, and \code{force}. \code{deep} is supported by all | |
hints. Additionally, subclasses of Expr may define their own hints or | |
meta-hints. | |
The \code{basic} hint is used for any special rewriting of an object that | |
should be done automatically (along with the other hints like \code{mul}) | |
when expand is called. This is a catch-all hint to handle any sort of | |
expansion that may not be described by the existing hint names. To use | |
this hint an object should override the \code{\_eval\_expand\_basic} method. | |
Objects may also define their own expand methods, which are not run by | |
default. See the API section below. | |
If \code{deep} is set to \code{True} (the default), things like arguments of | |
functions are recursively expanded. Use \code{deep=False} to only expand on | |
the top level. | |
If the \code{force} hint is used, assumptions about variables will be ignored | |
in making the expansion. | |
\strong{See also:} | |
{\hyperref[modules/core:sympy.core.function.expand_log]{\emph{\code{expand\_log}}}} (\autopageref*{modules/core:sympy.core.function.expand_log}), {\hyperref[modules/core:sympy.core.function.expand_mul]{\emph{\code{expand\_mul}}}} (\autopageref*{modules/core:sympy.core.function.expand_mul}), {\hyperref[modules/core:sympy.core.function.expand_multinomial]{\emph{\code{expand\_multinomial}}}} (\autopageref*{modules/core:sympy.core.function.expand_multinomial}), {\hyperref[modules/core:sympy.core.function.expand_complex]{\emph{\code{expand\_complex}}}} (\autopageref*{modules/core:sympy.core.function.expand_complex}), {\hyperref[modules/core:sympy.core.function.expand_trig]{\emph{\code{expand\_trig}}}} (\autopageref*{modules/core:sympy.core.function.expand_trig}), {\hyperref[modules/core:sympy.core.function.expand_power_base]{\emph{\code{expand\_power\_base}}}} (\autopageref*{modules/core:sympy.core.function.expand_power_base}), {\hyperref[modules/core:sympy.core.function.expand_power_exp]{\emph{\code{expand\_power\_exp}}}} (\autopageref*{modules/core:sympy.core.function.expand_power_exp}), {\hyperref[modules/core:sympy.core.function.expand_func]{\emph{\code{expand\_func}}}} (\autopageref*{modules/core:sympy.core.function.expand_func}), \code{hyperexpand} | |
\paragraph{Notes} | |
\begin{itemize} | |
\item {} | |
You can shut off unwanted methods: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{exp}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{n}{y}\PYG{p}{)}\PYG{o}{*}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{n}{y}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{expand}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{x*exp(x)*exp(y) + y*exp(x)*exp(y)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{exp}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{n}{y}\PYG{p}{)}\PYG{o}{*}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{n}{y}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{expand}\PYG{p}{(}\PYG{n}{power\PYGZus{}exp}\PYG{o}{=}\PYG{n+nb+bp}{False}\PYG{p}{)} | |
\PYG{g+go}{x*exp(x + y) + y*exp(x + y)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{exp}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{n}{y}\PYG{p}{)}\PYG{o}{*}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{n}{y}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{expand}\PYG{p}{(}\PYG{n}{mul}\PYG{o}{=}\PYG{n+nb+bp}{False}\PYG{p}{)} | |
\PYG{g+go}{(x + y)*exp(x)*exp(y)} | |
\end{Verbatim} | |
\item {} | |
Use deep=False to only expand on the top level: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{exp}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{n}{exp}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{n}{y}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{expand}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{exp(x)*exp(exp(x)*exp(y))} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{exp}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{n}{exp}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{n}{y}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{expand}\PYG{p}{(}\PYG{n}{deep}\PYG{o}{=}\PYG{n+nb+bp}{False}\PYG{p}{)} | |
\PYG{g+go}{exp(x)*exp(exp(x + y))} | |
\end{Verbatim} | |
\item {} | |
Hints are applied in an arbitrary, but consistent order (in the current | |
implementation, they are applied in alphabetical order, except | |
multinomial comes before mul, but this may change). Because of this, | |
some hints may prevent expansion by other hints if they are applied | |
first. For example, \code{mul} may distribute multiplications and prevent | |
\code{log} and \code{power\_base} from expanding them. Also, if \code{mul} is | |
applied before \code{multinomial{}`, the expression might not be fully | |
distributed. The solution is to use the various {}`{}`expand\_hint} helper | |
functions or to use \code{hint=False} to this function to finely control | |
which hints are applied. Here are some examples: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{expand}\PYG{p}{,} \PYG{n}{expand\PYGZus{}mul}\PYG{p}{,} \PYG{n}{expand\PYGZus{}power\PYGZus{}base} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{z} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x,y,z}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{positive}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expand}\PYG{p}{(}\PYG{n}{log}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{p}{(}\PYG{n}{y} \PYG{o}{+} \PYG{n}{z}\PYG{p}{)}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{log(x) + log(y + z)} | |
\end{Verbatim} | |
Here, we see that \code{log} was applied before \code{mul}. To get the mul | |
expanded form, either of the following will work: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expand\PYGZus{}mul}\PYG{p}{(}\PYG{n}{log}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{p}{(}\PYG{n}{y} \PYG{o}{+} \PYG{n}{z}\PYG{p}{)}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{log(x*y + x*z)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expand}\PYG{p}{(}\PYG{n}{log}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{p}{(}\PYG{n}{y} \PYG{o}{+} \PYG{n}{z}\PYG{p}{)}\PYG{p}{)}\PYG{p}{,} \PYG{n}{log}\PYG{o}{=}\PYG{n+nb+bp}{False}\PYG{p}{)} | |
\PYG{g+go}{log(x*y + x*z)} | |
\end{Verbatim} | |
A similar thing can happen with the \code{power\_base} hint: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expand}\PYG{p}{(}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{p}{(}\PYG{n}{y} \PYG{o}{+} \PYG{n}{z}\PYG{p}{)}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{(x*y + x*z)**x} | |
\end{Verbatim} | |
To get the \code{power\_base} expanded form, either of the following will | |
work: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expand}\PYG{p}{(}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{p}{(}\PYG{n}{y} \PYG{o}{+} \PYG{n}{z}\PYG{p}{)}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{n}{x}\PYG{p}{,} \PYG{n}{mul}\PYG{o}{=}\PYG{n+nb+bp}{False}\PYG{p}{)} | |
\PYG{g+go}{x**x*(y + z)**x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expand\PYGZus{}power\PYGZus{}base}\PYG{p}{(}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{p}{(}\PYG{n}{y} \PYG{o}{+} \PYG{n}{z}\PYG{p}{)}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{x**x*(y + z)**x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expand}\PYG{p}{(}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{n}{y}\PYG{p}{)}\PYG{o}{*}\PYG{n}{y}\PYG{o}{/}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{y + y**2/x} | |
\end{Verbatim} | |
The parts of a rational expression can be targeted: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expand}\PYG{p}{(}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{n}{y}\PYG{p}{)}\PYG{o}{*}\PYG{n}{y}\PYG{o}{/}\PYG{n}{x}\PYG{o}{/}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{p}{,} \PYG{n}{frac}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{(x*y + y**2)/(x**2 + x)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expand}\PYG{p}{(}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{n}{y}\PYG{p}{)}\PYG{o}{*}\PYG{n}{y}\PYG{o}{/}\PYG{n}{x}\PYG{o}{/}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{p}{,} \PYG{n}{numer}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{(x*y + y**2)/(x*(x + 1))} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expand}\PYG{p}{(}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{n}{y}\PYG{p}{)}\PYG{o}{*}\PYG{n}{y}\PYG{o}{/}\PYG{n}{x}\PYG{o}{/}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{p}{,} \PYG{n}{denom}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{y*(x + y)/(x**2 + x)} | |
\end{Verbatim} | |
\item {} | |
The \code{modulus} meta-hint can be used to reduce the coefficients of an | |
expression post-expansion: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expand}\PYG{p}{(}\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+go}{9*x**2 + 6*x + 1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expand}\PYG{p}{(}\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{n}{modulus}\PYG{o}{=}\PYG{l+m+mi}{5}\PYG{p}{)} | |
\PYG{g+go}{4*x**2 + x + 1} | |
\end{Verbatim} | |
\item {} | |
Either \code{expand()} the function or \code{.expand()} the method can be | |
used. Both are equivalent: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expand}\PYG{p}{(}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+go}{x**2 + 2*x + 1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{.}\PYG{n}{expand}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{x**2 + 2*x + 1} | |
\end{Verbatim} | |
\end{itemize} | |
\paragraph{Hints} | |
These hints are run by default | |
\paragraph{Mul} | |
Distributes multiplication over addition: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{cos}\PYG{p}{,} \PYG{n}{exp}\PYG{p}{,} \PYG{n}{sin} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{z} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{y}\PYG{o}{*}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{n}{z}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{expand}\PYG{p}{(}\PYG{n}{mul}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{x*y + y*z} | |
\end{Verbatim} | |
\paragraph{Multinomial} | |
Expand (x + y + ...)**n where n is a positive integer. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{n}{y} \PYG{o}{+} \PYG{n}{z}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{.}\PYG{n}{expand}\PYG{p}{(}\PYG{n}{multinomial}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{x**2 + 2*x*y + 2*x*z + y**2 + 2*y*z + z**2} | |
\end{Verbatim} | |
\paragraph{Power\_exp} | |
Expand addition in exponents into multiplied bases. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{exp}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{expand}\PYG{p}{(}\PYG{n}{power\PYGZus{}exp}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{exp(x)*exp(y)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{o}{*}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{n}{y}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{expand}\PYG{p}{(}\PYG{n}{power\PYGZus{}exp}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{2**x*2**y} | |
\end{Verbatim} | |
\paragraph{Power\_base} | |
Split powers of multiplied bases. | |
This only happens by default if assumptions allow, or if the | |
\code{force} meta-hint is used: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{n}{z}\PYG{p}{)}\PYG{o}{.}\PYG{n}{expand}\PYG{p}{(}\PYG{n}{power\PYGZus{}base}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{(x*y)**z} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{n}{z}\PYG{p}{)}\PYG{o}{.}\PYG{n}{expand}\PYG{p}{(}\PYG{n}{power\PYGZus{}base}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{,} \PYG{n}{force}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{x**z*y**z} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{n}{z}\PYG{p}{)}\PYG{o}{.}\PYG{n}{expand}\PYG{p}{(}\PYG{n}{power\PYGZus{}base}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{2**z*y**z} | |
\end{Verbatim} | |
Note that in some cases where this expansion always holds, SymPy performs | |
it automatically: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} | |
\PYG{g+go}{x**2*y**2} | |
\end{Verbatim} | |
\paragraph{Log} | |
Pull out power of an argument as a coefficient and split logs products | |
into sums of logs. | |
Note that these only work if the arguments of the log function have the | |
proper assumptions--the arguments must be positive and the exponents must | |
be real--or else the \code{force} hint must be True: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{log}\PYG{p}{,} \PYG{n}{symbols} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{log}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{expand}\PYG{p}{(}\PYG{n}{log}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{log(x**2*y)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{log}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{expand}\PYG{p}{(}\PYG{n}{log}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{,} \PYG{n}{force}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{2*log(x) + log(y)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x}\PYG{p}{,} \PYG{n}{y} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x,y}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{positive}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{log}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{expand}\PYG{p}{(}\PYG{n}{log}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{2*log(x) + log(y)} | |
\end{Verbatim} | |
\paragraph{Basic} | |
This hint is intended primarily as a way for custom subclasses to enable | |
expansion by default. | |
These hints are not run by default: | |
\paragraph{Complex} | |
Split an expression into real and imaginary parts. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x}\PYG{p}{,} \PYG{n}{y} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x,y}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{expand}\PYG{p}{(}\PYG{n+nb}{complex}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{re(x) + re(y) + I*im(x) + I*im(y)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{cos}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{expand}\PYG{p}{(}\PYG{n+nb}{complex}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{\PYGZhy{}I*sin(re(x))*sinh(im(x)) + cos(re(x))*cosh(im(x))} | |
\end{Verbatim} | |
Note that this is just a wrapper around \code{as\_real\_imag()}. Most objects | |
that wish to redefine \code{\_eval\_expand\_complex()} should consider | |
redefining \code{as\_real\_imag()} instead. | |
\paragraph{Func} | |
Expand other functions. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{gamma} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{gamma}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{.}\PYG{n}{expand}\PYG{p}{(}\PYG{n}{func}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{x*gamma(x)} | |
\end{Verbatim} | |
\paragraph{Trig} | |
Do trigonometric expansions. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{cos}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{expand}\PYG{p}{(}\PYG{n}{trig}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{\PYGZhy{}sin(x)*sin(y) + cos(x)*cos(y)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{sin}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{expand}\PYG{p}{(}\PYG{n}{trig}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{2*sin(x)*cos(x)} | |
\end{Verbatim} | |
Note that the forms of \code{sin(n*x)} and \code{cos(n*x)} in terms of \code{sin(x)} | |
and \code{cos(x)} are not unique, due to the identity \(\sin^2(x) + \cos^2(x) | |
= 1\). The current implementation uses the form obtained from Chebyshev | |
polynomials, but this may change. See \href{http://mathworld.wolfram.com/Multiple-AngleFormulas.html}{this MathWorld article} for more | |
information. | |
\paragraph{Api} | |
Objects can define their own expand hints by defining | |
\code{\_eval\_expand\_hint()}. The function should take the form: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{k}{def} \PYG{n+nf}{\PYGZus{}eval\PYGZus{}expand\PYGZus{}hint}\PYG{p}{(}\PYG{n+nb+bp}{self}\PYG{p}{,} \PYG{o}{*}\PYG{o}{*}\PYG{n}{hints}\PYG{p}{)}\PYG{p}{:} | |
\PYG{c}{\PYGZsh{} Only apply the method to the top\PYGZhy{}level expression} | |
\PYG{o}{.}\PYG{o}{.}\PYG{o}{.} | |
\end{Verbatim} | |
See also the example below. Objects should define \code{\_eval\_expand\_hint()} | |
methods only if \code{hint} applies to that specific object. The generic | |
\code{\_eval\_expand\_hint()} method defined in Expr will handle the no-op case. | |
Each hint should be responsible for expanding that hint only. | |
Furthermore, the expansion should be applied to the top-level expression | |
only. \code{expand()} takes care of the recursion that happens when | |
\code{deep=True}. | |
You should only call \code{\_eval\_expand\_hint()} methods directly if you are | |
100\% sure that the object has the method, as otherwise you are liable to | |
get unexpected \code{AttributeError{}`{}`s. Note, again, that you do not need to | |
recursively apply the hint to args of your object: this is handled | |
automatically by {}`{}`expand()}. \code{\_eval\_expand\_hint()} should | |
generally not be used at all outside of an \code{\_eval\_expand\_hint()} method. | |
If you want to apply a specific expansion from within another method, use | |
the public \code{expand()} function, method, or \code{expand\_hint()} functions. | |
In order for expand to work, objects must be rebuildable by their args, | |
i.e., \code{obj.func(*obj.args) == obj} must hold. | |
Expand methods are passed \code{**hints} so that expand hints may use | |
`metahints'--hints that control how different expand methods are applied. | |
For example, the \code{force=True} hint described above that causes | |
\code{expand(log=True)} to ignore assumptions is such a metahint. The | |
\code{deep} meta-hint is handled exclusively by \code{expand()} and is not | |
passed to \code{\_eval\_expand\_hint()} methods. | |
Note that expansion hints should generally be methods that perform some | |
kind of `expansion'. For hints that simply rewrite an expression, use the | |
.rewrite() API. | |
\paragraph{Example} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{Expr}\PYG{p}{,} \PYG{n}{sympify} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{class} \PYG{n+nc}{MyClass}\PYG{p}{(}\PYG{n}{Expr}\PYG{p}{)}\PYG{p}{:} | |
\PYG{g+gp}{... } \PYG{k}{def} \PYG{n+nf}{\PYGZus{}\PYGZus{}new\PYGZus{}\PYGZus{}}\PYG{p}{(}\PYG{n}{cls}\PYG{p}{,} \PYG{o}{*}\PYG{n}{args}\PYG{p}{)}\PYG{p}{:} | |
\PYG{g+gp}{... } \PYG{n}{args} \PYG{o}{=} \PYG{n}{sympify}\PYG{p}{(}\PYG{n}{args}\PYG{p}{)} | |
\PYG{g+gp}{... } \PYG{k}{return} \PYG{n}{Expr}\PYG{o}{.}\PYG{n}{\PYGZus{}\PYGZus{}new\PYGZus{}\PYGZus{}}\PYG{p}{(}\PYG{n}{cls}\PYG{p}{,} \PYG{o}{*}\PYG{n}{args}\PYG{p}{)} | |
\PYG{g+gp}{...} | |
\PYG{g+gp}{... } \PYG{k}{def} \PYG{n+nf}{\PYGZus{}eval\PYGZus{}expand\PYGZus{}double}\PYG{p}{(}\PYG{n+nb+bp}{self}\PYG{p}{,} \PYG{o}{*}\PYG{o}{*}\PYG{n}{hints}\PYG{p}{)}\PYG{p}{:} | |
\PYG{g+gp}{... } \PYG{l+s+sd}{\PYGZsq{}\PYGZsq{}\PYGZsq{}} | |
\PYG{g+gp}{... }\PYG{l+s+sd}{ Doubles the args of MyClass.} | |
\PYG{g+gp}{...} | |
\PYG{g+gp}{... }\PYG{l+s+sd}{ If there more than four args, doubling is not performed,} | |
\PYG{g+gp}{... }\PYG{l+s+sd}{ unless force=True is also used (False by default).} | |
\PYG{g+gp}{... }\PYG{l+s+sd}{ \PYGZsq{}\PYGZsq{}\PYGZsq{}} | |
\PYG{g+gp}{... } \PYG{n}{force} \PYG{o}{=} \PYG{n}{hints}\PYG{o}{.}\PYG{n}{pop}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{force}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n+nb+bp}{False}\PYG{p}{)} | |
\PYG{g+gp}{... } \PYG{k}{if} \PYG{o+ow}{not} \PYG{n}{force} \PYG{o+ow}{and} \PYG{n+nb}{len}\PYG{p}{(}\PYG{n+nb+bp}{self}\PYG{o}{.}\PYG{n}{args}\PYG{p}{)} \PYG{o}{\PYGZgt{}} \PYG{l+m+mi}{4}\PYG{p}{:} | |
\PYG{g+gp}{... } \PYG{k}{return} \PYG{n+nb+bp}{self} | |
\PYG{g+gp}{... } \PYG{k}{return} \PYG{n+nb+bp}{self}\PYG{o}{.}\PYG{n}{func}\PYG{p}{(}\PYG{o}{*}\PYG{p}{(}\PYG{n+nb+bp}{self}\PYG{o}{.}\PYG{n}{args} \PYG{o}{+} \PYG{n+nb+bp}{self}\PYG{o}{.}\PYG{n}{args}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+gp}{...} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a} \PYG{o}{=} \PYG{n}{MyClass}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{n}{MyClass}\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{4}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a} | |
\PYG{g+go}{MyClass(1, 2, MyClass(3, 4))} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a}\PYG{o}{.}\PYG{n}{expand}\PYG{p}{(}\PYG{n}{double}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{MyClass(1, 2, MyClass(3, 4, 3, 4), 1, 2, MyClass(3, 4, 3, 4))} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a}\PYG{o}{.}\PYG{n}{expand}\PYG{p}{(}\PYG{n}{double}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{,} \PYG{n}{deep}\PYG{o}{=}\PYG{n+nb+bp}{False}\PYG{p}{)} | |
\PYG{g+go}{MyClass(1, 2, MyClass(3, 4), 1, 2, MyClass(3, 4))} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{b} \PYG{o}{=} \PYG{n}{MyClass}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{4}\PYG{p}{,} \PYG{l+m+mi}{5}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{b}\PYG{o}{.}\PYG{n}{expand}\PYG{p}{(}\PYG{n}{double}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{MyClass(1, 2, 3, 4, 5)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{b}\PYG{o}{.}\PYG{n}{expand}\PYG{p}{(}\PYG{n}{double}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{,} \PYG{n}{force}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{MyClass(1, 2, 3, 4, 5, 1, 2, 3, 4, 5)} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\subsubsection{PoleError} | |
\label{modules/core:poleerror}\index{PoleError (class in sympy.core.function)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.function.PoleError}\pysigline{\strong{class }\code{sympy.core.function.}\bfcode{PoleError}} | |
\end{fulllineitems} | |
\subsubsection{count\_ops} | |
\label{modules/core:count-ops}\index{count\_ops() (in module sympy.core.function)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.function.count_ops}\pysiglinewithargsret{\code{sympy.core.function.}\bfcode{count\_ops}}{\emph{expr}, \emph{visual=False}}{} | |
Return a representation (integer or expression) of the operations in expr. | |
If \code{visual} is \code{False} (default) then the sum of the coefficients of the | |
visual expression will be returned. | |
If \code{visual} is \code{True} then the number of each type of operation is shown | |
with the core class types (or their virtual equivalent) multiplied by the | |
number of times they occur. | |
If expr is an iterable, the sum of the op counts of the | |
items will be returned. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{a}\PYG{p}{,} \PYG{n}{b}\PYG{p}{,} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{sin}\PYG{p}{,} \PYG{n}{count\PYGZus{}ops} | |
\end{Verbatim} | |
Although there isn't a SUB object, minus signs are interpreted as | |
either negations or subtractions: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{x} \PYG{o}{\PYGZhy{}} \PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{count\PYGZus{}ops}\PYG{p}{(}\PYG{n}{visual}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{SUB} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{n}{x}\PYG{p}{)}\PYG{o}{.}\PYG{n}{count\PYGZus{}ops}\PYG{p}{(}\PYG{n}{visual}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{NEG} | |
\end{Verbatim} | |
Here, there are two Adds and a Pow: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{1} \PYG{o}{+} \PYG{n}{a} \PYG{o}{+} \PYG{n}{b}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{.}\PYG{n}{count\PYGZus{}ops}\PYG{p}{(}\PYG{n}{visual}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{2*ADD + POW} | |
\end{Verbatim} | |
In the following, an Add, Mul, Pow and two functions: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{n}{x} \PYG{o}{+} \PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{.}\PYG{n}{count\PYGZus{}ops}\PYG{p}{(}\PYG{n}{visual}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{ADD + MUL + POW + 2*SIN} | |
\end{Verbatim} | |
for a total of 5: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{n}{x} \PYG{o}{+} \PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{.}\PYG{n}{count\PYGZus{}ops}\PYG{p}{(}\PYG{n}{visual}\PYG{o}{=}\PYG{n+nb+bp}{False}\PYG{p}{)} | |
\PYG{g+go}{5} | |
\end{Verbatim} | |
Note that ``what you type'' is not always what you get. The expression | |
1/x/y is translated by sympy into 1/(x*y) so it gives a DIV and MUL rather | |
than two DIVs: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{o}{/}\PYG{n}{x}\PYG{o}{/}\PYG{n}{y}\PYG{p}{)}\PYG{o}{.}\PYG{n}{count\PYGZus{}ops}\PYG{p}{(}\PYG{n}{visual}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{DIV + MUL} | |
\end{Verbatim} | |
The visual option can be used to demonstrate the difference in | |
operations for expressions in different forms. Here, the Horner | |
representation is compared with the expanded form of a polynomial: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{eq}\PYG{o}{=}\PYG{n}{x}\PYG{o}{*}\PYG{p}{(}\PYG{l+m+mi}{1} \PYG{o}{+} \PYG{n}{x}\PYG{o}{*}\PYG{p}{(}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{n}{x}\PYG{o}{*}\PYG{p}{(}\PYG{l+m+mi}{3} \PYG{o}{+} \PYG{n}{x}\PYG{p}{)}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{count\PYGZus{}ops}\PYG{p}{(}\PYG{n}{eq}\PYG{o}{.}\PYG{n}{expand}\PYG{p}{(}\PYG{p}{)}\PYG{p}{,} \PYG{n}{visual}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} \PYG{o}{\PYGZhy{}} \PYG{n}{count\PYGZus{}ops}\PYG{p}{(}\PYG{n}{eq}\PYG{p}{,} \PYG{n}{visual}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{\PYGZhy{}MUL + 3*POW} | |
\end{Verbatim} | |
The count\_ops function also handles iterables: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{count\PYGZus{}ops}\PYG{p}{(}\PYG{p}{[}\PYG{n}{x}\PYG{p}{,} \PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{,} \PYG{n+nb+bp}{None}\PYG{p}{,} \PYG{n+nb+bp}{True}\PYG{p}{,} \PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{p}{]}\PYG{p}{,} \PYG{n}{visual}\PYG{o}{=}\PYG{n+nb+bp}{False}\PYG{p}{)} | |
\PYG{g+go}{2} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{count\PYGZus{}ops}\PYG{p}{(}\PYG{p}{[}\PYG{n}{x}\PYG{p}{,} \PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{,} \PYG{n+nb+bp}{None}\PYG{p}{,} \PYG{n+nb+bp}{True}\PYG{p}{,} \PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{p}{]}\PYG{p}{,} \PYG{n}{visual}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{ADD + SIN} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{count\PYGZus{}ops}\PYG{p}{(}\PYG{p}{\PYGZob{}}\PYG{n}{x}\PYG{p}{:} \PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{,} \PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{p}{:} \PYG{n}{y} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{\PYGZcb{}}\PYG{p}{,} \PYG{n}{visual}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{2*ADD + SIN} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\subsubsection{expand\_mul} | |
\label{modules/core:expand-mul}\index{expand\_mul() (in module sympy.core.function)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.function.expand_mul}\pysiglinewithargsret{\code{sympy.core.function.}\bfcode{expand\_mul}}{\emph{expr}, \emph{deep=True}}{} | |
Wrapper around expand that only uses the mul hint. See the expand | |
docstring for more information. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{symbols}\PYG{p}{,} \PYG{n}{expand\PYGZus{}mul}\PYG{p}{,} \PYG{n}{exp}\PYG{p}{,} \PYG{n}{log} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x}\PYG{p}{,} \PYG{n}{y} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x,y}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{positive}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expand\PYGZus{}mul}\PYG{p}{(}\PYG{n}{exp}\PYG{p}{(}\PYG{n}{x}\PYG{o}{+}\PYG{n}{y}\PYG{p}{)}\PYG{o}{*}\PYG{p}{(}\PYG{n}{x}\PYG{o}{+}\PYG{n}{y}\PYG{p}{)}\PYG{o}{*}\PYG{n}{log}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{x*exp(x + y)*log(x*y**2) + y*exp(x + y)*log(x*y**2)} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\subsubsection{expand\_log} | |
\label{modules/core:expand-log}\index{expand\_log() (in module sympy.core.function)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.function.expand_log}\pysiglinewithargsret{\code{sympy.core.function.}\bfcode{expand\_log}}{\emph{expr}, \emph{deep=True}, \emph{force=False}}{} | |
Wrapper around expand that only uses the log hint. See the expand | |
docstring for more information. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{symbols}\PYG{p}{,} \PYG{n}{expand\PYGZus{}log}\PYG{p}{,} \PYG{n}{exp}\PYG{p}{,} \PYG{n}{log} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x}\PYG{p}{,} \PYG{n}{y} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x,y}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{positive}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expand\PYGZus{}log}\PYG{p}{(}\PYG{n}{exp}\PYG{p}{(}\PYG{n}{x}\PYG{o}{+}\PYG{n}{y}\PYG{p}{)}\PYG{o}{*}\PYG{p}{(}\PYG{n}{x}\PYG{o}{+}\PYG{n}{y}\PYG{p}{)}\PYG{o}{*}\PYG{n}{log}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{(x + y)*(log(x) + 2*log(y))*exp(x + y)} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\subsubsection{expand\_func} | |
\label{modules/core:expand-func}\index{expand\_func() (in module sympy.core.function)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.function.expand_func}\pysiglinewithargsret{\code{sympy.core.function.}\bfcode{expand\_func}}{\emph{expr}, \emph{deep=True}}{} | |
Wrapper around expand that only uses the func hint. See the expand | |
docstring for more information. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{expand\PYGZus{}func}\PYG{p}{,} \PYG{n}{gamma} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expand\PYGZus{}func}\PYG{p}{(}\PYG{n}{gamma}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{x*(x + 1)*gamma(x)} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\subsubsection{expand\_trig} | |
\label{modules/core:expand-trig}\index{expand\_trig() (in module sympy.core.function)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.function.expand_trig}\pysiglinewithargsret{\code{sympy.core.function.}\bfcode{expand\_trig}}{\emph{expr}, \emph{deep=True}}{} | |
Wrapper around expand that only uses the trig hint. See the expand | |
docstring for more information. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{expand\PYGZus{}trig}\PYG{p}{,} \PYG{n}{sin} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expand\PYGZus{}trig}\PYG{p}{(}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{o}{+}\PYG{n}{y}\PYG{p}{)}\PYG{o}{*}\PYG{p}{(}\PYG{n}{x}\PYG{o}{+}\PYG{n}{y}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{(x + y)*(sin(x)*cos(y) + sin(y)*cos(x))} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\subsubsection{expand\_complex} | |
\label{modules/core:expand-complex}\index{expand\_complex() (in module sympy.core.function)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.function.expand_complex}\pysiglinewithargsret{\code{sympy.core.function.}\bfcode{expand\_complex}}{\emph{expr}, \emph{deep=True}}{} | |
Wrapper around expand that only uses the complex hint. See the expand | |
docstring for more information. | |
\strong{See also:} | |
\code{Expr.as\_real\_imag} | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{expand\PYGZus{}complex}\PYG{p}{,} \PYG{n}{exp}\PYG{p}{,} \PYG{n}{sqrt}\PYG{p}{,} \PYG{n}{I} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{z} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expand\PYGZus{}complex}\PYG{p}{(}\PYG{n}{exp}\PYG{p}{(}\PYG{n}{z}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{I*exp(re(z))*sin(im(z)) + exp(re(z))*cos(im(z))} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expand\PYGZus{}complex}\PYG{p}{(}\PYG{n}{sqrt}\PYG{p}{(}\PYG{n}{I}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{sqrt(2)/2 + sqrt(2)*I/2} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\subsubsection{expand\_multinomial} | |
\label{modules/core:expand-multinomial}\index{expand\_multinomial() (in module sympy.core.function)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.function.expand_multinomial}\pysiglinewithargsret{\code{sympy.core.function.}\bfcode{expand\_multinomial}}{\emph{expr}, \emph{deep=True}}{} | |
Wrapper around expand that only uses the multinomial hint. See the expand | |
docstring for more information. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{symbols}\PYG{p}{,} \PYG{n}{expand\PYGZus{}multinomial}\PYG{p}{,} \PYG{n}{exp} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x}\PYG{p}{,} \PYG{n}{y} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{x y}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{positive}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expand\PYGZus{}multinomial}\PYG{p}{(}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{n}{exp}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+go}{x**2 + 2*x*exp(x + 1) + exp(2*x + 2)} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\subsubsection{expand\_power\_exp} | |
\label{modules/core:expand-power-exp}\index{expand\_power\_exp() (in module sympy.core.function)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.function.expand_power_exp}\pysiglinewithargsret{\code{sympy.core.function.}\bfcode{expand\_power\_exp}}{\emph{expr}, \emph{deep=True}}{} | |
Wrapper around expand that only uses the power\_exp hint. | |
See the expand docstring for more information. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{expand\PYGZus{}power\PYGZus{}exp} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expand\PYGZus{}power\PYGZus{}exp}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{p}{(}\PYG{n}{y} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{x**2*x**y} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\subsubsection{expand\_power\_base} | |
\label{modules/core:expand-power-base}\index{expand\_power\_base() (in module sympy.core.function)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.function.expand_power_base}\pysiglinewithargsret{\code{sympy.core.function.}\bfcode{expand\_power\_base}}{\emph{expr}, \emph{deep=True}, \emph{force=False}}{} | |
Wrapper around expand that only uses the power\_base hint. | |
See the expand docstring for more information. | |
A wrapper to expand(power\_base=True) which separates a power with a base | |
that is a Mul into a product of powers, without performing any other | |
expansions, provided that assumptions about the power's base and exponent | |
allow. | |
deep=False (default is True) will only apply to the top-level expression. | |
force=True (default is False) will cause the expansion to ignore | |
assumptions about the base and exponent. When False, the expansion will | |
only happen if the base is non-negative or the exponent is an integer. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y}\PYG{p}{,} \PYG{n}{z} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{expand\PYGZus{}power\PYGZus{}base}\PYG{p}{,} \PYG{n}{sin}\PYG{p}{,} \PYG{n}{cos}\PYG{p}{,} \PYG{n}{exp} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2} | |
\PYG{g+go}{x**2*y**2} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{n}{y} | |
\PYG{g+go}{(2*x)**y} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expand\PYGZus{}power\PYGZus{}base}\PYG{p}{(}\PYG{n}{\PYGZus{}}\PYG{p}{)} | |
\PYG{g+go}{2**y*x**y} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expand\PYGZus{}power\PYGZus{}base}\PYG{p}{(}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{n}{z}\PYG{p}{)} | |
\PYG{g+go}{(x*y)**z} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expand\PYGZus{}power\PYGZus{}base}\PYG{p}{(}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{n}{z}\PYG{p}{,} \PYG{n}{force}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{x**z*y**z} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expand\PYGZus{}power\PYGZus{}base}\PYG{p}{(}\PYG{n}{sin}\PYG{p}{(}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{n}{z}\PYG{p}{)}\PYG{p}{,} \PYG{n}{deep}\PYG{o}{=}\PYG{n+nb+bp}{False}\PYG{p}{)} | |
\PYG{g+go}{sin((x*y)**z)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expand\PYGZus{}power\PYGZus{}base}\PYG{p}{(}\PYG{n}{sin}\PYG{p}{(}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{n}{z}\PYG{p}{)}\PYG{p}{,} \PYG{n}{force}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{sin(x**z*y**z)} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expand\PYGZus{}power\PYGZus{}base}\PYG{p}{(}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{sin}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{n}{y} \PYG{o}{+} \PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{cos}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)} | |
\PYG{g+go}{2**y*sin(x)**y + 2**y*cos(x)**y} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expand\PYGZus{}power\PYGZus{}base}\PYG{p}{(}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{exp}\PYG{p}{(}\PYG{n}{y}\PYG{p}{)}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{2**x*exp(y)**x} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expand\PYGZus{}power\PYGZus{}base}\PYG{p}{(}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{cos}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)} | |
\PYG{g+go}{2**y*cos(x)**y} | |
\end{Verbatim} | |
Notice that sums are left untouched. If this is not the desired behavior, | |
apply full \code{expand()} to the expression: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expand\PYGZus{}power\PYGZus{}base}\PYG{p}{(}\PYG{p}{(}\PYG{p}{(}\PYG{n}{x}\PYG{o}{+}\PYG{n}{y}\PYG{p}{)}\PYG{o}{*}\PYG{n}{z}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+go}{z**2*(x + y)**2} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{p}{(}\PYG{p}{(}\PYG{n}{x}\PYG{o}{+}\PYG{n}{y}\PYG{p}{)}\PYG{o}{*}\PYG{n}{z}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{.}\PYG{n}{expand}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{x**2*z**2 + 2*x*y*z**2 + y**2*z**2} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{expand\PYGZus{}power\PYGZus{}base}\PYG{p}{(}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{o}{+}\PYG{n}{z}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{2**(z + 1)*y**(z + 1)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{o}{+}\PYG{n}{z}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{expand}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{2*2**z*y*y**z} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\subsubsection{nfloat} | |
\label{modules/core:nfloat}\index{nfloat() (in module sympy.core.function)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.function.nfloat}\pysiglinewithargsret{\code{sympy.core.function.}\bfcode{nfloat}}{\emph{expr}, \emph{n=15}, \emph{exponent=False}}{} | |
Make all Rationals in expr Floats except those in exponents | |
(unless the exponents flag is set to True). | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.core.function} \PYG{k+kn}{import} \PYG{n}{nfloat} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{cos}\PYG{p}{,} \PYG{n}{pi}\PYG{p}{,} \PYG{n}{sqrt} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{nfloat}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{4} \PYG{o}{+} \PYG{n}{x}\PYG{o}{/}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{n}{cos}\PYG{p}{(}\PYG{n}{pi}\PYG{o}{/}\PYG{l+m+mi}{3}\PYG{p}{)} \PYG{o}{+} \PYG{l+m+mi}{1} \PYG{o}{+} \PYG{n}{sqrt}\PYG{p}{(}\PYG{n}{y}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{x**4 + 0.5*x + sqrt(y) + 1.5} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{nfloat}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{4} \PYG{o}{+} \PYG{n}{sqrt}\PYG{p}{(}\PYG{n}{y}\PYG{p}{)}\PYG{p}{,} \PYG{n}{exponent}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{x**4.0 + y**0.5} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\subsection{evalf} | |
\label{modules/core:evalf}\label{modules/core:module-sympy.core.evalf}\index{sympy.core.evalf (module)} | |
\subsubsection{PrecisionExhausted} | |
\label{modules/core:precisionexhausted}\index{PrecisionExhausted (class in sympy.core.evalf)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.evalf.PrecisionExhausted}\pysigline{\strong{class }\code{sympy.core.evalf.}\bfcode{PrecisionExhausted}} | |
\end{fulllineitems} | |
\subsubsection{N} | |
\label{modules/core:n}\index{N (class in sympy.core.evalf)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.evalf.N}\pysigline{\strong{class }\code{sympy.core.evalf.}\bfcode{N}} | |
Calls x.evalf(n, **options). | |
Both .n() and N() are equivalent to .evalf(); use the one that you like better. | |
See also the docstring of .evalf() for information on the options. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{Sum}\PYG{p}{,} \PYG{n}{oo}\PYG{p}{,} \PYG{n}{N} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{k} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Sum}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{o}{/}\PYG{n}{k}\PYG{o}{*}\PYG{o}{*}\PYG{n}{k}\PYG{p}{,} \PYG{p}{(}\PYG{n}{k}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{oo}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{Sum(k**(\PYGZhy{}k), (k, 1, oo))} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{N}\PYG{p}{(}\PYG{n}{\PYGZus{}}\PYG{p}{,} \PYG{l+m+mi}{4}\PYG{p}{)} | |
\PYG{g+go}{1.291} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\subsection{containers} | |
\label{modules/core:module-sympy.core.containers}\label{modules/core:containers}\index{sympy.core.containers (module)} | |
\subsubsection{Tuple} | |
\label{modules/core:tuple}\index{Tuple (class in sympy.core.containers)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.containers.Tuple}\pysigline{\strong{class }\code{sympy.core.containers.}\bfcode{Tuple}} | |
Wrapper around the builtin tuple object | |
The Tuple is a subclass of Basic, so that it works well in the | |
SymPy framework. The wrapped tuple is available as self.args, but | |
you can also access elements or slices with {[}:{]} syntax. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{symbols} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.core.containers} \PYG{k+kn}{import} \PYG{n}{Tuple} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a}\PYG{p}{,} \PYG{n}{b}\PYG{p}{,} \PYG{n}{c}\PYG{p}{,} \PYG{n}{d} \PYG{o}{=} \PYG{n}{symbols}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{a b c d}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Tuple}\PYG{p}{(}\PYG{n}{a}\PYG{p}{,} \PYG{n}{b}\PYG{p}{,} \PYG{n}{c}\PYG{p}{)}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{:}\PYG{p}{]} | |
\PYG{g+go}{(b, c)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Tuple}\PYG{p}{(}\PYG{n}{a}\PYG{p}{,} \PYG{n}{b}\PYG{p}{,} \PYG{n}{c}\PYG{p}{)}\PYG{o}{.}\PYG{n}{subs}\PYG{p}{(}\PYG{n}{a}\PYG{p}{,} \PYG{n}{d}\PYG{p}{)} | |
\PYG{g+go}{(d, b, c)} | |
\end{Verbatim} | |
\index{index() (sympy.core.containers.Tuple method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.containers.Tuple.index}\pysiglinewithargsret{\bfcode{index}}{\emph{value}\optional{, \emph{start}\optional{, \emph{stop}}}}{{ $\rightarrow$ integer -- return first index of value.}} | |
Raises ValueError if the value is not present. | |
\end{fulllineitems} | |
\index{tuple\_count() (sympy.core.containers.Tuple method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.containers.Tuple.tuple_count}\pysiglinewithargsret{\bfcode{tuple\_count}}{\emph{value}}{} | |
T.count(value) -\textgreater{} integer -- return number of occurrences of value | |
\end{fulllineitems} | |
\end{fulllineitems} | |
\subsubsection{Dict} | |
\label{modules/core:dict}\index{Dict (class in sympy.core.containers)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.containers.Dict}\pysigline{\strong{class }\code{sympy.core.containers.}\bfcode{Dict}} | |
Wrapper around the builtin dict object | |
The Dict is a subclass of Basic, so that it works well in the | |
SymPy framework. Because it is immutable, it may be included | |
in sets, but its values must all be given at instantiation and | |
cannot be changed afterwards. Otherwise it behaves identically | |
to the Python dict. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.core.containers} \PYG{k+kn}{import} \PYG{n}{Dict} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{D} \PYG{o}{=} \PYG{n}{Dict}\PYG{p}{(}\PYG{p}{\PYGZob{}}\PYG{l+m+mi}{1}\PYG{p}{:} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{one}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{:} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{two}\PYG{l+s}{\PYGZsq{}}\PYG{p}{\PYGZcb{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{for} \PYG{n}{key} \PYG{o+ow}{in} \PYG{n}{D}\PYG{p}{:} | |
\PYG{g+gp}{... } \PYG{k}{if} \PYG{n}{key} \PYG{o}{==} \PYG{l+m+mi}{1}\PYG{p}{:} | |
\PYG{g+gp}{... } \PYG{k}{print}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{ }\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{\PYGZsq{}} \PYG{o}{\PYGZpc{}} \PYG{p}{(}\PYG{n}{key}\PYG{p}{,} \PYG{n}{D}\PYG{p}{[}\PYG{n}{key}\PYG{p}{]}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{1 one} | |
\end{Verbatim} | |
The args are sympified so the 1 and 2 are Integers and the values | |
are Symbols. Queries automatically sympify args so the following work: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{l+m+mi}{1} \PYG{o+ow}{in} \PYG{n}{D} | |
\PYG{g+go}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{D}\PYG{o}{.}\PYG{n}{has}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{one}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} \PYG{c}{\PYGZsh{} searches keys and values} | |
\PYG{g+go}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{one}\PYG{l+s}{\PYGZsq{}} \PYG{o+ow}{in} \PYG{n}{D} \PYG{c}{\PYGZsh{} not in the keys} | |
\PYG{g+go}{False} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{D}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{]} | |
\PYG{g+go}{one} | |
\end{Verbatim} | |
\index{get() (sympy.core.containers.Dict method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.containers.Dict.get}\pysiglinewithargsret{\bfcode{get}}{\emph{k}\optional{, \emph{d}}}{{ $\rightarrow$ D{[}k{]} if k in D, else d. d defaults to None.}} | |
\end{fulllineitems} | |
\index{items() (sympy.core.containers.Dict method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.containers.Dict.items}\pysiglinewithargsret{\bfcode{items}}{}{{ $\rightarrow$ list of D's (key, value) pairs, as 2-tuples}} | |
\end{fulllineitems} | |
\index{keys() (sympy.core.containers.Dict method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.containers.Dict.keys}\pysiglinewithargsret{\bfcode{keys}}{}{{ $\rightarrow$ list of D's keys}} | |
\end{fulllineitems} | |
\index{values() (sympy.core.containers.Dict method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.containers.Dict.values}\pysiglinewithargsret{\bfcode{values}}{}{{ $\rightarrow$ list of D's values}} | |
\end{fulllineitems} | |
\end{fulllineitems} | |
\subsection{compatibility} | |
\label{modules/core:compatibility}\label{modules/core:module-sympy.core.compatibility}\index{sympy.core.compatibility (module)} | |
\subsubsection{iterable} | |
\label{modules/core:iterable}\index{iterable() (in module sympy.core.compatibility)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.compatibility.iterable}\pysiglinewithargsret{\code{sympy.core.compatibility.}\bfcode{iterable}}{\emph{i}, \emph{exclude=((\textless{}type `str'\textgreater{}}, \emph{\textless{}type `unicode'\textgreater{})}, \emph{\textless{}type `dict'\textgreater{}}, \emph{\textless{}class sympy.core.compatibility.NotIterable at 0x7f446ae7d8d8\textgreater{})}}{} | |
Return a boolean indicating whether \code{i} is SymPy iterable. | |
True also indicates that the iterator is finite, i.e. you e.g. | |
call list(...) on the instance. | |
When SymPy is working with iterables, it is almost always assuming | |
that the iterable is not a string or a mapping, so those are excluded | |
by default. If you want a pure Python definition, make exclude=None. To | |
exclude multiple items, pass them as a tuple. | |
See also: is\_sequence | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.utilities.iterables} \PYG{k+kn}{import} \PYG{n}{iterable} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{Tuple} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{things} \PYG{o}{=} \PYG{p}{[}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{,} \PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,}\PYG{p}{)}\PYG{p}{,} \PYG{n+nb}{set}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{)}\PYG{p}{,} \PYG{n}{Tuple}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{)}\PYG{p}{,} \PYG{p}{(}\PYG{n}{j} \PYG{k}{for} \PYG{n}{j} \PYG{o+ow}{in} \PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{]}\PYG{p}{)}\PYG{p}{,} \PYG{p}{\PYGZob{}}\PYG{l+m+mi}{1}\PYG{p}{:}\PYG{l+m+mi}{2}\PYG{p}{\PYGZcb{}}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{1}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{for} \PYG{n}{i} \PYG{o+ow}{in} \PYG{n}{things}\PYG{p}{:} | |
\PYG{g+gp}{... } \PYG{k}{print}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{ }\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{\PYGZsq{}} \PYG{o}{\PYGZpc{}} \PYG{p}{(}\PYG{n}{iterable}\PYG{p}{(}\PYG{n}{i}\PYG{p}{)}\PYG{p}{,} \PYG{n+nb}{type}\PYG{p}{(}\PYG{n}{i}\PYG{p}{)}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{True \PYGZlt{}... \PYGZsq{}list\PYGZsq{}\PYGZgt{}} | |
\PYG{g+go}{True \PYGZlt{}... \PYGZsq{}tuple\PYGZsq{}\PYGZgt{}} | |
\PYG{g+go}{True \PYGZlt{}... \PYGZsq{}set\PYGZsq{}\PYGZgt{}} | |
\PYG{g+go}{True \PYGZlt{}class \PYGZsq{}sympy.core.containers.Tuple\PYGZsq{}\PYGZgt{}} | |
\PYG{g+go}{True \PYGZlt{}... \PYGZsq{}generator\PYGZsq{}\PYGZgt{}} | |
\PYG{g+go}{False \PYGZlt{}... \PYGZsq{}dict\PYGZsq{}\PYGZgt{}} | |
\PYG{g+go}{False \PYGZlt{}... \PYGZsq{}str\PYGZsq{}\PYGZgt{}} | |
\PYG{g+go}{False \PYGZlt{}... \PYGZsq{}int\PYGZsq{}\PYGZgt{}} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{iterable}\PYG{p}{(}\PYG{p}{\PYGZob{}}\PYG{p}{\PYGZcb{}}\PYG{p}{,} \PYG{n}{exclude}\PYG{o}{=}\PYG{n+nb+bp}{None}\PYG{p}{)} | |
\PYG{g+go}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{iterable}\PYG{p}{(}\PYG{p}{\PYGZob{}}\PYG{p}{\PYGZcb{}}\PYG{p}{,} \PYG{n}{exclude}\PYG{o}{=}\PYG{n+nb}{str}\PYG{p}{)} | |
\PYG{g+go}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{iterable}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s}{no}\PYG{l+s}{\PYGZdq{}}\PYG{p}{,} \PYG{n}{exclude}\PYG{o}{=}\PYG{n+nb}{str}\PYG{p}{)} | |
\PYG{g+go}{False} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\subsubsection{is\_sequence} | |
\label{modules/core:is-sequence}\index{is\_sequence() (in module sympy.core.compatibility)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.compatibility.is_sequence}\pysiglinewithargsret{\code{sympy.core.compatibility.}\bfcode{is\_sequence}}{\emph{i}, \emph{include=None}}{} | |
Return a boolean indicating whether \code{i} is a sequence in the SymPy | |
sense. If anything that fails the test below should be included as | |
being a sequence for your application, set `include' to that object's | |
type; multiple types should be passed as a tuple of types. | |
Note: although generators can generate a sequence, they often need special | |
handling to make sure their elements are captured before the generator is | |
exhausted, so these are not included by default in the definition of a | |
sequence. | |
See also: iterable | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.utilities.iterables} \PYG{k+kn}{import} \PYG{n}{is\PYGZus{}sequence} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{types} \PYG{k+kn}{import} \PYG{n}{GeneratorType} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{is\PYGZus{}sequence}\PYG{p}{(}\PYG{p}{[}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+go}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{is\PYGZus{}sequence}\PYG{p}{(}\PYG{n+nb}{set}\PYG{p}{(}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{False} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{is\PYGZus{}sequence}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{abc}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+go}{False} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{is\PYGZus{}sequence}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{abc}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{include}\PYG{o}{=}\PYG{n+nb}{str}\PYG{p}{)} | |
\PYG{g+go}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{generator} \PYG{o}{=} \PYG{p}{(}\PYG{n}{c} \PYG{k}{for} \PYG{n}{c} \PYG{o+ow}{in} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{abc}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{is\PYGZus{}sequence}\PYG{p}{(}\PYG{n}{generator}\PYG{p}{)} | |
\PYG{g+go}{False} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{is\PYGZus{}sequence}\PYG{p}{(}\PYG{n}{generator}\PYG{p}{,} \PYG{n}{include}\PYG{o}{=}\PYG{p}{(}\PYG{n+nb}{str}\PYG{p}{,} \PYG{n}{GeneratorType}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\subsubsection{as\_int} | |
\label{modules/core:as-int}\index{as\_int() (in module sympy.core.compatibility)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.compatibility.as_int}\pysiglinewithargsret{\code{sympy.core.compatibility.}\bfcode{as\_int}}{\emph{n}}{} | |
Convert the argument to a builtin integer. | |
The return value is guaranteed to be equal to the input. ValueError is | |
raised if the input has a non-integral value. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.core.compatibility} \PYG{k+kn}{import} \PYG{n}{as\PYGZus{}int} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{sqrt} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{l+m+mf}{3.0} | |
\PYG{g+go}{3.0} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{as\PYGZus{}int}\PYG{p}{(}\PYG{l+m+mf}{3.0}\PYG{p}{)} \PYG{c}{\PYGZsh{} convert to int and test for equality} | |
\PYG{g+go}{3} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb}{int}\PYG{p}{(}\PYG{n}{sqrt}\PYG{p}{(}\PYG{l+m+mi}{10}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{3} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{as\PYGZus{}int}\PYG{p}{(}\PYG{n}{sqrt}\PYG{p}{(}\PYG{l+m+mi}{10}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+gt}{Traceback (most recent call last):} | |
\PYG{c}{...} | |
\PYG{g+gr}{ValueError}: \PYG{n}{... is not an integer} | |
\PYG{g+gt}{Traceback (most recent call last):} | |
\PYG{c}{...} | |
\PYG{g+gr}{ValueError}: \PYG{n}{... is not an integer} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\subsection{exprtools} | |
\label{modules/core:module-sympy.core.exprtools}\label{modules/core:exprtools}\index{sympy.core.exprtools (module)} | |
\subsubsection{gcd\_terms} | |
\label{modules/core:gcd-terms}\index{gcd\_terms() (in module sympy.core.exprtools)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.exprtools.gcd_terms}\pysiglinewithargsret{\code{sympy.core.exprtools.}\bfcode{gcd\_terms}}{\emph{terms}, \emph{isprimitive=False}, \emph{clear=True}, \emph{fraction=True}}{} | |
Compute the GCD of \code{terms} and put them together. | |
\code{terms} can be an expression or a non-Basic sequence of expressions | |
which will be handled as though they are terms from a sum. | |
If \code{isprimitive} is True the \_gcd\_terms will not run the primitive | |
method on the terms. | |
\code{clear} controls the removal of integers from the denominator of an Add | |
expression. When True (default), all numerical denominator will be cleared; | |
when False the denominators will be cleared only if all terms had numerical | |
denominators other than 1. | |
\code{fraction}, when True (default), will put the expression over a common | |
denominator. | |
\strong{See also:} | |
{\hyperref[modules/core:sympy.core.exprtools.factor_terms]{\emph{\code{factor\_terms}}}} (\autopageref*{modules/core:sympy.core.exprtools.factor_terms}), {\hyperref[modules/polys/reference:sympy.polys.polytools.terms_gcd]{\emph{\code{sympy.polys.polytools.terms\_gcd}}}} (\autopageref*{modules/polys/reference:sympy.polys.polytools.terms_gcd}) | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.core} \PYG{k+kn}{import} \PYG{n}{gcd\PYGZus{}terms} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{gcd\PYGZus{}terms}\PYG{p}{(}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{y} \PYG{o}{+} \PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{*}\PYG{n}{y}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+go}{y*(x + 1)*(x + y + 1)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{gcd\PYGZus{}terms}\PYG{p}{(}\PYG{n}{x}\PYG{o}{/}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+go}{(x + 2)/2} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{gcd\PYGZus{}terms}\PYG{p}{(}\PYG{n}{x}\PYG{o}{/}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{clear}\PYG{o}{=}\PYG{n+nb+bp}{False}\PYG{p}{)} | |
\PYG{g+go}{x/2 + 1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{gcd\PYGZus{}terms}\PYG{p}{(}\PYG{n}{x}\PYG{o}{/}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{n}{y}\PYG{o}{/}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{n}{clear}\PYG{o}{=}\PYG{n+nb+bp}{False}\PYG{p}{)} | |
\PYG{g+go}{(x + y)/2} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{gcd\PYGZus{}terms}\PYG{p}{(}\PYG{n}{x}\PYG{o}{/}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{o}{/}\PYG{n}{x}\PYG{p}{)} | |
\PYG{g+go}{(x**2 + 2)/(2*x)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{gcd\PYGZus{}terms}\PYG{p}{(}\PYG{n}{x}\PYG{o}{/}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{o}{/}\PYG{n}{x}\PYG{p}{,} \PYG{n}{fraction}\PYG{o}{=}\PYG{n+nb+bp}{False}\PYG{p}{)} | |
\PYG{g+go}{(x + 2/x)/2} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{gcd\PYGZus{}terms}\PYG{p}{(}\PYG{n}{x}\PYG{o}{/}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{o}{/}\PYG{n}{x}\PYG{p}{,} \PYG{n}{fraction}\PYG{o}{=}\PYG{n+nb+bp}{False}\PYG{p}{,} \PYG{n}{clear}\PYG{o}{=}\PYG{n+nb+bp}{False}\PYG{p}{)} | |
\PYG{g+go}{x/2 + 1/x} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{gcd\PYGZus{}terms}\PYG{p}{(}\PYG{n}{x}\PYG{o}{/}\PYG{l+m+mi}{2}\PYG{o}{/}\PYG{n}{y} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{o}{/}\PYG{n}{x}\PYG{o}{/}\PYG{n}{y}\PYG{p}{)} | |
\PYG{g+go}{(x**2 + 2)/(2*x*y)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{gcd\PYGZus{}terms}\PYG{p}{(}\PYG{n}{x}\PYG{o}{/}\PYG{l+m+mi}{2}\PYG{o}{/}\PYG{n}{y} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{o}{/}\PYG{n}{x}\PYG{o}{/}\PYG{n}{y}\PYG{p}{,} \PYG{n}{fraction}\PYG{o}{=}\PYG{n+nb+bp}{False}\PYG{p}{,} \PYG{n}{clear}\PYG{o}{=}\PYG{n+nb+bp}{False}\PYG{p}{)} | |
\PYG{g+go}{(x + 2/x)/(2*y)} | |
\end{Verbatim} | |
The \code{clear} flag was ignored in this case because the returned | |
expression was a rational expression, not a simple sum. | |
\end{fulllineitems} | |
\subsubsection{factor\_terms} | |
\label{modules/core:factor-terms}\index{factor\_terms() (in module sympy.core.exprtools)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/core:sympy.core.exprtools.factor_terms}\pysiglinewithargsret{\code{sympy.core.exprtools.}\bfcode{factor\_terms}}{\emph{expr}, \emph{radical=False}, \emph{clear=False}, \emph{fraction=False}, \emph{sign=True}}{} | |
Remove common factors from terms in all arguments without | |
changing the underlying structure of the expr. No expansion or | |
simplification (and no processing of non-commutatives) is performed. | |
If radical=True then a radical common to all terms will be factored | |
out of any Add sub-expressions of the expr. | |
If clear=False (default) then coefficients will not be separated | |
from a single Add if they can be distributed to leave one or more | |
terms with integer coefficients. | |
If fraction=True (default is False) then a common denominator will be | |
constructed for the expression. | |
If sign=True (default) then even if the only factor in common is a -1, | |
it will be factored out of the expression. | |
\strong{See also:} | |
{\hyperref[modules/core:sympy.core.exprtools.gcd_terms]{\emph{\code{gcd\_terms}}}} (\autopageref*{modules/core:sympy.core.exprtools.gcd_terms}), {\hyperref[modules/polys/reference:sympy.polys.polytools.terms_gcd]{\emph{\code{sympy.polys.polytools.terms\_gcd}}}} (\autopageref*{modules/polys/reference:sympy.polys.polytools.terms_gcd}) | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy} \PYG{k+kn}{import} \PYG{n}{factor\PYGZus{}terms}\PYG{p}{,} \PYG{n}{Symbol} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x}\PYG{p}{,} \PYG{n}{y} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{factor\PYGZus{}terms}\PYG{p}{(}\PYG{n}{x} \PYG{o}{+} \PYG{n}{x}\PYG{o}{*}\PYG{p}{(}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{l+m+mi}{4}\PYG{o}{*}\PYG{n}{y}\PYG{p}{)}\PYG{o}{*}\PYG{o}{*}\PYG{l+m+mi}{3}\PYG{p}{)} | |
\PYG{g+go}{x*(8*(2*y + 1)**3 + 1)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{A} \PYG{o}{=} \PYG{n}{Symbol}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{A}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{commutative}\PYG{o}{=}\PYG{n+nb+bp}{False}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{factor\PYGZus{}terms}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{n}{A} \PYG{o}{+} \PYG{n}{x}\PYG{o}{*}\PYG{n}{A} \PYG{o}{+} \PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{o}{*}\PYG{n}{A}\PYG{p}{)} | |
\PYG{g+go}{x*(y*A + 2*A)} | |
\end{Verbatim} | |
When \code{clear} is False, a rational will only be factored out of an | |
Add expression if all terms of the Add have coefficients that are | |
fractions: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{factor\PYGZus{}terms}\PYG{p}{(}\PYG{n}{x}\PYG{o}{/}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{clear}\PYG{o}{=}\PYG{n+nb+bp}{False}\PYG{p}{)} | |
\PYG{g+go}{x/2 + 1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{factor\PYGZus{}terms}\PYG{p}{(}\PYG{n}{x}\PYG{o}{/}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{clear}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{(x + 2)/2} | |
\end{Verbatim} | |
This only applies when there is a single Add that the coefficient | |
multiplies: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{factor\PYGZus{}terms}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{o}{/}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{n}{y}\PYG{p}{,} \PYG{n}{clear}\PYG{o}{=}\PYG{n+nb+bp}{True}\PYG{p}{)} | |
\PYG{g+go}{y*(x + 2)/2} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{factor\PYGZus{}terms}\PYG{p}{(}\PYG{n}{x}\PYG{o}{*}\PYG{n}{y}\PYG{o}{/}\PYG{l+m+mi}{2} \PYG{o}{+} \PYG{n}{y}\PYG{p}{,} \PYG{n}{clear}\PYG{o}{=}\PYG{n+nb+bp}{False}\PYG{p}{)} \PYG{o}{==} \PYG{n}{\PYGZus{}} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
If a -1 is all that can be factored out, to \emph{not} factor it out, the | |
flag \code{sign} must be False: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{factor\PYGZus{}terms}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{n}{x} \PYG{o}{\PYGZhy{}} \PYG{n}{y}\PYG{p}{)} | |
\PYG{g+go}{\PYGZhy{}(x + y)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{factor\PYGZus{}terms}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{n}{x} \PYG{o}{\PYGZhy{}} \PYG{n}{y}\PYG{p}{,} \PYG{n}{sign}\PYG{o}{=}\PYG{n+nb+bp}{False}\PYG{p}{)} | |
\PYG{g+go}{\PYGZhy{}x \PYGZhy{} y} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{factor\PYGZus{}terms}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{x} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{y}\PYG{p}{,} \PYG{n}{sign}\PYG{o}{=}\PYG{n+nb+bp}{False}\PYG{p}{)} | |
\PYG{g+go}{\PYGZhy{}2*(x + y)} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\section{Combinatorics Module} | |
\label{modules/combinatorics/index:combinatorics-module}\label{modules/combinatorics/index::doc}\label{modules/combinatorics/index:combinatiorics-docs} | |
\subsection{Contents} | |
\label{modules/combinatorics/index:contents} | |
\subsubsection{Partitions} | |
\label{modules/combinatorics/partitions:module-sympy.combinatorics.partitions}\label{modules/combinatorics/partitions:partitions}\label{modules/combinatorics/partitions::doc}\label{modules/combinatorics/partitions:combinatorics-partitions}\index{sympy.combinatorics.partitions (module)}\index{Partition (class in sympy.combinatorics.partitions)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/partitions:sympy.combinatorics.partitions.Partition}\pysigline{\strong{class }\code{sympy.combinatorics.partitions.}\bfcode{Partition}} | |
This class represents an abstract partition. | |
A partition is a set of disjoint sets whose union equals a given set. | |
\strong{See also:} | |
{\hyperref[modules/utilities/iterables:sympy.utilities.iterables.partitions]{\emph{\code{sympy.utilities.iterables.partitions}}}} (\autopageref*{modules/utilities/iterables:sympy.utilities.iterables.partitions}), {\hyperref[modules/utilities/iterables:sympy.utilities.iterables.multiset_partitions]{\emph{\code{sympy.utilities.iterables.multiset\_partitions}}}} (\autopageref*{modules/utilities/iterables:sympy.utilities.iterables.multiset_partitions}) | |
\paragraph{Attributes} | |
\begin{tabulary}{\linewidth}{|L|L|} | |
\hline | |
is\_Complement | |
& \\ | |
\hline | |
is\_EmptySet | |
& \\ | |
\hline | |
is\_Intersection | |
& \\ | |
\hline | |
is\_UniversalSet | |
& \\ | |
\hline\end{tabulary} | |
\index{RGS (sympy.combinatorics.partitions.Partition attribute)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/partitions:sympy.combinatorics.partitions.Partition.RGS}\pysigline{\bfcode{RGS}} | |
Returns the ``restricted growth string'' of the partition. | |
The RGS is returned as a list of indices, L, where L{[}i{]} indicates | |
the block in which element i appears. For example, in a partition | |
of 3 elements (a, b, c) into 2 blocks ({[}c{]}, {[}a, b{]}) the RGS is | |
{[}1, 1, 0{]}: ``a'' is in block 1, ``b'' is in block 1 and ``c'' is in block 0. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.partitions} \PYG{k+kn}{import} \PYG{n}{Partition} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a} \PYG{o}{=} \PYG{n}{Partition}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mi}{3}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mi}{4}\PYG{p}{,} \PYG{l+m+mi}{5}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a}\PYG{o}{.}\PYG{n}{members} | |
\PYG{g+go}{(1, 2, 3, 4, 5)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a}\PYG{o}{.}\PYG{n}{RGS} | |
\PYG{g+go}{(0, 0, 1, 2, 2)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a} \PYG{o}{+} \PYG{l+m+mi}{1} | |
\PYG{g+go}{\PYGZob{}\PYGZob{}3\PYGZcb{}, \PYGZob{}4\PYGZcb{}, \PYGZob{}5\PYGZcb{}, \PYGZob{}1, 2\PYGZcb{}\PYGZcb{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{\PYGZus{}}\PYG{o}{.}\PYG{n}{RGS} | |
\PYG{g+go}{(0, 0, 1, 2, 3)} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{from\_rgs() (sympy.combinatorics.partitions.Partition class method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/partitions:sympy.combinatorics.partitions.Partition.from_rgs}\pysiglinewithargsret{\strong{classmethod }\bfcode{from\_rgs}}{\emph{rgs}, \emph{elements}}{} | |
Creates a set partition from a restricted growth string. | |
The indices given in rgs are assumed to be the index | |
of the element as given in elements \emph{as provided} (the | |
elements are not sorted by this routine). Block numbering | |
starts from 0. If any block was not referenced in \code{rgs} | |
an error will be raised. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.partitions} \PYG{k+kn}{import} \PYG{n}{Partition} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Partition}\PYG{o}{.}\PYG{n}{from\PYGZus{}rgs}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{,} \PYG{n+nb}{list}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{abcde}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{\PYGZob{}\PYGZob{}c\PYGZcb{}, \PYGZob{}a, d\PYGZcb{}, \PYGZob{}b, e\PYGZcb{}\PYGZcb{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Partition}\PYG{o}{.}\PYG{n}{from\PYGZus{}rgs}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{,} \PYG{n+nb}{list}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{cbead}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{\PYGZob{}\PYGZob{}e\PYGZcb{}, \PYGZob{}a, c\PYGZcb{}, \PYGZob{}b, d\PYGZcb{}\PYGZcb{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a} \PYG{o}{=} \PYG{n}{Partition}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{4}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mi}{2}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{5}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Partition}\PYG{o}{.}\PYG{n}{from\PYGZus{}rgs}\PYG{p}{(}\PYG{n}{a}\PYG{o}{.}\PYG{n}{RGS}\PYG{p}{,} \PYG{n}{a}\PYG{o}{.}\PYG{n}{members}\PYG{p}{)} | |
\PYG{g+go}{\PYGZob{}\PYGZob{}2\PYGZcb{}, \PYGZob{}1, 4\PYGZcb{}, \PYGZob{}3, 5\PYGZcb{}\PYGZcb{}} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{partition (sympy.combinatorics.partitions.Partition attribute)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/partitions:sympy.combinatorics.partitions.Partition.partition}\pysigline{\bfcode{partition}} | |
Return partition as a sorted list of lists. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.partitions} \PYG{k+kn}{import} \PYG{n}{Partition} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Partition}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{]}\PYG{p}{)}\PYG{o}{.}\PYG{n}{partition} | |
\PYG{g+go}{[[1], [2, 3]]} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{rank (sympy.combinatorics.partitions.Partition attribute)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/partitions:sympy.combinatorics.partitions.Partition.rank}\pysigline{\bfcode{rank}} | |
Gets the rank of a partition. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.partitions} \PYG{k+kn}{import} \PYG{n}{Partition} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a} \PYG{o}{=} \PYG{n}{Partition}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mi}{3}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mi}{4}\PYG{p}{,} \PYG{l+m+mi}{5}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a}\PYG{o}{.}\PYG{n}{rank} | |
\PYG{g+go}{13} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{sort\_key() (sympy.combinatorics.partitions.Partition method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/partitions:sympy.combinatorics.partitions.Partition.sort_key}\pysiglinewithargsret{\bfcode{sort\_key}}{\emph{order=None}}{} | |
Return a canonical key that can be used for sorting. | |
Ordering is based on the size and sorted elements of the partition | |
and ties are broken with the rank. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.utilities.iterables} \PYG{k+kn}{import} \PYG{n}{default\PYGZus{}sort\PYGZus{}key} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.partitions} \PYG{k+kn}{import} \PYG{n}{Partition} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.abc} \PYG{k+kn}{import} \PYG{n}{x} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a} \PYG{o}{=} \PYG{n}{Partition}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{b} \PYG{o}{=} \PYG{n}{Partition}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{4}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{c} \PYG{o}{=} \PYG{n}{Partition}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{x}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{d} \PYG{o}{=} \PYG{n}{Partition}\PYG{p}{(}\PYG{n+nb}{list}\PYG{p}{(}\PYG{n+nb}{range}\PYG{p}{(}\PYG{l+m+mi}{4}\PYG{p}{)}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{l} \PYG{o}{=} \PYG{p}{[}\PYG{n}{d}\PYG{p}{,} \PYG{n}{b}\PYG{p}{,} \PYG{n}{a} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n}{a}\PYG{p}{,} \PYG{n}{c}\PYG{p}{]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{l}\PYG{o}{.}\PYG{n}{sort}\PYG{p}{(}\PYG{n}{key}\PYG{o}{=}\PYG{n}{default\PYGZus{}sort\PYGZus{}key}\PYG{p}{)}\PYG{p}{;} \PYG{n}{l} | |
\PYG{g+go}{[\PYGZob{}\PYGZob{}1, 2\PYGZcb{}\PYGZcb{}, \PYGZob{}\PYGZob{}1\PYGZcb{}, \PYGZob{}2\PYGZcb{}\PYGZcb{}, \PYGZob{}\PYGZob{}1, x\PYGZcb{}\PYGZcb{}, \PYGZob{}\PYGZob{}3, 4\PYGZcb{}\PYGZcb{}, \PYGZob{}\PYGZob{}0, 1, 2, 3\PYGZcb{}\PYGZcb{}]} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\end{fulllineitems} | |
\index{IntegerPartition (class in sympy.combinatorics.partitions)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/partitions:sympy.combinatorics.partitions.IntegerPartition}\pysigline{\strong{class }\code{sympy.combinatorics.partitions.}\bfcode{IntegerPartition}} | |
This class represents an integer partition. | |
In number theory and combinatorics, a partition of a positive integer, | |
\code{n}, also called an integer partition, is a way of writing \code{n} as a | |
list of positive integers that sum to n. Two partitions that differ only | |
in the order of summands are considered to be the same partition; if order | |
matters then the partitions are referred to as compositions. For example, | |
4 has five partitions: {[}4{]}, {[}3, 1{]}, {[}2, 2{]}, {[}2, 1, 1{]}, and {[}1, 1, 1, 1{]}; | |
the compositions {[}1, 2, 1{]} and {[}1, 1, 2{]} are the same as partition | |
{[}2, 1, 1{]}. | |
\strong{See also:} | |
{\hyperref[modules/utilities/iterables:sympy.utilities.iterables.partitions]{\emph{\code{sympy.utilities.iterables.partitions}}}} (\autopageref*{modules/utilities/iterables:sympy.utilities.iterables.partitions}), {\hyperref[modules/utilities/iterables:sympy.utilities.iterables.multiset_partitions]{\emph{\code{sympy.utilities.iterables.multiset\_partitions}}}} (\autopageref*{modules/utilities/iterables:sympy.utilities.iterables.multiset_partitions}) | |
\begin{description} | |
\item[{\code{Reference}}] \leavevmode | |
\href{http://en.wikipedia.org/wiki/Partition\_\%28number\_theory\%29}{http://en.wikipedia.org/wiki/Partition\_\%28number\_theory\%29} | |
\end{description} | |
\index{as\_dict() (sympy.combinatorics.partitions.IntegerPartition method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/partitions:sympy.combinatorics.partitions.IntegerPartition.as_dict}\pysiglinewithargsret{\bfcode{as\_dict}}{}{} | |
Return the partition as a dictionary whose keys are the | |
partition integers and the values are the multiplicity of that | |
integer. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.partitions} \PYG{k+kn}{import} \PYG{n}{IntegerPartition} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{IntegerPartition}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{]}\PYG{o}{*}\PYG{l+m+mi}{3} \PYG{o}{+} \PYG{p}{[}\PYG{l+m+mi}{2}\PYG{p}{]} \PYG{o}{+} \PYG{p}{[}\PYG{l+m+mi}{3}\PYG{p}{]}\PYG{o}{*}\PYG{l+m+mi}{4}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}dict}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{\PYGZob{}1: 3, 2: 1, 3: 4\PYGZcb{}} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{as\_ferrers() (sympy.combinatorics.partitions.IntegerPartition method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/partitions:sympy.combinatorics.partitions.IntegerPartition.as_ferrers}\pysiglinewithargsret{\bfcode{as\_ferrers}}{\emph{char='\#'}}{} | |
Prints the ferrer diagram of a partition. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.partitions} \PYG{k+kn}{import} \PYG{n}{IntegerPartition} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{n}{IntegerPartition}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{5}\PYG{p}{]}\PYG{p}{)}\PYG{o}{.}\PYG{n}{as\PYGZus{}ferrers}\PYG{p}{(}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{\PYGZsh{}\PYGZsh{}\PYGZsh{}\PYGZsh{}\PYGZsh{}} | |
\PYG{g+go}{\PYGZsh{}} | |
\PYG{g+go}{\PYGZsh{}} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{conjugate (sympy.combinatorics.partitions.IntegerPartition attribute)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/partitions:sympy.combinatorics.partitions.IntegerPartition.conjugate}\pysigline{\bfcode{conjugate}} | |
Computes the conjugate partition of itself. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.partitions} \PYG{k+kn}{import} \PYG{n}{IntegerPartition} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a} \PYG{o}{=} \PYG{n}{IntegerPartition}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{6}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a}\PYG{o}{.}\PYG{n}{conjugate} | |
\PYG{g+go}{[5, 4, 3, 1, 1, 1]} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{next\_lex() (sympy.combinatorics.partitions.IntegerPartition method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/partitions:sympy.combinatorics.partitions.IntegerPartition.next_lex}\pysiglinewithargsret{\bfcode{next\_lex}}{}{} | |
Return the next partition of the integer, n, in lexical order, | |
wrapping around to {[}n{]} if the partition is {[}1, ..., 1{]}. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.partitions} \PYG{k+kn}{import} \PYG{n}{IntegerPartition} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{IntegerPartition}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{n}{p}\PYG{o}{.}\PYG{n}{next\PYGZus{}lex}\PYG{p}{(}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{[4]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{partition} \PYG{o}{\PYGZlt{}} \PYG{n}{p}\PYG{o}{.}\PYG{n}{next\PYGZus{}lex}\PYG{p}{(}\PYG{p}{)}\PYG{o}{.}\PYG{n}{partition} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{prev\_lex() (sympy.combinatorics.partitions.IntegerPartition method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/partitions:sympy.combinatorics.partitions.IntegerPartition.prev_lex}\pysiglinewithargsret{\bfcode{prev\_lex}}{}{} | |
Return the previous partition of the integer, n, in lexical order, | |
wrapping around to {[}1, ..., 1{]} if the partition is {[}n{]}. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.partitions} \PYG{k+kn}{import} \PYG{n}{IntegerPartition} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{IntegerPartition}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{4}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{n}{p}\PYG{o}{.}\PYG{n}{prev\PYGZus{}lex}\PYG{p}{(}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{[3, 1]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{partition} \PYG{o}{\PYGZgt{}} \PYG{n}{p}\PYG{o}{.}\PYG{n}{prev\PYGZus{}lex}\PYG{p}{(}\PYG{p}{)}\PYG{o}{.}\PYG{n}{partition} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\end{fulllineitems} | |
\index{random\_integer\_partition() (in module sympy.combinatorics.partitions)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/partitions:sympy.combinatorics.partitions.random_integer_partition}\pysiglinewithargsret{\code{sympy.combinatorics.partitions.}\bfcode{random\_integer\_partition}}{\emph{n}, \emph{seed=None}}{} | |
Generates a random integer partition summing to \code{n} as a list | |
of reverse-sorted integers. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.partitions} \PYG{k+kn}{import} \PYG{n}{random\PYGZus{}integer\PYGZus{}partition} | |
\end{Verbatim} | |
For the following, a seed is given so a known value can be shown; in | |
practice, the seed would not be given. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{random\PYGZus{}integer\PYGZus{}partition}\PYG{p}{(}\PYG{l+m+mi}{100}\PYG{p}{,} \PYG{n}{seed}\PYG{o}{=}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{12}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{85}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+go}{[85, 12, 2, 1]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{random\PYGZus{}integer\PYGZus{}partition}\PYG{p}{(}\PYG{l+m+mi}{10}\PYG{p}{,} \PYG{n}{seed}\PYG{o}{=}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{5}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+go}{[5, 3, 1, 1]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{random\PYGZus{}integer\PYGZus{}partition}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+go}{[1]} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{RGS\_generalized() (in module sympy.combinatorics.partitions)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/partitions:sympy.combinatorics.partitions.RGS_generalized}\pysiglinewithargsret{\code{sympy.combinatorics.partitions.}\bfcode{RGS\_generalized}}{\emph{m}}{} | |
Computes the m + 1 generalized unrestricted growth strings | |
and returns them as rows in matrix. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.partitions} \PYG{k+kn}{import} \PYG{n}{RGS\PYGZus{}generalized} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{RGS\PYGZus{}generalized}\PYG{p}{(}\PYG{l+m+mi}{6}\PYG{p}{)} | |
\PYG{g+go}{Matrix([} | |
\PYG{g+go}{[ 1, 1, 1, 1, 1, 1, 1],} | |
\PYG{g+go}{[ 1, 2, 3, 4, 5, 6, 0],} | |
\PYG{g+go}{[ 2, 5, 10, 17, 26, 0, 0],} | |
\PYG{g+go}{[ 5, 15, 37, 77, 0, 0, 0],} | |
\PYG{g+go}{[ 15, 52, 151, 0, 0, 0, 0],} | |
\PYG{g+go}{[ 52, 203, 0, 0, 0, 0, 0],} | |
\PYG{g+go}{[203, 0, 0, 0, 0, 0, 0]])} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{RGS\_enum() (in module sympy.combinatorics.partitions)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/partitions:sympy.combinatorics.partitions.RGS_enum}\pysiglinewithargsret{\code{sympy.combinatorics.partitions.}\bfcode{RGS\_enum}}{\emph{m}}{} | |
RGS\_enum computes the total number of restricted growth strings | |
possible for a superset of size m. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.partitions} \PYG{k+kn}{import} \PYG{n}{RGS\PYGZus{}enum} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.partitions} \PYG{k+kn}{import} \PYG{n}{Partition} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{RGS\PYGZus{}enum}\PYG{p}{(}\PYG{l+m+mi}{4}\PYG{p}{)} | |
\PYG{g+go}{15} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{RGS\PYGZus{}enum}\PYG{p}{(}\PYG{l+m+mi}{5}\PYG{p}{)} | |
\PYG{g+go}{52} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{RGS\PYGZus{}enum}\PYG{p}{(}\PYG{l+m+mi}{6}\PYG{p}{)} | |
\PYG{g+go}{203} | |
\end{Verbatim} | |
We can check that the enumeration is correct by actually generating | |
the partitions. Here, the 15 partitions of 4 items are generated: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a} \PYG{o}{=} \PYG{n}{Partition}\PYG{p}{(}\PYG{n+nb}{list}\PYG{p}{(}\PYG{n+nb}{range}\PYG{p}{(}\PYG{l+m+mi}{4}\PYG{p}{)}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{s} \PYG{o}{=} \PYG{n+nb}{set}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{for} \PYG{n}{i} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{l+m+mi}{20}\PYG{p}{)}\PYG{p}{:} | |
\PYG{g+gp}{... } \PYG{n}{s}\PYG{o}{.}\PYG{n}{add}\PYG{p}{(}\PYG{n}{a}\PYG{p}{)} | |
\PYG{g+gp}{... } \PYG{n}{a} \PYG{o}{+}\PYG{o}{=} \PYG{l+m+mi}{1} | |
\PYG{g+gp}{...} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{assert} \PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{s}\PYG{p}{)} \PYG{o}{==} \PYG{l+m+mi}{15} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{RGS\_unrank() (in module sympy.combinatorics.partitions)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/partitions:sympy.combinatorics.partitions.RGS_unrank}\pysiglinewithargsret{\code{sympy.combinatorics.partitions.}\bfcode{RGS\_unrank}}{\emph{rank}, \emph{m}}{} | |
Gives the unranked restricted growth string for a given | |
superset size. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.partitions} \PYG{k+kn}{import} \PYG{n}{RGS\PYGZus{}unrank} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{RGS\PYGZus{}unrank}\PYG{p}{(}\PYG{l+m+mi}{14}\PYG{p}{,} \PYG{l+m+mi}{4}\PYG{p}{)} | |
\PYG{g+go}{[0, 1, 2, 3]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{RGS\PYGZus{}unrank}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{4}\PYG{p}{)} | |
\PYG{g+go}{[0, 0, 0, 0]} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{RGS\_rank() (in module sympy.combinatorics.partitions)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/partitions:sympy.combinatorics.partitions.RGS_rank}\pysiglinewithargsret{\code{sympy.combinatorics.partitions.}\bfcode{RGS\_rank}}{\emph{rgs}}{} | |
Computes the rank of a restricted growth string. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.partitions} \PYG{k+kn}{import} \PYG{n}{RGS\PYGZus{}rank}\PYG{p}{,} \PYG{n}{RGS\PYGZus{}unrank} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{RGS\PYGZus{}rank}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+go}{42} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{RGS\PYGZus{}rank}\PYG{p}{(}\PYG{n}{RGS\PYGZus{}unrank}\PYG{p}{(}\PYG{l+m+mi}{4}\PYG{p}{,} \PYG{l+m+mi}{7}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{4} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\subsubsection{Permutations} | |
\label{modules/combinatorics/permutations:combinatorics-permutations}\label{modules/combinatorics/permutations::doc}\label{modules/combinatorics/permutations:module-sympy.combinatorics.permutations}\label{modules/combinatorics/permutations:permutations}\index{sympy.combinatorics.permutations (module)}\index{Permutation (class in sympy.combinatorics.permutations)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation}\pysigline{\strong{class }\code{sympy.combinatorics.permutations.}\bfcode{Permutation}} | |
A permutation, alternatively known as an `arrangement number' or `ordering' | |
is an arrangement of the elements of an ordered list into a one-to-one | |
mapping with itself. The permutation of a given arrangement is given by | |
indicating the positions of the elements after re-arrangement \phantomsection\label{modules/combinatorics/permutations:id1}{\hyperref[modules/combinatorics/permutations:r8]{\emph{{[}R8{]}}}} (\autopageref*{modules/combinatorics/permutations:r8}). For | |
example, if one started with elements {[}x, y, a, b{]} (in that order) and | |
they were reordered as {[}x, y, b, a{]} then the permutation would be | |
{[}0, 1, 3, 2{]}. Notice that (in SymPy) the first element is always referred | |
to as 0 and the permutation uses the indices of the elements in the | |
original ordering, not the elements (a, b, etc...) themselves. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics} \PYG{k+kn}{import} \PYG{n}{Permutation} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{o}{.}\PYG{n}{print\PYGZus{}cyclic} \PYG{o}{=} \PYG{n+nb+bp}{False} | |
\end{Verbatim} | |
\strong{See also:} | |
{\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Cycle]{\emph{\code{Cycle}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Cycle}) | |
\paragraph{References} | |
\phantomsection\label{modules/combinatorics/permutations:id2}{\hyperref[modules/combinatorics/permutations:r7]{\emph{{[}R7{]}}}} (\autopageref*{modules/combinatorics/permutations:r7}), \phantomsection\label{modules/combinatorics/permutations:id3}{\hyperref[modules/combinatorics/permutations:r8]{\emph{{[}R8{]}}}} (\autopageref*{modules/combinatorics/permutations:r8}), \phantomsection\label{modules/combinatorics/permutations:id4}{\hyperref[modules/combinatorics/permutations:r9]{\emph{{[}R9{]}}}} (\autopageref*{modules/combinatorics/permutations:r9}), \phantomsection\label{modules/combinatorics/permutations:id5}{\hyperref[modules/combinatorics/permutations:r10]{\emph{{[}R10{]}}}} (\autopageref*{modules/combinatorics/permutations:r10}), \phantomsection\label{modules/combinatorics/permutations:id6}{\hyperref[modules/combinatorics/permutations:r11]{\emph{{[}R11{]}}}} (\autopageref*{modules/combinatorics/permutations:r11}), \phantomsection\label{modules/combinatorics/permutations:id7}{\hyperref[modules/combinatorics/permutations:r12]{\emph{{[}R12{]}}}} (\autopageref*{modules/combinatorics/permutations:r12}), \phantomsection\label{modules/combinatorics/permutations:id8}{\hyperref[modules/combinatorics/permutations:r13]{\emph{{[}R13{]}}}} (\autopageref*{modules/combinatorics/permutations:r13}) | |
\paragraph{Permutations Notation} | |
Permutations are commonly represented in disjoint cycle or array forms. | |
\paragraph{Array Notation And 2-line Form} | |
In the 2-line form, the elements and their final positions are shown | |
as a matrix with 2 rows: | |
{[}0 1 2 ... n-1{]} | |
{[}p(0) p(1) p(2) ... p(n-1){]} | |
Since the first line is always range(n), where n is the size of p, | |
it is sufficient to represent the permutation by the second line, | |
referred to as the ``array form'' of the permutation. This is entered | |
in brackets as the argument to the Permutation class: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{)}\PYG{p}{;} \PYG{n}{p} | |
\PYG{g+go}{Permutation([0, 2, 1])} | |
\end{Verbatim} | |
Given i in range(p.size), the permutation maps i to i\textasciicircum{}p | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{[}\PYG{n}{i}\PYG{o}{\PYGZca{}}\PYG{n}{p} \PYG{k}{for} \PYG{n}{i} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{n}{p}\PYG{o}{.}\PYG{n}{size}\PYG{p}{)}\PYG{p}{]} | |
\PYG{g+go}{[0, 2, 1]} | |
\end{Verbatim} | |
The composite of two permutations p*q means first apply p, then q, so | |
i\textasciicircum{}(p*q) = (i\textasciicircum{}p)\textasciicircum{}q which is i\textasciicircum{}p\textasciicircum{}q according to Python precedence rules: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{q} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{[}\PYG{n}{i}\PYG{o}{\PYGZca{}}\PYG{n}{p}\PYG{o}{\PYGZca{}}\PYG{n}{q} \PYG{k}{for} \PYG{n}{i} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{p}{)}\PYG{p}{]} | |
\PYG{g+go}{[2, 0, 1]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{[}\PYG{n}{i}\PYG{o}{\PYGZca{}}\PYG{p}{(}\PYG{n}{p}\PYG{o}{*}\PYG{n}{q}\PYG{p}{)} \PYG{k}{for} \PYG{n}{i} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{p}{)}\PYG{p}{]} | |
\PYG{g+go}{[2, 0, 1]} | |
\end{Verbatim} | |
One can use also the notation p(i) = i\textasciicircum{}p, but then the composition | |
rule is (p*q)(i) = q(p(i)), not p(q(i)): | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{[}\PYG{p}{(}\PYG{n}{p}\PYG{o}{*}\PYG{n}{q}\PYG{p}{)}\PYG{p}{(}\PYG{n}{i}\PYG{p}{)} \PYG{k}{for} \PYG{n}{i} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{n}{p}\PYG{o}{.}\PYG{n}{size}\PYG{p}{)}\PYG{p}{]} | |
\PYG{g+go}{[2, 0, 1]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{[}\PYG{n}{q}\PYG{p}{(}\PYG{n}{p}\PYG{p}{(}\PYG{n}{i}\PYG{p}{)}\PYG{p}{)} \PYG{k}{for} \PYG{n}{i} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{n}{p}\PYG{o}{.}\PYG{n}{size}\PYG{p}{)}\PYG{p}{]} | |
\PYG{g+go}{[2, 0, 1]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{[}\PYG{n}{p}\PYG{p}{(}\PYG{n}{q}\PYG{p}{(}\PYG{n}{i}\PYG{p}{)}\PYG{p}{)} \PYG{k}{for} \PYG{n}{i} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{n}{p}\PYG{o}{.}\PYG{n}{size}\PYG{p}{)}\PYG{p}{]} | |
\PYG{g+go}{[1, 2, 0]} | |
\end{Verbatim} | |
\paragraph{Disjoint Cycle Notation} | |
In disjoint cycle notation, only the elements that have shifted are | |
indicated. In the above case, the 2 and 1 switched places. This can | |
be entered in two ways: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{)} \PYG{o}{==} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{]}\PYG{p}{]}\PYG{p}{)} \PYG{o}{==} \PYG{n}{p} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
Only the relative ordering of elements in a cycle matter: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,}\PYG{l+m+mi}{2}\PYG{p}{,}\PYG{l+m+mi}{3}\PYG{p}{)} \PYG{o}{==} \PYG{n}{Permutation}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{,}\PYG{l+m+mi}{3}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{)} \PYG{o}{==} \PYG{n}{Permutation}\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{,}\PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
The disjoint cycle notation is convenient when representing permutations | |
that have several cycles in them: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{5}\PYG{p}{)} \PYG{o}{==} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{5}\PYG{p}{]}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
It also provides some economy in entry when computing products of | |
permutations that are written in disjoint cycle notation: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{)}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{)} | |
\PYG{g+go}{Permutation([0, 3, 2, 1])} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{\PYGZus{}} \PYG{o}{==} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{]}\PYG{p}{]}\PYG{p}{)}\PYG{o}{*}\PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{]}\PYG{p}{]}\PYG{p}{)}\PYG{o}{*}\PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{p}{[}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{]}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
Entering a singleton in a permutation is a way to indicate the size of the | |
permutation. The \code{size} keyword can also be used. | |
Array-form entry: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mi}{9}\PYG{p}{]}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+go}{Permutation([0, 2, 1], size=10)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{]}\PYG{p}{]}\PYG{p}{,} \PYG{n}{size}\PYG{o}{=}\PYG{l+m+mi}{10}\PYG{p}{)} | |
\PYG{g+go}{Permutation([0, 2, 1], size=10)} | |
\end{Verbatim} | |
Cyclic-form entry: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{n}{size}\PYG{o}{=}\PYG{l+m+mi}{10}\PYG{p}{)} | |
\PYG{g+go}{Permutation([0, 2, 1], size=10)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{p}{(}\PYG{l+m+mi}{9}\PYG{p}{)}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+go}{Permutation([0, 2, 1], size=10)} | |
\end{Verbatim} | |
Caution: no singleton containing an element larger than the largest | |
in any previous cycle can be entered. This is an important difference | |
in how Permutation and Cycle handle the \_\_call\_\_ syntax. A singleton | |
argument at the start of a Permutation performs instantiation of the | |
Permutation and is permitted: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{p}{(}\PYG{l+m+mi}{5}\PYG{p}{)} | |
\PYG{g+go}{Permutation([], size=6)} | |
\end{Verbatim} | |
A singleton entered after instantiation is a call to the permutation | |
-- a function call -- and if the argument is out of range it will | |
trigger an error. For this reason, it is better to start the cycle | |
with the singleton: | |
The following fails because there is is no element 3: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{p}{)} | |
\PYG{g+gt}{Traceback (most recent call last):} | |
\PYG{c}{...} | |
\PYG{g+gr}{IndexError}: \PYG{n}{list index out of range} | |
\PYG{g+gt}{Traceback (most recent call last):} | |
\PYG{c}{...} | |
\PYG{g+gr}{IndexError}: \PYG{n}{list index out of range} | |
\end{Verbatim} | |
This is ok: only the call to an out of range singleton is prohibited; | |
otherwise the permutation autosizes: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{p}{)}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+go}{Permutation([0, 2, 1, 3])} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{4}\PYG{p}{)} \PYG{o}{==} \PYG{n}{Permutation}\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{4}\PYG{p}{)}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
\paragraph{Equality Testing} | |
The array forms must be the same in order for permutations to be equal: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{]}\PYG{p}{)} \PYG{o}{==} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+go}{False} | |
\end{Verbatim} | |
\paragraph{Identity Permutation} | |
The identity permutation is a permutation in which no element is out of | |
place. It can be entered in a variety of ways. All the following create | |
an identity permutation of size 4: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{I} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb}{all}\PYG{p}{(}\PYG{n}{p} \PYG{o}{==} \PYG{n}{I} \PYG{k}{for} \PYG{n}{p} \PYG{o+ow}{in} \PYG{p}{[} | |
\PYG{g+gp}{... }\PYG{n}{Permutation}\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{p}{)}\PYG{p}{,} | |
\PYG{g+gp}{... }\PYG{n}{Permutation}\PYG{p}{(}\PYG{n+nb}{range}\PYG{p}{(}\PYG{l+m+mi}{4}\PYG{p}{)}\PYG{p}{)}\PYG{p}{,} | |
\PYG{g+gp}{... }\PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{p}{]}\PYG{p}{,} \PYG{n}{size}\PYG{o}{=}\PYG{l+m+mi}{4}\PYG{p}{)}\PYG{p}{,} | |
\PYG{g+gp}{... }\PYG{n}{Permutation}\PYG{p}{(}\PYG{n}{size}\PYG{o}{=}\PYG{l+m+mi}{4}\PYG{p}{)}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
Watch out for entering the range \emph{inside} a set of brackets (which is | |
cycle notation): | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{I} \PYG{o}{==} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{n+nb}{range}\PYG{p}{(}\PYG{l+m+mi}{4}\PYG{p}{)}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+go}{False} | |
\end{Verbatim} | |
\paragraph{Permutation Printing} | |
There are a few things to note about how Permutations are printed. | |
1) If you prefer one form (array or cycle) over another, you can set that | |
with the print\_cyclic flag. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{(}\PYG{l+m+mi}{4}\PYG{p}{,} \PYG{l+m+mi}{5}\PYG{p}{)}\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{4}\PYG{p}{)} | |
\PYG{g+go}{Permutation([0, 2, 1, 4, 5, 3])} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{\PYGZus{}} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{o}{.}\PYG{n}{print\PYGZus{}cyclic} \PYG{o}{=} \PYG{n+nb+bp}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} | |
\PYG{g+go}{Permutation(1, 2)(3, 4, 5)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{o}{.}\PYG{n}{print\PYGZus{}cyclic} \PYG{o}{=} \PYG{n+nb+bp}{False} | |
\end{Verbatim} | |
2) Regardless of the setting, a list of elements in the array for cyclic | |
form can be obtained and either of those can be copied and supplied as | |
the argument to Permutation: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{array\PYGZus{}form} | |
\PYG{g+go}{[0, 2, 1, 4, 5, 3]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{cyclic\PYGZus{}form} | |
\PYG{g+go}{[[1, 2], [3, 4, 5]]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{p}{(}\PYG{n}{\PYGZus{}}\PYG{p}{)} \PYG{o}{==} \PYG{n}{p} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
3) Printing is economical in that as little as possible is printed while | |
retaining all information about the size of the permutation: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+go}{Permutation([1, 0, 2, 3])} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{]}\PYG{p}{,} \PYG{n}{size}\PYG{o}{=}\PYG{l+m+mi}{20}\PYG{p}{)} | |
\PYG{g+go}{Permutation([1, 0], size=20)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{4}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{5}\PYG{p}{,} \PYG{l+m+mi}{6}\PYG{p}{]}\PYG{p}{,} \PYG{n}{size}\PYG{o}{=}\PYG{l+m+mi}{20}\PYG{p}{)} | |
\PYG{g+go}{Permutation([1, 0, 2, 4, 3], size=20)} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{o}{.}\PYG{n}{print\PYGZus{}cyclic} \PYG{o}{=} \PYG{n+nb+bp}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} | |
\PYG{g+go}{Permutation(3)(0, 1)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{o}{.}\PYG{n}{print\PYGZus{}cyclic} \PYG{o}{=} \PYG{n+nb+bp}{False} | |
\end{Verbatim} | |
The 2 was not printed but it is still there as can be seen with the | |
array\_form and size methods: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{array\PYGZus{}form} | |
\PYG{g+go}{[1, 0, 2, 3]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{size} | |
\PYG{g+go}{4} | |
\end{Verbatim} | |
\paragraph{Short Introduction To Other Methods} | |
The permutation can act as a bijective function, telling what element is | |
located at a given position | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{q} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{5}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{4}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{q}\PYG{o}{.}\PYG{n}{array\PYGZus{}form}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{]} \PYG{c}{\PYGZsh{} the hard way} | |
\PYG{g+go}{2} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{q}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{)} \PYG{c}{\PYGZsh{} the easy way} | |
\PYG{g+go}{2} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb}{dict}\PYG{p}{(}\PYG{p}{[}\PYG{p}{(}\PYG{n}{i}\PYG{p}{,} \PYG{n}{q}\PYG{p}{(}\PYG{n}{i}\PYG{p}{)}\PYG{p}{)} \PYG{k}{for} \PYG{n}{i} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{n}{q}\PYG{o}{.}\PYG{n}{size}\PYG{p}{)}\PYG{p}{]}\PYG{p}{)} \PYG{c}{\PYGZsh{} showing the bijection} | |
\PYG{g+go}{\PYGZob{}0: 5, 1: 2, 2: 3, 3: 4, 4: 1, 5: 0\PYGZcb{}} | |
\end{Verbatim} | |
The full cyclic form (including singletons) can be obtained: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{full\PYGZus{}cyclic\PYGZus{}form} | |
\PYG{g+go}{[[0, 1], [2], [3]]} | |
\end{Verbatim} | |
Any permutation can be factored into transpositions of pairs of elements: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{4}\PYG{p}{,} \PYG{l+m+mi}{5}\PYG{p}{]}\PYG{p}{]}\PYG{p}{)}\PYG{o}{.}\PYG{n}{transpositions}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{[(1, 2), (3, 5), (3, 4)]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{o}{.}\PYG{n}{rmul}\PYG{p}{(}\PYG{o}{*}\PYG{p}{[}\PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{n}{ti}\PYG{p}{]}\PYG{p}{,} \PYG{n}{size}\PYG{o}{=}\PYG{l+m+mi}{6}\PYG{p}{)} \PYG{k}{for} \PYG{n}{ti} \PYG{o+ow}{in} \PYG{n}{\PYGZus{}}\PYG{p}{]}\PYG{p}{)}\PYG{o}{.}\PYG{n}{cyclic\PYGZus{}form} | |
\PYG{g+go}{[[1, 2], [3, 4, 5]]} | |
\end{Verbatim} | |
The number of permutations on a set of n elements is given by n! and is | |
called the cardinality. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{size} | |
\PYG{g+go}{4} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{cardinality} | |
\PYG{g+go}{24} | |
\end{Verbatim} | |
A given permutation has a rank among all the possible permutations of the | |
same elements, but what that rank is depends on how the permutations are | |
enumerated. (There are a number of different methods of doing so.) The | |
lexicographic rank is given by the rank method and this rank is used to | |
increment a permutation with addition/subtraction: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{rank}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{6} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{+} \PYG{l+m+mi}{1} | |
\PYG{g+go}{Permutation([1, 0, 3, 2])} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{next\PYGZus{}lex}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{Permutation([1, 0, 3, 2])} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{\PYGZus{}}\PYG{o}{.}\PYG{n}{rank}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{7} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{unrank\PYGZus{}lex}\PYG{p}{(}\PYG{n}{p}\PYG{o}{.}\PYG{n}{size}\PYG{p}{,} \PYG{n}{rank}\PYG{o}{=}\PYG{l+m+mi}{7}\PYG{p}{)} | |
\PYG{g+go}{Permutation([1, 0, 3, 2])} | |
\end{Verbatim} | |
The product of two permutations p and q is defined as their composition as | |
functions, (p*q)(i) = q(p(i)) \phantomsection\label{modules/combinatorics/permutations:id9}{\hyperref[modules/combinatorics/permutations:r12]{\emph{{[}R12{]}}}} (\autopageref*{modules/combinatorics/permutations:r12}). | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{q} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb}{list}\PYG{p}{(}\PYG{n}{q}\PYG{o}{*}\PYG{n}{p}\PYG{p}{)} | |
\PYG{g+go}{[2, 3, 0, 1]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb}{list}\PYG{p}{(}\PYG{n}{p}\PYG{o}{*}\PYG{n}{q}\PYG{p}{)} | |
\PYG{g+go}{[3, 2, 1, 0]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{[}\PYG{n}{q}\PYG{p}{(}\PYG{n}{p}\PYG{p}{(}\PYG{n}{i}\PYG{p}{)}\PYG{p}{)} \PYG{k}{for} \PYG{n}{i} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{n}{p}\PYG{o}{.}\PYG{n}{size}\PYG{p}{)}\PYG{p}{]} | |
\PYG{g+go}{[3, 2, 1, 0]} | |
\end{Verbatim} | |
The permutation can be `applied' to any list-like object, not only | |
Permutations: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{p}{(}\PYG{p}{[}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{zero}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{one}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{four}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{l+s}{\PYGZsq{}}\PYG{l+s}{two}\PYG{l+s}{\PYGZsq{}}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+go}{ [\PYGZsq{}one\PYGZsq{}, \PYGZsq{}zero\PYGZsq{}, \PYGZsq{}four\PYGZsq{}, \PYGZsq{}two\PYGZsq{}]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{zo42}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+go}{[\PYGZsq{}o\PYGZsq{}, \PYGZsq{}z\PYGZsq{}, \PYGZsq{}4\PYGZsq{}, \PYGZsq{}2\PYGZsq{}]} | |
\end{Verbatim} | |
If you have a list of arbitrary elements, the corresponding permutation | |
can be found with the from\_sequence method: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{o}{.}\PYG{n}{from\PYGZus{}sequence}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{SymPy}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+go}{Permutation([1, 3, 2, 0, 4])} | |
\end{Verbatim} | |
\index{array\_form (sympy.combinatorics.permutations.Permutation attribute)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.array_form}\pysigline{\bfcode{array\_form}} | |
Return a copy of the attribute \_array\_form | |
Examples | |
======== | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.permutations} \PYG{k+kn}{import} \PYG{n}{Permutation} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{o}{.}\PYG{n}{print\PYGZus{}cyclic} \PYG{o}{=} \PYG{n+nb+bp}{False} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{p}{[}\PYG{l+m+mi}{2}\PYG{p}{,}\PYG{l+m+mi}{0}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mi}{3}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{array\PYGZus{}form} | |
\PYG{g+go}{[2, 3, 0, 1]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{p}{[}\PYG{l+m+mi}{2}\PYG{p}{,}\PYG{l+m+mi}{0}\PYG{p}{,}\PYG{l+m+mi}{3}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{]}\PYG{p}{)}\PYG{o}{.}\PYG{n}{array\PYGZus{}form} | |
\PYG{g+go}{[3, 2, 0, 1]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{2}\PYG{p}{,}\PYG{l+m+mi}{0}\PYG{p}{,}\PYG{l+m+mi}{3}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{)}\PYG{o}{.}\PYG{n}{array\PYGZus{}form} | |
\PYG{g+go}{[2, 0, 3, 1]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mi}{4}\PYG{p}{,} \PYG{l+m+mi}{5}\PYG{p}{]}\PYG{p}{]}\PYG{p}{)}\PYG{o}{.}\PYG{n}{array\PYGZus{}form} | |
\PYG{g+go}{[0, 2, 1, 3, 5, 4]} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{ascents() (sympy.combinatorics.permutations.Permutation method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.ascents}\pysiglinewithargsret{\bfcode{ascents}}{}{} | |
Returns the positions of ascents in a permutation, ie, the location | |
where p{[}i{]} \textless{} p{[}i+1{]} | |
\strong{See also:} | |
{\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.descents]{\emph{\code{descents}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.descents}), {\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.inversions]{\emph{\code{inversions}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.inversions}), {\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.min]{\emph{\code{min}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.min}), {\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.max]{\emph{\code{max}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.max}) | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.permutations} \PYG{k+kn}{import} \PYG{n}{Permutation} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{4}\PYG{p}{,}\PYG{l+m+mi}{0}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{,}\PYG{l+m+mi}{3}\PYG{p}{,}\PYG{l+m+mi}{2}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{ascents}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{[1, 2]} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{atoms() (sympy.combinatorics.permutations.Permutation method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.atoms}\pysiglinewithargsret{\bfcode{atoms}}{}{} | |
Returns all the elements of a permutation | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics} \PYG{k+kn}{import} \PYG{n}{Permutation} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{4}\PYG{p}{,} \PYG{l+m+mi}{5}\PYG{p}{]}\PYG{p}{)}\PYG{o}{.}\PYG{n}{atoms}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{set([0, 1, 2, 3, 4, 5])} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mi}{4}\PYG{p}{,} \PYG{l+m+mi}{5}\PYG{p}{]}\PYG{p}{]}\PYG{p}{)}\PYG{o}{.}\PYG{n}{atoms}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{set([0, 1, 2, 3, 4, 5])} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{cardinality (sympy.combinatorics.permutations.Permutation attribute)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.cardinality}\pysigline{\bfcode{cardinality}} | |
Returns the number of all possible permutations. | |
\strong{See also:} | |
{\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.length]{\emph{\code{length}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.length}), {\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.order]{\emph{\code{order}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.order}), {\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.rank]{\emph{\code{rank}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.rank}), {\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.size]{\emph{\code{size}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.size}) | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.permutations} \PYG{k+kn}{import} \PYG{n}{Permutation} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{,}\PYG{l+m+mi}{2}\PYG{p}{,}\PYG{l+m+mi}{3}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{cardinality} | |
\PYG{g+go}{24} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{commutator() (sympy.combinatorics.permutations.Permutation method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.commutator}\pysiglinewithargsret{\bfcode{commutator}}{\emph{x}}{} | |
Return the commutator of self and x: \code{\textasciitilde{}x*\textasciitilde{}self*x*self} | |
If f and g are part of a group, G, then the commutator of f and g | |
is the group identity iff f and g commute, i.e. fg == gf. | |
\paragraph{References} | |
\href{http://en.wikipedia.org/wiki/Commutator}{http://en.wikipedia.org/wiki/Commutator} | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.permutations} \PYG{k+kn}{import} \PYG{n}{Permutation} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{o}{.}\PYG{n}{print\PYGZus{}cyclic} \PYG{o}{=} \PYG{n+nb+bp}{False} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{x} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{c} \PYG{o}{=} \PYG{n}{p}\PYG{o}{.}\PYG{n}{commutator}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{p}{;} \PYG{n}{c} | |
\PYG{g+go}{Permutation([2, 1, 3, 0])} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{c} \PYG{o}{==} \PYG{o}{\PYGZti{}}\PYG{n}{x}\PYG{o}{*}\PYG{o}{\PYGZti{}}\PYG{n}{p}\PYG{o}{*}\PYG{n}{x}\PYG{o}{*}\PYG{n}{p} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{I} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{p}{[}\PYG{n}{I} \PYG{o}{+} \PYG{n}{i} \PYG{k}{for} \PYG{n}{i} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{l+m+mi}{6}\PYG{p}{)}\PYG{p}{]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{for} \PYG{n}{i} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{p}\PYG{p}{)}\PYG{p}{)}\PYG{p}{:} | |
\PYG{g+gp}{... } \PYG{k}{for} \PYG{n}{j} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{n+nb}{len}\PYG{p}{(}\PYG{n}{p}\PYG{p}{)}\PYG{p}{)}\PYG{p}{:} | |
\PYG{g+gp}{... } \PYG{n}{c} \PYG{o}{=} \PYG{n}{p}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]}\PYG{o}{.}\PYG{n}{commutator}\PYG{p}{(}\PYG{n}{p}\PYG{p}{[}\PYG{n}{j}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{... } \PYG{k}{if} \PYG{n}{p}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]}\PYG{o}{*}\PYG{n}{p}\PYG{p}{[}\PYG{n}{j}\PYG{p}{]} \PYG{o}{==} \PYG{n}{p}\PYG{p}{[}\PYG{n}{j}\PYG{p}{]}\PYG{o}{*}\PYG{n}{p}\PYG{p}{[}\PYG{n}{i}\PYG{p}{]}\PYG{p}{:} | |
\PYG{g+gp}{... } \PYG{k}{assert} \PYG{n}{c} \PYG{o}{==} \PYG{n}{I} | |
\PYG{g+gp}{... } \PYG{k}{else}\PYG{p}{:} | |
\PYG{g+gp}{... } \PYG{k}{assert} \PYG{n}{c} \PYG{o}{!=} \PYG{n}{I} | |
\PYG{g+gp}{...} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{commutes\_with() (sympy.combinatorics.permutations.Permutation method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.commutes_with}\pysiglinewithargsret{\bfcode{commutes\_with}}{\emph{other}}{} | |
Checks if the elements are commuting. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.permutations} \PYG{k+kn}{import} \PYG{n}{Permutation} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,}\PYG{l+m+mi}{4}\PYG{p}{,}\PYG{l+m+mi}{3}\PYG{p}{,}\PYG{l+m+mi}{0}\PYG{p}{,}\PYG{l+m+mi}{2}\PYG{p}{,}\PYG{l+m+mi}{5}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{b} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{,}\PYG{l+m+mi}{2}\PYG{p}{,}\PYG{l+m+mi}{3}\PYG{p}{,}\PYG{l+m+mi}{4}\PYG{p}{,}\PYG{l+m+mi}{5}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a}\PYG{o}{.}\PYG{n}{commutes\PYGZus{}with}\PYG{p}{(}\PYG{n}{b}\PYG{p}{)} | |
\PYG{g+go}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{b} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{2}\PYG{p}{,}\PYG{l+m+mi}{3}\PYG{p}{,}\PYG{l+m+mi}{5}\PYG{p}{,}\PYG{l+m+mi}{4}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{,}\PYG{l+m+mi}{0}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a}\PYG{o}{.}\PYG{n}{commutes\PYGZus{}with}\PYG{p}{(}\PYG{n}{b}\PYG{p}{)} | |
\PYG{g+go}{False} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{cycle\_structure (sympy.combinatorics.permutations.Permutation attribute)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.cycle_structure}\pysigline{\bfcode{cycle\_structure}} | |
Return the cycle structure of the permutation as a dictionary | |
indicating the multiplicity of each cycle length. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics} \PYG{k+kn}{import} \PYG{n}{Permutation} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{o}{.}\PYG{n}{print\PYGZus{}cyclic} \PYG{o}{=} \PYG{n+nb+bp}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{p}{)}\PYG{o}{.}\PYG{n}{cycle\PYGZus{}structure} | |
\PYG{g+go}{\PYGZob{}1: 4\PYGZcb{}} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{4}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{)}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{(}\PYG{l+m+mi}{5}\PYG{p}{,} \PYG{l+m+mi}{6}\PYG{p}{)}\PYG{o}{.}\PYG{n}{cycle\PYGZus{}structure} | |
\PYG{g+go}{\PYGZob{}2: 2, 3: 1\PYGZcb{}} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{cycles (sympy.combinatorics.permutations.Permutation attribute)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.cycles}\pysigline{\bfcode{cycles}} | |
Returns the number of cycles contained in the permutation | |
(including singletons). | |
\strong{See also:} | |
{\hyperref[modules/functions/combinatorial:sympy.functions.combinatorial.numbers.stirling]{\emph{\code{sympy.functions.combinatorial.numbers.stirling}}}} (\autopageref*{modules/functions/combinatorial:sympy.functions.combinatorial.numbers.stirling}) | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics} \PYG{k+kn}{import} \PYG{n}{Permutation} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{]}\PYG{p}{)}\PYG{o}{.}\PYG{n}{cycles} | |
\PYG{g+go}{3} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{]}\PYG{p}{)}\PYG{o}{.}\PYG{n}{full\PYGZus{}cyclic\PYGZus{}form} | |
\PYG{g+go}{[[0], [1], [2]]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{)}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{)}\PYG{o}{.}\PYG{n}{cycles} | |
\PYG{g+go}{2} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{cyclic\_form (sympy.combinatorics.permutations.Permutation attribute)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.cyclic_form}\pysigline{\bfcode{cyclic\_form}} | |
This is used to convert to the cyclic notation | |
from the canonical notation. Singletons are omitted. | |
\strong{See also:} | |
{\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.array_form]{\emph{\code{array\_form}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.array_form}), {\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.full_cyclic_form]{\emph{\code{full\_cyclic\_form}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.full_cyclic_form}) | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.permutations} \PYG{k+kn}{import} \PYG{n}{Permutation} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{o}{.}\PYG{n}{print\PYGZus{}cyclic} \PYG{o}{=} \PYG{n+nb+bp}{False} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{cyclic\PYGZus{}form} | |
\PYG{g+go}{[[1, 3, 2]]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{4}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{5}\PYG{p}{]}\PYG{p}{)}\PYG{o}{.}\PYG{n}{cyclic\PYGZus{}form} | |
\PYG{g+go}{[[0, 1], [3, 4]]} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{descents() (sympy.combinatorics.permutations.Permutation method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.descents}\pysiglinewithargsret{\bfcode{descents}}{}{} | |
Returns the positions of descents in a permutation, ie, the location | |
where p{[}i{]} \textgreater{} p{[}i+1{]} | |
\strong{See also:} | |
{\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.ascents]{\emph{\code{ascents}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.ascents}), {\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.inversions]{\emph{\code{inversions}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.inversions}), {\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.min]{\emph{\code{min}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.min}), {\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.max]{\emph{\code{max}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.max}) | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.permutations} \PYG{k+kn}{import} \PYG{n}{Permutation} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{4}\PYG{p}{,}\PYG{l+m+mi}{0}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{,}\PYG{l+m+mi}{3}\PYG{p}{,}\PYG{l+m+mi}{2}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{descents}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{[0, 3]} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{from\_inversion\_vector() (sympy.combinatorics.permutations.Permutation class method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.from_inversion_vector}\pysiglinewithargsret{\strong{classmethod }\bfcode{from\_inversion\_vector}}{\emph{inversion}}{} | |
Calculates the permutation from the inversion vector. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.permutations} \PYG{k+kn}{import} \PYG{n}{Permutation} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{o}{.}\PYG{n}{print\PYGZus{}cyclic} \PYG{o}{=} \PYG{n+nb+bp}{False} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{o}{.}\PYG{n}{from\PYGZus{}inversion\PYGZus{}vector}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+go}{Permutation([3, 2, 1, 0, 4, 5])} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{from\_sequence() (sympy.combinatorics.permutations.Permutation class method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.from_sequence}\pysiglinewithargsret{\strong{classmethod }\bfcode{from\_sequence}}{\emph{i}, \emph{key=None}}{} | |
Return the permutation needed to obtain \code{i} from the sorted | |
elements of \code{i}. If custom sorting is desired, a key can be given. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics} \PYG{k+kn}{import} \PYG{n}{Permutation} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{o}{.}\PYG{n}{print\PYGZus{}cyclic} \PYG{o}{=} \PYG{n+nb+bp}{True} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{o}{.}\PYG{n}{from\PYGZus{}sequence}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{SymPy}\PYG{l+s}{\PYGZsq{}}\PYG{p}{)} | |
\PYG{g+go}{Permutation(4)(0, 1, 3)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{\PYGZus{}}\PYG{p}{(}\PYG{n+nb}{sorted}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s}{SymPy}\PYG{l+s}{\PYGZdq{}}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{[\PYGZsq{}S\PYGZsq{}, \PYGZsq{}y\PYGZsq{}, \PYGZsq{}m\PYGZsq{}, \PYGZsq{}P\PYGZsq{}, \PYGZsq{}y\PYGZsq{}]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{o}{.}\PYG{n}{from\PYGZus{}sequence}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{SymPy}\PYG{l+s}{\PYGZsq{}}\PYG{p}{,} \PYG{n}{key}\PYG{o}{=}\PYG{k}{lambda} \PYG{n}{x}\PYG{p}{:} \PYG{n}{x}\PYG{o}{.}\PYG{n}{lower}\PYG{p}{(}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{Permutation(4)(0, 2)(1, 3)} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{full\_cyclic\_form (sympy.combinatorics.permutations.Permutation attribute)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.full_cyclic_form}\pysigline{\bfcode{full\_cyclic\_form}} | |
Return permutation in cyclic form including singletons. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.permutations} \PYG{k+kn}{import} \PYG{n}{Permutation} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{)}\PYG{o}{.}\PYG{n}{full\PYGZus{}cyclic\PYGZus{}form} | |
\PYG{g+go}{[[0], [1, 2]]} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{get\_adjacency\_distance() (sympy.combinatorics.permutations.Permutation method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.get_adjacency_distance}\pysiglinewithargsret{\bfcode{get\_adjacency\_distance}}{\emph{other}}{} | |
Computes the adjacency distance between two permutations. | |
This metric counts the number of times a pair i,j of jobs is | |
adjacent in both p and p'. If n\_adj is this quantity then | |
the adjacency distance is n - n\_adj - 1 {[}1{]} | |
{[}1{]} Reeves, Colin R. Landscapes, Operators and Heuristic search, Annals | |
of Operational Research, 86, pp 473-490. (1999) | |
\strong{See also:} | |
{\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.get_precedence_matrix]{\emph{\code{get\_precedence\_matrix}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.get_precedence_matrix}), {\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.get_precedence_distance]{\emph{\code{get\_precedence\_distance}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.get_precedence_distance}), {\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.get_adjacency_matrix]{\emph{\code{get\_adjacency\_matrix}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.get_adjacency_matrix}) | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.permutations} \PYG{k+kn}{import} \PYG{n}{Permutation} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{4}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{q} \PYG{o}{=} \PYG{n}{Permutation}\PYG{o}{.}\PYG{n}{josephus}\PYG{p}{(}\PYG{l+m+mi}{4}\PYG{p}{,} \PYG{l+m+mi}{5}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{get\PYGZus{}adjacency\PYGZus{}distance}\PYG{p}{(}\PYG{n}{q}\PYG{p}{)} | |
\PYG{g+go}{3} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{r} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{4}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{get\PYGZus{}adjacency\PYGZus{}distance}\PYG{p}{(}\PYG{n}{r}\PYG{p}{)} | |
\PYG{g+go}{4} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{get\_adjacency\_matrix() (sympy.combinatorics.permutations.Permutation method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.get_adjacency_matrix}\pysiglinewithargsret{\bfcode{get\_adjacency\_matrix}}{}{} | |
Computes the adjacency matrix of a permutation. | |
If job i is adjacent to job j in a permutation p | |
then we set m{[}i, j{]} = 1 where m is the adjacency | |
matrix of p. | |
\strong{See also:} | |
{\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.get_precedence_matrix]{\emph{\code{get\_precedence\_matrix}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.get_precedence_matrix}), {\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.get_precedence_distance]{\emph{\code{get\_precedence\_distance}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.get_precedence_distance}), {\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.get_adjacency_distance]{\emph{\code{get\_adjacency\_distance}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.get_adjacency_distance}) | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.permutations} \PYG{k+kn}{import} \PYG{n}{Permutation} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{Permutation}\PYG{o}{.}\PYG{n}{josephus}\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{p}{,}\PYG{l+m+mi}{6}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{get\PYGZus{}adjacency\PYGZus{}matrix}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{Matrix([} | |
\PYG{g+go}{[0, 0, 0, 0, 0, 0],} | |
\PYG{g+go}{[0, 0, 0, 0, 1, 0],} | |
\PYG{g+go}{[0, 0, 0, 0, 0, 1],} | |
\PYG{g+go}{[0, 1, 0, 0, 0, 0],} | |
\PYG{g+go}{[1, 0, 0, 0, 0, 0],} | |
\PYG{g+go}{[0, 0, 0, 1, 0, 0]])} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{q} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{q}\PYG{o}{.}\PYG{n}{get\PYGZus{}adjacency\PYGZus{}matrix}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{Matrix([} | |
\PYG{g+go}{[0, 1, 0, 0],} | |
\PYG{g+go}{[0, 0, 1, 0],} | |
\PYG{g+go}{[0, 0, 0, 1],} | |
\PYG{g+go}{[0, 0, 0, 0]])} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{get\_positional\_distance() (sympy.combinatorics.permutations.Permutation method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.get_positional_distance}\pysiglinewithargsret{\bfcode{get\_positional\_distance}}{\emph{other}}{} | |
Computes the positional distance between two permutations. | |
\strong{See also:} | |
{\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.get_precedence_distance]{\emph{\code{get\_precedence\_distance}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.get_precedence_distance}), {\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.get_adjacency_distance]{\emph{\code{get\_adjacency\_distance}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.get_adjacency_distance}) | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.permutations} \PYG{k+kn}{import} \PYG{n}{Permutation} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{4}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{q} \PYG{o}{=} \PYG{n}{Permutation}\PYG{o}{.}\PYG{n}{josephus}\PYG{p}{(}\PYG{l+m+mi}{4}\PYG{p}{,} \PYG{l+m+mi}{5}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{r} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{4}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{get\PYGZus{}positional\PYGZus{}distance}\PYG{p}{(}\PYG{n}{q}\PYG{p}{)} | |
\PYG{g+go}{12} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{get\PYGZus{}positional\PYGZus{}distance}\PYG{p}{(}\PYG{n}{r}\PYG{p}{)} | |
\PYG{g+go}{12} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{get\_precedence\_distance() (sympy.combinatorics.permutations.Permutation method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.get_precedence_distance}\pysiglinewithargsret{\bfcode{get\_precedence\_distance}}{\emph{other}}{} | |
Computes the precedence distance between two permutations. | |
Suppose p and p' represent n jobs. The precedence metric | |
counts the number of times a job j is preceded by job i | |
in both p and p'. This metric is commutative. | |
\strong{See also:} | |
{\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.get_precedence_matrix]{\emph{\code{get\_precedence\_matrix}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.get_precedence_matrix}), {\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.get_adjacency_matrix]{\emph{\code{get\_adjacency\_matrix}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.get_adjacency_matrix}), {\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.get_adjacency_distance]{\emph{\code{get\_adjacency\_distance}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.get_adjacency_distance}) | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.permutations} \PYG{k+kn}{import} \PYG{n}{Permutation} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{4}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{q} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{4}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{get\PYGZus{}precedence\PYGZus{}distance}\PYG{p}{(}\PYG{n}{q}\PYG{p}{)} | |
\PYG{g+go}{7} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{q}\PYG{o}{.}\PYG{n}{get\PYGZus{}precedence\PYGZus{}distance}\PYG{p}{(}\PYG{n}{p}\PYG{p}{)} | |
\PYG{g+go}{7} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{get\_precedence\_matrix() (sympy.combinatorics.permutations.Permutation method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.get_precedence_matrix}\pysiglinewithargsret{\bfcode{get\_precedence\_matrix}}{}{} | |
Gets the precedence matrix. This is used for computing the | |
distance between two permutations. | |
\strong{See also:} | |
{\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.get_precedence_distance]{\emph{\code{get\_precedence\_distance}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.get_precedence_distance}), {\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.get_adjacency_matrix]{\emph{\code{get\_adjacency\_matrix}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.get_adjacency_matrix}), {\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.get_adjacency_distance]{\emph{\code{get\_adjacency\_distance}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.get_adjacency_distance}) | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.permutations} \PYG{k+kn}{import} \PYG{n}{Permutation} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{Permutation}\PYG{o}{.}\PYG{n}{josephus}\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{p}{,}\PYG{l+m+mi}{6}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} | |
\PYG{g+go}{Permutation([2, 5, 3, 1, 4, 0])} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{get\PYGZus{}precedence\PYGZus{}matrix}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{Matrix([} | |
\PYG{g+go}{[0, 0, 0, 0, 0, 0],} | |
\PYG{g+go}{[1, 0, 0, 0, 1, 0],} | |
\PYG{g+go}{[1, 1, 0, 1, 1, 1],} | |
\PYG{g+go}{[1, 1, 0, 0, 1, 0],} | |
\PYG{g+go}{[1, 0, 0, 0, 0, 0],} | |
\PYG{g+go}{[1, 1, 0, 1, 1, 0]])} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{index() (sympy.combinatorics.permutations.Permutation method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.index}\pysiglinewithargsret{\bfcode{index}}{}{} | |
Returns the index of a permutation. | |
The index of a permutation is the sum of all subscripts j such | |
that p{[}j{]} is greater than p{[}j+1{]}. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.permutations} \PYG{k+kn}{import} \PYG{n}{Permutation} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{4}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{index}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{2} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{inversion\_vector() (sympy.combinatorics.permutations.Permutation method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.inversion_vector}\pysiglinewithargsret{\bfcode{inversion\_vector}}{}{} | |
Return the inversion vector of the permutation. | |
The inversion vector consists of elements whose value | |
indicates the number of elements in the permutation | |
that are lesser than it and lie on its right hand side. | |
The inversion vector is the same as the Lehmer encoding of a | |
permutation. | |
\strong{See also:} | |
{\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.from_inversion_vector]{\emph{\code{from\_inversion\_vector}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.from_inversion_vector}) | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.permutations} \PYG{k+kn}{import} \PYG{n}{Permutation} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{4}\PYG{p}{,} \PYG{l+m+mi}{8}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{7}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{5}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{6}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{inversion\PYGZus{}vector}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{[4, 7, 0, 5, 0, 2, 1, 1]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{inversion\PYGZus{}vector}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{[3, 2, 1]} | |
\end{Verbatim} | |
The inversion vector increases lexicographically with the rank | |
of the permutation, the -ith element cycling through 0..i. | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{while} \PYG{n}{p}\PYG{p}{:} | |
\PYG{g+gp}{... } \PYG{k}{print}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{ }\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{ }\PYG{l+s+si}{\PYGZpc{}s}\PYG{l+s}{\PYGZsq{}} \PYG{o}{\PYGZpc{}} \PYG{p}{(}\PYG{n}{p}\PYG{p}{,} \PYG{n}{p}\PYG{o}{.}\PYG{n}{inversion\PYGZus{}vector}\PYG{p}{(}\PYG{p}{)}\PYG{p}{,} \PYG{n}{p}\PYG{o}{.}\PYG{n}{rank}\PYG{p}{(}\PYG{p}{)}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+gp}{... } \PYG{n}{p} \PYG{o}{=} \PYG{n}{p}\PYG{o}{.}\PYG{n}{next\PYGZus{}lex}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+gp}{...} | |
\PYG{g+go}{Permutation([0, 1, 2]) [0, 0] 0} | |
\PYG{g+go}{Permutation([0, 2, 1]) [0, 1] 1} | |
\PYG{g+go}{Permutation([1, 0, 2]) [1, 0] 2} | |
\PYG{g+go}{Permutation([1, 2, 0]) [1, 1] 3} | |
\PYG{g+go}{Permutation([2, 0, 1]) [2, 0] 4} | |
\PYG{g+go}{Permutation([2, 1, 0]) [2, 1] 5} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{inversions() (sympy.combinatorics.permutations.Permutation method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.inversions}\pysiglinewithargsret{\bfcode{inversions}}{}{} | |
Computes the number of inversions of a permutation. | |
An inversion is where i \textgreater{} j but p{[}i{]} \textless{} p{[}j{]}. | |
For small length of p, it iterates over all i and j | |
values and calculates the number of inversions. | |
For large length of p, it uses a variation of merge | |
sort to calculate the number of inversions. | |
\strong{See also:} | |
{\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.descents]{\emph{\code{descents}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.descents}), {\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.ascents]{\emph{\code{ascents}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.ascents}), {\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.min]{\emph{\code{min}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.min}), {\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.max]{\emph{\code{max}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.max}) | |
\paragraph{References} | |
{[}1{]} \href{http://www.cp.eng.chula.ac.th/~piak/teaching/algo/algo2008/count-inv.htm}{http://www.cp.eng.chula.ac.th/\textasciitilde{}piak/teaching/algo/algo2008/count-inv.htm} | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.permutations} \PYG{k+kn}{import} \PYG{n}{Permutation} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{,}\PYG{l+m+mi}{2}\PYG{p}{,}\PYG{l+m+mi}{3}\PYG{p}{,}\PYG{l+m+mi}{4}\PYG{p}{,}\PYG{l+m+mi}{5}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{inversions}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{0} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{3}\PYG{p}{,}\PYG{l+m+mi}{2}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{,}\PYG{l+m+mi}{0}\PYG{p}{]}\PYG{p}{)}\PYG{o}{.}\PYG{n}{inversions}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{6} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{is\_Empty (sympy.combinatorics.permutations.Permutation attribute)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.is_Empty}\pysigline{\bfcode{is\_Empty}} | |
Checks to see if the permutation is a set with zero elements | |
\strong{See also:} | |
{\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.is_Singleton]{\emph{\code{is\_Singleton}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.is_Singleton}) | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics} \PYG{k+kn}{import} \PYG{n}{Permutation} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{p}{]}\PYG{p}{)}\PYG{o}{.}\PYG{n}{is\PYGZus{}Empty} | |
\PYG{g+go}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{]}\PYG{p}{)}\PYG{o}{.}\PYG{n}{is\PYGZus{}Empty} | |
\PYG{g+go}{False} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{is\_Identity (sympy.combinatorics.permutations.Permutation attribute)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.is_Identity}\pysigline{\bfcode{is\_Identity}} | |
Returns True if the Permutation is an identity permutation. | |
\strong{See also:} | |
{\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.order]{\emph{\code{order}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.order}) | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.permutations} \PYG{k+kn}{import} \PYG{n}{Permutation} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{is\PYGZus{}Identity} | |
\PYG{g+go}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mi}{2}\PYG{p}{]}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{is\PYGZus{}Identity} | |
\PYG{g+go}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{is\PYGZus{}Identity} | |
\PYG{g+go}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{is\PYGZus{}Identity} | |
\PYG{g+go}{False} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{is\_Singleton (sympy.combinatorics.permutations.Permutation attribute)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.is_Singleton}\pysigline{\bfcode{is\_Singleton}} | |
Checks to see if the permutation contains only one number and is | |
thus the only possible permutation of this set of numbers | |
\strong{See also:} | |
{\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.is_Empty]{\emph{\code{is\_Empty}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.is_Empty}) | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics} \PYG{k+kn}{import} \PYG{n}{Permutation} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{]}\PYG{p}{)}\PYG{o}{.}\PYG{n}{is\PYGZus{}Singleton} | |
\PYG{g+go}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{)}\PYG{o}{.}\PYG{n}{is\PYGZus{}Singleton} | |
\PYG{g+go}{False} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{is\_even (sympy.combinatorics.permutations.Permutation attribute)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.is_even}\pysigline{\bfcode{is\_even}} | |
Checks if a permutation is even. | |
\strong{See also:} | |
{\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.is_odd]{\emph{\code{is\_odd}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.is_odd}) | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.permutations} \PYG{k+kn}{import} \PYG{n}{Permutation} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{,}\PYG{l+m+mi}{2}\PYG{p}{,}\PYG{l+m+mi}{3}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{is\PYGZus{}even} | |
\PYG{g+go}{True} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{3}\PYG{p}{,}\PYG{l+m+mi}{2}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{,}\PYG{l+m+mi}{0}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{is\PYGZus{}even} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{is\_odd (sympy.combinatorics.permutations.Permutation attribute)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.is_odd}\pysigline{\bfcode{is\_odd}} | |
Checks if a permutation is odd. | |
\strong{See also:} | |
{\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.is_even]{\emph{\code{is\_even}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.is_even}) | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.permutations} \PYG{k+kn}{import} \PYG{n}{Permutation} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{,}\PYG{l+m+mi}{2}\PYG{p}{,}\PYG{l+m+mi}{3}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{is\PYGZus{}odd} | |
\PYG{g+go}{False} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{3}\PYG{p}{,}\PYG{l+m+mi}{2}\PYG{p}{,}\PYG{l+m+mi}{0}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{is\PYGZus{}odd} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{josephus() (sympy.combinatorics.permutations.Permutation class method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.josephus}\pysiglinewithargsret{\strong{classmethod }\bfcode{josephus}}{\emph{m}, \emph{n}, \emph{s=1}}{} | |
Return as a permutation the shuffling of range(n) using the Josephus | |
scheme in which every m-th item is selected until all have been chosen. | |
The returned permutation has elements listed by the order in which they | |
were selected. | |
The parameter \code{s} stops the selection process when there are \code{s} | |
items remaining and these are selected by continuing the selection, | |
counting by 1 rather than by \code{m}. | |
Consider selecting every 3rd item from 6 until only 2 remain: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
choices chosen | |
======== ====== | |
012345 | |
01 345 2 | |
01 34 25 | |
01 4 253 | |
0 4 2531 | |
0 25314 | |
253140 | |
\end{Verbatim} | |
\paragraph{References} | |
\begin{enumerate} | |
\item {} | |
\href{http://en.wikipedia.org/wiki/Flavius\_Josephus}{http://en.wikipedia.org/wiki/Flavius\_Josephus} | |
\item {} | |
\href{http://en.wikipedia.org/wiki/Josephus\_problem}{http://en.wikipedia.org/wiki/Josephus\_problem} | |
\item {} | |
\href{http://www.wou.edu/~burtonl/josephus.html}{http://www.wou.edu/\textasciitilde{}burtonl/josephus.html} | |
\end{enumerate} | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics} \PYG{k+kn}{import} \PYG{n}{Permutation} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{o}{.}\PYG{n}{josephus}\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{6}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{.}\PYG{n}{array\PYGZus{}form} | |
\PYG{g+go}{[2, 5, 3, 1, 4, 0]} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{length() (sympy.combinatorics.permutations.Permutation method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.length}\pysiglinewithargsret{\bfcode{length}}{}{} | |
Returns the number of integers moved by a permutation. | |
\strong{See also:} | |
{\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.min]{\emph{\code{min}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.min}), {\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.max]{\emph{\code{max}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.max}), {\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.support]{\emph{\code{support}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.support}), {\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.cardinality]{\emph{\code{cardinality}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.cardinality}), {\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.order]{\emph{\code{order}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.order}), {\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.rank]{\emph{\code{rank}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.rank}), {\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.size]{\emph{\code{size}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.size}) | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics} \PYG{k+kn}{import} \PYG{n}{Permutation} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{)}\PYG{o}{.}\PYG{n}{length}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{2} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{]}\PYG{p}{]}\PYG{p}{)}\PYG{o}{.}\PYG{n}{length}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{4} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{list() (sympy.combinatorics.permutations.Permutation method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.list}\pysiglinewithargsret{\bfcode{list}}{\emph{size=None}}{} | |
Return the permutation as an explicit list, possibly | |
trimming unmoved elements if size is less than the maximum | |
element in the permutation; if this is desired, setting | |
\code{size=-1} will guarantee such trimming. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.permutations} \PYG{k+kn}{import} \PYG{n}{Permutation} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{o}{.}\PYG{n}{print\PYGZus{}cyclic} \PYG{o}{=} \PYG{n+nb+bp}{False} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{)}\PYG{p}{(}\PYG{l+m+mi}{4}\PYG{p}{,} \PYG{l+m+mi}{5}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{list}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{[0, 1, 3, 2, 5, 4]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{list}\PYG{p}{(}\PYG{l+m+mi}{10}\PYG{p}{)} | |
\PYG{g+go}{[0, 1, 3, 2, 5, 4, 6, 7, 8, 9]} | |
\end{Verbatim} | |
Passing a length too small will trim trailing, unchanged elements | |
in the permutation: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{4}\PYG{p}{)}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{4}\PYG{p}{)}\PYG{o}{.}\PYG{n}{list}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+go}{[0, 2, 1]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{p}{)}\PYG{o}{.}\PYG{n}{list}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+go}{[]} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{max() (sympy.combinatorics.permutations.Permutation method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.max}\pysiglinewithargsret{\bfcode{max}}{}{} | |
The maximum element moved by the permutation. | |
\strong{See also:} | |
{\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.min]{\emph{\code{min}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.min}), {\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.descents]{\emph{\code{descents}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.descents}), {\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.ascents]{\emph{\code{ascents}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.ascents}), {\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.inversions]{\emph{\code{inversions}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.inversions}) | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.permutations} \PYG{k+kn}{import} \PYG{n}{Permutation} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,}\PYG{l+m+mi}{0}\PYG{p}{,}\PYG{l+m+mi}{2}\PYG{p}{,}\PYG{l+m+mi}{3}\PYG{p}{,}\PYG{l+m+mi}{4}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{max}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{1} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{min() (sympy.combinatorics.permutations.Permutation method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.min}\pysiglinewithargsret{\bfcode{min}}{}{} | |
The minimum element moved by the permutation. | |
\strong{See also:} | |
{\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.max]{\emph{\code{max}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.max}), {\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.descents]{\emph{\code{descents}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.descents}), {\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.ascents]{\emph{\code{ascents}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.ascents}), {\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.inversions]{\emph{\code{inversions}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.inversions}) | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.permutations} \PYG{k+kn}{import} \PYG{n}{Permutation} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{,}\PYG{l+m+mi}{4}\PYG{p}{,}\PYG{l+m+mi}{3}\PYG{p}{,}\PYG{l+m+mi}{2}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{min}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{2} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{mul\_inv() (sympy.combinatorics.permutations.Permutation method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.mul_inv}\pysiglinewithargsret{\bfcode{mul\_inv}}{\emph{other}}{} | |
other*\textasciitilde{}self, self and other have \_array\_form | |
\end{fulllineitems} | |
\index{next\_lex() (sympy.combinatorics.permutations.Permutation method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.next_lex}\pysiglinewithargsret{\bfcode{next\_lex}}{}{} | |
Returns the next permutation in lexicographical order. | |
If self is the last permutation in lexicographical order | |
it returns None. | |
See {[}4{]} section 2.4. | |
\strong{See also:} | |
{\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.rank]{\emph{\code{rank}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.rank}), {\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.unrank_lex]{\emph{\code{unrank\_lex}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.unrank_lex}) | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.permutations} \PYG{k+kn}{import} \PYG{n}{Permutation} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{]}\PYG{p}{)}\PYG{p}{;} \PYG{n}{p}\PYG{o}{.}\PYG{n}{rank}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{17} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{p}\PYG{o}{.}\PYG{n}{next\PYGZus{}lex}\PYG{p}{(}\PYG{p}{)}\PYG{p}{;} \PYG{n}{p}\PYG{o}{.}\PYG{n}{rank}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{18} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{next\_nonlex() (sympy.combinatorics.permutations.Permutation method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.next_nonlex}\pysiglinewithargsret{\bfcode{next\_nonlex}}{}{} | |
Returns the next permutation in nonlex order {[}3{]}. | |
If self is the last permutation in this order it returns None. | |
\strong{See also:} | |
{\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.rank_nonlex]{\emph{\code{rank\_nonlex}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.rank_nonlex}), {\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.unrank_nonlex]{\emph{\code{unrank\_nonlex}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.unrank_nonlex}) | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.permutations} \PYG{k+kn}{import} \PYG{n}{Permutation} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{o}{.}\PYG{n}{print\PYGZus{}cyclic} \PYG{o}{=} \PYG{n+nb+bp}{False} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{)}\PYG{p}{;} \PYG{n}{p}\PYG{o}{.}\PYG{n}{rank\PYGZus{}nonlex}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{5} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{p}\PYG{o}{.}\PYG{n}{next\PYGZus{}nonlex}\PYG{p}{(}\PYG{p}{)}\PYG{p}{;} \PYG{n}{p} | |
\PYG{g+go}{Permutation([3, 0, 1, 2])} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{rank\PYGZus{}nonlex}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{6} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{next\_trotterjohnson() (sympy.combinatorics.permutations.Permutation method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.next_trotterjohnson}\pysiglinewithargsret{\bfcode{next\_trotterjohnson}}{}{} | |
Returns the next permutation in Trotter-Johnson order. | |
If self is the last permutation it returns None. | |
See {[}4{]} section 2.4. If it is desired to generate all such | |
permutations, they can be generated in order more quickly | |
with the \code{generate\_bell} function. | |
\strong{See also:} | |
{\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.rank_trotterjohnson]{\emph{\code{rank\_trotterjohnson}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.rank_trotterjohnson}), {\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.unrank_trotterjohnson]{\emph{\code{unrank\_trotterjohnson}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.unrank_trotterjohnson}), {\hyperref[modules/utilities/iterables:sympy.utilities.iterables.generate_bell]{\emph{\code{sympy.utilities.iterables.generate\_bell}}}} (\autopageref*{modules/utilities/iterables:sympy.utilities.iterables.generate_bell}) | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.permutations} \PYG{k+kn}{import} \PYG{n}{Permutation} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{o}{.}\PYG{n}{print\PYGZus{}cyclic} \PYG{o}{=} \PYG{n+nb+bp}{False} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{rank\PYGZus{}trotterjohnson}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{4} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{p}\PYG{o}{.}\PYG{n}{next\PYGZus{}trotterjohnson}\PYG{p}{(}\PYG{p}{)}\PYG{p}{;} \PYG{n}{p} | |
\PYG{g+go}{Permutation([0, 3, 2, 1])} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{rank\PYGZus{}trotterjohnson}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{5} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{order() (sympy.combinatorics.permutations.Permutation method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.order}\pysiglinewithargsret{\bfcode{order}}{}{} | |
Computes the order of a permutation. | |
When the permutation is raised to the power of its | |
order it equals the identity permutation. | |
\strong{See also:} | |
\code{identity}, {\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.cardinality]{\emph{\code{cardinality}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.cardinality}), {\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.length]{\emph{\code{length}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.length}), {\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.rank]{\emph{\code{rank}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.rank}), {\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.size]{\emph{\code{size}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.size}) | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.permutations} \PYG{k+kn}{import} \PYG{n}{Permutation} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{o}{.}\PYG{n}{print\PYGZus{}cyclic} \PYG{o}{=} \PYG{n+nb+bp}{False} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{5}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{4}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{order}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{4} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{(}\PYG{n}{p}\PYG{o}{*}\PYG{o}{*}\PYG{p}{(}\PYG{n}{p}\PYG{o}{.}\PYG{n}{order}\PYG{p}{(}\PYG{p}{)}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{Permutation([], size=6)} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{parity() (sympy.combinatorics.permutations.Permutation method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.parity}\pysiglinewithargsret{\bfcode{parity}}{}{} | |
Computes the parity of a permutation. | |
The parity of a permutation reflects the parity of the | |
number of inversions in the permutation, i.e., the | |
number of pairs of x and y such that \code{x \textgreater{} y} but \code{p{[}x{]} \textless{} p{[}y{]}}. | |
\strong{See also:} | |
\code{\_af\_parity} | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.permutations} \PYG{k+kn}{import} \PYG{n}{Permutation} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{,}\PYG{l+m+mi}{2}\PYG{p}{,}\PYG{l+m+mi}{3}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{parity}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{0} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{3}\PYG{p}{,}\PYG{l+m+mi}{2}\PYG{p}{,}\PYG{l+m+mi}{0}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{parity}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{1} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{random() (sympy.combinatorics.permutations.Permutation class method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.random}\pysiglinewithargsret{\strong{classmethod }\bfcode{random}}{\emph{n}}{} | |
Generates a random permutation of length \code{n}. | |
Uses the underlying Python pseudo-random number generator. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.permutations} \PYG{k+kn}{import} \PYG{n}{Permutation} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{o}{.}\PYG{n}{random}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{)} \PYG{o+ow}{in} \PYG{p}{(}\PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{]}\PYG{p}{)}\PYG{p}{,} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{rank() (sympy.combinatorics.permutations.Permutation method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.rank}\pysiglinewithargsret{\bfcode{rank}}{}{} | |
Returns the lexicographic rank of the permutation. | |
\strong{See also:} | |
{\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.next_lex]{\emph{\code{next\_lex}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.next_lex}), {\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.unrank_lex]{\emph{\code{unrank\_lex}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.unrank_lex}), {\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.cardinality]{\emph{\code{cardinality}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.cardinality}), {\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.length]{\emph{\code{length}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.length}), {\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.order]{\emph{\code{order}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.order}), {\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.size]{\emph{\code{size}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.size}) | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.permutations} \PYG{k+kn}{import} \PYG{n}{Permutation} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{rank}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{0} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{rank}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{23} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{rank\_nonlex() (sympy.combinatorics.permutations.Permutation method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.rank_nonlex}\pysiglinewithargsret{\bfcode{rank\_nonlex}}{\emph{inv\_perm=None}}{} | |
This is a linear time ranking algorithm that does not | |
enforce lexicographic order {[}3{]}. | |
\strong{See also:} | |
{\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.next_nonlex]{\emph{\code{next\_nonlex}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.next_nonlex}), {\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.unrank_nonlex]{\emph{\code{unrank\_nonlex}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.unrank_nonlex}) | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.permutations} \PYG{k+kn}{import} \PYG{n}{Permutation} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{,}\PYG{l+m+mi}{2}\PYG{p}{,}\PYG{l+m+mi}{3}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{rank\PYGZus{}nonlex}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{23} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{rank\_trotterjohnson() (sympy.combinatorics.permutations.Permutation method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.rank_trotterjohnson}\pysiglinewithargsret{\bfcode{rank\_trotterjohnson}}{}{} | |
Returns the Trotter Johnson rank, which we get from the minimal | |
change algorithm. See {[}4{]} section 2.4. | |
\strong{See also:} | |
{\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.unrank_trotterjohnson]{\emph{\code{unrank\_trotterjohnson}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.unrank_trotterjohnson}), {\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.next_trotterjohnson]{\emph{\code{next\_trotterjohnson}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.next_trotterjohnson}) | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.permutations} \PYG{k+kn}{import} \PYG{n}{Permutation} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{,}\PYG{l+m+mi}{2}\PYG{p}{,}\PYG{l+m+mi}{3}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{rank\PYGZus{}trotterjohnson}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{0} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,}\PYG{l+m+mi}{2}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{,}\PYG{l+m+mi}{3}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{rank\PYGZus{}trotterjohnson}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{7} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{rmul() (sympy.combinatorics.permutations.Permutation static method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.rmul}\pysiglinewithargsret{\strong{static }\bfcode{rmul}}{\emph{*args}}{} | |
Return product of Permutations {[}a, b, c, ...{]} as the Permutation whose | |
ith value is a(b(c(i))). | |
a, b, c, ... can be Permutation objects or tuples. | |
\paragraph{Notes} | |
All items in the sequence will be parsed by Permutation as | |
necessary as long as the first item is a Permutation: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{o}{.}\PYG{n}{rmul}\PYG{p}{(}\PYG{n}{a}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{)} \PYG{o}{==} \PYG{n}{Permutation}\PYG{o}{.}\PYG{n}{rmul}\PYG{p}{(}\PYG{n}{a}\PYG{p}{,} \PYG{n}{b}\PYG{p}{)} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
The reverse order of arguments will raise a TypeError. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.permutations} \PYG{k+kn}{import} \PYG{n}{\PYGZus{}af\PYGZus{}rmul}\PYG{p}{,} \PYG{n}{Permutation} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{o}{.}\PYG{n}{print\PYGZus{}cyclic} \PYG{o}{=} \PYG{n+nb+bp}{False} | |
\end{Verbatim} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a}\PYG{p}{,} \PYG{n}{b} \PYG{o}{=} \PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{n}{a}\PYG{p}{)}\PYG{p}{;} \PYG{n}{b} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{n}{b}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb}{list}\PYG{p}{(}\PYG{n}{Permutation}\PYG{o}{.}\PYG{n}{rmul}\PYG{p}{(}\PYG{n}{a}\PYG{p}{,} \PYG{n}{b}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{[1, 2, 0]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{[}\PYG{n}{a}\PYG{p}{(}\PYG{n}{b}\PYG{p}{(}\PYG{n}{i}\PYG{p}{)}\PYG{p}{)} \PYG{k}{for} \PYG{n}{i} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{p}{)}\PYG{p}{]} | |
\PYG{g+go}{[1, 2, 0]} | |
\end{Verbatim} | |
This handles the operands in reverse order compared to the \code{*} operator: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{n}{a}\PYG{p}{)}\PYG{p}{;} \PYG{n}{b} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{n}{b}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb}{list}\PYG{p}{(}\PYG{n}{a}\PYG{o}{*}\PYG{n}{b}\PYG{p}{)} | |
\PYG{g+go}{[2, 0, 1]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{p}{[}\PYG{n}{b}\PYG{p}{(}\PYG{n}{a}\PYG{p}{(}\PYG{n}{i}\PYG{p}{)}\PYG{p}{)} \PYG{k}{for} \PYG{n}{i} \PYG{o+ow}{in} \PYG{n+nb}{range}\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{p}{)}\PYG{p}{]} | |
\PYG{g+go}{[2, 0, 1]} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{rmul\_with\_af() (sympy.combinatorics.permutations.Permutation static method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.rmul_with_af}\pysiglinewithargsret{\strong{static }\bfcode{rmul\_with\_af}}{\emph{*args}}{} | |
same as rmul, but the elements of args are Permutation objects | |
which have \_array\_form | |
\end{fulllineitems} | |
\index{runs() (sympy.combinatorics.permutations.Permutation method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.runs}\pysiglinewithargsret{\bfcode{runs}}{}{} | |
Returns the runs of a permutation. | |
An ascending sequence in a permutation is called a run {[}5{]}. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.permutations} \PYG{k+kn}{import} \PYG{n}{Permutation} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{2}\PYG{p}{,}\PYG{l+m+mi}{5}\PYG{p}{,}\PYG{l+m+mi}{7}\PYG{p}{,}\PYG{l+m+mi}{3}\PYG{p}{,}\PYG{l+m+mi}{6}\PYG{p}{,}\PYG{l+m+mi}{0}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{,}\PYG{l+m+mi}{4}\PYG{p}{,}\PYG{l+m+mi}{8}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{runs}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{[[2, 5, 7], [3, 6], [0, 1, 4, 8]]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{q} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,}\PYG{l+m+mi}{3}\PYG{p}{,}\PYG{l+m+mi}{2}\PYG{p}{,}\PYG{l+m+mi}{0}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{q}\PYG{o}{.}\PYG{n}{runs}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{[[1, 3], [2], [0]]} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{signature() (sympy.combinatorics.permutations.Permutation method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.signature}\pysiglinewithargsret{\bfcode{signature}}{}{} | |
Gives the signature of the permutation needed to place the | |
elements of the permutation in canonical order. | |
The signature is calculated as (-1)\textasciicircum{}\textless{}number of inversions\textgreater{} | |
\strong{See also:} | |
{\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.inversions]{\emph{\code{inversions}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.inversions}) | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.permutations} \PYG{k+kn}{import} \PYG{n}{Permutation} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{,}\PYG{l+m+mi}{2}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{inversions}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{0} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{signature}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{q} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,}\PYG{l+m+mi}{2}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{q}\PYG{o}{.}\PYG{n}{inversions}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{1} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{q}\PYG{o}{.}\PYG{n}{signature}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{\PYGZhy{}1} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{size (sympy.combinatorics.permutations.Permutation attribute)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.size}\pysigline{\bfcode{size}} | |
Returns the number of elements in the permutation. | |
\strong{See also:} | |
{\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.cardinality]{\emph{\code{cardinality}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.cardinality}), {\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.length]{\emph{\code{length}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.length}), {\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.order]{\emph{\code{order}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.order}), {\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.rank]{\emph{\code{rank}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.rank}) | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics} \PYG{k+kn}{import} \PYG{n}{Permutation} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{p}{[}\PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{]}\PYG{p}{)}\PYG{o}{.}\PYG{n}{size} | |
\PYG{g+go}{4} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{support() (sympy.combinatorics.permutations.Permutation method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.support}\pysiglinewithargsret{\bfcode{support}}{}{} | |
Return the elements in permutation, P, for which P{[}i{]} != i. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics} \PYG{k+kn}{import} \PYG{n}{Permutation} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{p}{[}\PYG{l+m+mi}{3}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mi}{4}\PYG{p}{]}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{array\PYGZus{}form} | |
\PYG{g+go}{[1, 0, 3, 2, 4]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{support}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{[0, 1, 2, 3]} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{transpositions() (sympy.combinatorics.permutations.Permutation method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.transpositions}\pysiglinewithargsret{\bfcode{transpositions}}{}{} | |
Return the permutation decomposed into a list of transpositions. | |
It is always possible to express a permutation as the product of | |
transpositions, see {[}1{]} | |
\paragraph{References} | |
\begin{enumerate} | |
\item {} | |
\href{http://en.wikipedia.org/wiki/Transposition\_\%28mathematics\%29\#Properties}{http://en.wikipedia.org/wiki/Transposition\_\%28mathematics\%29\#Properties} | |
\end{enumerate} | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.permutations} \PYG{k+kn}{import} \PYG{n}{Permutation} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{]}\PYG{p}{,} \PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+m+mi}{4}\PYG{p}{,} \PYG{l+m+mi}{5}\PYG{p}{,} \PYG{l+m+mi}{6}\PYG{p}{,} \PYG{l+m+mi}{7}\PYG{p}{]}\PYG{p}{]}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{t} \PYG{o}{=} \PYG{n}{p}\PYG{o}{.}\PYG{n}{transpositions}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{t} | |
\PYG{g+go}{[(0, 7), (0, 6), (0, 5), (0, 4), (1, 3), (1, 2)]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k}{print}\PYG{p}{(}\PYG{l+s}{\PYGZsq{}}\PYG{l+s}{\PYGZsq{}}\PYG{o}{.}\PYG{n}{join}\PYG{p}{(}\PYG{n+nb}{str}\PYG{p}{(}\PYG{n}{c}\PYG{p}{)} \PYG{k}{for} \PYG{n}{c} \PYG{o+ow}{in} \PYG{n}{t}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{(0, 7)(0, 6)(0, 5)(0, 4)(1, 3)(1, 2)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{o}{.}\PYG{n}{rmul}\PYG{p}{(}\PYG{o}{*}\PYG{p}{[}\PYG{n}{Permutation}\PYG{p}{(}\PYG{p}{[}\PYG{n}{ti}\PYG{p}{]}\PYG{p}{,} \PYG{n}{size}\PYG{o}{=}\PYG{n}{p}\PYG{o}{.}\PYG{n}{size}\PYG{p}{)} \PYG{k}{for} \PYG{n}{ti} \PYG{o+ow}{in} \PYG{n}{t}\PYG{p}{]}\PYG{p}{)} \PYG{o}{==} \PYG{n}{p} | |
\PYG{g+go}{True} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{unrank\_lex() (sympy.combinatorics.permutations.Permutation class method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.unrank_lex}\pysiglinewithargsret{\strong{classmethod }\bfcode{unrank\_lex}}{\emph{size}, \emph{rank}}{} | |
Lexicographic permutation unranking. | |
\strong{See also:} | |
{\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.rank]{\emph{\code{rank}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.rank}), {\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.next_lex]{\emph{\code{next\_lex}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.next_lex}) | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.permutations} \PYG{k+kn}{import} \PYG{n}{Permutation} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{o}{.}\PYG{n}{print\PYGZus{}cyclic} \PYG{o}{=} \PYG{n+nb+bp}{False} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a} \PYG{o}{=} \PYG{n}{Permutation}\PYG{o}{.}\PYG{n}{unrank\PYGZus{}lex}\PYG{p}{(}\PYG{l+m+mi}{5}\PYG{p}{,} \PYG{l+m+mi}{10}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a}\PYG{o}{.}\PYG{n}{rank}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{10} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a} | |
\PYG{g+go}{Permutation([0, 2, 4, 1, 3])} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{unrank\_nonlex() (sympy.combinatorics.permutations.Permutation class method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.unrank_nonlex}\pysiglinewithargsret{\strong{classmethod }\bfcode{unrank\_nonlex}}{\emph{n}, \emph{r}}{} | |
This is a linear time unranking algorithm that does not | |
respect lexicographic order {[}3{]}. | |
\strong{See also:} | |
{\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.next_nonlex]{\emph{\code{next\_nonlex}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.next_nonlex}), {\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.rank_nonlex]{\emph{\code{rank\_nonlex}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.rank_nonlex}) | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.permutations} \PYG{k+kn}{import} \PYG{n}{Permutation} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{o}{.}\PYG{n}{print\PYGZus{}cyclic} \PYG{o}{=} \PYG{n+nb+bp}{False} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{o}{.}\PYG{n}{unrank\PYGZus{}nonlex}\PYG{p}{(}\PYG{l+m+mi}{4}\PYG{p}{,} \PYG{l+m+mi}{5}\PYG{p}{)} | |
\PYG{g+go}{Permutation([2, 0, 3, 1])} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{o}{.}\PYG{n}{unrank\PYGZus{}nonlex}\PYG{p}{(}\PYG{l+m+mi}{4}\PYG{p}{,} \PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+go}{Permutation([0, 1, 2, 3])} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\index{unrank\_trotterjohnson() (sympy.combinatorics.permutations.Permutation class method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.unrank_trotterjohnson}\pysiglinewithargsret{\strong{classmethod }\bfcode{unrank\_trotterjohnson}}{\emph{size}, \emph{rank}}{} | |
Trotter Johnson permutation unranking. See {[}4{]} section 2.4. | |
\strong{See also:} | |
{\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.rank_trotterjohnson]{\emph{\code{rank\_trotterjohnson}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.rank_trotterjohnson}), {\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.next_trotterjohnson]{\emph{\code{next\_trotterjohnson}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation.next_trotterjohnson}) | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.permutations} \PYG{k+kn}{import} \PYG{n}{Permutation} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{o}{.}\PYG{n}{unrank\PYGZus{}trotterjohnson}\PYG{p}{(}\PYG{l+m+mi}{5}\PYG{p}{,} \PYG{l+m+mi}{10}\PYG{p}{)} | |
\PYG{g+go}{Permutation([0, 3, 1, 2, 4])} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\end{fulllineitems} | |
\index{Cycle (class in sympy.combinatorics.permutations)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.permutations.Cycle}\pysiglinewithargsret{\strong{class }\code{sympy.combinatorics.permutations.}\bfcode{Cycle}}{\emph{*args}}{} | |
Wrapper around dict which provides the functionality of a disjoint cycle. | |
A cycle shows the rule to use to move subsets of elements to obtain | |
a permutation. The Cycle class is more flexible that Permutation in | |
that 1) all elements need not be present in order to investigate how | |
multiple cycles act in sequence and 2) it can contain singletons: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.permutations} \PYG{k+kn}{import} \PYG{n}{Perm}\PYG{p}{,} \PYG{n}{Cycle} | |
\end{Verbatim} | |
A Cycle will automatically parse a cycle given as a tuple on the rhs: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Cycle}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{)} | |
\PYG{g+go}{Cycle(1, 3, 2)} | |
\end{Verbatim} | |
The identity cycle, Cycle(), can be used to start a product: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Cycle}\PYG{p}{(}\PYG{p}{)}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{,}\PYG{l+m+mi}{3}\PYG{p}{)} | |
\PYG{g+go}{Cycle(1, 3, 2)} | |
\end{Verbatim} | |
The array form of a Cycle can be obtained by calling the list | |
method (or passing it to the list function) and all elements from | |
0 will be shown: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a} \PYG{o}{=} \PYG{n}{Cycle}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{a}\PYG{o}{.}\PYG{n}{list}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{[0, 2, 1]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb}{list}\PYG{p}{(}\PYG{n}{a}\PYG{p}{)} | |
\PYG{g+go}{[0, 2, 1]} | |
\end{Verbatim} | |
If a larger (or smaller) range is desired use the list method and | |
provide the desired size -- but the Cycle cannot be truncated to | |
a size smaller than the largest element that is out of place: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{b} \PYG{o}{=} \PYG{n}{Cycle}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{,}\PYG{l+m+mi}{4}\PYG{p}{)}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{,}\PYG{l+m+mi}{4}\PYG{p}{)}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,}\PYG{l+m+mi}{3}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{b}\PYG{o}{.}\PYG{n}{list}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{[0, 2, 1, 3, 4]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{b}\PYG{o}{.}\PYG{n}{list}\PYG{p}{(}\PYG{n}{b}\PYG{o}{.}\PYG{n}{size} \PYG{o}{+} \PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+go}{[0, 2, 1, 3, 4, 5]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{b}\PYG{o}{.}\PYG{n}{list}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+go}{[0, 2, 1]} | |
\end{Verbatim} | |
Singletons are not shown when printing with one exception: the largest | |
element is always shown -- as a singleton if necessary: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Cycle}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{4}\PYG{p}{,} \PYG{l+m+mi}{10}\PYG{p}{)}\PYG{p}{(}\PYG{l+m+mi}{4}\PYG{p}{,} \PYG{l+m+mi}{5}\PYG{p}{)} | |
\PYG{g+go}{Cycle(1, 5, 4, 10)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Cycle}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{(}\PYG{l+m+mi}{4}\PYG{p}{)}\PYG{p}{(}\PYG{l+m+mi}{5}\PYG{p}{)}\PYG{p}{(}\PYG{l+m+mi}{10}\PYG{p}{)} | |
\PYG{g+go}{Cycle(1, 2)(10)} | |
\end{Verbatim} | |
The array form can be used to instantiate a Permutation so other | |
properties of the permutation can be investigated: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Perm}\PYG{p}{(}\PYG{n}{Cycle}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,}\PYG{l+m+mi}{2}\PYG{p}{)}\PYG{p}{(}\PYG{l+m+mi}{3}\PYG{p}{,}\PYG{l+m+mi}{4}\PYG{p}{)}\PYG{o}{.}\PYG{n}{list}\PYG{p}{(}\PYG{p}{)}\PYG{p}{)}\PYG{o}{.}\PYG{n}{transpositions}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{[(1, 2), (3, 4)]} | |
\end{Verbatim} | |
\strong{See also:} | |
{\hyperref[modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation]{\emph{\code{Permutation}}}} (\autopageref*{modules/combinatorics/permutations:sympy.combinatorics.permutations.Permutation}) | |
\paragraph{Notes} | |
The underlying structure of the Cycle is a dictionary and although | |
the \_\_iter\_\_ method has been redefined to give the array form of the | |
cycle, the underlying dictionary items are still available with the | |
such methods as items(): | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n+nb}{list}\PYG{p}{(}\PYG{n}{Cycle}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{)}\PYG{o}{.}\PYG{n}{items}\PYG{p}{(}\PYG{p}{)}\PYG{p}{)} | |
\PYG{g+go}{[(1, 2), (2, 1)]} | |
\end{Verbatim} | |
\index{list() (sympy.combinatorics.permutations.Cycle method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.permutations.Cycle.list}\pysiglinewithargsret{\bfcode{list}}{\emph{size=None}}{} | |
Return the cycles as an explicit list starting from 0 up | |
to the greater of the largest value in the cycles and size. | |
Truncation of trailing unmoved items will occur when size | |
is less than the maximum element in the cycle; if this is | |
desired, setting \code{size=-1} will guarantee such trimming. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.permutations} \PYG{k+kn}{import} \PYG{n}{Cycle} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.permutations} \PYG{k+kn}{import} \PYG{n}{Permutation} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Permutation}\PYG{o}{.}\PYG{n}{print\PYGZus{}cyclic} \PYG{o}{=} \PYG{n+nb+bp}{False} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p} \PYG{o}{=} \PYG{n}{Cycle}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{3}\PYG{p}{)}\PYG{p}{(}\PYG{l+m+mi}{4}\PYG{p}{,} \PYG{l+m+mi}{5}\PYG{p}{)} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{list}\PYG{p}{(}\PYG{p}{)} | |
\PYG{g+go}{[0, 1, 3, 2, 5, 4]} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{p}\PYG{o}{.}\PYG{n}{list}\PYG{p}{(}\PYG{l+m+mi}{10}\PYG{p}{)} | |
\PYG{g+go}{[0, 1, 3, 2, 5, 4, 6, 7, 8, 9]} | |
\end{Verbatim} | |
Passing a length too small will trim trailing, unchanged elements | |
in the permutation: | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{n}{Cycle}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{4}\PYG{p}{)}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+m+mi}{4}\PYG{p}{)}\PYG{o}{.}\PYG{n}{list}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{1}\PYG{p}{)} | |
\PYG{g+go}{[0, 2, 1]} | |
\end{Verbatim} | |
\end{fulllineitems} | |
\end{fulllineitems} | |
\paragraph{Generators} | |
\label{modules/combinatorics/permutations:combinatorics-generators}\label{modules/combinatorics/permutations:generators}\label{modules/combinatorics/permutations:module-sympy.combinatorics.generators}\index{sympy.combinatorics.generators (module)}\index{symmetric() (sympy.combinatorics.generators method)} | |
\begin{fulllineitems} | |
\phantomsection\label{modules/combinatorics/permutations:sympy.combinatorics.generators.symmetric}\pysiglinewithargsret{\code{generators.}\bfcode{symmetric}}{\emph{n}}{} | |
Generates the symmetric group of order n, Sn. | |
\paragraph{Examples} | |
\begin{Verbatim}[commandchars=\\\{\}] | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZgt{} }\PYG{k+kn}{from} \PYG{n+nn}{sympy.combinatorics.permutations} \PYG{k+kn}{import} \PYG{n}{Permutation} | |
\PYG{g+gp}{\PYGZgt{}\PYGZgt{}\PYGZ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment