这段代码:
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
生成由2个整数形成的所有可能的E
矩阵(维度3x3):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)*
代表标准matrix multiplication(行,列)
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
目标是获得所有这些3行向量的范数相同且大于10的A_SC
和E
矩阵(公式1),
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
答案 0 :(得分: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\
那应该可以解决你的问题。
请记住,当您未在计算机上明确使用符号计算时,即使在最简单的示例中也应该始终为数字错误做好准备
如果您需要检查 STRICT ,数学某些事情的相等性 - 您必须使用符号数学包和相关机器。
如果所需的平等更多是“物理”和“物理”。感觉(比如,什么力足以拉动那个盒子) - 然后我描述的方法就可以了,因为一些错误&#39;在物理世界中总是存在,你必须说明所需的容忍度(在这种情况下我们使用tol_2
)