2

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?

nbro
  • 15,395
  • 32
  • 113
  • 196

1 Answers1

5

Things become much easier when one has an intuitive understanding of the algorithm.

There are two key points to understand here:

  1. C(B,B) is the identity matrix (i.e., do nothing to change from B to B)
  2. C(E,D)C(B,E) = C(B,D) , think of this as B -> E -> D = B -> D

A direct corollary of 1 and 2 is

  1. C(E,D)C(D,E) = C(D,D), the identity matrix

in other words

  • C(E,D) = C(D,E)-1

Summarizing. Algorithm to calculate the matrix C(B,D) to change from B to D:

  1. Define C(B,E) = [b1, ..., bn] (column vectors)
  2. Define C(D,E) = [d1, ..., dn] (column vectors)
  3. Compute C(E,D) as the inverse of C(D,E).
  4. Compute C(B,D) as the product C(E,D)C(B,E).

Example

B = {(1,2), (3,4)}
D = {(1,1), (1,-1)}

C(B,E) = | 1  3 |
         | 2  4 |

C(D,E) = | 1  1 |
         | 1 -1 |

C(E,D) = | .5  .5 |
         | .5 -.5 |

C(B,D) = | .5  .5 | | 1 3 | = | 1.5  3.5 |
         | .5 -.5 | | 2 4 |   | -.5  -.5 |

Verification

1.5 d1 + -.5 d2 = 1.5(1,1) + -.5(1,-1) = (1,2) = b1
3.5 d1 + -.5 d2 = 3.5(1,1) + -.5(1,-1) = (3,4) = b2

which shows that the columns of C(B,D) are in fact the coordinates of b1 and b2 in the base D.

Leandro Caniglia
  • 14,495
  • 4
  • 29
  • 51
  • Anyway, I forgot to edit my post, but my algorithm has an error (I am pretty sure). This line `C(i, :) = (A\B(:, i))';` should instead be `C(:, i) = A\B(:, i);`... – nbro Feb 28 '16 at 21:24
  • @nbro Thanks for telling me. Note however that my answer is an attempt to provide some explanation around the calculations. – Leandro Caniglia Feb 28 '16 at 21:28
  • Strange, with the change I'm pointing out in the first comment above (which I thought should fix a bug that I had), condition 1 and C(E,D) = C(D,E)^{-1} are satisfied by the algorithm, but not the others...Instead, if I use the original algorithm in my question above, all your conditions are satisfied. I'm must be honest, I've not yet understood your second point, except of course from the reason why you're doing it, i.e. this `B -> E -> D = B -> D` is very intuitive, but not the way you obtain it, i.e. `C(E,D)C(B,E) = C(B,D)`... – nbro Feb 28 '16 at 22:06
  • 1
    @nbro Given that we multiply from the right `C(B,E)` is applied first and `C(E,D)` second. The first matrix changes from `B` to `E` and the other from `E` to `D`. Then the product changes from `B` to `D`. Also, take a look at the example I've just included. – Leandro Caniglia Feb 28 '16 at 22:26