<h1>Introduction</h1>

<p><a href="https://docs.sympy.org/latest/tutorial/intro.html">From</a></p>

<h2>What is Symbolic Computation?</h2>

<p>Symbolic computation deals with the computation of mathematical objects symbolically.  This means that the mathematical objects are represented exactly, not approximately, and mathematical expressions with unevaluated variables are left in symbolic form.</p>

<p>Let's take an example. Say we wanted to use the built-in Python functions to compute square roots. We might do something like this</p>


```julia
   >>> import math
   >>> math.sqrt(9)
   3.0
```

<h5>In <code>Julia</code>:</h5>

<ul>
<li><p>Of course, <code>sqrt</code> is already there:</p>
</li>
</ul>


```julia
sqrt(9)
```




    3.0



<hr />

<p>9 is a perfect square, so we got the exact answer, 3. But suppose we computed the square root of a number that isn't a perfect square</p>


```julia
   >>> math.sqrt(8)
   2.82842712475
```

<h5>In <code>Julia</code>:</h5>


```julia
sqrt(8)
```




    2.8284271247461903



<hr />

<p>Here we got an approximate result. 2.82842712475 is not the exact square root of 8 (indeed, the actual square root of 8 cannot be represented by a finite decimal, since it is an irrational number).  If all we cared about was the decimal form of the square root of 8, we would be done.</p>

<p>But suppose we want to go further. Recall that $\sqrt{8} = \sqrt{4\cdot 2} = 2\sqrt{2}$.  We would have a hard time deducing this from the above result. This is where symbolic computation comes in.  With a symbolic computation system like SymPy, square roots of numbers that are not perfect squares are left unevaluated by default</p>


```julia
   >>> import sympy
   >>> sympy.sqrt(3)
   sqrt(3)
```

<h5>In <code>Julia</code>:</h5>


```julia
using SymPy
sympy.sqrt(3)
```




\begin{equation*}\sqrt{3}\end{equation*}



<ul>
<li><p>When <code>SymPy</code> is loaded, the <code>sqrt</code> function is overloaded for symbolic objects, so this could also be done through:</p>
</li>
</ul>


```julia
sqrt(Sym(3))
```




\begin{equation*}\sqrt{3}\end{equation*}



<hr />

<p>Furthermore–-and this is where we start to see the real power of symbolic computation–-symbolic results can be symbolically simplified.</p>


```julia
   >>> sympy.sqrt(8)
   2*sqrt(2)
```

<h5>In <code>Julia</code>:</h5>


```julia
sympy.sqrt(8)
```




\begin{equation*}2 \sqrt{2}\end{equation*}



<hr />

<h2>A More Interesting Example</h2>

<p>The above example starts to show how we can manipulate irrational numbers exactly using SymPy.  But it is much more powerful than that.  Symbolic computation systems (which by the way, are also often called computer algebra systems, or just CASs) such as SymPy are capable of computing symbolic expressions with variables.</p>

<p>As we will see later, in SymPy, variables are defined using <code>symbols</code>. Unlike many symbolic manipulation systems, variables in SymPy must be defined before they are used (the reason for this will be discussed in the :ref:<code>next section &lt;tutorial-gotchas-symbols&gt;</code>).</p>

<p>Let us define a symbolic expression, representing the mathematical expression <code>x &#43; 2y</code>.</p>


```julia
   >>> from sympy import symbols
   >>> x, y = symbols('x y')
   >>> expr = x + 2*y
   >>> expr
   x + 2*y
```

<h5>In <code>Julia</code>:</h5>

<ul>
<li><p>the command <code>from sympy import *</code> is <em>essentially</em> run (only functions are "imported", not all objects), so this becomes the same after adjusting the quotes:</p>
</li>
</ul>


```julia
x, y = symbols("x y")
expr = x + 2*y
expr
```




\begin{equation*}x + 2 y\end{equation*}



<hr />

<p>Note that we wrote <code>x &#43; 2*y</code> just as we would if <code>x</code> and <code>y</code> were ordinary Python variables. But in this case, instead of evaluating to something, the expression remains as just <code>x &#43; 2*y</code>.  Now let us play around with it:</p>


```julia
   >>> expr + 1
   x + 2*y + 1
   >>> expr - x
   2*y
```

<h5>In <code>Julia</code>:</h5>


```julia
expr + 1
```




\begin{equation*}x + 2 y + 1\end{equation*}




```julia
expr - x
```




\begin{equation*}2 y\end{equation*}



<hr />

<p>Notice something in the above example.  When we typed <code>expr - x</code>, we did not get <code>x &#43; 2*y - x</code>, but rather just <code>2*y</code>.  The <code>x</code> and the <code>-x</code> automatically canceled one another.  This is similar to how <code>sqrt&#40;8&#41;</code> automatically turned into <code>2*sqrt&#40;2&#41;</code> above.  This isn't always the case in SymPy, however:</p>


```julia
   >>> x*expr
   x*(x + 2*y)
```

<h5>In <code>Julia</code>:</h5>


```julia
x*expr
```




\begin{equation*}x \left(x + 2 y\right)\end{equation*}



<hr />

<p>Here, we might have expected <code>x&#40;x &#43; 2y&#41;</code> to transform into <code>x^2 &#43; 2xy</code>, but instead we see that the expression was left alone.  This is a common theme in SymPy.  Aside from obvious simplifications like <code>x - x &#61; 0</code> and <code>\sqrt&#123;8&#125; &#61; 2\sqrt&#123;2&#125;</code>, most simplifications are not performed automatically.  This is because we might prefer the factored form <code>x&#40;x &#43; 2y&#41;</code>, or we might prefer the expanded form <code>x^2 &#43; 2xy</code>.  Both forms are useful in different circumstances. In SymPy, there are functions to go from one form to the other</p>


```julia
   >>> from sympy import expand, factor
   >>> expanded_expr = expand(x*expr)
   >>> expanded_expr
   x**2 + 2*x*y
   >>> factor(expanded_expr)
   x*(x + 2*y)
```

<h5>In <code>Julia</code>:</h5>


```julia
expanded_expr = expand(x*expr)
expanded_expr
```




\begin{equation*}x^{2} + 2 x y\end{equation*}




```julia
factor(expanded_expr)
```




\begin{equation*}x \left(x + 2 y\right)\end{equation*}



<hr />

<h2>The Power of Symbolic Computation</h2>

<p>The real power of a symbolic computation system such as SymPy is the ability to do all sorts of computations symbolically.  SymPy can simplify expressions, compute derivatives, integrals, and limits, solve equations, work with matrices, and much, much more, and do it all symbolically.  It includes modules for plotting, printing (like 2D pretty printed output of math formulas, or <code>\LaTeX</code>), code generation, physics, statistics, combinatorics, number theory, geometry, logic, and more. Here is a small sampling of the sort of symbolic power SymPy is capable of, to whet your appetite.</p>


```julia
 >>> from sympy import *
 >>> x, t, z, nu = symbols('x t z nu')
```

<h5>In <code>Julia</code>:</h5>

<ul>
<li><p>again, the functions in the <code>sympy</code> module are already imported:</p>
</li>
</ul>


```julia
x, t, z, nu = symbols("x t z nu")
```




    (x, t, z, nu)



<hr />

<p>This will make all further examples pretty print with unicode characters.</p>


```julia
 >>> init_printing(use_unicode=True)
```

<h5>In <code>Julia</code>:</h5>

<ul>
<li><p>The printing in <code>Julia</code> is controlled by <code>show</code> and the appropriate MIME type.</p>
</li>
</ul>

<hr />

<p>Take the derivative of $\sin{(x)}e^x$.</p>


```julia
 >>> diff(sin(x)*exp(x), x)
  x           x
 ℯ ⋅sin(x) + ℯ ⋅cos(x)
```

<h5>In <code>Julia</code>:</h5>


```julia
diff(sin(x)*exp(x), x)
```




\begin{equation*}e^{x} \sin{\left (x \right )} + e^{x} \cos{\left (x \right )}\end{equation*}



<hr />

<p>Compute $\int(e^x\sin{(x)} + e^x\cos{(x)})\,dx$.</p>


```julia
 >>> integrate(exp(x)*sin(x) + exp(x)*cos(x), x)
  x
 ℯ ⋅sin(x)
```

<h5>In <code>Julia</code>:</h5>


```julia
integrate(exp(x)*sin(x) + exp(x)*cos(x), x)
```




\begin{equation*}e^{x} \sin{\left (x \right )}\end{equation*}



<hr />

<p>Compute $\int_{-\infty}^\infty \sin{(x^2)}\,dx$.</p>


```julia
 >>> integrate(sin(x**2), (x, -oo, oo))
 √2⋅√π
 ─────
   2
```

<h5>In <code>Julia</code>:</h5>

<ul>
<li><p>In <code>Julia</code> <code>**</code> is <code>^</code>:</p>
</li>
</ul>


```julia
integrate(sin(x^2), (x, -oo, oo))
```




\begin{equation*}\frac{\sqrt{2} \sqrt{\pi}}{2}\end{equation*}



<hr />

<p>Find $\lim_{x\to 0}\frac{\sin{(x)}}{x}$.</p>


```julia
 >>> limit(sin(x)/x, x, 0)
 1
```

<h5>In <code>Julia</code>:</h5>


```julia
limit(sin(x)/x, x, 0)
```




\begin{equation*}1\end{equation*}



<hr />

<p>Solve $x^2 - 2 = 0$.</p>


```julia
 >>> solve(x**2 - 2, x)
 [-√2, √2]
```

<h5>In <code>Julia</code>:</h5>


```julia
solve(x^2 - 2, x)
```




\[ \left[ \begin{array}{r}- \sqrt{2}\\\sqrt{2}\end{array} \right] \]



<hr />

<p>Solve the differential equation <code>y&#39;&#39; - y &#61; e^t</code>.</p>


```julia
 >>> y = Function('y')
 >>> dsolve(Eq(y(t).diff(t, t) - y(t), exp(t)), y(t))
            -t   ⎛     t⎞  t
 y(t) = C₂⋅ℯ   + ⎜C₁ + ─⎟⋅ℯ
                 ⎝     2⎠
```

<h5>In <code>Julia</code>:</h5>

<ul>
<li><p><code>Function</code> is not a function, so is not exported. We must qualify its use:</p>
</li>
</ul>


```julia
y = sympy.Function("y")
dsolve(Eq(y(t).diff(t, t) - y(t), exp(t)), y(t))
```




\begin{equation*}y{\left (t \right )} = C_{2} e^{- t} + \left(C_{1} + \frac{t}{2}\right) e^{t}\end{equation*}



<ul>
<li><p>This is made more familiar looking with the <code>SymFunction</code> class:</p>
</li>
</ul>


```julia
y = SymFunction("y")
dsolve(y''(t) - y(t) - exp(t), y(t))
```




\begin{equation*}y{\left (t \right )} = C_{2} e^{- t} + \left(C_{1} + \frac{t}{2}\right) e^{t}\end{equation*}



<hr />

<p>Find the eigenvalues of <code>\left&#91;\begin&#123;smallmatrix&#125;1 &amp; 2\\2 &amp; 2\end&#123;smallmatrix&#125;\right&#93;</code>.</p>


```julia
 >>> Matrix([[1, 2], [2, 2]]).eigenvals()
 ⎧3   √17       √17   3   ⎫
 ⎨─ + ───: 1, - ─── + ─: 1⎬
 ⎩2    2         2    2   ⎭
```

<h5>In <code>Julia</code>:</h5>

<ul>
<li><p>Like <code>Function</code>, <code>Matrix</code> is not imported and its use must by qualified:</p>
</li>
</ul>


```julia
out = sympy.Matrix([[1, 2], [2, 2]]).eigenvals()
```




    Dict{Any,Any} with 2 entries:
      -sqrt(17)/2 + 3/2 => 1
      3/2 + sqrt(17)/2 => 1



<ul>
<li><p>This can be pretty printed if the keys become symbolic:</p>
</li>
</ul>


```julia
convert(Dict{Sym, Any}, out)
```




\begin{equation*}\begin{cases}- \frac{\sqrt{17}}{2} + \frac{3}{2} & \text{=>} &1\\\frac{3}{2} + \frac{\sqrt{17}}{2} & \text{=>} &1\\\end{cases}\end{equation*}



<hr />

<p>Rewrite the Bessel function $J_{\nu}\left(z\right)$ in terms of the spherical Bessel function $j_\nu(z)$.</p>


```julia
  >>> besselj(nu, z).rewrite(jn)
  √2⋅√z⋅jn(ν - 1/2, z)
  ────────────────────
           √π
```

<h5>In <code>Julia</code>:</h5>

<ul>
<li><p>we need to call in <code>SpecialFunctions</code></p>
</li>
<li><p><code>jn</code> is imported as a function object and this is not what SymPy expects, instead we pass in the object <code>sympy.jn</code></p>
</li>
</ul>


```julia
using SpecialFunctions
besselj(nu, z).rewrite(sympy.jn)
```




\begin{equation*}\frac{\sqrt{2} \sqrt{z} j_{\nu - \frac{1}{2}}\left(z\right)}{\sqrt{\pi}}\end{equation*}



<hr />

<p>Print $\int_{0}^{\pi} \cos^{2}{\left (x \right )}\, dx$ using $\LaTeX$.</p>


```julia
  >>> latex(Integral(cos(x)**2, (x, 0, pi)))
  \int_{0}^{\pi} \cos^{2}{\left (x \right )}\, dx
```

<h5>In <code>Julia</code>:</h5>

<ul>
<li><p>Latex printing occurs when the mime type is requested. However, the <code>latex</code> function can be called directly. However, this is not imported by default to avoid name collisions, and so must be qualified. Below, the latex is output as a string, though</p>
</li>
<li><p><code>Integral</code>, like <code>Function</code> and <code>Matrix</code> is not a function and must be qualified</p>
</li>
<li><p><code>**</code> must become <code>^</code></p>
</li>
<li><p>and we use  <code>PI</code>, an alias for <code>sympy.pi</code>, the symbolic value for $\pi$:</p>
</li>
</ul>


```julia
sympy.latex(sympy.Integral(cos(x)^2, (x, 0, PI)))
```




\int_{0}^{\pi} \cos^{2}{\left (x \right )}\, dx



<hr />

<h2>Why SymPy?</h2>

<p>There are many computer algebra systems out there.  <code>This &lt;http://en.wikipedia.org/wiki/List_of_computer_algebra_systems&gt;</code>_ Wikipedia article lists many of them.  What makes SymPy a better choice than the alternatives?</p>

<p>First off, SymPy is completely free. It is open source, and licensed under the liberal BSD license, so you can modify the source code and even sell it if you want to.  This contrasts with popular commercial systems like Maple or Mathematica that cost hundreds of dollars in licenses.</p>

<p>Second, SymPy uses Python.  Most computer algebra systems invent their own language. Not SymPy. SymPy is written entirely in Python, and is executed entirely in Python. This means that if you already know Python, it is much easier to get started with SymPy, because you already know the syntax (and if you don't know Python, it is really easy to learn).  We already know that Python is a well-designed, battle-tested language.  The SymPy developers are confident in their abilities in writing mathematical software, but programming language design is a completely different thing.  By reusing an existing language, we are able to focus on those things that matter: the mathematics.</p>

<p>Another computer algebra system, Sage also uses Python as its language.  But Sage is large, with a download of over a gigabyte.  An advantage of SymPy is that it is lightweight.  In addition to being relatively small, it has no dependencies other than Python, so it can be used almost anywhere easily. Furthermore, the goals of Sage and the goals of SymPy are different.  Sage aims to be a full featured system for mathematics, and aims to do so by compiling all the major open source mathematical systems together into one. When you call some function in Sage, such as <code>integrate</code>, it calls out to one of the open source packages that it includes.  In fact, SymPy is included in Sage.  SymPy on the other hand aims to be an independent system, with all the features implemented in SymPy itself.</p>

<p>A final important feature of SymPy is that it can be used as a library. Many computer algebra systems focus on being usable in interactive environments, but if you wish to automate or extend them, it is difficult to do.  With SymPy, you can just as easily use it in an interactive Python environment or import it in your own Python application.  SymPy also provides APIs to make it easy to extend it with your own custom functions.</p>

<h5>In <code>Julia</code>:</h5>

<p>There are other symbolic packages for <code>Julia</code>:</p>

<ul>
<li><p>Reduce.jl</p>
</li>
<li><p>Symata.jl</p>
</li>
<li><p>SymEngine.jl</p>
</li>
<li><p>Nemo.jl</p>
</li>
</ul>

<p>SymPy is an attractive alternative as <code>PyCall</code> makes most all of its functinality directly available and SymPy is fairly feature rich.</p>

<hr />

<p><a href="./index.html">return to index</a></p>

45dfa31c9e0290560656802379482358d4c372c6
ipynb
Jupyter Notebook
examples/intro.ipynb
UnofficialJuliaMirrorSnapshots/SymPy.jl-24249f21-da20-56a4-8eb1-6a02cf4ae2e6
a6e5a24b3d1ad069a413d0c28f01052c5fa4c6cc
MIT
examples/intro.ipynb
UnofficialJuliaMirrorSnapshots/SymPy.jl-24249f21-da20-56a4-8eb1-6a02cf4ae2e6
a6e5a24b3d1ad069a413d0c28f01052c5fa4c6cc
MIT
examples/intro.ipynb
UnofficialJuliaMirrorSnapshots/SymPy.jl-24249f21-da20-56a4-8eb1-6a02cf4ae2e6
a6e5a24b3d1ad069a413d0c28f01052c5fa4c6cc
MIT
Qwen/Qwen-72B
1. YES
2. YES