简体   繁体   English

如果循环在 for 循环内起作用

[英]If loop acting inside a for loop

This piece of code:这段代码:

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

Generates all possible E matrices (dimensions 3x3) formed by 2 integers: 0 and 2 , eg:生成由 2 个整数组成的所有可能的E矩阵(维度 3x3): 02 ,例如:

...
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]])
...

Given this matrix equation:鉴于此矩阵方程:

A_SC = E * A     # Eqn. 1

where:在哪里:

1) * stands for the standard matrix multiplication (rows, columns) 1) *代表标准矩阵乘法(行、列)

2) A_SC , E and A are 3x3 matrices, 2) A_SCEA是 3x3 矩阵,

3) E are all the possible integer matrices generated by the above code. 3) E是上述代码生成的所有可能的整数矩阵。

4) A is a known matrix: 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]])

The A_SC matrix can be represented as 3 rows vectors: a1_SC , a2_SC and a3_SC : A_SC矩阵可以表示为 3 行向量: a1_SCa2_SCa3_SC

       |a1_SC|
A_SC = |a2_SC|
       |a3_SC|

For a given E matrix, there is a A_SC matrix.对于给定的E矩阵,有一个A_SC矩阵。

The following code:以下代码:

1) loops over all possible E matrices, 1) 遍历所有可能的E矩阵,

2) calculates the A_SC matrix, 2) 计算A_SC矩阵,

3) calculates the norm of a1_SC , a2_SC and a3_SC , 3) 计算a1_SCa2_SCa3_SC的范数,

4) and calculates the determinant of the E matrix in that iteration: 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

The goal would be to obtain all those A_SC and E matrices (Eqn. 1) for which the norm of these 3 rows vectors is the same and greater than 10,目标是获得所有这些A_SCE矩阵(方程 1),其中这 3 行向量的范数相同且大于 10,

norm(a1_SC) = norm(a2_SC) = norm(a3_SC) > 10

And at the same time, the determinant of E has to be greater than 0.0 .同时, E的行列式必须大于0.0 This condition can be expressed in this way: Just after this for loop, we can write an if loop:这个条件可以这样表达:在这个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

Just to be clear, this is the entire code:为了清楚起见,这是整个代码:

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

The problem is that no A_SC (and therefore no E_sol ) are printed.问题是没有A_SC (因此没有E_sol )。 If you run this code, all norms and determinants are printed at each iteration, for example the following:如果您运行此代码,则每次迭代时都会打印所有规范和行列式,例如以下内容:

a1_SC =  12.7513326014
a2_SC =  12.7513326014
a3_SC =  12.7513326014
det_indx_E =  8.0

This would be a perfect candidate, because it satisfies这将是一个完美的候选者,因为它满足

a1_SC = a2_SC = a3_SC = 12.7513326014 > 10.0

and

determinant > 0.0

However, no A_SC (and therefore no E_sol ) are printed... why is this happening?但是,没有A_SC (因此没有E_sol )......为什么会发生这种情况?

For example, this E matrix:例如,这个E矩阵:

        2 0 0
  E =   0 2 0
        0 0 2

has det = 8.0 , and is a candidate, because it has:det = 8.0 ,并且是候选人,因为它有:

a1_SC = a2_SC = a3_SC = 12.7513326014 > 10.0

The simple answer would be not to mistake double output as string with real underlying precision.简单的答案是不要将双输出误认为具有真正基础精度的字符串。 Simplest change:最简单的改变:

tol_2 = 1e-8

and change the tol_2 related conditions to limitation:并将 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\

That should solve your issue.那应该可以解决您的问题。

Remember that when you do not explicitly use symbolic computations on a computer, you should always be ready for numerical errors even in the simplest of examples请记住,当您没有在计算机上明确使用符号计算时,即使在最简单的示例中,您也应该始终为数值错误做好准备

And if you need to check for STRICT , mathematical equality of some things - you have to use symbolic math packages and related machinery.如果您需要检查STRICT ,某些事情的数学相等性 - 您必须使用符号数学包和相关机器。

In case if the required equality is more of a 'physical' sense (like, what force is enough for pulling that box) - then the approach I described would be OK since some 'error' is always present in a physical world, you just have to state required tolerance (which we do with tol_2 in this case)如果所需的相等性更像是一种“物理”意义(例如,拉动那个盒子的力足够大)-那么我描述的方法就可以了,因为物理世界中总是存在一些“错误”,您只需必须说明所需的容差(在这种情况下我们用tol_2做)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM