Replace ** with Math.pow in SymPy

后端 未结 1 1121
梦谈多话
梦谈多话 2021-01-23 05:25

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:

1条回答
  •  走了就别回头了
    2021-01-23 05:53

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

    0 讨论(0)
提交回复
热议问题