-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix_mul.lisp
66 lines (63 loc) · 1.67 KB
/
matrix_mul.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
(defun mulNxN (A B)
(if
(mismatch (list (car (reverse(array-dimensions A))))(list (car (array-dimensions B))))
(progn
(format t "Matrix Non Compatible")
(return-from mulNxN 0)
)
)
(setq C (make-array (list (car (array-dimensions A)) (car (reverse(array-dimensions B))))))
(loop for i from 0 to (- (car (array-dimensions A)) 1)
do(
loop for j from 0 to (- (car (reverse(array-dimensions B))) 1 )
do
( progn
(setf tmp 0)
(loop for k from 0 to (- (car (reverse(array-dimensions A))) 1 )
do
(setf tmp (+ tmp (* (aref A i k) (aref B k j))))
))
(setf (aref C i j) tmp)
)
)
(return-from mulNxN C)
)
(defun printmatrix (A)
(format t "The Resultant Matrix is ")
(terpri)
(loop for i from 0 to (- (car (array-dimensions A)) 1)
do(
loop for j from 0 to (- (car (reverse(array-dimensions A))) 1 )
do
(format t "~D " (aref A i j))
)
(terpri)
)
)
(format t "Enter N for First Matrix")
(setf n1 (read))
(format t "Enter M for First Matrix")
(setf m1 (read))
(format t "Enter N for Second Matrix")
(setf n2 (read))
(format t "Enter M for Second Matrix")
(setf m2 (read))
(setf A (make-array (list n1 m1)))
(dotimes (i n1)
(
dotimes (j m1)
(format t "Enter ~D ~D th Element of First Matrix" i j)
(terpri)
(setf (aref A i j) (read))
)
)
(setf B (make-array (list n2 m2)))
(dotimes (i n2)
(
dotimes (j m2)
(format t "Enter ~D ~D th Element of Second Matrix" i j)
(terpri)
(setf (aref B i j) (read))
)
)
(printmatrix (mulNxN A B))