[Math] Compute the change of basis matrix in Matlab

change-of-basislinear algebraMATLABproof-verificationvector-spaces

First of all, I didn't know where to ask this question, if here or on SO, so I decided to post in both, even if I know we should not do that.

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 = \{b_1, … , b_n \}$$ and $$D = \{d_1, … , d_n \}$$ 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 (according to some notes that I've):

$$ \begin{pmatrix} d_1 \\ \vdots \\ d_n \end{pmatrix} = S * \begin{pmatrix} b_1 \\ \vdots \\ b_n \end{pmatrix}$$

Or, by splitting up all the rows:

$$d_1 = s_{11} * b_1 + s_{12} * b_2 + … + s_{1n} * b_n$$
$$d_2 = s_{21} * b_1 + s_{22} * b_2 + … + s_{2n} * b_n$$
$$…$$
$$d_3 = s_{n1} * b_1 + s_{n2} * b_2 + … + s_{nn} * b_n$$

Note that $d_1$, $b_1$, $d_2$, $b_2$, etc, should all be column vectors. This can be further represented as

$$d_1 = \begin{pmatrix} b_1 & b_2 & \cdots & b_n \end{pmatrix} * \begin{pmatrix} s_{11} \\ s_{12} \\ \vdots \\ s_{1n} \end{pmatrix}$$

$$d_2 = \begin{pmatrix} b_1 & b_2 & \cdots & b_n \end{pmatrix} * \begin{pmatrix} s_{21} \\ s_{22} \\ \vdots \\ s_{2n} \end{pmatrix}$$

$$…$$

$$d_2 = \begin{pmatrix} b_1 & b_2 & \cdots & b_n \end{pmatrix} * \begin{pmatrix} s_{n1} \\ s_{n2} \\ \vdots \\ s_{nn} \end{pmatrix}$$

Lets call the matrix $$\begin{pmatrix} b_1 & b_2 & \cdots & b_n \end{pmatrix}$$ whose columns are the columns vectors of $B$, $A$.

Note that what we need now to find are all the entries $s_{ij}$ 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 = \begin{pmatrix} s_{11} & s_{12} & \cdots & s_{1n} \\ s_{21} & s_{22} & \cdots & s_{2n} \\ \cdots \\ s_{n1} & s_{n2} & \cdots & s_{nn}\end{pmatrix} $$

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?

Best Answer

Basically, your explanation is hard to follow because of a mixture between column and row vectors.

You should stick to a certain convention: every time you consider a vector known through its components with respect to a a basi, connect it with a column vector (this is the most usual convention), with notation $V$, and stick to notation $V^T$ if it is a row vector.

If such a convention is used, if we consider $B = (b_1, ... , b_n)$ and $D = (d_1, ... , d_n)$ (where the $b_k$ and the $d_k$ are column vectors), then, $B$ and $D$ are $n \times n$ matrices. We have the equivalence:

$$(\forall k, Sb_k=d_k) \ \Rightarrow \ SB=D \ \ (1)$$

From formula (1), the matrix you are looking for (as you say more or less) is simply

$$S=DB^{-1} \ \ (2)$$

For example, if you want the matrix $S$ sending $b_1=$$1 \choose 1$ and $b_2=$$-1 \choose 1$ to $d_1=$$0 \choose 1$ and $d_2=$$ -1 \choose 0$ resp., formula (2) gives:

$$S=\begin{pmatrix} 0 & -1\\ 1 & 0 \end{pmatrix} \begin{pmatrix} 1 & -1\\ 1 & 1 \end{pmatrix}^{-1}=\frac{1}{2}\begin{pmatrix} 1 & -1\\ 1 & 1 \end{pmatrix}$$

Related Question