向量是有序的有限数字列表。 向量通常写成垂直数组,用方括号或弯括号括起来,如
[ − 1.1 0.0 3.6 − 7.2 ] 或 ( − 1.1 0.0 3.6 − 7.2 ) \left[\begin{array}{r} -1.1 \\ 0.0 \\ 3.6 \\ -7.2 \end{array}\right] \quad \text { 或 } \quad\left(\begin{array}{r} -1.1 \\ 0.0 \\ 3.6 \\ -7.2 \end{array}\right) −1.10.03.6−7.2 或 −1.10.03.6−7.2
它们也可以写成用逗号分隔并用括号括起来的数字。 在这种符号风格中,上面的向量被写为
( − 1.1 , 0.0 , 3.6 , − 7.2 ) (-1.1,0.0,3.6,-7.2) (−1.1,0.0,3.6,−7.2)
Python示例:
import numpy as np
x = np.array([-1.1,0.0,3.6,-7.2])
y = np.array([-1.1,0.0,3.6,-7.2])
x,y, len(x), len(y)
输出:
(array([-1.1, 0. , 3.6, -7.2]), array([-1.1, 0. , 3.6, -7.2]), 4, 4)
块或堆叠向量
x = np.array([1,-2]); y = np.array([1,1,0]);
z = np.concatenate((x,y))
z
输出:
array([ 1, -2, 1, 1, 0])
子向量和切片
x = np.array([9,4,3,0,5])
y = x[1:4]
y
输出:
array([4, 3, 0])
一阶差分向量
x = np.array([1,0,0,-2,2])
np.array([x[i+1]-x[i] for i in range(len(x)-1)])
输出:
array([-1, 0, -2, 4])
矩阵是写在方括号之间的矩形数组,如
[ 0 1 − 2.3 0.1 1.3 4 − 0.1 0 4.1 − 1 0 1.7 ] \left[\begin{array}{cccc} 0 & 1 & -2.3 & 0.1 \\ 1.3 & 4 & -0.1 & 0 \\ 4.1 & -1 & 0 & 1.7 \end{array}\right] 01.34.114−1−2.3−0.100.101.7
使用大圆括号代替方括号也很常见,如
( 0 1 − 2.3 0.1 1.3 4 − 0.1 0 4.1 − 1 0 1.7 ) \left(\begin{array}{cccc} 0 & 1 & -2.3 & 0.1 \\ 1.3 & 4 & -0.1 & 0 \\ 4.1 & -1 & 0 & 1.7 \end{array}\right) 01.34.114−1−2.3−0.100.101.7
Python示例
import numpy as np
import numpy.linalg as npl
import math
import matplotlib.pyplot as plt
pg112 = np.matrix([[0,1,1,0],[1,0,0,1],[0,0,0,1],[1,0,0,0]])
G = nx.from_numpy_matrix(pg112)
pos = nx.layout.spring_layout(G)
node_sizes = [3 + 10 * i for i in range(len(G))]
M = G.number_of_edges()
edge_colors = range(2, M + 2)
edge_alphas = [(5 + i) / (M + 4) for i in range(M)]
nodes = nx.draw_network_nodes(G, pos, node_size=node_sizes, node_color='blue', with_labels=True)
edges = nx.draw_network_edges(G, pos, node_size=node_sizes, arrowstyle='->',
arrowsize=10, edge_color=edge_colors,
edge_cmap=plt.cm.Blues, width=2)
ax = plt.gca()
ax.set_axis_off()
plt.show()