23
24
25:- module(matrix_utls,
26 [ kronecker/3,
27 hadamard/3,
28 create_empty_matrix/3,
29 create_val_matrix/4,
30 create_rand_matrix/5,
31 create_I_matrix/2,
32 s_v_multiply/3,
33 s_m_multiply/3,
34 v_v_multiply/3,
35 v_m_multiply/3,
36 m_v_multiply/3,
37 m_m_multiply/3
38 ]).
39
40
42:- dynamic outRes/1, subOutRes/1, addRes/1.
52
53kronecker(A, B, O):-
54 assert(outRes([])),
55 assert(subOutRes([])),
56 maplist(get_A_Rows(B), A),
57 retract(subOutRes(_)),
58 retract(outRes(O)).
59
61get_A_Rows(B, A_Row):-
62 maplist(get_B_Rows(A_Row), B).
63
65get_A_Cells(B_Row, A_Cell):-
66 maplist(get_B_Cells(A_Cell), B_Row).
67
69get_B_Rows(A_Row, B_Row):-
70 maplist(get_A_Cells(B_Row), A_Row),
71 retract(outRes(Out)),
72 retract(subOutRes(SubOut)),
73 append(Out, [SubOut], FinalOut),
74 assert(subOutRes([])),
75 assert(outRes(FinalOut)).
76
78get_B_Cells(A_Cell, B_Cell):-
79
80 R is A_Cell * B_Cell,
81 retract(subOutRes(SubOut)),
82 append(SubOut, [R], FinalSubOut),
83 assert(subOutRes(FinalSubOut)).
93
94hadamard(A, B, O):-
95
96 checkRows(A ,B, Num_Rows),
97 checkCols(A, B, Num_Cols),
98 assert(outRes([])),
99 assert(subOutRes([])),
100 forall(between(1, Num_Rows, Row_Index),
101 doRows(A, B, Row_Index, Num_Cols)),
102 retract(subOutRes(_)),
103 retract(outRes(O)).
104
106checkRows(A, B, Num_A_Rows):-
107 length(A, Num_A_Rows),
108 length(B, Num_B_Rows),
109 ( Num_A_Rows =\= Num_B_Rows
110 -> writeln('\nError: Matrices do not have equal dimensionality (rows) ...\n'),
111 abort
112 ; ! ).
113
115checkCols(A, B, Num_A_Cols):-
116 nth1(1, A, A_Row),
117 nth1(1, B, B_Row),
118 length(A_Row, Num_A_Cols),
119 length(B_Row, Num_B_Cols),
120 ( Num_A_Cols =\= Num_B_Cols
121 -> writeln('\nError: Matrices do not have equal dimensionality (columns) ...\n'),
122 abort
123 ; !).
124
125doRows(A, B, Row_Index, Num_Cols):-
126 nth1(Row_Index, A, A_Row),
127 nth1(Row_Index, B, B_Row),
128 forall(between(1, Num_Cols, Col_Index),
129 doCols(A_Row, B_Row, Col_Index)),
130 retract(outRes(Out)),
131 retract(subOutRes(SubOut)),
132 append(Out, [SubOut], FinalOut),
133 assert(subOutRes([])),
134 assert(outRes(FinalOut)).
135
136doCols(A_Row, B_Row, Col_Index):-
137 nth1(Col_Index, A_Row, A_Cell),
138 nth1(Col_Index, B_Row, B_Cell),
139 R is A_Cell * B_Cell,
140 retract(subOutRes(SubOut)),
141 append(SubOut, [R], FinalSubOut),
142 assert(subOutRes(FinalSubOut)).
152
153create_empty_matrix(Rows, Cols, Mat):-
154 length(Mat, Rows),
155
156 maplist(create_list(Cols), Mat).
166
167create_val_matrix(Rows, Cols, Mat, Value):-
168 create_empty_matrix(Rows, Cols, Mat),
169 setValue(Value, 1, Mat).
179
180create_rand_matrix(Rows, Cols, Mat, Min_Value, Max_Value):-
181 create_empty_matrix(Rows, Cols, Mat),
182 setValue([Min_Value, Max_Value], 2, Mat).
183
185create_list(Cols, Sub_Mat):-
186 length(Sub_Mat, Cols).
187
189setValue(Value, Type, Mat):-
190 maplist(setRows(Value, Type), Mat).
191setRows(Value, Type, Mat_Rows):-
192 maplist(setCells(Value, Type), Mat_Rows).
193setCells(Value, 1, Mat_Cell):- Mat_Cell = Value, !.
194setCells(Value, 2, Mat_Cell):-
195 nth1(1, Value, Min_Value),
196 nth1(2, Value, Max_Value),
197 random(Min_Value, Max_Value, Mat_Cell), !.
206
207create_I_matrix(Dim, Mat):-
208 Dim_1 is Dim - 1,
209 create_val_matrix(1, Dim_1, Zeros, 0),
210 nth1(1, Zeros, Zero),
211 length(Mat, Dim),
212 numlist(1, Dim, Index),
213 maplist(insert_Ones(Zero), Index, Mat).
214
215insert_Ones(Zero, Index, Sub_Mat):-
216 nth1(Index, Sub_Mat, 1, Zero).
226
227s_v_multiply(S, V, O):-
228 s_m_multiply(S, V, O).
238
239s_m_multiply(S, M, O):-
240 assert(outRes([])),
241 assert(subOutRes([])),
242 length(M, M_Rows),
243 forall(between(1, M_Rows, Row_Index), do_Rows(S, M, Row_Index)),
244 retract(subOutRes(_)),
245 retract(outRes(O)).
246
247do_Rows(S, M, Row_Index):-
248 nth1(Row_Index, M, M_Row),
249 length(M_Row, M_Cols),
250 forall(between(1, M_Cols, Col_Index), do_Cols(S, M_Row, Col_Index)),
251 retract(outRes(Out)),
252 retract(subOutRes(SubOut)),
253 append(Out, [SubOut], FinalOut),
254 assert(subOutRes([])),
255 assert(outRes(FinalOut)).
256
257do_Cols(S, M_Row, Col_Index):-
258 nth1(Col_Index, M_Row, M_Cell),
259 R is S * M_Cell,
260 retract(subOutRes(SubOut)),
261 append(SubOut, [R], FinalSubOut),
262 assert(subOutRes(FinalSubOut)).
272
273v_v_multiply(V1, V2, O):-
274 m_m_multiply(V1, V2, O).
284
285v_m_multiply(V, M, O):-
286 m_m_multiply(V, M, O).
296
297m_v_multiply(M, V, O):-
298 m_m_multiply(M, V, O).
308
309m_m_multiply(M1, M2, O):-
310 length(M1, Num_M1_Rows),
311 length(M2, Num_M2_Rows),
312 nth1(1, M1, M1_Row),
313 length(M1_Row, Num_M1_Cols),
314 nth1(1, M2, M2_Row),
315 length(M2_Row, Num_M2_Cols),
316 checkDim(Num_M1_Cols, Num_M2_Rows),
317 assert(outRes([])),
318 assert(subOutRes([])),
319 assert(addRes(0.0)),
320 forall(between(1, Num_M1_Rows, M1_Row_Index),
321 do_M1_Rows(M1, M2, M1_Row_Index, Num_M1_Cols, Num_M2_Cols)),
322 retract(addRes(_)),
323 retract(subOutRes(_)),
324 retract(outRes(O)).
325
327checkDim(Num_M1_Cols, Num_M2_Rows):-
328 ( Num_M1_Cols =\= Num_M2_Rows
329 -> writeln('\nError: Matrices do not have equal dimensionality requirement:'),
330 write('M1 cols = '), write(Num_M1_Cols), nl,
331 write('M2 rows = '), write(Num_M2_Rows), nl,
332 writeln('Hint: Both matrices should have equal dimensions (rows and cols).\n'),
333 abort
334 ; ! ).
335
336do_M1_Rows(M1, M2, M1_Row_Index, Num_M1_Cols, Num_M2_Cols):-
337 forall(between(1, Num_M2_Cols, M2_Col_Index),
338 do_M2_Col(M1, M2, M1_Row_Index,Num_M1_Cols, M2_Col_Index)),
339 retract(outRes(Out)),
340 retract(subOutRes(SubOut)),
341 append(Out, [SubOut], FinalOut),
342 assert(subOutRes([])),
343 assert(outRes(FinalOut)).
344
345do_M2_Col(M1, M2, M1_Row_Index,Num_M1_Cols, M2_Col_Index):-
346 nth1(M1_Row_Index, M1, M1_Row),
347 forall(between(1, Num_M1_Cols, M1_Col_Index),
348 doMultiply(M1_Row, M2, M1_Col_Index, M2_Col_Index)),
349 retract(subOutRes(SubOut)),
350 retract(addRes(SubAdd)),
351 append(SubOut, [SubAdd], FinalSubOut),
352 assert(addRes(0.0)),
353 assert(subOutRes(FinalSubOut)).
354
355doMultiply(M1_Row, M2, M1_Col_Index, M2_Col_Index):-
356 nth1(M1_Col_Index, M1_Row, M1_Cell),
357 nth1(M1_Col_Index, M2, M2_Row),
358 nth1(M2_Col_Index, M2_Row, M2_Cell),
359 R is M1_Cell * M2_Cell,
360 retract(addRes(SubAdd)),
361 Out is SubAdd + R,
362 assert(addRes(Out)).
363
364