-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgotchas.html
1038 lines (1025 loc) · 105 KB
/
gotchas.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Gotchas and Pitfalls — SymPy 1.8 documentation</title>
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="_static/default.css" type="text/css" />
<link rel="stylesheet" type="text/css" href="_static/graphviz.css" />
<link rel="stylesheet" type="text/css" href="https://live.sympy.org/static/live-core.css" />
<link rel="stylesheet" type="text/css" href="https://live.sympy.org/static/live-autocomplete.css" />
<link rel="stylesheet" type="text/css" href="https://live.sympy.org/static/live-sphinx.css" />
<script id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></script>
<script src="_static/jquery.js"></script>
<script src="_static/underscore.js"></script>
<script src="_static/doctools.js"></script>
<script src="_static/language_data.js"></script>
<script src="https://live.sympy.org/static/utilities.js"></script>
<script src="https://live.sympy.org/static/external/classy.js"></script>
<script src="https://live.sympy.org/static/live-core.js"></script>
<script src="https://live.sympy.org/static/live-autocomplete.js"></script>
<script src="https://live.sympy.org/static/live-sphinx.js"></script>
<script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS_HTML-full"></script>
<script type="text/x-mathjax-config">MathJax.Hub.Config({"tex2jax": {"inlineMath": [["\\(", "\\)"]], "displayMath": [["\\[", "\\]"]]}})</script>
<link rel="shortcut icon" href="_static/sympy-notailtext-favicon.ico"/>
<link href="https://docs.sympy.org/latest/gotchas.html" rel="canonical" />
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="SymPy Modules Reference" href="modules/index.html" />
<link rel="prev" title="Advanced Expression Manipulation" href="tutorial/manipulation.html" />
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="modules/index.html" title="SymPy Modules Reference"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="tutorial/manipulation.html" title="Advanced Expression Manipulation"
accesskey="P">previous</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">SymPy 1.8 documentation</a> »</li>
<li class="nav-item nav-item-this"><a href="">Gotchas and Pitfalls</a></li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="gotchas-and-pitfalls">
<span id="gotchas"></span><h1>Gotchas and Pitfalls<a class="headerlink" href="#gotchas-and-pitfalls" title="Permalink to this headline">¶</a></h1>
<div class="section" id="introduction">
<h2>Introduction<a class="headerlink" href="#introduction" title="Permalink to this headline">¶</a></h2>
<p>SymPy runs under the <a class="reference external" href="https://www.python.org/">Python Programming Language</a>, 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 <a class="reference external" href="https://github.com/sympy/sympy/wiki/Faq">FAQ</a>, the <a class="reference internal" href="tutorial/index.html#tutorial"><span class="std std-ref">Tutorial</span></a>, the
remainder of the SymPy Docs, and the <a class="reference external" href="https://docs.python.org/3/tutorial/">official Python Tutorial</a>.</p>
<p>If you are already familiar with C or Java, you might also want to look
at this <a class="reference external" href="https://nerdparadise.com/programming/python4minutes/">4 minute Python tutorial</a>.</p>
<p>Ignore <code class="docutils literal notranslate"><span class="pre">#doctest:</span> <span class="pre">+SKIP</span></code> in the examples. That has to do with
internal testing of the examples.</p>
</div>
<div class="section" id="equals-signs">
<span id="id1"></span><h2>Equals Signs (=)<a class="headerlink" href="#equals-signs" title="Permalink to this headline">¶</a></h2>
<div class="section" id="single-equals-sign">
<h3>Single Equals Sign<a class="headerlink" href="#single-equals-sign" title="Permalink to this headline">¶</a></h3>
<p>The equals sign (<code class="docutils literal notranslate"><span class="pre">=</span></code>) is the assignment operator, not equality. If
you want to do <span class="math notranslate nohighlight">\(x = y\)</span>, use <code class="docutils literal notranslate"><span class="pre">Eq(x,</span> <span class="pre">y)</span></code> for equality.
Alternatively, all expressions are assumed to equal zero, so you can
just subtract one side and use <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">-</span> <span class="pre">y</span></code>.</p>
<p>The proper use of the equals sign is to assign expressions to variables.</p>
<p>For example:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span>
<span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="n">x</span> <span class="o">-</span> <span class="n">y</span>
<span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="go">x - y</span>
</pre></div>
</div>
</div>
<div class="section" id="double-equals-signs">
<h3>Double Equals Signs<a class="headerlink" href="#double-equals-signs" title="Permalink to this headline">¶</a></h3>
<p>Double equals signs (<code class="docutils literal notranslate"><span class="pre">==</span></code>) are used to test equality. However, this
tests expressions exactly, not symbolically. For example:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="p">(</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span> <span class="o">==</span> <span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">2</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span>
<span class="go">False</span>
<span class="gp">>>> </span><span class="p">(</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span> <span class="o">==</span> <span class="p">(</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span>
<span class="go">True</span>
</pre></div>
</div>
<p>If you want to test for symbolic equality, one way is to subtract one
expression from the other and run it through functions like
<a class="reference internal" href="modules/core.html#sympy.core.function.expand" title="sympy.core.function.expand"><code class="xref py py-func docutils literal notranslate"><span class="pre">expand()</span></code></a>, <a class="reference internal" href="modules/simplify/simplify.html#sympy.simplify.simplify.simplify" title="sympy.simplify.simplify.simplify"><code class="xref py py-func docutils literal notranslate"><span class="pre">simplify()</span></code></a>, and <a class="reference internal" href="modules/simplify/simplify.html#sympy.simplify.trigsimp.trigsimp" title="sympy.simplify.trigsimp.trigsimp"><code class="xref py py-func docutils literal notranslate"><span class="pre">trigsimp()</span></code></a> and see if the
equation reduces to 0.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">simplify</span><span class="p">,</span> <span class="n">cos</span><span class="p">,</span> <span class="n">sin</span><span class="p">,</span> <span class="n">expand</span>
<span class="gp">>>> </span><span class="n">simplify</span><span class="p">((</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">2</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span><span class="p">))</span>
<span class="go">0</span>
<span class="gp">>>> </span><span class="n">eq</span> <span class="o">=</span> <span class="n">sin</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">x</span><span class="p">)</span> <span class="o">-</span> <span class="mi">2</span><span class="o">*</span><span class="n">sin</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">*</span><span class="n">cos</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">simplify</span><span class="p">(</span><span class="n">eq</span><span class="p">)</span>
<span class="go">0</span>
<span class="gp">>>> </span><span class="n">expand</span><span class="p">(</span><span class="n">eq</span><span class="p">,</span> <span class="n">trig</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go">0</span>
</pre></div>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>See also <a class="reference external" href="https://github.com/sympy/sympy/wiki/Faq">Why does SymPy say that two equal expressions are unequal?</a> in the FAQ.</p>
</div>
</div>
</div>
<div class="section" id="variables">
<h2>Variables<a class="headerlink" href="#variables" title="Permalink to this headline">¶</a></h2>
<div class="section" id="variables-assignment-does-not-create-a-relation-between-expressions">
<h3>Variables Assignment does not Create a Relation Between Expressions<a class="headerlink" href="#variables-assignment-does-not-create-a-relation-between-expressions" title="Permalink to this headline">¶</a></h3>
<p>When you use <code class="docutils literal notranslate"><span class="pre">=</span></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:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">Symbol</span>
<span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">'a'</span><span class="p">)</span> <span class="c1"># Symbol, `a`, stored as variable "a"</span>
<span class="gp">>>> </span><span class="n">b</span> <span class="o">=</span> <span class="n">a</span> <span class="o">+</span> <span class="mi">1</span> <span class="c1"># an expression involving `a` stored as variable "b"</span>
<span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>
<span class="go">a + 1</span>
<span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="mi">4</span> <span class="c1"># "a" now points to literal integer 4, not Symbol('a')</span>
<span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="go">4</span>
<span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="n">b</span><span class="p">)</span> <span class="c1"># "b" is still pointing at the expression involving `a`</span>
<span class="go">a + 1</span>
</pre></div>
</div>
<p>Changing quantity <code class="docutils literal notranslate"><span class="pre">a</span></code> does not change <code class="docutils literal notranslate"><span class="pre">b</span></code>; 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.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">var</span>
<span class="gp">>>> </span><span class="n">r</span><span class="p">,</span> <span class="n">t</span><span class="p">,</span> <span class="n">d</span> <span class="o">=</span> <span class="n">var</span><span class="p">(</span><span class="s1">'rate time short_life'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">r</span><span class="o">*</span><span class="n">t</span>
<span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>
<span class="go">rate*time</span>
<span class="gp">>>> </span><span class="n">r</span> <span class="o">=</span> <span class="mi">80</span>
<span class="gp">>>> </span><span class="n">t</span> <span class="o">=</span> <span class="mi">2</span>
<span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="n">d</span><span class="p">)</span> <span class="c1"># We haven't changed d, only r and t</span>
<span class="go">rate*time</span>
<span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">r</span><span class="o">*</span><span class="n">t</span>
<span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="n">d</span><span class="p">)</span> <span class="c1"># Now d is using the current values of r and t</span>
<span class="go">160</span>
</pre></div>
</div>
<p>If you need variables that have dependence on each other, you can define
functions. Use the <code class="docutils literal notranslate"><span class="pre">def</span></code> operator. Indent the body of the function.
See the Python docs for more information on defining functions.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">c</span><span class="p">,</span> <span class="n">d</span> <span class="o">=</span> <span class="n">var</span><span class="p">(</span><span class="s1">'c d'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="n">c</span><span class="p">)</span>
<span class="go">c</span>
<span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>
<span class="go">d</span>
<span class="gp">>>> </span><span class="k">def</span> <span class="nf">ctimesd</span><span class="p">():</span>
<span class="gp">... </span> <span class="sd">"""</span>
<span class="gp">... </span><span class="sd"> This function returns whatever c is times whatever d is.</span>
<span class="gp">... </span><span class="sd"> """</span>
<span class="gp">... </span> <span class="k">return</span> <span class="n">c</span><span class="o">*</span><span class="n">d</span>
<span class="gp">...</span>
<span class="gp">>>> </span><span class="n">ctimesd</span><span class="p">()</span>
<span class="go">c*d</span>
<span class="gp">>>> </span><span class="n">c</span> <span class="o">=</span> <span class="mi">2</span>
<span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="n">c</span><span class="p">)</span>
<span class="go">2</span>
<span class="gp">>>> </span><span class="n">ctimesd</span><span class="p">()</span>
<span class="go">2*d</span>
</pre></div>
</div>
<p>If you define a circular relationship, you will get a
<code class="docutils literal notranslate"><span class="pre">RuntimeError</span></code>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">def</span> <span class="nf">a</span><span class="p">():</span>
<span class="gp">... </span> <span class="k">return</span> <span class="n">b</span><span class="p">()</span>
<span class="gp">...</span>
<span class="gp">>>> </span><span class="k">def</span> <span class="nf">b</span><span class="p">():</span>
<span class="gp">... </span> <span class="k">return</span> <span class="n">a</span><span class="p">()</span>
<span class="gp">...</span>
<span class="gp">>>> </span><span class="n">a</span><span class="p">()</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="gr"> File "...", line ..., in ...</span>
<span class="gr"> compileflags, 1) in test.globs</span>
<span class="gr"> File "<...>", line 1, in <module></span>
<span class="gr"> a()</span>
<span class="gr"> File "<...>", line 2, in a</span>
<span class="gr"> return b()</span>
<span class="gr"> File "<...>", line 2, in b</span>
<span class="gr"> return a()</span>
<span class="gr"> File "<...>", line 2, in a</span>
<span class="gr"> return b()</span>
<span class="gr">...</span>
<span class="gr">RuntimeError</span>: <span class="n">maximum recursion depth exceeded</span>
</pre></div>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>See also <a class="reference external" href="https://github.com/sympy/sympy/wiki/Faq">Why doesn’t changing one variable change another that depends on it?</a> in the FAQ.</p>
</div>
</div>
<div class="section" id="symbols">
<span id="id2"></span><h3>Symbols<a class="headerlink" href="#symbols" title="Permalink to this headline">¶</a></h3>
<p>Symbols are variables, and like all other variables, they need to be
assigned before you can use them. For example:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">sympy</span>
<span class="gp">>>> </span><span class="n">z</span><span class="o">**</span><span class="mi">2</span> <span class="c1"># z is not defined yet </span>
<span class="gt">Traceback (most recent call last):</span>
File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>, in <span class="n"><module></span>
<span class="gr">NameError</span>: <span class="n">name 'z' is not defined</span>
<span class="gp">>>> </span><span class="n">sympy</span><span class="o">.</span><span class="n">var</span><span class="p">(</span><span class="s1">'z'</span><span class="p">)</span> <span class="c1"># This is the easiest way to define z as a standard symbol</span>
<span class="go">z</span>
<span class="gp">>>> </span><span class="n">z</span><span class="o">**</span><span class="mi">2</span>
<span class="go">z**2</span>
</pre></div>
</div>
<p>If you use <strong class="command">isympy</strong>, it runs the following commands for you,
giving you some default Symbols and Functions.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">division</span>
<span class="gp">>>> </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="o">*</span>
<span class="gp">>>> </span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">,</span> <span class="n">t</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">'x y z t'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">k</span><span class="p">,</span> <span class="n">m</span><span class="p">,</span> <span class="n">n</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">'k m n'</span><span class="p">,</span> <span class="n">integer</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">f</span><span class="p">,</span> <span class="n">g</span><span class="p">,</span> <span class="n">h</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">'f g h'</span><span class="p">,</span> <span class="bp">cls</span><span class="o">=</span><span class="n">Function</span><span class="p">)</span>
</pre></div>
</div>
<p>You can also import common symbol names from <a class="reference internal" href="modules/abc.html#module-sympy.abc" title="sympy.abc"><code class="xref py py-mod docutils literal notranslate"><span class="pre">sympy.abc</span></code></a>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">w</span>
<span class="gp">>>> </span><span class="n">w</span>
<span class="go">w</span>
<span class="gp">>>> </span><span class="kn">import</span> <span class="nn">sympy</span>
<span class="gp">>>> </span><span class="nb">dir</span><span class="p">(</span><span class="n">sympy</span><span class="o">.</span><span class="n">abc</span><span class="p">)</span>
<span class="go">['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',</span>
<span class="go">'P', 'Q', 'R', 'S', 'Symbol', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',</span>
<span class="go">'__builtins__', '__doc__', '__file__', '__name__', '__package__', '_greek',</span>
<span class="go">'_latin', 'a', 'alpha', 'b', 'beta', 'c', 'chi', 'd', 'delta', 'e',</span>
<span class="go">'epsilon', 'eta', 'f', 'g', 'gamma', 'h', 'i', 'iota', 'j', 'k', 'kappa',</span>
<span class="go">'l', 'm', 'mu', 'n', 'nu', 'o', 'omega', 'omicron', 'p', 'phi', 'pi',</span>
<span class="go">'psi', 'q', 'r', 'rho', 's', 'sigma', 't', 'tau', 'theta', 'u', 'upsilon',</span>
<span class="go">'v', 'w', 'x', 'xi', 'y', 'z', 'zeta']</span>
</pre></div>
</div>
<p>If you want control over the assumptions of the variables, use
<a class="reference internal" href="modules/core.html#sympy.core.symbol.Symbol" title="sympy.core.symbol.Symbol"><code class="xref py py-class docutils literal notranslate"><span class="pre">Symbol</span></code></a> and <a class="reference internal" href="modules/core.html#sympy.core.symbol.symbols" title="sympy.core.symbol.symbols"><code class="xref py py-func docutils literal notranslate"><span class="pre">symbols()</span></code></a>. See <a class="reference internal" href="#keyword-arguments"><span class="std std-ref">Keyword
Arguments</span></a> below.</p>
<p>Lastly, it is recommended that you not use <a class="reference internal" href="modules/core.html#sympy.core.numbers.ImaginaryUnit" title="sympy.core.numbers.ImaginaryUnit"><code class="xref py py-obj docutils literal notranslate"><span class="pre">I</span></code></a>, <a class="reference internal" href="modules/stats.html#sympy.stats.E" title="sympy.stats.E"><code class="xref py py-obj docutils literal notranslate"><span class="pre">E</span></code></a>, <a class="reference internal" href="modules/physics/quantum/gate.html#sympy.physics.quantum.gate.S" title="sympy.physics.quantum.gate.S"><code class="xref py py-obj docutils literal notranslate"><span class="pre">S</span></code></a>, <a class="reference internal" href="modules/core.html#sympy.core.evalf.N" title="sympy.core.evalf.N"><code class="xref py py-obj docutils literal notranslate"><span class="pre">N</span></code></a>, <code class="docutils literal notranslate"><span class="pre">C</span></code>, <a class="reference internal" href="modules/series/series.html#sympy.series.order.Order" title="sympy.series.order.Order"><code class="xref py py-obj docutils literal notranslate"><span class="pre">O</span></code></a>, or <a class="reference internal" href="modules/assumptions/ask.html#sympy.assumptions.ask.AssumptionKeys" title="sympy.assumptions.ask.AssumptionKeys"><code class="xref py py-obj docutils literal notranslate"><span class="pre">Q</span></code></a> for variable or symbol names, as those
are used for the imaginary unit (<span class="math notranslate nohighlight">\(i\)</span>), the base of the natural logarithm
(<span class="math notranslate nohighlight">\(e\)</span>), the <a class="reference internal" href="modules/core.html#sympy.core.sympify.sympify" title="sympy.core.sympify.sympify"><code class="xref py py-func docutils literal notranslate"><span class="pre">sympify()</span></code></a> function (see <a class="reference internal" href="#symbolic-expressions"><span class="std std-ref">Symbolic
Expressions</span></a> below), numeric evaluation (<a class="reference internal" href="modules/core.html#sympy.core.evalf.N" title="sympy.core.evalf.N"><code class="xref py py-func docutils literal notranslate"><span class="pre">N()</span></code></a> is
equivalent to <a class="reference internal" href="modules/evalf.html#evalf-label"><span class="std std-ref">evalf()</span></a> ), the <a class="reference external" href="https://en.wikipedia.org/wiki/Big_O_notation">big O</a> order symbol (as in
<span class="math notranslate nohighlight">\(O(n\log{n})\)</span>), and the assumptions object that holds a list of
supported ask keys (such as <code class="docutils literal notranslate"><span class="pre">Q.real</span></code>), respectively. You can use the
mnemonic <code class="docutils literal notranslate"><span class="pre">OSINEQ</span></code> 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.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">cos</span><span class="p">(</span><span class="n">pi</span><span class="p">)</span> <span class="c1"># cos and pi are a built-in sympy names.</span>
<span class="go">-1</span>
<span class="gp">>>> </span><span class="n">pi</span> <span class="o">=</span> <span class="mi">3</span> <span class="c1"># Notice that there is no warning for overriding pi.</span>
<span class="gp">>>> </span><span class="n">cos</span><span class="p">(</span><span class="n">pi</span><span class="p">)</span>
<span class="go">cos(3)</span>
<span class="gp">>>> </span><span class="k">def</span> <span class="nf">cos</span><span class="p">(</span><span class="n">x</span><span class="p">):</span> <span class="c1"># No warning for overriding built-in functions either.</span>
<span class="gp">... </span> <span class="k">return</span> <span class="mi">5</span><span class="o">*</span><span class="n">x</span>
<span class="gp">...</span>
<span class="gp">>>> </span><span class="n">cos</span><span class="p">(</span><span class="n">pi</span><span class="p">)</span>
<span class="go">15</span>
<span class="gp">>>> </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">cos</span> <span class="c1"># reimport to restore normal behavior</span>
</pre></div>
</div>
<p>To get a full list of all default names in SymPy do:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">sympy</span>
<span class="gp">>>> </span><span class="nb">dir</span><span class="p">(</span><span class="n">sympy</span><span class="p">)</span>
<span class="go"># A big list of all default sympy names and functions follows.</span>
<span class="go"># Ignore everything that starts and ends with __.</span>
</pre></div>
</div>
<p>If you have <a class="reference external" href="https://ipython.org/">IPython</a> installed and
use <strong class="command">isympy</strong>, you can also press the TAB key to get a list of
all built-in names and to autocomplete. Also, see <a class="reference external" href="https://kogs-www.informatik.uni-hamburg.de/~meine/python_tricks">this page</a> for a
trick for getting tab completion in the regular Python console.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>See also <a class="reference external" href="https://github.com/sympy/sympy/wiki/Faq">What is the best way to create symbols?</a> in the FAQ.</p>
</div>
</div>
<div class="section" id="functions">
<span id="calling-functions"></span><h3>Functions<a class="headerlink" href="#functions" title="Permalink to this headline">¶</a></h3>
<p>A function like <code class="docutils literal notranslate"><span class="pre">f(x)</span></code> can be created by defining the Function
and the variable:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">Function</span>
<span class="gp">>>> </span><span class="n">f</span> <span class="o">=</span> <span class="n">Function</span><span class="p">(</span><span class="s1">'f'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">x</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">'x'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">f</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="go">f(x)</span>
</pre></div>
</div>
<p>If you assign <code class="docutils literal notranslate"><span class="pre">f(x)</span></code> to a Python variable <span class="math notranslate nohighlight">\(f\)</span> you will lose your
ability to copy and paste that function or to create a function
with a different argument: <code class="docutils literal notranslate"><span class="pre">Function('f')</span></code> is callable, but
<code class="docutils literal notranslate"><span class="pre">Function('f')(x)</span></code> is not:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">f1</span> <span class="o">=</span> <span class="n">Function</span><span class="p">(</span><span class="s1">'f1'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">f2</span> <span class="o">=</span> <span class="n">Function</span><span class="p">(</span><span class="s1">'f2'</span><span class="p">)(</span><span class="s1">'x'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">f1</span>
<span class="go">f1</span>
<span class="gp">>>> </span><span class="n">f2</span>
<span class="go">f2(x)</span>
<span class="gp">>>> </span><span class="n">f1</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="go">f1(1)</span>
<span class="gp">>>> </span><span class="n">f2</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">TypeError</span>: <span class="n">'f2' object is not callable</span>
<span class="gp">>>> </span><span class="n">f2</span><span class="o">.</span><span class="n">subs</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="go">f2(1)</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="symbolic-expressions">
<span id="id3"></span><h2>Symbolic Expressions<a class="headerlink" href="#symbolic-expressions" title="Permalink to this headline">¶</a></h2>
<div class="section" id="python-numbers-vs-sympy-numbers">
<span id="python-vs-sympy-numbers"></span><h3>Python numbers vs. SymPy Numbers<a class="headerlink" href="#python-numbers-vs-sympy-numbers" title="Permalink to this headline">¶</a></h3>
<p>SymPy uses its own classes for integers, rational numbers, and floating
point numbers instead of the default Python <code class="docutils literal notranslate"><span class="pre">int</span></code> and <code class="docutils literal notranslate"><span class="pre">float</span></code>
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 <a class="reference internal" href="modules/core.html#sympy.core.sympify.sympify" title="sympy.core.sympify.sympify"><code class="xref py py-func docutils literal notranslate"><span class="pre">sympify()</span></code></a> function, or just
<a class="reference internal" href="modules/physics/quantum/gate.html#sympy.physics.quantum.gate.S" title="sympy.physics.quantum.gate.S"><code class="xref py py-obj docutils literal notranslate"><span class="pre">S</span></code></a>, to ensure that something is a SymPy expression.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="mf">6.2</span> <span class="c1"># Python float. Notice the floating point accuracy problems.</span>
<span class="go">6.2000000000000002</span>
<span class="gp">>>> </span><span class="nb">type</span><span class="p">(</span><span class="mf">6.2</span><span class="p">)</span> <span class="c1"># <type 'float'> in Python 2.x, <class 'float'> in Py3k</span>
<span class="go"><... 'float'></span>
<span class="gp">>>> </span><span class="n">S</span><span class="p">(</span><span class="mf">6.2</span><span class="p">)</span> <span class="c1"># SymPy Float has no such problems because of arbitrary precision.</span>
<span class="go">6.20000000000000</span>
<span class="gp">>>> </span><span class="nb">type</span><span class="p">(</span><span class="n">S</span><span class="p">(</span><span class="mf">6.2</span><span class="p">))</span>
<span class="go"><class 'sympy.core.numbers.Float'></span>
</pre></div>
</div>
<p>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 class="docutils literal notranslate"><span class="pre"><number>/<number></span></code> inside of a SymPy expression, Python will
evaluate the two numbers before SymPy has a chance to get
to them. The solution is to <a class="reference internal" href="modules/core.html#sympy.core.sympify.sympify" title="sympy.core.sympify.sympify"><code class="xref py py-func docutils literal notranslate"><span class="pre">sympify()</span></code></a> one of the numbers, or use
<a class="reference internal" href="modules/core.html#sympy.core.numbers.Rational" title="sympy.core.numbers.Rational"><code class="xref py py-obj docutils literal notranslate"><span class="pre">Rational</span></code></a>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">x</span><span class="o">**</span><span class="p">(</span><span class="mi">1</span><span class="o">/</span><span class="mi">2</span><span class="p">)</span> <span class="c1"># evaluates to x**0 or x**0.5</span>
<span class="go">x**0.5</span>
<span class="gp">>>> </span><span class="n">x</span><span class="o">**</span><span class="p">(</span><span class="n">S</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">/</span><span class="mi">2</span><span class="p">)</span> <span class="c1"># sympyify one of the ints</span>
<span class="go">sqrt(x)</span>
<span class="gp">>>> </span><span class="n">x</span><span class="o">**</span><span class="n">Rational</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span> <span class="c1"># use the Rational class</span>
<span class="go">sqrt(x)</span>
</pre></div>
</div>
<p>With a power of <code class="docutils literal notranslate"><span class="pre">1/2</span></code> you can also use <code class="docutils literal notranslate"><span class="pre">sqrt</span></code> shorthand:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">sqrt</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">==</span> <span class="n">x</span><span class="o">**</span><span class="n">Rational</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="go">True</span>
</pre></div>
</div>
<p>If the two integers are not directly separated by a division sign then
you don’t have to worry about this problem:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">x</span><span class="o">**</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">x</span><span class="o">/</span><span class="mi">3</span><span class="p">)</span>
<span class="go">x**(2*x/3)</span>
</pre></div>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>A common mistake is copying an expression that is printed and
reusing it. If the expression has a <a class="reference internal" href="modules/core.html#sympy.core.numbers.Rational" title="sympy.core.numbers.Rational"><code class="xref py py-obj docutils literal notranslate"><span class="pre">Rational</span></code></a> (i.e.,
<code class="docutils literal notranslate"><span class="pre"><number>/<number></span></code>) in it, you will not get the same result,
obtaining the Python result for the division rather than a SymPy
Rational.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">x</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">'x'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="n">solve</span><span class="p">(</span><span class="mi">7</span><span class="o">*</span><span class="n">x</span> <span class="o">-</span><span class="mi">22</span><span class="p">,</span> <span class="n">x</span><span class="p">))</span>
<span class="go">[22/7]</span>
<span class="gp">>>> </span><span class="mi">22</span><span class="o">/</span><span class="mi">7</span> <span class="c1"># If we just copy and paste we get int 3 or a float</span>
<span class="go">3.142857142857143</span>
<span class="gp">>>> </span><span class="c1"># One solution is to just assign the expression to a variable</span>
<span class="gp">>>> </span><span class="c1"># if we need to use it again.</span>
<span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="n">solve</span><span class="p">(</span><span class="mi">7</span><span class="o">*</span><span class="n">x</span> <span class="o">-</span> <span class="mi">22</span><span class="p">,</span> <span class="n">x</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
<span class="gp">>>> </span><span class="n">a</span>
<span class="go">22/7</span>
</pre></div>
</div>
<p>The other solution is to put quotes around the expression
and run it through S() (i.e., sympify it):</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">S</span><span class="p">(</span><span class="s2">"22/7"</span><span class="p">)</span>
<span class="go">22/7</span>
</pre></div>
</div>
</div>
<p>Also, if you do not use <strong class="command">isympy</strong>, you could use <code class="docutils literal notranslate"><span class="pre">from</span>
<span class="pre">__future__</span> <span class="pre">import</span> <span class="pre">division</span></code> to prevent the <code class="docutils literal notranslate"><span class="pre">/</span></code> sign from performing
<a class="reference external" href="https://en.wikipedia.org/wiki/Integer_division">integer division</a>.</p>
<blockquote>
<div><div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">division</span>
<span class="gp">>>> </span><span class="mi">1</span><span class="o">/</span><span class="mi">2</span> <span class="c1"># With division imported it evaluates to a python float</span>
<span class="go">0.5</span>
<span class="gp">>>> </span><span class="mi">1</span><span class="o">//</span><span class="mi">2</span> <span class="c1"># You can still achieve integer division with //</span>
<span class="go">0</span>
</pre></div>
</div>
<p>But be careful: you will now receive floats where you might have desired
a Rational:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">x</span><span class="o">**</span><span class="p">(</span><span class="mi">1</span><span class="o">/</span><span class="mi">2</span><span class="p">)</span>
<span class="go">x**0.5</span>
</pre></div>
</div>
</div></blockquote>
<p><a class="reference internal" href="modules/core.html#sympy.core.numbers.Rational" title="sympy.core.numbers.Rational"><code class="xref py py-obj docutils literal notranslate"><span class="pre">Rational</span></code></a> 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 class="docutils literal notranslate"><span class="pre">/</span></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.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">Rational</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="n">x</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">TypeError</span>: <span class="n">invalid input: x</span>
<span class="gp">>>> </span><span class="mi">2</span><span class="o">/</span><span class="n">x</span>
<span class="go">2/x</span>
</pre></div>
</div>
</div>
<div class="section" id="evaluating-expressions-with-floats-and-rationals">
<h3>Evaluating Expressions with Floats and Rationals<a class="headerlink" href="#evaluating-expressions-with-floats-and-rationals" title="Permalink to this headline">¶</a></h3>
<p>SymPy keeps track of the precision of <code class="docutils literal notranslate"><span class="pre">Float</span></code> objects. The default precision is
15 digits. When an expression involving a <code class="docutils literal notranslate"><span class="pre">Float</span></code> 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.</p>
<p>The first issue to keep in mind is how the <code class="docutils literal notranslate"><span class="pre">Float</span></code> is created: it is created
with a value and a precision. The precision indicates how precise of a value
to use when that <code class="docutils literal notranslate"><span class="pre">Float</span></code> (or an expression it appears in) is evaluated.</p>
<p>The values can be given as strings, integers, floats, or rationals.</p>
<blockquote>
<div><ul class="simple">
<li><p>strings and integers are interpreted as exact</p></li>
</ul>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">Float</span><span class="p">(</span><span class="mi">100</span><span class="p">)</span>
<span class="go">100.000000000000</span>
<span class="gp">>>> </span><span class="n">Float</span><span class="p">(</span><span class="s1">'100'</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span>
<span class="go">100.00</span>
</pre></div>
</div>
<ul class="simple">
<li><p>to have the precision match the number of digits, the null string
can be used for the precision</p></li>
</ul>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">Float</span><span class="p">(</span><span class="mi">100</span><span class="p">,</span> <span class="s1">''</span><span class="p">)</span>
<span class="go">100.</span>
<span class="gp">>>> </span><span class="n">Float</span><span class="p">(</span><span class="s1">'12.34'</span><span class="p">)</span>
<span class="go">12.3400000000000</span>
<span class="gp">>>> </span><span class="n">Float</span><span class="p">(</span><span class="s1">'12.34'</span><span class="p">,</span> <span class="s1">''</span><span class="p">)</span>
<span class="go">12.34</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">s</span><span class="p">,</span> <span class="n">r</span> <span class="o">=</span> <span class="p">[</span><span class="n">Float</span><span class="p">(</span><span class="n">j</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span> <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="p">(</span><span class="s1">'0.25'</span><span class="p">,</span> <span class="n">Rational</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">7</span><span class="p">))]</span>
<span class="gp">>>> </span><span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="p">[</span><span class="n">s</span><span class="p">,</span> <span class="n">r</span><span class="p">]:</span>
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
<span class="go">0.250</span>
<span class="go">0.143</span>
</pre></div>
</div>
</div></blockquote>
<p>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:</p>
<blockquote>
<div><p>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):</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">s</span><span class="o">.</span><span class="n">n</span><span class="p">(</span><span class="mi">20</span><span class="p">)</span>
<span class="go">0.25000000000000000000</span>
<span class="gp">>>> </span><span class="n">r</span><span class="o">.</span><span class="n">n</span><span class="p">(</span><span class="mi">20</span><span class="p">)</span>
<span class="go">0.14285278320312500000</span>
</pre></div>
</div>
<p>It is important to realize that although a Float is being displayed in
decimal at arbitrary 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.</p>
</div></blockquote>
<p>If inexact, low-precision numbers are involved in a calculation 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:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">Float</span><span class="p">(</span><span class="s1">'0.1'</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span> <span class="o">+</span> <span class="n">Float</span><span class="p">(</span><span class="s1">'0.1'</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
<span class="go">0.2000061035</span>
</pre></div>
</div>
<p>Although the <code class="docutils literal notranslate"><span class="pre">evalf</span></code> 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.</p>
<p>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:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">big</span> <span class="o">=</span> <span class="mi">12345678901234567890</span>
<span class="gp">>>> </span><span class="n">big_trig_identity</span> <span class="o">=</span> <span class="n">big</span><span class="o">*</span><span class="n">cos</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">big</span><span class="o">*</span><span class="n">sin</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="n">big</span><span class="o">*</span><span class="mi">1</span>
<span class="gp">>>> </span><span class="nb">abs</span><span class="p">(</span><span class="n">big_trig_identity</span><span class="o">.</span><span class="n">subs</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="o">.</span><span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">n</span><span class="p">(</span><span class="mi">2</span><span class="p">))</span> <span class="o">></span> <span class="mi">1000</span>
<span class="go">True</span>
</pre></div>
</div>
<p>When the <span class="math notranslate nohighlight">\(\cos\)</span> and <span class="math notranslate nohighlight">\(\sin\)</span> 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.</p>
<p>There are three things that will help you obtain more precise numerical
values for expressions:</p>
<blockquote>
<div><p>1) Pass the desired substitutions with the call to evaluate. By doing
the subs first, the <code class="docutils literal notranslate"><span class="pre">Float</span></code> values cannot 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:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">big_trig_identity</span><span class="o">.</span><span class="n">n</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="p">{</span><span class="n">x</span><span class="p">:</span> <span class="mf">0.1</span><span class="p">})</span>
<span class="go">-0.e-91</span>
</pre></div>
</div>
<p>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 class="docutils literal notranslate"><span class="pre">-1.4e+3</span></code> above with the nearly zero value obtained when
replacing x with a Rational representing 1/10 – before the call
to evaluate:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">big_trig_identity</span><span class="o">.</span><span class="n">subs</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">S</span><span class="p">(</span><span class="s1">'1/10'</span><span class="p">))</span><span class="o">.</span><span class="n">n</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="go">0.e-91</span>
</pre></div>
</div>
<p>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:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">big_trig_identity</span><span class="o">.</span><span class="n">simplify</span><span class="p">()</span>
<span class="go">0</span>
</pre></div>
</div>
</div></blockquote>
</div>
<div class="section" id="immutability-of-expressions">
<span id="id4"></span><h3>Immutability of Expressions<a class="headerlink" href="#immutability-of-expressions" title="Permalink to this headline">¶</a></h3>
<p>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:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">main</span><span class="p">():</span>
<span class="n">var</span><span class="p">(</span><span class="s1">'x y a b'</span><span class="p">)</span>
<span class="n">expr</span> <span class="o">=</span> <span class="mi">3</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="mi">4</span><span class="o">*</span><span class="n">y</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">'original ='</span><span class="p">,</span> <span class="n">expr</span><span class="p">)</span>
<span class="n">expr_modified</span> <span class="o">=</span> <span class="n">expr</span><span class="o">.</span><span class="n">subs</span><span class="p">({</span><span class="n">x</span><span class="p">:</span> <span class="n">a</span><span class="p">,</span> <span class="n">y</span><span class="p">:</span> <span class="n">b</span><span class="p">})</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">'modified ='</span><span class="p">,</span> <span class="n">expr_modified</span><span class="p">)</span>
<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s2">"__main__"</span><span class="p">:</span>
<span class="n">main</span><span class="p">()</span>
</pre></div>
</div>
<p>The output shows that the <a class="reference internal" href="modules/core.html#sympy.core.basic.Basic.subs" title="sympy.core.basic.Basic.subs"><code class="xref py py-obj docutils literal notranslate"><span class="pre">subs()</span></code></a> function has replaced variable
<code class="docutils literal notranslate"><span class="pre">x</span></code> with variable <code class="docutils literal notranslate"><span class="pre">a</span></code>, and variable <code class="docutils literal notranslate"><span class="pre">y</span></code> with variable <code class="docutils literal notranslate"><span class="pre">b</span></code>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">original</span> <span class="o">=</span> <span class="mi">3</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="mi">4</span><span class="o">*</span><span class="n">y</span>
<span class="n">modified</span> <span class="o">=</span> <span class="mi">3</span><span class="o">*</span><span class="n">a</span> <span class="o">+</span> <span class="mi">4</span><span class="o">*</span><span class="n">b</span>
</pre></div>
</div>
<p>The <a class="reference internal" href="modules/core.html#sympy.core.basic.Basic.subs" title="sympy.core.basic.Basic.subs"><code class="xref py py-obj docutils literal notranslate"><span class="pre">subs()</span></code></a> function does not modify the original expression <code class="docutils literal notranslate"><span class="pre">expr</span></code>.
Rather, a modified copy of the expression is returned. This returned object
is stored in the variable <code class="docutils literal notranslate"><span class="pre">expr_modified</span></code>. Note that unlike C/C++ and
other high-level languages, Python does not require you to declare a variable
before it is used.</p>
</div>
<div class="section" id="mathematical-operators">
<h3>Mathematical Operators<a class="headerlink" href="#mathematical-operators" title="Permalink to this headline">¶</a></h3>
<p>SymPy uses the same default operators as Python. Most of these, like
<code class="docutils literal notranslate"><span class="pre">*/+-</span></code>, are standard. Aside from integer division discussed in
<a class="reference internal" href="#python-vs-sympy-numbers"><span class="std std-ref">Python numbers vs. SymPy Numbers</span></a> above,
you should also be aware that implied multiplication is not allowed. You
need to use <code class="docutils literal notranslate"><span class="pre">*</span></code> whenever you wish to multiply something. Also, to
raise something to a power, use <code class="docutils literal notranslate"><span class="pre">**</span></code>, not <code class="docutils literal notranslate"><span class="pre">^</span></code> as many computer
algebra systems use. Parentheses <code class="docutils literal notranslate"><span class="pre">()</span></code> change operator precedence as
you would normally expect.</p>
<p>In <strong class="command">isympy</strong>, with the <strong class="command">ipython</strong> shell:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="mi">2</span><span class="n">x</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">SyntaxError</span>: <span class="n">invalid syntax</span>
<span class="gp">>>> </span><span class="mi">2</span><span class="o">*</span><span class="n">x</span>
<span class="go">2*x</span>
<span class="gp">>>> </span><span class="p">(</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span><span class="o">^</span><span class="mi">2</span> <span class="c1"># This is not power. Use ** instead.</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">TypeError</span>: <span class="n">unsupported operand type(s) for ^: 'Add' and 'int'</span>
<span class="gp">>>> </span><span class="p">(</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span>
<span class="go">(x + 1)**2</span>
<span class="gp">>>> </span><span class="n">pprint</span><span class="p">(</span><span class="mi">3</span> <span class="o">-</span> <span class="n">x</span><span class="o">**</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">x</span><span class="p">)</span><span class="o">/</span><span class="p">(</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span><span class="p">))</span>
<span class="go"> 2*x</span>
<span class="go"> x</span>
<span class="go">- ----- + 3</span>
<span class="go"> x + 1</span>
</pre></div>
</div>
</div>
<div class="section" id="inverse-trig-functions">
<h3>Inverse Trig Functions<a class="headerlink" href="#inverse-trig-functions" title="Permalink to this headline">¶</a></h3>
<p>SymPy uses different names for some functions than most computer algebra
systems. In particular, the inverse trig functions use the python names
of <a class="reference internal" href="modules/functions/elementary.html#sympy.functions.elementary.trigonometric.asin" title="sympy.functions.elementary.trigonometric.asin"><code class="xref py py-obj docutils literal notranslate"><span class="pre">asin</span></code></a>, <a class="reference internal" href="modules/functions/elementary.html#sympy.functions.elementary.trigonometric.acos" title="sympy.functions.elementary.trigonometric.acos"><code class="xref py py-obj docutils literal notranslate"><span class="pre">acos</span></code></a> and so on instead of the usual <code class="docutils literal notranslate"><span class="pre">arcsin</span></code>
and <code class="docutils literal notranslate"><span class="pre">arccos</span></code>. Use the methods described in <a class="reference internal" href="#symbols"><span class="std std-ref">Symbols</span></a>
above to see the names of all SymPy functions.</p>
</div>
<div class="section" id="sqrt-is-not-a-function">
<h3>Sqrt is not a Function<a class="headerlink" href="#sqrt-is-not-a-function" title="Permalink to this headline">¶</a></h3>
<p>There is no <code class="docutils literal notranslate"><span class="pre">sqrt</span></code> function in the same way that there is an
exponential function (<code class="docutils literal notranslate"><span class="pre">exp</span></code>). <code class="docutils literal notranslate"><span class="pre">sqrt(x)</span></code> is used to represent
<code class="docutils literal notranslate"><span class="pre">Pow(x,</span> <span class="pre">S(1)/2)</span></code> so if you want to know if an expression has any
square roots in it, <code class="docutils literal notranslate"><span class="pre">expr.has(sqrt)</span></code> will not work. You must look
for <code class="docutils literal notranslate"><span class="pre">Pow</span></code> with an exponent of one half (or negative one half if it
is in a denominator, e.g.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="p">(</span><span class="n">y</span> <span class="o">+</span> <span class="n">sqrt</span><span class="p">(</span><span class="n">x</span><span class="p">))</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="n">Wild</span><span class="p">(</span><span class="s1">'w'</span><span class="p">)</span><span class="o">**</span><span class="n">S</span><span class="o">.</span><span class="n">Half</span><span class="p">)</span>
<span class="go">{sqrt(x)}</span>
<span class="gp">>>> </span><span class="p">(</span><span class="n">y</span> <span class="o">+</span> <span class="mi">1</span><span class="o">/</span><span class="n">sqrt</span><span class="p">(</span><span class="n">x</span><span class="p">))</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="n">Wild</span><span class="p">(</span><span class="s1">'w'</span><span class="p">)</span><span class="o">**-</span><span class="n">S</span><span class="o">.</span><span class="n">Half</span><span class="p">)</span>
<span class="go">{1/sqrt(x)}</span>
</pre></div>
</div>
<p>If you are interested in any power of the <code class="docutils literal notranslate"><span class="pre">sqrt</span></code> then the
following pattern would be appropriate</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">sq</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">s</span><span class="p">:</span> <span class="n">s</span><span class="o">.</span><span class="n">is_Pow</span> <span class="ow">and</span> <span class="n">s</span><span class="o">.</span><span class="n">exp</span><span class="o">.</span><span class="n">is_Rational</span> <span class="ow">and</span> <span class="n">s</span><span class="o">.</span><span class="n">exp</span><span class="o">.</span><span class="n">q</span> <span class="o">==</span> <span class="mi">2</span>
<span class="gp">>>> </span><span class="p">(</span><span class="n">y</span> <span class="o">+</span> <span class="n">sqrt</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">**</span><span class="mi">3</span><span class="p">)</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="n">sq</span><span class="p">)</span>
<span class="go">{x**(3/2)}</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="special-symbols">
<h2>Special Symbols<a class="headerlink" href="#special-symbols" title="Permalink to this headline">¶</a></h2>
<p>The symbols <code class="docutils literal notranslate"><span class="pre">[]</span></code>, <code class="docutils literal notranslate"><span class="pre">{}</span></code>, <code class="docutils literal notranslate"><span class="pre">=</span></code>, and <code class="docutils literal notranslate"><span class="pre">()</span></code> have special meanings in
Python, and thus in SymPy. See the Python docs linked to above for
additional information.</p>
<div class="section" id="lists">
<span id="id5"></span><h3>Lists<a class="headerlink" href="#lists" title="Permalink to this headline">¶</a></h3>
<p>Square brackets <code class="docutils literal notranslate"><span class="pre">[]</span></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.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>List indexes begin at 0.</p>
</div>
<p>Example:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="p">[</span><span class="n">x</span><span class="p">,</span> <span class="mi">1</span><span class="p">]</span> <span class="c1"># A simple list of two items</span>
<span class="gp">>>> </span><span class="n">a</span>
<span class="go">[x, 1]</span>
<span class="gp">>>> </span><span class="n">a</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="c1"># This is the first item</span>
<span class="go">x</span>
<span class="gp">>>> </span><span class="n">a</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="mi">2</span> <span class="c1"># You can change values of lists after they have been created</span>
<span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="go">[2, 1]</span>
<span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="n">solve</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">2</span><span class="o">*</span><span class="n">x</span> <span class="o">-</span> <span class="mi">1</span><span class="p">,</span> <span class="n">x</span><span class="p">))</span> <span class="c1"># Some functions return lists</span>
<span class="go">[-1 + sqrt(2), -sqrt(2) - 1]</span>
</pre></div>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>See the Python docs for more information on lists and the square
bracket notation for accessing elements of a list.</p>
</div>
</div>
<div class="section" id="dictionaries">
<h3>Dictionaries<a class="headerlink" href="#dictionaries" title="Permalink to this headline">¶</a></h3>
<p>Curly brackets <code class="docutils literal notranslate"><span class="pre">{}</span></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 class="docutils literal notranslate"><span class="pre">{key:</span> <span class="pre">value}</span></code>. You can access values of keys using square
bracket notation.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="p">{</span><span class="s1">'a'</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="s1">'b'</span><span class="p">:</span> <span class="mi">2</span><span class="p">}</span> <span class="c1"># A dictionary.</span>
<span class="gp">>>> </span><span class="n">d</span>
<span class="go">{'a': 1, 'b': 2}</span>
<span class="gp">>>> </span><span class="n">d</span><span class="p">[</span><span class="s1">'a'</span><span class="p">]</span> <span class="c1"># How to access items in a dict</span>
<span class="go">1</span>
<span class="gp">>>> </span><span class="n">roots</span><span class="p">((</span><span class="n">x</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span><span class="o">*</span><span class="p">(</span><span class="n">x</span> <span class="o">-</span> <span class="mi">2</span><span class="p">),</span> <span class="n">x</span><span class="p">)</span> <span class="c1"># Some functions return dicts</span>
<span class="go">{1: 2, 2: 1}</span>
<span class="gp">>>> </span><span class="c1"># Some SymPy functions return dictionaries. For example,</span>
<span class="gp">>>> </span><span class="c1"># roots returns a dictionary of root:multiplicity items.</span>
<span class="gp">>>> </span><span class="n">roots</span><span class="p">((</span><span class="n">x</span> <span class="o">-</span> <span class="mi">5</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span><span class="o">*</span><span class="p">(</span><span class="n">x</span> <span class="o">+</span> <span class="mi">3</span><span class="p">),</span> <span class="n">x</span><span class="p">)</span>
<span class="go">{-3: 1, 5: 2}</span>
<span class="gp">>>> </span><span class="c1"># This means that the root -3 occurs once and the root 5 occurs twice.</span>
</pre></div>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>See the Python docs for more information on dictionaries.</p>
</div>
</div>
<div class="section" id="tuples">
<h3>Tuples<a class="headerlink" href="#tuples" title="Permalink to this headline">¶</a></h3>
<p>Parentheses <code class="docutils literal notranslate"><span class="pre">()</span></code>, aside from changing operator precedence and their
use in function calls, (like <code class="docutils literal notranslate"><span class="pre">cos(x)</span></code>), are also used for tuples. A
<code class="docutils literal notranslate"><span class="pre">tuple</span></code> is identical to a <a class="reference internal" href="#lists"><span class="std std-ref">list</span></a>, except that it is not
mutable. That means that you cannot 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.</p>
<blockquote>
<div><div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">t</span> <span class="o">=</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="n">x</span><span class="p">)</span> <span class="c1"># Tuples are like lists</span>
<span class="gp">>>> </span><span class="n">t</span>
<span class="go">(1, 2, x)</span>
<span class="gp">>>> </span><span class="n">t</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="go">1</span>
<span class="gp">>>> </span><span class="n">t</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="mi">4</span> <span class="c1"># Except you cannot change them after they have been created</span>
<span class="gt">Traceback (most recent call last):</span>
File <span class="nb">"<console>"</span>, line <span class="m">1</span>, in <span class="n"><module></span>
<span class="gr">TypeError</span>: <span class="n">'tuple' object does not support item assignment</span>
</pre></div>
</div>
<p>Single element tuples, unlike lists, must have a comma in them:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="p">(</span><span class="n">x</span><span class="p">,)</span>
<span class="go">(x,)</span>
</pre></div>
</div>
<p>Without the comma, a single expression without a comma is not a tuple:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="go">x</span>
</pre></div>
</div>
<p>integrate takes a sequence as the second argument if you want to integrate
with limits (and a tuple or list will work):</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">integrate</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span><span class="p">,</span> <span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span>
<span class="go">1/3</span>
<span class="gp">>>> </span><span class="n">integrate</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span><span class="p">,</span> <span class="p">[</span><span class="n">x</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">])</span>
<span class="go">1/3</span>
</pre></div>
</div>
</div></blockquote>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>See the Python docs for more information on tuples.</p>
</div>
</div>
<div class="section" id="keyword-arguments">
<span id="id6"></span><h3>Keyword Arguments<a class="headerlink" href="#keyword-arguments" title="Permalink to this headline">¶</a></h3>
<p>Aside from the usage described <a class="reference internal" href="#equals-signs"><span class="std std-ref">above</span></a>, equals signs
(<code class="docutils literal notranslate"><span class="pre">=</span></code>) are also used to give named arguments to functions. Any
function that has <code class="docutils literal notranslate"><span class="pre">key=value</span></code> in its parameters list (see below on how
to find this out), then <code class="docutils literal notranslate"><span class="pre">key</span></code> is set to <code class="docutils literal notranslate"><span class="pre">value</span></code> 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 class="docutils literal notranslate"><span class="pre">**</span></code> followed by
a name in the parameters list (usually <code class="docutils literal notranslate"><span class="pre">**kwargs</span></code> or
<code class="docutils literal notranslate"><span class="pre">**assumptions</span></code>) allow you to add any number of <code class="docutils literal notranslate"><span class="pre">key=value</span></code> pairs
that you want, and they will all be evaluated according to the function.</p>
<blockquote>
<div><p><code class="docutils literal notranslate"><span class="pre">sqrt(x**2)</span></code> doesn’t auto simplify to x because x is assumed to be
complex by default, and, for example, <code class="docutils literal notranslate"><span class="pre">sqrt((-1)**2)</span> <span class="pre">==</span> <span class="pre">sqrt(1)</span> <span class="pre">==</span> <span class="pre">1</span> <span class="pre">!=</span> <span class="pre">-1</span></code>:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">sqrt</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
<span class="go">sqrt(x**2)</span>
</pre></div>
</div>
<p>Giving assumptions to Symbols is an example of using the keyword argument:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">x</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">'x'</span><span class="p">,</span> <span class="n">positive</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
</pre></div>
</div>
<p>The square root will now simplify since it knows that <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">>=</span> <span class="pre">0</span></code>:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">sqrt</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
<span class="go">x</span>
</pre></div>
</div>
<p>powsimp has a default argument of <code class="docutils literal notranslate"><span class="pre">combine='all'</span></code>:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">pprint</span><span class="p">(</span><span class="n">powsimp</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="n">n</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="n">m</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="n">n</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="n">m</span><span class="p">))</span>
<span class="go"> m + n</span>
<span class="go">(x*y)</span>
</pre></div>
</div>
<p>Setting combine to the default value is the same as not setting it.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">pprint</span><span class="p">(</span><span class="n">powsimp</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="n">n</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="n">m</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="n">n</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="n">m</span><span class="p">,</span> <span class="n">combine</span><span class="o">=</span><span class="s1">'all'</span><span class="p">))</span>
<span class="go"> m + n</span>
<span class="go">(x*y)</span>
</pre></div>
</div>
<p>The non-default options are <code class="docutils literal notranslate"><span class="pre">'exp'</span></code>, which combines exponents…</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">pprint</span><span class="p">(</span><span class="n">powsimp</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="n">n</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="n">m</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="n">n</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="n">m</span><span class="p">,</span> <span class="n">combine</span><span class="o">=</span><span class="s1">'exp'</span><span class="p">))</span>
<span class="go"> m + n m + n</span>
<span class="go">x *y</span>
</pre></div>
</div>
<p>…and ‘base’, which combines bases.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">pprint</span><span class="p">(</span><span class="n">powsimp</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="n">n</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="n">m</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="n">n</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="n">m</span><span class="p">,</span> <span class="n">combine</span><span class="o">=</span><span class="s1">'base'</span><span class="p">))</span>
<span class="go"> m n</span>
<span class="go">(x*y) *(x*y)</span>
</pre></div>
</div>
</div></blockquote>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>See the Python docs for more information on function parameters.</p>
</div>
</div>
</div>
<div class="section" id="getting-help-from-within-sympy">
<h2>Getting help from within SymPy<a class="headerlink" href="#getting-help-from-within-sympy" title="Permalink to this headline">¶</a></h2>
<div class="section" id="help">
<h3>help()<a class="headerlink" href="#help" title="Permalink to this headline">¶</a></h3>
<p>Although all docs are available at <a class="reference external" href="https://docs.sympy.org/">docs.sympy.org</a> or on the
<a class="reference external" href="https://wiki.sympy.org/">SymPy Wiki</a>, 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 class="docutils literal notranslate"><span class="pre">help(function)</span></code>, or <code class="docutils literal notranslate"><span class="pre">function?</span></code> if you are using <strong class="command">ipython</strong>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>In [1]: help(powsimp) # help() works everywhere
In [2]: # But in ipython, you can also use ?, which is better because it
In [3]: # it gives you more information
In [4]: powsimp?
</pre></div>
</div>
<p>These will give you the function parameters and docstring for
<a class="reference internal" href="modules/simplify/simplify.html#sympy.simplify.powsimp.powsimp" title="sympy.simplify.powsimp.powsimp"><code class="xref py py-func docutils literal notranslate"><span class="pre">powsimp()</span></code></a>. The output will look something like this:</p>
<span class="target" id="module-sympy.simplify.simplify"></span><dl class="py function">
<dt>
<code class="sig-prename descclassname">sympy.simplify.simplify.</code><code class="sig-name descname">powsimp</code><span class="sig-paren">(</span><em class="sig-param">expr</em>, <em class="sig-param">deep=False</em>, <em class="sig-param">combine='all'</em>, <em class="sig-param">force=False</em>, <em class="sig-param">measure=<function count_ops></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/46e00feeef5204d896a2fbec65390bd4145c3902/sympy/simplify/powsimp.py#L15-L484"><span class="viewcode-link">[source]</span></a></dt>
<dd><p>reduces expression by combining powers with similar bases and exponents.</p>
<p class="rubric">Explanation</p>
<p>If <code class="docutils literal notranslate"><span class="pre">deep</span></code> is <code class="docutils literal notranslate"><span class="pre">True</span></code> then powsimp() will also simplify arguments of
functions. By default <code class="docutils literal notranslate"><span class="pre">deep</span></code> is set to <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
<p>If <code class="docutils literal notranslate"><span class="pre">force</span></code> is <code class="docutils literal notranslate"><span class="pre">True</span></code> then bases will be combined without checking for
assumptions, e.g. sqrt(x)*sqrt(y) -> sqrt(x*y) which is not true
if x and y are both negative.</p>
<p>You can make powsimp() only combine bases or only combine exponents by
changing combine=’base’ or combine=’exp’. By default, combine=’all’,
which does both. combine=’base’ will only combine:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span> <span class="n">a</span> <span class="n">a</span> <span class="n">a</span> <span class="mi">2</span><span class="n">x</span> <span class="n">x</span>
<span class="n">x</span> <span class="o">*</span> <span class="n">y</span> <span class="o">=></span> <span class="p">(</span><span class="n">x</span><span class="o">*</span><span class="n">y</span><span class="p">)</span> <span class="k">as</span> <span class="n">well</span> <span class="k">as</span> <span class="n">things</span> <span class="n">like</span> <span class="mi">2</span> <span class="o">=></span> <span class="mi">4</span>
</pre></div>
</div>
<p>and combine=’exp’ will only combine</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span> <span class="n">a</span> <span class="n">b</span> <span class="p">(</span><span class="n">a</span> <span class="o">+</span> <span class="n">b</span><span class="p">)</span>
<span class="n">x</span> <span class="o">*</span> <span class="n">x</span> <span class="o">=></span> <span class="n">x</span>
</pre></div>
</div>
<p>combine=’exp’ will strictly only combine exponents in the way that used
to be automatic. Also use deep=True if you need the old behavior.</p>
<p>When combine=’all’, ‘exp’ is evaluated first. Consider the first
example below for when there could be an ambiguity relating to this.
This is done so things like the second example can be completely
combined. If you want ‘base’ combined first, do something like
powsimp(powsimp(expr, combine=’base’), combine=’exp’).</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">powsimp</span><span class="p">,</span> <span class="n">exp</span><span class="p">,</span> <span class="n">log</span><span class="p">,</span> <span class="n">symbols</span>
<span class="gp">>>> </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">,</span> <span class="n">n</span>
<span class="gp">>>> </span><span class="n">powsimp</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="n">y</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="n">z</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="n">z</span><span class="p">,</span> <span class="n">combine</span><span class="o">=</span><span class="s1">'all'</span><span class="p">)</span>
<span class="go">x**(y + z)*y**z</span>
<span class="gp">>>> </span><span class="n">powsimp</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="n">y</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="n">z</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="n">z</span><span class="p">,</span> <span class="n">combine</span><span class="o">=</span><span class="s1">'exp'</span><span class="p">)</span>
<span class="go">x**(y + z)*y**z</span>
<span class="gp">>>> </span><span class="n">powsimp</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="n">y</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="n">z</span><span class="o">*</span><span class="n">y</span><span class="o">**</span><span class="n">z</span><span class="p">,</span> <span class="n">combine</span><span class="o">=</span><span class="s1">'base'</span><span class="p">,</span> <span class="n">force</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go">x**y*(x*y)**z</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">powsimp</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="n">z</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="n">y</span><span class="o">*</span><span class="n">n</span><span class="o">**</span><span class="n">z</span><span class="o">*</span><span class="n">n</span><span class="o">**</span><span class="n">y</span><span class="p">,</span> <span class="n">combine</span><span class="o">=</span><span class="s1">'all'</span><span class="p">,</span> <span class="n">force</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go">(n*x)**(y + z)</span>
<span class="gp">>>> </span><span class="n">powsimp</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="n">z</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="n">y</span><span class="o">*</span><span class="n">n</span><span class="o">**</span><span class="n">z</span><span class="o">*</span><span class="n">n</span><span class="o">**</span><span class="n">y</span><span class="p">,</span> <span class="n">combine</span><span class="o">=</span><span class="s1">'exp'</span><span class="p">)</span>
<span class="go">n**(y + z)*x**(y + z)</span>
<span class="gp">>>> </span><span class="n">powsimp</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="n">z</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="n">y</span><span class="o">*</span><span class="n">n</span><span class="o">**</span><span class="n">z</span><span class="o">*</span><span class="n">n</span><span class="o">**</span><span class="n">y</span><span class="p">,</span> <span class="n">combine</span><span class="o">=</span><span class="s1">'base'</span><span class="p">,</span> <span class="n">force</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go">(n*x)**y*(n*x)**z</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">'x y'</span><span class="p">,</span> <span class="n">positive</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">powsimp</span><span class="p">(</span><span class="n">log</span><span class="p">(</span><span class="n">exp</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">*</span><span class="n">exp</span><span class="p">(</span><span class="n">y</span><span class="p">)))</span>
<span class="go">log(exp(x)*exp(y))</span>
<span class="gp">>>> </span><span class="n">powsimp</span><span class="p">(</span><span class="n">log</span><span class="p">(</span><span class="n">exp</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">*</span><span class="n">exp</span><span class="p">(</span><span class="n">y</span><span class="p">)),</span> <span class="n">deep</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go">x + y</span>
</pre></div>
</div>
<p>Radicals with Mul bases will be combined if combine=’exp’</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">sqrt</span>
<span class="gp">>>> </span><span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">'x y'</span><span class="p">)</span>
</pre></div>
</div>
<p>Two radicals are automatically joined through Mul:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">a</span><span class="o">=</span><span class="n">sqrt</span><span class="p">(</span><span class="n">x</span><span class="o">*</span><span class="n">sqrt</span><span class="p">(</span><span class="n">y</span><span class="p">))</span>
<span class="gp">>>> </span><span class="n">a</span><span class="o">*</span><span class="n">a</span><span class="o">**</span><span class="mi">3</span> <span class="o">==</span> <span class="n">a</span><span class="o">**</span><span class="mi">4</span>
<span class="go">True</span>
</pre></div>
</div>
<p>But if an integer power of that radical has been
autoexpanded then Mul does not join the resulting factors:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">a</span><span class="o">**</span><span class="mi">4</span> <span class="c1"># auto expands to a Mul, no longer a Pow</span>
<span class="go">x**2*y</span>
<span class="gp">>>> </span><span class="n">_</span><span class="o">*</span><span class="n">a</span> <span class="c1"># so Mul doesn't combine them</span>
<span class="go">x**2*y*sqrt(x*sqrt(y))</span>
<span class="gp">>>> </span><span class="n">powsimp</span><span class="p">(</span><span class="n">_</span><span class="p">)</span> <span class="c1"># but powsimp will</span>
<span class="go">(x*sqrt(y))**(5/2)</span>
<span class="gp">>>> </span><span class="n">powsimp</span><span class="p">(</span><span class="n">x</span><span class="o">*</span><span class="n">y</span><span class="o">*</span><span class="n">a</span><span class="p">)</span> <span class="c1"># but won't when doing so would violate assumptions</span>
<span class="go">x*y*sqrt(x*sqrt(y))</span>
</pre></div>
</div>
</dd></dl>
</div>
<div class="section" id="source">
<h3>source()<a class="headerlink" href="#source" title="Permalink to this headline">¶</a></h3>
<p>Another useful option is the <a class="reference internal" href="modules/utilities/source.html#sympy.utilities.source.source" title="sympy.utilities.source.source"><code class="xref py py-func docutils literal notranslate"><span class="pre">source()</span></code></a> function. This will print
the source code of a function, including any docstring that it may have.
You can also do <code class="docutils literal notranslate"><span class="pre">function??</span></code> in <strong class="command">ipython</strong>. For example,
from SymPy 0.6.5:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">source</span><span class="p">(</span><span class="n">simplify</span><span class="p">)</span> <span class="c1"># simplify() is actually only 2 lines of code. </span>
<span class="go">In file: ./sympy/simplify/simplify.py</span>
<span class="go">def simplify(expr):</span>
<span class="go"> """Naively simplifies the given expression.</span>
<span class="go"> ...</span>
<span class="go"> Simplification is not a well defined term and the exact strategies</span>
<span class="go"> this function tries can change in the future versions of SymPy. If</span>
<span class="go"> your algorithm relies on "simplification" (whatever it is), try to</span>
<span class="go"> determine what you need exactly - is it powsimp()? radsimp()?</span>
<span class="go"> together()?, logcombine()?, or something else? And use this particular</span>
<span class="go"> function directly, because those are well defined and thus your algorithm</span>
<span class="go"> will be robust.</span>
<span class="go"> ...</span>
<span class="go"> """</span>
<span class="go"> expr = Poly.cancel(powsimp(expr))</span>
<span class="go"> return powsimp(together(expr.expand()), combine='exp', deep=True)</span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<p class="logo"><a href="index.html">
<img class="logo" src="_static/sympylogo.png" alt="Logo"/>
</a></p>
<h3><a href="index.html">Table of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Gotchas and Pitfalls</a><ul>
<li><a class="reference internal" href="#introduction">Introduction</a></li>
<li><a class="reference internal" href="#equals-signs">Equals Signs (=)</a><ul>
<li><a class="reference internal" href="#single-equals-sign">Single Equals Sign</a></li>
<li><a class="reference internal" href="#double-equals-signs">Double Equals Signs</a></li>
</ul>
</li>
<li><a class="reference internal" href="#variables">Variables</a><ul>
<li><a class="reference internal" href="#variables-assignment-does-not-create-a-relation-between-expressions">Variables Assignment does not Create a Relation Between Expressions</a></li>
<li><a class="reference internal" href="#symbols">Symbols</a></li>
<li><a class="reference internal" href="#functions">Functions</a></li>
</ul>
</li>
<li><a class="reference internal" href="#symbolic-expressions">Symbolic Expressions</a><ul>
<li><a class="reference internal" href="#python-numbers-vs-sympy-numbers">Python numbers vs. SymPy Numbers</a></li>
<li><a class="reference internal" href="#evaluating-expressions-with-floats-and-rationals">Evaluating Expressions with Floats and Rationals</a></li>
<li><a class="reference internal" href="#immutability-of-expressions">Immutability of Expressions</a></li>
<li><a class="reference internal" href="#mathematical-operators">Mathematical Operators</a></li>
<li><a class="reference internal" href="#inverse-trig-functions">Inverse Trig Functions</a></li>
<li><a class="reference internal" href="#sqrt-is-not-a-function">Sqrt is not a Function</a></li>
</ul>
</li>
<li><a class="reference internal" href="#special-symbols">Special Symbols</a><ul>
<li><a class="reference internal" href="#lists">Lists</a></li>
<li><a class="reference internal" href="#dictionaries">Dictionaries</a></li>
<li><a class="reference internal" href="#tuples">Tuples</a></li>
<li><a class="reference internal" href="#keyword-arguments">Keyword Arguments</a></li>
</ul>
</li>
<li><a class="reference internal" href="#getting-help-from-within-sympy">Getting help from within SymPy</a><ul>
<li><a class="reference internal" href="#help">help()</a></li>
<li><a class="reference internal" href="#source">source()</a></li>
</ul>
</li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="tutorial/manipulation.html"
title="previous chapter">Advanced Expression Manipulation</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="modules/index.html"
title="next chapter">SymPy Modules Reference</a></p>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/gotchas.rst.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3 id="searchlabel">Quick search</h3>