I have to write a predicate the predicate product/3
which receives two matrix and returns the matrix multiplication of them if possible or fail otherwise. (This means if the matrices fullfill the requirement [n x p] [p x y]
, then return the multiplication with dimensions [n x y]
)
Example:
product(M1, M2, R)
?- product([[1,2],[3,4],[5,6]], [[1,1,1],[1,1,1]], M).
M = [[3, 3, 3], [7, 7, 7], [11, 11, 11]];
No
For this I have two codes that index the nth row on a matrix rowI
and that index the nth column columnI
(I explain how they work in the code below).
%Predicate: rowI(M, I, RI)
%Input rowI([[1,2],[3,4],[5,6]], 2, RI).
% RI = [3,4];
rowI([H|_],1,H):-!.
rowI([_|T],I,X) :-
I1 is I-1,
rowI(T,I1,X).
% columnJ(M, J, CJ)
%Input columnJ([[1,2],[3,4],[5,6]], 1, CJ).
% CJ = [1,3,5];
columnJ([],_,[]).
columnJ([H|T], I, [R|X]):-
rowI(H, I, R),
columnJ(T,I,X).
product([H|T], M2, [R|X]):-
columnJ(M2, C, Z),
mult(H, Z , X),
product(T, M2 , X).
I was thinking somehow by grabbing the head of the M1
(which will be each row) and then multiplied for each column in M2
and after adding the multiplication this list will be the new row. So (C would have to be a counter starting from 1 to the length of M2
and then mult
I was just thinking on having it multiplying the lists. (mult is not defined at this point, just a guess).
Here I am trying to explain the way I am thinking it.. but there may be a simplier way. What do you think?