quantitiesを試してみる
公式:http://packages.python.org/quantities/を
訳しただけ
1.起動
hercules:~% python
Python 2.7.3 (default, Jan 2 2013, 16:53:07)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more
information.
>>> import quantities as pq
pqは“physical quantities” or “python quantities” =
「物理量」または「パイソン量」(パイソンはプログラミング言語)の略
m(=メートル)やft(=フィート)といった物理量の単位を読み込んだ?
2.練習
>>> q = 1
* pq.m :変数qの定
義 q=1×(pqのm)
>>> print
q :変数qを出力
1.0 m :変数qは1.0 m(メートル)
3.変換
>>> q.units = pq.ft :変数qの単位をft(フィート)に変換
>>> print q
3.28083989501 ft :変数qは3.28083989501 ft(フィート)
50kpcは
>>> q = 50000
* pq.pc
>>> q.units = pq.m
>>> q
array(1.542840125e+21) * m
1.542840125×10^21 mだとわかる
J(=joule)とW(=watt)は変換できない
>>> q
= 10 *
pq.joule
>>> q.units = pq.watts
ValueError: Unable to convert between units of "J"
and "W"
>>> print q
10.0 J
4.計算
>>> q
= (10 *
pq.meter)**3
:**は階乗
>>> q2 = q/(5*pq.meter)**2
>>> print q2
40 m
単位が違っていても
>>> q = (10 * pq.meter)**3
>>> q2 = q/(5*pq.ft)**2
>>> print q2 :このままではダメ
40.0 m**3/ft**2
>>> q3 = q2.simplified :単位の統一(単純化)
>>> print q3
430.556416668 m
5.単位
>>> print pq.J.simplified
: Jを単純化するとどうなるか?
1.0 kg*m**2/s**2
>>> pq.set_default_units('cgs')
:設定変更もできる
>>> print pq.J.simplified
10000000.0 g*cm**2/s**2
単位導入
>>> uK =
pq.UnitQuantity('microkelvin', pq.degK/1e6, symbol='uK')
>>> print uK
1 uK (microkelvin)
>>> q = 1000*uK
>>> print q.simplified
0.001 K
佐藤先生がやってた光度の変換計算
hercules:~> ipython
:ipythonでも起動できる
In [1]: import quantities as pq
In [2]: f = 1e-12 * pq.erg / pq.sec / pq.cm**2
:fluxの入力 f=1*10^-12 erg/s/cm^2
In [3]: r = 1 * pq.kilo * pq.parsec
:距離の入力 r=1 kpc
In [4]: f, r
:確認
Out[4]: (array(1e-12) * erg/(cm**2*s), array(1000.0) *
pc)
In [5]: luminosity = 4 * pq.pi * r**2 * f :
光度の定義 L=4πr^2f
In [6]: luminosity
:確認
Out[6]: array(1.2566370614359172e-05) *
pc**2*erg/(cm**2*s)
In [7]: luminosity.units = pq.erg / pq.sec
:単位を揃える
In [8]: luminosity
:確認
Out[8]: array(1.1964972523338386e+32) * erg/s :結果