1
# -*- coding: utf-8 -*-
2
"""Modify the PhysicalQuantities class for more convenient interactive use.
4
Also redefine some math functions to operate on PhysQties with no need for
5
special method syntax. This just means moving them out to the global
8
This module should always be loaded *after* math or Numeric, so it can
9
overwrite math functions with the versions that handle units."""
11
#*****************************************************************************
12
# Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu>
14
# Distributed under the terms of the BSD License. The full license is in
15
# the file COPYING, distributed as part of this software.
16
#*****************************************************************************
18
from IPython import Release
19
__author__ = '%s <%s>' % Release.authors['Fernando']
20
__license__ = Release.license
22
from Scientific.Physics.PhysicalQuantities import PhysicalQuantity
24
# This code can be set up to work with Numeric or with math for providing the
25
# mathematical functions. Uncomment the one you prefer to use below.
27
# If you use math, sin(x) won't work for x an array, only float or PhysQty
30
# If you use Numeric, sin(x) works for x a float, PhysQty an array.
31
#import Numeric as math
33
class PhysicalQuantityFunction:
34
"""Generic function wrapper for PhysicalQuantity instances.
36
Calls functions from either the math library or the instance's methods as
37
required. Allows using sin(theta) or sqrt(v**2) syntax irrespective of
38
whether theta is a pure number or a PhysicalQuantity.
40
This is *slow*. It's meant for convenient interactive use, not for
43
def __init__(self,name):
47
if isinstance(x,PhysicalQuantity):
48
return PhysicalQuantity.__dict__[self.name](x)
50
return math.__dict__[self.name](x)
52
class PhysicalQuantityInteractive(PhysicalQuantity):
53
"""Physical quantity with units - modified for Interactive use.
55
Basically, the __str__ and __repr__ methods have been swapped for more
56
convenient interactive use. Powers are shown as ^ instead of ** and only 4
57
significant figures are shown.
59
Also adds the following aliases for commonly used methods:
60
b = PhysicalQuantity.inBaseUnits
61
u = PhysicalQuantity.inUnitsOf
63
These are useful when doing a lot of interactive calculations.
66
# shorthands for the most useful unit conversions
67
b = PhysicalQuantity.inBaseUnits # so you can just type x.b to get base units
68
u = PhysicalQuantity.inUnitsOf
70
# This can be done, but it can get dangerous when coupled with IPython's
71
# auto-calling. Everything ends up shown in baseunits and things like x*2
72
# get automatically converted to k(*2), which doesn't work.
73
# Probably not a good idea in general...
77
return PhysicalQuantity.__repr__(self)
80
value = '%.4G' % self.value
81
units = self.unit.name().replace('**','^')
82
return value + ' ' + units
84
# implement the methods defined in PhysicalQuantity as PhysicalQuantityFunctions
85
sin = PhysicalQuantityFunction('sin')
86
cos = PhysicalQuantityFunction('cos')
87
tan = PhysicalQuantityFunction('tan')
88
sqrt = PhysicalQuantityFunction('sqrt')