Top-rated recipes tagged "meta:requires=math"http://code.activestate.com/recipes/tags/meta:requires=math/top/2016-08-07T22:02:10-07:00ActiveState Code RecipesDirt simple map/reduce (Python) 2011-05-15T16:46:55-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577676-dirt-simple-mapreduce/ <p style="color: grey"> Python recipe 577676 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/analysis/">analysis</a>, <a href="/recipes/tags/crosstab/">crosstab</a>, <a href="/recipes/tags/functional/">functional</a>, <a href="/recipes/tags/map_reduce/">map_reduce</a>, <a href="/recipes/tags/pivot_table/">pivot_table</a>, <a href="/recipes/tags/statistics/">statistics</a>). Revision 9. </p> <p>Simple tool for analyzing datasets.</p> Binary floating point summation accurate to full precision (Python) 2009-03-28T23:32:08-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/393090-binary-floating-point-summation-accurate-to-full-p/ <p style="color: grey"> Python recipe 393090 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). Revision 5. </p> <p>Completely eliminates rounding errors and loss of significance due to catastrophic cancellation during summation. Achieves exactness by keeping full precision intermediate subtotals. Offers three alternative approaches, each using a different technique to store exact subtotals.</p> Public Key Encryption (RSA) (Python) 2012-05-12T23:34:22-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577737-public-key-encryption-rsa/ <p style="color: grey"> Python recipe 577737 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/encryption/">encryption</a>, <a href="/recipes/tags/inverse/">inverse</a>, <a href="/recipes/tags/multiplicative/">multiplicative</a>, <a href="/recipes/tags/primality_testing/">primality_testing</a>, <a href="/recipes/tags/primes/">primes</a>, <a href="/recipes/tags/publickey/">publickey</a>, <a href="/recipes/tags/rsa/">rsa</a>). Revision 2. </p> <p>Simple code to create and use public/private keypairs. Accompanied by a rudimentary encoder.</p> Efficient Running Median using an Indexable Skiplist (Python) 2010-02-07T14:40:03-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/576930-efficient-running-median-using-an-indexable-skipli/ <p style="color: grey"> Python recipe 576930 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/advanced_data_structure/">advanced_data_structure</a>, <a href="/recipes/tags/container/">container</a>, <a href="/recipes/tags/median/">median</a>, <a href="/recipes/tags/new_algorithm/">new_algorithm</a>, <a href="/recipes/tags/skiplist/">skiplist</a>, <a href="/recipes/tags/sliding_window/">sliding_window</a>). Revision 10. </p> <p>Maintains sorted data as new elements are added and old one removed as a sliding window advances over a stream of data. Also gives fast indexed access to value. Running time per median update is proportional to the log of the window size.</p> Simple Web Crawler (Python) 2011-01-31T21:57:58-08:00James Millshttp://code.activestate.com/recipes/users/4167757/http://code.activestate.com/recipes/576551-simple-web-crawler/ <p style="color: grey"> Python recipe 576551 by <a href="/recipes/users/4167757/">James Mills</a> (<a href="/recipes/tags/crawler/">crawler</a>, <a href="/recipes/tags/network/">network</a>, <a href="/recipes/tags/parsing/">parsing</a>, <a href="/recipes/tags/web/">web</a>). Revision 2. </p> <p>NOTE: This recipe has been updated with suggested improvements since the last revision.</p> <p>This is a simple web crawler I wrote to test websites and links. It will traverse all links found to any given depth.</p> <p>See --help for usage.</p> <p>I'm posting this recipe as this kind of problem has been asked on the Python Mailing List a number of times... I thought I'd share my simple little implementation based on the standard library and BeautifulSoup.</p> <p>--JamesMills</p> Calculating the distance between zip codes (Python) 2006-04-25T20:40:00-07:00Kevin Ryanhttp://code.activestate.com/recipes/users/1654599/http://code.activestate.com/recipes/393241-calculating-the-distance-between-zip-codes/ <p style="color: grey"> Python recipe 393241 by <a href="/recipes/users/1654599/">Kevin Ryan</a> (<a href="/recipes/tags/web/">web</a>). Revision 2. </p> <p>I came across the mention of a formula labeled "The Great Circle Distance Formula" that purported to calculate the distance between any two points on the earth given their longitude and latitude points (the reference was in a Linux Magazine article). So, I looked up some information and cooked up a Python version of the calculation. There are references in the code where you can obtain approximate zip code data for free (e.g., if you wanted to enhance your website by adding a "Search within x mi's" feature), as well as references to the GCDF if you have further interest. Enjoy!</p> <p>04/25/2006 update: I've decided to update this recipe with an object oriented bent where the information is cached once the object is instantiated. I've also added command line access to automatically download the zipcode file from the census website (just use 'python zips.py -d' and it will download a copy to your harddrive under 'zips.txt'). Lastly, I've added some unit testing so that if any future changes are made this will automatically run and tell me if anything pops out as wrong.</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> Simple Back-propagation Neural Network in Python source code (Python) 2012-05-30T17:09:49-07:00David Adlerhttp://code.activestate.com/recipes/users/4182015/http://code.activestate.com/recipes/578148-simple-back-propagation-neural-network-in-python-s/ <p style="color: grey"> Python recipe 578148 by <a href="/recipes/users/4182015/">David Adler</a> (<a href="/recipes/tags/back/">back</a>, <a href="/recipes/tags/back_propagation/">back_propagation</a>, <a href="/recipes/tags/neural/">neural</a>, <a href="/recipes/tags/neural_network/">neural_network</a>, <a href="/recipes/tags/propagation/">propagation</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>This is a slightly different version of this <a href="http://arctrix.com/nas/python/bpnn.py" rel="nofollow">http://arctrix.com/nas/python/bpnn.py</a></p> Comparing two images (Python) 2011-04-03T11:13:24-07:00Charlie Clarkhttp://code.activestate.com/recipes/users/4171013/http://code.activestate.com/recipes/577630-comparing-two-images/ <p style="color: grey"> Python recipe 577630 by <a href="/recipes/users/4171013/">Charlie Clark</a> (<a href="/recipes/tags/compare/">compare</a>, <a href="/recipes/tags/images/">images</a>). </p> <p>Compare two images using the root mean squared analysis. A result close to 0 means a good match.</p> Archimedes method for calculating PI (Python) 2009-12-09T15:13:09-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/576981-archimedes-method-for-calculating-pi/ <p style="color: grey"> Python recipe 576981 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/pi/">pi</a>). </p> <p>Archimedes method for calculating PI.</p> Miller-Rabin Primality Test (Python) 2007-04-07T16:11:50-07:00Dite Ditehttp://code.activestate.com/recipes/users/4047667/http://code.activestate.com/recipes/511459-miller-rabin-primality-test/ <p style="color: grey"> Python recipe 511459 by <a href="/recipes/users/4047667/">Dite Dite</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). </p> <p>Very simple implementation of Miller-Rabin Primality Test with Tkinter</p> Property decorator for python 2.4 (Python) 2005-04-25T23:32:11-07:00George Sakkishttp://code.activestate.com/recipes/users/2591466/http://code.activestate.com/recipes/410698-property-decorator-for-python-24/ <p style="color: grey"> Python recipe 410698 by <a href="/recipes/users/2591466/">George Sakkis</a> (<a href="/recipes/tags/oop/">oop</a>). Revision 3. </p> <p>This recipe refines an older recipe on creating class properties ( <a href="http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/205183" rel="nofollow">http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/205183</a>). The refinement consists of: - Using a decorator (introduced in python 2.4) to "declare" a function in class scope as property. - Using a trace function to capture the locals() of the decorated function, instead of requiring the latter to return locals(), as in the older recipe.</p> Cache decorator in python 2.4 (Python) 2004-11-01T19:11:21-08:00George Sakkishttp://code.activestate.com/recipes/users/2591466/http://code.activestate.com/recipes/325205-cache-decorator-in-python-24/ <p style="color: grey"> Python recipe 325205 by <a href="/recipes/users/2591466/">George Sakkis</a> (<a href="/recipes/tags/oop/">oop</a>). Revision 2. </p> <p>The latest version of Python introduced a new language feature, function and method decorators (PEP 318, <a href="http://www.python.org/peps/pep-0318.html" rel="nofollow">http://www.python.org/peps/pep-0318.html</a>). This recipe show a common callable transformation that can benefit from the new syntax, often referred to as Memoization pattern.</p> Table indentation (Python) 2004-02-14T03:50:24-08:00George Sakkishttp://code.activestate.com/recipes/users/2591466/http://code.activestate.com/recipes/267662-table-indentation/ <p style="color: grey"> Python recipe 267662 by <a href="/recipes/users/2591466/">George Sakkis</a> (<a href="/recipes/tags/text/">text</a>). Revision 7. </p> <p>A function for pretty-printing a table.</p> Genetic Algorithm Neural Network in Python Source Code (Python) 2012-08-16T16:31:12-07:00David Adlerhttp://code.activestate.com/recipes/users/4182015/http://code.activestate.com/recipes/578241-genetic-algorithm-neural-network-in-python-source-/ <p style="color: grey"> Python recipe 578241 by <a href="/recipes/users/4182015/">David Adler</a> (<a href="/recipes/tags/artificial_intelligence/">artificial_intelligence</a>, <a href="/recipes/tags/neural/">neural</a>, <a href="/recipes/tags/neural_network/">neural_network</a>). </p> <p>A simple genetic algorithm neural network. </p> Bitset (Python) 2009-05-04T16:37:02-07:00geremy condrahttp://code.activestate.com/recipes/users/4170000/http://code.activestate.com/recipes/576738-bitset/ <p style="color: grey"> Python recipe 576738 by <a href="/recipes/users/4170000/">geremy condra</a> (<a href="/recipes/tags/bits/">bits</a>, <a href="/recipes/tags/bytes/">bytes</a>). </p> <p>This is a simple Bitset type for Python. It implements the Sequence interface plus __setitem__, the set operations, and string and integer conversions.</p> Arbitrary precision binary floating point arithmetic (Python) 2005-05-23T22:10:34-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/412428-arbitrary-precision-binary-floating-point-arithmet/ <p style="color: grey"> Python recipe 412428 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). Revision 2. </p> <p>Achieves exactness by manipulating values stored as a long integer mantissa with an integer exponent (base two).</p> A Python-based descriptive statistical analysis tool. (Python) 2005-04-18T23:52:25-07:00Chad J. Schroederhttp://code.activestate.com/recipes/users/1760491/http://code.activestate.com/recipes/409413-a-python-based-descriptive-statistical-analysis-to/ <p style="color: grey"> Python recipe 409413 by <a href="/recipes/users/1760491/">Chad J. Schroeder</a> (<a href="/recipes/tags/programs/">programs</a>). Revision 2. </p> <p>A Python module implementing a class which can be used for computing numerical statistics for a given data set.</p> A Sinus Plasma, using Pygame (Python) 2004-11-07T23:38:22-08:00S Whttp://code.activestate.com/recipes/users/1759688/http://code.activestate.com/recipes/334696-a-sinus-plasma-using-pygame/ <p style="color: grey"> Python recipe 334696 by <a href="/recipes/users/1759688/">S W</a> (<a href="/recipes/tags/graphics/">graphics</a>). </p> <p>An old-school demo effect, using sine wave interference patterns. Entertaining for at least a few minutes :)</p> <p>Modify the freq variable for different patterns.</p> Multi-segment Fast File Downloader (Python) 2012-12-18T14:57:32-08:00Nelson Rushhttp://code.activestate.com/recipes/users/144881/http://code.activestate.com/recipes/114217-multi-segment-fast-file-downloader/ <p style="color: grey"> Python recipe 114217 by <a href="/recipes/users/144881/">Nelson Rush</a> (<a href="/recipes/tags/web/">web</a>). Revision 2. </p> <p>A fast multi-part file downloader ala FlashGet, GetRight, Gozilla, etc. Currently only supports HTTP but FTP wouldn't be hard. Use this instead of wget. ;-)</p>