您的位置:首页 > 编程语言 > Python开发

『Numpy学习指南』第五章_矩阵和通用函数

2017-05-17 18:57 393 查看

简单的矩阵生成以及合并操作:

np.mat('1 2 3;4 5 6;7 8 9')
np.bmat('A B;B A')
np.arange(1,10).reshape(3,3)

import numpy as np

'''通用函数'''

# 字符串创建矩阵
# 也可以使用数组创建
A = np.mat('1 2 3;4 5 6;7 8 9')
# 数组创建矩阵
A = np.mat(np.arange(1,10).reshape(3,3))     # ndarray可以直接转化为矩阵
print('创建矩阵:\n',A)
print('转置矩阵:\n',A.T)
print('矩阵求逆:\n',A.I)

# 字符串创建复合矩阵
A = np.eye(2)
B = A*2
print(np.bmat('A B;B A'))


创建矩阵:
[[1 2 3]
[4 5 6]
[7 8 9]]
转置矩阵:
[[1 4 7]
[2 5 8]
[3 6 9]]
矩阵求逆:
[[ -4.50359963e+15   9.00719925e+15  -4.50359963e+15]
[  9.00719925e+15  -1.80143985e+16   9.00719925e+15]
[ -4.50359963e+15   9.00719925e+15  -4.50359963e+15]]
[[ 1.  0.  2.  0.]
[ 0.  1.  0.  2.]
[ 2.  0.  1.  0.]
[ 0.  2.  0.  1.]]


通用函数生成以及4个方法:

np.frompyfunc(ultimate_answer,1,1)

np.add.reduce(a)

np.add.accumulate(a)

np.add.reduceat(a,[0,5,2,7])

np.add.outer(np.arange(3),np.arange(9))

'''通用函数'''

# 输入一组标量,输出一组标量的基本数学运算操作
# frompyfunc方法创建通用函数(此时和原函数并无什么差异)
def ultimate_answer(a):
result = np.zeros_like(a)
result.flat = 42
return result
ufunc = np.frompyfunc(ultimate_answer,1,1)
print(ultimate_answer(np.arange(4).reshape(2,2)))
print(ufunc(np.arange(4).reshape(2,2)))

# 通用函数的方法
# 只对连个输入一个输出的函数有效

# reduce
# 连续递归的作用通用函数于数组元素上
a = np.arange(9)
print('Reduce:',np.add.reduce(a))

# accumulate
# 同上,但返回每一步的结果
print('Accumulate:',np.add.accumulate(a))

# reduceat
# 使用索引分区递归
# [0,5],[5,2](直接返回索引5),[2,7],[7:]
print('Reduceat:',np.add.reduceat(a,[0,5,2,7]))

# outer
# 输入两个数组
# 将后一个数组的每一个元素和前一个数组的每一个元素做运算
print('Outer:\n',np.add.outer(np.arange(3),np.arange(9)))


[[42 42]
[42 42]]
[[array(42) array(42)]
[array(42) array(42)]]
Reduce: 36
Accumulate: [ 0  1  3  6 10 15 21 28 36]
Reduceat: [10  5 20 15]
Outer:
[[ 0  1  2  3  4  5  6  7  8]
[ 1  2  3  4  5  6  7  8  9]
[ 2  3  4  5  6  7  8  9 10]]


np.remainder(a,2)

np.mod(a,2)

a % 2

np.mod(a,-2)

np.fmod(a,-2)

'''算数运算'''

# 算数运算符隐式关联通用函数
# +:add
# -:substract
# *:multiply
# 除法
a = np.array([2,6,5])
b = np.array([1,2,3])
# 3.x中divide和/等价
print('Divide:\n',np.divide(a,b),np.divide(b,a))
print('',a/b,b/a)
print('',a//b,b//a)        # 地板除

# 求余
a = np.arange(-4,4)
# 三种等价求余操作
# 余数符号等于除数符号
print('Remainder:',np.remainder(a,2))
print('Mod      :',np.mod(a,2))
print('%        :',a % 2)
# fmod求余操作
# 余数符号由被除数确定
print('Mod      :',np.mod(a,-2))
print('Fmod     :',np.fmod(a,-2))


Divide:
[ 2.          3.          1.66666667] [ 0.5         0.33333333  0.6       ]
[ 2.          3.          1.66666667] [ 0.5         0.33333333  0.6       ]
[2 3 1] [0 0 0]
Remainder: [0 1 0 1 0 1 0 1]
Mod      : [0 1 0 1 0 1 0 1]
%        : [0 1 0 1 0 1 0 1]
Mod      : [ 0 -1  0 -1  0 -1  0 -1]
Fmod     : [ 0 -1  0 -1  0  1  0  1]


np.matrix([[1,1],[1,0]])

np.rint((phi**n-(-1/phi)**n)/sqrt5)

'''斐波那契数列'''

# 矩阵方式求数列
F = np.mat([[1,1],[1,0]])
F = np.matrix([[1,1],[1,0]])
print('F:\n',F)
print('斐波那契数列第八个值是:',(F**7)[0,0])       # matrix只能[x,y],不能[x][y]

# 比奈公式求数列
n = np.arange(1,9)
sqrt5 = np.sqrt(5)                               # 开根号
phi = (1+sqrt5)/2
fibonacci = np.rint((phi**n-(-1/phi)**n)/sqrt5)  # 四舍五入取整,但结果仍然是float
print(fibonacci)


F:
[[1 1]
[1 0]]
斐波那契数列第八个值是: 21
[  1.   1.   2.   3.   5.   8.  13.  21.]


np.sin(a*t+np.pi/2)

'''利萨茹曲线-熟悉三角函数的使用'''

# x = A sin(at + n/2)
# y = B sin(bt)
import matplotlib.pyplot as plt

A,B,a,b = (1,1,10,13)
t = np.linspace(-np.pi,np.pi,201)                 # np.pi
x = np.sin(a*t+np.pi/2)
y = np.sin(b*t)
plt.plot(x,y)
plt.xlim([-1,1])
plt.ylim([-1,1])
# plt.show()


使用矢量化函数取缔循环提升效率

'''绘制方波'''

t = np.linspace(-np.pi,np.pi,201)
k = np.arange(1,99)
k = 2*k - 1
f = np.zeros_like(t)
# for i in range(len(t)):
#     f[i] = np.sum(np.sin(k*t[i])/k)
# f = (4/np.pi)*f

def func(t):
return np.sum(np.sin(k*t)/k)
func = np.vectorize(func)
f = func(t)
f = (4/np.pi)*f

plt.plot(t,f)
# plt.show()


位操作,虽然感觉对我这种计算机组成原理学的一般(为了研究生复试看了一个月,这书学过的都知道多坑何况我这种背景知识几近全无的人)的人没啥大用,但既然书里面都写了我就放这里吧。

x^y

x&(x-1)

x&((1<<2)-1)

'''位操作'''

# ^异或操作
# &和操作
# <<左移操作
x = np.arange(-9,9)
y = -x
print(x,'\n',(x^y)<0)
print(x,'\n',x&(x-1)==0)
print(x,'\n',x&((1<<2)-1))


[-9 -8 -7 -6 -5 -4 -3 -2 -1  0  1  2  3  4  5  6  7  8]
[ True  True  True  True  True  True  True  True  True False  True  True
True  True  True  True  True  True]
[-9 -8 -7 -6 -5 -4 -3 -2 -1  0  1  2  3  4  5  6  7  8]
[False False False False False False False False False  True  True  True
False  True False False False  True]
[-9 -8 -7 -6 -5 -4 -3 -2 -1  0  1  2  3  4  5  6  7  8]
[3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: