Jekyll2020-05-01T21:16:31+00:00https://8hantanu.me/atom.xmlShantanu’s ShallopShantanu's shallop - random patterns ...Shantanu MishraGSoC’18 log#06: Packing Up2018-08-11T00:00:00+00:002018-08-11T07:00:00+00:00https://8hantanu.me/hack-stack/2018/08/11/gsoc-log06-packing-up<p>This is GSoC’18 log#06. Here I will cover on what I have done in week #12-13. Link to the <a href="/hack-stack/2018/07/28/gsoc-log05-dynamic-simplification-and-plotting.html" title="GSoC'18 log#05">previous log</a>. The work in these two weeks involved packaging and documenting the code.</p>
<p><img src="/assets/images/vismabanner.jpg" alt="visma-banner" class="align-center" /></p>
<h2 id="done-so-far">Done so far…</h2>
<p>Deciding which packaging style to choose was a difficult task. Though there were a number of options available for packaging python apps I couldn’t find one which makes it simple for packaging it for windows. I managed to convert the source into a single executable file using <strong>pyinstaller</strong> but the performance was compromised.</p>
<p>Finally I went with <strong>PyPI</strong>. A simple <em>setup.py</em> was created which contained information about the dependencies to be installed. Installing <strong>visma</strong> is quite simple now. To install just type:</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nv">$ </span>pip3 <span class="nb">install </span>VISualMAth
</code></pre></div></div>
<p>And to launch type:</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nv">$ </span>visma
</code></pre></div></div>
<p>I also modified the <em>run</em> script which can be used by future developers to install, test and package <strong>visma</strong>. Below are the available options in the new <em>run</em> script.</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>$ ./run
Enter command arguments with run
./run install - Install all dependencies for visma
./run visma - Open visma GUI
./run test - Run all the tests and generates coverage report
./run test path/to/test_file.py - Runs all tests and shows coverage for given file
./run test syntax - Run syntax test using pylama
./run test modules - Run tests using pytest for all modules
./run test coverage - After running all the tests, open coverage report
./run pack - Generate builds for visma package
./run pack upload - Generate builds and then upload to test.pypi.org
./run pack final - Generate builds and upload final build to pypi.org
./run clean - Clean all cache, reports and builds
</code></pre></div></div>
<p>The <em>plotter</em> has been divided into two separate tabs i.e. 2D plot and 3D plot. The appropriate tab will be focused while plotting a given input. Also I have embedded the settings menu into the UI instead of creating a pop-up one. The settings contain options to enable/disable UI elements, change font sizes, change plot’s axis ranges and mesh density etc.</p>
<p>Plotting an equation in different axis ranges helps in visualizing it in a better way. The below demo justifies the previous statement.</p>
<p><img src="/assets/images/demos/visma/demo6.gif" alt="visma-demo6" class="align-center" /></p>
<h2 id="what-i-will-be-doing-next">What I will be doing next…</h2>
<p>The github wiki is yet to be updated. It will contain the user and developer manual. Some more inline comments and docstrings can be added.</p>
<p>Though the GSoC period is coming to an end, there are still a lot of new things I want to implement in <strong>visma</strong>. I am thinking of making <strong>visma</strong> accessible from the terminal itself. A webapp is also on my mind (will implement after adding some more useful features).</p>Shantanu MishraThis is GSoC’18 log#06. Here I will cover on what I have done in week #12-13. Link to the previous log. The work in these two weeks involved packaging and documenting the code.GSoC’18 log#05: Dynamic Simplification and Plotting2018-07-28T00:00:00+00:002018-07-28T07:00:00+00:00https://8hantanu.me/hack-stack/2018/07/28/gsoc-log05-dynamic-simplification-and-plotting<p>This is GSoC’18 log#05. Here I will cover on what I have done in week #10-11. Link to the <a href="/hack-stack/2018/07/14/gsoc-log04-develop-test-repeat.html" title="GSoC'18 log#04">previous log</a>. The work during this period is mostly done in <a href="https://github.com/aerospaceresearch/visma/blob/master/visma/gui">visma/gui</a>.</p>
<p><img src="/assets/images/vismabanner.jpg" alt="visma-banner" class="align-center" /></p>
<h2 id="done-so-far">Done so far…</h2>
<p>A quick solver (<a href="https://github.com/aerospaceresearch/visma/blob/master/visma/gui/qsolver.py">visma/gui/qsolver.py</a>) has been implemented which dynamically simplifies the expression as the user inputs. Instead of implementing the <em>logger</em> which was supposed to report if the input expression is invalid, I have implemented this feature in the <em>qsolver</em> itself. It lets the user know what is missing in the input or if the input syntax is wrong.</p>
<p>A basic <em>plotter</em> using <strong>matplotlib</strong> was implemented in the first phase which plotted the result in 2D. It is now modified for plotting graphs in both 2D and 3D. The 3D graph is a mesh which resembles the surface of the input. Given the input, <em>plotter</em> decides (based on the number of variables and the type of input) in which dimension is the graph to be plotted.</p>
<p>The <em>plotter</em> logic is:</p>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">if</span> <span class="p">(</span><span class="n">noOfVars</span> <span class="o">==</span> <span class="mi">1</span> <span class="ow">and</span> <span class="n">eqType</span> <span class="o">==</span> <span class="s">"expression"</span><span class="p">)</span> <span class="ow">or</span> <span class="p">(</span><span class="n">noOfVars</span> <span class="o">==</span> <span class="mi">2</span> <span class="ow">and</span> <span class="n">eqType</span> <span class="o">==</span> <span class="s">"equation"</span><span class="p">):</span>
<span class="n">graphVars</span><span class="p">,</span> <span class="n">func</span> <span class="o">=</span> <span class="n">plotIn2D</span><span class="p">(</span><span class="n">LHStok</span><span class="p">,</span> <span class="n">RHStok</span><span class="p">,</span> <span class="n">variables</span><span class="p">)</span>
<span class="k">elif</span> <span class="p">(</span><span class="n">noOfVars</span> <span class="o">==</span> <span class="mi">2</span> <span class="ow">and</span> <span class="n">eqType</span> <span class="o">==</span> <span class="s">"expression"</span><span class="p">)</span> <span class="ow">or</span> <span class="p">(</span><span class="n">noOfVars</span> <span class="o">==</span> <span class="mi">3</span> <span class="ow">and</span> <span class="n">eqType</span> <span class="o">==</span> <span class="s">"equation"</span><span class="p">):</span>
<span class="n">graphVars</span><span class="p">,</span> <span class="n">func</span> <span class="o">=</span> <span class="n">plotIn3D</span><span class="p">(</span><span class="n">LHStok</span><span class="p">,</span> <span class="n">RHStok</span><span class="p">,</span> <span class="n">variables</span><span class="p">)</span>
</code></pre></div></div>
<p>The above snippet can be found <a href="https://github.com/aerospaceresearch/visma/blob/4476efceddabb7e543332e377062e8d166591844/visma/gui/plotter.py#L22">here</a>.</p>
<p>For example:</p>
<ul>
<li>
<p>If input is <strong>x^2</strong> , the plot <strong>f(x) = x^2</strong> is plotted in 2D.</p>
</li>
<li>
<p>If input is <strong>x^2 + y^2 = 5</strong> , the plot <strong>x^2 + y^2 - 5 = 0</strong> is plotted in 2D.</p>
</li>
<li>
<p>If input is <strong>x^2 - 2*y</strong> , the plot <strong>x^2 - 2y = 0</strong> is plotted in 3D. This case (expression type with 2 variables) needs a fix. A plot for <strong>f(x,y) = x^2 - 2y</strong> must be plotted instead.</p>
</li>
<li>
<p>If input is <strong>x^2/2 + y^2/5 = z</strong> , the plot <strong>0.5x^2 + 0.2y^2/5 - z = 0</strong> is plotted in 3D.</p>
</li>
</ul>
<p>Below the <em>qsolver</em> and <em>plotter</em> can be seen in action.</p>
<p><img src="/assets/images/demos/visma/demo5.gif" alt="visma-demo5" class="align-center" /></p>
<h2 id="what-i-will-be-doing-next">What I will be doing next…</h2>
<p>Almost all of the basic work related to GUI is finished. I will try to enhance the UI by adding settings menu and options to enable or disable certain UI elements. A simple option for enabling/disabling <em>qsolver</em> has been already added.</p>
<p>In the coming days, I will package <strong>visma</strong> for all desktop operating systems(Linux, Mac, and Windows). I am looking into pyinstaller for packaging <strong>visma</strong> for windows. Also in the final weeks, I will write docstrings and inline comments for the code, and update the github wiki.</p>
<p>Link to <a href="https://github.com/aerospaceresearch/visma" title="visma">project source</a> and <a href="https://github.com/aerospaceresearch/visma/projects/1" title="Project Progress">to-do board</a>.</p>Shantanu MishraThis is GSoC’18 log#05. Here I will cover on what I have done in week #10-11. Link to the previous log. The work during this period is mostly done in visma/gui.GSoC’18 log#04: Develop, Test, Repeat2018-07-14T00:00:00+00:002018-07-14T07:00:00+00:00https://8hantanu.me/hack-stack/2018/07/14/gsoc-log04-develop-test-repeat<p>This is GSoC log#04 (view log#03 <a href="/hack-stack/2018/06/30/gsoc-log03-finding-the-unknown.html">here</a>). Here I will cover on what I have done in week #08-09 and a gist of what has been accomplished in Phase II coding period and what is to be done in the final coding period.</p>
<p><img src="/assets/images/vismabanner.jpg" alt="visma-banner" class="align-center" /></p>
<h2 id="done-so-far">Done so far…</h2>
<ul>
<li>Added tests using pytest</li>
<li>Code coverage using coverage.py through pytest</li>
<li>Added variable substitution methods</li>
<li>Created an equation solver in multi-variables</li>
<li>Initialized work on matrices</li>
<li>Ported source to Python3 and using PyQt5</li>
</ul>
<p>Variable substitution and matrices will be used to solve multiple equations at once. The matrices will be used for solving multiple linear equations. The variable substitution will be used to solve the rest of the equation types.</p>
<p>Almost all test cases have been added to the tests module. Tests have been integrated to Travis CI. So any new commit/pull request by anyone will go through all test cases’ check.
Checking code-coverage through tests has helped me to see clearly which parts of the code are unused. View <a href="https://coveralls.io/github/8hantanu/visma">code-coverage</a>.</p>
<p>Below is an example dump generated after running tests through pytest.</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nv">$ </span>pytest
<span class="o">===============================</span> <span class="nb">test </span>session starts <span class="o">================================</span>
platform linux2 <span class="nt">--</span> Python 3.6.5, pytest-3.5.1, py-1.5.3, pluggy-0.6.0
rootdir: /Projects/GitHub/visma, inifile: setup.cfg
plugins: pylama-7.4.3
collected 25 items
tests/test_calculus.py .. <span class="o">[</span> 8%]
tests/test_functions.py .. <span class="o">[</span> 16%]
tests/test_io.py .... <span class="o">[</span> 32%]
tests/test_matrix.py ....... <span class="o">[</span> 60%]
tests/test_simplify.py ... <span class="o">[</span> 72%]
tests/test_solvers.py .. <span class="o">[</span> 80%]
tests/test_transform.py .. <span class="o">[</span> 88%]
tests/test_utils.py ... <span class="o">[</span>100%]
<span class="o">============================</span> 25 passed <span class="k">in </span>0.93 seconds <span class="o">=============================</span>
</code></pre></div></div>
<h2 id="phase---2-deliverables">Phase - 2 deliverables:</h2>
<ul>
<li><strong>Modules created</strong>:
<ul>
<li><strong>Solver</strong> - An equation solver</li>
<li><strong>Tests</strong> - Added tests for all modules</li>
<li><strong>Matrix</strong> - Support for functions in matrix</li>
<li><strong>Utils</strong> - Simple universal methods</li>
</ul>
</li>
<li><strong>Enhancements</strong>:
<ul>
<li>Ported code base to python3 from python2</li>
<li>Moved to PyQt5 from PyQt4</li>
</ul>
</li>
</ul>
<h2 id="what-i-will-be-doing-next">What I will be doing next…</h2>
<p>Most of the time of final period will be spent on working on the missing test cases and integrating the different kinds of solvers and matrices with the GUI. Also, calculus functionalities will be enhanced and support for more functions will be added.</p>
<p>GUI will be enhanced. Logger will be added and plotter will be modified to support equations in multiple variables. Here is a sneak peek of the dynamic simplifier I am working on.</p>
<p><img src="/assets/images/demos/visma/demo4.gif" alt="visma-demo4" class="align-center" /></p>
<p>Link to <a href="https://github.com/aerospaceresearch/visma" title="visma">project source</a> and <a href="https://github.com/aerospaceresearch/visma/projects/1" title="Project Progress">to-do board</a>.</p>Shantanu MishraThis is GSoC log#04 (view log#03 here). Here I will cover on what I have done in week #08-09 and a gist of what has been accomplished in Phase II coding period and what is to be done in the final coding period.GSoC’18 log#03: Finding the Unknown2018-06-30T00:00:00+00:002018-05-30T07:00:00+00:00https://8hantanu.me/hack-stack/2018/06/30/gsoc-log03-finding-the-unknown<p>This is GSoC’18 log#03. Here I will cover on what I have done in week #06-07. Link to the <a href="/hack-stack/2018/06/16/gsoc-log02-integrating-the-integrator.html" title="GSoC'18 log#02">previous log</a>.</p>
<p><img src="/assets/images/vismabanner.jpg" alt="visma-banner" class="align-center" /></p>
<h2 id="done-so-far">Done so far…</h2>
<p>I have implemented a simple solver which is capable of solving a given variable in terms of other variables and numbers. Below is a simple example of how the solver works.</p>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">x</span><span class="o">^</span><span class="mi">2</span><span class="o">*</span><span class="n">y</span> <span class="o">-</span> <span class="n">z</span> <span class="o">=</span> <span class="mi">1</span> <span class="c1"># solving for x
</span><span class="n">x</span><span class="o">^</span><span class="mi">2</span><span class="n">y</span> <span class="o">-</span> <span class="n">z</span> <span class="o">-</span> <span class="mi">1</span> <span class="o">=</span> <span class="mi">0</span> <span class="c1"># simplified and moved to LHS
</span><span class="n">x</span><span class="o">^</span><span class="mi">2</span><span class="n">y</span> <span class="o">=</span> <span class="n">z</span> <span class="o">+</span> <span class="mi">1</span> <span class="c1"># all functions not containing x moved to RHS
</span><span class="n">x</span><span class="o">^</span><span class="mi">2</span> <span class="o">=</span> <span class="p">(</span><span class="n">z</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span><span class="o">/</span><span class="n">y</span> <span class="c1"># inverse method is called
</span><span class="n">x</span> <span class="o">=</span> <span class="p">((</span><span class="n">z</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span><span class="o">/</span><span class="n">y</span><span class="p">)</span><span class="o">^</span><span class="mf">0.5</span> <span class="c1"># final result
</span></code></pre></div></div>
<p>To make the solver some features like expression operations were required.
Also, support for nested expressions has been added. So when operating with expressions the operations will be worked out from inside-out.</p>
<p>While I was working on expressions, I missed a few trivial test cases. Since the number of functions/operations are increasing the number of test cases is increasing.</p>
<p>Hence I decided to automate testing using unit-test. Since this topic was new to me, I tried using various tests available to find which was best suited for <strong>visma</strong>. I started using <strong>doctest</strong> but later moved on to <strong>pytest</strong> for its number of features. Writing a test through pytest is as simple as:-</p>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kn">from</span> <span class="nn">visma.utils.integers</span> <span class="kn">import</span> <span class="n">gcd</span>
<span class="k">def</span> <span class="nf">test_gcd</span><span class="p">():</span> <span class="c1"># simple test for gcd
</span> <span class="k">assert</span> <span class="n">gcd</span><span class="p">([</span><span class="mi">1</span><span class="p">])</span> <span class="o">==</span> <span class="mi">1</span>
<span class="k">assert</span> <span class="n">gcd</span><span class="p">([</span><span class="mi">3</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">24</span><span class="p">])</span> <span class="o">==</span> <span class="mi">3</span>
</code></pre></div></div>
<p>While working on these I learned a lot about unit-tests and code-coverage. I used <strong>coverage.py</strong> through <strong>pytest</strong> to measure code-coverage. It helps to know which part of the code is being neglected and helps find missing test cases.</p>
<p>I fiddled a little with the stylesheets in PyQt4 and made the GUI a little cleaner. Below is the solver in action.</p>
<p><img src="/assets/images/demos/visma/demo3.gif" alt="visma-demo3" class="align-center" /></p>
<h2 id="what-i-will-be-doing-next">What I will be doing next…</h2>
<p>Since the solver for single equation has been implemented, the next thing will be to extend it to solve multiple equations at once. A variable substitution method will be written which will substitute a variable from one equation to the other equation.</p>
<p>Also for multi-variable linear equations, matrix modules will be created and Gauss-Jordan elimination will be used for solving these. The solver will also be modified to support inequality solving.</p>
<p>Since I have just initialized testing using pytest, there are many test cases left to be written. Tests for specifically each module will be created.</p>
<p>Link to <a href="https://github.com/aerospaceresearch/visma" title="visma">project source</a> and <a href="https://github.com/aerospaceresearch/visma/projects/1" title="Project Progress">to-do board</a>.</p>Shantanu MishraThis is GSoC’18 log#03. Here I will cover on what I have done in week #06-07. Link to the previous log.GSoC’18 log#02: Integrating the Integrator2018-06-16T00:00:00+00:002018-06-16T07:00:00+00:00https://8hantanu.me/hack-stack/2018/06/16/gsoc-log02-integrating-the-integrator<p>This is GSoC log#02 (view log#01 <a href="/hack-stack/2018/05/26/gsoc-log01-making-visma-classy.html">here</a>). Here I will cover on what I have done in week #03-05 and a gist of what has been accomplished in Phase I coding period.</p>
<p><img src="/assets/images/vismabanner.jpg" alt="visma-banner" class="align-center" /></p>
<h2 id="done-so-far">Done so far…</h2>
<ul>
<li>Created parser.py which handles conversion of tokens to LaTeX or strings and vice-versa.</li>
<li>Some simplification issues were fixed</li>
<li>Integrated the differentiation.py and integration.py to the project along with the step-by-step explanation.</li>
<li>Support for partial differentiation and “partial integration” was added.</li>
<li>The code was again refactored to decrease complexity and remove unused modules.</li>
<li>Added factorize.py module. Can currently factorize polynomials.</li>
</ul>
<p>The differentiation.py works for all functions. The integration.py is a tough one. There are going to be many checks for integration as when to apply by-parts or some another method. I am working on a simpler version of integration by-parts algorithm. For now, VisMa supports basic calculus operations. Also, transform module was added to change from one function type to another.</p>
<p>The below demo shows the newly added functionalities like factorizing polynomials, differentiating and integrating expressions with respect to a variable.</p>
<p><img src="/assets/images/demos/visma/demo2.gif" alt="visma-demo" class="align-center" /></p>
<h2 id="phase---1-deliverables">Phase - 1 deliverables:</h2>
<ul>
<li><strong>Modules created</strong>:
<ul>
<li><strong>Functions</strong> - All function classes added</li>
<li><strong>GUI/Plotter</strong> - Graph plotting added</li>
<li><strong>IO</strong> - Input, output parsers</li>
<li><strong>Calculus</strong> - Diff and integrate</li>
</ul>
</li>
<li><strong>Issues fixed</strong>:
<ul>
<li>Refactored code to follow object class style</li>
<li>Rectified equation/expression simplifications</li>
<li>Embedded solver into main GUI</li>
<li>Reorganised code into smaller modules</li>
</ul>
</li>
</ul>
<h2 id="what-i-will-be-doing-next">What I will be doing next…</h2>
<p>The next thing which I will work on is a basic equation solver. For making the equation solver modules like factorization, expression multiplication and division are required.</p>
<p>Also as the number of functions are increasing, the cases to test are increasing. I will try to automate testing using unit tests.</p>
<p>Most of the time of phase II period will be spent on working on all kinds of solvers. Also, calculus functionalities will be enhanced and support for more types of functions will be added.</p>
<p>Link to <a href="https://github.com/aerospaceresearch/visma" title="visma">project source</a> and <a href="https://github.com/aerospaceresearch/visma/projects/1" title="Project Progress">to-do board</a>.</p>Shantanu MishraThis is GSoC log#02 (view log#01 here). Here I will cover on what I have done in week #03-05 and a gist of what has been accomplished in Phase I coding period.GSoC’18 log#01: Making VisMa *classy*2018-05-26T00:00:00+00:002018-05-26T07:00:00+00:00https://8hantanu.me/hack-stack/2018/05/26/gsoc-log01-making-visma-classy<p>I came across <a href="https://aerospaceresearch.net">AerospaceResearch.net</a> when browsing through GSoC organizations. It offered projects both in my field of interests and academic domain. I had to make a hard choice between <strong>DirectDemod</strong> and <strong>VisMa</strong> and I finally chose <strong>VisMa</strong> as my project for GSoC.</p>
<p><img src="/assets/images/vismabanner.jpg" alt="visma-banner" class="align-center" /></p>
<p>This is my first GSoC dev log (more to come). Here I will be logging about what I learned, what I have done and what I will do. So following is the work I have done in VisMa. This log covers on what I have done in week#01-02.</p>
<h2 id="done-so-far">Done so far…</h2>
<p>I used the community bonding period to fix minor errors and get more familiar with the source. I restructured the code base so that new modules could be accommodated. Code duplication was reduced using proper imports between modules. New modules like calculus, transform, solvers, gui etc were initialized. A token IDing module was written to handle equations during calculus operations.</p>
<p>Till now VisMa processed input equations into ‘tokens’ which were formatted in the form of python dictionaries. Erm?! Tokens? Check this out.
The main aim during this two week period was to convert all the dictionaries to class-objects i.e. to follow the object-oriented style. Classes for the following function types were created:</p>
<ul>
<li>trigonometric</li>
<li>hyperbolic</li>
<li>exponential</li>
<li>variable</li>
<li>constant</li>
</ul>
<p>While making these, class methods like ‘differentiate’ and ‘inverse’ were added to these function classes.</p>
<p>While everything worked perfectly with objects, the steps animator didn’t comply. The animator used JSON serializer which would use only strings/dictionary format. Although the object properties could be converted to dictionary format, it would defeat the main purpose. So I decided to use TeX to render equations. The step animator was remolded to render the equations in TeX format using matplotlib’s renderer.</p>
<p>An equation plotter was built with the help of matplotlib. The plotter supports plotting equations in one and two variables. While working on the GUI I learned a lot about PyQT4, a GUI library for python. I have updated the GUI so that the plotter and step-animator are embedded in the main window itself. Here is the new GUI in action.</p>
<p><img src="/assets/images/demos/visma/demo1.gif" alt="visma-demo" class="align-center" /></p>
<h2 id="what-i-will-be-doing-next">What I will be doing next…</h2>
<p>The above example is one of the test cases of the input equations. The animator and plotter have to be fixed to run all cases. Adding animations to the animator is one of the things. I will try to add support for a 3d graph as well(for equations in three variables).</p>
<p>The next thing to do will be integrating the token IDing module. The rules of calculus will be based on this IDing module. The code can be optimized by converting some functions to class methods. Also, some work relating to equation solvers will be initiated.</p>
<p>From now on I will be updating the log on a fortnightly basis (read on <a href="https://aerospaceresearch.net/?p=691">AerospaceResearch.net</a>). The project progress can be viewed here. BTW I added a new VisMa logo.</p>
<p align="center"><b>VisMa, now classy and sassy !!</b>
</p>Shantanu MishraI came across AerospaceResearch.net when browsing through GSoC organizations. It offered projects both in my field of interests and academic domain. I had to make a hard choice between DirectDemod and VisMa and I finally chose VisMa as my project for GSoC.Solving the Cake Series2016-12-25T00:00:00+00:002018-09-06T07:00:00+00:00https://8hantanu.me/geek-streak/2016/12/25/solving-the-cake-series<p>In the <a href="/geek-streak/2016/12/18/hypercake-number">previous post</a> about hypercake numbers, we obtained a recurrence relation for the hypercake number:</p>
<script type="math/tex; mode=display">c_{m,n} = c_{m,n-1} + c_{m-1,n-1}</script>
<p>with <script type="math/tex">c_{m,1} = 2</script> and <script type="math/tex">c_{1,m} = m+1</script>.</p>
<p>To move forward, some basic understanding of generating functions<sup id="fnref:gf"><a href="#fn:gf" class="footnote">1</a></sup> and binomial coefficients<sup id="fnref:bc"><a href="#fn:bc" class="footnote">2</a></sup> is required. <!--more--> For the sake of completeness and simplifying things let us also consider the case of 0 cuts which yield <script type="math/tex">c_{m,0} = 1</script> (max cake slices with <script type="math/tex">0</script> cuts) and <script type="math/tex">c_{0,n} = 1</script> (a point cannot be sliced further). Now the new initial conditions are:</p>
<script type="math/tex; mode=display">c_{m,n} = c_{m,n-1} + c_{m-1,n-1}</script>
<p>with <script type="math/tex">c_{m,0} = 1</script> and <script type="math/tex">c_{0,n} = 1</script>.</p>
<p>One way to go about this and generally about solving recurrence relations is to use generating functions, in our case, it will lead to a generating function in two variables. Let’s suppose we have</p>
<script type="math/tex; mode=display">f(x,y)=\sum_{i,j=0}^{\infty}c_{i,j} x^i y^j</script>
<p>a formal powers series which encodes coefficients of our sequence. To find a generating function for this <script type="math/tex">f(x,y)</script> was not so straightforward<sup id="fnref:hint"><a href="#fn:hint" class="footnote">3</a></sup> for me, so I had to seek help from a good samaritan at Math SE<sup id="fnref:mse"><a href="#fn:mse" class="footnote">4</a></sup>.</p>
<script type="math/tex; mode=display">% <![CDATA[
\begin{align}
f(x,y)&=\sum_{i,j=0}^{\infty}c_{i,j} x^i y^j \\
&=c_{0,0}+\sum_{i=1}^{\infty}c_{i,0} x^i+\sum_{j=1}^{\infty}c_{0,j} y^j + \sum_{i,j=1}^{\infty}c_{i,j} x^i y^j \\
\end{align} %]]></script>
<p>We separate the the terms where either <script type="math/tex">i</script> or <script type="math/tex">j</script> equals <script type="math/tex">0</script>, since we can write recurence for <script type="math/tex">c_{i,j}</script> iff <script type="math/tex">i, j \geq 1</script>. For second and third sums above<sup id="fnref:gf1"><a href="#fn:gf1" class="footnote">5</a></sup>,</p>
<script type="math/tex; mode=display">\sum_{i=1}^{\infty}c_{i,0} x^i = x+x^2+x^3+\dots = \frac{x}{1-x}</script>
<p>So overall we have</p>
<script type="math/tex; mode=display">f(x,y) = 1+\frac{x}{1-x}+\frac{y}{1-y}+\sum_{i,j=1}^{\infty}c_{i,j} x^i y^j</script>
<p>Now for the last sum, we can apply our recurrence relation</p>
<script type="math/tex; mode=display">% <![CDATA[
\begin{align}
\sum_{i,j=1}^{\infty}c_{i,j} x^i y^j &= \sum_{i,j=1}^{\infty}(c_{i,j-1}+c_{i-1,j-1}) x^i y^j \\
&= \sum_{i,j=1}^{\infty}c_{i,j-1} x^i y^j+\sum_{i,j=1}^{\infty}c_{i-1,j-1} x^i y^j \\
&= y\sum_{i,j=1}^{\infty}c_{i,j-1} x^i y^{j-1}+xy\sum_{i,j=1}^{\infty}c_{i-1,j-1} x^{i-1} y^{j-1}\\
\end{align} %]]></script>
<p>Rearranging the limits and product in the sums so that we can obtain it in the <script type="math/tex">f(x,y)</script> form</p>
<script type="math/tex; mode=display">% <![CDATA[
\begin{align}
\sum_{i,j=1}^{\infty}c_{i,j} x^i y^j &= y\sum_{i=1,j=0}^{\infty}c_{i,j} x^i y^{j}+xy\sum_{i,j=0}^{\infty}c_{i,j} x^{i} y^{j}\\
&= y\left(\sum_{i=0,j=0}^{\infty}c_{i,j} x^i y^{j}-\sum_{j=0}^{\infty}c_{0,j}\right)+xy\sum_{i,j=0}^{\infty}c_{i,j} x^{i} y^{j}\\
&= y\left(f(x,y)-\sum_{j=0}^{\infty}c_{0,j}\right)+xyf(x,y)\\
&= y\left(f(x,y)-\frac{1}{1-y}\right)+xyf(x,y)\\
\end{align} %]]></script>
<p>Plugging in the last sum <script type="math/tex">\sum_{i,j=1}^{\infty}c_{i,j} x^i y^j</script>, in the original definition of <script type="math/tex">f(x,y)</script> we have</p>
<script type="math/tex; mode=display">% <![CDATA[
\begin{align}
f(x,y) &=1+\frac{x}{1-x}+\frac{y}{1-y}+ y\left(f(x,y)-\frac{1}{1-y}\right)+xyf(x,y)\\
(1-y-xy) f(x,y) &= 1+\frac{x}{1-x}+\frac{y}{1-y}-y\left(\frac{1}{1-y}\right)\\
f(x,y) &= \frac{1}{(1-x)(1-y-xy)}\\
\end{align} %]]></script>
<p>Now that we have the generating function of <script type="math/tex">f(x,y)</script>, it encodes all of the coefficients compactly.</p>
<p>Notice that in <script type="math/tex">f(x,y)=\frac{1}{1-x}.\frac{1}{1-y-xy}</script> the first term is</p>
<script type="math/tex; mode=display">\frac{1}{1-x}=1+x+x^2+x^3+\dots</script>
<p>Also, the second expression is a well known generating function</p>
<script type="math/tex; mode=display">\frac{1}{1-y-xy}=\frac{1}{1-y(1+x)}=\sum_{i,j=0}^{\infty}\binom{j}{i} x^i y^j</script>
<p>So we can view our function in this form as a product</p>
<script type="math/tex; mode=display">f(x,y) = (1+x+x^2+x^3+\dots) \left(\sum_{i,j=0}^{\infty}\binom{j}{i} x^i y^j\right)</script>
<p>Coming to the showdown we need to find the value of <script type="math/tex">c_{i,j}</script> which will be the same as the coefficient of <script type="math/tex">x^i y^j</script> is in the above product. It is not hard to see that it will be</p>
<script type="math/tex; mode=display">\dots + \left(\binom{j}{i}+\binom{j}{i-1}+\dots+\binom{j}{0}\right) x^i y^j + \dots</script>
<p>Therefore we have (as speculated in <a href="/geek-streak/2016/12/18/hypercake-number">previous post</a>)</p>
<script type="math/tex; mode=display">c_{i,j} =\binom{j}{i}+\binom{j}{i-1}+\dots+\binom{j}{0} =\sum_{k=0}^{i}\binom{j}{k}</script>
<p>Finally the Hypercake Numbers <script type="math/tex">c_{m,n}</script> which represents the number of maximum pieces a <script type="math/tex">m</script>D-hypercake can be sliced into with <script type="math/tex">n</script> cuts made by a slicer of dimension <script type="math/tex">\geq (m-1)</script>.</p>
<script type="math/tex; mode=display">\text{Hypercake Number:} ~~ c_{m,n} =\sum_{i=0}^{m}\binom{n}{i}</script>
<div class="footnotes">
<ol>
<li id="fn:gf">
<p><a href="http://discrete.openmathbooks.org/dmoi2/section-27.html">http://discrete.openmathbooks.org/dmoi2/section-27.html</a> <a href="#fnref:gf" class="reversefootnote">↩</a></p>
</li>
<li id="fn:bc">
<p><a href="http://discrete.openmathbooks.org/dmoi3/sec_counting-binom.html#UCc">http://discrete.openmathbooks.org/dmoi3/sec_counting-binom.html</a> <a href="#fnref:bc" class="reversefootnote">↩</a></p>
</li>
<li id="fn:hint">
<p><strong>Hint</strong>: <script type="math/tex">f(x,y)=\frac{1}{(x-1)(xy+y-1)}</script> <a href="#fnref:hint" class="reversefootnote">↩</a></p>
</li>
<li id="fn:mse">
<p><a href="https://math.stackexchange.com/a/2065193">https://math.stackexchange.com/a/2065193</a> <a href="#fnref:mse" class="reversefootnote">↩</a></p>
</li>
<li id="fn:gf1">
<p>Proof:
<script type="math/tex">% <![CDATA[
\begin{align*}
S & = 1 + x + x^2 + x^3 + \cdots\\
\underline{- xS} & \underline{\,\, = ~~ - x - x^2 - x^3 - \cdots}\\
(1-x)S & = 1
\end{align*} %]]></script> <a href="#fnref:gf1" class="reversefootnote">↩</a></p>
</li>
</ol>
</div>Shantanu MishraIn the previous post about hypercake numbers, we obtained a recurrence relation for the hypercake number: with and . To move forward, some basic understanding of generating functions1 and binomial coefficients2 is required. http://discrete.openmathbooks.org/dmoi2/section-27.html ↩ http://discrete.openmathbooks.org/dmoi3/sec_counting-binom.html ↩Hypercake Number2016-12-18T00:00:00+00:002016-12-18T07:00:00+00:00https://8hantanu.me/geek-streak/2016/12/18/hypercake-number<p>Cake number, as defined in mathematics is the number of maximum pieces a cake can be cut into with a given number of cuts. In this post, we will try to do the same with a cake (a hypercake if you may) in higher dimensions.</p>
<!--more-->
<h2 id="the-knifes-dimension">The Knife’s Dimension</h2>
<p>Before we move on to cutting cakes, let’s ponder upon the dimension of the knife. What should be the dimension of the knife when cutting a multidimensional cake?</p>
<ul>
<li>For a regular cake (<script type="math/tex">3</script>D), we use a regular knife (a <script type="math/tex">2</script>D plane).</li>
<li>For a cutting pancake (<script type="math/tex">2</script>D), we use a regular knife (<script type="math/tex">2</script>D). We can even use a razor-thin string (<script type="math/tex">1</script>D) as well to cut the pancake.</li>
<li>For cutting a candy stick(<script type="math/tex">1</script>D), we can use a knife, a garrote, and even a point (<script type="math/tex">0</script>D).</li>
</ul>
<p>Would it be safe to say that we can cut an <script type="math/tex">n</script>-dimensional cake if we have a slicer of at least <script type="math/tex">(n-1)</script> dimension?</p>
<p>Some points to think about:</p>
<ul>
<li>If using an <script type="math/tex">(n-1)</script>D slicer to cut an <script type="math/tex">n</script>D cake, the slicer must lie in the same dimension as that of the cake.</li>
<li>If using an <script type="math/tex">\geq n</script>D slicer to cut an <script type="math/tex">n</script>D cake, the slicer must not be parallel to the cake.</li>
</ul>
<p>Moving on to the main problem let <script type="math/tex">c_{m,n}</script> represents the number of maximum slices with <script type="math/tex">m</script> as the cake’s dimension and <script type="math/tex">n</script> as the number of cuts. For the sake of simplicity, we will take a slicer of <script type="math/tex">(m-1)</script> dimension to slice the <script type="math/tex">m</script>-dimensional cake.</p>
<h2 id="the-pancake-problem">The Pancake Problem</h2>
<p>Starting with a pancake (a <script type="math/tex">2</script>D cake) and a <script type="math/tex">1</script>D slicer (a line), the first line cuts the plane into <script type="math/tex">2</script> pieces. For <script type="math/tex">n > 1</script>, the <script type="math/tex">n</script>-th line crosses every earlier line and also avoids every previous line intersection and parallelism, thus increasing the number of pieces by <script type="math/tex">n</script>. So this leads to a recurring relation<sup id="fnref:1"><a href="#fn:1" class="footnote">1</a></sup>:</p>
<script type="math/tex; mode=display">c_{2,n} = c_{2,n-1} + n</script>
<p>where <script type="math/tex">c_{2,n}</script> represent the number of maximum pieces obtained with <script type="math/tex">n</script> cuts with <script type="math/tex">c_{2,1} = 2</script>.</p>
<p>Solving the above series<sup id="fnref:hint"><a href="#fn:hint" class="footnote">2</a></sup>, we obtain:</p>
<script type="math/tex; mode=display">% <![CDATA[
\begin{align}
c_{2,n} &= \frac{n^2+n+2}{2} \\
&= \binom{n}{2}+\binom{n}{1}+\binom{n}{0}
\end{align} %]]></script>
<p><strong>Note</strong>: <script type="math/tex">\binom{n}{r} = \frac{n!}{(n-r)!\times r!}</script> and for <script type="math/tex">% <![CDATA[
n < r %]]></script>, <script type="math/tex">\binom{n}{r} = 0</script></p>
<h2 id="the-cake-problem">The Cake Problem</h2>
<p>Now do the same for a regular cake (<script type="math/tex">3</script>D) with a knife (<script type="math/tex">2</script>D plane), we have to make each cut (plane) such that it is not parallel to any of the existing cuts (planes) and slices each one of them.</p>
<p>One way to visualize this would be to view the lines of intersection between all existing planes and the incoming plane. On the incoming plane, these lines will represent the cuts that yield the maximum number of slices in the plane dimension<sup id="fnref:book"><a href="#fn:book" class="footnote">3</a></sup>.</p>
<p>From the pancake problem, we can say that the plane is sliced into <script type="math/tex">c_{2,n-1}</script> pieces assuming <script type="math/tex">n-1</script> planes already exist. This represents the number of new slices created after <script type="math/tex">n</script>th cut<sup id="fnref:2"><a href="#fn:2" class="footnote">4</a></sup>.</p>
<script type="math/tex; mode=display">c_{3,n} = c_{3,n-1} + c_{2,n-1}</script>
<p>with <script type="math/tex">c_{3,1} = 2</script>. Solving above recurrence we have<sup id="fnref:hint2"><a href="#fn:hint2" class="footnote">5</a></sup>:</p>
<script type="math/tex; mode=display">c_{3,n} = \binom{n}{3}+\binom{n}{2}+\binom{n}{1}+\binom{n}{0}</script>
<p>Revisiting our pancake problem, we have:</p>
<script type="math/tex; mode=display">% <![CDATA[
\begin{align}
c_{2,n} &= c_{2,n-1} + n \\
&= c_{2,n-1} + c_{1,n-1}
\end{align} %]]></script>
<p>Here <script type="math/tex">c_{1,n-1} = n</script> is clearly the number of pieces obtained after <script type="math/tex">n-1</script> cuts on a line.</p>
<h2 id="the-hypercake-problem">The Hypercake Problem</h2>
<p>The above method can be extended to cakes in higher dimension but the visualization gets difficult. We have math to do that for us. Generalizing we can write:</p>
<script type="math/tex; mode=display">c_{m,n} = c_{m,n-1} + c_{m-1,n-1}</script>
<p>with <script type="math/tex">c_{m,1} = 2</script> and <script type="math/tex">c_{1,m} = m+1</script>.</p>
<p>Using the same method, suppose <script type="math/tex">n-1</script> <script type="math/tex">(m-1)</script>D-hyperplanes are already cutting the <script type="math/tex">m</script>-D hypercake. The incoming <script type="math/tex">(m-1)</script>D-hyperplane will intersect with the existing <script type="math/tex">(m-1)</script>D-hyperplanes in <script type="math/tex">(m-2)</script>D-hyperplanes which create <script type="math/tex">c_{m-1,n-1}</script> new slices<sup id="fnref:shots"><a href="#fn:shots" class="footnote">6</a></sup>.</p>
<p>From the previous series …</p>
<script type="math/tex; mode=display">% <![CDATA[
\begin{align}
c_{1,n} &= \binom{n}{1}+\binom{n}{0} \\ \\
c_{2,n} &= \binom{n}{2}+\binom{n}{1}+\binom{n}{0} \\ \\
c_{3,n} &= \binom{n}{3}+\binom{n}{2}+\binom{n}{1}+\binom{n}{0}
\end{align} %]]></script>
<p>… it seems that the hypercake series will unfold to<sup id="fnref:proof"><a href="#fn:proof" class="footnote">7</a></sup></p>
<script type="math/tex; mode=display">c_{m,n} = \binom{n}{m}+\binom{n}{m-1}+\dots+\binom{n}{1}+\binom{n}{0}</script>
<p>Also, when <script type="math/tex">m \to \infty</script></p>
<script type="math/tex; mode=display">\lim_{m \to \infty} c_{m,n} = 2^n</script>
<div class="footnotes">
<ol>
<li id="fn:1">
<p><a href="https://oeis.org/A000124">https://oeis.org/A000124</a> <a href="#fnref:1" class="reversefootnote">↩</a></p>
</li>
<li id="fn:hint">
<p><strong>Hint</strong>: Sum of first n positive integers = <script type="math/tex">\frac{n(n+1)}{2}</script> <a href="#fnref:hint" class="reversefootnote">↩</a></p>
</li>
<li id="fn:book">
<p>Suggested Reading: <a href="https://www.goodreads.com/book/show/722419.Hyperspace">Hyperspace - Michio Kaku</a> <a href="#fnref:book" class="reversefootnote">↩</a></p>
</li>
<li id="fn:2">
<p><a href="https://oeis.org/A000125">https://oeis.org/A000125</a> <a href="#fnref:2" class="reversefootnote">↩</a></p>
</li>
<li id="fn:hint2">
<p><strong>Hint</strong>: Sum of squares of first n positive integers = <script type="math/tex">\frac{n(n+1)(2n+1)}{6}</script> <a href="#fnref:hint2" class="reversefootnote">↩</a></p>
</li>
<li id="fn:shots">
<p>Take a shot everytime hyper is mentioned. <a href="#fnref:shots" class="reversefootnote">↩</a></p>
</li>
<li id="fn:proof">
<p>Read next: <a href="/geek-streak/2016/12/25/solving-the-cake-series">Deriving the hypercake series</a> <a href="#fnref:proof" class="reversefootnote">↩</a></p>
</li>
</ol>
</div>Shantanu MishraCake number, as defined in mathematics is the number of maximum pieces a cake can be cut into with a given number of cuts. In this post, we will try to do the same with a cake (a hypercake if you may) in higher dimensions.