sympy

python快速求解不定积分和定积分

流过昼夜 提交于 2020-11-06 07:00:05
欢迎点击「算法与编程之美」↑关注我们! 本文首发于微信公众号:"算法与编程之美",欢迎关注,及时了解更多此系列博客。 基本概念 定积分的定义如下: 不定积分定义如下: 如果想了解更多,大家可以继续阅读同济大学《高等数学》,关注公众号,回复关键词'gdsx',可以获得高清电子版。 sympy介绍 sympy库的安装非常的简单,利用conda命令可以快速的完成安装。 conda install sympy 接下来,我们将介绍利用第三方库sympy来完成积分的计算。 python求解不定积分 接下来,我们将介绍上述的不定积分的求解。 首先导入sympy库中的所有类和函数。 from sympy import * 接下来我们需要定义,本次需要使用到的符号变量x,其定义如下: x = symbols('x') 最后我们来计算积分,定积分和不定积分我们都需要用到函数integrate,这个函数的用法非常的简单,完全可以自己领悟。 integrate(cos(x) ,x) >> sin(x) 这里面需要注意两点: 1)cos后面要跟一对括号,不能直接写cosx。 2)求解的结果中省略了常数C,需要自己加上。 python求解定积分 定积分的求解和不定积分类似,唯一的区别在于,定积分说明了积分的上下限。 integrate(cos(x), (x,-pi, pi)) 其中(x,-pi,pi

How to make my Piecewise function zero outside the provided interval in python

亡梦爱人 提交于 2020-08-26 06:46:41
问题 Here is my code: In [61]: import sympy as sp In [62]: x = sp.Symbol('x') In [63]: phi_1 = sp.Piecewise( ( (1.3-x)/0.3, 1<=x <=1.3 )) In [64]: phi_1.subs(x,1.2) Out[64]: 0.333333333333334 In [65]: phi_1.subs(x,1.4) Out[65]: Piecewise() More specifically, I want to get zero as an answer to the input no. 65, since 1.4 is outside the interval [1, 1.3]. 回答1: You need to tell Piecewise that you want the function to evaluate to zero when outside the bounds, for example: import sympy as sp x = sp

Numpy Covariance Matrix numpy.cov

左心房为你撑大大i 提交于 2020-08-18 07:48:14
问题 I am using numpy and want to compute the covariance matrix for an ndarray. I am trying to use numpy.cov() but am not getting the correct results. More details below. My ndarray is 768x8 for where 8 is the numbers features in my data set. When I use MATLAB to compute the covariance matrix, I get a 8x8 (which is what I require), but when I use np.cov(), I get a 768x768 which is incorrect. I tried changing the rowvar argument to true and this does not work. What would be the correct call to