Matrix multiplication: Difference between revisions
Content added Content deleted
(Creation of page.) |
No edit summary |
||
Line 3: | Line 3: | ||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
(defun matrix-multiply (a b) |
(defun matrix-multiply (a b) |
||
(flet ((col (mat i) (mapcar #'(lambda (row) (elt row i)) mat)) |
(flet ((col (mat i) (mapcar #'(lambda (row) (elt row i)) mat)) |
||
(row (mat i) (elt mat i))) |
(row (mat i) (elt mat i))) |
||
(loop for row from 0 below (length a) |
(loop for row from 0 below (length a) |
||
collect (loop for col from 0 below (length (row b 0)) |
collect (loop for col from 0 below (length (row b 0)) |
||
collect (apply #'+ (mapcar #'* (row a row) (col b col))))))) |
collect (apply #'+ (mapcar #'* (row a row) (col b col))))))) |
||
=={{header|SQL}}== |
=={{header|SQL}}== |
||
CREATE TABLE a (x integer, y integer, e real); |
CREATE TABLE a (x integer, y integer, e real); |
||
CREATE TABLE b (x integer, y integer, e real); |
CREATE TABLE b (x integer, y integer, e real); |
||
-- test data |
-- test data |
||
-- A is a 2x2 matrix |
-- A is a 2x2 matrix |
||
INSERT INTO a VALUES(0,0,1); INSERT INTO a VALUES(1,0,2); |
INSERT INTO a VALUES(0,0,1); INSERT INTO a VALUES(1,0,2); |
||
INSERT INTO a VALUES(0,1,3); INSERT INTO a VALUES(1,1,4); |
INSERT INTO a VALUES(0,1,3); INSERT INTO a VALUES(1,1,4); |
||
-- B is a 2x3 matrix |
-- B is a 2x3 matrix |
||
INSERT INTO b VALUES(0,0,-3); INSERT INTO b VALUES(1,0,-8); INSERT INTO b VALUES(2,0,3); |
INSERT INTO b VALUES(0,0,-3); INSERT INTO b VALUES(1,0,-8); INSERT INTO b VALUES(2,0,3); |
||
INSERT INTO b VALUES(0,1,-2); INSERT INTO b VALUES(1,1, 1); INSERT INTO b VALUES(2,1,4); |
INSERT INTO b VALUES(0,1,-2); INSERT INTO b VALUES(1,1, 1); INSERT INTO b VALUES(2,1,4); |
||
-- C is 2x2 * 2x3 so will be a 2x3 matrix |
-- C is 2x2 * 2x3 so will be a 2x3 matrix |
||
SELECT rhs.x, lhs.y, (SELECT sum(a.e*b.e) FROM a, b |
SELECT rhs.x, lhs.y, (SELECT sum(a.e*b.e) FROM a, b |
||
WHERE a.y = lhs.y |
WHERE a.y = lhs.y |
||
AND b.x = rhs.x |
AND b.x = rhs.x |
||
AND a.x = b.y) |
AND a.x = b.y) |
||
INTO TABLE c |
INTO TABLE c |
||
FROM a AS lhs, b AS rhs |
FROM a AS lhs, b AS rhs |
||
WHERE lhs.x = 0 AND rhs.y = 0; |
WHERE lhs.x = 0 AND rhs.y = 0; |
Revision as of 23:42, 9 December 2007
Matrix multiplication
You are encouraged to solve this task according to the task description, using any language you may know.
You are encouraged to solve this task according to the task description, using any language you may know.
Multiply two Matrices together, they can be of any dimension as long as the number of columns of the first = the number of rows of the second.
Common Lisp
(defun matrix-multiply (a b) (flet ((col (mat i) (mapcar #'(lambda (row) (elt row i)) mat)) (row (mat i) (elt mat i))) (loop for row from 0 below (length a) collect (loop for col from 0 below (length (row b 0)) collect (apply #'+ (mapcar #'* (row a row) (col b col)))))))
SQL
CREATE TABLE a (x integer, y integer, e real); CREATE TABLE b (x integer, y integer, e real); -- test data -- A is a 2x2 matrix INSERT INTO a VALUES(0,0,1); INSERT INTO a VALUES(1,0,2); INSERT INTO a VALUES(0,1,3); INSERT INTO a VALUES(1,1,4); -- B is a 2x3 matrix INSERT INTO b VALUES(0,0,-3); INSERT INTO b VALUES(1,0,-8); INSERT INTO b VALUES(2,0,3); INSERT INTO b VALUES(0,1,-2); INSERT INTO b VALUES(1,1, 1); INSERT INTO b VALUES(2,1,4); -- C is 2x2 * 2x3 so will be a 2x3 matrix SELECT rhs.x, lhs.y, (SELECT sum(a.e*b.e) FROM a, b WHERE a.y = lhs.y AND b.x = rhs.x AND a.x = b.y) INTO TABLE c FROM a AS lhs, b AS rhs WHERE lhs.x = 0 AND rhs.y = 0;