(*******************************************************************) function init_replace_matrix(VAR mx1,mx2:ptr_matrix_node):integer; var status:integer; begin status:=ss_normal; NEW(mx1); mx2:=mx1; mx2^.next:=nil; mx2^.back:=nil; mx2^.poly_head:=nil; mx2^.poly_tail:=nil; init_replace_matrix:=status; end; (***********************************************************************) procedure advance_matrix(VAR node: ptr_matrix_node); begin NEW(node^.next); node^.next^.back:=node; node:=node^.next; node^.next:=nil; node^.poly_head:=nil; node^.poly_tail:=nil; end; (***********************************************************************) procedure mulmatric; VAR nrow,ncol:array [1..2] of integer; matrix_ :ptr_matrix_node; i,k,j,h,iter1,iter2:integer; nmatrix:integer; matrix_heads :array [1..2] of ptr_matrix_node; matrix_marker:ptr_matrix_node; n_variables:integer; variables :sst; result,result_marker:ptr_matrix_node; mark1,mark2: ptr_matrix_node; choice:char; more: boolean; rowc,colc:integer; final_result:ptr_matrix_node; final_result_marker:ptr_matrix_node; this,that:ptr_matrix_node; temp:ptr_matrix_node; status:integer; limit:integer; label 10,20; label 1; procedure init_row_col; begin for i:=1 to 2 do begin nrow[i]:=0; ncol[i]:=0; END; end; BEGIN status:=ss_normal; more:=true; get_info(n_variables,variables); nmatrix:=0; init_row_col; REPEAT REPEAT nmatrix:=nmatrix+1; status:=init_replace_matrix(matrix_heads[nmatrix] ,matrix_marker ); if (status <> ss_normal) then goto 1; 20: REPEAT write('Matrix ',nmatrix,' number of rows ? > '); readln(nrow[nmatrix]); UNTIL nrow[nmatrix] <>0; REPEAT write('Matrix ',nmatrix,' number of coloums ? > '); readln(ncol[nmatrix]); UNTIL ncol[nmatrix] <>0; IF nmatrix=2 then IF ncol[1]<>nrow[2] then begin writeln('number of col in first should equal row of second'); ncol[2]:=0; nrow[2]:=0; goto 20; END; FOR i:=1 to nrow[nmatrix] do begin FOR k:=1 to ncol[nmatrix] do begin 10: write('marix',nmatrix,' (',i,',',k,') ? >'); status:=get_poly (matrix_marker^.poly_head ,matrix_marker^.poly_tail ,n_variables ,variables ); IF status<>ss_normal then begin writeln('Bad poly input poly not parsed input again'); goto 10; END; simplify_list(matrix_marker^.poly_head); IF (confirm=true) then begin write('Confirm > '); display(matrix_marker^.poly_head ,n_variables ,variables ); END; advance_matrix(matrix_marker); END; END; matrix_marker^.back^.next:=nil; dispose(matrix_marker); (* get rid of last node *) UNTIL nmatrix=2; if debug then writeln('-- after until nmartix=2 loop'); status:=init_replace_matrix(result ,result_marker ); if (status <> ss_normal) then goto 1; mark1:=matrix_heads[1]; mark2:=matrix_heads[2]; if debug then writeln('-- befor the muly loop'); FOR j:=1 to nrow[1] do begin FOR i:=1 to ncol[2] do begin FOR k:=1 to ncol[1] do begin if debug then writeln ('k=',k,'i=',i,'k=',k); if debug then writeln('--befor mult mark1.coef.enu=',mark1^.poly_head^.coef.enum); if debug then writeln('--befor mult mark1.coef.deno=',mark1^.poly_head^.coef.deno); mulpoly(mark1^.poly_head ,mark2^.poly_head , result_marker^.poly_head ,n_variables ); simplify_list(result_marker^.poly_head); if debug then writeln('--AFTER mult mark1.coef.deno=',result_marker^.poly_head^.coef.deno); if debug then writeln('--AFTER mult mark1.coef.deno=',result_marker^.poly_head^.coef.deno); IF k1 then begin FOR rowc:=1 to ncol[1]-1 do (* rewind for next iteration *) mark1:=mark1^.back; FOR rowc:=1 to (( nrow[2]-1)*(ncol[2]-1)) do mark2:=mark2^.back; (* rewind for next iteration *) END; END; IF ncol[1]>1 then begin FOR rowc:=1 to ncol[1]-1 do begin mark1:=mark1^.next; mark2:=matrix_heads[2]; END; END; END; if debug then writeln ('-- afte mul loop with for'); IF (nrow[1] > 1 ) then begin result_marker^.back^.next:=nil; DISPOSE(result_marker); END; (* now the result of above multiplication is in a link list of nrow[1]*ncol[1]*ncol[2] elements, scan this and add every ncol[1] elements these will make a node in resultanat matrix e.g. for 2x2 by 2x4 we get 2x2x4=16 that need to tied up to make a resultant matrix of 2x4 of 8 elements *) status:=init_replace_matrix(final_result ,final_result_marker ); if (status <> ss_normal) then goto 1; this:=result; that:=result^.next; IF ncol[1]*nrow[1]*ncol[2]=1 then final_result:= result ELSE begin if debug then writeln (' befor addpoly in marix multiplication '); if debug then writeln('-- this.coef.enum=',this^.poly_head^.coef.enum); if debug then writeln('-- this.coef.deno=',this^.poly_head^.coef.deno); if debug then writeln('-- that.coef.enum=',this^.poly_head^.coef.enum); if debug then writeln('***** befor 3 for loops ***********'); FOR k:=1 to nrow[2] do begin if debug then writeln('--k=',k); FOR h:=1 to ncol[2] do begin if debug then writeln('--h=',h); FOR k:=1 to ncol[1]-1 do begin (* it ok we start at 2 *) if debug then writeln('--k=',k); addpoly(this^.poly_head ,that^.poly_head ,final_result_marker^.poly_head ); simplify_list(final_result^.poly_head); IF k+1 < ncol[1] then begin if debug then writeln('--befor this=fianl resul'); this:=final_result_marker; that:=that^.next; END; END; IF h+1 '); advance_matrix(final_result_marker); END; END; IF k+1 '); if final_result_marker^.poly_head=nil then writeln('0') else display(final_result_marker^.poly_head ,n_variables ,variables ); if debug then writeln ('-- befor final result marker=final.next'); final_result_marker:= final_result_marker^.next; END; END; if debug then writeln('-- befor asking if need to muly afain '); write('Do you want to MULTIPLY to this matrix [y/n] ? > '); readln(choice); IF (choice='n') or (choice='n') then begin write('do you like to MULTIPLY 2 new MATRIX [y/n] ? > '); readln (choice) ; IF (choice='y') or (choice='Y') then begin FOR i:=1 to 2 do free_matrix(matrix_heads[i]); nmatrix:=0; END ELSE more:=false; END ELSE begin nmatrix:=1; free_matrix(matrix_heads[nmatrix]); matrix_heads[nmatrix]:=result; ncol[1]:=ncol[2]; nrow[1]:=nrow[1]; ncol[2]:=0; nrow[2]:=0; END; UNTIL (more=false); FOR i:=1 to 2 do free_matrix(matrix_heads[i]); 1: (* change to function later *) end;