{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# 3x3 Givens Rotation"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "metadata": {
        "collapsed": false
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "array([ 0.73090072,  1.77355794,  0.80801157])"
            ]
          },
          "execution_count": 5,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "a = np.random.randn(3)\n",
        "a"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Let's zero out $a_2$:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 7,
      "metadata": {
        "collapsed": false
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "array([[ 0.38102265,  0.9245657 ,  0.        ],\n",
              "       [-0.9245657 ,  0.38102265,  0.        ],\n",
              "       [ 0.        ,  0.        ,  0.        ]])"
            ]
          },
          "execution_count": 7,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "G = np.zeros((3, 3))\n",
        "\n",
        "c = a[0]/np.sqrt(a[0]**2 + a[1]**2)\n",
        "s = a[1]/np.sqrt(a[0]**2 + a[1]**2)\n",
        "\n",
        "G[:2,:2] = np.array([\n",
        "        [c, s],\n",
        "        [-s, c]\n",
        "        ])\n",
        "G"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Anything wrong with $G$?"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "<!--\n",
        "G zeroes out the last component.\n",
        "-->\n",
        "(Edit this cell for solution.)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "How would we fix that issue?"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 11,
      "metadata": {
        "collapsed": true
      },
      "outputs": [],
      "source": [
        "G[2,2] = 1"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 10,
      "metadata": {
        "collapsed": false
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "array([ 1.91826057,  0.        ,  0.80801157])"
            ]
          },
          "execution_count": 10,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "G @ a"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Observations?"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.5.1+"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}