{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chapter3 Orthogonality"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex:3.1.1 Pg: 143"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x1=\n",
      "[[ 2]\n",
      " [ 2]\n",
      " [-1]]\n",
      "x2=\n",
      "[[-1]\n",
      " [ 2]\n",
      " [ 2]]\n",
      "x1'*x2=\n",
      "[[0]]\n",
      "Therefore,X1 is orthogonal to x2 .Both have length of sqrt(14).\n"
     ]
    }
   ],
   "source": [
    "from numpy import mat,transpose\n",
    "x1=mat([[2],[2],[-1]])\n",
    "print 'x1=\\n',x1\n",
    "x2=mat([[-1],[2],[2]])\n",
    "print 'x2=\\n',x2\n",
    "print \"x1'*x2=\\n\",(transpose(x1)*x2)\n",
    "print 'Therefore,X1 is orthogonal to x2 .Both have length of sqrt(14).'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex:3.1.3 Pg: 145"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A=\n",
      "[[1 3]\n",
      " [2 6]\n",
      " [3 9]]\n",
      "Null space=\n",
      "[[-0.9486833 ]\n",
      " [ 0.31622777]]\n",
      "A(1,:)*ns= [[ -2.22044605e-16]]\n",
      "A(2,:)*ns= [[ -4.44089210e-16]]\n",
      "A(3,:)*ns= [[ -4.44089210e-16]]\n",
      "This shows that the null space of A is orthogonal to the row space.\n"
     ]
    }
   ],
   "source": [
    "from numpy import mat,linalg,atleast_2d\n",
    "A=mat([[1, 3],[2, 6],[3, 9]])\n",
    "print 'A=\\n',A\n",
    "def nullspace(A, atol=1e-13, rtol=0):\n",
    "       \n",
    "       A = atleast_2d(A)\n",
    "       u, s, vh = linalg.svd(A)\n",
    "       tol = max(atol, rtol * s[0])\n",
    "       nnz = (s >= tol).sum()\n",
    "       ns = vh[nnz:].conj().T\n",
    "       return ns\n",
    "ns=nullspace(A)\n",
    "print 'Null space=\\n',ns\n",
    "print 'A(1,:)*ns=',(A[0]*ns)\n",
    "print 'A(2,:)*ns=',(A[1]*ns)\n",
    "print 'A(3,:)*ns=',(A[2]*ns)\n",
    "print 'This shows that the null space of A is orthogonal to the row space.'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "## Ex:3.2.1 Pg: 155"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b=\n",
      "[[1]\n",
      " [2]\n",
      " [3]]\n",
      "a=\n",
      "[[1]\n",
      " [1]\n",
      " [1]]\n",
      "Projection p of b onto the line through a is x***a=\n",
      "[[2]\n",
      " [2]\n",
      " [2]]\n",
      "cos(thetha) = 0.925820099773\n"
     ]
    }
   ],
   "source": [
    "from numpy import mat,transpose,sqrt,transpose\n",
    "b=mat([[1],[2],[3]])\n",
    "print 'b=\\n',b\n",
    "a=mat([[1],[1],[1]])\n",
    "print 'a=\\n',a\n",
    "x=(transpose(a)*b)/(transpose(a)*a)\n",
    "x=x[0,0]\n",
    "print 'Projection p of b onto the line through a is x***a=\\n',(x*a)\n",
    "cos_theta=(transpose(a)*b)[0,0]/(sqrt(transpose(a)*a)[0,0]*sqrt(transpose(b)*b)[0,0])\n",
    "print 'cos(thetha) =',cos_theta"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex:3.2.2 Pg: 156"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a=\n",
      "[[ 1.]\n",
      " [ 1.]\n",
      " [ 1.]]\n",
      "Matrix that projects onto a line through a=(1,1,1) is\n",
      "[[ 0.33333333  0.33333333  0.33333333]\n",
      " [ 0.33333333  0.33333333  0.33333333]\n",
      " [ 0.33333333  0.33333333  0.33333333]]\n"
     ]
    }
   ],
   "source": [
    "from numpy import mat,transpose\n",
    "a=mat([[1.],[1],[1]])\n",
    "print 'a=\\n',a\n",
    "P=(a*transpose(a))/(transpose(a)*a)[0,0]\n",
    "print 'Matrix that projects onto a line through a=(1,1,1) is\\n',P"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex:3.2.3 Pg: 156"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a=\n",
      "[[ 0.70710678]\n",
      " [ 0.70710678]]\n",
      "Projection of line onto the thetha-direction(thetha taken as 45) in the x-y plane passing through a is\n",
      "[[ 0.5  0.5]\n",
      " [ 0.5  0.5]]\n"
     ]
    }
   ],
   "source": [
    "from numpy import mat,transpose,sin,pi,cos\n",
    "thetha=45# #Taking some value for thetha\n",
    "a=mat([[cos(thetha*pi/180)],[sin(thetha*pi/180)]])\n",
    "print 'a=\\n',a\n",
    "P=(a*transpose(a))/(transpose(a)*a)\n",
    "print 'Projection of line onto the thetha-direction(thetha taken as 45) in the x-y plane passing through a is\\n',P"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex:3.3.1 Pg: 165"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A=\n",
      "[[ 0.09246118  0.04909557  0.71167391  0.77035248]\n",
      " [ 0.4079182   0.15730679  0.91259156  0.21064709]\n",
      " [ 0.02898159  0.87211114  0.11852139  0.69009963]\n",
      " [ 0.16889615  0.35412933  0.60067694  0.1180308 ]]\n",
      "P=A*inv(A*A)*A\n",
      "Projection of a  invertible 4x4 matrix on to the whole space is:\n",
      "[[  3.38771356e-02  -8.74775251e-02  -1.51348916e-02   1.06873528e+00]\n",
      " [ -8.74775251e-02   1.39664206e-03   1.08138671e+00   4.69417392e-03]\n",
      " [ -1.51348916e-02   1.08138671e+00  -1.24018336e-04  -6.61277992e-02]\n",
      " [  1.06873528e+00   4.69417392e-03  -6.61277992e-02  -7.30165590e-03]]\n",
      "Its identity matrix.\n"
     ]
    }
   ],
   "source": [
    "from numpy import mat,transpose,linalg,random\n",
    "A=random.rand(4,4)\n",
    "print 'A=\\n',A\n",
    "P=A*linalg.inv(transpose(A)*A)*transpose(A)\n",
    "print 'P=A*inv(A''*A)*A'\n",
    "print 'Projection of a  invertible 4x4 matrix on to the whole space is:\\n',P\n",
    "print 'Its identity matrix.'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex:3.3.2 Pg: 166"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b=C+Dt\n",
      "Ax=b\n",
      "A=\n",
      "[[ 1 -1]\n",
      " [ 1  1]\n",
      " [ 1  2]]\n",
      "b=\n",
      "[[1]\n",
      " [1]\n",
      " [3]]\n",
      "If Ax=b could be solved then they would be no errors, they cant be solved because the points are not on a line.Therefore they are solved by least squares.\n",
      "so,AAx**=Ab\n",
      "C** = 1.28571428571\n",
      "D**= 0.571428571429\n",
      "The best line is 9/7+4/7t\n"
     ]
    }
   ],
   "source": [
    "from numpy import mat,transpose,zeros,linalg\n",
    "print 'b=C+Dt'\n",
    "print 'Ax=b'\n",
    "A=mat([[1, -1],[1, 1],[1, 2]])\n",
    "print 'A=\\n',A\n",
    "b=mat([[1],[1],[3]])\n",
    "print 'b=\\n',b\n",
    "print 'If Ax=b could be solved then they would be no errors, they can''t be solved because the points are not on a line.Therefore they are solved by least squares.'\n",
    "print 'so,A''Ax**=A''b'\n",
    "x=zeros([1,2])\n",
    "x=linalg.solve((transpose(A)*A), (transpose(A)*b))\n",
    "print 'C** =',x[0,0]\n",
    "print 'D**=',x[1,0]\n",
    "print 'The best line is 9/7+4/7t'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex:3.4.1 Pg: 175"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Q=\n",
      "[[ 0.70710678 -0.70710678]\n",
      " [ 0.70710678  0.70710678]]\n",
      "\n",
      "Q'=inv(Q)=\n",
      "[[ 0.70710678  0.70710678]\n",
      " [-0.70710678  0.70710678]]\n",
      "\n",
      "Q rotates every vector through an angle thetha, and Q rotates it back through -thetha.The columns are clearly orthogonal and they are orthonormal because sin**2(theta)+cos**2(thetha)=1.\n"
     ]
    }
   ],
   "source": [
    "from numpy import mat,transpose,sin,pi,cos\n",
    "thetha=45##Taking some value for thetha.\n",
    "Q=mat([[cos(pi/180*thetha),-sin(thetha*pi/180)],[sin(pi/180*thetha),cos(pi/180*thetha)]])\n",
    "print 'Q=\\n',Q\n",
    "print \"\\nQ'=inv(Q)=\\n\",transpose(Q)\n",
    "print '\\nQ rotates every vector through an angle thetha, and Q'' rotates it back through -thetha.The columns are clearly orthogonal and they are orthonormal because sin**2(theta)+cos**2(thetha)=1.'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex:3.4.2 Pg: 175"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Any permutation matrix is an orthogonal matrix.The columns are certainly unit vectors and certainly orthogonal-because the 1 appears in a differnt place in each column\n",
      "P=\n",
      "[[0 1 0]\n",
      " [0 0 1]\n",
      " [1 0 0]]\n",
      "inv(P)=P'=\n",
      "[[0 0 1]\n",
      " [1 0 0]\n",
      " [0 1 0]]\n",
      "And,P'*P=\n",
      "[[1 0 0]\n",
      " [0 1 0]\n",
      " [0 0 1]]\n"
     ]
    }
   ],
   "source": [
    "from numpy import mat,transpose\n",
    "print 'Any permutation matrix is an orthogonal matrix.The columns are certainly unit vectors and certainly orthogonal-because the 1 appears in a differnt place in each column'\n",
    "P=mat([[0, 1, 0],[0, 0 ,1],[1, 0, 0]])\n",
    "print 'P=\\n',P\n",
    "print \"inv(P)=P'=\\n\",transpose(P)\n",
    "print \"And,P'*P=\\n\",(transpose(P)*P)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex:3.4.3 Pg: 175"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "If we project b=(x,y,z) onto the x-y plane then its projection is p=(x,y,0),and is the sum of projection onto x- any y-axes.\n",
      "q1=\n",
      "[[1]\n",
      " [0]\n",
      " [0]]\n",
      "q2=\n",
      "[[0]\n",
      " [1]\n",
      " [0]]\n",
      "Overall projection matrix,P=\n",
      "[[1 0 0]\n",
      " [0 1 0]\n",
      " [0 0 0]]\n",
      "and,P[x#y#z]=[x#y#0]\n",
      "Projection onto a plane=sum of projections onto orthonormal q1 and q2.\n"
     ]
    }
   ],
   "source": [
    "from numpy import mat,transpose,random\n",
    "print 'If we project b=(x,y,z) onto the x-y plane then its projection is p=(x,y,0),and is the sum of projection onto x- any y-axes.'\n",
    "b=random.rand(3,1)\n",
    "q1=mat([[1],[0],[0]])\n",
    "print 'q1=\\n',q1\n",
    "q2=mat([[0],[1],[0]])\n",
    "print 'q2=\\n',q2\n",
    "P=q1*transpose(q1)+q2*transpose(q2)\n",
    "print 'Overall projection matrix,P=\\n',P\n",
    "print 'and,P[x#y#z]=[x#y#0]'\n",
    "print 'Projection onto a plane=sum of projections onto orthonormal q1 and q2.'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex:3.4.4 Pg: 166"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "y=C+Dt\n",
      "Ax=b\n",
      "A=\n",
      "[[ 1 -3]\n",
      " [ 1  0]\n",
      " [ 1  3]]\n",
      "y=\n",
      "[[ 0.72089857]\n",
      " [ 0.89298883]\n",
      " [ 0.60457288]]\n",
      "the columns of A are orthogonal,so\n",
      "C** =\n",
      "[[ 0.12324779]]\n",
      "D** =\n",
      "[[-0.12014976  0.          0.12014976]\n",
      " [-0.14883147  0.          0.14883147]\n",
      " [-0.10076215  0.          0.10076215]]\n",
      "C** gives the besy fit ny horizontal line, whereas D**t is the best fit by a straight line through the origin.\n"
     ]
    }
   ],
   "source": [
    "from numpy import mat,transpose,random,zeros\n",
    "print 'y=C+Dt'\n",
    "print 'Ax=b'\n",
    "A=mat([[1, -3],[1, 0],[1, 3]])\n",
    "print 'A=\\n',A\n",
    "y=random.rand(3,1)\n",
    "print 'y=\\n',y\n",
    "print 'the columns of A are orthogonal,so'\n",
    "x=zeros([1,2])\n",
    "print \"C** =\\n\",( (mat([1, 1, 1])*y)/(transpose(A[:,1])*A[:,1]) )\n",
    "\n",
    "print \"D** =\\n\",( mat([-3, 0 ,3]*y)/(transpose(A[:,1])*A[:,1]) )\n",
    "print 'C** gives the besy fit ny horizontal line, whereas D**t is the best fit by a straight line through the origin.'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex:3.4.5 Pg: 166"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A=\n",
      "[[1 0 1]\n",
      " [1 0 0]\n",
      " [2 1 0]]\n",
      "Q=\n",
      "[[ 0.40824829  0.          0.91287093]\n",
      " [ 0.40824829  0.         -0.18257419]\n",
      " [ 0.81649658  1.         -0.36514837]]\n"
     ]
    }
   ],
   "source": [
    "from numpy import mat,transpose,zeros,shape,linalg\n",
    "A=mat([[1, 0 ,1],[1, 0, 0],[2, 1, 0]]) #independent vectors stored in columns of A\n",
    "print 'A=\\n',A\n",
    "m,n=shape(A)\n",
    "V=mat(zeros([n,n]))\n",
    "R=mat(zeros([n,n]))\n",
    "for k in range(0,n):\n",
    "    V[:,k]=A[:,k]\n",
    "    for j in range(0,k-1):\n",
    "        R[j,k]=transpose(V[:,j])*A[:,k]\n",
    "        V[:,k]=V[:,k]-R[j,k]*V[:,j]\n",
    "    \n",
    "    R[k,k]=linalg.norm(V[:,k])\n",
    "    V[:,k]=V[:,k]/R[k,k]\n",
    "\n",
    "print 'Q=\\n',V"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}