-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2.37.scm
More file actions
37 lines (36 loc) · 915 Bytes
/
2.37.scm
File metadata and controls
37 lines (36 loc) · 915 Bytes
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
#lang scheme/base
(define (accumulate op initial sequence)
(if (null? sequence) initial
(op (car sequence) (accumulate op initial (cdr sequence)))
)
)
(define (accumulate-n op init seqs)
(if (null? (car seqs))
null
(cons (accumulate op init (map car seqs))
(accumulate-n op init (map cdr seqs)))
)
)
(accumulate-n + 0 '((1 2 3) (4 5 6) (7 8 9) (10 11 12)))
(define (vector-mul-util v w)
(accumulate-n * 1 (list v w))
)
(define (dot-product v w)
(accumulate + 0 (map * v w))
)
(define (matrix*vector m v)
(map (lambda(s) (dot-product s v)) m)
)
(define (transpose m)
(accumulate-n cons null m)
)
(define (matrix*matrix m n)
(let ([cols (transpose n)])
(map (lambda(x) (matrix*vector n x)) m)
)
)
(define m '((1 3) (1 2)))
(define m3 '((1 2 3) (4 5 6) (7 8 9)))
(define ee '((1 0 0) (0 2 0) (0 0 2)))
(define v '(2 3))
(matrix*matrix m3 ee)