马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
本帖最后由 dnbcgrass 于 2013-6-10 18:10 编辑
下面是生成n阶非奇异矩阵(n×n阶方阵)函数
- ;======================== Cszdfz ==================================================================
- ;; 用法:
- ;; (Cszdfz 矩阵的阶数XN)
- ;; 返回值:
- ;; (n+2)/(2n+2) -1/2 0 0 ... 0 1/(2n+2)
- ;; -1/2 1 -1/2 0 ... 0 0
- ;; 0 -1/2 1 -1/2 ... 0 0
- ;; ..............................................
- ;; 0 ... -1/2 1 -1/2
- ;; 1/(2n+2) 0 ..............................-1/2 (n+2)/(2n+2)
- (defun Cszdfz(XN / B i j zb dx)
- (setq B nil i 1)
- (repeat xn
- (setq j 1 zb nil)
- (repeat xn
- (if (= i j)
- (progn
- (setq dx 1.0)
- (if (or (= i 1) (= i xn))
- (setq dx (/ (+ xn 2.0) (+ (* 2.0 xn) 2.0)))
- )
- )
- (if (or (= (1- i) j) (= (1+ i) j))
- (setq dx -0.5)
- (progn
- (setq dx 0.0)
- (if (or (and (= i 1) (= j xn))
- (and (= i xn) (= j 1))
- )
- (setq dx (/ 1.0 (+ (* 2.0 xn) 2.0)))
- )
- )
- )
- )
- (setq zb (cons dx zb)
- j (1+ j)
- )
- )
- (setq B (cons (reverse zb) B)
- i (1+ i)
- )
- )
- (setq B (reverse B))
- )
下面是求n阶非奇异矩阵的逆矩阵的函数,好像是由Highflybird提供,出自何处记不得清了,
用上面生成的矩阵,采用该函数(Inverse)求得的逆矩阵为:
n n-1 n-2 ... 1 2
n-1 n n-1 ... 3 2
n-2 n-1 n ... 4 3
..........................
2 3 4 ... n n-1
1 2 3 ... n-1 n
但是,当n大于500以上时,速度很慢,大家有没有更快的方法?
- ;============= Inverse ========================================================================
- ;; Matrix Inverse - gile & Lee Mac
- ;; Uses Gauss-Jordan Elimination to return the inverse of a non-singular nxn matrix.
- ;; Args: m - nxn matrix
- (defun Inverse ( m / c f p r d)
- (defun ff ( p m )
- (mapcar '(lambda ( x ) (mapcar '(lambda ( a b ) (- a (* (car x) b))) (cdr x) p)) m)
- )
-
- (setq m (mapcar 'append m (imat (length m))));原矩阵 单位矩阵
- (while m
- (setq c (mapcar '(lambda ( x ) (abs (car x))) m))
- (repeat (vl-position (apply 'max c) c)
- (setq m (append (cdr m) (list (car m))))
-
- )
- (if (equal 0.0 (caar m) 1e-14)
- (setq m nil
- r nil
- )
- (setq p (mapcar '(lambda ( x ) (/ (float x) (caar m))) (cdar m))
- m (ff p (cdr m))
- r (cons p (ff p r))
- )
- )
- )
- (reverse r)
- )
- ;;============= imat ===========================================================================
- ;; Identity Matrix - Lee Mac
- ;; Args: n - matrix dimension
- (defun imat ( n / i j l m )
- (repeat (setq i n)
- (repeat (setq j n)
- (setq l (cons (if (= i j) 1.0 0.0) l)
- j (1- j)
- )
- )
- (setq m (cons l m)
- l nil
- i (1- i)
- )
- )
- m
- )
|