/usr/share/pyshared/quantities/umath.py is in python-quantities 0.10.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 | from __future__ import absolute_import
import numpy as np
from .quantity import Quantity
from .units import dimensionless, radian, degree
from .decorators import with_doc
#__all__ = [
# 'exp', 'expm1', 'log', 'log10', 'log1p', 'log2'
#]
@with_doc(np.prod)
def prod(a, axis=None, dtype=None, out=None):
return a.prod(axis, dtype, out)
@with_doc(np.sum)
def sum(a, axis=None, dtype=None, out=None):
return a.sum(axis, dtype, out)
@with_doc(np.nansum)
def nansum(a, axis=None):
if not isinstance(a, Quantity):
return np.nansum(a, axis)
return Quantity(
np.nansum(a.magnitude, axis),
a.dimensionality,
copy=False
)
@with_doc(np.cumprod)
def cumprod(a, axis=None, dtype=None, out=None):
"""
Raises a ValueError if input cannot be rescaled to a dimensionless
quantity.
"""
return a.cumprod(axis, dtype, out)
@with_doc(np.cumsum)
def cumsum(a,axis=None, dtype=None, out=None):
return a.cumsum(axis, dtype, out)
diff = np.diff
@with_doc(np.ediff1d)
def ediff1d(ary, to_end=None, to_begin=None):
if not isinstance(ary, Quantity):
return np.ediff1d(ary, to_end, to_begin)
return Quantity(
np.ediff1d(ary.magnitude, to_end, to_begin),
ary.dimensionality,
copy=False
)
@with_doc(np.gradient)
def gradient(f, *varargs):
# if no sample distances are specified, use dimensionless 1
# this mimicks the behavior of np.gradient, but perhaps we should
# remove this default behavior
# removed for now::
#
# if len(varargs) == 0:
# varargs = (Quantity(1),)
varargsQuantities = [Quantity(i, copy=False) for i in varargs]
varargsMag = tuple([i.magnitude for i in varargsQuantities])
ret = np.gradient(f.magnitude, *varargsMag)
if len(varargs) == 1:
# if there was only one sample distance provided,
# apply the units in all directions
return tuple([ Quantity(i, f.units/varargs[0].units) for i in ret])
else:
#give each output array the units of the input array
#divided by the units of the spacing quantity given
return tuple([ Quantity(i, f.units/j.units)
for i,j in zip( ret, varargsQuantities)])
@with_doc(np.cross)
def cross (a, b , axisa=-1, axisb=-1, axisc=-1, axis=None):
if not (isinstance(a, Quantity) and isinstance(b, Quantity)):
return np.cross(a, b, axisa, axisb, axisc, axis)
if not isinstance(a, Quantity):
a = Quantity(a, dimensionless, copy=False)
if not isinstance(b, Quantity):
b = Quantity(b, dimensionless, copy=False)
return Quantity(
np.cross(a, b, axisa, axisb, axisc, axis),
a._dimensionality*b._dimensionality,
copy=False
)
@with_doc(np.trapz)
def trapz(y, x=None, dx=1.0, axis=-1):
# this function has a weird input structure, so it is tricky to wrap it
# perhaps there is a simpler way to do this
if (
not isinstance(y, Quantity)
and not isinstance(x, Quantity)
and not isinstance(dx, Quantity)
):
return np.trapz(y, x, dx, axis)
if not isinstance(y, Quantity):
y = Quantity(y, copy = False)
if not isinstance(x, Quantity) and not x is None:
x = Quantity(x, copy = False)
if not isinstance(dx, Quantity):
dx = Quantity(dx, copy = False)
if x is None:
ret = np.trapz(y.magnitude , x, dx.magnitude, axis)
return Quantity ( ret, y.units * dx.units)
else:
ret = np.trapz(y.magnitude , x.magnitude, dx.magnitude, axis)
return Quantity ( ret, y.units * x.units)
@with_doc(np.sin)
def sin(x, out=None):
"""
Raises a ValueError if input cannot be rescaled to radians.
Returns a dimensionless quantity.
"""
if not isinstance(x, Quantity):
return np.sin(x, out)
return Quantity(np.sin(x.rescale(radian).magnitude, out),
copy=False)
@with_doc(np.arcsin)
def arcsin(x, out=None):
"""
Raises a ValueError if input cannot be rescaled to a dimensionless
quantity.
Returns a quantity in units of radians.
"""
if not isinstance(x, Quantity):
return np.arcsin(x, out)
return Quantity(
np.arcsin(x.rescale(dimensionless).magnitude, out),
radian,
copy=False
)
@with_doc(np.cos)
def cos(x, out=None):
"""
Raises a ValueError if input cannot be rescaled to radians.
Returns a dimensionless quantity.
"""
if not isinstance(x, Quantity):
return np.cos(x, out)
return Quantity(np.cos(x.rescale(radian).magnitude), copy=False)
@with_doc(np.arccos)
def arccos(x, out=None):
"""
Raises a ValueError if input cannot be rescaled to a dimensionless
quantity.
Returns a quantity in units of radians.
"""
if not isinstance(x, Quantity):
return np.arccos(x, out)
return Quantity(
np.arccos(x.rescale(dimensionless).magnitude, out),
radian,
copy=False
)
@with_doc(np.tan)
def tan(x, out=None):
"""
Raises a ValueError if input cannot be rescaled to radians.
Returns a dimensionless quantity.
"""
if not isinstance(x, Quantity):
return np.tan(x, out)
return Quantity(np.tan(x.rescale(radian).magnitude), copy=False)
@with_doc(np.arctan)
def arctan(x, out=None):
"""
Raises a ValueError if input cannot be rescaled to a dimensionless
quantity.
Returns a quantity in units of radians.
"""
if not isinstance(x, Quantity):
return np.arctan(x, out)
return Quantity(
np.arctan(x.rescale(dimensionless).magnitude, out),
radian,
copy=False
)
@with_doc(np.arctan2)
def arctan2(x1, x2, out=None):
"""
Raises a ValueError if inputs do not have identical units.
Returns a quantity in units of radians.
"""
if not (isinstance(x1, Quantity) and isinstance(x2, Quantity)):
return np.arctan2(x1, x2, out)
if not isinstance(x1, Quantity):
x1 = Quantity(x1, dimensionless, copy=False)
if not isinstance(x2, Quantity):
x2 = Quantity(x2, dimensionless, copy=False)
if x1._dimensionality.simplified != x2._dimensionality.simplified:
raise ValueError(
'x1 and x2 must have identical units, got "%s" and "%s"'\
% (str(x1._dimensionality), str(x2._dimensionality))
)
return Quantity(
np.arctan2(x1.magnitude, x2.magnitude, out),
radian,
copy=False
)
@with_doc(np.hypot)
def hypot(x1, x2, out = None):
"""
Raises a ValueError if inputs do not have identical units.
"""
if not (isinstance(x1, Quantity) and isinstance(x2, Quantity)):
return np.hypot(x1, x2, out)
if not isinstance(x1, Quantity):
x1 = Quantity(x1, dimensionless, copy=False)
if not isinstance(x2, Quantity):
x2 = Quantity(x2, dimensionless, copy=False)
if x1._dimensionality != x2._dimensionality:
raise ValueError(
'x1 and x2 must have identical units, got "%s" and "%s"'\
% (str(x1._dimensionality), str(x2._dimensionality))
)
return Quantity(
np.hypot(x1.magnitude, x2.magnitude, out),
x1.dimensionality,
copy = False
)
@with_doc(np.unwrap)
def unwrap(p, discont=np.pi, axis=-1):
if not (isinstance(p, Quantity) and isinstance(discont, Quantity)):
return np.unwrap(p, discont, axis)
if not isinstance(p, Quantity):
p = Quantity(p, copy=False)
if not isinstance(discont, Quantity):
discont = Quantity(discont, copy=False)
discont = discont.rescale(p.units)
return Quantity(
np.unwrap(p.magnitude, discont.magnitude, axis),
p.units
)
@with_doc(np.sinh)
def sinh(x, out=None):
"""
Raises a ValueError if input cannot be rescaled to a dimensionless
quantity.
"""
if not isinstance(x, Quantity):
return np.sinh(x, out)
return Quantity(
np.sinh(x.rescale(dimensionless).magnitude, out),
dimensionless,
copy=False
)
@with_doc(np.cosh)
def cosh(x, out=None):
"""
Raises a ValueError if input cannot be rescaled to a dimensionless
quantity.
"""
if not isinstance(x, Quantity):
return np.cosh(x, out)
return Quantity(
np.cosh(x.rescale(dimensionless).magnitude, out),
dimensionless,
copy=False
)
@with_doc(np.tanh)
def tanh(x, out=None):
"""
Raises a ValueError if input cannot be rescaled to a dimensionless
quantity.
"""
if not isinstance(x, Quantity):
return np.tanh(x, out)
return Quantity(
np.tanh(x.rescale(dimensionless).magnitude, out),
dimensionless,
copy=False
)
@with_doc(np.arcsinh)
def arcsinh(x, out=None):
"""
Raises a ValueError if input cannot be rescaled to a dimensionless
quantity.
"""
if not isinstance(x, Quantity):
return np.arcsinh(x, out)
return Quantity(
np.arcsinh(x.rescale(dimensionless).magnitude, out),
dimensionless,
copy=False
)
@with_doc(np.arccosh)
def arccosh(x, out=None):
"""
Raises a ValueError if input cannot be rescaled to a dimensionless
quantity.
"""
if not isinstance(x, Quantity):
return np.arccosh(x, out)
return Quantity(
np.arccosh(x.rescale(dimensionless).magnitude, out),
dimensionless,
copy=False
)
@with_doc(np.arctanh)
def arctanh(x, out=None):
"""
Raises a ValueError if input cannot be rescaled to a dimensionless
quantity.
"""
if not isinstance(x, Quantity):
return np.arctanh(x, out)
return Quantity(
np.arctanh(x.rescale(dimensionless).magnitude, out),
dimensionless,
copy=False
)
|