[Solved-1 Solution] Matrix multiplication apache pig ?
What is matrix multiplication
- In mathematics matrix multiplication or the matrix product is a binary operation that produces a matrix from two matrices. The definition is motivated by linear equations and linear transformations on vectors, which have numerous applications in applied mathematics, Physics etc..
Problem :
Here is an example to perform matrix multiplication in pig latin.
matrix1 = LOAD 'mat1' AS (row,col,value);
matrix2 = LOAD 'mat2' AS (row,col,value);
mult_mat = COGROUP matrix1 BY row, matrix2 BY col;
mult_mat = FOREACH mult_mat {
A = COGROUP matrix1 BY col, matrix2 BY row;
B = FOREACH A GENERATE group AS col, matrix1.value*matrix2.value AS prod;
GENERATE group AS row, B.col AS col, SUM(B.prod) AS value;}
- This coding is not working
A = COGROUP matrix1.
with
ERROR org.apache.pig.tools.grunt.Grunt - ERROR 1200: <line 14, column 37> mismatched input
Solution 1:
The below code is helps for matrix multiplication in pig
matrix1 = LOAD 'mat1' AS (row,col,value);
matrix2 = LOAD 'mat2' AS (row,col,value);
A = JOIN matrix1 BY column FULL OUTER, matrix2 BY row;
B = FOREACH A GENERATE matrix1::row AS m1r, matrix2::column AS m2c, (matrix1::value)*(matrix2::value) AS value;
C = GROUP B BY (m1r, m2c);
multiplied_matrices = FOREACH C GENERATE group.$0 as row, group.$1 as column, SUM(B.value) AS val;
Multiplied matrices should return the product of matrix1*matrix2 in the same format that the 2 matrices were entered, (row, col, value).