基础

from sympy import *

数学格式输出:

init_printing()

添加变量:

x, y, z, a, b, c = symbols('x y z a b c')

声明分数:

Rational(1, 3)

\(\displaystyle \frac{1}{3}\)

化简式子:

simplify((x**3 + x**2 - x - 1)/(x**2 + 2*x + 1))

\(\displaystyle x - 1\)

因式分解:

expand((x + 2)*(x - 3))

\(\displaystyle x^{2} - x - 6\)

提取公因式:

factor(x**3 - x**2 + x - 1)

\(\displaystyle \left(x - 1\right) \left(x^{2} + 1\right)\)

约分:

cancel((x**2 + 2*x + 1)/(x**2 + x))

\(\displaystyle \frac{x + 1}{x}\)

裂项:

apart((4*x**3 + 21*x**2 + 10*x + 12)/(x**4 + 5*x**3 + 5*x**2 + 4*x))

\(\displaystyle \frac{2 x - 1}{x^{2} + x + 1} - \frac{1}{x + 4} + \frac{3}{x}\)

变换形式:

tan(x).rewrite(sin)

\(\displaystyle \frac{2 \sin^{2}{\left(x \right)}}{\sin{\left(2 x \right)}}\)

数列求和:

Sum(x ** 2, (x, 1, a)).doit()

\(\displaystyle \frac{a^{3}}{3} + \frac{a^{2}}{2} + \frac{a}{6}\)

数列求积:

Product(x**2,(x, 1, a)).doit()

\(\displaystyle a!^{2}\)

微积分

求导:

diff(cos(x), x)

\(\displaystyle - \sin{\left(x \right)}\)

求高阶导:

diff(x**4, x, 3)

\(\displaystyle 24 x\)

连续求偏导:

diff(exp(x*y*z), x, y, 2, z, 4)

\(\displaystyle x^{3} y^{2} \left(x^{3} y^{3} z^{3} + 14 x^{2} y^{2} z^{2} + 52 x y z + 48\right) e^{x y z}\)

不定积分:

integrate(cos(x), x)

\(\displaystyle \sin{\left(x \right)}\)

定积分:

integrate(exp(-x), (x, 0, oo))

\(\displaystyle 1\)

多重积分:

integrate(exp(-x**2 - y**2), (x, -oo, oo), (y, -oo, oo))

\(\displaystyle \pi\)

极限:

limit(sin(x)/x, x, 0)

\(\displaystyle 1\)

泰勒展开(到第4阶):

sin(x).series(x, 0, 4)

\(\displaystyle x - \frac{x^{3}}{6} + O\left(x^{4}\right)\)

泰勒展开(在x=6处):

exp(x - 6).series(x, 6)

\(\displaystyle -5 + \frac{\left(x - 6\right)^{2}}{2} + \frac{\left(x - 6\right)^{3}}{6} + \frac{\left(x - 6\right)^{4}}{24} + \frac{\left(x - 6\right)^{5}}{120} + x + O\left(\left(x - 6\right)^{6}; x\rightarrow 6\right)\)

矩阵

矩阵求逆:

Matrix([[1, 3], [-2, 3]])**-1

\(\displaystyle \left[\begin{matrix}\frac{1}{3} & - \frac{1}{3}\\\frac{2}{9} & \frac{1}{9}\end{matrix}\right]\)

求转置:

Matrix([[1, 2, 3], [4, 5, 6]]).T

\(\displaystyle \left[\begin{matrix}1 & 4\\2 & 5\\3 & 6\end{matrix}\right]\)

生成单位矩阵:

eye(3)

\(\displaystyle \left[\begin{matrix}1 & 0 & 0\\0 & 1 & 0\\0 & 0 & 1\end{matrix}\right]\)

求行列式:

Matrix([[1, 0, 1], [2, -1, 3], [4, 3, 2]]).det()

\(\displaystyle -1\)

化成行阶梯形矩阵:

Matrix([[1, 0, 1, 3], [2, 3, 4, 7], [-1, -3, -3, -4]]).rref()

\(\displaystyle \left( \left[\begin{matrix}1 & 0 & 1 & 3\\0 & 1 & \frac{2}{3} & \frac{1}{3}\\0 & 0 & 0 & 0\end{matrix}\right], \ \left( 0, \ 1\right)\right)\)

求列向量空间:

Matrix([[1, 1, 2], [2 ,1 , 3], [3 , 1, 4]]).columnspace()

\(\displaystyle \left[ \left[\begin{matrix}1\\2\\3\end{matrix}\right], \ \left[\begin{matrix}1\\1\\1\end{matrix}\right]\right]\)

M = Matrix([[3, -2,  4, -2], [5,  3, -3, -2], [5, -2,  2, -2], [5, -2, -3,  3]])

求特征值:

M.eigenvals()

\(\displaystyle \left\{ -2 : 1, \ 3 : 1, \ 5 : 2\right\}\)

求特征向量:

M.eigenvects()

\(\displaystyle \left[ \left( -2, \ 1, \ \left[ \left[\begin{matrix}0\\1\\1\\1\end{matrix}\right]\right]\right), \ \left( 3, \ 1, \ \left[ \left[\begin{matrix}1\\1\\1\\1\end{matrix}\right]\right]\right), \ \left( 5, \ 2, \ \left[ \left[\begin{matrix}1\\1\\1\\0\end{matrix}\right], \ \left[\begin{matrix}0\\-1\\0\\1\end{matrix}\right]\right]\right)\right]\)

求对角化矩阵,返回两个矩阵P、D满足\(PDP^{-1}=M\):

M.diagonalize()

\(\displaystyle \left( \left[\begin{matrix}0 & 1 & 1 & 0\\1 & 1 & 1 & -1\\1 & 1 & 1 & 0\\1 & 1 & 0 & 1\end{matrix}\right], \ \left[\begin{matrix}-2 & 0 & 0 & 0\\0 & 3 & 0 & 0\\0 & 0 & 5 & 0\\0 & 0 & 0 & 5\end{matrix}\right]\right)\)

解方程

求解集:

solveset(x**2 - x, x)

\(\displaystyle \left\{0, 1\right\}\)

求解集(显示多少个重根):

roots(x**3 - 6*x**2 + 9*x, x)

\(\displaystyle \left\{ 0 : 1, \ 3 : 2\right\}\)

求解集(用Eq构造等式):

solveset(Eq(sin(x), 1), x, domain=S.Reals)

\(\displaystyle \left\{2 n \pi + \frac{\pi}{2}\; |\; n \in \mathbb{Z}\right\}\)

解线性方程组:

linsolve([x + y + z - 1, x + y + 2*z - 3 ], (x, y, z))

\(\displaystyle \left\{\left( - y - 1, \ y, \ 2\right)\right\}\)

解线性方程组(矩阵表示):

linsolve(Matrix(([1, 1, 1, 1], [1, 1, 2, 3])), (x, y, z))

\(\displaystyle \left\{\left( - y - 1, \ y, \ 2\right)\right\}\)

解非线性方程组:

nonlinsolve([exp(x) - sin(y), 1/y - 3], [x, y])

\(\displaystyle \left\{\left( \log{\left(\sin{\left(\frac{1}{3} \right)} \right)}, \ \frac{1}{3}\right), \left( \left\{2 n i \pi + \left(\log{\left(\sin{\left(\frac{1}{3} \right)} \right)}\bmod{2 i \pi}\right)\; |\; n \in \mathbb{Z}\right\}, \ \frac{1}{3}\right)\right\}\)

解微分方程:

f, g = symbols('f g', cls=Function)
dsolve(Eq(f(x).diff(x, x) - 2*f(x).diff(x) + f(x), sin(x)), f(x))

\(\displaystyle f{\left(x \right)} = \left(C_{1} + C_{2} x\right) e^{x} + \frac{\cos{\left(x \right)}}{2}\)

解不等式组:

from sympy.solvers.inequalities import *
reduce_inequalities([x <= x ** 2], [x])

\(\displaystyle \left(1 \leq x \wedge x < \infty\right) \vee \left(x \leq 0 \wedge -\infty < x\right)\)

逻辑代数

from sympy.logic.boolalg import *

合取范式:

to_cnf(~(x | y) | z)

\(\displaystyle \left(z \vee \neg x\right) \wedge \left(z \vee \neg y\right)\)

析取范式:

to_dnf(x & (y | z))

\(\displaystyle \left(x \wedge y\right) \vee \left(x \wedge z\right)\)

化简逻辑函数:

simplify_logic((~x & ~y & ~z) | ( ~x & ~y & z))

\(\displaystyle \neg x \wedge \neg y\)

from sympy.logic import *

化简最小项之和为析取范式

minterms = [0, 7]
SOPform([x, y, z], minterms)

\(\displaystyle \left(x \wedge y \wedge z\right) \vee \left(\neg x \wedge \neg y \wedge \neg z\right)\)

化简最小项之和为合取范式

minterms = [[1, 0, 1], [1, 1, 0], [1, 1, 1]]
POSform([x, y, z], minterms)

\(\displaystyle x \wedge \left(y \vee z\right)\)

化简最小项之和为析取范式(第7项任取)

minterms = [[1, 0, 1], [1, 1, 0]]
dontcares = [7]
SOPform([x, y, z], minterms, dontcares)

\(\displaystyle \left(x \wedge y\right) \vee \left(x \wedge z\right)\)

数论

from sympy.ntheory import *

阶乘:

factorial(10)

\(\displaystyle 3628800\)

分解质因数:

factorint(300)

\(\displaystyle \left\{ 2 : 2, \ 3 : 1, \ 5 : 2\right\}\)

factorint(300, visual=True)

\(\displaystyle 2^{2} \cdot 3^{1} \cdot 5^{2}\)

求欧拉函数:

totient(25)

\(\displaystyle 20\)

判断质数:

isprime(101)
True

莫比乌斯函数:

mobius(13 * 17 * 5)

\(\displaystyle -1\)

乘法逆元(模后者意义):

mod_inverse(3, 5)

\(\displaystyle 2\)

from sympy.ntheory.factor_ import *

求因子:

divisors(36)

\(\displaystyle \left[ 1, \ 2, \ 3, \ 4, \ 6, \ 9, \ 12, \ 18, \ 36\right]\)

from sympy.ntheory.modular import *

中国剩余定理解同余方程(模数需互质,前三个数为模数,后三个数为余数,返回第一个数为结果):

crt([99, 97, 95], [49, 76, 65])

\(\displaystyle \left( 639985, \ 912285\right)\)

解同余方程(模数不需互质但比中国剩余定理慢,每个元组第一个数为余数,第二个数为模数,返回第一个数为结果):

solve_congruence((2, 3), (3, 5), (2, 7))

\(\displaystyle \left( 23, \ 105\right)\)

from sympy.ntheory.residue_ntheory import *

求原根(如下2在模19意义下的所有幂占满了0到18):

primitive_root(19)

\(\displaystyle 2\)

求离散对数(如下\(7^3 mod 15 = 41\)):

discrete_log(41, 15, 7)

\(\displaystyle 3\)

Sympy常用函数总结的更多相关文章

  1. oracle常用函数及示例

    学习oracle也有一段时间了,发现oracle中的函数好多,对于做后台的程序猿来说,大把大把的时间还要学习很多其他的新东西,再把这些函数也都记住是不太现实的,所以总结了一下oracle中的一些常用函 ...

  2. 总结js常用函数和常用技巧(持续更新)

    学习和工作的过程中总结的干货,包括常用函数.常用js技巧.常用正则表达式.git笔记等.为刚接触前端的童鞋们提供一个简单的查询的途径,也以此来缅怀我的前端学习之路. PS:此文档,我会持续更新. Aj ...

  3. [转]SQL 常用函数及示例

    原文地址:http://www.cnblogs.com/canyangfeixue/archive/2013/07/21/3203588.html --SQL 基础-->常用函数 --===== ...

  4. PHP常用函数、数组方法

    常用函数:rand(); 生成随机数rand(0,50); 范围随机数时间:time(); 取当前时间戳date("Y-m-d H:i:s"); Y:年 m:月份 d:天 H:当前 ...

  5. Oracle常用函数

    前一段时间学习Oracle 时做的学习笔记,整理了一下,下面是分享的Oracle常用函数的部分笔记,以后还会分享其他部分的笔记,请大家批评指正. 1.Oracle 数据库中的to_date()函数的使 ...

  6. Thinkcmf:页面常用函数

    Thinkcmf:页面常用函数 全站seo: 文章列表: {$site_seo_title}        <!--SEO标题--> {$site_seo_keywords}   < ...

  7. matlab进阶:常用功能的实现,常用函数的说明

    常用功能的实现 获取当前脚本所在目录 current_script_dir = fileparts(mfilename('fullpath')); % 结尾不带'/' 常用函数的说明 bsxfun m ...

  8. iOS导航控制器常用函数与navigationBar常用属性

    导航控制器常用函数触发时机 当视图控制器的View将要出现时触发 - (void)viewWillAppear:(BOOL)animated 当视图控制器的View已经出现时触发 - (void)vi ...

  9. 《zw版·Halcon-delphi系列原创教程》 zw版-Halcon常用函数Top100中文速查手册

    <zw版·Halcon-delphi系列原创教程> zw版-Halcon常用函数Top100中文速查手册 Halcon函数库非常庞大,v11版有1900多个算子(函数). 这个Top版,对 ...

随机推荐

  1. JS之预解释原理

    预解释的原理 预解释的不同机制 var的预解释机制 function 的预解释机制 预解释机制 面试题练习 预解释的的不同机制 预解释也叫预声明,是提前解释声明的意思:预解释是针对变量和函数来说的:但 ...

  2. xdoj 2020校赛复盘

    平时写东西都不喜欢复盘,这肯定不是一个好习惯,感觉每次花好几个小时甚至好几天写题目然后没写出来也不去看题解是一种很蠢的行为( 花了这么久时间打校赛,虽然水平很low,数据结构也不太会用,还是记录一下自 ...

  3. c常用函数-strlen

    strlen 返回字符串长度 Action() { char test[] = "yyyHHHJJJoo"; unsigned int a; a = strlen(test); l ...

  4. Linux下自己和自己用各种方法进行文件的上传下载

    环境: Ubuntu 16.04 1.SCP # 上传 scp /home/sea/Desktop/test.sh sea@192.168.1.31:/home/sea/Desktop/test.sh ...

  5. git&github&Jenkins完成可持续集成

    1.安装git :想要安装Git首先要下载Git的安装包程序. Git安装包下载地址:https://git-scm.com/downloads/ 2.双击下载git安装包进入安装界面, 点击下一步, ...

  6. springMVC --@RequestParam注解(后台控制器获取参数)

    在SpringMVC后台控制层获取参数的方式主要有两种,一种是request.getParameter("name"),另外一种是用注解@RequestParam直接获取. 1.获 ...

  7. Python-17-作用域

    python有一个名为vars的内置函数,它返回变量关联的不可见的字典: >>> x = 1  >>> scope = vars()  >>> s ...

  8. 深入理解JVM(③)虚拟机性能监控、故障处理工具

    前言 JDK的bin目录中有一系列的小工具,除了java.exe.javac.exe这两个编译和运行Java程序外,还有打包.部署.签名.调试.监控.运维等各种场景都会用到这些小工具. 这些工具根据软 ...

  9. 【漏洞三】跨站点脚本(XSS)攻击

    [漏洞] 跨站点脚本(XSS)攻击 [原因] 跨站点脚本(也称为xss)是一个漏洞,攻击者可以发送恶意代码(通常在(Javascript的形式)给另一个用户.因为浏览器无法知道脚本是否值得信任,所以它 ...

  10. java关于传值与传引用

    关于java传值还是传引用的问题经常出现在一些烦人的面试题中,主要考察个人对java基础的掌握情况. 首先明确一下:本地方法中,java的参数传递都是传值.但是如果是远程调用方法时,会将对象本身传递过 ...