Skip to main content Accessibility help
×
Hostname: page-component-848d4c4894-pftt2 Total loading time: 0 Render date: 2024-05-01T08:50:29.827Z Has data issue: false hasContentIssue false

9 - Mathematics

Published online by Cambridge University Press:  05 February 2015

Tim J. Stevens
Affiliation:
MRC Laboratory of Molecular Biology, Cambridge
Wayne Boucher
Affiliation:
University of Cambridge
Get access

Summary

Using Python for mathematics

Given that Python is an interpreted programming language, rather than a fast compiled language, many people do not consider it for writing programs that involve extensive numerical work. While Python programs are certainly slower to execute than the equivalent written in something like C or FORTRAN, mathematical functionality certainly exists in Python and has the inherent advantages of the language; it is easy for people to use and conveniently links to other helpful data structures. Of course speed of calculation may not be so important, for a scientific investigation it may not matter if something takes 1 second or 0.1 second to run. Fortunately, computers get faster and the Python interpreter becomes improved, so you can do quite a bit of numerical work without concern. However, if calculation speed really is important in a given situation then there are a few things you can do to make things faster while still keeping the convenience of Python. For example, you can write code in C, a very efficient numerical language, and use it from within Python (this is called a C extension), effectively extending the vocabulary of the interpreted language with speedy subroutines. More recently the language Cython has helped make C extensions very easy to write. Cython is a Python-like language, and virtually all Python programs can be interpreted by it, without alteration, but the language ultimately generates C code that can be compiled. Cython can be used to call fast library code written in pure C, and can incorporate a mixture of Python and C data structures in the same code; although less flexible, the C data structures are very efficient. Writing C extensions and Cython modules is discussed in Chapter 27.

Python includes standard arithmetic operations as part of the core functionality: add, multiply etc. There is an additional module, math, which always comes packaged with Python and which provides further numerical functionality: logarithms, trigonometry etc. For numerical calculations that are not especially intensive, the core functionality and the math module will often suffice. There has been a history of trying to provide modules for quick numerical algorithms in Python. The first attempt, begun in 1995, was called Numeric, and the second attempt was called Numarray. These two are now deemed to be obsolete, but the third attempt, begun in 2005, is called NumPy (http://numpy.scipy.org/), incorporates elements from the earlier attempts and will hopefully last longer.

Type
Chapter
Information
Python Programming for Biology
Bioinformatics and Beyond
, pp. 137 - 159
Publisher: Cambridge University Press
Print publication year: 2015

Access options

Get access to the full version of this content by using one of the access options below. (Log in options will check for institutional or personal access. Content may require purchase if you do not have access.)

Save book to Kindle

To save this book to your Kindle, first ensure coreplatform@cambridge.org is added to your Approved Personal Document E-mail List under your Personal Document Settings on the Manage Your Content and Devices page of your Amazon account. Then enter the ‘name’ part of your Kindle email address below. Find out more about saving to your Kindle.

Note you can select to save to either the @free.kindle.com or @kindle.com variations. ‘@free.kindle.com’ emails are free but can only be saved to your device when it is connected to wi-fi. ‘@kindle.com’ emails can be delivered even when you are not connected to wi-fi, but note that service fees apply.

Find out more about the Kindle Personal Document Service.

  • Mathematics
  • Tim J. Stevens, MRC Laboratory of Molecular Biology, Cambridge, Wayne Boucher, University of Cambridge
  • Book: Python Programming for Biology
  • Online publication: 05 February 2015
  • Chapter DOI: https://doi.org/10.1017/CBO9780511843556.010
Available formats
×

Save book to Dropbox

To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Dropbox.

  • Mathematics
  • Tim J. Stevens, MRC Laboratory of Molecular Biology, Cambridge, Wayne Boucher, University of Cambridge
  • Book: Python Programming for Biology
  • Online publication: 05 February 2015
  • Chapter DOI: https://doi.org/10.1017/CBO9780511843556.010
Available formats
×

Save book to Google Drive

To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Google Drive.

  • Mathematics
  • Tim J. Stevens, MRC Laboratory of Molecular Biology, Cambridge, Wayne Boucher, University of Cambridge
  • Book: Python Programming for Biology
  • Online publication: 05 February 2015
  • Chapter DOI: https://doi.org/10.1017/CBO9780511843556.010
Available formats
×