3
$\begingroup$

Is it possible somehow to contract two tensors, say A={{a1,a2,a3},{a4,a5,a6}} and B={b1,b2,b3}, where the elements a1,a2,...,b1,b2 are themselves vectors:

 A = {{a1, a2, -a3}, {a4, -a5, a6}, {a7, a8, a9}};
 B={b1,b2,b3};
 x=TensorProduct[A,B];
 TensorContract[x,{1,3}];

 (*{a1 b1 + a4 b2 + a7 b3, a2 b1 - a5 b2 + a8 b3, -a3 b1 + a6 b2 + a9 b3} *)

and get instead of the ordinary Times between elements like a1 b1 the KroneckerProduct[a1,b1]. A replacement rule like Times->KroneckerProduct does not work unfortunately, because a minus sign in Mathematica is handled as Times[-1,a5,b2]. Thus replacing here Times->KroneckerProduct yields something nonsensible, because -1 is not a tensor.

$\endgroup$
2
  • $\begingroup$ When I enter your code, it gives an error since a1....b3 are not defined as lists - but if you go ahead and define them as lists, like a1= {a11, a12} etc, then you can't do a KroneckerProduct like you want to. Do you want to make this substitution at the level on the TensorProduct or not until TensorContract? $\endgroup$
    – Jason B.
    Commented Dec 28, 2015 at 9:56
  • $\begingroup$ @JasonB I fixed the problem in the question and addded the result. What I want is KroneckerProduct[a1,b1]+...-KroneckerProduct[a5,b2]+.... . The a1,a2,... and b1,b2,... are column and row vectors that I put in there through replacement rules. $\endgroup$
    – jak
    Commented Dec 28, 2015 at 10:03

1 Answer 1

4
$\begingroup$

This should do the trick,

TensorContract[x, {1, 3}] /. {Times[-1, x_, y_] :>
   Times[-1, KroneckerProduct[x, y]], 
   Times[x_, y_] :> KroneckerProduct[x, y]}
(* {KroneckerProduct[a1, b1] + KroneckerProduct[a4, b2] + KroneckerProduct[a7, b3], 
    KroneckerProduct[a2, b1] - KroneckerProduct[a5, b2] + KroneckerProduct[a8, b3],
    -KroneckerProduct[a3, b1] + KroneckerProduct[a6, b2] + KroneckerProduct[a9, b3]} *)

I wish this could be done better though, since it has one fatal flaw. The vectors in question will always be in lexicographical order. In this case, you always want the KroneckerProduct[a,b], so it isn't a problem.

$\endgroup$
2
  • $\begingroup$ Thank you so much! Thanks to your comment regarding the lexicographical order I now simply an A in front of all column vectors and a B in front of all row vectors, such that I always get the correct order. Yesterday I resorted the terms by hand $\endgroup$
    – jak
    Commented Dec 28, 2015 at 10:22
  • $\begingroup$ @JakobH - it seems to me that we could write our own TensorProduct function that uses KroneckerProduct in place of Times, and then we could use any matrices. $\endgroup$
    – Jason B.
    Commented Dec 28, 2015 at 10:23

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.