diff options
| author | Christian Kolset <christian.kolset@gmail.com> | 2025-04-24 17:37:21 -0600 |
|---|---|---|
| committer | Christian Kolset <christian.kolset@gmail.com> | 2025-04-24 17:37:21 -0600 |
| commit | e53c35223bed9a32f1e9cd3fe75caf344d4b5c7e (patch) | |
| tree | 3a749e57d1ac92f9fdda3f4bb50a3535e6fc72fa | |
| parent | 665eaed5e9a677c4d51d066d21aa8ddb612ff565 (diff) | |
Updated tex files
| -rw-r--r-- | book/module0/intro_to_anaconda.tex | 2 | ||||
| -rw-r--r-- | book/module1/array.tex | 293 | ||||
| -rw-r--r-- | book/module1/basics_of_python.tex | 200 | ||||
| -rw-r--r-- | book/module1/classes_and_objects.tex | 96 | ||||
| -rw-r--r-- | book/module1/control_structures.tex | 4 | ||||
| -rw-r--r-- | book/module1/functions.tex | 2 | ||||
| -rw-r--r-- | book/module1/installing_anaconda.tex | 2 | ||||
| -rw-r--r-- | book/module1/intro_to_anaconda.tex | 2 | ||||
| -rw-r--r-- | book/module1/jupyter_lab_notebook.tex | 160 | ||||
| -rw-r--r-- | book/module1/module1.tex | 2 | ||||
| -rw-r--r-- | book/module2/debugging_code.tex | 2 | ||||
| -rw-r--r-- | book/module4/linear_regression.tex | 22 | ||||
| -rw-r--r-- | book/module4/module4.tex | 1 |
13 files changed, 627 insertions, 161 deletions
diff --git a/book/module0/intro_to_anaconda.tex b/book/module0/intro_to_anaconda.tex index e580178..e4ac639 100644 --- a/book/module0/intro_to_anaconda.tex +++ b/book/module0/intro_to_anaconda.tex @@ -32,7 +32,7 @@ the folder and click on \emph{Anaconda Navigator} to launch the program. \begin{figure} \centering -\includegraphics{figures/installingAnaconda_windows_launched.png} +\includegraphics{../tutorials/figures/installingAnaconda_windows_launched.png} \caption{Anaconda Navigator screen} \end{figure} diff --git a/book/module1/array.tex b/book/module1/array.tex new file mode 100644 index 0000000..db176e7 --- /dev/null +++ b/book/module1/array.tex @@ -0,0 +1,293 @@ +\section{Arrays}\label{arrays} + +In computer programming, an array is a structure for storing and +retrieving data. We often talk about an array as if it were a grid in +space, with each cell storing one element of the data. For instance, if +each element of the data were a number, we might visualize a +``one-dimensional'' array like a list: + +\begin{longtable}[]{@{}llll@{}} +\toprule\noalign{} +1 & 5 & 2 & 0 \\ +\midrule\noalign{} +\endhead +\bottomrule\noalign{} +\endlastfoot +\end{longtable} + +A two-dimensional array would be like a table: + +\begin{longtable}[]{@{}llll@{}} +\toprule\noalign{} +1 & 5 & 2 & 0 \\ +\midrule\noalign{} +\endhead +\bottomrule\noalign{} +\endlastfoot +8 & 3 & 6 & 1 \\ +1 & 7 & 2 & 9 \\ +\end{longtable} + +A three-dimensional array would be like a set of tables, perhaps stacked +as though they were printed on separate pages. If we visualize the +position of each element as a position in space. Then we can represent +the value of the element as a property. In other words, if we were to +analyze the stress concentration of an aluminum block, the property +would be stress. + +\begin{itemize} +\tightlist +\item + From + \href{https://numpy.org/doc/2.2/user/absolute_beginners.html}{Numpy + documentation} + \includegraphics{https://www.mathworks.com/help/examples/matlab/win64/nddemo_02.gif} +\end{itemize} + +If the load on this block changes over time, then we may want to add a +4th dimension i.e.~additional sets of 3-D arrays for each time +increment. As you can see - the more dimensions we add, the more +complicated of a problem we have to solve. It is possible to increase +the number of dimensions to the n-th order. This course we will not be +going beyond dimensional analysis. + +\subsection{Numpy - the python's array +library}\label{numpy---the-pythons-array-library} + +In this tutorial we will be introducing arrays and we will be using the +numpy library. Arrays, lists, vectors, matrices, sets - You might've +heard of them before, they all store data. In programming, an array is a +variable that can hold more than one value at a time. We will be using +the Numpy python library to create arrays. Since we already have +installed Numpy previously, we can start using the package. + +Before importing our first package, let's as ourselves \emph{what is a +package?} A package can be thought of as pre-written python code that we +can re-use. This means the for every script that we write in python we +need to tell it to use a certain package. We call this importing a +package. + +\subsubsection{Importing Numpy}\label{importing-numpy} + +When using packages in python, we need to let it know what package we +will be using. This is called importing. To import numpy we need to +declare it a the start of a script as follows: + +\begin{Shaded} +\begin{Highlighting}[] +\ImportTok{import}\NormalTok{ numpy }\ImportTok{as}\NormalTok{ np} +\end{Highlighting} +\end{Shaded} + +\begin{itemize} +\tightlist +\item + \texttt{import} - calls for a library to use, in our case it is Numpy. +\item + \texttt{as} - gives the library an alias in your script. It's common + convention in Python programming to make the code shorter and more + readable. We will be using \emph{np} as it's a standard using in many + projects. +\end{itemize} + +\subsection{Creating arrays}\label{creating-arrays} + +Now that we have imported the library we can create a one dimensional +array or \emph{vector} with three elements. + +\begin{Shaded} +\begin{Highlighting}[] +\NormalTok{x }\OperatorTok{=}\NormalTok{ np.array([}\DecValTok{1}\NormalTok{,}\DecValTok{2}\NormalTok{,}\DecValTok{3}\NormalTok{])} +\end{Highlighting} +\end{Shaded} + +To create a \emph{matrix} we can nest the arrays to create a two +dimensional array. This is done as follows. + +\begin{Shaded} +\begin{Highlighting}[] +\NormalTok{matrix }\OperatorTok{=}\NormalTok{ np.array([[}\DecValTok{1}\NormalTok{,}\DecValTok{2}\NormalTok{,}\DecValTok{3}\NormalTok{],} +\NormalTok{ [}\DecValTok{4}\NormalTok{,}\DecValTok{5}\NormalTok{,}\DecValTok{6}\NormalTok{],} +\NormalTok{ [}\DecValTok{7}\NormalTok{,}\DecValTok{8}\NormalTok{,}\DecValTok{9}\NormalTok{]])} +\end{Highlighting} +\end{Shaded} + +\emph{Note: for every array we nest, we get a new dimension in our data +structure.} + +\subsubsection{Numpy array creation +functions}\label{numpy-array-creation-functions} + +Numpy comes with some built-in function that we can use to create arrays +quickly. Here are a couple of functions that are commonly used in +python. \#\#\#\# np.arange The \texttt{np.arange()} function returns an +array with evenly spaced values within a specified range. It is similar +to the built-in \texttt{range()} function in Python but returns a Numpy +array instead of a list. The parameters for this function are the start +value (inclusive), the stop value (exclusive), and the step size. If the +step size is not provided, it defaults to 1. + +\begin{Shaded} +\begin{Highlighting}[] +\OperatorTok{\textgreater{}\textgreater{}\textgreater{}}\NormalTok{ np.arange(}\DecValTok{4}\NormalTok{)} +\NormalTok{array([}\FloatTok{0.}\NormalTok{ , }\FloatTok{1.}\NormalTok{, }\FloatTok{2.}\NormalTok{, }\FloatTok{3.}\NormalTok{ ])} +\end{Highlighting} +\end{Shaded} + +In this example, \texttt{np.arange(4)} generates an array starting from +0 and ending before 4, with a step size of 1. + +\paragraph{np.linspace}\label{np.linspace} + +The \texttt{np.linspace()} function returns an array of evenly spaced +values over a specified range. Unlike \texttt{np.arange()}, which uses a +step size to define the spacing between elements, \texttt{np.linspace()} +uses the number of values you want to generate and calculates the +spacing automatically. It accepts three parameters: the start value, the +stop value, and the number of samples. + +\begin{Shaded} +\begin{Highlighting}[] +\OperatorTok{\textgreater{}\textgreater{}\textgreater{}}\NormalTok{ np.linspace(}\FloatTok{1.}\NormalTok{, }\FloatTok{4.}\NormalTok{, }\DecValTok{6}\NormalTok{)} +\NormalTok{array([}\FloatTok{1.}\NormalTok{ , }\FloatTok{1.6}\NormalTok{, }\FloatTok{2.2}\NormalTok{, }\FloatTok{2.8}\NormalTok{, }\FloatTok{3.4}\NormalTok{, }\FloatTok{4.}\NormalTok{ ])} +\end{Highlighting} +\end{Shaded} + +In this example, \texttt{np.linspace(1.,\ 4.,\ 6)} generates 6 evenly +spaced values between 1. and 4., including both endpoints. + +Try this and see what happens: + +\begin{Shaded} +\begin{Highlighting}[] +\NormalTok{x }\OperatorTok{=}\NormalTok{ np.linspace(}\DecValTok{0}\NormalTok{,}\DecValTok{100}\NormalTok{,}\DecValTok{101}\NormalTok{)} +\NormalTok{y }\OperatorTok{=}\NormalTok{ np.sin(x)} +\end{Highlighting} +\end{Shaded} + +\paragraph{Other useful functions}\label{other-useful-functions} + +\begin{itemize} +\tightlist +\item + \texttt{np.zeros()} +\item + \texttt{np.ones()} +\item + \texttt{np.eye()} +\end{itemize} + +\subsubsection{Working with Arrays}\label{working-with-arrays} + +Now that we have been introduced to some ways to create arrays using the +Numpy functions let's start using them. \#\#\#\# Indexing Indexing in +Python allows you to access specific elements within an array based on +their position. This means you can directly retrieve and manipulate +individual items as needed. + +Python uses \textbf{zero-based indexing}, meaning the first element is +at position \textbf{0} rather than \textbf{1}. This approach is common +in many programming languages. For example, in a list with five +elements, the first element is at index \texttt{0}, followed by elements +at indices \texttt{1}, \texttt{2}, \texttt{3}, and \texttt{4}. + +Here's an example of data from a rocket test stand where thrust was +recorded as a function of time. + +\begin{Shaded} +\begin{Highlighting}[] +\NormalTok{thrust\_lbf }\OperatorTok{=}\NormalTok{ np.array(}\FloatTok{0.603355}\NormalTok{, }\FloatTok{2.019083}\NormalTok{, }\FloatTok{2.808092}\NormalTok{, }\FloatTok{4.054973}\NormalTok{, }\FloatTok{1.136618}\NormalTok{, }\FloatTok{0.943668}\NormalTok{)} + +\OperatorTok{\textgreater{}\textgreater{}\textgreater{}}\NormalTok{ thrust\_lbs[}\DecValTok{3}\NormalTok{]} +\end{Highlighting} +\end{Shaded} + +Due to the nature of zero-based indexing. If we want to call the value +\texttt{4.054973} that will be the 3rd index. \#\#\#\# Operations on +arrays - Arithmetic operations (\texttt{+}, \texttt{-}, \texttt{*}, +\texttt{/}, \texttt{**}) - \texttt{np.add()}, \texttt{np.subtract()}, +\texttt{np.multiply()}, \texttt{np.divide()} - \texttt{np.dot()} for dot +product - \texttt{np.matmul()} for matrix multiplication - +\texttt{np.linalg.inv()}, \texttt{np.linalg.det()} for linear algebra +\#\#\#\#\# Statistics - \texttt{np.mean()}, \texttt{np.median()}, +\texttt{np.std()}, \texttt{np.var()} - \texttt{np.min()}, +\texttt{np.max()}, \texttt{np.argmin()}, \texttt{np.argmax()} - +Summation along axes: \texttt{np.sum(arr,\ axis=0)} \#\#\#\#\# Combining +arrays - Concatenation: \texttt{np.concatenate((arr1,\ arr2),\ axis=0)} +- Stacking: \texttt{np.vstack()}, \texttt{np.hstack()} - Splitting: +\texttt{np.split()} + +\subsection{Exercise}\label{exercise} + +Let's solve a statics problem given the following problem + +A simply supported bridge of length L=20L = 20L=20 m is subjected to +three point loads: + +\begin{itemize} +\tightlist +\item + \(P1=10P_1 = 10P1=10 kN\) at \(x=5x = 5x=5 m\) +\item + \(P2=15P_2 = 15P2=15 kN\) at \(x=10x = 10x=10 m\) +\item + \(P3=20P_3 = 20P3=20 kN\) at \(x=15x = 15x=15 m\) +\end{itemize} + +The bridge is supported by two reaction forces at points AAA (left +support) and BBB (right support). We assume the bridge is in static +equilibrium, meaning the sum of forces and sum of moments about any +point must be zero. + +\subparagraph{Equilibrium Equations:}\label{equilibrium-equations} + +\begin{enumerate} +\def\labelenumi{\arabic{enumi}.} +\tightlist +\item + \textbf{Sum of Forces in the Vertical Direction}: + \(RA+RB−P1−P2−P3=0R_A + R_B - P_1 - P_2 - P_3 = 0RA+RB−P1−P2−P3=0\) +\item + \textbf{Sum of Moments About Point A}: + \(5P1+10P2+15P3−20RB=05 P_1 + 10 P_2 + 15 P_3 - 20 R_B = 05P1+10P2+15P3−20RB=0\) +\item + \textbf{Sum of Moments About Point B}: + \(20RA−15P3−10P2−5P1=020 R_A - 15 P_3 - 10 P_2 - 5 P_1 = 020RA−15P3−10P2−5P1=0\) +\end{enumerate} + +\subparagraph{System of Equations:}\label{system-of-equations} + +\{RA+RB−10−15−20=05(10)+10(15)+15(20)−20RB=020RA−5(10)−10(15)−15(20)=0 + +\begin{cases} R_A + R_B - 10 - 15 - 20 = 0 \\ 5(10) + 10(15) + 15(20) - 20 R_B = 0 \\ 20 R_A - 5(10) - 10(15) - 15(20) = 0 \end{cases} + +⎩ + +\subsubsection{Solution}\label{solution} + +\begin{Shaded} +\begin{Highlighting}[] +\ImportTok{import}\NormalTok{ numpy }\ImportTok{as}\NormalTok{ np} + +\CommentTok{\# Define the coefficient matrix A} +\NormalTok{A }\OperatorTok{=}\NormalTok{ np.array([} +\NormalTok{ [}\DecValTok{1}\NormalTok{, }\DecValTok{1}\NormalTok{],} +\NormalTok{ [}\DecValTok{0}\NormalTok{, }\OperatorTok{{-}}\DecValTok{20}\NormalTok{],} +\NormalTok{ [}\DecValTok{20}\NormalTok{, }\DecValTok{0}\NormalTok{]} +\NormalTok{])} + +\CommentTok{\# Define the right{-}hand side vector b} +\NormalTok{b }\OperatorTok{=}\NormalTok{ np.array([} + \DecValTok{45}\NormalTok{,} + \DecValTok{5}\OperatorTok{*}\DecValTok{10} \OperatorTok{+} \DecValTok{10}\OperatorTok{*}\DecValTok{15} \OperatorTok{+} \DecValTok{15}\OperatorTok{*}\DecValTok{20}\NormalTok{,} + \DecValTok{5}\OperatorTok{*}\DecValTok{10} \OperatorTok{+} \DecValTok{10}\OperatorTok{*}\DecValTok{15} \OperatorTok{+} \DecValTok{15}\OperatorTok{*}\DecValTok{20} +\NormalTok{])} + +\CommentTok{\# Solve the system of equations Ax = b} +\NormalTok{x }\OperatorTok{=}\NormalTok{ np.linalg.lstsq(A, b, rcond}\OperatorTok{=}\VariableTok{None}\NormalTok{)[}\DecValTok{0}\NormalTok{] }\CommentTok{\# Using least squares to handle potential overdetermination} + +\CommentTok{\# Display the results} +\BuiltInTok{print}\NormalTok{(}\SpecialStringTok{f"Reaction force at A (R\_A): }\SpecialCharTok{\{}\NormalTok{x[}\DecValTok{0}\NormalTok{]}\SpecialCharTok{:.2f\}}\SpecialStringTok{ kN"}\NormalTok{)} +\BuiltInTok{print}\NormalTok{(}\SpecialStringTok{f"Reaction force at B (R\_B): }\SpecialCharTok{\{}\NormalTok{x[}\DecValTok{1}\NormalTok{]}\SpecialCharTok{:.2f\}}\SpecialStringTok{ kN"}\NormalTok{)} +\end{Highlighting} +\end{Shaded} diff --git a/book/module1/basics_of_python.tex b/book/module1/basics_of_python.tex index 29f8cc4..4b9db05 100644 --- a/book/module1/basics_of_python.tex +++ b/book/module1/basics_of_python.tex @@ -1,57 +1,37 @@ - \hypertarget{basics-of-python}{ -\section{Basics of Python}\label{basics-of-python}} +\section{Basics of Python}\label{basics-of-python} This page contains important fundamental concepts used in Python such as syntax, operators, order or precedence and more. - \hypertarget{syntax}{% -\subsection{Syntax}\label{syntax}} +\subsection{Syntax}\label{syntax} -\hypertarget{indentations-and-blocks}{% -\subsubsection{Indentations and blocks}\label{indentations-and-blocks}} +\subsubsection{Indentations and blocks}\label{indentations-and-blocks} In python \emph{indentations} or the space at the start of each line, signifies a block of code. This becomes important when we start working with function and loops. We will talk more about this in the controls -structures tutorial. +structures tutorial. \#\#\# Comments Comments can be added to your code +using the hash operator (\#). Any text behind the comment operator till +the end of the line will be rendered as a comment. If you have an entire +block of text or code that needs to be commented out, the triple +quotation marks (``\,``\,``) can be used. Once used all the code after +it will be considered a comment until the comment is ended with the +triple quotation marks.f -\hypertarget{comments}{% -\subsubsection{Comments}\label{comments}} - -Comments can be added to your code using the hash operator (\#). Any -text behind the comment operator till the end of the line will be -rendered as a comment. If you have an entire block of text or code that -needs to be commented out, the triple quotation marks (""") can be used. -Once used all the code after it will be considered a comment until the -comment is ended with the triple quotation marks. - - \hypertarget{operators}{% -\subsection{Operators}\label{operators}} +\subsection{Operators}\label{operators} In python, operators are special symbols or keywords that perform operations on values or variables. This section covers some of the most -common operator that you will see in this course. - -\hypertarget{arithmetic-operators}{% -\subsubsection{Arithmetic operators}\label{arithmetic-operators}} - -\begin{longtable}[]{@{}ll@{}} -\toprule -Operator & Name \\ -\midrule -\endhead -+ & Addition \\ -- & Subtraction \\ -* & Multiplication \\ -/ & Division \\ -\% & Modulus \\ -** & Exponentiation \\ -// & Floor division \\ -\bottomrule -\end{longtable} +common operator that you will see in this course. \#\#\# Arithmetic +operators \textbar{} Operator \textbar{} Name \textbar{} \textbar{} --- +\textbar{} --- \textbar{} \textbar{} + \textbar{} Addition \textbar{} +\textbar{} - \textbar{} Subtraction \textbar{} \textbar{} * \textbar{} +Multiplication \textbar{} \textbar{} / \textbar{} Division \textbar{} +\textbar{} \% \textbar{} Modulus \textbar{} \textbar{} ** \textbar{} +Exponentiation \textbar{} \textbar{} // \textbar{} Floor division +\textbar{} -\hypertarget{comparison-operators}{% -\subsubsection{Comparison operators}\label{comparison-operators}} +\subsubsection{Comparison operators}\label{comparison-operators} Used in conditional statements such as \texttt{if} statements or \texttt{while} loops. Note that in the computer world a double equal @@ -59,60 +39,66 @@ sign (\texttt{==}) means \emph{is equal to}, where as the single equal sign assigns the variable or defines the variable to be something. \begin{longtable}[]{@{}ll@{}} -\toprule +\toprule\noalign{} Operator & Name \\ -\midrule +\midrule\noalign{} \endhead +\bottomrule\noalign{} +\endlastfoot == & Equal \\ != & Not equal \\ \textgreater{} & Greater than \\ \textless{} & Less than \\ \textgreater= & Greater than or equal to \\ \textless= & Less than or equal to \\ -\bottomrule \end{longtable} -\hypertarget{logical-operators}{% -\subsubsection{Logical operators}\label{logical-operators}} +\subsubsection{Logical operators}\label{logical-operators} \begin{longtable}[]{@{}ll@{}} -\toprule +\toprule\noalign{} Operator & Descrription \\ -\midrule +\midrule\noalign{} \endhead -\texttt{and} & Returns True if both statemetns are true \\ -\texttt{or} & Returns True if one of the statements is true \\ -\texttt{not} & Reerse the result, returns False if the result is true \\ -\bottomrule +\bottomrule\noalign{} +\endlastfoot +and & Returns True if both statemetns are true \\ +or & Returns True if one of the statements is true \\ +not & Reerse the result, returns False if the result is true \\ \end{longtable} -\hypertarget{identity-operators}{% -\subsubsection{Identity operators}\label{identity-operators}} +\subsubsection{Identity operators}\label{identity-operators} \begin{longtable}[]{@{}ll@{}} -\toprule +\toprule\noalign{} Operator & Description \\ -\midrule +\midrule\noalign{} \endhead +\bottomrule\noalign{} +\endlastfoot is & Returns True if both variables are the same object \\ is not & Returns True if both variables are not the same object \\ -\bottomrule \end{longtable} - \hypertarget{order-of-operation}{% -\subsection{Order of Operation}\label{order-of-operation}} +\subsection{Order of Operation}\label{order-of-operation} Similarly to the order or precedence in mathematics, different computer languages have their own set of rules. Here is a comprehensive table of the order of operation that python follows. \begin{longtable}[]{@{} - >{\raggedright\arraybackslash}p{(\columnwidth - 2\tabcolsep) * \real{0.51}} - >{\raggedright\arraybackslash}p{(\columnwidth - 2\tabcolsep) * \real{0.49}}@{}} -\toprule -Operator & Description \\ -\midrule + >{\raggedright\arraybackslash}p{(\columnwidth - 2\tabcolsep) * \real{0.5093}} + >{\raggedright\arraybackslash}p{(\columnwidth - 2\tabcolsep) * \real{0.4907}}@{}} +\toprule\noalign{} +\begin{minipage}[b]{\linewidth}\raggedright +Operator +\end{minipage} & \begin{minipage}[b]{\linewidth}\raggedright +Description +\end{minipage} \\ +\midrule\noalign{} \endhead +\bottomrule\noalign{} +\endlastfoot \texttt{()} & Parentheses \\ \texttt{**} & Exponentiation \\ \texttt{+x} \texttt{-x} \texttt{\textasciitilde{}x} & Unary plus, unary @@ -132,11 +118,9 @@ operators \\ \texttt{not} & logical NOT \\ \texttt{and} & AND \\ \texttt{or} & OR \\ -\bottomrule \end{longtable} - \hypertarget{data-types}{% -\subsection{Data types}\label{data-types}} +\subsection{Data types}\label{data-types} Data types are different ways a computer stores data. Other data types use fewer bits than others allowing you to better utilize your computer @@ -149,10 +133,12 @@ The comprehensive table below show all built-in data types available in python. \begin{longtable}[]{@{}ll@{}} -\toprule +\toprule\noalign{} Category & Data Type \\ -\midrule +\midrule\noalign{} \endhead +\bottomrule\noalign{} +\endlastfoot Text & int, float, complex \\ Sequance & list, tuple, range \\ Mapping & dict \\ @@ -160,42 +146,28 @@ Set & set, frozenset \\ Boolean & bytes, bytearray, memoryview \\ Binary & bytes, bytearray, memoryview \\ None & NoneType \\ -\bottomrule \end{longtable} - \hypertarget{variables}{% -\subsection{Variables}\label{variables}} +\subsection{Variables}\label{variables} A \textbf{variable} in Python is a name that stores a value, allowing you to use and manipulate data efficiently. -\hypertarget{declaring-and-assigning-variables}{% \paragraph{Declaring and Assigning -Variables}\label{declaring-and-assigning-variables}} +Variables}\label{declaring-and-assigning-variables} It is common in low-level computer languages to declare the datatype if the variable. In python, the datatype is set whilst you assign it. We assign values to variables using a single \texttt{=}. -```python - - \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder] -\prompt{In}{incolor}{1}{\boxspacing} -\begin{Verbatim}[commandchars=\\\{\}] -\PY{n}{x} \PY{o}{=} \PY{l+m+mi}{33} \PY{c+c1}{\PYZsh{} Integer} -\PY{n}{y} \PY{o}{=} \PY{l+m+mf}{3.14} \PY{c+c1}{\PYZsh{} Float} -\PY{n}{name} \PY{o}{=} \PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{Joe}\PY{l+s+s2}{\PYZdq{}} \PY{c+c1}{\PYZsh{} String} -\PY{n}{is\PYZus{}valid} \PY{o}{=} \PY{k+kc}{True} \PY{c+c1}{\PYZsh{} Boolean} - -\PY{n+nb}{print}\PY{p}{(}\PY{n}{x}\PY{o}{*}\PY{o}{*}\PY{l+m+mi}{2}\PY{o}{+}\PY{l+m+mi}{3}\PY{o}{*}\PY{n}{y}\PY{p}{)} -\end{Verbatim} -\end{tcolorbox} - - \begin{Verbatim}[commandchars=\\\{\}] -1098.42 - \end{Verbatim} - - Change the x and y values above, re-run the cell to see what happens. +\begin{Shaded} +\begin{Highlighting}[] +\NormalTok{x }\OperatorTok{=} \DecValTok{10} \CommentTok{\# Integer} +\NormalTok{y }\OperatorTok{=} \FloatTok{3.14} \CommentTok{\# Float} +\NormalTok{name }\OperatorTok{=} \StringTok{"Joe"} \CommentTok{\# String} +\NormalTok{is\_valid }\OperatorTok{=} \VariableTok{True} \CommentTok{\# Boolean} +\end{Highlighting} +\end{Shaded} You can assign multiple variables at once: @@ -213,8 +185,7 @@ Similarly we can assign the same value to multiple variables: \end{Highlighting} \end{Shaded} - \hypertarget{rules}{% -\subparagraph{Rules}\label{rules}} +\subparagraph{Rules}\label{rules} \begin{itemize} \tightlist @@ -228,8 +199,7 @@ Similarly we can assign the same value to multiple variables: Case-sensitive (\texttt{Name} and \texttt{name} are different) \end{itemize} -\hypertarget{updating-variables}{% -\paragraph{Updating Variables}\label{updating-variables}} +\paragraph{Updating Variables}\label{updating-variables} You can change a variable's value at any time. @@ -248,37 +218,17 @@ Or shorthand: \end{Highlighting} \end{Shaded} -\hypertarget{variable-types-type-checking}{% \paragraph{Variable Types \& Type -Checking}\label{variable-types-type-checking}} +Checking}\label{variable-types-type-checking} Use \texttt{type()} to check a variable's type. - \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder] -\prompt{In}{incolor}{2}{\boxspacing} -\begin{Verbatim}[commandchars=\\\{\}] -\PY{n}{x} \PY{o}{=} \PY{l+m+mi}{10} -\PY{n+nb}{print}\PY{p}{(}\PY{l+s+sa}{f}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{ Variable x is type: }\PY{l+s+si}{\PYZob{}}\PY{n+nb}{type}\PY{p}{(}\PY{n}{x}\PY{p}{)}\PY{l+s+si}{\PYZcb{}}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)} - -\PY{n}{y} \PY{o}{=} \PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{Hello}\PY{l+s+s2}{\PYZdq{}} -\PY{n+nb}{print}\PY{p}{(}\PY{l+s+sa}{f}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{ Variable y is type: }\PY{l+s+si}{\PYZob{}}\PY{n+nb}{type}\PY{p}{(}\PY{n}{y}\PY{p}{)}\PY{l+s+si}{\PYZcb{}}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)} -\end{Verbatim} -\end{tcolorbox} - - \begin{Verbatim}[commandchars=\\\{\}] - Variable x is type: <class 'int'> - Variable y is type: <class 'str'> - \end{Verbatim} - - \hypertarget{exercise}{% -\section{Exercise}\label{exercise}} - - \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder] -\prompt{In}{incolor}{ }{\boxspacing} -\begin{Verbatim}[commandchars=\\\{\}] - -\end{Verbatim} -\end{tcolorbox} - +\begin{Shaded} +\begin{Highlighting}[] +\NormalTok{x }\OperatorTok{=} \DecValTok{10} +\BuiltInTok{print}\NormalTok{(}\BuiltInTok{type}\NormalTok{(x)) }\CommentTok{\# Output: \textless{}class \textquotesingle{}int\textquotesingle{}\textgreater{}} - % Add a bibliography block to the postdoc +\NormalTok{y }\OperatorTok{=} \StringTok{"Hello"} +\BuiltInTok{print}\NormalTok{(}\BuiltInTok{type}\NormalTok{(y)) }\CommentTok{\# Output: \textless{}class \textquotesingle{}str\textquotesingle{}\textgreater{}} +\end{Highlighting} +\end{Shaded} diff --git a/book/module1/classes_and_objects.tex b/book/module1/classes_and_objects.tex index cb9f7ec..b783048 100644 --- a/book/module1/classes_and_objects.tex +++ b/book/module1/classes_and_objects.tex @@ -13,29 +13,77 @@ or vehicles) \end{itemize} -\begin{longtable}[]{@{} - >{\raggedleft\arraybackslash}p{(\columnwidth - 0\tabcolsep) * \real{0.0556}}@{}} -\toprule\noalign{} -\begin{minipage}[b]{\linewidth}\raggedleft -\#\# 2. Core OOP Concepts - A. \textbf{Classes and Objects} - -Definitions - Syntax in Python - B. \textbf{Attributes and Methods} - -Instance variables - Functions inside classes - C. -\textbf{Encapsulation} - Public vs private variables - Using -\texttt{\_\_init\_\_} and \texttt{self} - D. \textbf{Inheritance} - -Parent and child classes - Reuse and extension of code - E. -\textbf{Polymorphism} \emph{(brief overview)} - Method overriding - -Flexibility in interfaces -\end{minipage} \\ -\midrule\noalign{} -\endhead -\bottomrule\noalign{} -\endlastfoot -\#\# 3. Python OOP Syntax and Examples - A. Define a simple class (e.g., -\texttt{Spring}) - B. Instantiate objects and use methods - C. Show -\texttt{\_\_init\_\_}, \texttt{\_\_str\_\_}, custom methods - D. Add a -derived class (e.g., \texttt{DampedSpring} inherits from -\texttt{Spring}) \\ -\end{longtable} +\subsection{2. Core OOP Concepts}\label{core-oop-concepts} + +\begin{itemize} +\tightlist +\item + A. \textbf{Classes and Objects} + + \begin{itemize} + \tightlist + \item + Definitions + \item + Syntax in Python + \end{itemize} +\item + B. \textbf{Attributes and Methods} + + \begin{itemize} + \tightlist + \item + Instance variables + \item + Functions inside classes + \end{itemize} +\item + C. \textbf{Encapsulation} + + \begin{itemize} + \tightlist + \item + Public vs private variables + \item + Using \texttt{\_\_init\_\_} and \texttt{self} + \end{itemize} +\item + D. \textbf{Inheritance} + + \begin{itemize} + \tightlist + \item + Parent and child classes + \item + Reuse and extension of code + \end{itemize} +\item + E. \textbf{Polymorphism} \emph{(brief overview)} + + \begin{itemize} + \tightlist + \item + Method overriding + \item + Flexibility in interfaces + \end{itemize} +\end{itemize} + +\subsection{3. Python OOP Syntax and +Examples}\label{python-oop-syntax-and-examples} + +\begin{itemize} +\tightlist +\item + A. Define a simple class (e.g., \texttt{Spring}) +\item + B. Instantiate objects and use methods +\item + C. Show \texttt{\_\_init\_\_}, \texttt{\_\_str\_\_}, custom methods +\item + D. Add a derived class (e.g., \texttt{DampedSpring} inherits from + \texttt{Spring}) +\end{itemize} \subsection{4. Engineering Applications of OOP}\label{engineering-applications-of-oop} @@ -57,8 +105,6 @@ OOP}\label{engineering-applications-of-oop} C. Organizing simulation code with OOP \end{itemize} -\begin{center}\rule{0.5\linewidth}{0.5pt}\end{center} - \subsection{5. Hands-On Coding Activity}\label{hands-on-coding-activity} \begin{itemize} diff --git a/book/module1/control_structures.tex b/book/module1/control_structures.tex index 746b7a5..4042e09 100644 --- a/book/module1/control_structures.tex +++ b/book/module1/control_structures.tex @@ -88,8 +88,6 @@ that information, we print different messages. \end{Highlighting} \end{Shaded} -\begin{center}\rule{0.5\linewidth}{0.5pt}\end{center} - \subsection{Loops in Python}\label{loops-in-python} Loops allow a program to execute a block of code multiple times. This is @@ -163,8 +161,6 @@ we break out of the loop. \end{Highlighting} \end{Shaded} -\begin{center}\rule{0.5\linewidth}{0.5pt}\end{center} - \subsection{Loop Control Statements}\label{loop-control-statements} Python provides special statements to control the behavior of loops. diff --git a/book/module1/functions.tex b/book/module1/functions.tex index 07da182..19a8a85 100644 --- a/book/module1/functions.tex +++ b/book/module1/functions.tex @@ -81,7 +81,7 @@ function we will have\ldots{} \begin{Shaded} \begin{Highlighting}[] -\KeywordTok{def}\NormalTok{ function\_name(argument1, argument2, argument3)} +\KeywordTok{def}\NormalTok{ function\_name(argument1, argument2, argument3):} \NormalTok{ output1 }\OperatorTok{=}\NormalTok{ argument1 }\OperatorTok{*}\NormalTok{ argument2 }\OperatorTok{{-}}\NormalTok{ argument3} \NormalTok{ output2 }\OperatorTok{=}\NormalTok{ argument2 }\OperatorTok{+}\NormalTok{ argument3} \ControlFlowTok{return}\NormalTok{ output1, output2} diff --git a/book/module1/installing_anaconda.tex b/book/module1/installing_anaconda.tex index 9b9bf1a..0ff3d6f 100644 --- a/book/module1/installing_anaconda.tex +++ b/book/module1/installing_anaconda.tex @@ -21,8 +21,6 @@ lets you create separate environments, so different projects don't interfere with each other. Additionally, Anaconda includes programs like JupyterLab for interactive coding, and Spyer a MATLAB-like IDE. -\begin{center}\rule{0.5\linewidth}{0.5pt}\end{center} - \subsection{Instructions}\label{instructions} \begin{enumerate} diff --git a/book/module1/intro_to_anaconda.tex b/book/module1/intro_to_anaconda.tex index 5e58908..bc548c9 100644 --- a/book/module1/intro_to_anaconda.tex +++ b/book/module1/intro_to_anaconda.tex @@ -19,8 +19,6 @@ indicated, Navigator is a tool in the Anaconda toolbox that allows the user to select and configure python environments and libraries. Let's see how we can do this. -\begin{center}\rule{0.5\linewidth}{0.5pt}\end{center} - \subsection{Getting Started}\label{getting-started} Note to windows 10 users: Some installation instances do not allow users diff --git a/book/module1/jupyter_lab_notebook.tex b/book/module1/jupyter_lab_notebook.tex new file mode 100644 index 0000000..9688bea --- /dev/null +++ b/book/module1/jupyter_lab_notebook.tex @@ -0,0 +1,160 @@ +\subsection{Introduction}\label{introduction} + +Jupyter Notebooks are often used for data science and scientific +computing such as machine learning as the interactive design allow you +to experiment easily with your code. For our purpose, we will use +Notebooks as it's a useful tool to learn how to code as well as writing +reports. + +\emph{Note on the difference between Notebook and Lab: Jupyter Notebook +offers a simplified, lightweight notebook authoring experience, where +as, JupyterLab offers a feature-rich, tabbed multi-notebook editing +environment with additional tools like a customizable interface layout +and system console} + +\subsection{Setup and Installation}\label{setup-and-installation} + +Installing with Conda using CLI: - \texttt{conda\ install\ jupyter} +Jupyter can also be installed using a Anaconda Navigator + +\begin{verbatim} +- Launching: + - From terminal: `jupyter notebook` or `jupyter lab` + - From Anaconda Navigator or VSCode +- Navigating the interface: + - Tabs and file browser + - Kernel and terminals +\end{verbatim} + +\subsection{Notebook Basics}\label{notebook-basics} + +\begin{itemize} +\tightlist +\item + Creating a new notebook (\texttt{.ipynb}) +\item + Types of cells: + + \begin{itemize} + \tightlist + \item + Code + \item + Markdown + \item + Raw + \end{itemize} +\item + Running a cell: \texttt{Shift\ +\ Enter} +\item + Adding/removing cells +\item + Restarting the kernel +\item + Saving and auto-checkpoints +\end{itemize} + +\subsection{Writing and Running Code}\label{writing-and-running-code} + +\begin{itemize} +\tightlist +\item + Python syntax: - \texttt{print("Hello,\ world!")} - Variables and + functions - Loops and conditionals +\item + Importing libraries: - \texttt{import\ numpy\ as\ np} - + \texttt{import\ pandas\ as\ pd} - + \texttt{import\ matplotlib.pyplot\ as\ plt} +\end{itemize} + +\subsection{Using Markdown}\label{using-markdown} + +\begin{itemize} +\tightlist +\item + Headers: \texttt{\#}, \texttt{\#\#}, \texttt{\#\#\#} +\item + Bullet lists: \texttt{-\ item} +\item + Numbered lists: \texttt{1.\ item} +\item + Emphasis: \emph{italic}, \textbf{bold}, \texttt{monospace} +\item + LaTeX equations: + + \begin{itemize} + \tightlist + \item + Inline: \texttt{\$E\ =\ mc\^{}2\$} + \item + Block: + \texttt{\$\$\textbackslash{}int\_0\^{}\textbackslash{}infty\ e\^{}\{-x\^{}2\}\ dx\ =\ \textbackslash{}frac\{\textbackslash{}sqrt\{\textbackslash{}pi\}\}\{2\}\$\$} + \end{itemize} +\item + Embedding links and images +\end{itemize} + +\subsection{Interactive Widgets +(optional)}\label{interactive-widgets-optional} + +Install \texttt{ipywidgets} from your package manager + +\begin{Shaded} +\begin{Highlighting}[] +\ImportTok{import}\NormalTok{ ipywidgets }\ImportTok{as}\NormalTok{ widgets} +\NormalTok{widgets.IntSlider()} +\end{Highlighting} +\end{Shaded} + +Example using interact: + +\begin{Shaded} +\begin{Highlighting}[] +\ImportTok{from}\NormalTok{ ipywidgets }\ImportTok{import}\NormalTok{ interact} +\NormalTok{interact(}\KeywordTok{lambda}\NormalTok{ x: x}\OperatorTok{**}\DecValTok{2}\NormalTok{, x}\OperatorTok{=}\DecValTok{5}\NormalTok{)} +\end{Highlighting} +\end{Shaded} + +\subsection{Productivity Tips}\label{productivity-tips} + +Here are some keyboard shortcuts to improve your productivity when +writing in notebooks. + +\begin{longtable}[]{@{}ll@{}} +\toprule\noalign{} +Key & Action \\ +\midrule\noalign{} +\endhead +\bottomrule\noalign{} +\endlastfoot +A & insert cell above \\ +B & insert cell below \\ +M & switch to Markdown \\ +Y & switch to code \\ +\end{longtable} + +Magic commands: - \texttt{\%timeit}, \texttt{\%matplotlib\ inline}, +\texttt{\%\%bash} Splitting and merging cells Auto-save behavior + +\subsection{Exporting and Sharing}\label{exporting-and-sharing} + +\begin{itemize} +\tightlist +\item + File \textgreater{} Download As: + + \begin{itemize} + \tightlist + \item + Notebook (\texttt{.ipynb}) + \item + HTML + \item + PDF (requires LaTeX) + \item + Markdown + \end{itemize} +\item + Notebooks can be saved and shared via the following services: - GitHub + - nbviewer.org - mybinder.org - JupyterHub +\end{itemize} diff --git a/book/module1/module1.tex b/book/module1/module1.tex index 5f4f16b..58db30b 100644 --- a/book/module1/module1.tex +++ b/book/module1/module1.tex @@ -1,4 +1,5 @@ \chapter{Module 1} +\input{module1/array} \input{module1/arrays} \input{module1/basics_of_python} \input{module1/classes_and_objects} @@ -10,5 +11,6 @@ \input{module1/installing_anaconda} \input{module1/intro_to_anaconda} \input{module1/intro_to_programming} +\input{module1/jupyter_lab_notebook} \input{module1/open_source_software} \input{module1/spyder_getting_started} diff --git a/book/module2/debugging_code.tex b/book/module2/debugging_code.tex index ce3e99e..0d244fb 100644 --- a/book/module2/debugging_code.tex +++ b/book/module2/debugging_code.tex @@ -3,7 +3,7 @@ \subsection{Introduction}\label{introduction} Have you ever had a piece of code not work the way you expected? What -did you do? You may have , asked a friend or used an AI assistant. In +did you do? You may have, asked a friend or used an AI assistant. In this section, the following concepts are introduced - definition of a bug, common types of bugs and debugging techniques. diff --git a/book/module4/linear_regression.tex b/book/module4/linear_regression.tex new file mode 100644 index 0000000..5cd81db --- /dev/null +++ b/book/module4/linear_regression.tex @@ -0,0 +1,22 @@ +\section{Linear Regression}\label{linear-regression} + +\subsection{Statical tools}\label{statical-tools} + +Numpy comes with some useful statistical tools that we can use to +analyze our data. + +\subsubsection{Mean}\label{mean} + +The mean is the average of a set of numbers. It is calculated by summing +all the numbers and dividing by the count of numbers. + +\begin{Shaded} +\begin{Highlighting}[] +\ImportTok{import}\NormalTok{ numpy }\ImportTok{as}\NormalTok{ np} + +\NormalTok{mean }\OperatorTok{=}\NormalTok{ np.mean([}\DecValTok{1}\NormalTok{, }\DecValTok{2}\NormalTok{, }\DecValTok{3}\NormalTok{, }\DecValTok{4}\NormalTok{, }\DecValTok{5}\NormalTok{])} +\NormalTok{median }\OperatorTok{=}\NormalTok{ np.median([}\DecValTok{1}\NormalTok{, }\DecValTok{2}\NormalTok{, }\DecValTok{3}\NormalTok{, }\DecValTok{4}\NormalTok{, }\DecValTok{5}\NormalTok{])} +\NormalTok{std }\OperatorTok{=}\NormalTok{ np.std([}\DecValTok{1}\NormalTok{, }\DecValTok{2}\NormalTok{, }\DecValTok{3}\NormalTok{, }\DecValTok{4}\NormalTok{, }\DecValTok{5}\NormalTok{])} +\NormalTok{variance }\OperatorTok{=}\NormalTok{ np.var([}\DecValTok{1}\NormalTok{, }\DecValTok{2}\NormalTok{, }\DecValTok{3}\NormalTok{, }\DecValTok{4}\NormalTok{, }\DecValTok{5}\NormalTok{])} +\end{Highlighting} +\end{Shaded} diff --git a/book/module4/module4.tex b/book/module4/module4.tex index 52442db..a8e0676 100644 --- a/book/module4/module4.tex +++ b/book/module4/module4.tex @@ -1,2 +1,3 @@ \chapter{Module 4} +\input{module4/linear_regression} \input{module4/plotting} |
