为什么 inv(matrix)*matrix 在 Octave 中不是精确的单位矩阵?

问题描述

为什么 inv(A)*A 不是精确的单位矩阵? 所有对角线元素都是正确的,但其余的则不正确。 我知道这是残差,那怎么处理?

代码

A = [1,2,0;0,5,6;7,9]
A_inv = inv(A)
A_invA = inv(A)*A

输出

code

解决方法

model User { id Int @id @default(autoincrement()) ..... email String @unique diets Diet[] @relation("diets") dietsGivenBy Diet[] @relation("dietsGivenBy") Diets Diets[] } model mealPlan { id Int @id @default(autoincrement()) name String fooditems Json user User? @relation(fields: [userid],references: [id]) userid Int? } 文档的探索引导您沿着以下路径前进,它很好地回答了您的问题(重点是我的):

octave:1> help inv
'inv' is a built-in function from the file libinterp/corefcn/inv.cc

 -- X = inv (A)
 -- [X,RCOND] = inv (A)
 -- [...] = inverse (...)
     Compute the inverse of the square matrix A.

     Return an estimate of the reciprocal condition number if requested,otherwise warn of an ill-conditioned matrix if the reciprocal
     condition number is small.

     In general it is best to avoid calculating the inverse of a matrix
     directly.  For example,it is both faster and more accurate to
     solve systems of equations (A*x = b) with 'Y = A \ b',rather than
     'Y = inv (A) * b'.

在您的特定情况下,您会看到:

  Mutation: {
    giveDiet: async (_,args,context) => {
        console.log(args)

      try {
        const haha = await client.user.Diets({

          data: {
            name: args.data.name,userid: args.data.id,fooditems: args.data.fooditems,},});


export default gql`
  type giveDiet {
    ok: Boolean!
    diet: String
    error: String
  }


  input CreatFoodItems {

    Meal1: { // **MAX SHOULD be 4**
        foodname: String!
        qty: Int!
    protein: Int!
    carbs: Int!
    fat: Int!
}

  }
    
      type Mutation {
        giveDiet(userid: Int!,name: String!,fooditems: CreatFoodItems!): giveDiet!
      }
    `;

那么,这个值是什么意思?你可以在相关函数inv中找到答案,直接计算这个值:

octave:2> help rcond
'rcond' is a built-in function from the file libinterp/corefcn/rcond.cc

 -- C = rcond (A)
     Compute the 1-norm estimate of the reciprocal condition number as
     returned by LAPACK.

     If the matrix is well-conditioned then C will be near 1 and if the
     matrix is poorly conditioned it will be close to 0.

     [...]

     See also: cond,condest.

您的值为 0.07,非常接近于 0,因此您的 A 矩阵条件相当差。

要详细了解“条件不佳”的确切含义,我们可以查看 A = [1,2,0;0,5,6;7,9]; [X,RCOND] = inv(A); RCOND % RCOND = 0.070492 函数:

octave:26> help cond
'cond' is a function from the file /opt/octave-6.2.0/share/octave/6.2.0/m/linear-algebra/cond.m

 -- cond (A)
 -- cond (A,P)
     Compute the P-norm condition number of a matrix with respect to
     inversion.

     'cond (A)' is defined as 'norm (A,P) * norm (inv (A),P)'.

     [...]

     The condition number of a matrix quantifies the sensitivity of the
     matrix inversion operation when small changes are made to matrix
     elements.  Ideally the condition number will be close to 1.  When
     the number is large this indicates small changes (such as underflow
     or round-off error) will produce large changes in the resulting
     output.  In such cases the solution results from numerical
     computing are not likely to be accurate.

就你而言:

rcond

所以你有它。您的矩阵条件相对较差,这意味着其反演更容易受到精度误差的影响。如果您改用 cond(即 cond(A,2) % ans = 7.080943875445246 运算符),可能会获得更好的结果。