SymPy可以打印幂基和幂?

时间:2018-11-22 12:59:11

标签: sympy

在此先感谢您,英语不好!

我想要

1.x:如何求幂和可能的确定?

2.print:base = b,exponent = n

WrongScript.py

from sympy import *
var('x y z a b n')
x=b**n
y=3**n
z=a
# output 1.---------------------------------
print("x=",x) # x= b**n,**
print("y=",y) # y= 3**n,**
print("z=",z) # z= a,   not **
# output 2.---------------------------------
print(MyBaseOut(x),MyExponentOut(x))  # b,n
print(MyBaseOut(y),MyExponentOut(y))  # 3,n

def MyBaseOut(p):
    #   ans=?
    return ans
def MyExponentOut(q):
    #   ans=?
    return ans

2018-11-26 ------------------------------

FullScript.py

from sympy import *
var('b n')
def MyBaseOut(p):
    return p.as_base_exp()[0]
def MyExponentOut(q):
    return q.as_base_exp()[1]
x=b**n
y=3**n
print(MyBaseOut(x),MyExponentOut(x))
print(MyBaseOut(y),MyExponentOut(y))
# b n
# 3 n

1 个答案:

答案 0 :(得分:1)

属性is_Pow会告诉您它是否具有除1以外的指数,方法as_base_exp()会告诉您基数和指数是什么-为基数选择元素0,为元素选择元素1指数:

>>> [(i.is_Pow, i.as_base_exp()) for i in (y,1/y,y**2,y**z)]
[(False, (y, 1)), (True, (y, -1)), (True, (y, 2)), (True, (y, z))]