I've an assignment where I basically need to create a function which, given two basis (which I'm representing as a matrix of vectors), it should return the change of basis matrix from one basis to the other.
So far this is the function I came up with, based on the algorithm that I will explain next:
function C = cob(A, B)
% Returns C, which is the change of basis matrix from A to B,
% that is, given basis A and B, we represent B in terms of A.
% Assumes that A and B are square matrices
n = size(A, 1);
% Creates a square matrix full of zeros
% of the same size as the number of rows of A.
C = zeros(n);
for i=1:n
C(i, :) = (A\B(:, i))';
end
end
And here are my tests:
clc
clear out
S = eye(3);
B = [1 0 0; 0 1 0; 2 1 1];
D = B;
disp(cob(S, B)); % Returns cob matrix from S to B.
disp(cob(B, D));
disp(cob(S, D));
Here's the algorithm that I used based on some notes. Basically, if I have two basis B = {b1, ... , bn}
and D = {d1, ... , dn}
for a certain vector space, and I want to represent basis D
in terms of basis B
, I need to find a change of basis matrix S
. The vectors of these bases are related in the following form:
(d1 ... dn)^T = S * (b1, ... , bn)^T
Or, by splitting up all the rows:
d1 = s11 * b1 + s12 * b2 + ... + s1n * bn
d2 = s21 * b1 + s22 * b2 + ... + s2n * bn
...
dn = sn1 * b1 + sn2 * b2 + ... + snn * bn
Note that d1
, b1
, d2
, b2
, etc, are all column vectors. This can be further represented as
d1 = [b1 b2 ... bn] * [s11; s12; ... s1n];
d2 = [b1 b2 ... bn] * [s21; s22; ... s2n];
...
dn = [b1 b2 ... bn] * [sn1; sn2; ... s1n];
Lets call the matrix [b1 b2 ... bn]
, whose columns are the columns vectors of B
, A
, so we have:
d1 = A * [s11; s12; ... s1n];
d2 = A * [s21; s22; ... s2n];
...
dn = A * [sn1; sn2; ... s1n];
Note that what we need now to find are all the entries sij
for i=1...n
and j=1...n
. We can do that by left-multiplying both sides by the inverse of A
, i.e. by A^(-1)
.
So, S
might look something like this
S = [s11 s12 ... s1n;
s21 s22 ... s2n;
...
sn1 sn2 ... snn;]
If this idea is correct, to find the change of basis matrix S
from B
to D
is really what I'm doing in the code.
Is my idea correct? If not, what's wrong? If yes, can I improve it?
Things become much easier when one has an intuitive understanding of the algorithm.
There are two key points to understand here:
C(B,B)
is the identity matrix (i.e., do nothing to change fromB
toB
)C(E,D)C(B,E) = C(B,D)
, think of this asB -> E -> D = B -> D
A direct corollary of 1 and 2 is
C(E,D)C(D,E) = C(D,D)
, the identity matrixin other words
C(E,D) = C(D,E)
-1Summarizing. Algorithm to calculate the matrix
C(B,D)
to change fromB
toD
:C(B,E) = [b1, ..., bn]
(column vectors)C(D,E) = [d1, ..., dn]
(column vectors)C(E,D)
as the inverse ofC(D,E)
.C(B,D)
as the productC(E,D)C(B,E)
.Example
Verification
which shows that the columns of
C(B,D)
are in fact the coordinates ofb1
andb2
in the baseD
.