用SymPy中的Math.pow替换** [英] Replace ** with Math.pow in SymPy
问题描述
我在Python中有一个SymPy表达式,我想将其复制并粘贴到Java源代码中。问题是对取幂有不同的表示法:
I have a SymPy expression in Python and I would like to copy and paste it into a Java source code. Problem is that there’s a different notation for exponentiation:
- Java使用
Math.pow(a,b)
; - Python使用
a ** b
。
- Java uses
Math.pow(a,b)
; - Python uses
a**b
.
所以我的问题是:有没有办法以Java格式打印SymPy表达式?
So my question is: Is there a way how to print the SymPy expression in the "Java format"?
推荐答案
SymPy有几个代码打印机专门用于此目的。虽然没有Java代码打印机,但有一个用于Javascript 。我不能说Java和Javascript是否与各种目的相似,但是取幂是按照你想要的方式打印的:
SymPy has several code printers that are intended specifically for such a purpose. While there is no Java code printer, there is one for Javascript. I cannot say whether Java and Javascript are sufficiently similar for every purpose, but exponentiation is printed the way you want it:
import sympy
a,b = sympy.symbols("a, b")
print(sympy.printing.jscode(a**b))
# 'Math.pow(a, b)'
这篇关于用SymPy中的Math.pow替换**的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!