使用sympy solver和numpy dot不起作用

时间:2016-03-09 14:17:33

标签: python numpy sympy

我正在尝试使用sympy solve解决一个长方程。这是等式的简化版本,但问题是相同的。

此代码可以正常工作:

SELECT *
FROM app_posts AS p
LEFT JOIN app_comments AS c ON c.post_id = p.post_id
LEFT JOIN app_user AS u ON u.user_id = p.user_id
LEFT JOIN app_likes AS l ON l.post_id = p.post_id
WHERE u.user_banned = 0
AND p.post_public = 1
GROUP BY p.post_id
ORDER BY p.post_date DESC

import numpy as np import sympy as sy coupons = [0.504452818664, 0.486892427806, 0.47758800215, 100.468050176] rate = sy.Symbol('rate') rate_final = (sy.solve(100 - (rate*coupons[0]+rate*coupons[1]+rate*coupons[2]+rate*coupons[3]),rate)) print rate_final rate-final

但是当Ι尝试在等式中使用[0.980998226948197]时,它会给出一个空列表作为结果。

numpy.dot

import numpy as np import sympy as sy coupons = [0.504452818664, 0.486892427806, 0.47758800215, 100.468050176] rate = sy.Symbol('rate') rate_final = (sy.solve(100 - np.dot(rate,coupons[:]),rate)) print rate_final rate_final

我的代码是否有问题,如果[]在等式中,sympy.solve将无效?

1 个答案:

答案 0 :(得分:1)

标量rate和向量coupons的点积几乎没有意义。您只能获得rate和每个元素的元素乘法。但是,您可以这样做:

import numpy as np
import sympy as sy

coupons = np.array([0.504452818664, 0.486892427806, 0.47758800215, 100.468050176])

rate = sy.Symbol('rate')
rate_final = sy.solve(100 - np.sum(rate * coupons), rate)
print(rate_final)