<?xml version="1.0" encoding="UTF-8"?>

<rss version="2.0"
     xmlns:content="http://purl.org/rss/1.0/modules/content/"
     xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
     xmlns:atom="http://www.w3.org/2005/Atom"
     xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:wfw="http://wellformedweb.org/CommentAPI/"
     >
  <channel>
    <atom:link href="http://kitchingroup.cheme.cmu.edu/blog/feed/index.xml" rel="self" type="application/rss+xml" />
    <title>The Kitchin Research Group</title>
    <link>https://kitchingroup.cheme.cmu.edu/blog</link>
    <description>Chemical Engineering at Carnegie Mellon University</description>
    <pubDate>Sat, 01 Nov 2025 13:47:46 GMT</pubDate>
    <generator>Blogofile</generator>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    
    <item>
      <title>Symbolic math in python</title>
      <link>https://kitchingroup.cheme.cmu.edu/blog/2013/03/01/Symbolic-math-in-python</link>
      <pubDate>Fri, 01 Mar 2013 19:07:48 EST</pubDate>
      <category><![CDATA[symbolic]]></category>
      <category><![CDATA[math]]></category>
      <guid isPermaLink="false">AAKSB0VVX_XkVXNAmXvGC1HyE4w=</guid>
      <description>Symbolic math in python</description>
      <content:encoded><![CDATA[


&lt;p&gt;
 &lt;a href="http://matlab.cheme.cmu.edu/2011/08/10/symbolic-math-in-matlab/" &gt;Matlab post&lt;/a&gt;
Python has capability to do symbolic math through the sympy package. 
&lt;/p&gt;
&lt;div id="outline-container-1" class="outline-2"&gt;
&lt;h2 id="sec-1"&gt;&lt;span class="section-number-2"&gt;1&lt;/span&gt; Solve the quadratic equation&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-1"&gt;
&lt;div class="org-src-container"&gt;

&lt;pre class="src src-python"&gt;&lt;span style="color: #8b0000;"&gt;from&lt;/span&gt; sympy &lt;span style="color: #8b0000;"&gt;import&lt;/span&gt; solve, symbols, pprint

a,b,c,x = symbols(&lt;span style="color: #228b22;"&gt;'a,b,c,x'&lt;/span&gt;)

f = a*x**2 + b*x + c

solution = solve(f, x)
&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; solution
pprint(solution)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
&amp;gt;&amp;gt;&amp;gt; &amp;gt;&amp;gt;&amp;gt; &amp;gt;&amp;gt;&amp;gt; &amp;gt;&amp;gt;&amp;gt; &amp;gt;&amp;gt;&amp;gt; &amp;gt;&amp;gt;&amp;gt; [(-b + (-4*a*c + b**2)**(1/2))/(2*a), -(b + (-4*a*c + b**2)**(1/2))/(2*a)]
_____________   /       _____________\ 
        /           2    |      /           2 | 
 -b + \/  -4*a*c + b    -\b + \/  -4*a*c + b  / 
[---------------------, -----------------------]
          2*a                     2*a
&lt;/pre&gt;

&lt;p&gt;
The solution you should recognize in the form of \(\frac{b \pm \sqrt{b^2 - 4 a c}}{2 a}\) although python does not print it this nicely!
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-2" class="outline-2"&gt;
&lt;h2 id="sec-2"&gt;&lt;span class="section-number-2"&gt;2&lt;/span&gt; differentiation&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-2"&gt;
&lt;p&gt;
you might find this helpful!
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-python"&gt;&lt;span style="color: #8b0000;"&gt;from&lt;/span&gt; sympy &lt;span style="color: #8b0000;"&gt;import&lt;/span&gt; diff

&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; diff(f, x)
&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; diff(f, x, 2)

&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; diff(f, a)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
&amp;gt;&amp;gt;&amp;gt; 2*a*x + b
2*a
&amp;gt;&amp;gt;&amp;gt; x**2
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-3" class="outline-2"&gt;
&lt;h2 id="sec-3"&gt;&lt;span class="section-number-2"&gt;3&lt;/span&gt; integration&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-3"&gt;
&lt;div class="org-src-container"&gt;

&lt;pre class="src src-python"&gt;&lt;span style="color: #8b0000;"&gt;from&lt;/span&gt; sympy &lt;span style="color: #8b0000;"&gt;import&lt;/span&gt; integrate

&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; integrate(f, x)          &lt;span style="color: #ff0000; font-weight: bold;"&gt;# &lt;/span&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;indefinite integral&lt;/span&gt;
&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; integrate(f, (x, 0, 1))  &lt;span style="color: #ff0000; font-weight: bold;"&gt;# &lt;/span&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;definite integral from x=0..1&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
&amp;gt;&amp;gt;&amp;gt; a*x**3/3 + b*x**2/2 + c*x
a/3 + b/2 + c
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-4" class="outline-2"&gt;
&lt;h2 id="sec-4"&gt;&lt;span class="section-number-2"&gt;4&lt;/span&gt; Analytically solve a simple ODE&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-4"&gt;
&lt;div class="org-src-container"&gt;

&lt;pre class="src src-python"&gt;&lt;span style="color: #8b0000;"&gt;from&lt;/span&gt; sympy &lt;span style="color: #8b0000;"&gt;import&lt;/span&gt; Function, Symbol, dsolve
f = Function(&lt;span style="color: #228b22;"&gt;'f'&lt;/span&gt;)
x = Symbol(&lt;span style="color: #228b22;"&gt;'x'&lt;/span&gt;)
fprime = f(x).diff(x) - f(x) &lt;span style="color: #ff0000; font-weight: bold;"&gt;# &lt;/span&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;f' = f(x)&lt;/span&gt;

y = dsolve(fprime, f(x))

&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; y
&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; y.subs(x,4)
&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; [y.subs(x, X) &lt;span style="color: #8b0000;"&gt;for&lt;/span&gt; X &lt;span style="color: #8b0000;"&gt;in&lt;/span&gt; [0, 0.5, 1]] &lt;span style="color: #ff0000; font-weight: bold;"&gt;# &lt;/span&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;multiple values&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
&amp;gt;&amp;gt;&amp;gt; &amp;gt;&amp;gt;&amp;gt; &amp;gt;&amp;gt;&amp;gt; &amp;gt;&amp;gt;&amp;gt; &amp;gt;&amp;gt;&amp;gt; &amp;gt;&amp;gt;&amp;gt; f(x) == exp(C1 + x)
f(4) == exp(C1 + 4)
[f(0) == exp(C1), f(0.5) == exp(C1 + 0.5), f(1) == exp(C1 + 1)]
&lt;/pre&gt;

&lt;p&gt;
It is not clear you can solve the initial value problem to get C1.
&lt;/p&gt;

&lt;p&gt;
The symbolic math in sympy is pretty good. It is not up to the capability of Maple or Mathematica, (but neither is Matlab) but it continues to be developed, and could be helpful in some situations.
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Copyright (C) 2013 by John Kitchin. See the &lt;a href="/copying.html"&gt;License&lt;/a&gt; for information about copying.&lt;p&gt;&lt;p&gt;&lt;a href="/org/2013/03/01/Symbolic-math-in-python.org"&gt;org-mode source&lt;/a&gt;&lt;p&gt;]]></content:encoded>
    </item>
  </channel>
</rss>
