# Chapter 6 Positive Definite Matrices¶

## Ex:6.1.1 Pg:313¶

In [1]:
print 'f(x,y)=x**2-10*x*y+y**2'
a=1
c=1
def f(x,y):
ff=x**2-10*x*y+y**2
return ff
print 'f(1,1)=',f(1,1)
print 'The conditions a>0 and c>0 ensure that f(x,y) is positive on the x and y axes. But this function is negative on the line x=y,because b=-10 overwhelms a and c. '

f(x,y)=x**2-10*x*y+y**2
f(1,1)= -8
The conditions a>0 and c>0 ensure that f(x,y) is positive on the x and y axes. But this function is negative on the line x=y,because b=-10 overwhelms a and c.


## Ex:6.1.3 Pg:315¶

In [2]:
print 'f(x,y)=2*x**2+4*x*y+y**2'
A=[[2, 2],[2, 1]]
a=1
c=1
b=2
print 'ac=',a*c
print 'b**2=',b**2

f(x,y)=2*x**2+4*x*y+y**2
ac= 1
b**2= 4


## Ex:6.1.4 Pg:315¶

In [3]:
print 'f(x,y)=2*x**2+4*x*y+y**2'
A=[[2 ,2],[2, 1]]
a=0
c=0
b=1
print 'ac=',a*c
print 'b**2=',b**2

f(x,y)=2*x**2+4*x*y+y**2
ac= 0
b**2= 1


## Ex:6.2.2 Pg:313¶

In [4]:
print 'f(x,y)=x**2+4*x*y+y**2'
a=1
c=1
def f(x,y):
ff=x**2+4*x*y+y**2
return ff
print 'f(0,0)=',f(0,0)
print 'Here 2b=4  it still does not ensure a minimum ,the sign of b is of no importance.Neither F nor f has a minimum at(0,0) because f(1,-1)=-1.'

f(x,y)=x**2+4*x*y+y**2
f(0,0)= 0
Here 2b=4  it still does not ensure a minimum ,the sign of b is of no importance.Neither F nor f has a minimum at(0,0) because f(1,-1)=-1.


## Ex:6.3.1 Pg:332¶

In [5]:
from scipy.linalg import svd
from numpy import array,transpose
A=transpose(array([[-1, 2, 2]]))
print 'A=',A
ans=svd(A)
U=ans[0]
diagnol=ans[1]
V=ans[2]
print 'U=',U
print 'diagnol=',diagnol
print "V'=",V
print "A=U*diagnol*V'=\n",U*diagnol*transpose(V)

A= [[-1]
[ 2]
[ 2]]
U= [[-0.33333333  0.66666667  0.66666667]
[ 0.66666667  0.66666667 -0.33333333]
[ 0.66666667 -0.33333333  0.66666667]]
diagnol= [ 3.]
V'= [[ 1.]]
A=U*diagnol*V'=
[[-1.  2.  2.]
[ 2.  2. -1.]
[ 2. -1.  2.]]


## Ex:6.3.2 Pg:332¶

In [6]:
from scipy.linalg import svd
from numpy import mat,array,transpose as tp, zeros
A=mat([[-1, 1, 0],[0, -1, 1]])
print 'A=\n',A
U,diagnl1,V=svd(A)
diagnl=zeros([2,3])
diagnl[0,0]=diagnl1[0]
diagnl[1,1]=diagnl1[1]
U=mat(U)
diagnl=mat(diagnl)
V=mat(V)
print 'U=\n',U
print 'Diagonal=\n',diagnl
print "V'=",tp(V)
print (U*diagnl)
print "A=U*diagonal*V'=\n",((U*diagnl)*tp(V))

A=
[[-1  1  0]
[ 0 -1  1]]
U=
[[-0.70710678  0.70710678]
[ 0.70710678  0.70710678]]
Diagonal=
[[ 1.73205081  0.          0.        ]
[ 0.          1.          0.        ]]
V'= [[  4.08248290e-01  -7.07106781e-01   5.77350269e-01]
[ -8.16496581e-01  -2.77555756e-16   5.77350269e-01]
[  4.08248290e-01   7.07106781e-01   5.77350269e-01]]
[[-1.22474487  0.70710678  0.        ]
[ 1.22474487  0.70710678  0.        ]]
A=U*diagonal*V'=
[[-1.07735027  0.8660254  -0.29885849]
[-0.07735027 -0.8660254   1.11535507]]


## Ex:6.3.3 Pg:332¶

In [7]:
from numpy import mat, transpose as tp
from scipy.linalg import svd
A=mat([[1, -2],[3, -1]])
print 'A=\n',A
U,S,V=svd(A)
Q=U*tp(mat(V))
S=V*S*tp(mat(V))
print 'Q=\n',Q
print 'S=\n',S
print 'A=SQ=\n',(Q*S)

A=
[[ 1 -2]
[ 3 -1]]
Q=
[[  1.11022302e-16  -1.00000000e+00]
[  1.00000000e+00   1.11022302e-16]]
S=
[[ 3. -1.]
[-1.  2.]]
A=SQ=
[[ 1. -2.]
[ 3. -1.]]


## Ex:6.3.4 Pg:332¶

In [8]:
from __future__ import division
from numpy import mat, transpose as tp
from scipy.linalg import svd

A=mat([[1, -2],[3, -1]])
print 'A=\n',A
U,diag1,V=svd(A)
Q=U*tp(mat(V))
S=mat([[2, 1],[1, 3]])
print 'Q=\n',Q
print 'S=\n',S
print "A=S'Q=\n",(S*tp(mat(Q)))

A=
[[ 1 -2]
[ 3 -1]]
Q=
[[  1.11022302e-16  -1.00000000e+00]
[  1.00000000e+00   1.11022302e-16]]
S=
[[2 1]
[1 3]]
A=S'Q=
[[-1.  2.]
[-3.  1.]]