简体   繁体   English

Sympy拒绝计算值

[英]Sympy refuses to calculate values

I'm trying to implement a markdown-like language to do math with. 我正在尝试实现类似markdown的语言来进行数学运算。 The basic idea is to have a file where you can write down your math, then have a python-script do the calculations and spit out tex. 基本思想是拥有一个文件,您可以在其中写下数学内容,然后使用python脚本进行计算并吐出tex。

However, I'm facing the problem, that Sympy refuses to spit out values, it only gives me back the equation. 但是,我正面临一个问题,即Sympy拒绝吐出值,它只给了我等式。 Much weirder is the fact, that it DOES spit out values in an alternate test-script, that is essentially the same code. 事实很奇怪,它确实在备用测试脚本中吐出了值,该脚本实质上是相同的代码。

This is the working code: 这是工作代码:

import sympy as sp
m = sp.symbols('m')
kg = sp.symbols('kg')
s = sp.symbols('s')
g = sp.sympify(9.80665*m/s**2)
mass = sp.sympify(0.2*kg)
acc = sp.sympify(g)
F = sp.sympify(mass*acc)
print F

Output: 输出:

1.96133*kg*m/s**2

This the not working code: 这不是工作代码:

import re
import sympy as sp
print 'import sympy as sp'

#read units
mymunits = 'units.mymu'
with open(mymunits) as mymu:
    mymuinput = mymu.readlines()
    for lines in mymuinput:
        lines = re.sub('\s+','',lines).split()
        if lines != []:
            if lines[0][0] != '#':
                unit = lines[0].split('#')[0]
                globals()[unit] = sp.symbols(unit)
                print unit+' = sp.symbols(\''+unit+'\')'

#read constants
mymconstants = 'constants.mymc'
with open(mymconstants) as mymc:
    mymcinput = mymc.readlines()
    for lines in mymcinput:
        lines = re.sub('\s+','',lines).split()
        if lines != []:
            if lines[0][0] != '#':
                constant = lines[0].split('#')[0].split(':=')
                globals()[constant[0]] = sp.sympify(constant[1])
                print constant[0]+' = sp.sympify('+constant[1]+')'

#read file
mymfile = 'test.mym'
with open(mymfile) as mym:
    myminput = mym.readlines()
    #create equations by removing spaces and splitting lines
    for line in myminput:
        line = line.replace(' ','').strip().split(';')
        for eqstr in line:
            if eqstr != '':
                eq = re.split(':=',eqstr)
                globals()[eq[0]] = sp.sympify(eq[1])
                print eq[0]+' = sp.sympify('+eq[1]+')'

print 'print F'
print F

It outputs this: 它输出:

acc*mass

It SHOULD output a value, just like the test-script. 它应该输出一个值,就像测试脚本一样。 The same script also outputs the code that is used in the test-script. 同一脚本还输出测试脚本中使用的代码。 The only difference is, that in the not-working script, I try to generate the code from an input-file, which looks like that: 唯一的区别是,在不起作用的脚本中,我尝试从输入文件生成代码,如下所示:

mass := 0.2*kg ; acc := g

F := mass*acc

as well as files for units: 以及单位文件:

#SI

m       #length
kg      #mass
s       #time

and constants: 和常量:

#constants

g:=9.80665*m/s**2       #standard gravity

The whole code is also to be found on github . 整个代码也可以在github上找到。 What I don't get is why the one version works, while the other doesn't. 我不明白的是为什么一个版本可以工作而另一个版本却不能工作。 Any ideas are welcomed. 任何想法都欢迎。 Thank you. 谢谢。

Based on Everts comment, I cam up with this solution: 根据Everts的评论,我提出了以下解决方案:

change: 更改:

sp.sympify(eq[1])

to: 至:

sp.sympify(eval(eq[1]))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM