fork download
  1. ; your code goes here
  2. (deftemplate matrix (slot data))
  3.  
  4. ;; === Множення двох матриць ===
  5. (deffunction mat-mul (?m1 ?m2)
  6. (bind ?result (create$))
  7. (bind ?rows (length ?m1))
  8. (bind ?cols (length (nth$ 1 ?m2)))
  9. (bind ?inner (length (nth$ 1 ?m1)))
  10. (foreach ?row ?m1
  11. (bind ?new-row (create$))
  12. (for (?j 1 ?cols)
  13. (bind ?sum 0)
  14. (for (?k 1 ?inner)
  15. (bind ?sum (+ ?sum (* (nth$ ?k ?row) (nth$ ?k (nth$ ?j (transpose ?m2))))))
  16. )
  17. (bind ?new-row (create$ ?new-row ?sum))
  18. )
  19. (bind ?result (create$ ?result ?new-row))
  20. )
  21. ?result
  22. )
  23.  
  24. ;; === Транспонування матриці ===
  25. (deffunction transpose (?matrix)
  26. (bind ?rows (length ?matrix))
  27. (bind ?cols (length (nth$ 1 ?matrix)))
  28. (bind ?result (create$))
  29. (for (?j 1 ?cols)
  30. (bind ?new-row (create$))
  31. (for (?i 1 ?rows)
  32. (bind ?new-row (create$ ?new-row (nth$ ?j (nth$ ?i ?matrix))))
  33. )
  34. (bind ?result (create$ ?result ?new-row))
  35. )
  36. ?result
  37. )
  38.  
  39. ;; === Піднесення матриці до степеня ===
  40. (deffunction mat-pow (?matrix ?power)
  41. (bind ?result ?matrix)
  42. (if (= ?power 1) then
  43. (return ?result)
  44. )
  45. (for (?i 2 ?power)
  46. (bind ?result (mat-mul ?result ?matrix))
  47. )
  48. ?result
  49. )
  50.  
  51. ;; === Допоміжна функція — копіювання матриці ===
  52. (deffunction copy-matrix (?matrix)
  53. (bind ?result (create$))
  54. (foreach ?row ?matrix
  55. (bind ?result (create$ ?result (create$ ?row)))
  56. )
  57. ?result
  58. )
  59.  
  60. ;; === Інверсія матриці методом Гаусса-Жордана ===
  61. (deffunction mat-inverse (?matrix)
  62. (bind ?n (length ?matrix))
  63. (bind ?aug (create$))
  64. (for (?i 1 ?n)
  65. (bind ?row (nth$ ?i ?matrix))
  66. (bind ?identity (create$))
  67. (for (?j 1 ?n)
  68. (if (= ?i ?j) then
  69. (bind ?identity (create$ ?identity 1))
  70. else
  71. (bind ?identity (create$ ?identity 0))
  72. )
  73. )
  74. (bind ?aug (create$ ?aug (create$ ?row ?identity)))
  75. )
  76.  
  77. ;; Прямий хід
  78. (for (?i 1 ?n)
  79. (bind ?pivot (nth$ ?i (nth$ ?i ?aug)))
  80. (if (= ?pivot 0) then (return FALSE))
  81. ;; нормалізація
  82. (for (?j 1 (* 2 ?n))
  83. (bind ?val (/ (nth$ ?j (nth$ ?i ?aug)) ?pivot))
  84. (modify ?aug (replace$ ?i ?i (replace$ (nth$ ?i ?aug) ?j ?j ?val)))
  85. )
  86. ;; занулення інших
  87. (for (?k 1 ?n)
  88. (if (<> ?k ?i) then
  89. (bind ?factor (nth$ ?i (nth$ ?k ?aug)))
  90. (for (?j 1 (* 2 ?n))
  91. (bind ?val (- (nth$ ?j (nth$ ?k ?aug)) (* ?factor (nth$ ?j (nth$ ?i ?aug)))))
  92. (modify ?aug (replace$ ?k ?k (replace$ (nth$ ?k ?aug) ?j ?j ?val)))
  93. )
  94. )
  95. )
  96. )
  97.  
  98. ;; Витяг інвертованої частини
  99. (bind ?inverse (create$))
  100. (for (?i 1 ?n)
  101. (bind ?row (subseq$ (+ ?n 1) (* 2 ?n) (nth$ ?i ?aug)))
  102. (bind ?inverse (create$ ?inverse ?row))
  103. )
  104. ?inverse
  105. )
  106.  
  107. ;; === Головна функція ===
  108. (defrule compute-matrix-expression
  109. =>
  110. (bind ?A ((create$ (create$ 5 3 1 8)
  111. (create$ 2 1 4 2)
  112. (create$ 7 6 4 3)
  113. (create$ 9 3 5 1))))
  114. (bind ?B ((create$ 9 7 1 8)
  115. (create$ 2 4 6 1)
  116. (create$ 7 5 3 9)
  117. (create$ 4 6 8 7)))
  118. (bind ?C ((create$ 12 3 2 5)
  119. (create$ 1 10 4 2)
  120. (create$ 3 6 9 3)
  121. (create$ 7 3 5 8)))
  122.  
  123. (bind ?B3 (mat-pow ?B 3))
  124. (bind ?prod1 (mat-mul ?A ?B3))
  125. (bind ?prod2 (mat-mul ?prod1 ?C))
  126. (bind ?inverse (mat-inverse ?prod2))
  127.  
  128. (printout t "Inverse matrix: " crlf)
  129. (foreach ?row ?inverse
  130. (printout t ?row crlf)
  131. )
  132. )
  133.  
  134. (exit)
  135. ; empty line at the end
Success #stdin #stdout 0.02s 5432KB
stdin
Standard input is empty
stdout
[EXPRNPSR3] Missing function declaration for for.

ERROR:
(deffunction MAIN::mat-mul
   (?m1 ?m2)
   (bind ?result (create$))
   (bind ?rows (length ?m1))
   (bind ?cols (length (nth$ 1 ?m2)))
   (bind ?inner (length (nth$ 1 ?m1)))
   (foreach ?row ?m1
      (bind ?new-row (create$))
      (

[EXPRNPSR3] Missing function declaration for for.

ERROR:
(deffunction MAIN::transpose
   (?matrix)
   (bind ?rows (length ?matrix))
   (bind ?cols (length (nth$ 1 ?matrix)))
   (bind ?result (create$))
   (for

[EXPRNPSR3] Missing function declaration for for.

ERROR:
(deffunction MAIN::mat-pow
   (?matrix ?power)
   (bind ?result ?matrix)
   (if (= ?power 1)
      then
      (return ?result))
   (for

[EXPRNPSR3] Missing function declaration for for.

ERROR:
(deffunction MAIN::mat-inverse
   (?matrix)
   (bind ?n (length ?matrix))
   (bind ?aug (create$))
   (for

[EXPRNPSR1] A function name must be a symbol

ERROR:
(defrule MAIN::compute-matrix-expression
   =>
   (bind ?A ((