%==========================================================================
% This plots the sine and cosine functions in the first slide
%==========================================================================
figure; hold on;
set(gca,'Fontsize',16);
x = -pi:0.01:pi;
plot(x*180/pi,sin(x), 'b-', 'LineWidth',2); grid on;
plot(x*180/pi,cos(x), 'r-', 'LineWidth',2); grid on;
box on;
axis square;


%==========================================================================
% Review of Matrix Algebra
%
% m x n matrix is a rectangular array of elements arranged in m rows and n
% columns. Such matrix is said to have order m x n. The elements (the
% entries of the matrix) can be scalars or functions or whatever is
% meaningful in your problem domain
%
% If m = 1 we have a row matrix; if n = 1 we have a column matrix
% if m = n we have a square matrix. Two matrices are said to be equal if
% they are of the same order and all elements are the same.
%
% In general a matrix A is represented as A = [a(i,j)] where "i" indicates
% a row and "j" indicates a column in which the element a(i,j) of the
% matrix is located. There are several operations on the matrix, e.g. the
% transposed operation A' represented with a " ' " in matlab, for a matrix
% A the transposed is A^t=[a(i,j)]^t = [a(j,i)], which amounts to
% interchanging the rows and columns:
%
% [x2;y2] = [A]*[x1;y1];
%==========================================================================

theta=pi/4;

A=[ cos(theta) sin(theta); ...   %Frame Rotation - Point Fixed
   -sin(theta) cos(theta)];

x1=1; y1=1;  %the individual coordinates of P on the plane

r=[x1; y1];  %the vector r from O to P

r1=A*r;  %rotated frame viewed from the fixed point

C=A';    %the tranposed A   %Point Rotation - Frame Fixed

r2=C*r;  %rotated point viewed from the fixed frame

r3=C*r1; %Equivalence, back to r

%==========================================================================
%Matrix operations
%==========================================================================
B=A;
a=isequal(A,B); %a = 1 yes, same order and same entries
b=isequal(A,C); %b = 0 C is the transpose of A

D=A*A';  %identity

q=D*r; 
d=isequal(r,q) % c=1 because D is the identity 

%==========================================================================
%Addition and Subtraction
%A+B = [a(i,j) + b(i,j)] if A and B have the same order add elem wise
%==========================================================================

A=[1 2; 4 -1]; B=[3 1; 2 3]; 
C=A+B;

O=zeros(2,2); I=eye(2,2);
D=A+O; isequal(A,D); %Z-zero matrix sum identity
E=B*I; isequal(E,B); %I-Identity matrix, multip identity

%==========================================================================
%the negative of a matrix -A=[-a(i,j)], the matrix whose elements are the
%negative of the elements of the matrix A and for any matrix A we have that
%A+(-A)=O the identity for the sum
%==========================================================================

T=A+(-A);
isequal(T,O)

%==========================================================================
%Subtraction from matrices of the same order is usually defined as addition
%of the negative: A-B=A+(-B)
%==========================================================================

D1=A+(-B);
D2=A-B;
isequal(D1,D2)

%==========================================================================
%Multiplication by a scalar: If k is a scalar and A is a matrix, the
%product of the scalar and the matrix is k*A=[k*a(i,j)] which is simply
%multiplying each element of A by the scalar k
%==========================================================================
k=2;
B=2*A;

%we can write now A+A=2A and -A=(-1)*A

%==========================================================================
%Products of matrices
%We need to review the dot product of vectors, say in R2 v=(v1,v2) and
%w=(w1,w2), then the dot product is a.b=a1*b1+a2*b2
%==========================================================================
v=[1 3]; w=[-4 2];
s=dot(v,w); %this is v(1)*w(1) + v(2)*w(2) which gives the scalar s

%==========================================================================
%think of the dot product as a machine that takes 2 coins and gives a
%coke out; i.e. the 2 vectors and gives you a scalar; we will see that the
%identity matrix is hidden in there bet the 2 vectors  when the scalar 
%product is Euclidean and other different matrices appear with certain 
%properties when the space is non-flat, in non-Euclidean geometries 
%we may get to this soon in a couple of lectures
%The definition extends to dimension n using n-tuples
%
%Matrix Product: The element in the ith row and the jth column of the
%product AB is the dot product of the ith row vector of the matrix A and
%the jth column of the matrix B. Thus is the ith row of A is (ai1, ai2,
%..., ain) and the jth column vector of B is (b1j, b2j,...,bnj) then the
%element common to the ith row and the jth column of C=A*B is 
% c(i,j)=SUM(k=1:p) aik*bkj
%==========================================================================
A=[1 3; -2 7]; B=[0 2; 4 3];

%The element in the 1st row and 1st col of the product AB is the dot
%product of the 1st row of the matrix A and the 1st col of the matrix B
%that is (1,3)*(4,4)=1*0 + 3*4 = 12

C=A*B;
D=B*A;
isequal(C,D);
%and notice that the results are different: matrix multip is
%non-commutative. The mathematical system which consists of 2x2 matrices is
%not a field
%Notwithstanding under multip for any 2x2 rotation matrices A we always
%have that A'*A=A*A'=I the identity matrix

%Rotation matrices:
%The transformed coordinates in the case where the frame rotates while the
%points (vectors) remain fixed were given by
%
%x2 = x1*cos(theta) + y1*sin(theta);
%y2 = y1*cos(theta) - X1*sin(theta); 

%Now swap the x1 and y1 in y2
%[x2; y2] = A*[x1;y1]; where A=[cos(theta) sin(theta); -sin(theta) cos(theta)]
% and we say that A transforms the vector r1=[x1;y1] into r2=[x2;y2]
%A is called a rotation operator that takes r1 into r2: r1=A*r2
%Now for B=[cos(theta) -sin(theta); sin(theta) cos(theta)] we obtain
%another rotation operator. In this case we may verify that for the second
%type of rotation in the plane, when the frame is fixed and the vector
%(point) rotates we use B and B = A'
%Now consider that you may want to invert the rotation. First we may just
%rotate by -theta. This would give 
%B=[cos(-theta) sin(-theta); -sin(-theta) cos(-theta)]
%We know that cos(-theta)=cos(theta) and sin(-theta)=-sin(theta), thus we
%get that B=[cos(theta) -sin(theta); sin(theta) cos(theta)] exactly 
%as before, namely the transpose of A => A'. This means that the rotation
%matrix necessary to "undo" or invert the rotation is A', but we had
%already seen that A'*A=I so we came across a neat operator
%Equivalently we can invert by the rotation that we are considering by a
%rotation of the point (or vector) through an angle theta as the frame
%remains fixed. The matrix associated with this operation is B, so we
%arrive at the equivalence - see below again

theta=pi/4;

A=[ cos(theta) sin(theta); ...   %Frame Rotation - Point Fixed
   -sin(theta) cos(theta)];

x1=1; y1=1;  %the individual coordinates of P on the plane

r=[x1; y1];  %the vector r from O to P

r1=A*r;  %rotated frame viewed from the fixed point

B=A';    %the tranposed A   %Point Rotation - Frame Fixed

r2=B*r;  %rotated point viewed from the fixed frame

r3=B*r1; %Equivalence, back to r => B*(A*r)=A'*A*r=I*r=r

isequal(r,r3); 
% the original vector r and the vector r3 first through a frame rotation 
%r1 = A*r and then trough a Point rotation
%Finally whether theta is CCW or CW the result holds -so long as you are
%consistent

%Determinants
%How do we do matrix division?
%We need to define division in terms of the multiplication by the inverse
%of the matrix, thus we need to define ways to find the inverse
%In the case of square matrices nxn which we will be using here, we need to
%find matrix B such that AB=BA=I
%For example, say that you have a linear system of equations with 2
%unknowns: x+3y=7; 2x+7y=16; Using the product of matrices to write it in
%Matlab form, we get AX=B as A=[1 3; 2 7]; B=[7; 16]; X=[x;y]
%If we had the inverse of A available A^-1, we can write A^-1AX=A^-1B and
%obtain X=A^-1B directly giving X=[7 -3;-2 1][7;16]=[1;2]
%to find the inverse A^-1 we need its determinant
%for dimension 1 (a scalar), B=[b], det[b]=b
%for dimension 2 (2x2 matrix) A=[a b; c d], det(A)=ad - bc
%for dimension n>2 we need to introduce the notions of minors and cofactors
%Minors: basically erase the rowi and the colj to obtain the elements of
%the minor ij In the 3x3 eaxample below:
%A =[ a11 a12 a13;
%     a21 a22 a23;
%     a31 a32 a33]
% the minor a32 is the 2x2 matrix M32=[a11 a13; a21 a23] and to obtain its
% determinant use the rule above: a11*a23 - a13*a21
%
%Cofactors: Here we refer to the sign: if ij add up to an even number the
%sign is + and if they add up to an odd number, then is -
%(-1)^i+j * Aij where Aij is the Minor
%To obtain the determinant of an nxn matrix you have to recursively obtain
%its signed Minors till you reduce to a 2x2 and then work your way back up
%Luckily in Matlab you type det(M) and you get the determinant
%To get the inverse you also need the Adjoint Matrix you get the signed
%minor, which is the cofactor matrix and then tranpose it
%notation is A=[a(i,j)]; minor Aij; cofactor cAij=(-1)^i+j Aij; Cofactor
%Matrix cA(ij)=[(-1)^i+jAij]; Adjoint aA=(A^c)'
%The inverse A^-1=aA/det(A); det(A)~=0
A=[1 0 1; 2 1 -1; 0 1 2];
cA=[3 -4 2; 1 2 -1; -1 3 1];
aA=cA';
invA=aA/det(A);
invA=inv(A)

%Rotation matrices are special; 
%det(M)=+1 which comes from cos(theta)^2 + sin(theta)^1 = 1 
%and M'*M=I
%Geometric interpretations of determinants in 2D
%it's the area inside of the parallogram
A=[1 2; 3 4]; area=det(A);
figure;hold on;
set(gca, 'Fontsize',16);
line([0 1],[0 2], 'LineWidth',2);
line([0 3],[0 4], 'LineWidth',2);
line([3 4],[4 6], 'LineWidth',2);
line([1 4],[2 6], 'LineWidth',2);
line([0 4],[0 6], 'LineWidth',2);
plot(0,0,'ro','MarkerSize',6);
plot(1,2,'ro','MarkerSize',6);
plot(3,4,'ro','MarkerSize',6);
plot(4,6,'ro','MarkerSize',6);
box on
axis square






