4
$\begingroup$

Suppose I have two matrices A and B. After some calculations, I arrive at a matrix M and realize that M can be written as a linear combination of A and B,

M = a  A + b  B

for scalar constants a and b.

Is there a way to instruct Mathematica to express M as a A + b B?

In general, I am thinking of a function that takes a matrix M as an argument and explicitly writes it as a linear combination of a set of given matrices (in this case A and B).

Example:

A={{x,1},{0,y}};
B={{y^2,0},{x,2}}

From another calculation I have

M={{x+2y^2,1},{2x,y+4}};

I am looking for a function (or whatever can work) in Mathematica such that invoking f[M] produces:

A + 2*B
$\endgroup$

1 Answer 1

7
$\begingroup$
$Version

(* "14.0.0 for Mac OS X ARM (64-bit) (December 13, 2023)" *)

Clear["Global`*"]

A = {{x, 1}, {0, y}};
B = {{y^2, 0}, {x, 2}};

M = {{x + 2 y^2, 1}, {2 x, y + 4}};

sol = Solve[
   Flatten[Thread /@ Thread[a*A + b*B == M]],
   {a, b}, {x, y}][[1]]

(* {a -> 1, b -> 2} *)

EDIT: Solve can automatically Thread

sol = Solve[a*A + b*B == M, {a, b}, {x, y}][[1]]

(* {a -> 1, b -> 2} *)

Verifying,

a*A + b*B == M /. sol

(* True *)

HoldForm[a*A + b*B] /. sol

enter image description here

$\endgroup$
6

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.