# Series of List manipulation Question

Given a list

{{{a1},{a2},{a3},{a}},
{{a4},{a2},{a6},{a}},
{{a7},{a8},{a9},{a}},
{{b1},{b2},{b3},{b}},
{{b4},{b2},{b6},{b}}}


I want to change this to

{{{1},{a2},{a1*a3+a4*a6},{a}},
{{a7},{a8},{a9},{a}},
{{1},{b2},{b1*b3+b4*b6},{b}}}


As you can see The first element is multiplied to the third element and gets contracted by summing all the modified third elements, which labels at the end and the same and second element are the same. When Contraction happens, first element becomes 1.

The first element is a number, third column is an expression, and labels are some operators.

How can I achieve this?

Caution It is not a matrix manipulation problem!

• ah! yes, b5 must be b2, and I forgot to write that when ever they are contracted, first element becomes 1.
– Tom
Commented Mar 29, 2018 at 22:20
• Doesn't the data contain unnecessary extra depth? Commented Apr 29, 2018 at 7:14

A = {
{{a1}, {a2}, {a3}, {a}},
{{a4}, {a2}, {a6}, {a}},
{{a7}, {a8}, {a9}, {a}},
{{b1}, {b2}, {b3}, {b}},
{{b4}, {b2}, {b6}, {b}}
};
Values[
Merge[
(x \[Function] x[[{2, 4}]] -> x) /@ A,
x \[Function] If[Length[x] == 1,
x,
{{1}, x[[1, 2]], Total[x[[All, 1]] x[[All, 3]]], x[[1, 4]]}
]
]
]


{{{1}, {a2}, {a1 a3 + a4 a6}, {a}}, {{{a7}, {a8}, {a9}, {a}}}, {{1}, {b2}, {b1 b3 + b4 b6}, {b}}}