mahuan1279 发表于 2020-3-6 12:55:01

卷积(多项式乘以多项式)

;;;多项式系数由高次到低次排列
(defun add (lst1 lst2)
(setq k1 (length lst1))
(setq k2 (length lst2))
(if (> k1 k2)
          (repeat (- k1 k2)(setq lst2 (cons 0 lst2)) )
          (repeat (- k2 k1)(setq lst1 (cons 0 lst1)) )
   )
(setq addlst (mapcar '(lambda (x y) (+ x y)) lst1 lst2 ))
addlst
)
(defun lnum (lst n n0)
   (setq lst (mapcar '(lambda (x) (* x n)) lst))
   (setq zlst (reverse lst))
   (repeat n0 (setq zlst (cons 0 zlst) ) )
   (reverse zlst)
)
(defun xc(lst1 lst2)
   (setq n1 (length lst1))
   (setq n2 (length lst2))
   (setq j 1 fflst '(0))
   (if (> n1 n2)
          (while (<= j n2)
            (setq fflst (add (lnum lst1 (nth (- j 1) lst2) (- n2 j) ) fflst))
            (setq j (+ 1 j))
         )
          (while (<= j n1)
            (setq fflst (add (lnum lst2 (nth (- j 1) lst1) (- n1 j) ) fflst))
            (setq j (+ 1 j))
         )
    )
fflst
)
(defun jinw (blst)
    (while (not (apply 'and (mapcar '(lambda (x) (<= x 9)) blst )))
         (setq hlst (mapcar '(lambda (x) (rem x 10)) blst))
         (setq qlst (mapcar '(lambda (x) (/ x 10)) blst))
         (setq qlst (append qlst (list 0)))
         (setq blst (add hlst qlst))
    )
blst
)
(defun dc (a_strb_str)
(setq lst1 (mapcar '(lambda (x) (- x 48)) (vl-string->list a_str)))
(setq lst2 (mapcar '(lambda (x) (- x 48)) (vl-string->list b_str)))
(setq tenlst (jinw (xc lst1 lst2)))
(setq str (apply 'strcat (mapcar '(lambda (x) (itoa x)) tenlst)))
(if (= (car tenlst) 0)
      (if (> (length tenlst) 1)
          (setq str (vl-string-left-trim "0" str))
                  (setq str "0")
         )
)
str
)
(defun dj (a_strb_str)
(setq lst1 (mapcar '(lambda (x) (- x 48)) (vl-string->list a_str)))
(setq lst2 (mapcar '(lambda (x) (- x 48)) (vl-string->list b_str)))
(setq tenlst (jinw (add lst1 lst2)))
(setq str (apply 'strcat (mapcar '(lambda (x) (itoa x)) tenlst ) ) )
(if (= (car tenlst) 0)
      (if (> (length tenlst) 1)
          (setq str (vl-string-left-trim "0" str))
                  (setq str "0")
         )
)
str
)
;;;以下都是字符串数值进行运算 lst1= '("123456789" "9876544321234" "34562627381")lst2='("9" "6" "3")
(defun fcj (nlst_lst1 nlst_lst2)
    (setq vv_pest "0")
    (foreach en (mapcar '(lambda (x y) (dc x y)) nlst_lst1nlst_lst2)
         (setq vv_pest (dj en vv_pest))
   )
)
(defun f2 (lst k)    ;;;lst向右移动K位(f2 '("1" "2" "3") 2)=("1")
(if (= k 0) lst (reverse (cdr (reverse (f2 lst (- k 1))))))
)
(defun f3 (lst k)    ;;;从右边开始取lst右边K位(f3 '("1" "2" "3") 2)=("2" "3")
(if (= k 1) (list (last lst)) (cons (car (reverse (f2 lst (- k 1))) ) (f3 lst (- k 1))))
)
(defun f4 (lst1 lst2)
   (setq n1 (length lst1) n2 (length lst2))
   (if (<= n1n2)
         (progn
            (setq flst lst1)
            (repeat (- n2 n1)
                  (setq flst (cons "0" flst))
             )
             (setq lst (list flst lst2 (- n2 n1)))
         )
          (f4 lst2 lst1)   
      )
)
;;;(setq lst1 '("1" "2" "3") lst2 '("9" "8" "7" "6" "5" "4"))
;;;slst='(("0" "0" "0" "1" "2" "3") ("9" "8" "7" "6" "5" "4") 3)
;;;llst=("0" "0" "0" "1" "2" "3") rlst=("9" "8" "7" "6" "5" "4")k=3
(defun fc (lst1 lst2)
    (setq slst (f4 lst1 lst2) ii 1 jj 1 va nil)
    (setq llst (car slst) rlst (cadr slst) kkt (caddr slst) nnt (length rlst))
    (while (<= ii nnt)
         (setq va (cons (fcj (f3 llst ii) (reverse (f3 rlst ii))) va))
         (setq ii (+ ii 1))
   )
         (setq llst (reverse llst) rlst (reverse rlst) vb nil)
   (while (<= jj (1- nnt))
         (setq vb (cons (fcj (f3 llst jj)(reverse (f3 rlst jj))) vb))
         (setq jj (+ jj 1))
   )
    (append (reverse (f2 vb kkt)) va)
)
_$ (setq lst '("1" "1"))
("1" "1")
_$ (setq lst (fc lst lst))
("1" "2" "1")
_$ (setq lst (fc lst lst))
("1" "4" "6" "4" "1")
_$(setq lst (fc lst lst))
("1" "8" "28" "56" "70" "56" "28" "8" "1")
_$ (setq lst (fc lst lst))
("1" "16" "120" "560" "1820" "4368" "8008" "11440" "12870" "11440" "8008" "4368" "1820" "560" "120" "16" "1")
_$ (setq lst (fc lst lst))
("1" "32" "496" "4960" "35960" "201376" "906192" "3365856" "10518300" "28048800" "64512240" "129024480" "225792840" "347373600" "471435600" "565722720" "601080390" "565722720" "471435600" "347373600" "225792840" "129024480" "64512240" "28048800" "10518300" "3365856" "906192" "201376" "35960" "4960" "496" "32" "1")
_$ (setq lst (fc lst lst))
("1" "64" "2016" "41664" "635376" "7624512" "74974368" "621216192" "4426165368" "27540584512" "151473214816" "743595781824" "3284214703056" "13136858812224" "47855699958816" "159518999862720" "488526937079580" "1379370175283520" "3601688791018080" "8719878125622720" "19619725782651120" "41107996877935680" "80347448443237920" "146721427591999680" "250649105469666120" "401038568751465792" "601557853127198688" "846636978475316672" "1118770292985239888" "1388818294740297792" "1620288010530347424" "1777090076065542336" "1832624140942590534" "1777090076065542336" "1620288010530347424" "1388818294740297792" "1118770292985239888" "846636978475316672" "601557853127198688" "401038568751465792" "250649105469666120" "146721427591999680" "80347448443237920" "41107996877935680" "19619725782651120" "8719878125622720" "3601688791018080" "1379370175283520" "488526937079580" "159518999862720" "47855699958816" "13136858812224" "3284214703056" "743595781824" "151473214816" "27540584512" "4426165368" "621216192" "74974368" "7624512" "635376" "41664" "2016" "64" "1")
_$ (setq lst (fc lst lst))
("1" "128" "8128" "341376" "10668000" "264566400" "5423611200" "94525795200" "1429702652400" "19062702032000" "226846154180800" "2433440563030400" "23726045489546400" "211709328983644800" "1739040916651368000" "13216710966550396800" "93343021201262177400" "614965786737727286400" "3792289018215984932800" "21955357473882018032000" "119656698232656998274400" "615377305196521705411200" "2992971438910355567227200" "13793694457586856092438400" "60347413251942495404418000" "251045239128080780882378880" "994525370392012324264808640" "3757095843703157669444832640" "13552381436214961593354574880" "46732349780051591701222672000" "154216754274170252614034817600" "487523932866731766328239100800" "1477806921502280666682474774300" "4299074680733907393985381161600" "12012120431462388306723859128000" "32261123444498985738058364515200" "83341235564955713156650774997600" "207226855999349340821942467561600" "496253786735283947757809593371200" "1145201046312193725594945215472000" "2548072328044631039448753104425200" "5469033289461647109060738370473600" "11328711813884840440197243767409600" "22657423627769680880394487534819200" "43770022917282338064398441828628000" "81704042778927031053543758080105600" "147422511970672686466176780883668800" "257205233650960857238861617711932800" "434033831785996446590578979888886600" "708626664140402361780537110022672000" "1119630129341835731613248633835821760" "1712375491934572295408497910572433280" "2535632939980039745124121906039949280" "3636001951669490955272325752057285760" "5050002710652070771211563544524008000" "6794549101604604310357376405359574400" "8857180078877430618858722814129445200" "11188016941739912360663649870479299200" "13695675911440237544950330013862590400" "16249107013573163188924120355430192000" "18686473065609137667262738408744720800" "20830822433793792809407642816305590400" "22510727468777163197263097882136686400" "23582666872052266206656578733667004800" "23951146041928082866135587776380551750" "23582666872052266206656578733667004800" "22510727468777163197263097882136686400" "20830822433793792809407642816305590400" "18686473065609137667262738408744720800" "16249107013573163188924120355430192000" "13695675911440237544950330013862590400" "11188016941739912360663649870479299200" "8857180078877430618858722814129445200" "6794549101604604310357376405359574400" "5050002710652070771211563544524008000" "3636001951669490955272325752057285760" "2535632939980039745124121906039949280" "1712375491934572295408497910572433280" "1119630129341835731613248633835821760" "708626664140402361780537110022672000" "434033831785996446590578979888886600" "257205233650960857238861617711932800" "147422511970672686466176780883668800" "81704042778927031053543758080105600" "43770022917282338064398441828628000" "22657423627769680880394487534819200" "11328711813884840440197243767409600" "5469033289461647109060738370473600" "2548072328044631039448753104425200" "1145201046312193725594945215472000" "496253786735283947757809593371200" "207226855999349340821942467561600" "83341235564955713156650774997600" "32261123444498985738058364515200" "12012120431462388306723859128000" "4299074680733907393985381161600" "1477806921502280666682474774300" "487523932866731766328239100800" "154216754274170252614034817600" "46732349780051591701222672000" "13552381436214961593354574880" "3757095843703157669444832640" "994525370392012324264808640" "251045239128080780882378880" "60347413251942495404418000" "13793694457586856092438400" "2992971438910355567227200" "615377305196521705411200" "119656698232656998274400" "21955357473882018032000" "3792289018215984932800" "614965786737727286400" "93343021201262177400" "13216710966550396800" "1739040916651368000" "211709328983644800" "23726045489546400" "2433440563030400" "226846154180800" "19062702032000" "1429702652400" "94525795200" "5423611200" "264566400" "10668000" "341376" "8128" "128" "1")
_$ (length lst)
129

mahuan1279 发表于 2020-3-6 12:57:24

;;;求组合函数C(n,r)
_$ (defun cc (NNN rrr);;;NNN>=rrr
   (setq plst '("1" "1") cmlst '("1"))
   (repeat NNN
      (setq cmlst (fc cmlst plst))
   )
   (nth rrr (reverse cmlst))
)
_$ (cc 30 15)
"155117520"
_$ (cc 40 15)
"40225345056"
_$ (cc 50 20)
"47129212243960"
_$

satan421 发表于 2020-3-7 14:56:48

十分佩服楼主,留个脚印。
页: [1]
查看完整版本: 卷积(多项式乘以多项式)