In [1]:
x = ones(3)
Out[1]:
In [2]:
norm(x)
Out[2]:
In [3]:
A = eye(3)
Out[3]:
In [7]:
A = [1.0 2.0; 3.0 4.0]
Out[7]:
In [8]:
det(A)
Out[8]:
In [9]:
Ainv = inv(A)
Out[9]:
In [10]:
A* Ainv
Out[10]:
In [11]:
y = ones(2,1)
Out[11]:
In [13]:
x = Ainv * y
Out[13]:
In [14]:
x = A /y
In [15]:
evals, evecs = eig(A)
Out[15]:
In [18]:
#here we try and miplement gram schmidt orthogonalisation for fun and testing
#params: X - an nxk array with linearly idnependent columns
#return U: an nxk array with orthonormal columns
function gram_schmidt(X)
n,k = size(X)
#we create our array to put everything in
U = Array{Float64}(n,k)
I = eye(n)
#first col of u is just normalised first col of x
v1 = X[:,1]
U[:,1] = v1/norm(v1)
#okay, now we begin the proper loop
for i in 2:k
#set up
b = X[:i] #get the col we want
Z = X[:, 1:i-1] # te first i-1 columns of X - i.e. the ones we've already sorted
#we project onto orthogonal complement of the col span of Z
M = I - Z *inv(Z' *Z)*Z' # this is the standard forumal
u = M*b
#and normalize
U[:,i] = u/norm(u)
end
return U
end
#not THAT difficult of a function. and it doesn't need any crazy numpy stuff to go
# fast either, don't think. so that's very good indeed.
Out[18]:
In [19]:
#now some testing
y = [1 3 -3]'
X = [1 0; 0 -6; 2 2]
Py1 = X*inv(X'X)*X' *y
#gram schmidt
U = gram_schmidt(X)
In [20]:
Q, R = qr(X)
Out[20]:
In [ ]: