1
$\begingroup$

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!

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

1 Answer 1

2
$\begingroup$
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}}}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.