Python:妙解微积分

简介:一般进行数学算式的场景中,我们常用math模块进行均可以处理,但涉及到极限,微积分、高阶方程时math模块就捉襟见肘。sympy是一个支持科学计算的第三方库,专门解决数学运算问题。

相关攻略

Python内置库:数据计算相关 - math,random

安装:

pip install sympy

案例:

求解一元二次方程

因式分解和展开

解方程组

求极限

求导函数

求不定积分

求定积分

案例源码:

# -*- coding: utf-8 -*-
# time: 2022/6/23 21:56
# file: calculus.py
# 公众号: 玩转测试开发
from sympy import Symbol, solve, factor, expand, limit, diff, integrate

# 求解一元二次方程
x = Symbol('x')
expression = x ** 2 + 2 * x - 3
result1 = solve(expression, dict=True)
print(f"result1:{result1}")  # [{x: -3}, {x: 1}]

# 因式分解和展开
x = Symbol('x')
y = Symbol('y')
print(factor(x ** 2 - y ** 2))  # (x - y)*(x + y)
print(expand((x - y) ** 2))  # x**2 - 2*x*y + y**2

# 解方程组
x = Symbol('x')
y = Symbol('y')
exp1 = 2 * x - y + 4
exp2 = 3 * x + 2 * y - 1
result3 = solve((exp1, exp2))
print(f"result3:{result3}")  # {x: -1, y: 2}

# 求极限
result4 = limit(1 - 1 / x, x, 0)  # 1 - (1 / x) , x无限接近于0
print(f"result4:{result4}")  # result4:-oo

# 求导函数
x = Symbol('x')
result5 = diff(x ** 3 - 4, x)
print(f"result5:{result5}")  # result5:3*x**2

# 求不定积分
x = Symbol('x')
result6 = integrate(2 * x, x)
print(f"result6:{result6}")  # x**2

# 求定积分
x = Symbol('x')
result7 = integrate(1 / x ** 2, (x, -float("inf"), -1))
print(f"result7:{result7}")  # result7:1

map: map()会根据提供的函数对指定序列做映射。

语法:
map(function, iterable, ...)

参数:
function: 函数
iterable: 一个或多个序列

返回值:
Python 2.x 返回列表
Python 3.x 返回迭代器

reduce: reduce()函数会对参数序列中元素进行累积。Python3.x reduce() 已经被移到 functools 模块里,如果我们要使用,需要引入 functools 模块来调用 reduce() 函数:

语法:
reduce(function, iterable[, initializer])

参数:
function:函数,有两个参数
iterable:可迭代对象
initializer:可选,初始参数

返回值:
返回函数计算结果

zip: zip()函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表。

语法:
zip([iterable, ...])

参数:
iterabl:一个或多个迭代器;

返回值:
元组列表。

案例源码:

# -*- coding: utf-8 -*-
# time: 2022/5/30 14:07
# file: functionMethods.py
# author: tom
# 公众号: 玩转测试开发
from functools import reduce


def odd_number(x):
    return x % 2 == 1


def square(x):
    return x ** 2


def multiply(x, y):
    return x * y


# filter: 过滤器
new_list10 = list(filter(odd_number, [1, 2, 3, 4, 5]))
new_list11 = list(filter(lambda x: x % 2 == 1, [1, 2, 3, 4, 5]))
new_list12 = list(filter(lambda x: x > 3, [1, 2, 3, 4, 5]))

print(new_list10)  # [1, 3, 5]
print(new_list11)  # [1, 3, 5]
print(new_list12)  # [4, 5]

# map: 映射
new_list20 = list(map(square, [1, 2, 3, 4, 5]))
new_list21 = list(map(lambda x: x > 3, [1, 2, 3, 4, 5]))
new_list22 = list(map(lambda x, y, z: x + y + z, [1, 2], [2, 3], [3, 4]))  # 3 个列表,对相同位置的列表数据进行相加

print(new_list20)  # [1, 4, 9, 16, 25]
print(new_list21)  # [False, False, False, True, True]
print(new_list22)  # [6, 9]

# reduce: 对参数迭代器中的元素进行累积
new_list30 = reduce(multiply, [1, 2, 3])
new_list31 = reduce(lambda x, y: x + y, [1, 2, 3, 4, 5])
new_list32 = reduce(lambda x, y: x + y, ["a1", "b2", "c3"])
print(new_list30)  # 6
print(new_list31)  # 15
print(new_list32)  # a1b2c3

# zip: 打包聚合. *zip函数:会把zip聚合的之后的聚合在一起的几个元素以数组的形式返回
new_list40 = list(zip(("a", "b", "c"), [1, 2, 3]))
new_dict41 = dict(zip(("a", "b", "c"), [1, 2, 3]))
new_dict42 = dict(zip(("a", "b", "c"), [1, 2]))

print(new_list40)  # [('a', 1), ('b', 2), ('c', 3)]
print(new_dict41)  # {'a': 1, 'b': 2, 'c': 3}
print(new_dict42)  # {'a': 1, 'b': 2}   取短进行打包聚合

# *zip() 函数是zip()函数的逆过程,将zip对象变成原先组合前的数据。
a1, a2 = zip(*zip(("a", "b", "c"), [1, 2, 3]))
print(a1)  # ('a', 'b', 'c')
print(a2)  # (1, 2, 3)

微信公众号:玩转测试开发
欢迎关注,共同进步,谢谢!

你可能感兴趣的:(Python学习,python,开发语言)