目录
Checking that it works
# import mpmath as mp
from mpmath import *
from icecream import ic
mp.dps = 50 # Set precision
print(mpf(2)**mpf('0.5'))
1. Introduction
1.2 Basic usage of mpmath
mpmath的数值类型: mpf(real float),mpc(complex float),matrix
print(mp.dps)
print(mp.cos(3.14))
a1=mpf(4)
a2=mpf('4.0')
ic(a1,a2)
a3=mpf("1.25e6")
ic(a3)
a4=mpf("-inf")
ic(a4)
a5=mpc(1,2)
ic(a5)
a6=mpc(complex(1,20))
ic(a6)
ic(a5.conjugate()) # 共轭复数
ic(a5.real) # 实部
ic(a5.imag) # 虚部
Mpmath使用全局工作精度;它不跟踪单个数字的精度或准确性。执行算术运算或调用mpf()将结果舍入到当前的工作精度。工作精度由一个名为mp的上下文对象控制
print(mp)
# Mpmath settings:
# mp.prec = 169 [default: 53]
# mp.dps = 50 [default: 15]
# mp.trap_complex = False [default: False]
# prec---二进制精度
# dps---十进制精度
# prec = 3.33*dps
# 当创建一个新的mpf时,该值最多将与输入一样准确
#!!警告: 将mpmath数字与Python浮点数混合使用时要小心
mp.dps = 30
# bad
ic(mpf(10.8))
# good
ic(mpf('10.8'))
# also good
ic(mpf(108)/mpf(10))
# ? 然而,0.5、1.5、0.75、0.125等通常作为输入是安全的,因为它们可以用Python浮点数精确地表示
1.3 输出格式化
# 设置 mp.pretty = True 可以输出更好看的结果
mp.pretty = True
mpf(0.6)
# output: 0.599999999999999977795539507497
mp.pretty = False
mpf(0.6)
# output: mpf('0.599999999999999977795539507496869')
1.4 输出的小数点位数
use mpmath.nstr() and mpmath.nprint() 函数.默认情况下打印数字的位数由工作精度决定。要指定要显示的位数,而不改变工作精度,
a=mpf(1)/6.0
mp.nstr(a, 5)
# output: '0.16667'
mp.nprint(a, 10)
# output: '0.1666666667'
2. BASIC FEATURES
mpmath中的高级代码是作为“上下文对象”上的方法实现的。上下文实现算术、类型转换和其他基本操作。上下文还保存诸如精度之类的设置,并存储缓存数据。提供了一些不同的上下文(具有基本兼容的接口),以便高级算法可以与底层算法的不同实现一起使用,从而允许不同的特性和速度-精度权衡。
目前,mpmath提供了以下上下文:
- Arbitrary-precision arithmetic (mp)
- A faster Cython-based version of mp (used by default in Sage, and currently only available there)
- Arbitrary-precision interval arithmetic (iv)
- Double-precision arithmetic using Python’s builtin float and complex types (fp)
2.1 Arbitrary-precision floating-point (mp)
mp支持任意精度,并且支持绝大多数函数,经过充分测试,充分优化.
2.2 Arbitrary-precision interval arithmetic (iv)
iv支持区间运算,但是在版本1.3.0中,iv还处于测试阶段,只支持少部分函数.
2.3 Double-precision arithmetic using Python’s builtin float and complex types (fp)
尽管mpmath通常是为任意精度算术设计的,但许多高级算法可以很好地处理普通的Python浮点数和复数,它们使用硬件双精度(在大多数系统上,这相当于53位精度).mp object的函数会把输入转换为mpmath numbers type. fp object则是将输入转换为Python的浮点数和复数类型.当需要进行大量的函数求值(数值积分、绘图等),并且当fp算法提供足够的精度时,这可以大大提高mp算法的速度。
基于以上优势,可以使用fp.func 进行运算.(使用func 或mp.func 都是mp的函数)
使用fp算法计算的结果可能比使用等效精度(mp)的mp计算的结果不那么精确.(mp.prec = 53),因为后者经常使用更高的内部精度。其精度高度依赖于:
- for some functions, fp almost always gives 14-15 correct digits; for others, results can be accurate to only 2-3 digits or even completely wrong.
- The recommended use for fp is therefore to speed up large-scale computations where accuracy can be verified in advance on a subset of the input set, or where results can be verified afterwards.
fp.pretty=True
u=fp.rand()
print(u)
# 0.6473090346719316
print(type(u))
# <class 'float'>
m=fp.matrix([[1,2],[3,4]])**2
print(m)
# [ 7.0 10.0]
# [15.0 22.0]
print(type(m))
# <class 'mpmath.matrices.matrices.matrix'>
2.4 基本函数
fp bject 为基本函数 包装了Python的math和cmath模块。支持实数和复数,并自动为实数输入生成复数结果(数学会引发异常)
fp.sqrt(5)
# 2.23606797749979
fp.sqrt(-5)
# 2.23606797749979j
fp.sin(fp.pi/2)
# 1.0
fp.power(-2,0.5)
# (8.659560562354934e-17+1.4142135623730951j)
print(type(fp.power(-2,0.5)))
# <class 'complex'>
2.4 实用函数
2.4.1 converion and printing
# mp.mpmathify(x,strings=True)
# 能够将x转换为mpf or mpc类型
fp.dps=15
fp.pretty = True
mpmathify(3.5)
# mpf('3.5')
mpmathify(3+4j)
#mpc(real='3.0', imag='4.0')
mpmathify('3.5')
# mpf('3.5')
# mpmath.nstr(x,n=6,**kwargs)
# 将mpf或mpc转换为具有n位有效数字的十进制字符串
# nprint(x,n=6) 会直接打印nstr()的字符串结果
f=mp.mpf(1.53)
print(f)
nprint(f,n=12)
# 1.530000000000000026645352591
# 1.53
2.4.2 算术运算
包括加减乘除、取模、幂运算等。
# ! mpmath.fadd(x, y,**kwargs)
# 实现x+y,返回浮点数结果.可以自定义精度和舍入模式.默认精度是工作精度,
# kwargs中可以设置精度和舍入模式.:
# + prec,dps(int)--设置精度; exact=True--不四舍五入.
# + rounding(str)--设置舍入模式,可选'n'(default)--nearst,'f'--floor,'c'-ceiling,'d'-down,'u'-up.
x=mp.rand()
y=mp.rand()
ic(x,y)
nprint(mp.fadd(x,y,dps=5,rounding='u'))
# ic| x: mpf('0.936352251244129142275205120579475')
# y: mpf('0.118268786487759443295747691143063')
# 1.05462
# ! mpmath.fsub(x, y,**kwargs)
# 实现x-y,返回浮点数结果.
x=mp.mpf(1.53)
y=mp.mpf(2.87)
print(x-y)
# -1.34000000000000007993605777301
print(mp.fsub(x,y))
# -1.34000000000000007993605777301
# ! mpmath.fneg(x,**kwargs)
# 实现-x,返回浮点数结果.
print(mp.fneg(mp.mpf(2.5)))
# -2.5
# ! mpmath.fmul(x, y,**kwargs)
# 实现x*y,返回浮点数结果.
x=mp.mpf(2.5)
y=mp.mpf(3.14)
print(x*y)
# 7.85000000000000031086244689504
print(mp.fmul(x,y))
# 7.85000000000000031086244689504
a=mp.mpc(2,5)
b=mp.mpc(3,4)
print(a*b)
# (-14.0 + 23.0j)
print(mp.fmul(a,b))
# (-14.0 + 23.0j)
# ! mpmath.fdiv(x, y,**kwargs)
# 实现x/y,返回浮点数结果.
x=mp.mpf(3)
y=mp.mpf(2)
print(x/y)
# 1.5
print(mp.fdiv(x,y))
# 1.5
# ! mpmath.fmod(x,y)
# Converts
标签:mpf,fp,笔记,学习,Mpmath,mp,print,mpmath,精度
From: https://www.cnblogs.com/aksoam/p/18279394