muwind 发表于 2021-9-21 21:41:36

N阶单位矩阵 修改

本帖最后由 muwind 于 2021-9-21 22:38 编辑

;N阶矩阵
(defun tt (n / xm lst1 lst2 lst3)
(if (> n 1)
    (progn
      (setq x 0
          lst1 '()
      lst3 '()
      )   
      (while (< x n)
       (setq lst1 (cons (strcat "x" (vl-princ-to-stringx)) lst1)
         x (1+ x))
      )
    (while (> x 0)
       (setq lst2 '()
         x (- x 1))
   (foreach m lst1
         (if (=m(strcat "x" (vl-princ-to-stringx)))
          (setq lst2 (cons 1.0 lst2))
          (setq lst2 (cons 0.0 lst2))
       )
      )
   (setq lst3 ( cons lst2 lst3))   
      )
    )
)
lst3
)以上是以前仿照(XD::Mat:Identity)写的一个任一N阶单位矩阵的,

(defun tt (n / xm lst1 lst2 lst3)
(if (> n 1)
    (progn
      (setq x 0
                lst1 '()
                        lst3 '()
                        )       
      (while (< x n)
             (setq lst1 (cons (strcat "x" (vl-princ-to-stringx)) lst1)
                     x (1+ x))
      )
          (while (> x 0)
             (setq lst2 '()
                     x (- x 1))       
               (mapcar
                   '(lambda(m)
                     (if (= m(strcat "x" (vl-princ-to-stringx)))
                          (setq lst2 (cons 1.0 lst2))
                          (setq lst2 (cons 0.0 lst2))
                       )
                  ) lst1
              )
               (setq lst3 ( cons lst2 lst3))       
      )
    )
)
lst3
)

kkq0305 发表于 2021-9-22 01:46:47

我也写了一个
(defun mkeye (n / f1 f2)
(setq        f1 (lambda (x)
             (if (> x 0)
             (cons 0 (f1 (1- x)))
             )
           )
)
(setq        f2
       (lambda (lst x)
           (if (> x 0)
             (cons
             lst             
             (f2 (cons (last lst) (reverse (cdr (reverse lst))))(1- x))
             )
           )
       )
)
(f2 (cons 1 (f1 (1- n))) n)
)

muwind 发表于 2021-9-22 22:13:09

本帖最后由 muwind 于 2021-9-22 23:32 编辑

kkq0305 发表于 2021-9-22 01:46
我也写了一个
(defun mkeye (n / f1 f2)
(setq      f1 (lambda (x)

你这个比较简洁, 以我对lambda的应用能力以及递归的理解, 基本是蒙圈的 ,我的方法比较笨:lol
我把你的拆成三个 函数 大致理解到怎么回事了:L
(defunf1 (x / )
(if (> x 0)   (cons 0.0 (f1 (1- x))))
)

(defunf2 (lst x)
(if (> x 0)
   (conslst (f2 (cons (last lst) (reverse (cdr (reverse lst))))(1- x)) )
)
)

(defunf3 (n)
    (f2 (cons 1.0 (f1 (1- n))) n)
)

kkq0305 发表于 2021-9-22 23:40:54

;; (abc 5) → ((1 0 0 0 0) (0 1 0 0 0) (0 0 1 0 0) (0 0 0 1 0) (0 0 0 0 1))
(defun abc (n / a lst)
(setq a '(1))
(repeat (1- n) (setq a (cons 0 a)))
(setq lst (list a))
(repeat (1- n)
    (setq lst (cons (setq a (append (cdr a) '(0))) lst))
)
lst
)
院长写的 更简单

xj6019 发表于 2021-9-23 10:17:15

不懂啥情况用的到

tigcat 发表于 2021-9-23 23:14:50

xj6019 发表于 2021-9-23 10:17
不懂啥情况用的到

坐标变换时用
页: [1]
查看完整版本: N阶单位矩阵 修改