Last active
December 16, 2015 11:19
-
-
Save jonashaag/5426614 to your computer and use it in GitHub Desktop.
Got bored while doing numerics exercises.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(use-modules (srfi srfi-1)) | |
(define (print-matrix mat size) | |
(for-each (lambda (row) | |
(display row) | |
(newline)) | |
(matrix-to-2d mat size))) | |
(define (matrix-to-2d mat size) | |
(map (lambda (i) | |
(map (lambda (j) (mat i j)) | |
(iota size))) | |
(iota size))) | |
(define (matrix-* mat1 mat2 size) | |
(lambda (i j) | |
(fold-right | |
+ | |
0 | |
(map (lambda (x) | |
(* (mat1 i x) | |
(mat2 x j))) | |
(iota size))))) | |
(define (list->mat rows) | |
(lambda (i j) | |
(list-ref (list-ref rows i) j))) | |
(define identity | |
(lambda (i j) | |
(if (= i j) | |
1 | |
0))) | |
(define i+j | |
(lambda (i j) | |
(+ i j))) | |
(define i*j | |
(lambda (i j) | |
(* i j))) | |
(print-matrix identity 5) | |
(newline) | |
(print-matrix i+j 5) | |
(newline) | |
(print-matrix i*j 5) | |
(newline) | |
(print-matrix (matrix-* i*j i+j 5) 5) | |
(newline) | |
(print-matrix (matrix-* i+j i*j 5) 5) | |
(newline) | |
(print-matrix (matrix-* (list->mat '((0 1 0) | |
(0 0 1) | |
(1 0 0))) | |
(list->mat '((2 -4 3) | |
(-8 12 -4) | |
(4 -2 10))) | |
3) | |
3) | |
(newline) | |
(print-matrix (matrix-* (list->mat '((1 0 0) | |
(-1/2 1 0) | |
(-1/4 -1/4 1))) | |
(list->mat '((-8 12 -4) | |
(0 4 8) | |
(0 0 4))) | |
3) | |
3) | |
(newline) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment