Most viewed recipes tagged "mathematics"http://code.activestate.com/recipes/tags/mathematics/views/2016-07-23T07:58:14-07:00ActiveState Code RecipesA simple Matrix class (Python) 2012-05-14T13:34:31-07:00Anand B Pillaihttp://code.activestate.com/recipes/users/4169530/http://code.activestate.com/recipes/578131-a-simple-matrix-class/ <p style="color: grey"> Python recipe 578131 by <a href="/recipes/users/4169530/">Anand B Pillai</a> (<a href="/recipes/tags/algebra/">algebra</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>, <a href="/recipes/tags/matrix/">matrix</a>). Revision 3. </p> <p>A simple class in Python representing a Matrix with basic operations, operator overloading and class factory methods to make Matrices from different sources.</p> Bisection Method in Python (Python) 2013-01-16T16:37:33-08:00Captain DeadBoneshttp://code.activestate.com/recipes/users/4184772/http://code.activestate.com/recipes/578417-bisection-method-in-python/ <p style="color: grey"> Python recipe 578417 by <a href="/recipes/users/4184772/">Captain DeadBones</a> (<a href="/recipes/tags/bisect/">bisect</a>, <a href="/recipes/tags/mathematics/">mathematics</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>This is a quick way to do bisection method in python. I wrote his code as part of an article, <a href="http://thelivingpearl.com/2013/01/15/the-easy-way-to-solve-equations-in-python/">How to solve equations using python</a></p> Shannon Entropy Calculation (Python) 2010-11-29T08:37:34-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577476-shannon-entropy-calculation/ <p style="color: grey"> Python recipe 577476 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>). </p> <p>Shannon entropy of a string indicates minimum average number of bits per symbol required for encoding (compressing) the string.</p> ODE Solver using Euler Method (Python) 2011-04-10T00:30:58-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577647-ode-solver-using-euler-method/ <p style="color: grey"> Python recipe 577647 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>). </p> <p>First and Second Order Ordinary Differential Equation (ODE) Solver using Euler Method.</p> Integer square root function (Python) 2011-08-04T05:29:16-07:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/577821-integer-square-root-function/ <p style="color: grey"> Python recipe 577821 by <a href="/recipes/users/4172944/">Steven D'Aprano</a> (<a href="/recipes/tags/integer/">integer</a>, <a href="/recipes/tags/isqrt/">isqrt</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>, <a href="/recipes/tags/maths/">maths</a>, <a href="/recipes/tags/root/">root</a>, <a href="/recipes/tags/square/">square</a>). </p> <p>The <em>integer square root</em> function, or isqrt, is equivalent to floor(sqrt(x)) for non-negative x. For small x, the most convenient way to calculate isqrt is by calling int(x**0.5) or int(math.sqrt(x)), but if x is a large enough integer, the sqrt calculation overflows.</p> <p>You can calculate the isqrt without any floating point maths, using just pure integer maths, allowing the function to operate with numbers far larger than possible with floats:</p> <pre class="prettyprint"><code>&gt;&gt;&gt; n = 1234567*(10**1000) &gt;&gt;&gt; n2 = n*n &gt;&gt;&gt; math.sqrt(n2) Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; OverflowError: long int too large to convert to float &gt;&gt;&gt; isqrt(n2) == n True </code></pre> Discrete Fourier Transform (Python) 2014-12-27T21:43:53-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/578994-discrete-fourier-transform/ <p style="color: grey"> Python recipe 578994 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>, <a href="/recipes/tags/signal/">signal</a>, <a href="/recipes/tags/signal_processing/">signal_processing</a>). </p> <p>Discrete Fourier Transform and Inverse Discrete Fourier Transform</p> <p>To test, it creates an input signal using a Sine wave that has known frequency, amplitude, phase. Later it calculates DFT of the input signal and finds its frequency, amplitude, phase to compare.</p> Hamming Error Correction Code (Python) 2016-07-23T07:58:14-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/580691-hamming-error-correction-code/ <p style="color: grey"> Python recipe 580691 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/encoding/">encoding</a>, <a href="/recipes/tags/engineering/">engineering</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>, <a href="/recipes/tags/utility/">utility</a>). </p> <p>Hamming(7,4) Error Correction Code (ECC).</p> Newton's Method to Solve Equations in Python (Python) 2013-01-16T16:40:55-08:00Captain DeadBoneshttp://code.activestate.com/recipes/users/4184772/http://code.activestate.com/recipes/578419-newtons-method-to-solve-equations-in-python/ <p style="color: grey"> Python recipe 578419 by <a href="/recipes/users/4184772/">Captain DeadBones</a> (<a href="/recipes/tags/mathematics/">mathematics</a>, <a href="/recipes/tags/newtons_method/">newtons_method</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>This is how you would use Newton's method to solve equations. For more information about solving equations in python checkout <a href="http://thelivingpearl.com/2013/01/15/the-easy-way-to-solve-equations-in-python/">How to solve equations using python</a>.</p> Simple Infix Expression Evaluation (Python) 2015-11-07T18:44:05-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/579122-simple-infix-expression-evaluation/ <p style="color: grey"> Python recipe 579122 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>, <a href="/recipes/tags/stack/">stack</a>). </p> <p>Simple infix expression evaluation using a stack.</p> 2D Discrete Fourier Transform (Python) 2015-01-03T03:39:34-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/578997-2d-discrete-fourier-transform/ <p style="color: grey"> Python recipe 578997 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/image/">image</a>, <a href="/recipes/tags/images/">images</a>, <a href="/recipes/tags/image_processing/">image_processing</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>, <a href="/recipes/tags/signal_processing/">signal_processing</a>). </p> <p>2D Discrete Fourier Transform (DFT) and its inverse.</p> <p>Calculates 2D DFT of an image and recreates the image using inverse 2D DFT.</p> <p>Computation is slow so only suitable for thumbnail size images.</p> Area of Polygon using Shoelace formula (Python) 2012-02-18T16:34:33-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/578047-area-of-polygon-using-shoelace-formula/ <p style="color: grey"> Python recipe 578047 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>). </p> <p>Area of Polygon using Shoelace formula.</p> Spring-Mass System Simulation (Python) 2011-05-02T01:59:45-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577681-spring-mass-system-simulation/ <p style="color: grey"> Python recipe 577681 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/chaos/">chaos</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>, <a href="/recipes/tags/physics/">physics</a>, <a href="/recipes/tags/simulation/">simulation</a>). </p> <p>It simulates a damped spring-mass system driven by sinusoidal force.</p> Decimal to Binary Conversion (Python) 2010-12-05T22:26:29-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577488-decimal-to-binary-conversion/ <p style="color: grey"> Python recipe 577488 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>). </p> <p>Unlike bin() function of Python, it can convert floating-point numbers also.</p> Automated Stock Market Trading Simulation (Python) 2014-05-16T02:40:28-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/578881-automated-stock-market-trading-simulation/ <p style="color: grey"> Python recipe 578881 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>, <a href="/recipes/tags/simulation/">simulation</a>, <a href="/recipes/tags/stock/">stock</a>, <a href="/recipes/tags/stock_market/">stock_market</a>). </p> <p>It simulates an automated trading strategy against a simulated stock.</p> <p>I think the results are interesting. It seems number of wins are always higher than number of losses but average amount of loss is also always higher than average amount of win! </p> Convolving Gaussian (Python) 2009-05-19T05:23:48-07:00alexander bakerhttp://code.activestate.com/recipes/users/4166679/http://code.activestate.com/recipes/576764-convolving-gaussian/ <p style="color: grey"> Python recipe 576764 by <a href="/recipes/users/4166679/">alexander baker</a> (<a href="/recipes/tags/image/">image</a>, <a href="/recipes/tags/mathematics/">mathematics</a>). </p> <p>Convolving Gaussian</p> Mandelbrot Fractal using Tkinter (Python) 2013-07-06T23:53:20-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/578590-mandelbrot-fractal-using-tkinter/ <p style="color: grey"> Python recipe 578590 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/fractal/">fractal</a>, <a href="/recipes/tags/graphics/">graphics</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>, <a href="/recipes/tags/tkinter/">tkinter</a>). </p> <p>Mandelbrot Fractal using Tkinter</p> Polynomial Interpolation using Lagrange Polynomial (Python) 2010-11-28T03:03:36-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577474-polynomial-interpolation-using-lagrange-polynomial/ <p style="color: grey"> Python recipe 577474 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/graphics/">graphics</a>, <a href="/recipes/tags/image/">image</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>). </p> <p>Polynomial Interpolation (curve-fitting) using Lagrange Polynomial.</p> Bezier Curve using De Casteljau algorithm (Python) 2011-11-25T02:33:54-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577961-bezier-curve-using-de-casteljau-algorithm/ <p style="color: grey"> Python recipe 577961 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/graphics/">graphics</a>, <a href="/recipes/tags/image/">image</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>). </p> <p>Draws a random Bezier Curve using De Casteljau algorithm.</p> Secant Method of Solving Equtions in Python (Python) 2013-01-16T16:42:26-08:00Captain DeadBoneshttp://code.activestate.com/recipes/users/4184772/http://code.activestate.com/recipes/578420-secant-method-of-solving-equtions-in-python/ <p style="color: grey"> Python recipe 578420 by <a href="/recipes/users/4184772/">Captain DeadBones</a> (<a href="/recipes/tags/mathematics/">mathematics</a>, <a href="/recipes/tags/pytho/">pytho</a>, <a href="/recipes/tags/secant/">secant</a>). </p> <p>Solving equations using the Newton's method without taking derivatives. This code war written for the article <a href="http://thelivingpearl.com/2013/01/15/the-easy-way-to-solve-equations-in-python/">How to solve equations using python</a>.</p> Reaction Diffusion Simulation (Python) 2015-10-16T19:52:02-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/579114-reaction-diffusion-simulation/ <p style="color: grey"> Python recipe 579114 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/chaos/">chaos</a>, <a href="/recipes/tags/fractal/">fractal</a>, <a href="/recipes/tags/graphics/">graphics</a>, <a href="/recipes/tags/image/">image</a>, <a href="/recipes/tags/images/">images</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>, <a href="/recipes/tags/physics/">physics</a>, <a href="/recipes/tags/simulation/">simulation</a>). Revision 2. </p> <p>Reaction-Diffusion Simulation using Gray-Scott Model.</p>