Python Programming for Biology: Bioinformatics and Beyond



Download 7,75 Mb.
Pdf ko'rish
bet110/514
Sana30.12.2021
Hajmi7,75 Mb.
#91066
1   ...   106   107   108   109   110   111   112   113   ...   514
Bog'liq
[Tim J. Stevens, Wayne Boucher] Python Programming

9

Mathematics

Contents

Using Python for mathematics

The Python ‘

math


’ module

Example: mean angle

Rounding

Plotting


Linear algebra

Matrix transformations

Multi-dimensional arrays

Python multi-dimensional lists

NumPy package

Array objects

Array operations and methods

Linear algebra examples

Rotation matrices

Torsion angle



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.



NumPy  provides  support  for  basic  numerical  operations,  with  an  emphasis  on

specifying  calculations  that  operate  on  a  whole  array  of  numbers  at  once.  As  will  be

discussed  below,  its  operations  include  functionality  for  random  numbers,  linear  algebra

and Fourier transforms. It is implemented in C underneath,

1

and thus is quick to run, but



can naturally be accessed in Python. NumPy is relatively easy to use because you are still

working  with  Python  commands,  but  the  way  that  some  things  work,  especially  how  to

think  about  numeric  array  operations,  can  take  some  learning.  For  serious  linear  algebra

work in Python, NumPy is the method of choice. There is another closely related package,

called  SciPy

2

 (Scientific  Python),  which  adds  some  higher-level  numerical  capabilities,



such as integration, optimisation and signal processing. NumPy and SciPy are not part of

the  standard  Python  software  release,  so  require  a  separate  download  and  installation,

although  modern  download  managers  ought  to  make  this  fairly  easy  to  do.  See

http://www.cambridge.org/pythonforbiology

 for  details  of  where  to  download  SciPy  and

NumPy. In some sense these packages could be deemed to be the Pythonic answer to the

analogous capabilities in, for example, the MATLAB system.

3


Download 7,75 Mb.

Do'stlaringiz bilan baham:
1   ...   106   107   108   109   110   111   112   113   ...   514




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish