unit_math.py 4.46 KB
Newer Older
1
2
3
4
5
#!/bin/env python
"""
Module simtk.unit.math

Arithmetic methods on Quantities and Units
6
7
8
9
10
11
12
13
14
15

This is part of the OpenMM molecular simulation toolkit originating from
Simbios, the NIH National Center for Physics-Based Simulation of
Biological Structures at Stanford, funded under the NIH Roadmap for
Medical Research, grant U54 GM072970. See https://simtk.org.

Portions copyright (c) 2012 Stanford University and the Authors.
Authors: Christopher M. Bruns
Contributors: Peter Eastman

Justin MacCallum's avatar
Justin MacCallum committed
16
Permission is hereby granted, free of charge, to any person obtaining a
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR OTHER DEALINGS IN THE SOFTWARE.
33
34
"""

Peter Eastman's avatar
Peter Eastman committed
35
36
from __future__ import division

37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
__author__ = "Christopher M. Bruns"
__version__ = "0.5"


import math
from quantity import is_quantity
from unit_definitions import *

####################
### TRIGONOMETRY ###
####################

def sin(angle):
    """
    Examples
Justin MacCallum's avatar
Justin MacCallum committed
52

53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
    >>> sin(90*degrees)
    1.0
    """
    if is_quantity(angle):
        return math.sin(angle/radians)
    else:
        return math.sin(angle)

def sinh(angle):
    if is_quantity(angle):
        return math.sinh(angle/radians)
    else:
        return math.sinh(angle)

def cos(angle):
    """
    Examples
Justin MacCallum's avatar
Justin MacCallum committed
70

71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
    >>> cos(180*degrees)
    -1.0
    """
    if is_quantity(angle):
        return math.cos(angle/radians)
    else:
        return math.cos(angle)

def cosh(angle):
    if is_quantity(angle):
        return math.cosh(angle/radians)
    else:
        return math.cosh(angle)

def tan(angle):
    if is_quantity(angle):
        return math.tan(angle/radians)
    else:
        return math.tan(angle)

def tanh(angle):
    if is_quantity(angle):
        return math.tanh(angle/radians)
    else:
        return math.tanh(angle)

def acos(x):
    """
    >>> acos(1.0)
    Quantity(value=0.0, unit=radian)
Peter Eastman's avatar
Peter Eastman committed
101
    >>> print(acos(1.0))
102
103
104
    0.0 rad
    """
    return math.acos(x) * radians
Justin MacCallum's avatar
Justin MacCallum committed
105

106
107
def acosh(x):
    return math.acosh(x) * radians
Justin MacCallum's avatar
Justin MacCallum committed
108

109
110
111
112
113
114
115
116
def asin(x):
    return math.asin(x) * radians

def asinh(x):
    return math.asinh(x) * radians

def atan(x):
    return math.atan(x) * radians
Justin MacCallum's avatar
Justin MacCallum committed
117

118
119
def atanh(x):
    return math.atanh(x) * radians
Justin MacCallum's avatar
Justin MacCallum committed
120

121
122
123
124
125
126
127
128
129
130
131
def atan2(x, y):
    return math.atan2(x, y) * radians

###################
### SQUARE ROOT ###
###################

def sqrt(val):
    """
    >>> sqrt(9.0)
    3.0
Peter Eastman's avatar
Peter Eastman committed
132
    >>> print(sqrt(meter*meter))
133
134
135
136
137
138
139
140
141
142
143
144
145
    meter
    >>> sqrt(9.0*meter*meter)
    Quantity(value=3.0, unit=meter)
    >>> sqrt(9.0*meter*meter*meter)
    Traceback (most recent call last):
    ...
    ArithmeticError: Exponents in Unit.sqrt() must be even.
    """
    try:
        return val.sqrt()
    except AttributeError:
        return math.sqrt(val)

146
147
148
149
150
151
152
153
154
155
156
157
158
###########
### SUM ###
###########

def sum(val):
    """
    >>> sum((1.0, 2.0))
    3.0
    >>> sum((2.0*meter, 3.0*meter))
    Quantity(value=5.0, unit=meter)
    >>> sum((2.0*meter, 30.0*centimeter))
    Quantity(value=2.3, unit=meter)
    """
159
160
161
162
    try:
        return val.sum()
    except AttributeError:
        pass
163
164
165
166
167
168
169
    if len(val) == 0:
        return 0
    result = val[0]
    for i in range(1, len(val)):
        result += val[i]
    return result

170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
###################
### VECTOR MATH ###
###################

def dot(x, y):
    """
    >>> dot((2, 3)*meter, (4, 5)*meter)
    Quantity(value=23, unit=meter**2)
    """
    sum = x[0]*y[0]
    for i in range(1, len(x)):
        sum += x[i]*y[i]
    return sum

def norm(x):
    """
186
187
    >>> norm((3, 4)*meter)
    Quantity(value=5.0, unit=meter)
188
189
190
    """
    return sqrt(dot(x, x))

191
192
193
194
195
# run module directly for testing
if __name__=='__main__':
    # Test the examples in the docstrings
    import doctest, sys
    doctest.testmod(sys.modules[__name__])