gpt4 book ai didi

list - 两个矩阵的乘法

转载 作者:太空宇宙 更新时间:2023-11-03 18:58:24 24 4
gpt4 key购买 nike

我想问一下如何将两个矩阵相乘。我有矩阵 mat1 '((1 2) (4 5)) 和 mat2 '((3 6) (7 8))。我刚刚实现了这段代码:

(defun multi_matrices (mat1 mat2)
(cond((or (null mat1) (null mat2)) nil)
((not(eq (length mat1) (length mat2))) nil)
(( format t "~a" (mapcar #'* (car mat1) (car mat2))))
((cdr mat1)(multi_matrices (cdr mat1) (cdr mat2)) (print 'OK))))

但我明白了:

(multi_matrices '((1 2) (4 5)) '((3 6) (7 8)))

(3 12)(28 40)

那部分没问题,但我不知道如何求和乘法要求的元素:此链接显示 how to multiply matrices

感谢您的帮助!

最佳答案

试试这个:

CL-USER> (defun mmat (mat1 mat2)
"multiply two matrices as lists of lists"
(loop with num-rows1 = (length mat1)
for row1 in mat1
collect (loop for c from 0 below num-rows1
collect (loop for e in row1
for r from 0
sum (* e (nth c (nth r mat2)))))))
MMAT
CL-USER> (mmat '((1 2 3)(4 5 6)) '((7 8)(9 10)(11 12)))
((58 64) (139 154))

该函数不检查输入是否正确。

关于list - 两个矩阵的乘法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45181074/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com