Python Programming for Biology: Bioinformatics and Beyond



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

Example: mean angle

Below  we  give  a  more  involved  example  that  uses  the  math  module  inside  a  function

definition. This function will be used later on in this book, where it is used to calculate the

average angle value from a list of input angles. Later, in context, it will be used to look at

the angles of chemical bonds that come from alternative molecular shapes (conformations)

to give the average bond angle. Because angles are a circular measure (i.e. 360° equals 0°)

we  cannot  take  the  simple  mean  value  of  the  numbers.  Instead  we  find  the  average  sine

and cosine of the angles, which oscillate between plus and minus one and so don’t have a

problem when angles go past a full turn or become negative. Both the sine and cosine are

required because each function on its own is not unique to a single angle, e.g. the sine of

45° equals the sine of 135°. Accordingly, we use the averages of both sine and cosine to

generate  the  average  angle  via  a  special  inverse  tangent  function,  and  naturally  the

mathematical operations come from the math module:

from math import sin, cos, degrees, radians, atan2

def meanAngle(angles, inDegrees=True):

sumCos = 0.0




sumSin = 0.0

for angle in angles:

if inDegrees:

angle = radians(angle)

sumCos += cos(angle)

sumSin += sin(angle)

N = len(angles)

meanAngle = atan2(sumSin/N, sumCos/N)

if inDegrees:

meanAngle = degrees(meanAngle)

return meanAngle

The inverse tangent function used is specifically atan2; while a normal inverse tangent

(atan)  operates  on  a  single  number  and  gives  an  angle  between  −90°  and  +90°,  this

function uses both sine and cosine values to calculate the inverse tangent and also which

quadrant  of  the  circle  it  lies  in.  Thus  the  result  is  an  angle  between  −180°  and  +180°.

Finally,  note  how  all  calculations  are  done  in  radians,  so  we  must  explicitly  convert  the

input and output when working with values specified in units of degrees, signalled by the

input argument inDegrees being True.




Download 7,75 Mb.

Do'stlaringiz bilan baham:
1   ...   108   109   110   111   112   113   114   115   ...   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