#!/usr/bin/env python
# coding: utf-8

# # Classical Mechanics, Mathematics and Numerical background
#
# ## Introduction
#
# Classical mechanics is a topic which has been taught intensively over
# several centuries. It is, with its many variants and ways of
# presenting the educational material, normally the first **real** physics
# course many of us meet and it lays the foundation for further physics
# studies. Many of the equations and ways of reasoning about the
# underlying laws of motion and pertinent forces, shape our approaches and understanding
# of the scientific method and discourse, as well as the way we develop our insights
# and deeper understanding about physical systems.
#
#
# There is a wealth of
# well-tested (from both a physics point of view and a pedagogical
# standpoint) exercises and problems which can be solved
# analytically. However, many of these problems represent idealized and
# less realistic situations.  The large majority of these problems are
# solved by paper and pencil and are traditionally aimed
# at what we normally refer to as continuous models from which we may find an analytical solution.  As a consequence,
# when teaching mechanics, it implies that we can seldomly venture beyond an idealized case
# in order to develop our understandings and insights about the
# underlying forces and laws of motion.
#
# We aim at changing this here by introducing throughout the course what
# we will call a **computational path**, where with computations we mean
# solving scientific problems with all possible tools and means, from
# plain paper an pencil exercises, via symbolic calculations to writing
# a code and running a program to solve a specific
# problem. Mathematically this normally means that we move from a
# continuous problem to a discretized one. This appproach enables us to
# solve a much broader class of problems.
# In mechanics this means, since we often rephrase the physical problems in terms of differential equations, that we can in most settings reuse the same program with some minimal changes.
#
#
# ## Reminder on vectors and other mathematical quantities
#
# Our studies will start with the motion of different types of objects
# such as a falling ball, a runner, a bicycle etc etc. It means that an
# object's position in space varies with time.
# In order to study such systems we need to define
#
# * choice of origin
#
# * choice of the direction of the axes
#
# * choice of positive direction (left-handed or right-handed system of reference)
#
# * choice of units and dimensions
#
# These choices lead to some important questions such as
#
# * is the  physics of a system independent of the origin of the axes?
#
# * is the  physics independent of the directions of the axes, that is are there privileged axes?
#
# * is the physics independent of the orientation of system?
#
# * is the physics independent of the scale of the length?
#
# Throughout this course we will use the standardized SI units. The standard unit for length is thus one meter 1m, for mass
# one kilogram 1kg, for time one second 1s, for force one Newton 1kgm/s$^2$ and for energy 1 Joule 1kgm$^2$s$^{-2}$.
#
# We will use the following notations for various variables (vectors are always boldfaced in these lecture notes):
# * position $\boldsymbol{r}$, in one dimention we will normally just use $x$,
#
# * mass $m$,
#
# * time $t$,
#
# * velocity $\boldsymbol{v}$ or just $v$ in one dimension,
#
# * acceleration $\boldsymbol{a}$ or just $a$ in one dimension,
#
# * momentum $\boldsymbol{p}$ or just $p$ in one dimension,
#
# * kinetic energy $K$,
#
# * potential energy $V$ and
#
# * frequency $\omega$.
#
# More variables will be defined as we need them.
#
#
# It is also important to keep track of dimensionalities. Don't mix this
# up with a chosen unit for a given variable. We mark the dimensionality
# in these lectures as $[a]$, where $a$ is the quantity we are
# interested in. Thus
#
# * $[\boldsymbol{r}]=$ length
#
# * $[m]=$ mass
#
# * $[K]=$ energy
#
# * $[t]=$ time
#
# * $[\boldsymbol{v}]=$ length over time
#
# * $[\boldsymbol{a}]=$ length over time squared
#
# * $[\boldsymbol{p}]=$ mass times length over time
#
# * $[\omega]=$ 1/time
#
# ## Scalars, Vectors and Matrices
#
# A scalar is something with a value that is independent of coordinate
# system. Examples are mass, or the relative time between events. A
# vector has magnitude and direction. Under rotation, the magnitude
# stays the same but the direction changes. Scalars have no spatial
# index, whereas a three-dimensional vector has 3 indices, e.g. the
# position $\boldsymbol{r}$ has components $r_1,r_2,r_3$, which are often
# referred to as $x,y,z$.
#
# There are several categories of changes of coordinate system. The
# observer can translate the origin, might move with a different
# velocity, or might rotate her/his coordinate axes. For instance, a
# particle's position vector changes when the origin is translated, but
# its velocity does not. When you study relativity you will find that
# quantities you thought of as scalars, such as time or an electric
# potential, are actually parts of four-dimensional vectors and that
# changes of the velocity of the reference frame act in a similar way to
# rotations.
#
# In addition to vectors and scalars, there are matrices, which have two
# indices. One also has objects with 3 or four indices. These are called
# tensors of rank $n$, where $n$ is the number of indices. A matrix is a
# rank-two tensor. The Levi-Civita symbol, $\epsilon_{ijk}$ used for
# cross products of vectors, is a tensor of rank three.
#
#
#
#
# In these lectures we will use boldfaced lower-case letters to label a
# vector. A vector $\boldsymbol{a}$ in three dimensions is thus defined as

# $$
# \boldsymbol{a} =(a_x,a_y, a_z),
# $$

# and using the unit vectors (see below) in a cartesian system we have

# $$
# \boldsymbol{a} = a_x\boldsymbol{e}_1+a_y\boldsymbol{e}_2+a_z\boldsymbol{e}_3,
# $$

# where the unit vectors have magnitude $\vert\boldsymbol{e}_i\vert = 1$ with
# $i=1=x$, $i=2=y$ and $i=3=z$. Some authors use letters
# $\boldsymbol{i}=\boldsymbol{e}_1$, $\boldsymbol{j}=\boldsymbol{e}_2$ and $\boldsymbol{k}=\boldsymbol{e}_3$.
#
#
#
# Alternatively, you may also encounter the above vector as

# $$
# \boldsymbol{a} = a_1\boldsymbol{e}_1+a_2\boldsymbol{e}_2+a_3\boldsymbol{e}_3.
# $$

# Here we have used that $a_1=a_x$, $a_2=a_y$ and $a_3=a_z$. Such a
# notation is sometimes more convenient if we wish to represent vector
# operations in a mathematically more compact way, see below here. We may also find this useful if  we want the different
# components to represent other coordinate systems that the Cartesian one. A typical example would be going from a Cartesian representation to a spherical basis. We will encounter such cases many times in this course.
#
# We use lower-case letters for vectors and upper-case letters for matrices. Vectors and matrices are always boldfaced.
#
#
#
# ### Polar Coordinates
#
# As an example, consider a two-dimensional Cartesian system with a vector $\boldsymbol{r}=(x,y)$.
# Our vector is then written as

# $$
# \boldsymbol{r} = x\boldsymbol{e}_1+y\boldsymbol{e}_2.
# $$

# Transforming to polar coordinates with the radius $\rho\in [0,\infty)$
# and the angle $\phi \in [0,2\pi]$ we have the familiar transformations

# $$
# x = \rho \cos{\phi} \hspace{0.5cm}   y = \rho \sin{\phi},
# $$

# and the inverse relations

# $$
# \rho =\sqrt{x^2+y^2} \hspace{0.5cm}   \phi = \mathrm{arctan}(\frac{y}{x}).
# $$

# We can rewrite the vector $\boldsymbol{a}$ in terms of $\rho$ and $\phi$ as

# $$
# \boldsymbol{a} = \rho \cos{\phi}\boldsymbol{e}_1+\rho \sin{\phi}\boldsymbol{e}_2,
# $$

# and we define the new unit vectors as $\boldsymbol{e}'_1=\cos{\phi}\boldsymbol{e}_1$ and $\boldsymbol{e}'_2=\sin{\phi}\boldsymbol{e}_2$, we have

# $$
# \boldsymbol{a}' = \rho\boldsymbol{e}'_1+\rho \boldsymbol{e}'_2.
# $$

# Below we will show that the norms of this vector in a Cartesian basis and a Polar basis are equal.
#
#
# ### Unit Vectors
#
# Also known as basis vectors, unit vectors point in the direction of
# the coordinate axes, have unit norm, and are orthogonal to one
# another. Sometimes this is referred to as an orthonormal basis,

#
#

#
# $$
# \begin{equation}
# \boldsymbol{e}_i\cdot\boldsymbol{e}_j=\delta_{ij}=\begin{bmatrix}
# 1 & 0 & 0\\
# 0& 1 & 0\\
# 0 & 0 & 1
# \end{bmatrix}.
# \label{_auto1} \tag{1}
# \end{equation}
# $$

# Here, $\delta_{ij}$ is unity when $i=j$ and is zero otherwise. This is
# called the unit matrix, because you can multiply it with any other
# matrix and not change the matrix. The **dot** denotes the dot product,
# $\boldsymbol{a}\cdot\boldsymbol{b}=a_1b_1+a_2b_2+a_3b_3=|a||b|\cos\theta_{ab}$. Sometimes
# the unit vectors are called $\hat{x}$, $\hat{y}$ and
# $\hat{z}$.
#
#
#
# Vectors can be decomposed in terms of unit vectors,

#
#

#
# $$
# \begin{equation}
# \boldsymbol{r}=r_1\hat{e}_1+r_2\hat{e}_2+r_3\hat{e}_3.
# \label{_auto2} \tag{2}
# \end{equation}
# $$

# The vector components $r_1$, $r_2$ and $r_3$ might be
# called $x$, $y$ and $z$ for a displacement. Another way to write this is to define the vector $\boldsymbol{r}=(x,y,z)$.
#
# Similarly, for the velocity we will use in this course the components  $\boldsymbol{v}=(v_x, v_y,v_z$. The accelaration is then given by $\boldsymbol{a}=(a_x,a_y,a_z)$.
#
#
#
# As mentioned above, repeated indices infer sums.
# This means that when you encounter an expression like the one on the left-hand side here, it stands actually for a sum (right-hand side)

# $$
# x_iy_i=\sum_i x_iy_i=\boldsymbol{x}\cdot\boldsymbol{y}.
# $$

# We will in our lectures seldom use this notation and rather spell out the summations.  This inferred summation over indices is normally called [Einstein summation convention](https://en.wikipedia.org/wiki/Einstein_notation).
#
#
# ## Vector Operations, Scalar Product (or dot product)
#
# For two vectors $\boldsymbol{a}$ and $\boldsymbol{b}$ we have

# $$
# \begin{eqnarray*}
# \boldsymbol{a}\cdot\boldsymbol{b}&=&\sum_ia_ib_i=|a||b|\cos\theta_{ab},\\
# |a|&\equiv& \sqrt{\boldsymbol{a}\cdot\boldsymbol{a}},
# \end{eqnarray*}
# $$

# or with a norm-2 notation

# $$
# |a|\equiv \vert\vert \boldsymbol{a}\vert\vert_2=\sqrt{\sum_i a_i^2}.
# $$

# Not of all of you are familiar with linear algebra. Numerically we will always deal with arrays and the dot product vector is given by the product of the transposed vector multiplied with the other vector, that is we have

# $$
# \boldsymbol{a}^T\boldsymbol{b}=\sum_i a_ib_i=|a||b|\cos\theta_{ab}.
# $$

# The superscript $T$ represents the transposition operations.
#
#
#
# ## Digression, Linear Algebra Notation for Vectors
#
# As an example, consider a three-dimensional velocity defined by a vector $\boldsymbol{v}=(v_x,v_y,v_z)$. For those of you familiar with linear algebra, we would write this quantity as

# $$
# \boldsymbol{v}=\begin{bmatrix} v_x\\ v_y \\ v_z \end{bmatrix},
# $$

# and the transpose as

# $$
# \boldsymbol{v}^T=\begin{bmatrix} v_x & v_y &v_z \end{bmatrix}.
# $$

# The norm is

# $$
# \boldsymbol{v}^T\boldsymbol{v}=v_x^2+v_y^2+v_z^2,
# $$

# as it should.
#
# Since we will use Python as a programming language throughout this course, the above vector, using the package **numpy** (see discussions below), can be written as

# In[1]:

import numpy as np
# Define the values of vx, vy and vz
vx = 0.0
vy = 1.0
vz = 0.0
v = np.array([vx, vy, vz])
print(v)
# The print the transpose of v
print(v.T)

# Try to figure out how to calculate the norm with **numpy**.
# We will come back to **numpy** in the examples below.
#
#
#
#
# As an example, consider our transformation of a two-dimensional Cartesian vector $\boldsymbol{r}$ to polar coordinates.
# We had

# $$
# \boldsymbol{r} = x\boldsymbol{e}_1+y\boldsymbol{e}_2.
# $$

# Transforming to polar coordinates with the radius $\rho\in [0,\infty)$
# and the angle $\phi \in [0,2\pi]$ we have

# $$
# x = \rho \cos{\phi} \hspace{0.5cm}   y = \rho \sin{\phi}.
# $$

# We can write this

# $$
# \boldsymbol{r} = \begin{bmatrix} x \\ y \end{bmatrix}= \begin{bmatrix} \rho \cos{\phi}  \\ \rho \sin{\phi}  \end{bmatrix}.
# $$

# The norm in Cartesian coordinates is $\boldsymbol{r}\cdot\boldsymbol{r}=x^2+y^2$ and
# using Polar coordinates we have
# $\rho^2(\cos{\phi})^2+\rho^2(\cos{\phi})^2=\rho^2$, which shows that
# the norm is conserved since we have $\rho = \sqrt{x^2+y^2}$. A
# transformation to a new basis should not change the norm.
#
#
#
# ## Vector Product (or cross product) of vectors $\boldsymbol{a}$ and $\boldsymbol{b}$

# $$
# \begin{eqnarray*}
# \boldsymbol{c}&=&\boldsymbol{a}\times\boldsymbol{b},\\
# c_i&=&\epsilon_{ijk}a_jb_k.
# \end{eqnarray*}
# $$

# Here $\epsilon$ is the third-rank anti-symmetric tensor, also known as
# the Levi-Civita symbol. It is $\pm 1$ only if all three indices are
# different, and is zero otherwise. The choice of $\pm 1$ depends on
# whether the indices are an even or odd permutation of the original
# symbols. The permutation $xyz$ or $123$ is considered to be $+1$. Its elements are

# $$
# \begin{eqnarray}
# \epsilon_{ijk}&=&-\epsilon_{ikj}=-\epsilon_{jik}=-\epsilon_{kji}\\
# \nonumber
# \epsilon_{123}&=&\epsilon_{231}=\epsilon_{312}=1,\\
# \nonumber
# \epsilon_{213}&=&\epsilon_{132}=\epsilon_{321}=-1,\\
# \nonumber
# \epsilon_{iij}&=&\epsilon_{iji}=\epsilon_{jii}=0.
# \end{eqnarray}
# $$

# You may have met cross-products when studying magnetic
# fields. Because the matrix is anti-symmetric, switching the $x$ and
# $y$ axes (or any two axes) flips the sign. If the coordinate system is
# right-handed, meaning the $xyz$ axes satisfy
# $\hat{x}\times\hat{y}=\hat{z}$, where you can point along the $x$ axis
# with your extended right index finger, the $y$ axis with your
# contracted middle finger and the $z$ axis with your extended
# thumb. Switching to a left-handed system flips the sign of the vector
# $\boldsymbol{c}=\boldsymbol{a}\times\boldsymbol{b}$.
#
# Note that
# $\boldsymbol{a}\times\boldsymbol{b}=-\boldsymbol{b}\times\boldsymbol{a}$. The vector $\boldsymbol{c}$ is
# perpendicular to both $\boldsymbol{a}$ and $\boldsymbol{b}$ and the magnitude of
# $\boldsymbol{c}$ is given by

# $$
# |c|=|a||b|\sin{\theta_{ab}}.
# $$

# ### Pseudo-vectors
#
# Vectors obtained by the cross product of two real vectors are called
# pseudo-vectors because the assignment of their direction can be
# arbitrarily flipped by defining the Levi-Civita symbol to be based on
# left-handed rules. Examples are the magnetic field and angular
# momentum. If the direction of a real vector prefers the right-handed
# over the left-handed direction, that constitutes a violation of
# parity. For instance, one can polarize the spins (angular momentum) of
# nuclei with a magnetic field so that the spins preferentially point
# along the direction of the magnetic field. This does not violate
# parity because both are pseudo-vectors. Now assume these polarized
# nuclei decay and that electrons are one of the products. If these
# electrons prefer to exit the decay parallel vs. antiparallel to the
# polarizing magnetic field, this constitutes parity violation because
# the direction of the outgoing electron momenta are a real vector. This
# is precisely what is observed in weak decays.
#
#
# ## Differentiation of a vector with respect to a scalar
#
# As an  example, consider the
# acceleration $\boldsymbol{a}$, which is given by the change in velocity per unit time, $\boldsymbol{a}=d\boldsymbol{v}/dt$
# with components

# $$
# a_i = (d\boldsymbol{v}/dt)_i=\frac{dv_i}{dt}.
# $$

# Here $i=x,y,z$ or $i=1,2,3$ if we are in three dimensions.
#
#
# ### Gradient operator $\nabla$
#
# This represents  the derivatives $\partial/\partial
# x$, $\partial/\partial y$ and $\partial/\partial z$. An often used shorthand is  $\partial_x=\partial/\partial_x$.
#
# The gradient of a scalar function of position and time
# $\Phi(x,y,z)=\Phi(\boldsymbol{r},t)$ is given by

# $$
# \boldsymbol{\nabla}~\Phi,
# $$

# with components $i$

# $$
# (\nabla\Phi(x,y,z,t))_i=\partial/\partial r_i\Phi(\boldsymbol{r},t)=\partial_i\Phi(\boldsymbol{r},t).
# $$

# Note that the gradient is a vector.
#
# Taking the dot product of the gradient with a vector, normally called the divergence,
# we have

# $$
# \mathrm{div} \boldsymbol{a}, \nabla\cdot\boldsymbol{a}=\sum_i \partial_i a_i.
# $$

# Note that the divergence is a scalar.
#
#
# ### The curl
#
# The **curl** of a vector is defined as
# $\nabla\times\boldsymbol{a}$,

# $$
# {\rm\bf curl}~\boldsymbol{a},
# $$

# with components

# $$
# (\boldsymbol{\nabla}\times\boldsymbol{a})_i=\epsilon_{ijk}\partial_j a_k(\boldsymbol{r},t).
# $$

# ### The Laplacian
#
# The Laplacian is referred to as $\nabla^2$ and is defined as

# $$
# \boldsymbol{\nabla}^2=\boldsymbol{\nabla}\cdot\boldsymbol{\nabla}=\frac{\partial^2}{\partial x^2}+\frac{\partial^2}{\partial y^2}+\frac{\partial^2}{\partial z^2}.
# $$

# Question: is the Laplacian a scalar or a vector?
#
# ### Some identities
#
# Here we simply state these, but you may wish to prove a few. They are useful for this class and will be essential when you study electromagnetism.

# $$
# \begin{eqnarray}
# \boldsymbol{a}\cdot(\boldsymbol{b}\times\boldsymbol{c})&=&\boldsymbol{b}\cdot(\boldsymbol{c}\times\boldsymbol{a})=\boldsymbol{c}\cdot(\boldsymbol{a}\times\boldsymbol{b})\\
# \nonumber
# \boldsymbol{a}\times(\boldsymbol{b}\times\boldsymbol{c})&=&(\boldsymbol{a}\cdot\boldsymbol{c})\boldsymbol{b}-(\boldsymbol{a}\cdot\boldsymbol{b})\boldsymbol{c}\\
# \nonumber
# (\boldsymbol{a}\times\boldsymbol{b})\cdot(\boldsymbol{c}\times\boldsymbol{d})&=&(\boldsymbol{a}\cdot\boldsymbol{c})(\boldsymbol{b}\cdot\boldsymbol{d})
# -(\boldsymbol{a}\cdot\boldsymbol{d})(\boldsymbol{b}\cdot\boldsymbol{c})
# \end{eqnarray}
# $$

# ### More useful relations
#
# Using the fact that multiplication of reals is distributive we can show that

# $$
# \boldsymbol{a}(\boldsymbol{b}+\boldsymbol{c})=\boldsymbol{a}\boldsymbol{b}+\boldsymbol{a}\boldsymbol{c},
# $$

# Similarly we can also show that (using product rule for differentiating reals)

# $$
# \frac{d}{dt}(\boldsymbol{a}\boldsymbol{b})=\boldsymbol{a}\frac{d\boldsymbol{b}}{dt}+\boldsymbol{b}\frac{d\boldsymbol{a}}{dt}.
# $$

# We can repeat these operations for the cross products and show that they are distribuitive

# $$
# \boldsymbol{a}\times(\boldsymbol{b}+\boldsymbol{c})=\boldsymbol{a}\times\boldsymbol{b}+\boldsymbol{a}\times\boldsymbol{c}.
# $$

# We have also that

# $$
# \frac{d}{dt}(\boldsymbol{a}\times\boldsymbol{b})=\boldsymbol{a}\times\frac{d\boldsymbol{b}}{dt}+\boldsymbol{b}\times\frac{d\boldsymbol{a}}{dt}.
# $$

# ### Gauss's Theorem
#
# For an integral over a volume $V$ confined by a surface $S$, Gauss's theorem gives

# $$
# \int_V dv~\nabla\cdot\boldsymbol{A}=\int_Sd\boldsymbol{S}\cdot\boldsymbol{A}.
# $$

# For a closed path $C$ which carves out some area $S$,

# $$
# \int_C d\boldsymbol{\ell}\cdot\boldsymbol{A}=\int_Sd\boldsymbol{s} \cdot(\nabla\times\boldsymbol{A})
# $$

# ### and Stokes's Theorem
#
# Stoke's law can be understood by considering a small rectangle,
# $-\Delta x<x<\Delta x$, $-\Delta y<y<\Delta y$. The path integral
# around the edges is

# $$
# \begin{eqnarray}
# \int_C d\boldsymbol{\ell}\cdot\boldsymbol{A}&=&2\Delta y[A_y(\Delta x,0)-A_y(-\Delta x,0)]-2\Delta x[A_x(0,\Delta y)-A_x(0,-\Delta y)]\\
# \nonumber
# &=&4\Delta x\Delta y\left\{
# \frac{A_y(\Delta x,0)-A_y(-\Delta x,0)}{2\Delta x}-\frac{A_x(0,\Delta y)-A_x(0,-\Delta y)}{2\Delta y}\right\}\\
# \nonumber
# &=&4\Delta x\Delta y\left\{\frac{\partial A_y}{\partial x}-\frac{\partial A_x}{\partial y}\right\}\\
# &=&\Delta S \cdot \nabla\times\boldsymbol{A}.
# \end{eqnarray}
# $$

# Here $\Delta S$ is the area of the surface element.
#
#
#
#
# ## Basic Matrix Features
#
# **Note**: The material on matrices is optional and will not be used much
# (except for illustrations at the very end on garmonic oscillations)
# since most of you have not yet taken a course on linear algebra. The
# material is however included here for the sake of completeness.

# $$
# \mathbf{A} =
#       \begin{bmatrix} a_{11} & a_{12} & a_{13} & a_{14} \\
#                                  a_{21} & a_{22} & a_{23} & a_{24} \\
#                                    a_{31} & a_{32} & a_{33} & a_{34} \\
#                                   a_{41} & a_{42} & a_{43} & a_{44}
#              \end{bmatrix}\qquad
# \mathbf{I} =
#       \begin{bmatrix} 1 & 0 & 0 & 0 \\
#                                  0 & 1 & 0 & 0 \\
#                                  0 & 0 & 1 & 0 \\
#                                  0 & 0 & 0 & 1
#              \end{bmatrix}
# $$

# The inverse of a matrix is defined by

# $$
# \mathbf{A}^{-1} \cdot \mathbf{A} = I
# $$

#

#

#
              Relations               	       Name     	                             matrix elements

#

#

#
   $A = A^{T}$                               	    symmetric          	    $a_{ij} = a_{ji}$

#
   $A = \left (A^{T} \right )^{-1}$          	    real orthogonal    	    $\sum_k a_{ik} a_{jk} = \sum_k a_{ki} a_{kj} = \delta_{ij}$

#
   $A = A^{ * }$                             	    real matrix        	    $a_{ij} = a_{ij}^{ * }$

#
   $A = A^{\dagger}$                         	    hermitian          	    $a_{ij} = a_{ji}^{ * }$

#
   $A = \left (A^{\dagger} \right )^{-1}$    	    unitary            	    $\sum_k a_{ik} a_{jk}^{ * } = \sum_k a_{ki}^{ * } a_{kj} = \delta_{ij}$

#

#

#
#
# ### Some famous Matrices
#
#   * Diagonal if $a_{ij}=0$ for $i\ne j$
#
#   * Upper triangular if $a_{ij}=0$ for $i > j$
#
#   * Lower triangular if $a_{ij}=0$ for $i < j$
#
#   * Upper Hessenberg if $a_{ij}=0$ for $i > j+1$
#
#   * Lower Hessenberg if $a_{ij}=0$ for $i < j+1$
#
#   * Tridiagonal if $a_{ij}=0$ for $|i -j| > 1$
#
#   * Lower banded with bandwidth $p$: $a_{ij}=0$ for $i > j+p$
#
#   * Upper banded with bandwidth $p$: $a_{ij}=0$ for $i < j+p$
#
#   * Banded, block upper triangular, block lower triangular....
#
# For an $N\times N$ matrix  $\mathbf{A}$ the following properties are all equivalent
#
#   * If the inverse of $\mathbf{A}$ exists, $\mathbf{A}$ is nonsingular.
#
#   * The equation $\mathbf{Ax}=0$ implies $\mathbf{x}=0$.
#
#   * The rows of $\mathbf{A}$ form a basis of $R^N$.
#
#   * The columns of $\mathbf{A}$ form a basis of $R^N$.
#
#   * $\mathbf{A}$ is a product of elementary matrices.
#
#   * $0$ is not eigenvalue of $\mathbf{A}$.
#
# ## Rotations
#
#
# Here, we use rotations as an example of matrices and their operations. One can consider a different orthonormal basis $\hat{e}'_1$, $\hat{e}'_2$ and $\hat{e}'_3$. The same vector $\boldsymbol{r}$ mentioned above can also be expressed in the new basis,

#
#

#
# $$
# \begin{equation}
# \boldsymbol{r}=r'_1\hat{e}'_1+r'_2\hat{e}'_2+r'_3\hat{e}'_3.
# \label{_auto3} \tag{3}
# \end{equation}
# $$

# Even though it is the same vector, the components have changed. Each
# new unit vector $\hat{e}'_i$ can be expressed as a linear sum of the
# previous vectors,

#
#

#
# $$
# \begin{equation}
# \hat{e}'_i=\sum_j U_{ij}\hat{e}_j,
# \label{_auto4} \tag{4}
# \end{equation}
# $$

# and the matrix $U$ can be found by taking the dot product of both sides with $\hat{e}_k$,

#
#

#
# $$
# \begin{eqnarray}
# \nonumber
# \hat{e}_k\cdot\hat{e}'_i&=&\sum_jU_{ij}\hat{e}_k\cdot\hat{e}_j\\
# \label{eq:lambda_angles} \tag{5}
# \hat{e}_k\cdot\hat{e}'_i&=&\sum_jU_{ij}\delta_{jk}=U_{ik}.
# \end{eqnarray}
# $$

# Thus, the matrix lambda has components $U_{ij}$ that are equal to the
# cosine of the angle between new unit vector $\hat{e}'_i$ and the old
# unit vector $\hat{e}_j$.

#
#

#
# $$
# \begin{equation}
# U = \begin{bmatrix}
# \hat{e}'_1\cdot\hat{e}_1& \hat{e}'_1\cdot\hat{e}_2& \hat{e}'_1\cdot\hat{e}_3\\
# \hat{e}'_2\cdot\hat{e}_1& \hat{e}'_2\cdot\hat{e}_2& \hat{e}'_2\cdot\hat{e}_3\\
# \hat{e}'_3\cdot\hat{e}_1& \hat{e}'_3\cdot\hat{e}_2& \hat{e}'_3\cdot\hat{e}_3
# \end{bmatrix},~~~~~U_{ij}=\hat{e}'_i\cdot\hat{e}_j=\cos\theta_{ij}.
# \label{_auto5} \tag{6}
# \end{equation}
# $$

# Note that the matrix is not symmetric, $U_{ij}\ne U_{ji}$. One can also look at the inverse transformation, by switching the primed and unprimed coordinates,

#
#

#
# $$
# \begin{eqnarray}
# \label{eq:inverseU} \tag{7}
# \hat{e}_i&=&\sum_jU^{-1}_{ij}\hat{e}'_j,\\
# \nonumber
# U^{-1}_{ij}&=&\hat{e}_i\cdot\hat{e}'_j=U_{ji}.
# \end{eqnarray}
# $$

# The definition of transpose of a matrix, $M^{t}_{ij}=M_{ji}$, allows one to state this as

#
#

#
# $$
# \begin{eqnarray}
# \label{eq:transposedef} \tag{8}
# U^{-1}&=&U^{t}.
# \end{eqnarray}
# $$

# ### Tensors
#
# A tensor obeying Eq. ([8](#eq:transposedef)) defines what is known as
# a unitary, or orthogonal, transformation.
#
# The matrix $U$ can be used to transform any vector to the new basis. Consider a vector

# $$
# \begin{eqnarray}
# \boldsymbol{r}&=&r_1\hat{e}_1+r_2\hat{e}_2+r_3\hat{e}_3\\
# \nonumber
# &=&r'_1\hat{e}'_1+r'_2\hat{e}'_2+r'_3\hat{e}'_3.
# \end{eqnarray}
# $$

# This is the same vector expressed as a sum over two different sets of
# basis vectors. The coefficients $r_i$ and $r'_i$ represent components
# of the same vector. The relation between them can be found by taking
# the dot product of each side with one of the unit vectors,
# $\hat{e}_i$, which gives

# $$
# \begin{eqnarray}
# r_i&=&\sum_j \hat{e}_i\cdot\hat{e}'_j~r'_j.
# \end{eqnarray}
# $$

# Using Eq. ([7](#eq:inverseU)) one can see that the transformation of $r$ can be also written in terms of $U$,

#
#

#
# $$
# \begin{eqnarray}
# \label{eq:rotateR} \tag{9}
# r_i&=&\sum_jU^{-1}_{ij}~r'_j.
# \end{eqnarray}
# $$

# Thus, the matrix that transforms the coordinates of the unit vectors,
# Eq. ([7](#eq:inverseU)) is the same one that transforms the
# coordinates of a vector, Eq. ([9](#eq:rotateR)).
#
#
#
# As a small exercise, find the rotation matrix $U$ for finding the
# components in the primed coordinate system given from those in the
# unprimed system, given that the unit vectors in the new system are
# found by rotating the coordinate system by and angle $\phi$ about the
# $z$ axis.
#
# In this case

# $$
# \begin{eqnarray*}
# \hat{e}'_1&=&\cos\phi \hat{e}_1-\sin\phi\hat{e}_2,\\
# \hat{e}'_2&=&\sin\phi\hat{e}_1+\cos\phi\hat{e}_2,\\
# \hat{e}'_3&=&\hat{e}_3.
# \end{eqnarray*}
# $$

# By inspecting Eq. ([5](#eq:lambda_angles)), we get

# $$
# U=\left(\begin{array}{ccc}
# \cos\phi&-\sin\phi&0\\
# \sin\phi&\cos\phi&0\\
# 0&0&1\end{array}\right).
# $$

# Under a unitary transformation $U$ (or basis transformation) scalars
# are unchanged, whereas vectors $\boldsymbol{r}$ and matrices $M$ change as

# $$
# \begin{eqnarray}
# r'_i&=&U_{ij}~ r_j, ~~({\rm sum~inferred})\\
# \nonumber
# M'_{ij}&=&U_{ik}M_{km}U^{-1}_{mj}.
# \end{eqnarray}
# $$

# Physical quantities with no spatial indices are scalars (or
# pseudoscalars if they depend on right-handed vs. left-handed
# coordinate systems), and are unchanged by unitary
# transformations. This includes quantities like the trace of a matrix,
# the matrix itself had indices but none remain after performing the
# trace.

# $$
# \begin{eqnarray}
# {\rm Tr} M&\equiv& M_{ii}.
# \end{eqnarray}
# $$

# Because there are no remaining indices, one expects it to be a scalar. Indeed one can see this,

# $$
# \begin{eqnarray}
# {\rm Tr} M'&=&U_{ij}M_{jm}U^{-1}_{mi}\\
# \nonumber
# &=&M_{jm}U^{-1}_{mi}U_{ij}\\
# \nonumber
# &=&M_{jm}\delta_{mj}\\
# \nonumber
# &=&M_{jj}={\rm Tr} M.
# \end{eqnarray}
# $$

# A similar example is the determinant of a matrix, which is also a scalar.
#
#
#
#
# ## Numerical Elements
#
# Numerical algorithms call for approximate discrete models and much of
# the development of methods for continuous models are nowadays being
# replaced by methods for discrete models in science and industry,
# simply because **much larger classes of problems can be addressed** with
# discrete models, often by simpler and more generic methodologies.
#
# As we will see throughout this course, when properly scaling the equations at hand,
# discrete models open up for more advanced abstractions and the possibility to
# study  real life systems, with the added bonus that we can explore and
# deepen our basic understanding of various physical systems
#
# Analytical solutions are as important as before. In addition, such
# solutions provide us with invaluable benchmarks and tests for our
# discrete models. Such benchmarks, as we will see below, allow us
# to discuss possible sources of errors and their behaviors.  And
# finally, since most of our models are based on various algorithms from
# numerical mathematics, we have a unique oppotunity to gain a deeper
# understanding of the mathematical approaches we are using.
#
#
#
# With computing and data science as important elements in essentially
# all aspects of a modern society, we could  then try to define Computing as
# **solving scientific problems using all possible tools, including
# symbolic computing, computers and numerical algorithms, and analytical
# paper and pencil solutions**.
# Computing provides us with the tools to develope our own understanding of the scientific method by enhancing algorithmic thinking.
#
#
#
# The way we will teach this course reflects this definition of
# computing. The course contains both classical paper and pencil
# exercises as well as computational projects and exercises. The hope is
# that this will allow you to explore the physics of systems governed by
# the degrees of freedom of classical mechanics at a deeper level, and
# that these insights about the scientific method will help you to
# develop a better understanding of how the underlying forces and
# equations of motion and how they impact a given system.
#
# Furthermore,
# by introducing various numerical methods via computational projects
# and exercises, we aim at developing your competences and skills about
# these topics.
#
#
#
#
# These competences will enable you to
#
# * understand how algorithms are used to solve mathematical problems,
#
# * derive, verify, and implement algorithms,
#
# * understand what can go wrong with algorithms,
#
# * use these algorithms to construct reproducible scientific outcomes and to engage in science in ethical ways, and
#
# * think algorithmically for the purposes of gaining deeper insights about scientific problems.
#
# All these elements are central for maturing and gaining a better understanding of the modern scientific process *per se*.
#
# The power of the scientific method lies in identifying a given problem
# as a special case of an abstract class of problems, identifying
# general solution methods for this class of problems, and applying a
# general method to the specific problem (applying means, in the case of
# computing, calculations by pen and paper, symbolic computing, or
# numerical computing by ready-made and/or self-written software). This
# generic view on problems and methods is particularly important for
# understanding how to apply available, generic software to solve a
# particular problem.
#
# *However, verification of algorithms and understanding their limitations requires much of the classical knowledge about continuous models.*
#
#
#
# ## A well-known example to illustrate many of the above concepts
#
# Before we venture into a reminder on Python and mechanics relevant applications, let us briefly outline some of the
# abovementioned topics using an example many of you may have seen before in for example CMSE201.
# A simple algorithm for integration is the Trapezoidal rule.
# Integration of a function $f(x)$ by the Trapezoidal Rule is given by following algorithm for an interval $x \in [a,b]$

# $$
# \int_a^b(f(x) dx = \frac{1}{2}\left [f(a)+2f(a+h)+\dots+2f(b-h)+f(b)\right] +O(h^2),
# $$

# where $h$ is the so-called stepsize defined by the number of integration points $N$ as $h=(b-a)/(n)$.
# Python offers an  extremely versatile programming  environment, allowing for
# the inclusion of analytical studies in a numerical program. Here we show an
# example code with the **trapezoidal rule**. We use also **SymPy** to evaluate the exact value of the integral and compute the absolute error
# with respect to the numerically evaluated one of the integral
# $\int_0^1 dx x^2 = 1/3$.
# The following code for  the trapezoidal rule allows you  to plot the relative error by comparing with the exact result. By increasing to $10^8$ points one arrives at a region where numerical errors start to accumulate.

# In[2]:

get_ipython().run_line_magic('matplotlib', 'inline')

from math import log10
import numpy as np
from sympy import Symbol, integrate
import matplotlib.pyplot as plt
# function for the trapezoidal rule
def Trapez(a,b,f,n):
   h = (b-a)/float(n)
   s = 0
   x = a
   for i in range(1,n,1):
       x = x+h
       s = s+ f(x)
   s = 0.5*(f(a)+f(b)) +s
   return h*s
#  function to compute pi
def function(x):
    return x*x
# define integration limits
a = 0.0;  b = 1.0;
# find result from sympy
# define x as a symbol to be used by sympy
x = Symbol('x')
exact = integrate(function(x), (x, a, b))
# set up the arrays for plotting the relative error
n = np.zeros(9); y = np.zeros(9);
# find the relative error as function of integration points
for i in range(1, 8, 1):
    npts = 10**i
    result = Trapez(a,b,function,npts)
    RelativeError = abs((exact-result)/exact)
    n[i] = log10(npts); y[i] = log10(RelativeError);
plt.plot(n,y, 'ro')
plt.xlabel('n')
plt.ylabel('Relative error')
plt.show()

# ### Analyzing the above example
#
# This example shows the potential of combining numerical algorithms
# with symbolic calculations, allowing us to
#
# * Validate and verify  their  algorithms.
#
# * Including concepts like unit testing, one has the possibility to test and test several or all parts of the code.
#
# * Validation and verification are then included *naturally* and one can develop a better attitude to what is meant with an ethically sound scientific approach.
#
# * The above example allows the student to also test the mathematical error of the algorithm for the trapezoidal rule by changing the number of integration points. The students get **trained from day one to think error analysis**.
#
# * With a Jupyter notebook you can keep exploring similar examples and turn them in as your own notebooks.
#
# ## Python practicalities, Software and needed installations
#
# We will make extensive use of Python as programming language and its
# myriad of available libraries.  You will find
# Jupyter notebooks invaluable in your work.
#
# If you have Python installed (we strongly recommend Python3) and you feel
# pretty familiar with installing different packages, we recommend that
# you install the following Python packages via **pip** as
#
# 1. pip install numpy scipy matplotlib ipython scikit-learn mglearn sympy pandas pillow
#
# For Python3, replace **pip** with **pip3**.
#
# For OSX users we recommend, after having installed Xcode, to
# install **brew**. Brew allows for a seamless installation of additional
# software via for example
#
# 1. brew install python3
#
# For Linux users, with its variety of distributions like for example the widely popular Ubuntu distribution,
# you can use **pip** as well and simply install Python as
#
# 1. sudo apt-get install python3  (or python for pyhton2.7)
#
# etc etc.
#
#
#
# ### Python installers
#
# If you don't want to perform these operations separately and venture
# into the hassle of exploring how to set up dependencies and paths, we
# recommend two widely used distrubutions which set up all relevant
# dependencies for Python, namely
#
# * [Anaconda](https://docs.anaconda.com/),
#
# which is an open source
# distribution of the Python and R programming languages for large-scale
# data processing, predictive analytics, and scientific computing, that
# aims to simplify package management and deployment. Package versions
# are managed by the package management system **conda**.
#
# * [Enthought canopy](https://www.enthought.com/product/canopy/)
#
# is a Python
# distribution for scientific and analytic computing distribution and
# analysis environment, available for free and under a commercial
# license.
#
# Furthermore, [Google's Colab](https://colab.research.google.com/notebooks/welcome.ipynb) is a free Jupyter notebook environment that requires
# no setup and runs entirely in the cloud. Try it out!
#
#
# ## Useful Python libraries
#
# Here we list several useful Python libraries we strongly recommend (if you use anaconda many of these are already there)
#
# * [NumPy](https://www.numpy.org/) is a highly popular library for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays
#
# * [The pandas](https://pandas.pydata.org/) library provides high-performance, easy-to-use data structures and data analysis tools
#
# * [Xarray](http://xarray.pydata.org/en/stable/) is a Python package that makes working with labelled multi-dimensional arrays simple, efficient, and fun!
#
# * [Scipy](https://www.scipy.org/) (pronounced “Sigh Pie”) is a Python-based ecosystem of open-source software for mathematics, science, and engineering.
#
# * [Matplotlib](https://matplotlib.org/) is a Python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms.
#
# * [Autograd](https://github.com/HIPS/autograd) can automatically differentiate native Python and Numpy code. It can handle a large subset of Python's features, including loops, ifs, recursion and closures, and it can even take derivatives of derivatives of derivatives
#
# * [SymPy](https://www.sympy.org/en/index.html) is a Python library for symbolic mathematics.
#
# * [scikit-learn](https://scikit-learn.org/stable/) has simple and efficient tools for machine learning, data mining and data analysis
#
# * [TensorFlow](https://www.tensorflow.org/) is a Python library for fast numerical computing created and released by Google
#
# * [Keras](https://keras.io/) is a high-level neural networks API, written in Python and capable of running on top of TensorFlow, CNTK, or Theano
#
# * And many more such as [pytorch](https://pytorch.org/),  [Theano](https://pypi.org/project/Theano/) etc
#
# Your jupyter notebook can easily be
# converted into a nicely rendered **PDF** file or a Latex file for
# further processing. For example, convert to latex as

#         pycod jupyter nbconvert filename.ipynb --to latex
#

# And to add more versatility, the Python package [SymPy](http://www.sympy.org/en/index.html) is a Python library for symbolic mathematics. It aims to become a full-featured computer algebra system (CAS)  and is entirely written in Python.
#
#
#
# ## Numpy examples and Important Matrix and vector handling packages
#
# There are several central software libraries for linear algebra and eigenvalue problems. Several of the more
# popular ones have been wrapped into ofter software packages like those from the widely used text **Numerical Recipes**. The original source codes in many of the available packages are often taken from the widely used
# software package LAPACK, which follows two other popular packages
# developed in the 1970s, namely EISPACK and LINPACK.  We describe them shortly here.
#
#   * LINPACK: package for linear equations and least square problems.
#
#   * LAPACK:package for solving symmetric, unsymmetric and generalized eigenvalue problems. From LAPACK's website <http://www.netlib.org> it is possible to download for free all source codes from this library. Both C/C++ and Fortran versions are available.
#
#   * BLAS (I, II and III): (Basic Linear Algebra Subprograms) are routines that provide standard building blocks for performing basic vector and matrix operations. Blas I is vector operations, II vector-matrix operations and III matrix-matrix operations. Highly parallelized and efficient codes, all available for download from <http://www.netlib.org>.
#
# [Numpy](http://www.numpy.org/) provides an easy way to handle arrays in Python. The standard way to import this library is as

# In[ ]:

import numpy as np

# Here follows a simple example where we set up an array of ten elements, all determined by random numbers drawn according to the normal distribution,

# In[ ]:

n = 10
x = np.random.normal(size=n)
print(x)

# We defined a vector $x$ with $n=10$ elements with its values given by the Normal distribution $N(0,1)$.
# Another alternative is to declare a vector as follows

# In[ ]:

import numpy as np
x = np.array([1, 2, 3])
print(x)

# Here we have defined a vector with three elements, with $x_0=1$, $x_1=2$ and $x_2=3$. Note that both Python and C++
# start numbering array elements from $0$ and on. This means that a vector with $n$ elements has a sequence of entities $x_0, x_1, x_2, \dots, x_{n-1}$. We could also let (recommended) Numpy to compute the logarithms of a specific array as

# In[ ]:

import numpy as np
x = np.log(np.array([4, 7, 8]))
print(x)

# In the last example we used Numpy's unary function $np.log$. This function is
# highly tuned to compute array elements since the code is vectorized
# and does not require looping. We normaly recommend that you use the
# Numpy intrinsic functions instead of the corresponding **log** function
# from Python's **math** module. The looping is done explicitely by the
# **np.log** function. The alternative, and slower way to compute the
# logarithms of a vector would be to write

# In[ ]:

import numpy as np
from math import log
x = np.array([4, 7, 8])
for i in range(0, len(x)):
    x[i] = log(x[i])
print(x)

# We note that our code is much longer already and we need to import the **log** function from the **math** module.
# The attentive reader will also notice that the output is $[1, 1, 2]$. Python interprets automagically our numbers as integers (like the **automatic** keyword in C++). To change this we could define our array elements to be double precision numbers as

# In[ ]:

import numpy as np
x = np.log(np.array([4, 7, 8], dtype = np.float64))
print(x)

# or simply write them as double precision numbers (Python uses 64 bits as default for floating point type variables), that is

# In[ ]:

import numpy as np
x = np.log(np.array([4.0, 7.0, 8.0])
print(x)

# To check the number of bytes (remember that one byte contains eight bits for double precision variables), you can use simple use the **itemsize** functionality (the array $x$ is actually an object which inherits the functionalities defined in Numpy) as

# In[ ]:

import numpy as np
x = np.log(np.array([4.0, 7.0, 8.0])
print(x.itemsize)

# ### Matrices in Python
#
# Having defined vectors, we are now ready to try out matrices. We can
# define a $3 \times 3 $ real matrix $\hat{A}$ as (recall that we user
# lowercase letters for vectors and uppercase letters for matrices)

# In[ ]:

import numpy as np
A = np.log(np.array([ [4.0, 7.0, 8.0], [3.0, 10.0, 11.0], [4.0, 5.0, 7.0] ]))
print(A)

# If we use the **shape** function we would get $(3, 3)$ as output, that is verifying that our matrix is a $3\times 3$ matrix. We can slice the matrix and print for example the first column (Python organized matrix elements in a row-major order, see below) as

# In[ ]:

import numpy as np
A = np.log(np.array([ [4.0, 7.0, 8.0], [3.0, 10.0, 11.0], [4.0, 5.0, 7.0] ]))
# print the first column, row-major order and elements start with 0
print(A[:,0])

# We can continue this was by printing out other columns or rows. The example here prints out the second column

# In[ ]:

import numpy as np
A = np.log(np.array([ [4.0, 7.0, 8.0], [3.0, 10.0, 11.0], [4.0, 5.0, 7.0] ]))
# print the first column, row-major order and elements start with 0
print(A[1,:])

# Numpy contains many other functionalities that allow us to slice, subdivide etc etc arrays. We strongly recommend that you look up the [Numpy website for more details](http://www.numpy.org/). Useful functions when defining a matrix are the **np.zeros** function which declares a matrix of a given dimension and sets all elements to zero

# In[ ]:

import numpy as np
n = 10
# define a matrix of dimension 10 x 10 and set all elements to zero
A = np.zeros( (n, n) )
print(A)

# or initializing all elements to

# In[ ]:

import numpy as np
n = 10
# define a matrix of dimension 10 x 10 and set all elements to one
A = np.ones( (n, n) )
print(A)

# or as unitarily distributed random numbers (see the material on random number generators in the statistics part)

# In[ ]:

import numpy as np
n = 10
# define a matrix of dimension 10 x 10 and set all elements to random numbers with x \in [0, 1]
A = np.random.rand(n, n)
print(A)

# ### Meet the Pandas
#
# Another useful Python package is
# [pandas](https://pandas.pydata.org/), which is an open source library
# providing high-performance, easy-to-use data structures and data
# analysis tools for Python. **pandas** stands for panel data, a term borrowed from econometrics and is an efficient library for data analysis with an emphasis on tabular data.
#
# **pandas** has two major classes, the **DataFrame** class with
# two-dimensional data objects and tabular data organized in columns and
# the class **Series** with a focus on one-dimensional data objects. Both
# classes allow you to index data easily as we will see in the examples
# below.  **pandas** allows you also to perform mathematical operations on
# the data, spanning from simple reshapings of vectors and matrices to
# statistical operations.
#
# The following simple example shows how we can, in an easy way make
# tables of our data. Here we define a data set which includes names,
# place of birth and date of birth, and displays the data in an easy to
# read way. We will see repeated use of **pandas**, in particular in
# connection with classification of data.

# In[ ]:

import pandas as pd
from IPython.display import display
data = {'First Name': ["Frodo", "Bilbo", "Aragorn II", "Samwise"],
        'Last Name': ["Baggins", "Baggins","Elessar","Gamgee"],
        'Place of birth': ["Shire", "Shire", "Eriador", "Shire"],
        'Date of Birth T.A.': [2968, 2890, 2931, 2980]
        }
data_pandas = pd.DataFrame(data)
display(data_pandas)

# In the above we have imported **pandas** with the shorthand **pd**, the latter has become the standard way we import **pandas**. We make then a list of various variables
# and reorganize the above lists into a **DataFrame** and then print out  a neat table with specific column labels as *Name*, *place of birth* and *date of birth*.
# Displaying these results, we see that the indices are given by the default numbers from zero to three.
# **pandas** is extremely flexible and we can easily change the above indices by defining a new type of indexing as

# In[ ]:

data_pandas = pd.DataFrame(data,index=['Frodo','Bilbo','Aragorn','Sam'])
display(data_pandas)

# Thereafter we display the content of the row which begins with the index **Aragorn**

# In[ ]:

display(data_pandas.loc['Aragorn'])

# We can easily append data to this, for example

# In[ ]:

new_hobbit = {'First Name': ["Peregrin"],
              'Last Name': ["Took"],
              'Place of birth': ["Shire"],
              'Date of Birth T.A.': [2990]
              }
data_pandas=data_pandas.append(pd.DataFrame(new_hobbit, index=['Pippin']))
display(data_pandas)

# Here are other examples where we use the **DataFrame** functionality to handle arrays, now with more interesting features for us, namely numbers. We set up a matrix
# of dimensionality $10\times 5$ and compute the mean value and standard deviation of each column. Similarly, we can perform mathematial operations like squaring the matrix elements and many other operations.

# In[ ]:

import numpy as np
import pandas as pd
from IPython.display import display
np.random.seed(100)
# setting up a 10 x 5 matrix
rows = 10
cols = 5
a = np.random.randn(rows,cols)
df = pd.DataFrame(a)
display(df)
print(df.mean())
print(df.std())
display(df**2)

# Thereafter we can select specific columns only and plot final results

# In[ ]:

df.columns = ['First', 'Second', 'Third', 'Fourth', 'Fifth']
df.index = np.arange(10)

display(df)
print(df['Second'].mean() )
print(df.info())
print(df.describe())

from pylab import plt, mpl
plt.style.use('seaborn')
mpl.rcParams['font.family'] = 'serif'

df.cumsum().plot(lw=2.0, figsize=(10,6))
plt.show()

df.plot.bar(figsize=(10,6), rot=15)
plt.show()

# We can produce a $4\times 4$ matrix

# In[ ]:

b = np.arange(16).reshape((4,4))
print(b)
df1 = pd.DataFrame(b)
print(df1)

# and many other operations.
#
#
# The **Series** class is another important class included in
# **pandas**. You can view it as a specialization of **DataFrame** but where
# we have just a single column of data. It shares many of the same
# features as **DataFrame**. As with **DataFrame**, most operations are
# vectorized, achieving thereby a high performance when dealing with
# computations of arrays, in particular labeled arrays.  As we will see
# below it leads also to a very concice code close to the mathematical
# operations we may be interested in.  For multidimensional arrays, we
# recommend strongly
# [xarray](http://xarray.pydata.org/en/stable/). **xarray** has much of
# the same flexibility as **pandas**, but allows for the extension to
# higher dimensions than two.
#
#
#
#
#
# ## Introduction to Git and GitHub/GitLab and similar
#
# [Git](https://git-scm.com/) is a distributed version-control system
# for tracking changes in any set of files, originally designed for
# coordinating work among programmers cooperating on source code during
# software development.
#
# The [reference document and videos here](https://git-scm.com/doc)
# give you an excellent introduction to the **git**.
#
# We believe you will find version-control software very useful in your work.
#
#
#
#
# All teaching material related to this course is open and freely
# available via the GitHub site of the course. The video here gives a
# short intro to
# [GitHub](https://www.youtube.com/watch/w3jLJU7DT5E?reload=9).
#
# See also the [overview video on Git and GitHub](https://mediaspace.msu.edu/media/t/1_8mgx3cyf).
#
#
#
# ### Useful Git and GitHub links
#
# These are a couple references that we have found useful (git commands, markdown, GitPages):
# * <https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet>
#
# * <https://education.github.com/git-cheat-sheet-education.pdf>
#
# * <https://guides.github.com/features/pages/>
#
# When dealing with homeworks, at some point you would need to use an
# editor, or an integrated development envinroment (IDE). As an IDE, we
# would like to recommend **anaconda** since we end up using
# jupyter-notebooks.  **anaconda** runs on all known operating systems.
#
#
# If you prefer editing **Python** codes, there are several excellent cross-platform editors.
# If you are in a Windows environment, **word** is the classical text editor.
#
# There is however a wealth of text editors and/ord IDEs that run on all operating
# systems and functions well with Python.  Some of the more popular ones  are
#
# * [Atom](https://atom.io/)
#
# * [Sublime](https://www.sublimetext.com/)


#!/usr/bin/env python
# coding: utf-8

# # Residual Networks
#
# Welcome to the first assignment of this week! You'll be building a very deep convolutional network, using Residual Networks (ResNets). In theory, very deep networks can represent very complex functions; but in practice, they are hard to train. Residual Networks, introduced by [He et al.](https://arxiv.org/pdf/1512.03385.pdf), allow you to train much deeper networks than were previously feasible.
#
# **By the end of this assignment, you'll be able to:**
#
# - Implement the basic building blocks of ResNets in a deep neural network using Keras
# - Put together these building blocks to implement and train a state-of-the-art neural network for image classification
# - Implement a skip connection in your network
#
# For this assignment, you'll use Keras.
#
# Before jumping into the problem, run the cell below to load the required packages.

# ## Table of Content
#
# - [1 - Packages](#1)
# - [2 - The Problem of Very Deep Neural Networks](#2)
# - [3 - Building a Residual Network](#3)
#     - [3.1 - The Identity Block](#3-1)
#         - [Exercise 1 - identity_block](#ex-1)
#     - [3.2 - The Convolutional Block](#3-2)
#         - [Exercise 2 - convolutional_block](#ex-2)
# - [4 - Building Your First ResNet Model (50 layers)](#4)
#     - [Exercise 3 - ResNet50](#ex-3)
# - [5 - Test on Your Own Image (Optional/Ungraded)](#5)
# - [6 - Bibliography](#6)

#
# ## 1 - Packages

# In[1]:

import tensorflow as tf
import numpy as np
import scipy.misc
from tensorflow.keras.applications.resnet_v2 import ResNet50V2
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.resnet_v2 import preprocess_input, decode_predictions
from tensorflow.keras import layers
from tensorflow.keras.layers import Input, Add, Dense, Activation, ZeroPadding2D, BatchNormalization, Flatten, Conv2D, AveragePooling2D, MaxPooling2D, GlobalMaxPooling2D
from tensorflow.keras.models import Model, load_model
from resnets_utils import *
from tensorflow.keras.initializers import random_uniform, glorot_uniform, constant, identity
from tensorflow.python.framework.ops import EagerTensor
from matplotlib.pyplot import imshow

from test_utils import summary, comparator
import public_tests

get_ipython().run_line_magic('matplotlib', 'inline')

#
# ## 2 - The Problem of Very Deep Neural Networks
#
# Last week, you built your first convolutional neural networks: first manually with numpy, then using Tensorflow and Keras.
#
# In recent years, neural networks have become much deeper, with state-of-the-art networks evolving from having just a few layers (e.g., AlexNet) to over a hundred layers.
#
# * The main benefit of a very deep network is that it can represent very complex functions. It can also learn features at many different levels of abstraction, from edges (at the shallower layers, closer to the input) to very complex features (at the deeper layers, closer to the output).
#
# * However, using a deeper network doesn't always help. A huge barrier to training them is vanishing gradients: very deep networks often have a gradient signal that goes to zero quickly, thus making gradient descent prohibitively slow.
#
# * More specifically, during gradient descent, as you backpropagate from the final layer back to the first layer, you are multiplying by the weight matrix on each step, and thus the gradient can decrease exponentially quickly to zero (or, in rare cases, grow exponentially quickly and "explode," from gaining very large values).
#
# * During training, you might therefore see the magnitude (or norm) of the gradient for the shallower layers decrease to zero very rapidly as training proceeds, as shown below:

#
#
   Figure 1   : Vanishing gradient
 The speed of learning decreases very rapidly for the shallower layers as the network trains

#
# Not to worry! You are now going to solve this problem by building a Residual Network!

#
# ## 3 - Building a Residual Network
#
# In ResNets, a "shortcut" or a "skip connection" allows the model to skip layers:
#
#
#
   Figure 2   : A ResNet block showing a skip-connection

#
# The image on the left shows the "main path" through the network. The image on the right adds a shortcut to the main path. By stacking these ResNet blocks on top of each other, you can form a very deep network.
#
# The lecture mentioned that having ResNet blocks with the shortcut also makes it very easy for one of the blocks to learn an identity function. This means that you can stack on additional ResNet blocks with little risk of harming training set performance.
#
# On that note, there is also some evidence that the ease of learning an identity function accounts for ResNets' remarkable performance even more than skip connections help with vanishing gradients.
#
# Two main types of blocks are used in a ResNet, depending mainly on whether the input/output dimensions are the same or different. You are going to implement both of them: the "identity block" and the "convolutional block."

#
# ### 3.1 - The Identity Block
#
# The identity block is the standard block used in ResNets, and corresponds to the case where the input activation (say $a^{[l]}$) has the same dimension as the output activation (say $a^{[l+2]}$). To flesh out the different steps of what happens in a ResNet's identity block, here is an alternative diagram showing the individual steps:
#
#
#
   Figure 3   : Identity block. Skip connection "skips over" 2 layers.

#
# The upper path is the "shortcut path." The lower path is the "main path." In this diagram, notice the CONV2D and ReLU steps in each layer. To speed up training, a BatchNorm step has been added. Don't worry about this being complicated to implement--you'll see that BatchNorm is just one line of code in Keras!
#
# In this exercise, you'll actually implement a slightly more powerful version of this identity block, in which the skip connection "skips over" 3 hidden layers rather than 2 layers. It looks like this:
#
#
#
   Figure 4   : Identity block. Skip connection "skips over" 3 layers.

# These are the individual steps:
#
# First component of main path:
# - The first CONV2D has $F_1$ filters of shape (1,1) and a stride of (1,1). Its padding is "valid". Use 0 as the seed for the random uniform initialization: `kernel_initializer = initializer(seed=0)`.
# - The first BatchNorm is normalizing the 'channels' axis.
# - Then apply the ReLU activation function. This has no hyperparameters.
#
# Second component of main path:
# - The second CONV2D has $F_2$ filters of shape $(f,f)$ and a stride of (1,1). Its padding is "same". Use 0 as the seed for the random uniform initialization: `kernel_initializer = initializer(seed=0)`.
# - The second BatchNorm is normalizing the 'channels' axis.
# - Then apply the ReLU activation function. This has no hyperparameters.
#
# Third component of main path:
# - The third CONV2D has $F_3$ filters of shape (1,1) and a stride of (1,1). Its padding is "valid". Use 0 as the seed for the random uniform initialization: `kernel_initializer = initializer(seed=0)`.
# - The third BatchNorm is normalizing the 'channels' axis.
# - Note that there is **no** ReLU activation function in this component.
#
# Final step:
# - The `X_shortcut` and the output from the 3rd layer `X` are added together.
# - **Hint**: The syntax will look something like `Add()([var1,var2])`
# - Then apply the ReLU activation function. This has no hyperparameters.
#
#
# ### Exercise 1 - identity_block
#
# Implement the ResNet identity block. The first component of the main path has been implemented for you already! First, you should read these docs carefully to make sure you understand what's happening. Then, implement the rest.
# - To implement the Conv2D step: [Conv2D](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Conv2D)
# - To implement BatchNorm: [BatchNormalization](https://www.tensorflow.org/api_docs/python/tf/keras/layers/BatchNormalization) `BatchNormalization(axis = 3)(X, training = training)`. If training is set to False, its weights are not updated with the new examples. I.e when the model is used in prediction mode.
# - For the activation, use:  `Activation('relu')(X)`
# - To add the value passed forward by the shortcut: [Add](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Add)
#
# We have added the initializer argument to our functions. This parameter receives an initializer function like the ones included in the package [tensorflow.keras.initializers](https://www.tensorflow.org/api_docs/python/tf/keras/initializers) or any other custom initializer. By default it will be set to [random_uniform](https://www.tensorflow.org/api_docs/python/tf/keras/initializers/RandomUniform)
#
# Remember that these functions accept a `seed` argument that can be any value you want, but that in this notebook must set to 0 for **grading purposes**.

#  Here is where you're actually using the power of the Functional API to create a shortcut path:

# In[2]:

# UNQ_C1
# GRADED FUNCTION: identity_block

def identity_block(X, f, filters, training=True, initializer=random_uniform):
    """
    Implementation of the identity block as defined in Figure 4

    Arguments:
    X -- input tensor of shape (m, n_H_prev, n_W_prev, n_C_prev)
    f -- integer, specifying the shape of the middle CONV's window for the main path
    filters -- python list of integers, defining the number of filters in the CONV layers of the main path
    training -- True: Behave in training mode
                False: Behave in inference mode
    initializer -- to set up the initial weights of a layer. Equals to random uniform initializer

    Returns:
    X -- output of the identity block, tensor of shape (m, n_H, n_W, n_C)
    """

    # Retrieve Filters
    F1, F2, F3 = filters

    # Save the input value. You'll need this later to add back to the main path.
    X_shortcut = X

    # First component of main path
    X = Conv2D(filters = F1, kernel_size = 1, strides = (1,1), padding = 'valid', kernel_initializer = initializer(seed=0))(X)
    X = BatchNormalization(axis = 3)(X, training = training) # Default axis
    X = Activation('relu')(X)

    ### START CODE HERE
    ## Second component of main path (≈3 lines)
    X = Conv2D(filters = F2, kernel_size = (f, f), strides = (1,1), padding = 'same', kernel_initializer = initializer(seed=0))(X)
    X = BatchNormalization(axis = 3)(X, training = training)
    X = Activation('relu')(X)

    ## Third component of main path (≈2 lines)
    X = Conv2D(filters = F3, kernel_size = (1, 1), strides = (1,1), padding = 'valid', kernel_initializer = initializer(seed=0))(X)
    X = BatchNormalization(axis = 3)(X, training = training)

    ## Final step: Add shortcut value to main path, and pass it through a RELU activation (≈2 lines)
    X = Add()([X, X_shortcut])
    X = Activation('relu')(X)
    ### END CODE HERE

    return X

# In[3]:

np.random.seed(1)
X1 = np.ones((1, 4, 4, 3)) * -1
X2 = np.ones((1, 4, 4, 3)) * 1
X3 = np.ones((1, 4, 4, 3)) * 3

X = np.concatenate((X1, X2, X3), axis = 0).astype(np.float32)

A3 = identity_block(X, f=2, filters=[4, 4, 3],
                   initializer=lambda seed=0:constant(value=1),
                   training=False)
print('\033[1mWith training=False\033[0m\n')
A3np = A3.numpy()
print(np.around(A3.numpy()[:,(0,-1),:,:].mean(axis = 3), 5))
resume = A3np[:,(0,-1),:,:].mean(axis = 3)
print(resume[1, 1, 0])

print('\n\033[1mWith training=True\033[0m\n')
np.random.seed(1)
A4 = identity_block(X, f=2, filters=[3, 3, 3],
                   initializer=lambda seed=0:constant(value=1),
                   training=True)
print(np.around(A4.numpy()[:,(0,-1),:,:].mean(axis = 3), 5))

public_tests.identity_block_test(identity_block)

# **Expected value**
#
# ```
# With training=False
#
# [[[  0.        0.        0.        0.     ]
#   [  0.        0.        0.        0.     ]]
#
#  [[192.71234 192.71234 192.71234  96.85617]
#   [ 96.85617  96.85617  96.85617  48.92808]]
#
#  [[578.1371  578.1371  578.1371  290.5685 ]
#   [290.5685  290.5685  290.5685  146.78426]]]
# 96.85617
#
# With training=True
#
# [[[0.      0.      0.      0.     ]
#   [0.      0.      0.      0.     ]]
#
#  [[0.40739 0.40739 0.40739 0.40739]
#   [0.40739 0.40739 0.40739 0.40739]]
#
#  [[4.99991 4.99991 4.99991 3.25948]
#   [3.25948 3.25948 3.25948 2.40739]]]
# ```

#
# ### 3.2 - The Convolutional Block
#
# The ResNet "convolutional block" is the second block type. You can use this type of block when the input and output dimensions don't match up. The difference with the identity block is that there is a CONV2D layer in the shortcut path:
#
#
#
   Figure 4   : Convolutional block

#
# * The CONV2D layer in the shortcut path is used to resize the input $x$ to a different dimension, so that the dimensions match up in the final addition needed to add the shortcut value back to the main path. (This plays a similar role as the matrix $W_s$ discussed in lecture.)
# * For example, to reduce the activation dimensions's height and width by a factor of 2, you can use a 1x1 convolution with a stride of 2.
# * The CONV2D layer on the shortcut path does not use any non-linear activation function. Its main role is to just apply a (learned) linear function that reduces the dimension of the input, so that the dimensions match up for the later addition step.
# * As for the previous exercise, the additional `initializer` argument is required for grading purposes, and it has been set by default to [glorot_uniform](https://www.tensorflow.org/api_docs/python/tf/keras/initializers/GlorotUniform)
#
# The details of the convolutional block are as follows.
#
# First component of main path:
# - The first CONV2D has $F_1$ filters of shape (1,1) and a stride of (s,s). Its padding is "valid". Use 0 as the `glorot_uniform` seed `kernel_initializer = initializer(seed=0)`.
# - The first BatchNorm is normalizing the 'channels' axis.
# - Then apply the ReLU activation function. This has no hyperparameters.
#
# Second component of main path:
# - The second CONV2D has $F_2$ filters of shape (f,f) and a stride of (1,1). Its padding is "same".  Use 0 as the `glorot_uniform` seed `kernel_initializer = initializer(seed=0)`.
# - The second BatchNorm is normalizing the 'channels' axis.
# - Then apply the ReLU activation function. This has no hyperparameters.
#
# Third component of main path:
# - The third CONV2D has $F_3$ filters of shape (1,1) and a stride of (1,1). Its padding is "valid".  Use 0 as the `glorot_uniform` seed `kernel_initializer = initializer(seed=0)`.
# - The third BatchNorm is normalizing the 'channels' axis. Note that there is no ReLU activation function in this component.
#
# Shortcut path:
# - The CONV2D has $F_3$ filters of shape (1,1) and a stride of (s,s). Its padding is "valid".  Use 0 as the `glorot_uniform` seed `kernel_initializer = initializer(seed=0)`.
# - The BatchNorm is normalizing the 'channels' axis.
#
# Final step:
# - The shortcut and the main path values are added together.
# - Then apply the ReLU activation function. This has no hyperparameters.
#
#
# ### Exercise 2 - convolutional_block
#
# Implement the convolutional block. The first component of the main path is already implemented; then it's your turn to implement the rest! As before, always use 0 as the seed for the random initialization, to ensure consistency with the grader.
# - [Conv2D](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Conv2D)
# - [BatchNormalization](https://www.tensorflow.org/api_docs/python/tf/keras/layers/BatchNormalization) (axis: Integer, the axis that should be normalized (typically the features axis)) `BatchNormalization(axis = 3)(X, training = training)`. If training is set to False, its weights are not updated with the new examples. I.e when the model is used in prediction mode.
# - For the activation, use:  `Activation('relu')(X)`
# - [Add](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Add)
#
# We have added the initializer argument to our functions. This parameter receives an initializer function like the ones included in the package [tensorflow.keras.initializers](https://www.tensorflow.org/api_docs/python/tf/keras/initializers) or any other custom initializer. By default it will be set to [random_uniform](https://www.tensorflow.org/api_docs/python/tf/keras/initializers/RandomUniform)
#
# Remember that these functions accept a `seed` argument that can be any value you want, but that in this notebook must set to 0 for **grading purposes**.

# In[4]:

# UNQ_C2
# GRADED FUNCTION: convolutional_block

def convolutional_block(X, f, filters, s = 2, training=True, initializer=glorot_uniform):
    """
    Implementation of the convolutional block as defined in Figure 4

    Arguments:
    X -- input tensor of shape (m, n_H_prev, n_W_prev, n_C_prev)
    f -- integer, specifying the shape of the middle CONV's window for the main path
    filters -- python list of integers, defining the number of filters in the CONV layers of the main path
    s -- Integer, specifying the stride to be used
    training -- True: Behave in training mode
                False: Behave in inference mode
    initializer -- to set up the initial weights of a layer. Equals to Glorot uniform initializer,
                   also called Xavier uniform initializer.

    Returns:
    X -- output of the convolutional block, tensor of shape (n_H, n_W, n_C)
    """

    # Retrieve Filters
    F1, F2, F3 = filters

    # Save the input value
    X_shortcut = X

    ##### MAIN PATH #####

    # First component of main path glorot_uniform(seed=0)
    X = Conv2D(filters = F1, kernel_size = 1, strides = (s, s), padding='valid', kernel_initializer = initializer(seed=0))(X)
    X = BatchNormalization(axis = 3)(X, training=training)
    X = Activation('relu')(X)

    ### START CODE HERE

    ## Second component of main path (≈3 lines)
    X = Conv2D(filters = F2, kernel_size = (f,f), strides = (1, 1), padding='same', kernel_initializer = initializer(seed=0))(X)
    X = BatchNormalization(axis = 3)(X, training=training)
    X = Activation('relu')(X)

    ## Third component of main path (≈2 lines)
    X = Conv2D(filters = F3, kernel_size = (1,1), strides = (1, 1), padding='valid', kernel_initializer = initializer(seed=0))(X)
    X = BatchNormalization(axis = 3)(X, training=training)

    ##### SHORTCUT PATH ##### (≈2 lines)
    X_shortcut = Conv2D(filters = F3, kernel_size = (1,1), strides = (s, s), padding='valid', kernel_initializer = initializer(seed=0))(X_shortcut)
    X_shortcut = BatchNormalization(axis = 3)(X_shortcut, training=training)

    ### END CODE HERE

    # Final step: Add shortcut value to main path (Use this order [X, X_shortcut]), and pass it through a RELU activation
    X = Add()([X, X_shortcut])
    X = Activation('relu')(X)

    return X

# In[5]:

from outputs import convolutional_block_output1, convolutional_block_output2
np.random.seed(1)
#X = np.random.randn(3, 4, 4, 6).astype(np.float32)
X1 = np.ones((1, 4, 4, 3)) * -1
X2 = np.ones((1, 4, 4, 3)) * 1
X3 = np.ones((1, 4, 4, 3)) * 3

X = np.concatenate((X1, X2, X3), axis = 0).astype(np.float32)

A = convolutional_block(X, f = 2, filters = [2, 4, 6], training=False)

assert type(A) == EagerTensor, "Use only tensorflow and keras functions"
assert tuple(tf.shape(A).numpy()) == (3, 2, 2, 6), "Wrong shape."
assert np.allclose(A.numpy(), convolutional_block_output1), "Wrong values when training=False."
print(A[0])

B = convolutional_block(X, f = 2, filters = [2, 4, 6], training=True)
assert np.allclose(B.numpy(), convolutional_block_output2), "Wrong values when training=True."

print('\033[92mAll tests passed!')

# **Expected value**
#
# ```
# tf.Tensor(
# [[[0.         0.66683817 0.         0.         0.88853896 0.5274254 ]
#   [0.         0.65053666 0.         0.         0.89592844 0.49965227]]
#
#  [[0.         0.6312079  0.         0.         0.8636247  0.47643146]
#   [0.         0.5688321  0.         0.         0.85534114 0.41709304]]], shape=(2, 2, 6), dtype=float32)
# ```

#
# ## 4 - Building Your First ResNet Model (50 layers)
#
# You now have the necessary blocks to build a very deep ResNet. The following figure describes in detail the architecture of this neural network. "ID BLOCK" in the diagram stands for "Identity block," and "ID BLOCK x3" means you should stack 3 identity blocks together.
#
#
#
   Figure 5   : ResNet-50 model

#
# The details of this ResNet-50 model are:
# - Zero-padding pads the input with a pad of (3,3)
# - Stage 1:
#     - The 2D Convolution has 64 filters of shape (7,7) and uses a stride of (2,2).
#     - BatchNorm is applied to the 'channels' axis of the input.
#     - ReLU activation is applied.
#     - MaxPooling uses a (3,3) window and a (2,2) stride.
# - Stage 2:
#     - The convolutional block uses three sets of filters of size [64,64,256], "f" is 3, and "s" is 1.
#     - The 2 identity blocks use three sets of filters of size [64,64,256], and "f" is 3.
# - Stage 3:
#     - The convolutional block uses three sets of filters of size [128,128,512], "f" is 3 and "s" is 2.
#     - The 3 identity blocks use three sets of filters of size [128,128,512] and "f" is 3.
# - Stage 4:
#     - The convolutional block uses three sets of filters of size [256, 256, 1024], "f" is 3 and "s" is 2.
#     - The 5 identity blocks use three sets of filters of size [256, 256, 1024] and "f" is 3.
# - Stage 5:
#     - The convolutional block uses three sets of filters of size [512, 512, 2048], "f" is 3 and "s" is 2.
#     - The 2 identity blocks use three sets of filters of size [512, 512, 2048] and "f" is 3.
# - The 2D Average Pooling uses a window of shape (2,2).
# - The 'flatten' layer doesn't have any hyperparameters.
# - The Fully Connected (Dense) layer reduces its input to the number of classes using a softmax activation.
#
#
#
# ### Exercise 3 - ResNet50
#
# Implement the ResNet with 50 layers described in the figure above. We have implemented Stages 1 and 2. Please implement the rest. (The syntax for implementing Stages 3-5 should be quite similar to that of Stage 2) Make sure you follow the naming convention in the text above.
#
# You'll need to use this function:
# - Average pooling [see reference](https://www.tensorflow.org/api_docs/python/tf/keras/layers/AveragePooling2D)
#
# Here are some other functions we used in the code below:
# - Conv2D: [See reference](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Conv2D)
# - BatchNorm: [See reference](https://www.tensorflow.org/api_docs/python/tf/keras/layers/BatchNormalization) (axis: Integer, the axis that should be normalized (typically the features axis))
# - Zero padding: [See reference](https://www.tensorflow.org/api_docs/python/tf/keras/layers/ZeroPadding2D)
# - Max pooling: [See reference](https://www.tensorflow.org/api_docs/python/tf/keras/layers/MaxPool2D)
# - Fully connected layer: [See reference](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Dense)
# - Addition: [See reference](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Add)

# In[9]:

# UNQ_C3
# GRADED FUNCTION: ResNet50

def ResNet50(input_shape = (64, 64, 3), classes = 6):
    """
    Stage-wise implementation of the architecture of the popular ResNet50:
    CONV2D -> BATCHNORM -> RELU -> MAXPOOL -> CONVBLOCK -> IDBLOCK*2 -> CONVBLOCK -> IDBLOCK*3
    -> CONVBLOCK -> IDBLOCK*5 -> CONVBLOCK -> IDBLOCK*2 -> AVGPOOL -> FLATTEN -> DENSE

    Arguments:
    input_shape -- shape of the images of the dataset
    classes -- integer, number of classes

    Returns:
    model -- a Model() instance in Keras
    """

    # Define the input as a tensor with shape input_shape
    X_input = Input(input_shape)

    # Zero-Padding
    X = ZeroPadding2D((3, 3))(X_input)

    # Stage 1
    X = Conv2D(64, (7, 7), strides = (2, 2), kernel_initializer = glorot_uniform(seed=0))(X)
    X = BatchNormalization(axis = 3)(X)
    X = Activation('relu')(X)
    X = MaxPooling2D((3, 3), strides=(2, 2))(X)

    # Stage 2
    X = convolutional_block(X, f = 3, filters = [64, 64, 256], s = 1)
    X = identity_block(X, 3, [64, 64, 256])
    X = identity_block(X, 3, [64, 64, 256])

    ### START CODE HERE

    ## Stage 3 (≈4 lines)
    X = convolutional_block(X, f = 3, filters = [128,128,512], s = 2)
    X = identity_block(X, 3, [128,128,512])
    X = identity_block(X, 3, [128,128,512])
    X = identity_block(X, 3, [128,128,512])

    ## Stage 4 (≈6 lines)
    X = convolutional_block(X, f = 3, filters = [256,256,1024], s = 2)
    X = identity_block(X, 3, [256, 256, 1024])
    X = identity_block(X, 3, [256, 256, 1024])
    X = identity_block(X, 3, [256, 256, 1024])
    X = identity_block(X, 3, [256, 256, 1024])
    X = identity_block(X, 3, [256, 256, 1024])

    ## Stage 5 (≈3 lines)
    X = convolutional_block(X, f = 3, filters = [512, 512, 2048], s = 2)
    X = identity_block(X, 3, [512, 512, 2048])
    X = identity_block(X, 3, [512, 512, 2048])

    ## AVGPOOL (≈1 line). Use "X = AveragePooling2D(...)(X)"
    X = X = AveragePooling2D((2,2))(X)

    ### END CODE HERE

    # output layer
    X = Flatten()(X)
    X = Dense(classes, activation='softmax', kernel_initializer = glorot_uniform(seed=0))(X)

    # Create model
    model = Model(inputs = X_input, outputs = X)

    return model

# Run the following code to build the model's graph. If your implementation is incorrect, you'll know it by checking your accuracy when running `model.fit(...)` below.

# In[10]:

model = ResNet50(input_shape = (64, 64, 3), classes = 6)
print(model.summary())

# In[11]:

from outputs import ResNet50_summary

model = ResNet50(input_shape = (64, 64, 3), classes = 6)

comparator(summary(model), ResNet50_summary)

# As shown in the Keras Tutorial Notebook, prior to training a model, you need to configure the learning process by compiling the model.

# In[12]:

model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

# The model is now ready to be trained. The only thing you need now is a dataset!

# Let's load your old friend, the SIGNS dataset.
#
#
#
   Figure 6   : SIGNS dataset

#

# In[13]:

X_train_orig, Y_train_orig, X_test_orig, Y_test_orig, classes = load_dataset()

# Normalize image vectors
X_train = X_train_orig / 255.
X_test = X_test_orig / 255.

# Convert training and test labels to one hot matrices
Y_train = convert_to_one_hot(Y_train_orig, 6).T
Y_test = convert_to_one_hot(Y_test_orig, 6).T

print ("number of training examples = " + str(X_train.shape[0]))
print ("number of test examples = " + str(X_test.shape[0]))
print ("X_train shape: " + str(X_train.shape))
print ("Y_train shape: " + str(Y_train.shape))
print ("X_test shape: " + str(X_test.shape))
print ("Y_test shape: " + str(Y_test.shape))

# Run the following cell to train your model on 10 epochs with a batch size of 32. On a GPU, it should take less than 2 minutes.

# In[14]:

model.fit(X_train, Y_train, epochs = 10, batch_size = 32)

# **Expected Output**:
#
# ```
# Epoch 1/10
# 34/34 [==============================] - 1s 34ms/step - loss: 1.9241 - accuracy: 0.4620
# Epoch 2/10
# 34/34 [==============================] - 2s 57ms/step - loss: 0.6403 - accuracy: 0.7898
# Epoch 3/10
# 34/34 [==============================] - 1s 24ms/step - loss: 0.3744 - accuracy: 0.8731
# Epoch 4/10
# 34/34 [==============================] - 2s 44ms/step - loss: 0.2220 - accuracy: 0.9231
# Epoch 5/10
# 34/34 [==============================] - 2s 57ms/step - loss: 0.1333 - accuracy: 0.9583
# Epoch 6/10
# 34/34 [==============================] - 2s 52ms/step - loss: 0.2243 - accuracy: 0.9444
# Epoch 7/10
# 34/34 [==============================] - 2s 48ms/step - loss: 0.2913 - accuracy: 0.9102
# Epoch 8/10
# 34/34 [==============================] - 1s 30ms/step - loss: 0.2269 - accuracy: 0.9306
# Epoch 9/10
# 34/34 [==============================] - 2s 46ms/step - loss: 0.1113 - accuracy: 0.9630
# Epoch 10/10
# 34/34 [==============================] - 2s 57ms/step - loss: 0.0709 - accuracy: 0.9778
# ```
#
# The exact values could not match, but don't worry about that. The important thing that you must see is that the loss value decreases, and the accuracy increases for the firsts 5 epochs.

# Let's see how this model (trained on only two epochs) performs on the test set.

# In[15]:

preds = model.evaluate(X_test, Y_test)
print ("Loss = " + str(preds[0]))
print ("Test Accuracy = " + str(preds[1]))

# **Expected Output**:
#
#

#

#
#             Test Accuracy
#
#
#            >0.80
#
#

#
#

# For the purposes of this assignment, you've been asked to train the model for ten epochs. You can see that it performs well. The online grader will only run your code for a small number of epochs as well. Please go ahead and submit your assignment.

# After you have finished this official (graded) part of this assignment, you can also optionally train the ResNet for more iterations, if you want. It tends to get much better performance when trained for ~20 epochs, but this does take more than an hour when training on a CPU.
#
# Using a GPU, this ResNet50 model's weights were trained on the SIGNS dataset. You can load and run the trained model on the test set in the cells below. It may take ≈1min to load the model. Have fun!

# In[16]:

pre_trained_model = tf.keras.models.load_model('resnet50.h5')

# In[17]:

preds = pre_trained_model.evaluate(X_test, Y_test)
print ("Loss = " + str(preds[0]))
print ("Test Accuracy = " + str(preds[1]))

# **Congratulations** on finishing this assignment! You've now implemented a state-of-the-art image classification system! Woo hoo!
#
# ResNet50 is a powerful model for image classification when it's trained for an adequate number of iterations. Hopefully, from this point, you can use what you've learned and apply it to your own classification problem to perform state-of-the-art accuracy.

#
#
# **What you should remember**:
#
# - Very deep "plain" networks don't work in practice because vanishing gradients make them hard to train.
# - Skip connections help address the Vanishing Gradient problem. They also make it easy for a ResNet block to learn an identity function.
# - There are two main types of blocks: The **identity block** and the **convolutional block**.
# - Very deep Residual Networks are built by stacking these blocks together.

#
# ## 5 - Test on Your Own Image (Optional/Ungraded)

# If you wish, you can also take a picture of your own hand and see the output of the model. To do this:
#     1. Click on "File" in the upper bar of this notebook, then click "Open" to go on your Coursera Hub.
#     2. Add your image to this Jupyter Notebook's directory, in the "images" folder
#     3. Write your image's name in the following code
#     4. Run the code and check if the algorithm is right!

# In[ ]:

img_path = 'images/my_image.jpg'
img = image.load_img(img_path, target_size=(64, 64))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = x/255.0
print('Input image shape:', x.shape)
imshow(img)
prediction = pre_trained_model.predict(x)
print("Class prediction vector [p(0), p(1), p(2), p(3), p(4), p(5)] = ", prediction)
print("Class:", np.argmax(prediction))

# Even though the model has high accuracy, it might be performing poorly on your own set of images. Notice that, the shape of the pictures, the lighting where the photos were taken, and all of the preprocessing steps can have an impact on the performance of the model. Considering everything you have learned in this specialization so far, what do you think might be the cause here?
#
# *Hint*: It might be related to some distributions. Can you come up with a potential solution ?

# You can also print a summary of your model by running the following code.

# In[ ]:

pre_trained_model.summary()

#
# ## 6 - Bibliography
#
# This notebook presents the ResNet algorithm from He et al. (2015). The implementation here also took significant inspiration and follows the structure given in the GitHub repository of Francois Chollet:
#
# - Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun - [Deep Residual Learning for Image Recognition (2015)](https://arxiv.org/abs/1512.03385)
# - Francois Chollet's GitHub repository: https://github.com/fchollet/deep-learning-models/blob/master/resnet50.py
#

# In[ ]:

4 - Convolutional Neural Networks/Residual_Networks.py