Task

Multiply two matrices together.

They can be of any dimensions, so long as the number of columns of the first matrix is equal to the number of rows of the second matrix.




 
program Matrix_multiplication type BasicProgram {}
 
	function main()
		a float[][] = [[1,2,3],[4,5,6]];
		b float[][] = [[1,2],[3,4],[5,6]];
		c float[][] = mult(a, b);
	end
 
	function mult(a float[][], b float[][]) returns(float[][])
		if(a.getSize() == 0)
			return (new float[0][0]);
		end
		if(a[1].getSize() != b.getSize())
			return (null); //invalid dims
		end
 
	   	n int = a[1].getSize();
	   	m int = a.getSize();
	   	p int = b[1].getSize();
 
		ans float[0][0];
		ans.resizeAll([m, p]);
 
		// Calculate dot product.
		for(i int from 1 to m)
			for(j int from 1 to p)
				for(k int from 1 to n)
	            	                ans[i][j] += a[i][k] * b[k][j];
				end
			end
		end
		return (ans);
	end
end
 