数値のオーダーの取得(order of magnitude)

27 12月

Pyhton で、数値のオーダーを取得するコードを書いてみました。例えば、数値が 123 なら、2 を得ます。あまり自信はありません。

%reset -f

import sys
#from sympy import *
import sympy

def getOrderOfMagnitude(tmpStr):
    
    orderOfMagnitude = None
    sign = None

    #typeArray = ["<class 'sympy.core.numbers.Integer'>", "<class 'sympy.core.numbers.Float'>", "<class 'sympy.core.numbers.Zero'>", "<class 'sympy.core.numbers.One'>"]
    typeArray = ["<class 'sympy.core.numbers.Integer'>", "<class 'sympy.core.numbers.Float'>", "<class 'sympy.core.numbers.Zero'>", "<class 'sympy.core.numbers.One'>", "<class 'sympy.core.numbers.NegativeOne'>"]

    
    try:
        expr = sympy.sympify(tmpStr, evaluate=True)
    except Exception as ex:
        expr = ex
        
    print('expr : ', expr)

    exprClass = str(type(expr))

    if exprClass == "<class 'sympy.core.sympify.SympifyError'>":    
        return sign, orderOfMagnitude, "is not numerical value or expression."
    

    # 以下、とりあえず数式にはなっている
    
    # ゼロは対数計算ができない
    if exprClass == "<class 'sympy.core.numbers.Zero'>":
        sign = 0
        return sign, orderOfMagnitude, "is Zero."    
    
    if exprClass in typeArray:
        if expr > 0:
            sign = +1
        else:
            sign = -1
        orderOfMagnitude = int(float(sympy.log(sympy.Abs(expr), 10)))
        return sign, orderOfMagnitude, "float or integer"
    else:
        return sign, orderOfMagnitude, "is expression. But is not numerical value."

    
    
    
testArray = []
testArray.append('0')
testArray.append('1')
testArray.append('-1')
testArray.append('2') 
testArray.append('100')
testArray.append('123')
testArray.append('0.010')
testArray.append('0.012')
testArray.append('0.00123')

testArray.append('-123')
testArray.append('- 123') # 符号との間に空白
testArray.append('-123 ') # 右側に空白
testArray.append(' - 123  ') # 多数の空白
testArray.append(' -0123  ') 
testArray.append('-0.123')

testArray.append('0.12e-3')
testArray.append('-0.12e-3')
testArray.append('-0.12E-3')
testArray.append('-0.0120e-3')
testArray.append(' + 0.01200e-3')
testArray.append('-2.14*10^3')
testArray.append('-2.14*10**3')

testArray.append('123*10^2*3*10^2')
testArray.append('x*y') 
testArray.append('sin(x)') 
testArray.append('x[0]') 
testArray.append('x1') 
testArray.append('2*pi') 

for i in range(len(testArray)):

    tmpStr = testArray[i]
    significantFigures = -1
    orderOfMagnitude = None
    sign = None
    
    
    print('\nNo. ' + str(i))
    print('"' + tmpStr + '"')

    sign, orderOfMagnitude, message = getOrderOfMagnitude(tmpStr)    
    print(message)
    print('sign : ' + str(sign) + ', orderOfMagnitude : ' + str(orderOfMagnitude))

getOrderOfMagnitude という関数に数式を文字列で渡すと、符号とオーダーと数値なのかどうかなどの情報の3変数を返します。以下が実行結果です。

​
No. 0
"0"
expr :  0
is Zero.
sign : 0, orderOfMagnitude : None

No. 1
"1"
expr :  1
float or integer
sign : 1, orderOfMagnitude : 0

No. 2
"-1"
expr :  -1
float or integer
sign : -1, orderOfMagnitude : 0

No. 3
"2"
expr :  2
float or integer
sign : 1, orderOfMagnitude : 0

No. 4
"100"
expr :  100
float or integer
sign : 1, orderOfMagnitude : 2

No. 5
"123"
expr :  123
float or integer
sign : 1, orderOfMagnitude : 2

No. 6
"0.010"
expr :  0.0100000000000000
float or integer
sign : 1, orderOfMagnitude : -2

No. 7
"0.012"
expr :  0.0120000000000000
float or integer
sign : 1, orderOfMagnitude : -1

No. 8
"0.00123"
expr :  0.00123000000000000
float or integer
sign : 1, orderOfMagnitude : -2

No. 9
"-123"
expr :  -123
float or integer
sign : -1, orderOfMagnitude : 2

No. 10
"- 123"
expr :  -123
float or integer
sign : -1, orderOfMagnitude : 2

No. 11
"-123 "
expr :  -123
float or integer
sign : -1, orderOfMagnitude : 2

No. 12
" - 123  "
expr :  -123
float or integer
sign : -1, orderOfMagnitude : 2

No. 13
" -0123  "
expr :  Sympify of expression 'could not parse ' -0123  '' failed, because of exception being raised:
SyntaxError: invalid syntax (<string>, line 1)
is not numerical value or expression.
sign : None, orderOfMagnitude : None

No. 14
"-0.123"
expr :  -0.123000000000000
float or integer
sign : -1, orderOfMagnitude : 0

No. 15
"0.12e-3"
expr :  0.000120000000000000
float or integer
sign : 1, orderOfMagnitude : -3

No. 16
"-0.12e-3"
expr :  -0.000120000000000000
float or integer
sign : -1, orderOfMagnitude : -3

No. 17
"-0.12E-3"
expr :  -0.000120000000000000
float or integer
sign : -1, orderOfMagnitude : -3

No. 18
"-0.0120e-3"
expr :  -1.20000000000000e-5
float or integer
sign : -1, orderOfMagnitude : -4

No. 19
" + 0.01200e-3"
expr :  1.20000000000000e-5
float or integer
sign : 1, orderOfMagnitude : -4

No. 20
"-2.14*10^3"
expr :  -2140.00000000000
float or integer
sign : -1, orderOfMagnitude : 3

No. 21
"-2.14*10**3"
expr :  -2140.00000000000
float or integer
sign : -1, orderOfMagnitude : 3

No. 22
"123*10^2*3*10^2"
expr :  3690000
float or integer
sign : 1, orderOfMagnitude : 6

No. 23
"x*y"
expr :  x*y
is expression. But is not numerical value.
sign : None, orderOfMagnitude : None

No. 24
"sin(x)"
expr :  sin(x)
is expression. But is not numerical value.
sign : None, orderOfMagnitude : None

No. 25
"x[0]"
expr :  'Symbol' object is not subscriptable
is expression. But is not numerical value.
sign : None, orderOfMagnitude : None

No. 26
"x1"
expr :  x1
is expression. But is not numerical value.
sign : None, orderOfMagnitude : None

No. 27
"2*pi"
expr :  2*pi
is expression. But is not numerical value.
sign : None, orderOfMagnitude : None

​