明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2183|回复: 6

lisp中有没有矩阵运算函数

[复制链接]
发表于 2009-8-11 14:06:00 | 显示全部楼层 |阅读模式

请哪位大侠指点,lisp中有没有有关矩阵运算的函数?

发表于 2009-8-11 19:44:00 | 显示全部楼层

2000以后版的VLISP有此功能。看vlax-make-safearray函数。

 楼主| 发表于 2009-8-12 13:15:00 | 显示全部楼层
可是没有找到像两个矩阵相加或相乘的函数
发表于 2009-8-12 13:32:00 | 显示全部楼层
theswamp上找到的代码,不记得具体链接了。
  1. ;;; gile-trp transpose a matrix -doug wilson-
  2. (defun gile-trp (m)
  3.   (apply
  4.     'mapcar
  5.     (cons 'list m)
  6.   )
  7. )
  8. ;;; gile-cofact (gile)
  9. ;;; returns the gile-cofactor associated to ij item of a matrix
  10. ;;;
  11. ;;; arguments
  12. ;;; i = row index (first row = 1)
  13. ;;; j = column index (first column = 1)
  14. ;;; m = a matrix
  15. (defun gile-cofact (i j m)
  16.   (* (gile-determ (th-remove-nth (1- i) (mapcar
  17.        (function (lambda (x)
  18.             (th-remove-nth
  19.              (1- j)
  20.              x
  21.             )
  22.           )
  23.        )
  24.        m
  25.      )
  26.     )
  27.      ) (expt -1 (+ i j))
  28.   )
  29. )
  30. ;;; gile-determ (gile)
  31. ;;; returns the déterminant of a matrix
  32. ;;;
  33. ;;; argument : a matrix
  34. (defun gile-determ (m)
  35.   (if (= 2 (length m))
  36.     (- (* (caar m) (cadadr m)) (* (caadr m) (cadar m)))
  37.     ((lambda (r n)
  38.        (apply
  39.   '+
  40.   (mapcar
  41.     (function (lambda (x)
  42.          (* x (gile-cofact 1 (setq n (1+ n))
  43.       m
  44.        )
  45.          )
  46.        )
  47.     )
  48.     r
  49.   )
  50.        )
  51.      )
  52.      (car m)
  53.      0
  54.     )
  55.   )
  56. )
  57. ;;; gile-adj-mat (gile)
  58. ;;; returns the adjugate matrix
  59. ;;;
  60. ;;; argument : a matrix
  61. (defun gile-adj-mat (m / i)
  62.   (setq i 0)
  63.   (gile-trp (mapcar
  64.        (function (lambda (v / j)
  65.      (setq i (1+ i)
  66.     j 0
  67.      )
  68.      (mapcar
  69.        (function (lambda (x)
  70.      (gile-cofact i (setq j (1+ j))
  71.            m
  72.      )
  73.           )
  74.        )
  75.        v
  76.      )
  77.    )
  78.        )
  79.        m
  80.      )
  81.   )
  82. )
  83. ;;; gile-inv-mat (gile)
  84. ;;; inverse a matrix
  85. ;;;
  86. ;;; argument : a matrix
  87. (defun gile-inv-mat (m / d)
  88.   (if (/= 0 (setq d (gile-determ m)))
  89.     (mapcar
  90.       (function (lambda (v)
  91.     (mapcar
  92.       (function (lambda (x)
  93.     (* (/ 1 d) x)
  94.          )
  95.       )
  96.       v
  97.     )
  98.   )
  99.       )
  100.       (gile-adj-mat m)
  101.     )
  102.   )
  103. )
  104. ;;; gile-vxv returns the dot product of 2 vectors
  105. (defun gile-vxv (v1 v2)
  106.   (apply
  107.     '+
  108.     (mapcar
  109.       '*
  110.       v1
  111.       v2
  112.     )
  113.   )
  114. )
  115. ;;; gile-trp transpose a matrix -doug wilson-
  116. (defun gile-trp (m)
  117.   (apply
  118.     'mapcar
  119.     (cons 'list m)
  120.   )
  121. )
  122. ;;; gile-mxv apply a transformation matrix to a vector -vladimir
  123. ;;; nesterovsky-
  124. (defun gile-mxv (m v)
  125.   (mapcar
  126.     '(lambda (r)
  127.        (gile-vxv r v)
  128.      )
  129.     m
  130.   )
  131. )
  132. ;;; gile-mxm multiply two matrices -vladimir nesterovsky-
  133. (defun gile-mxm (m q)
  134.   (mapcar
  135.     '(lambda (r)
  136.        (gile-mxv (gile-trp q) r)
  137.      )
  138.     m
  139.   )
  140. )
  141. ;;; gile-wcs2rcs (gile)
  142. ;;; translates coordinates from wcs to reference (block or xref) coordinate
  143. ;;; system
  144. ;;; pt : a point in wcs
  145. ;;; mat : a transformation matrix as those returned either by (nentsel) or
  146. ;;; (nentselp)
  147. (defun gile-wcs2rcs (pt mat)
  148.   (setq pt (trans pt 0 0))
  149.   (if (= 3 (length (car mat)))
  150.     (setq mat (append
  151.   (gile-trp mat)
  152.   (list '(0.0 0.0 0.0 1.0))
  153.        )
  154.     )
  155.   )
  156.   (setq mat (gile-inv-mat mat))
  157.   (mapcar
  158.     '+
  159.     (gile-mxv mat pt)
  160.     (th-butlast (mapcar
  161.     'last
  162.     mat
  163.   )
  164.     )
  165.   )
  166. )
  167. ;;; gile-rcs2wcs (gile)
  168. ;;; translates coordinates from reference (block or xref) coordinate system
  169. ;;; to wcs
  170. ;;; arguments :
  171. ;;; pt : a point in rcs, got by (cdr (assoc 10 (entget (car (nentsel)))))
  172. ;;; i.e.
  173. ;;; mat : a transformation matrix as those returned either by (nentsel) or
  174. ;;; (nentselp)
  175. (defun gile-rcs2wcs (pt mat)
  176.   (if (= 3 (length (car mat)))
  177.     (mapcar
  178.       '+
  179.       (gile-mxv (gile-trp (th-butlast mat)) pt)
  180.       (last mat)
  181.     )
  182.     (mapcar
  183.       '+
  184.       (gile-mxv (mapcar
  185.     'th-butlast
  186.     (th-butlast mat)
  187.   ) pt
  188.       )
  189.       (th-butlast (mapcar
  190.       'last
  191.       mat
  192.     )
  193.       )
  194.     )
  195.   )
  196. )
  197. ;;; gile-inverse-matrix (gile) 2009/03/17
  198. ;;; uses the gauss-jordan elimination method to calculate the inverse
  199. ;;; matrix of any dimension square matrix
  200. ;;;
  201. ;;; argument : a square matrix
  202. ;;; return : the inverse matrix (or nil if singular)
  203. (defun gile-inverse-matrix (mat / col piv row res)
  204.   (setq mat (mapcar
  205.        '(lambda (x1 x2)
  206.    (append
  207.      x1
  208.      x2
  209.    )
  210.         )
  211.        mat
  212.        (gile-imat (length mat))
  213.      )
  214.   )
  215.   (while mat
  216.     (setq col (mapcar
  217.   '(lambda (x)
  218.      (abs (car x))
  219.    )
  220.   mat
  221.        )
  222.     )
  223.     (repeat (vl-position (apply
  224.       'max
  225.       col
  226.     ) col
  227.      )
  228.       (setq mat (append
  229.     (cdr mat)
  230.     (list (car mat))
  231.   )
  232.       )
  233.     )
  234.     (if (equal (setq piv (caar mat))
  235.         0.0 1e-14
  236. )
  237.       (setq mat nil
  238.      res nil
  239.       )
  240.       (setq piv (/ 1.0 piv)
  241.      row (mapcar
  242.     '(lambda (x)
  243.        (* x piv)
  244.      )
  245.     (car mat)
  246.   )
  247.      mat (mapcar
  248.     '(lambda (r / e)
  249.        (setq e (car r))
  250.        (cdr (mapcar
  251.        '(lambda (x n)
  252.           (- x (* n e))
  253.         )
  254.        r
  255.        row
  256.      )
  257.        )
  258.      )
  259.     (cdr mat)
  260.   )
  261.      res (cons (cdr row) (mapcar
  262.       '(lambda (r / e)
  263.          (setq e (car r))
  264.          (cdr (mapcar
  265.          '(lambda (x n)
  266.             (- x (* n e))
  267.           )
  268.          r
  269.          row
  270.        )
  271.          )
  272.        )
  273.       res
  274.     )
  275.   )
  276.       )
  277.     )
  278.   )
  279.   (reverse res)
  280. )
  281. ;;; gile-imat (gile)
  282. ;;; returns the specified dimension identity matrix
  283. ;;;
  284. ;;; argument
  285. ;;; d : the matrix dimension (positive integer)
  286. (defun gile-imat (d / i n r m)
  287.   (setq i d)
  288.   (while (<= 0 (setq i (1- i)))
  289.     (setq n d
  290.    r nil
  291.     )
  292.     (while (<= 0 (setq n (1- n)))
  293.       (setq r (cons (if (= i n)
  294.         1.0
  295.         0.0
  296.       ) r
  297.        )
  298.       )
  299.     )
  300.     (setq m (cons r m))
  301.   )
  302. )
发表于 2009-8-13 22:07:00 | 显示全部楼层
新手菜鸟来着先学习下哈~!
发表于 2009-8-14 08:09:00 | 显示全部楼层

共同学习!

 楼主| 发表于 2009-8-20 13:43:00 | 显示全部楼层
研究过了,非常感谢各位
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-10-1 12:20 , Processed in 0.169044 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表