访问 Maxima 矩阵图中的 i 和 j 变量?

发布于 2024-09-16 19:17:17 字数 800 浏览 21 评论 0原文

我仍然是一个 maxima 新手,所以请耐心等待。我正在尝试编写自己的公式来计算矩阵的伴随(我知道 maxima 已经有一个内置公式,但我正在尝试自己的公式作为学习练习)。到目前为止我已经(对于 3x3 矩阵):

/* cofactor of some submatrix of the matrix, by deleting row i and column j */
cof(i, j, M) := determinant(submatrix(i, M, j));

/* for 3 x 3 matrix */
C3(M) := matrix( [cof(1,1,M), cof(1,2,M), cof(1,3)],
                 [cof(2,1,M), cof(2,2,M), cof(2,3)],
                 [cof(3,1,M), cof(3,2,M), cof(3,3)] );

/* function for calculating adjoint sign for x at position i, j */
adj_f(i, j, x) := -1^(i+j) * x;

/* adjugate for a 3x3 matrix M */
adj3(M) := matrixmap(lambda([i,j,x], adj_f(i,j,x), transpose(C3(M))));

我知道这可能不是最好的方法;但是,我想知道使用 MatrixMap 或 FullMapl 时是否有一种方法可以访问 i 和 j 元素?

(我正在使用 wxMaxima 并且我没有太多的 lisp 经验,我试图在不接触任何代码的情况下摆脱这个问题)。

I'm still a maxima newbie so bear with me. I am trying to write my own formula for calculating the adjoint of a matrix (I know maxima already has one built-in, but I was trying my own as a learning exercise). So far I have (for a 3x3 matrix):

/* cofactor of some submatrix of the matrix, by deleting row i and column j */
cof(i, j, M) := determinant(submatrix(i, M, j));

/* for 3 x 3 matrix */
C3(M) := matrix( [cof(1,1,M), cof(1,2,M), cof(1,3)],
                 [cof(2,1,M), cof(2,2,M), cof(2,3)],
                 [cof(3,1,M), cof(3,2,M), cof(3,3)] );

/* function for calculating adjoint sign for x at position i, j */
adj_f(i, j, x) := -1^(i+j) * x;

/* adjugate for a 3x3 matrix M */
adj3(M) := matrixmap(lambda([i,j,x], adj_f(i,j,x), transpose(C3(M))));

I know this probably isn't the best way of doing it; however, I was wondering if there was a way of accessing the i and j elements when using matrixmap or fullmapl?

(I'm using wxMaxima and I don't have a whole lot of lisp experience, I was trying to get away with this without touching any code).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那一片橙海, 2024-09-23 19:17:17

好吧,你不能用矩阵映射来做到这一点

 -- Function: matrixmap (<f>, <M>)
     Returns a matrix with element `i,j' equal to `<f>(<M>[i,j])'.

     See also `map', `fullmap', `fullmapl', and `apply'.

,因为 i 和 j 不是 M 的第 (i,j) 个元素的函数。

命令式解决方案可能如下所示:

adj3(M) :=
block([adjugate: transpose(C3(M))],
  for i: 1 thru 3 do
    for j: 1 thru 3 do
      adjugate[i,j]: adj_f(i,j,adjugate[i,j]),
  adjugate)$

请注意,你的 C3 函数缺少一些“M” 's 和 adj_f 必须为

adj_f(i, j, x) := (-1)^(i+j) * x;

(否则对于所有 i,j 都是 -( (1)^(i+j) ) = -1)。

Well, you can't do it with matrixmap

 -- Function: matrixmap (<f>, <M>)
     Returns a matrix with element `i,j' equal to `<f>(<M>[i,j])'.

     See also `map', `fullmap', `fullmapl', and `apply'.

since i and j are not functions of the (i,j)-th element of M.

An imperative solution might look like the following:

adj3(M) :=
block([adjugate: transpose(C3(M))],
  for i: 1 thru 3 do
    for j: 1 thru 3 do
      adjugate[i,j]: adj_f(i,j,adjugate[i,j]),
  adjugate)$

Note that your C3 function was missing some "M"'s and adj_f needs to be

adj_f(i, j, x) := (-1)^(i+j) * x;

(otherwise it's -( (1)^(i+j) ) = -1 for all i,j).

月野兔 2024-09-23 19:17:17

尝试使用 genmatrix 而不是矩阵图。 genmatrix 的第一个参数是一个以 i 和 j 作为参数的函数。

(%i2) cof (i, j, M) := determinant (submatrix (i, M, j)) $
(%i3) adj_sign (i, j) := (-1)^(i + j) $
(%i4) M : matrix([1, 2, 3], [-1, 2, 3], [1, -2, 3]) $
(%i5) my_inverse (M) := (1 / determinant (M)) * genmatrix (lambda ([i, j], cof (j, i, M) *  adj_sign (i, j)), 3, 3) $
(%i6) M1 : my_inverse (M);
(%o6) matrix([1/2,-1/2,0],[1/4,0,-1/4],[0,1/6,1/6])

抱歉回复晚了。将其留在这里,以防有人通过搜索找到它。

Try genmatrix instead of matrixmap. First argument of genmatrix is a function which takes i and j as arguments.

(%i2) cof (i, j, M) := determinant (submatrix (i, M, j)) $
(%i3) adj_sign (i, j) := (-1)^(i + j) $
(%i4) M : matrix([1, 2, 3], [-1, 2, 3], [1, -2, 3]) $
(%i5) my_inverse (M) := (1 / determinant (M)) * genmatrix (lambda ([i, j], cof (j, i, M) *  adj_sign (i, j)), 3, 3) $
(%i6) M1 : my_inverse (M);
(%o6) matrix([1/2,-1/2,0],[1/4,0,-1/4],[0,1/6,1/6])

Sorry for the late reply. Leaving this here in case someone finds it by searching.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文