~ellisonbg/ipython/bugfixes0411409

« back to all changes in this revision

Viewing changes to IPython/Extensions/PhysicalQInteractive.py

  • Committer: ville
  • Date: 2008-02-16 09:50:47 UTC
  • mto: (0.12.1 ipython_main)
  • mto: This revision was merged to the branch mainline in revision 990.
  • Revision ID: ville@ville-pc-20080216095047-500x6dluki1iz40o
initialization (no svn history)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
"""Modify the PhysicalQuantities class for more convenient interactive use.
 
3
 
 
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
 
6
namespace.
 
7
 
 
8
This module should always be loaded *after* math or Numeric, so it can
 
9
overwrite math functions with the versions that handle units."""
 
10
 
 
11
#*****************************************************************************
 
12
#       Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu>
 
13
#
 
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
#*****************************************************************************
 
17
 
 
18
from IPython import Release
 
19
__author__  = '%s <%s>' % Release.authors['Fernando']
 
20
__license__ = Release.license
 
21
 
 
22
from Scientific.Physics.PhysicalQuantities import PhysicalQuantity
 
23
 
 
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.
 
26
 
 
27
# If you use math, sin(x) won't work for x an array, only float or PhysQty
 
28
import math
 
29
 
 
30
# If you use Numeric, sin(x) works for x a float, PhysQty an array.
 
31
#import Numeric as math
 
32
 
 
33
class PhysicalQuantityFunction:
 
34
    """Generic function wrapper for PhysicalQuantity instances.
 
35
 
 
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.
 
39
 
 
40
    This is *slow*. It's meant for convenient interactive use, not for
 
41
    speed."""
 
42
 
 
43
    def __init__(self,name):
 
44
        self.name = name
 
45
        
 
46
    def __call__(self,x):
 
47
        if isinstance(x,PhysicalQuantity):
 
48
            return PhysicalQuantity.__dict__[self.name](x)
 
49
        else:
 
50
            return math.__dict__[self.name](x)
 
51
 
 
52
class PhysicalQuantityInteractive(PhysicalQuantity):
 
53
    """Physical quantity with units - modified for Interactive use.
 
54
 
 
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.
 
58
 
 
59
    Also adds the following aliases for commonly used methods:
 
60
      b = PhysicalQuantity.inBaseUnits
 
61
      u = PhysicalQuantity.inUnitsOf
 
62
      
 
63
    These are useful when doing a lot of interactive calculations.
 
64
    """
 
65
    
 
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
 
69
 
 
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...
 
74
    #__call__ = b
 
75
    
 
76
    def __str__(self):
 
77
        return PhysicalQuantity.__repr__(self)
 
78
 
 
79
    def __repr__(self):
 
80
        value = '%.4G' % self.value
 
81
        units = self.unit.name().replace('**','^')
 
82
        return value + ' ' + units
 
83
 
 
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')