这段代码:
K = 3
N = 3
E = [np.reshape(np.array(i), (K, N)) for i in itertools.product([0, 1, -1], repeat = K*N)]
print 'E = ', E
生成所有可能的E
矩阵(维度 3x3),由 2 个整数组成:0
和2
,例如:
...
array([[0, 2, 2],
[0, 0, 0],
[2, 0, 0]]), array([[0, 2, 2],
[0, 0, 0],
[2, 0, 2]]), array([[0, 2, 2],
[0, 0, 0],
[2, 2, 0]])
...
给定此矩阵方程:
A_SC = E * A # Eqn. 1
哪里:
1)*
代表标准矩阵乘法(行、列)
2)A_SC
,E
和A
是 3x3 矩阵,
3)E
都是由上述代码生成的所有可能的整数矩阵。
4)A
是一个已知的矩阵:
A =np.array([[ 0.288155519353E+01, 0.000000000000E+00, 0.568733333333E+01],
[ -0.144077759676E+01, 0.249550000000E+01, 0.568733333333E+01],
[ -0.144077759676E+01, -0.249550000000E+01, 0.568733333333E+01]])
A_SC
矩阵可以表示为 3 行向量:a1_SC
、a2_SC
和a3_SC
:
|a1_SC|
A_SC = |a2_SC|
|a3_SC|
对于给定的E
矩阵,有一个A_SC
矩阵。
以下代码:
1) 遍历所有可能的E
矩阵,
2)计算A_SC
矩阵,
3)计算a1_SC
、a2_SC
和a3_SC
的范数,
4) 并计算该迭代中E
矩阵的行列式:
for indx_E in E:
A_SC = np.dot(indx_E,A)
a1_SC = np.linalg.norm(A_SC[0])
a2_SC = np.linalg.norm(A_SC[1])
a3_SC = np.linalg.norm(A_SC[2])
det_indx_E = np.linalg.det(indx_E)
print 'a1_SC = ', a1_SC
print 'a2_SC = ', a2_SC
print 'a3_SC = ', a3_SC
print 'det_indx_E = ', det_indx_E
目标是获得所有那些A_SC
和E
矩阵 (方程 1),这些矩阵的 3 行向量的范数相同且大于 10,
norm(a1_SC) = norm(a2_SC) = norm(a3_SC) > 10
同时,E
的行列式必须大于0.0
. 这个条件可以这样表示:在这个for
循环之后,我们可以编写一个if
循环:
tol_1 = 10
tol_2 = 0
for indx_E in E:
A_SC = np.dot(indx_E,A)
a1_SC = np.linalg.norm(A_SC[0])
a2_SC = np.linalg.norm(A_SC[1])
a3_SC = np.linalg.norm(A_SC[2])
det_indx_E = np.linalg.det(indx_E)
print 'a1_SC = ', a1_SC
print 'a2_SC = ', a2_SC
print 'a3_SC = ', a3_SC
print 'det_indx_E = ', det_indx_E
if a1_SC > tol_1
and a2_SC > tol_1
and a3_SC > tol_1
and abs(a1_SC - a2_SC) == tol_2
and abs(a1_SC - a3_SC) == tol_2
and abs(a2_SC - a3_SC) == tol_2
and det_indx_E > 0.0:
print 'A_SC = ', A_SC
print 'a1_SC = ', a1_SC
print 'a2_SC = ', a2_SC
print 'a3_SC = ', a3_SC
print 'det_indx_E = ', det_indx_E
# Now, which is the `E` matrix for this `A_SC` ?
# A_SC = E * A # Eqn. 1
# A_SC * inv(A) = E * A * inv(A) # Eqn. 2
#
# ------------------------------
# | A_SC * inv(A) = E # Eqn. 3 |
# ------------------------------
E_sol = np.dot(A_SC, np.linalg.inv(A))
print 'E_sol = ', E_sol
需要明确的是,这是整个代码:
A =np.array([[ 0.288155519353E+01, 0.000000000000E+00, 0.568733333333E+01],
[ -0.144077759676E+01, 0.249550000000E+01, 0.568733333333E+01],
[ -0.144077759676E+01, -0.249550000000E+01, 0.568733333333E+01]])
K = 3
N = 3
E = [np.reshape(np.array(i), (K, N)) for i in itertools.product([0, 1, -1], repeat = K*N)]
print 'type(E) = ', type(E)
print 'E = ', E
print 'len(E) = ', len(E)
tol_1 = 10
tol_2 = 0
for indx_E in E:
A_SC = np.dot(indx_E,A)
a1_SC = np.linalg.norm(A_SC[0])
a2_SC = np.linalg.norm(A_SC[1])
a3_SC = np.linalg.norm(A_SC[2])
det_indx_E = np.linalg.det(indx_E)
print 'a1_SC = ', a1_SC
print 'a2_SC = ', a2_SC
print 'a3_SC = ', a3_SC
print 'det_indx_E = ', det_indx_E
if a1_SC > tol_1
and a2_SC > tol_1
and a3_SC > tol_1
and abs(a1_SC - a2_SC) == tol_2
and abs(a1_SC - a3_SC) == tol_2
and abs(a2_SC - a3_SC) == tol_2
and det_indx_E > 0.0:
print 'A_SC = ', A_SC
print 'a1_SC = ', a1_SC
print 'a2_SC = ', a2_SC
print 'a3_SC = ', a3_SC
print 'det_indx_E = ', det_indx_E
# Now, which is the `E` matrix for this `A_SC` ?
# A_SC = E * A # Eqn. 1
# A_SC * inv(A) = E * A * inv(A) # Eqn. 2
#
# ------------------------------
# | A_SC * inv(A) = E # Eqn. 3 |
# ------------------------------
E_sol = np.dot(A_SC, np.linalg.inv(A))
print 'E_sol = ', E_sol
问题是没有打印A_SC
(因此没有E_sol
)。 如果运行此代码,则在每次迭代时都会打印所有规范和行列式,例如:
a1_SC = 12.7513326014
a2_SC = 12.7513326014
a3_SC = 12.7513326014
det_indx_E = 8.0
这将是一个完美的候选人,因为它满足
a1_SC = a2_SC = a3_SC = 12.7513326014 > 10.0
和
determinant > 0.0
但是,没有打印A_SC
(因此没有E_sol
)...为什么会这样?
例如,此E
矩阵:
2 0 0
E = 0 2 0
0 0 2
具有det = 8.0
,并且是候选者,因为它具有:
a1_SC = a2_SC = a3_SC = 12.7513326014 > 10.0
简单的答案是不要将双输出误认为具有真正底层精度的字符串。最简单的更改:
tol_2 = 1e-8
并将tol_2相关条件更改为限制:
and abs(a1_SC - a2_SC) <= tol_2
and abs(a1_SC - a3_SC) <= tol_2
and abs(a2_SC - a3_SC) <= tol_2
这应该可以解决您的问题。
请记住,当您没有在计算机上显式使用符号计算时,即使在最简单的示例中,您也应该始终准备好数值错误
如果你需要检查某些东西的严格数学相等性- 你必须使用符号数学包和相关机制。
如果所需的平等更像是"物理"意义上的(例如,什么力足以拉动那个盒子) - 那么我描述的方法就可以了,因为物理世界中总是存在一些"错误",你只需要说明所需的公差(在这种情况下,我们用tol_2
这样做)