latex/examples/glimpse-at-scipy.tex
author Puneeth Chaganti <punchagan@fossee.in>
Tue, 31 Aug 2010 18:45:36 +0530
changeset 103 313bebeb7862
permissions -rw-r--r--
Added sample document for LaTeX session.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
103
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     1
\documentclass{article}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     2
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     3
\usepackage{amsmath} 
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     4
\usepackage{graphicx} 
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     5
\usepackage{color}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     6
\usepackage{listings} 
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     7
\usepackage{url}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     8
%\definecolor{darkgreen}{rgb}{0,0.5,0}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     9
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    10
\lstset{language=Python,
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    11
    basicstyle=\ttfamily\bfseries,
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    12
    commentstyle=\color{red}\itshape,
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    13
  stringstyle=\color{green},
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    14
  showstringspaces=false,
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    15
  keywordstyle=\color{blue}\bfseries}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    16
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    17
\title{A Glimpse at Scipy}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    18
\author{FOSSEE}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    19
\date{June 2010}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    20
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    21
\begin{document}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    22
\maketitle
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    23
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    24
\begin{abstract}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    25
This document shows a glimpse of the features of Scipy that will be
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    26
explored during this course.
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    27
\end{abstract}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    28
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    29
\section{Introduction}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    30
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    31
SciPy is open-source software for mathematics, science, and
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    32
engineering.
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    33
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    34
SciPy (pronounced ``Sigh Pie'') is a collection of mathematical
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    35
algorithms and convenience functions built on the Numpy extension for
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    36
Python. It adds significant power to the interactive Python session by
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    37
exposing the user to high-level commands and classes for the
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    38
manipulation and visualization of data. With SciPy, an interactive
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    39
Python session becomes a data-processing and system-prototyping
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    40
environment rivaling sytems such as \emph{Matlab, IDL, Octave, R-Lab,
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    41
  and Scilab}. \cite{scipy}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    42
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    43
%% \begin{quote}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    44
%%         	In 1998, ... I came across Python and its numerical extension
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    45
%%       	(Numeric) while I was looking for ways to analyze large data sets
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    46
%%       	... using a high-level language. I quickly fell in love with
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    47
%%       	Python programming which is a remarkable statement to make about a
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    48
%%       	programming language. If I had not seen others with the same view,
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    49
%%       	I might have seriously doubted my sanity.  -- Travis Oliphant, Creator of Numpy    
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    50
%% \end{quote}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    51
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    52
\subsection{Sub-packages of Scipy}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    53
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    54
SciPy is organized into subpackages covering different scientific
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    55
computing domains. These are summarized in the \underline{table
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    56
  \ref{subpkg}}.
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    57
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    58
\begin{table}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    59
  \caption{Sub-packages available in Scipy}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    60
  \label{subpkg}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    61
\begin{tabular}{|l|l|}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    62
\hline
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    63
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    64
\textbf{Subpackage} & \textbf{Description}\\
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    65
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    66
\hline
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    67
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    68
\texttt{cluster} & Clustering algorithms\\
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    69
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    70
\hline
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    71
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    72
\texttt{constants} & Physical and mathematical constants\\
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    73
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    74
\hline
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    75
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    76
\texttt{fftpack} & Fast Fourier Transform routines\\
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    77
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    78
\hline
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    79
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    80
\texttt{integrate} & Integration and ordinary differential equation
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    81
solvers\\
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    82
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    83
\hline
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    84
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    85
\texttt{interpolate} & Interpolation and smoothing splines\\
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    86
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    87
\hline
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    88
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    89
\texttt{io} & Input and Output\\
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    90
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    91
\hline
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    92
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    93
\texttt{linalg} & Linear algebra\\
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    94
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    95
\hline
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    96
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    97
\texttt{maxentropy} & Maximum entropy methods\\
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    98
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    99
\hline
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   100
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   101
\texttt{ndimage} & N-dimensional image processing\\
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   102
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   103
\hline
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   104
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   105
\texttt{odr} & Orthogonal distance regression\\
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   106
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   107
\hline
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   108
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   109
\texttt{optimize} & Optimization and root-finding routines\\
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   110
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   111
\hline
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   112
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   113
\texttt{signal} & Signal processing\\
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   114
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   115
\hline
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   116
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   117
\texttt{sparse} & Sparse matrices and associated routines\\
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   118
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   119
\hline
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   120
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   121
\texttt{spatial} & Spatial data structures and algorithms\\
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   122
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   123
\hline
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   124
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   125
\texttt{special} & Special functions\\
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   126
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   127
\hline
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   128
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   129
\texttt{stats} & Statistical distributions and functions\\
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   130
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   131
\hline
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   132
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   133
\texttt{weave} & C/C++ integration\\
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   134
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   135
\hline
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   136
\end{tabular}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   137
\end{table}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   138
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   139
\subsection{Use of Scipy in this course}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   140
Following is a partial list of tasks we shall perform using Scipy, in
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   141
this course.
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   142
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   143
\begin{enumerate}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   144
  \item Plotting \footnote{using \texttt{pylab} - see Appendix
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   145
    \ref{mpl}}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   146
  \item Matrix Operations
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   147
  \begin{itemize}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   148
    \item Inverse
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   149
    \item Determinant
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   150
  \end{itemize}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   151
  \item Solving Equations
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   152
  \begin{itemize}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   153
    \item System of Linear equations
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   154
    \item Polynomials
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   155
    \item Non-linear equations
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   156
  \end{itemize}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   157
  \item Integration 
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   158
  \begin{itemize}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   159
    \item Quadrature
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   160
    \item ODEs
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   161
  \end{itemize}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   162
\end{enumerate}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   163
\section{A Glimpse of Scipy functions}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   164
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   165
This section gives a brief overview of the tasks that are going to be
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   166
performed using Scipy, in future classes of this course.
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   167
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   168
\subsection{Matrix Operations}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   169
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   170
Let $\mathbf{A}$ be the matrix 
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   171
\(
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   172
\begin{bmatrix}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   173
1 &3 &5\\
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   174
2 &5 &1\\
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   175
2 &3 &8
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   176
\end{bmatrix}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   177
\) 
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   178
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   179
To input $\mathbf{A}$ matrix into python, we do the following in
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   180
\texttt{ipython}\footnote{\texttt{ipython} must be started with
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   181
  \texttt{-pylab} flag}\\
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   182
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   183
\begin{lstlisting}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   184
  In []: A = array([[1,3,5],[2,5,1],[2,3,8]])
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   185
\end{lstlisting}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   186
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   187
\subsubsection{Inverse}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   188
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   189
The inverse of a matrix $\mathbf{A}$ is the matrix $\mathbf{B}$ such
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   190
that $\mathbf{A}\mathbf{B} = \mathbf{I}$ where $\mathbf{I}$ is the
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   191
identity matrix consisting of ones down the main diagonal. Usually
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   192
$\mathbf{B}$ is denoted $\mathbf{B} = \mathbf{A}^{-1}$ . In SciPy, the
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   193
matrix inverse of matrix $\mathbf{A}$ is obtained using
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   194
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   195
\lstinline+inv(A)+.
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   196
\begin{lstlisting}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   197
  In []: inv(A)
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   198
  Out[]: 
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   199
  array([[-1.48,  0.36,  0.88],
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   200
         [ 0.56,  0.08, -0.36],
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   201
         [ 0.16, -0.12,  0.04]])
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   202
\end{lstlisting}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   203
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   204
\subsubsection{Determinant}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   205
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   206
The determinant of a square matrix $\mathbf{A}$ is denoted
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   207
$\left|\mathbf{A}\right|$. Suppose $a_{ij}$ are the elements of the
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   208
matrix $\mathbf{A}$ and let
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   209
$\mathbf{M}_{ij}=\left|\mathbf{A}_{ij}\right|$ be the determinant of
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   210
the matrix left by removing the $i^{th}$ row and $j^{th}$ column from
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   211
$\mathbf{A}$. Then for any row $i$
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   212
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   213
    \[ \left|\mathbf{A}\right|=\sum_{j}\left(-1\right)^{i+j}a_{ij}\mathbf{M}_{ij} \]
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   214
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   215
This is a recursive way to define the determinant where the base case
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   216
is defined by accepting that the determinant of a $1\times1$ matrix is
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   217
the only matrix element. In SciPy the determinant can be calculated
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   218
with $det$ . For example, the determinant of
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   219
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   220
    \[ \mathbf{A}=\begin{bmatrix} 1 & 3 & 5\\ 2 & 5 & 1\\ 2 & 3 & 8\end{bmatrix}\]
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   221
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   222
is
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   223
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   224
    \begin{eqnarray*} 
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   225
      |\mathbf{A}| & = & 1\begin{vmatrix} 5 & 1\\ 3 & 8\end{vmatrix}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   226
                        -3\begin{vmatrix} 2 & 1\\ 2 & 8\end{vmatrix}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   227
                        +5\begin{vmatrix}2 & 5\\ 2 & 3\end{vmatrix}\\  
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   228
                   & = & 1(5\cdot8-3\cdot1)-3(2\cdot8-2\cdot1)+5(2\cdot3-2\cdot5)=-25
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   229
    \end{eqnarray*}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   230
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   231
In SciPy, this is computed as shown below
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   232
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   233
\begin{lstlisting}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   234
  In []: A = array([[1, 3, 5], [2, 5, 1], [2, 3, 8]])
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   235
  In []: det(A)
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   236
  Out[]: -25.0
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   237
\end{lstlisting}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   238
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   239
\subsection{Solving Equations}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   240
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   241
\subsubsection{Linear Equations}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   242
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   243
Solving linear systems of equations is straightforward using the scipy
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   244
command \lstinline+solve+. This command expects an input matrix and a
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   245
right-hand-side vector. The solution vector is then computed. An
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   246
option for entering a symmetrix matrix is offered which can speed up
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   247
the processing when applicable.  As an example, suppose it is desired
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   248
to solve the following simultaneous equations:
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   249
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   250
    \begin{eqnarray} x+3y+5z & = & 10\\ 2x+5y+z & = & 8\\ 2x+3y+8z & = & 3\end{eqnarray}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   251
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   252
We could find the solution vector using a matrix inverse:
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   253
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   254
    \[ \left[\begin{array}{c} x\\ y\\ z\end{array}\right]=\left[\begin{array}{ccc} 1 & 3 & 5\\ 2 & 5 & 1\\ 2 & 3 & 8\end{array}\right]^{-1}\left[\begin{array}{c} 10\\ 8\\ 3\end{array}\right]=\frac{1}{25}\left[\begin{array}{c} -232\\ 129\\ 19\end{array}\right]=\left[\begin{array}{c} -9.28\\ 5.16\\ 0.76\end{array}\right] \]
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   255
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   256
However, it is better to use the solve command which can be faster and
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   257
more numerically stable. In this case it however gives the same
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   258
answer.
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   259
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   260
\begin{lstlisting}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   261
  In []: A = array([[1, 3, 5], [2, 5, 1], [2, 3, 8]])
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   262
  In []: b = array([[10], [8], [3]])
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   263
  In []: dot(inv(A), b)
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   264
  Out[]: 
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   265
  array([[-9.28],
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   266
         [ 5.16],
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   267
         [ 0.76]])
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   268
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   269
  In []: solve(A,b)
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   270
  Out[]: 
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   271
  array([[-9.28],
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   272
         [ 5.16],
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   273
         [ 0.76]])
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   274
\end{lstlisting}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   275
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   276
\subsubsection{Polynomials}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   277
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   278
Solving a polynomial is straightforward in scipy using the
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   279
\lstinline+roots+ command. It expects the coefficients of the
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   280
polynomial in their decreasing order. For example, let's find the
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   281
roots of $x^3 - 2x^2 - \frac{1}{2}x + 1$ are $2$, $\sqrt{2}$ and
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   282
$-\sqrt{2}$. This is easy to see.
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   283
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   284
\begin{align*}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   285
x^3 - 2x^2 - \frac{1}{2}x + 1 = 0\\
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   286
x^2(x-2) - \frac{1}{2}(x-2) = 0\\
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   287
(x-2)(x^2 - \frac{1}{2}) = 0\\
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   288
(x-2)(x - \frac{1}{\sqrt{2}})(x + \frac{1}{\sqrt{2}}) = 0
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   289
\end{align*}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   290
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   291
We do it in scipy as shown below:
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   292
\begin{lstlisting}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   293
  In []: coeff = array([1, -2, -2, 4])
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   294
  In []: roots(coeff)
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   295
\end{lstlisting}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   296
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   297
\subsubsection{Non-linear Equations}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   298
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   299
To find a root of a set of non-linear equations, the command
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   300
\lstinline+fsolve+ is needed. For example, the following example finds
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   301
the roots of the single-variable transcendental equation
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   302
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   303
    \[ x+2\cos\left(x\right)=0,\]
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   304
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   305
and the set of non-linear equations
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   306
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   307
    \begin{align} 
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   308
      x_{0}\cos\left(x_{1}\right) &= 4,\\ 
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   309
      x_{0}x_{1}-x_{1} &= 5
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   310
    \end{align}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   311
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   312
The results are $x=-1.0299$ and $x_{0}=6.5041,\, x_{1}=0.9084$ .
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   313
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   314
\begin{lstlisting}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   315
In []: def func(x):
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   316
   ...:     return x + 2*cos(x)
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   317
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   318
In []: def func2(x):
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   319
  ...:     out = [x[0]*cos(x[1]) - 4]
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   320
  ...:     out.append(x[1]*x[0] - x[1] - 5)
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   321
  ...:     return out
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   322
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   323
In []: from scipy.optimize import fsolve
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   324
In []: x0 = fsolve(func, 0.3)
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   325
In []: print x0
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   326
-1.02986652932
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   327
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   328
In []: x02 = fsolve(func2, [1, 1])
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   329
In []: print x02
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   330
[ 6.50409711  0.90841421]
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   331
\end{lstlisting}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   332
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   333
\subsection{Integration}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   334
% To be done in the lab. 
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   335
\subsubsection{Quadrature}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   336
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   337
The function \texttt{quad} is provided to integrate a function of one
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   338
variable between two points. The points can be $\pm\infty$ ($\pm$
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   339
\texttt{inf}) to indicate infinite limits. For example, suppose you
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   340
wish to integrate the expression $e^{\sin(x)}$ in the interval
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   341
$[0,2\pi]$, i.e. $\int_0^{2\pi}e^{\sin(x)}dx$, it could be computed
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   342
using
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   343
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   344
\begin{lstlisting}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   345
In []: def func(x):
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   346
   ...:     return exp(sin(x))
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   347
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   348
In []: from scipy.integrate import quad
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   349
In []: result = quad(func, 0, 2*pi)
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   350
In []: print result
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   351
(7.9549265210128457, 4.0521874164521979e-10)
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   352
\end{lstlisting}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   353
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   354
\subsubsection{ODE}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   355
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   356
We wish to solve an (a system of) Ordinary Differential Equation. For
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   357
this purpose, we shall use \lstinline{odeint}. As an illustration, let
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   358
us solve the ODE 
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   359
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   360
\begin{align} 
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   361
  \frac{dy}{dt} = ky(L-y)\\ 
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   362
  L = 25000,\,k = 0.00003,\,y(0) = 250 \nonumber 
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   363
\end{align}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   364
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   365
We solve it in scipy as shown below. 
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   366
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   367
\begin{lstlisting}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   368
In []: from scipy.integrate import odeint
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   369
In []: def f(y, t):
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   370
  ...:     k, L = 0.00003, 25000
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   371
  ...:     return k*y*(L-y)
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   372
  ...:
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   373
In []: t = linspace(0, 12, 60)
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   374
In []: y0 = 250
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   375
In []: y = odeint(f, y0, t)
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   376
\end{lstlisting}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   377
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   378
Note: To solve a system of ODEs, we need to change the function to
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   379
return the right hand side of all the equations and the system and the
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   380
pass the required number of initial conditions to the
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   381
\lstinline{odeint} function.
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   382
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   383
\appendix
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   384
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   385
\section{Plotting using Pylab}\label{mpl}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   386
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   387
The following piece of code, produces the plot in Figure \ref{fig:sin}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   388
using \texttt{pylab}\cite{pylab} in \texttt{ipython}\footnote{start
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   389
  \texttt{ipython} with \texttt{-pylab} flag}\cite{ipy}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   390
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   391
\begin{lstlisting}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   392
In []: x = linspace(0, 2*pi, 50)
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   393
In []: plot(x, sin(x))
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   394
In []: title('Sine Curve between 0 and $\pi$')
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   395
In []: legend(['sin(x)'])
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   396
\end{lstlisting}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   397
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   398
\begin{figure}[h!]
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   399
  \begin{center}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   400
    \includegraphics[scale=0.4]{sine}    
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   401
  \end{center}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   402
  \caption{Sine curve}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   403
  \label{fig:sin}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   404
\end{figure}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   405
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   406
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   407
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   408
\begin{thebibliography}{9}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   409
  \bibitem{scipy} 
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   410
    Eric Jones and Travis Oliphant and Pearu Peterson and others,
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   411
    \emph{SciPy: Open source scientific tools for Python}, 2001 -- , 
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   412
    \url{http://www.scipy.org/} 
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   413
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   414
  \bibitem{pylab}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   415
   John D. Hunter, ``Matplotlib: A 2D Graphics Environment,''
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   416
   \emph{Computing in Science and Engineering}, vol. 9, no. 3,
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   417
   pp. 90-95, May/June 2007, doi:10.1109/MCSE.2007.55 
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   418
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   419
  \bibitem{ipy}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   420
    Fernando Perez, Brian E. Granger, ``IPython: A System for
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   421
    Interactive Scientific Computing,'' \emph{Computing in Science and
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   422
    Engineering}, vol.~9, no.~3, pp.~21-29, May/June 2007,
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   423
    doi:10.1109/MCSE.2007.53. 
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   424
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   425
\end{thebibliography}
313bebeb7862 Added sample document for LaTeX session.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   426
\end{document}