Exemple #1
0
    def equation(self, exp):
        """计算方程"""

        if exp.find(Configuration.ConstantPrefix) == -1:raise Exception("could not found the unknown number")

        #出自:http://code.activestate.com/recipes/365013-linear-equations-solver-in-3-lines/
        #缺点是只能求解一元一次方程
        #未知数不能位于函数参数处和数组里
        #未知数位于分母的方程是分式方程,不属于一元一次方程
        #原理简述:利用复数的虚部系数保存了未知数的系数,其他常数参与计算后成为实部
        #将最后结果的复数实部除以虚部并取反就是未知数的值
        e1, e2 = exp.split("=")
        exp = "(%s)-(%s)" % (e1, e2)
        self.save_vars((Configuration.UnknownNumber,), (1j,))#使用复数1j替换未知数
        r, o, e = self.eval(exp)

        if r.imag == 0:raise Exception("could not solve this equation")

        r = autonum(-r.real/r.imag)#实部除以虚部并取反

        #保存结果
        self.save_vars((Configuration.AnswerConstant,), (r,))
        #保存未知数
        self.save_vars((Configuration.UnknownNumber,), (r,))
        return r, o, e
Exemple #2
0
 def array(arg):
     """参数包装成一维数组(grape.GrapeArray)"""
     arr = deque()
     if isinstance(arg, tuple):
         for e in arg:
             if isinstance(e, (list, tuple)):
                 arr.extend(Supporter.array(e))
             else:arr.append(grape.autonum(e))
     else:arr.append(arg)
     return grape.GrapeArray(arr)
Exemple #3
0
 def _(*arg, **kw):
     return grape.autonum(func(Supporter.array(arg), **kw))
Exemple #4
0
 def __floor(n):
     """取整"""
     return grape.GrapeArray(map(lambda i:grape.autonum(re.sub("\.\d*",".0", str(i))), n))