Replace ** with Math.pow in SymPy

最后都变了- 提交于 2019-12-20 03:28:07

问题


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 uses Math.pow(a,b);
  • Python uses a**b.

So my question is: Is there a way how to print the SymPy expression in the “Java format”?


回答1:


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)'


来源:https://stackoverflow.com/questions/41675777/replace-with-math-pow-in-sympy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!