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