Popular Python recipes tagged "math"http://code.activestate.com/recipes/langs/python/tags/math/2016-12-13T05:05:52-08:00ActiveState Code RecipesHamming 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> 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> Roman Numeral Converter (Python) 2016-12-13T05:05:52-08:00Brandon Martinhttp://code.activestate.com/recipes/users/4194238/http://code.activestate.com/recipes/580731-roman-numeral-converter/ <p style="color: grey"> Python recipe 580731 by <a href="/recipes/users/4194238/">Brandon Martin</a> (<a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/numbers/">numbers</a>, <a href="/recipes/tags/numerals/">numerals</a>, <a href="/recipes/tags/roman/">roman</a>). </p> <p>Provides two functions: romanToNumber - Converts a string of Roman Numerals (I,V,X,L,C,D,M) into an integer. (ignores formatting, will return best guess if improperly formatted) numberToRoman - Converts an integer into a string of Roman Numerals</p> Image Projection Onto Sphere (Python) 2016-07-31T22:50:28-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/580695-image-projection-onto-sphere/ <p style="color: grey"> Python recipe 580695 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/images/">images</a>, <a href="/recipes/tags/image_processing/">image_processing</a>, <a href="/recipes/tags/imaging/">imaging</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>, <a href="/recipes/tags/pil/">pil</a>). </p> <p>Image projection onto sphere using Equirectangular map projection.</p> <p>Download the test image from the Wikipedia page!</p> Auto differentiation (Python) 2016-08-07T22:02:10-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/580610-auto-differentiation/ <p style="color: grey"> Python recipe 580610 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/autodifferentiation/">autodifferentiation</a>, <a href="/recipes/tags/calculus/">calculus</a>, <a href="/recipes/tags/descent/">descent</a>, <a href="/recipes/tags/gradient/">gradient</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/optimization/">optimization</a>, <a href="/recipes/tags/vector/">vector</a>). Revision 5. </p> <p>Directly computes derivatives from ordinary Python functions using auto differentiation. The technique directly computes the desired derivatives to full precision without resorting to symbolic math and without making estimates bases on numerical methods.</p> <p>The module provides a Num class for "dual" numbers that performs both regular floating point math on a value and its derivative at the same time. In addition, the module provides drop-in substitutes for most of the functions in the math module. There are also tools for partial derivatives, directional derivatives, gradients of scalar fields, and the curl and divergence of vector fields.</p> Projectile Motion (Python) 2016-04-08T01:42:01-07:00Tage Burnetthttp://code.activestate.com/recipes/users/4193902/http://code.activestate.com/recipes/580638-projectile-motion/ <p style="color: grey"> Python recipe 580638 by <a href="/recipes/users/4193902/">Tage Burnett</a> (<a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/physics/">physics</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>This program calculates the characteristics of a projectile in a vacuum at several different locations in our solar system.</p> Random Planet Generator Using Plasma Fractal (Python) 2016-01-25T23:05:25-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/580495-random-planet-generator-using-plasma-fractal/ <p style="color: grey"> Python recipe 580495 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/image/">image</a>, <a href="/recipes/tags/images/">images</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>). </p> <p>Random Planet Generator using 3D Plasma Fractal and Voxel-based Ray Tracing for rendering.</p> <p>Instead of generating 2D Plasma Fractal and projecting onto sphere, it generates 3D Plasma Fractal (cube) and cuts sphere from it.</p> Infix Expression Evaluation (Python) 2015-11-08T05:08:26-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/579123-infix-expression-evaluation/ <p style="color: grey"> Python recipe 579123 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>Infix expression evaluation using two stacks.</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> Quasicrystal Pattern Generator (Python) 2015-08-08T20:50:23-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/579094-quasicrystal-pattern-generator/ <p style="color: grey"> Python recipe 579094 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/image/">image</a>, <a href="/recipes/tags/images/">images</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>). </p> <p>Quasicrystal Pattern Generator</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> Get the inversion number of a permutation (Python) 2016-02-21T06:00:46-08:00Samuel James Ericksonhttp://code.activestate.com/recipes/users/4187478/http://code.activestate.com/recipes/579051-get-the-inversion-number-of-a-permutation/ <p style="color: grey"> Python recipe 579051 by <a href="/recipes/users/4187478/">Samuel James Erickson</a> (<a href="/recipes/tags/combinatorics/">combinatorics</a>, <a href="/recipes/tags/discrete/">discrete</a>, <a href="/recipes/tags/inversion/">inversion</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/permutations/">permutations</a>). Revision 2. </p> <p>This function takes a permutation in the form of a list and returns the number of inversions in the permutation. </p> 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> Mandelbrot Fractal image output to ppm file (Python) 2015-01-17T19:21:47-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/579006-mandelbrot-fractal-image-output-to-ppm-file/ <p style="color: grey"> Python recipe 579006 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/fractal/">fractal</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>). </p> <p>It creates image output without using any external library.</p> <p>PPM image files can be viewed and converted to other image file formats using many applications, GIMP or IrfanView for example.</p> Garden Requirements Calculator (Python) 2014-09-17T22:04:36-07:00Ethan Hannhttp://code.activestate.com/recipes/users/4190746/http://code.activestate.com/recipes/578935-garden-requirements-calculator/ <p style="color: grey"> Python recipe 578935 by <a href="/recipes/users/4190746/">Ethan Hann</a> (<a href="/recipes/tags/calculator/">calculator</a>, <a href="/recipes/tags/garden/">garden</a>, <a href="/recipes/tags/math/">math</a>). </p> <p>This program takes input from the user and determines the amount of gardening materials needed for a garden. The garden is a perfect square. The four outer flowerbeds are congruent semicircles and the central flowerbed is a perfect circle. Everything else inside the garden is considered fill: stone, mulch, or other fill material.</p> 2D Fluid Simulation using FHP LGCA (Python) 2014-08-18T05:01:04-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/578924-2d-fluid-simulation-using-fhp-lgca/ <p style="color: grey"> Python recipe 578924 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/physics/">physics</a>, <a href="/recipes/tags/simulation/">simulation</a>). </p> <p>2D Fluid Simulation using FHP LGCA (Lattice Gas Cellular Automata)</p> <p>Simulates fluid flow in a circular channel.</p> <p>It works really slow but I think it can be a lot faster if it modified for NumPy and possibly Py2Exe.</p> <p>But my main goal was to provide easy to understand code (not performance) anyway. </p> Perfect Square Checker (Python) 2014-09-10T02:21:27-07:00Ethan Hannhttp://code.activestate.com/recipes/users/4190746/http://code.activestate.com/recipes/578931-perfect-square-checker/ <p style="color: grey"> Python recipe 578931 by <a href="/recipes/users/4190746/">Ethan Hann</a> (<a href="/recipes/tags/check/">check</a>, <a href="/recipes/tags/checker/">checker</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>, <a href="/recipes/tags/root/">root</a>, <a href="/recipes/tags/square/">square</a>). </p> <p>This program will take input from the user and either check if a number is a perfect square or square a number, depending on user's choice.</p> A 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> 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> Factorial (Python) 2014-03-08T13:09:41-08:00Johnhttp://code.activestate.com/recipes/users/4189390/http://code.activestate.com/recipes/578848-factorial/ <p style="color: grey"> Python recipe 578848 by <a href="/recipes/users/4189390/">John</a> (<a href="/recipes/tags/factorial/">factorial</a>, <a href="/recipes/tags/loop/">loop</a>, <a href="/recipes/tags/loops/">loops</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/range/">range</a>, <a href="/recipes/tags/simple/">simple</a>). </p> <p>Just a simple factorial program I made in Python 3.</p>