0
$\begingroup$

Assuming G a 4x4, C a 4x2, and DT a 2x4 matrix, I want to vectorize the following expression, i.e., to transform it into matrix notation form to accelerate its computation. For instance, C[1,1]*DT[1,3]+C[1,2]*DT[2,3] are to be replaced with G[1,3]. However, I cannot do the task using Replace or ReplaceAll.

c = Table[C[i, j], {i, 1, 4}, {j, 1, 2}]
dt = Table[DT[i, j], {i, 1, 2}, {j, 1, 4}]
g = Table[G[i, j], {i, 1, 4}, {j, 1, 4}]
cdt = c.dt

A=(G[1, 1]^4 + (2*G[2, 2] + G[3, 3])*
    G[1, 1]^3 + (G[2, 2]^2 + G[3, 3]*G[2, 2] - 2*G[1, 2]*G[2, 1] - 
      C[1, 1]*DT[1, 3]*G[3, 1] - C[2, 1]*DT[1, 3]*G[2, 1] - 
      C[3, 1]*DT[1, 2]*G[1, 2] - C[1, 2]*DT[2, 3]*G[3, 1] - 
      C[2, 1]*DT[1, 3]*G[3, 2] - C[2, 2]*DT[2, 3]*G[2, 1] - 
      C[3, 2]*DT[2, 2]*G[1, 2] - C[2, 2]*DT[2, 3]*G[3, 2])*
    G[1, 1]^2 + (C[1, 1]*DT[1, 3]*G[2, 1]^2 - 
      G[1, 2]*G[2, 1]*G[3, 3] - 2*G[1, 2]*G[2, 1]*G[2, 2] + 
      C[1, 2]*DT[2, 3]*G[2, 1]^2 + C[1, 1]*DT[1, 2]*G[1, 2]*G[3, 1] + 
      C[2, 1]*DT[1, 2]*G[1, 2]*G[2, 1] + 
      C[1, 1]*DT[1, 3]*G[2, 1]*G[3, 2] - 
      C[1, 1]*DT[1, 3]*G[2, 2]*G[3, 1] + 
      C[1, 2]*DT[2, 2]*G[1, 2]*G[3, 1] + 
      C[2, 1]*DT[1, 2]*G[1, 2]*G[3, 2] + 
      C[2, 1]*DT[1, 3]*G[1, 2]*G[3, 1] + 
      C[2, 2]*DT[2, 2]*G[1, 2]*G[2, 1] - 
      C[3, 1]*DT[1, 2]*G[1, 2]*G[2, 2] + 
      C[1, 2]*DT[2, 3]*G[2, 1]*G[3, 2] - 
      C[1, 2]*DT[2, 3]*G[2, 2]*G[3, 1] + 
      C[2, 2]*DT[2, 2]*G[1, 2]*G[3, 2] + 
      C[2, 2]*DT[2, 3]*G[1, 2]*G[3, 1] - 
      C[3, 2]*DT[2, 2]*G[1, 2]*G[2, 2])*G[1, 1] + 
   G[1, 2]^2*G[2, 1]^2 - C[1, 1]*DT[1, 2]*G[1, 2]*G[2, 1]^2 - 
   C[1, 2]*DT[2, 2]*G[1, 2]*G[2, 1]^2 - 
   C[2, 1]*DT[1, 2]*G[1, 2]^2*G[3, 1] + 
   C[3, 1]*DT[1, 2]*G[1, 2]^2*G[2, 1] - 
   C[2, 2]*DT[2, 2]*G[1, 2]^2*G[3, 1] + 
   C[3, 2]*DT[2, 2]*G[1, 2]^2*G[2, 1] - 
   C[1, 1]*DT[1, 2]*G[1, 2]*G[2, 1]*G[3, 2] + 
   C[1, 1]*DT[1, 2]*G[1, 2]*G[2, 2]*G[3, 1] - 
   C[1, 2]*DT[2, 2]*G[1, 2]*G[2, 1]*G[3, 2] + 
   C[1, 2]*DT[2, 2]*G[1, 2]*G[2, 2]*G[3, 1])/(G[1, 1]^3 + 
   G[2, 2]*G[1, 1]^2 - G[1, 2]*G[2, 1]*G[1, 1])


Simplify[A] //. cdt -> g

Thanks for your help.

$\endgroup$
2
  • 2
    $\begingroup$ Do this: Collect[A, _G] /. C[x_, 1] DT[1, y_] :> G[x, y] - C[x, 2] DT[2, y] // Simplify. But also, don't use capital letters for user-defined symbols (e.g. C is a built-in Mathematica symbol, and by using it you might run into problems since it's already defined as something). $\endgroup$
    – march
    Commented Mar 23, 2017 at 3:52
  • $\begingroup$ Would you please answer my question in a more detailed version to accept it. $\endgroup$
    – remo
    Commented Mar 23, 2017 at 5:25

1 Answer 1

2
$\begingroup$

Do this:

Collect[A, _G] /. C[x_, 1] DT[1, y_] :> G[x, y] - C[x, 2] DT[2, y] // Simplify
$\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.