diff options
| author | Christian Kolset <christian.kolset@gmail.com> | 2025-11-05 10:13:24 -0700 |
|---|---|---|
| committer | Christian Kolset <christian.kolset@gmail.com> | 2025-11-05 10:13:24 -0700 |
| commit | 397ba81d210dad7331626e4bbaccfca3b745576c (patch) | |
| tree | c5dd8180103638bf0394cd2104dc687e68d0e6df /book/module1 | |
| parent | 6a5f11936e4419866b941f35b5dfad0729008cb5 (diff) | |
Removed book/ from tracking
Diffstat (limited to 'book/module1')
| -rw-r--r-- | book/module1/1_excel_to_python.tex | 119 | ||||
| -rw-r--r-- | book/module1/array.tex | 293 | ||||
| -rw-r--r-- | book/module1/arrays.tex | 411 | ||||
| -rw-r--r-- | book/module1/basics_of_python.tex | 248 | ||||
| -rw-r--r-- | book/module1/classes_and_objects.tex | 120 | ||||
| -rw-r--r-- | book/module1/computational_expense.tex | 1 | ||||
| -rw-r--r-- | book/module1/computing_fundamentals.tex | 19 | ||||
| -rw-r--r-- | book/module1/control_structures.tex | 202 | ||||
| -rw-r--r-- | book/module1/functions.tex | 110 | ||||
| -rw-r--r-- | book/module1/fundamentals_of_programming.tex | 25 | ||||
| -rw-r--r-- | book/module1/installing_anaconda.tex | 158 | ||||
| -rw-r--r-- | book/module1/intro_to_anaconda.tex | 191 | ||||
| -rw-r--r-- | book/module1/intro_to_programming.tex | 38 | ||||
| -rw-r--r-- | book/module1/jupyter_lab_notebook.tex | 146 | ||||
| -rw-r--r-- | book/module1/module1.tex | 12 | ||||
| -rw-r--r-- | book/module1/open_source_software.tex | 104 | ||||
| -rw-r--r-- | book/module1/spyder_getting_started.tex | 107 |
17 files changed, 0 insertions, 2304 deletions
diff --git a/book/module1/1_excel_to_python.tex b/book/module1/1_excel_to_python.tex deleted file mode 100644 index 300c951..0000000 --- a/book/module1/1_excel_to_python.tex +++ /dev/null @@ -1,119 +0,0 @@ -\section{Excel to Python}\label{excel-to-python} - -\begin{itemize} -\tightlist -\item - Importing -\item - Plotting -\item - Statistical analysis -\end{itemize} - -\subsection{\texorpdfstring{\textbf{How Excel Translates to -Python}}{How Excel Translates to Python}}\label{how-excel-translates-to-python} - -Here's how common Excel functionalities map to Python: - -\begin{longtable}[]{@{} - >{\raggedright\arraybackslash}p{(\columnwidth - 2\tabcolsep) * \real{0.2911}} - >{\raggedright\arraybackslash}p{(\columnwidth - 2\tabcolsep) * \real{0.7089}}@{}} -\toprule\noalign{} -\begin{minipage}[b]{\linewidth}\raggedright -\textbf{Excel Feature} -\end{minipage} & \begin{minipage}[b]{\linewidth}\raggedright -\textbf{Python Equivalent} -\end{minipage} \\ -\midrule\noalign{} -\endhead -\bottomrule\noalign{} -\endlastfoot -Formulas (SUM, AVERAGE) & \texttt{numpy}, \texttt{pandas} -(\texttt{df.sum()}, \texttt{df.mean()}) \\ -Sorting \& Filtering & \texttt{pandas.sort\_values()}, -\texttt{df{[}df{[}\textquotesingle{}col\textquotesingle{}{]}\ \textgreater{}\ value{]}} \\ -Conditional Formatting & \texttt{matplotlib} for highlighting \\ -Pivot Tables & \texttt{pandas.pivot\_table()} \\ -Charts \& Graphs & \texttt{matplotlib}, \texttt{seaborn}, -\texttt{plotly} \\ -Regression Analysis & \texttt{scipy.stats.linregress}, -\texttt{sklearn.linear\_model} \\ -Solver/Optimization & \texttt{scipy.optimize} \\ -VBA Macros & Python scripting with \texttt{openpyxl}, \texttt{pandas}, -or \texttt{xlwings} \\ -\end{longtable} - -\subsection{Statistical functions}\label{statistical-functions} - -\paragraph{SUM}\label{sum} - -Built-in: - -\begin{Shaded} -\begin{Highlighting}[] -\NormalTok{my\_array }\OperatorTok{=}\NormalTok{ [}\DecValTok{1}\NormalTok{, }\DecValTok{2}\NormalTok{, }\DecValTok{3}\NormalTok{, }\DecValTok{4}\NormalTok{, }\DecValTok{5}\NormalTok{]} -\NormalTok{total }\OperatorTok{=} \BuiltInTok{sum}\NormalTok{(my\_array)} -\BuiltInTok{print}\NormalTok{(total) }\CommentTok{\# Output: 15} -\end{Highlighting} -\end{Shaded} - -Numpy: - -\begin{Shaded} -\begin{Highlighting}[] -\ImportTok{import}\NormalTok{ numpy }\ImportTok{as}\NormalTok{ np} - -\NormalTok{my\_array }\OperatorTok{=}\NormalTok{ np.array([}\DecValTok{1}\NormalTok{, }\DecValTok{2}\NormalTok{, }\DecValTok{3}\NormalTok{, }\DecValTok{4}\NormalTok{, }\DecValTok{5}\NormalTok{])} -\NormalTok{total }\OperatorTok{=}\NormalTok{ np.}\BuiltInTok{sum}\NormalTok{(my\_array)} -\BuiltInTok{print}\NormalTok{(total) }\CommentTok{\# Output: 15} -\end{Highlighting} -\end{Shaded} - -\subsubsection{Average}\label{average} - -Built-in: - -\begin{Shaded} -\begin{Highlighting}[] -\NormalTok{my\_array }\OperatorTok{=}\NormalTok{ [}\DecValTok{1}\NormalTok{, }\DecValTok{2}\NormalTok{, }\DecValTok{3}\NormalTok{, }\DecValTok{4}\NormalTok{, }\DecValTok{5}\NormalTok{]} -\NormalTok{average }\OperatorTok{=} \BuiltInTok{sum}\NormalTok{(my\_array) }\OperatorTok{/} \BuiltInTok{len}\NormalTok{(my\_array)} -\BuiltInTok{print}\NormalTok{(average) }\CommentTok{\# Output: 3.0} -\end{Highlighting} -\end{Shaded} - -Numpy: - -\begin{Shaded} -\begin{Highlighting}[] -\ImportTok{import}\NormalTok{ numpy }\ImportTok{as}\NormalTok{ np} - -\NormalTok{my\_array }\OperatorTok{=}\NormalTok{ np.array([}\DecValTok{1}\NormalTok{, }\DecValTok{2}\NormalTok{, }\DecValTok{3}\NormalTok{, }\DecValTok{4}\NormalTok{, }\DecValTok{5}\NormalTok{])} -\NormalTok{average }\OperatorTok{=}\NormalTok{ np.mean(my\_array)} -\BuiltInTok{print}\NormalTok{(average) }\CommentTok{\# Output: 3.0} -\end{Highlighting} -\end{Shaded} - -\subsection{Plotting}\label{plotting} - -We can use the package \emph{matplotlib} to plot our graphs in python. -Matplotlib provides data visualization tools for the Scientific Python -Ecosystem. You can make very professional looking figures with this -tool. - -Here is a section from the matplotlib documentation page that you can -run in python. - -\begin{Shaded} -\begin{Highlighting}[] -\ImportTok{import}\NormalTok{ matplotlib.pyplot }\ImportTok{as}\NormalTok{ plt} - -\NormalTok{fig, ax }\OperatorTok{=}\NormalTok{ plt.subplots() }\CommentTok{\# Create a figure containing a single Axes.} -\NormalTok{ax.plot([}\DecValTok{1}\NormalTok{, }\DecValTok{2}\NormalTok{, }\DecValTok{3}\NormalTok{, }\DecValTok{4}\NormalTok{], [}\DecValTok{1}\NormalTok{, }\DecValTok{4}\NormalTok{, }\DecValTok{2}\NormalTok{, }\DecValTok{3}\NormalTok{]) }\CommentTok{\# Plot some data on the Axes.} -\NormalTok{plt.show() }\CommentTok{\# Show the figure.} -\end{Highlighting} -\end{Shaded} - -Check out the documentation pages for a -\href{https://matplotlib.org/stable/users/explain/quick_start.html\#a-simple-example}{simple -example} or more information on the types of plots you came create -\href{https://matplotlib.org/stable/plot_types/index.html}{here}. diff --git a/book/module1/array.tex b/book/module1/array.tex deleted file mode 100644 index e442ba6..0000000 --- a/book/module1/array.tex +++ /dev/null @@ -1,293 +0,0 @@ -\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} -\end{itemize} - -\begin{figure} -\centering -\includegraphics{figures/multi-dimensional-array.png} -\caption{Mathworks 3-D array} -\end{figure} - -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{)} - -\BuiltInTok{print}\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)} - -\subsection{Exercise}\label{exercise} - -Let's solve a statics problem given the following problem - -A simply supported bridge of length L = 20 m is subjected to three point -loads: - -\begin{itemize} -\tightlist -\item - \(P_1 = 10 kN\) at \(x = 5 m\) -\item - \(P_2 = 15 kN\) at \(x = 10 m\) -\item - \(P_3 = 20 kN\) at \(x = 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}: - \(R_A + R_B - P_1 - P_2 - P_3 = 0\) -\item - \textbf{Sum of Moments About Point A}: - \(5 P_1 + 10 P_2 + 15 P_3 - 20 R_B = 0\) -\item - \textbf{Sum of Moments About Point B}: - \(20 R_A - 15 P_3 - 10 P_2 - 5 P_1 = 0\) -\end{enumerate} - -\subparagraph{System of Equations:}\label{system-of-equations} - -\[ -\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/arrays.tex b/book/module1/arrays.tex deleted file mode 100644 index a5486d9..0000000 --- a/book/module1/arrays.tex +++ /dev/null @@ -1,411 +0,0 @@ - \hypertarget{matrixarrays}{% -\section{matrixArrays}\label{matrixarrays}} - -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 -1 & 5 & 2 & 0 \\ -\midrule -\endhead -\bottomrule -\end{longtable} - -A two-dimensional array would be like a table: - -\begin{longtable}[]{@{}llll@{}} -\toprule -1 & 5 & 2 & 0 \\ -\midrule -\endhead -8 & 3 & 6 & 1 \\ -1 & 7 & 2 & 9 \\ -\bottomrule -\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} - -\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. - -\begin{center}\rule{0.5\linewidth}{0.5pt}\end{center} - -\hypertarget{numpy---the-pythons-array-library}{% -\section{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. - -\hypertarget{importing-numpy}{% -\subsection{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} - -\begin{center}\rule{0.5\linewidth}{0.5pt}\end{center} - -\hypertarget{creating-arrays}{% -\section{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.} - - \hypertarget{display-arrays}{% -\section{Display arrays}\label{display-arrays}} - -Using command print("") Accessing particular elements of an array -\ldots.. - - \hypertarget{practice-problem}{% -\section{Practice Problem}\label{practice-problem}} - -Problem statement - - \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder] -\prompt{In}{incolor}{1}{\boxspacing} -\begin{Verbatim}[commandchars=\\\{\}] -\PY{k+kn}{import} \PY{n+nn}{numpy} \PY{k}{as} \PY{n+nn}{np} - -\PY{n}{x} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{array}\PY{p}{(}\PY{p}{[}\PY{l+m+mi}{7}\PY{p}{,} \PY{l+m+mi}{10} \PY{p}{,}\PY{l+m+mi}{12}\PY{p}{]}\PY{p}{)} - -\PY{n+nb}{print}\PY{p}{(}\PY{n}{x}\PY{p}{)} - -\PY{n+nb}{print}\PY{p}{(}\PY{n}{x}\PY{p}{[}\PY{l+m+mi}{1}\PY{p}{]}\PY{p}{)} -\end{Verbatim} -\end{tcolorbox} - - \begin{Verbatim}[commandchars=\\\{\}] -[ 7 10 12] -10 - \end{Verbatim} - - \hypertarget{numpy-array-creation-functions}{% -\subsection{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. - -\hypertarget{np.arange}{% -\subsubsection{np.arange}\label{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. - -\hypertarget{np.linspace}{% -\subsubsection{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} - -\hypertarget{other-useful-functions}{% -\subsubsection{Other useful functions}\label{other-useful-functions}} - -\begin{itemize} -\tightlist -\item - \texttt{np.zeros()} -\item - \texttt{np.ones()} -\item - \texttt{np.eye()} -\end{itemize} - - \hypertarget{practice-problem}{% -\subsection{Practice problem}\label{practice-problem}} - -Problem statement below - - \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}{y}\PY{o}{=}\PY{n}{np}\PY{o}{.}\PY{n}{linspace}\PY{p}{(}\PY{l+m+mi}{10}\PY{p}{,}\PY{l+m+mi}{20}\PY{p}{,}\PY{l+m+mi}{5}\PY{p}{)} -\PY{n+nb}{print}\PY{p}{(}\PY{n}{y}\PY{p}{)} -\end{Verbatim} -\end{tcolorbox} - - \begin{Verbatim}[commandchars=\\\{\}] -[10. 12.5 15. 17.5 20. ] - \end{Verbatim} - - \hypertarget{working-with-arrays}{% -\subsection{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. - -\hypertarget{indexing}{% -\subsubsection{Indexing}\label{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. - -\hypertarget{operations-on-arrays}{% -\subsubsection{Operations on arrays}\label{operations-on-arrays}} - -\begin{itemize} -\tightlist -\item - Arithmetic operations (\texttt{+}, \texttt{-}, \texttt{*}, \texttt{/}, - \texttt{**}) -\item - \texttt{np.add()}, \texttt{np.subtract()}, \texttt{np.multiply()}, - \texttt{np.divide()} -\item - \texttt{np.dot()} for dot product -\item - \texttt{np.matmul()} for matrix multiplication -\item - \texttt{np.linalg.inv()}, \texttt{np.linalg.det()} for linear algebra -\end{itemize} - -\hypertarget{statistics}{% -\paragraph{Statistics}\label{statistics}} - -\begin{itemize} -\tightlist -\item - \texttt{np.mean()}, \texttt{np.median()}, \texttt{np.std()}, - \texttt{np.var()} -\item - \texttt{np.min()}, \texttt{np.max()}, \texttt{np.argmin()}, - \texttt{np.argmax()} -\item - Summation along axes: \texttt{np.sum(arr,\ axis=0)} -\end{itemize} - -\hypertarget{combining-arrays}{% -\paragraph{Combining arrays}\label{combining-arrays}} - -\begin{itemize} -\tightlist -\item - Concatenation: \texttt{np.concatenate((arr1,\ arr2),\ axis=0)} -\item - Stacking: \texttt{np.vstack()}, \texttt{np.hstack()} -\item - Splitting: \texttt{np.split()} -\end{itemize} - - \hypertarget{exercise}{% -\section{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=1010 kN\) at \(x=5m\) -\item - \(P2=15 kN\) at \(x=10m\) -\item - \(P3=20 kN\) at \(x=15m\) -\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. - -\hypertarget{equilibrium-equations}{% -\paragraph{Equilibrium Equations:}\label{equilibrium-equations}} - -\begin{enumerate} -\def\labelenumi{\arabic{enumi}.} -\item - \textbf{Sum of Forces in the Vertical Direction}: - - \(R_A + R_B - P_1 - P_2 - P_3 = 0\) -\item - \textbf{Sum of Moments About Point A}: - - \(5 P_1 + 10 P_2 + 15 P_3 - 20 R_B = 0\) -\item - \textbf{Sum of Moments About Point B}: - - \(20 R_A - 15 P_3 - 10 P_2 - 5 P_1 = 0\) -\end{enumerate} - -\hypertarget{system-of-equations}{% -\paragraph{System of Equations:}\label{system-of-equations}} - -\[ -\begin{cases} -R_A + R_B - 10 - 15 - 20 = 0 \\ -5 \cdot 10 + 10 \cdot 15 + 15 \cdot 20 - 20 R_B = 0 \\ -20 R_A - 5 \cdot 10 - 10 \cdot 15 - 15 \cdot 20 = 0 -\end{cases} -\] - - \hypertarget{solution}{% -\subsubsection{Solution}\label{solution}} - - \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder] -\prompt{In}{incolor}{3}{\boxspacing} -\begin{Verbatim}[commandchars=\\\{\}] -\PY{k+kn}{import} \PY{n+nn}{numpy} \PY{k}{as} \PY{n+nn}{np} - -\PY{c+c1}{\PYZsh{} Define the coefficient matrix A} -\PY{n}{A} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{array}\PY{p}{(}\PY{p}{[} - \PY{p}{[}\PY{l+m+mi}{1}\PY{p}{,} \PY{l+m+mi}{1}\PY{p}{]}\PY{p}{,} - \PY{p}{[}\PY{l+m+mi}{0}\PY{p}{,} \PY{o}{\PYZhy{}}\PY{l+m+mi}{20}\PY{p}{]}\PY{p}{,} - \PY{p}{[}\PY{l+m+mi}{20}\PY{p}{,} \PY{l+m+mi}{0}\PY{p}{]} -\PY{p}{]}\PY{p}{)} - -\PY{c+c1}{\PYZsh{} Define the right\PYZhy{}hand side vector b} -\PY{n}{b} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{array}\PY{p}{(}\PY{p}{[} - \PY{l+m+mi}{45}\PY{p}{,} - \PY{l+m+mi}{5}\PY{o}{*}\PY{l+m+mi}{10} \PY{o}{+} \PY{l+m+mi}{10}\PY{o}{*}\PY{l+m+mi}{15} \PY{o}{+} \PY{l+m+mi}{15}\PY{o}{*}\PY{l+m+mi}{20}\PY{p}{,} - \PY{l+m+mi}{5}\PY{o}{*}\PY{l+m+mi}{10} \PY{o}{+} \PY{l+m+mi}{10}\PY{o}{*}\PY{l+m+mi}{15} \PY{o}{+} \PY{l+m+mi}{15}\PY{o}{*}\PY{l+m+mi}{20} -\PY{p}{]}\PY{p}{)} - -\PY{c+c1}{\PYZsh{} Solve the system of equations Ax = b} -\PY{c+c1}{\PYZsh{} Using least squares to handle potential overdetermination} -\PY{n}{x} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{linalg}\PY{o}{.}\PY{n}{lstsq}\PY{p}{(}\PY{n}{A}\PY{p}{,} \PY{n}{b}\PY{p}{,} \PY{n}{rcond}\PY{o}{=}\PY{k+kc}{None}\PY{p}{)}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]} - -\PY{c+c1}{\PYZsh{} Display the results} -\PY{n+nb}{print}\PY{p}{(}\PY{l+s+sa}{f}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{Reaction force at A (R\PYZus{}A): }\PY{l+s+si}{\PYZob{}}\PY{n}{x}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]}\PY{l+s+si}{:}\PY{l+s+s2}{.2f}\PY{l+s+si}{\PYZcb{}}\PY{l+s+s2}{ kN}\PY{l+s+s2}{\PYZdq{}}\PY{p}{)} -\PY{n+nb}{print}\PY{p}{(}\PY{l+s+sa}{f}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{Reaction force at B (R\PYZus{}B): }\PY{l+s+si}{\PYZob{}}\PY{n}{x}\PY{p}{[}\PY{l+m+mi}{1}\PY{p}{]}\PY{l+s+si}{:}\PY{l+s+s2}{.2f}\PY{l+s+si}{\PYZcb{}}\PY{l+s+s2}{ kN}\PY{l+s+s2}{\PYZdq{}}\PY{p}{)} -\end{Verbatim} -\end{tcolorbox} - - \begin{Verbatim}[commandchars=\\\{\}] -Reaction force at A (R\_A): 25.11 kN -Reaction force at B (R\_B): -24.89 kN - \end{Verbatim} - - - % Add a bibliography block to the postdoc diff --git a/book/module1/basics_of_python.tex b/book/module1/basics_of_python.tex deleted file mode 100644 index a68400e..0000000 --- a/book/module1/basics_of_python.tex +++ /dev/null @@ -1,248 +0,0 @@ -\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. - -\subsection{Syntax}\label{syntax} - -\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. - -\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.f - -\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. - -\subsubsection{Arithmetic operators}\label{arithmetic-operators} - -\begin{longtable}[]{@{}ll@{}} -\toprule\noalign{} -Operator & Name \\ -\midrule\noalign{} -\endhead -\bottomrule\noalign{} -\endlastfoot -+ & Addition \\ -- & Subtraction \\ -* & Multiplication \\ -/ & Division \\ -\% & Modulus \\ -** & Exponentiation \\ -// & Floor division \\ -\end{longtable} - -\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 -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\noalign{} -Operator & Name \\ -\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 \\ -\end{longtable} - -\subsubsection{Logical operators}\label{logical-operators} - -\begin{longtable}[]{@{}ll@{}} -\toprule\noalign{} -Operator & Descrription \\ -\midrule\noalign{} -\endhead -\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} - -\subsubsection{Identity operators}\label{identity-operators} - -\begin{longtable}[]{@{}ll@{}} -\toprule\noalign{} -Operator & Description \\ -\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 \\ -\end{longtable} - -\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.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 -minus, and bitwise NOT \\ -\texttt{*} \texttt{/} \texttt{//} \texttt{\%} & Multiplication, -Division, floor division, and modulus \\ -\texttt{+} \texttt{-} & Addition and subtraction \\ -\texttt{\textless{}\textless{}} \texttt{\textgreater{}\textgreater{}} & -Bitwise left and right shifts \\ -\& & Bitwise AND \\ -\^{} & Bitwise XOR \\ -\textbar{} & Bitwise OR \\ -\texttt{==} \texttt{!=} \texttt{\textgreater{}} \texttt{\textgreater{}=} -\texttt{\textless{}} \texttt{\textless{}=} \texttt{is} \texttt{is\ not} -\texttt{in} \texttt{not\ in} & Comparision, identity and membership -operators \\ -\texttt{not} & logical NOT \\ -\texttt{and} & AND \\ -\texttt{or} & OR \\ -\end{longtable} - -\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 -memory. This is important for engineers because The most common data -types that an engineer encounters in python are numeric types. - -\texttt{int} - integer - \texttt{float} - a decimal number - -\texttt{complex} - imaginary number - -The comprehensive table below show all built-in data types available in -python. - -\begin{longtable}[]{@{}ll@{}} -\toprule\noalign{} -Category & Data Type \\ -\midrule\noalign{} -\endhead -\bottomrule\noalign{} -\endlastfoot -Text & int, float, complex \\ -Sequance & list, tuple, range \\ -Mapping & dict \\ -Set & set, frozenset \\ -Boolean & bytes, bytearray, memoryview \\ -Binary & bytes, bytearray, memoryview \\ -None & NoneType \\ -\end{longtable} - -\subsection{Variables}\label{variables} - -A \textbf{variable} in Python is a name that stores a value, allowing -you to use and manipulate data efficiently. - -\paragraph{Declaring and Assigning -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{=}. - -\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: - -\begin{Shaded} -\begin{Highlighting}[] -\NormalTok{a, b, c }\OperatorTok{=} \DecValTok{1}\NormalTok{, }\DecValTok{2}\NormalTok{, }\DecValTok{3} -\end{Highlighting} -\end{Shaded} - -Similarly we can assign the same value to multiple variables: - -\begin{Shaded} -\begin{Highlighting}[] -\NormalTok{x }\OperatorTok{=}\NormalTok{ y }\OperatorTok{=}\NormalTok{ z }\OperatorTok{=} \DecValTok{100} -\end{Highlighting} -\end{Shaded} - -\subparagraph{Rules}\label{rules} - -\begin{itemize} -\tightlist -\item - Must start with a letter or \texttt{\_} -\item - Cannot start with a number -\item - Can only contain letters, numbers, and \texttt{\_} -\item - Case-sensitive (\texttt{Name} and \texttt{name} are different) -\end{itemize} - -\paragraph{Updating Variables}\label{updating-variables} - -You can change a variable's value at any time. - -\begin{Shaded} -\begin{Highlighting}[] -\NormalTok{x }\OperatorTok{=} \DecValTok{5} -\NormalTok{x }\OperatorTok{=}\NormalTok{ x }\OperatorTok{+} \DecValTok{10} \CommentTok{\# Now x is 15} -\end{Highlighting} -\end{Shaded} - -Or shorthand: - -\begin{Shaded} -\begin{Highlighting}[] -\NormalTok{x }\OperatorTok{+=} \DecValTok{10} \CommentTok{\# Same as x = x + 10} -\end{Highlighting} -\end{Shaded} - -\paragraph{Variable Types \& Type -Checking}\label{variable-types-type-checking} - -Use \texttt{type()} to check a variable's type. - -\begin{Shaded} -\begin{Highlighting}[] -\NormalTok{x }\OperatorTok{=} \DecValTok{10} -\BuiltInTok{print}\NormalTok{(}\BuiltInTok{type}\NormalTok{(x)) }\CommentTok{\# Output: \textless{}class \textquotesingle{}int\textquotesingle{}\textgreater{}} - -\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 deleted file mode 100644 index b783048..0000000 --- a/book/module1/classes_and_objects.tex +++ /dev/null @@ -1,120 +0,0 @@ -\section{Modular Programming}\label{modular-programming} - -\subsection{1. Introduction}\label{introduction} - -\begin{itemize} -\tightlist -\item - A. What is Object-Oriented Programming? -\item - B. Why use OOP? (vs.~procedural) -\item - C. Real-world analogies (e.g., modeling components like pumps, motors, - or vehicles) -\end{itemize} - -\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} - -\begin{itemize} -\tightlist -\item - A. Modeling a mechanical system using classes - - \begin{itemize} - \tightlist - \item - Example: Mass-Spring-Damper system - \end{itemize} -\item - B. Creating reusable components (e.g., \texttt{Material}, - \texttt{Beam}, \texttt{Force}) -\item - C. Organizing simulation code with OOP -\end{itemize} - -\subsection{5. Hands-On Coding Activity}\label{hands-on-coding-activity} - -\begin{itemize} -\tightlist -\item - A. Write a class for a basic physical component (e.g., \texttt{Motor}) -\item - B. Add behavior (e.g., \texttt{calculate\_torque}) -\item - C. Extend with inheritance (e.g., \texttt{ServoMotor}) -\item - D. Bonus: Integrate two objects to simulate interaction -\end{itemize} diff --git a/book/module1/computational_expense.tex b/book/module1/computational_expense.tex deleted file mode 100644 index f315969..0000000 --- a/book/module1/computational_expense.tex +++ /dev/null @@ -1 +0,0 @@ -\section{Computational Expense}\label{computational-expense} diff --git a/book/module1/computing_fundamentals.tex b/book/module1/computing_fundamentals.tex deleted file mode 100644 index af38b93..0000000 --- a/book/module1/computing_fundamentals.tex +++ /dev/null @@ -1,19 +0,0 @@ -\section{Computing Fundamentals}\label{computing-fundamentals} - -\subsection{Using computers as a tool for -engineers}\label{using-computers-as-a-tool-for-engineers} - -\subsection{How do computers work?}\label{how-do-computers-work} - -Globe analogy: Hardware, Kernel, Shell, Application, Software. - -\subsection{Interfaces}\label{interfaces} - -\subsubsection{Text editor for -Scripting}\label{text-editor-for-scripting} - -\subsubsection{Command window}\label{command-window} - -Command window, terminal, console, command prompt you might've heard of -theses terms before. They all essentially mean the same thing. The -command window is used to control your system. diff --git a/book/module1/control_structures.tex b/book/module1/control_structures.tex deleted file mode 100644 index 4042e09..0000000 --- a/book/module1/control_structures.tex +++ /dev/null @@ -1,202 +0,0 @@ -\section{Control Structures}\label{control-structures} - -Control structures allow us to control the flow of execution in a Python -program. The two main types are \textbf{conditional statements -(\texttt{if} statements)} and \textbf{loops (\texttt{for} and -\texttt{while} loops)}. - -\subsection{Conditional Statements}\label{conditional-statements} - -Conditional statements allow a program to execute different blocks of -code depending on whether a given condition is \texttt{True} or -\texttt{False}. These conditions are typically comparisons, such as -checking if one number is greater than another. - -\subsubsection{\texorpdfstring{The \texttt{if} -Statement}{The if Statement}}\label{the-if-statement} - -The simplest form of a conditional statement is the \texttt{if} -statement. If the condition evaluates to \texttt{True}, the indented -block of code runs. Otherwise, the program moves on without executing -the statement. - -For example, consider a situation where we need to determine if a person -is an adult based on their age. If the age is 18 or greater, we print a -message saying they are an adult. - -\subsubsection{\texorpdfstring{The \texttt{if-else} -Statement}{The if-else Statement}}\label{the-if-else-statement} - -Sometimes, we need to specify what should happen if the condition is -\texttt{False}. The \texttt{else} clause allows us to handle this case. -Instead of just skipping over the block, the program can execute an -alternative action. - -For instance, if a person is younger than 18, they are considered a -minor. If the condition of being an adult is not met, the program will -print a message indicating that the person is a minor. - -\subsubsection{\texorpdfstring{The \texttt{if-elif-else} -Statement}{The if-elif-else Statement}}\label{the-if-elif-else-statement} - -When dealing with multiple conditions, the \texttt{if-elif-else} -structure is useful. The program evaluates conditions in order, -executing the first one that is \texttt{True}. If none of the conditions -are met, the \texttt{else} block runs. - -For example, in a grading system, different score ranges correspond to -different letter grades. If a student's score is 90 or higher, they -receive an ``A''. If it's between 80 and 89, they get a ``B'', and so -on. If none of the conditions match, they receive an ``F''. - -\subsubsection{\texorpdfstring{Nested \texttt{if} -Statements}{Nested if Statements}}\label{nested-if-statements} - -Sometimes, we need to check conditions within other conditions. This is -known as \textbf{nesting}. For example, if we first determine that a -person is an adult, we can then check if they are a student. Based on -that information, we print different messages. - -\begin{Shaded} -\begin{Highlighting}[] -\CommentTok{\# Getting user input for the student\textquotesingle{}s score} -\NormalTok{score }\OperatorTok{=} \BuiltInTok{int}\NormalTok{(}\BuiltInTok{input}\NormalTok{(}\StringTok{"Enter the student\textquotesingle{}s score (0{-}100): "}\NormalTok{))} - -\ControlFlowTok{if} \DecValTok{0} \OperatorTok{\textless{}=}\NormalTok{ score }\OperatorTok{\textless{}=} \DecValTok{100}\NormalTok{:} - \ControlFlowTok{if}\NormalTok{ score }\OperatorTok{\textgreater{}=} \DecValTok{90}\NormalTok{:} -\NormalTok{ grade }\OperatorTok{=} \StringTok{"A"} - \ControlFlowTok{elif}\NormalTok{ score }\OperatorTok{\textgreater{}=} \DecValTok{80}\NormalTok{:} -\NormalTok{ grade }\OperatorTok{=} \StringTok{"B"} - \ControlFlowTok{elif}\NormalTok{ score }\OperatorTok{\textgreater{}=} \DecValTok{70}\NormalTok{:} -\NormalTok{ grade }\OperatorTok{=} \StringTok{"C"} - \ControlFlowTok{elif}\NormalTok{ score }\OperatorTok{\textgreater{}=} \DecValTok{60}\NormalTok{:} -\NormalTok{ grade }\OperatorTok{=} \StringTok{"D"} - \ControlFlowTok{else}\NormalTok{:} -\NormalTok{ grade }\OperatorTok{=} \StringTok{"F"} \CommentTok{\# Score below 60 is a failing grade} - - - \ControlFlowTok{if}\NormalTok{ grade }\OperatorTok{==} \StringTok{"F"}\NormalTok{:} - \BuiltInTok{print}\NormalTok{(}\StringTok{"The student has failed."}\NormalTok{)} -\NormalTok{ retake\_eligible }\OperatorTok{=} \BuiltInTok{input}\NormalTok{(}\StringTok{"Is the student eligible for a retest? (yes/no): "}\NormalTok{).strip().lower()} - - \ControlFlowTok{if}\NormalTok{ retake\_eligible }\OperatorTok{==} \StringTok{"yes"}\NormalTok{:} - \BuiltInTok{print}\NormalTok{(}\StringTok{"The student is eligible for a retest."}\NormalTok{)} - \ControlFlowTok{else}\NormalTok{:} - \BuiltInTok{print}\NormalTok{(}\StringTok{"The student has failed the course and must retake it next semester."}\NormalTok{)} - - -\end{Highlighting} -\end{Shaded} - -\subsection{Loops in Python}\label{loops-in-python} - -Loops allow a program to execute a block of code multiple times. This is -especially useful for tasks such as processing lists of data, performing -repetitive calculations, or automating tasks. - -\subsubsection{\texorpdfstring{The \texttt{for} -Loop}{The for Loop}}\label{the-for-loop} - -A \texttt{for} loop iterates over a sequence, such as a list, tuple, -string, or a range of numbers. Each iteration assigns the next value in -the sequence to a loop variable, which can then be used inside the loop. - -For instance, if we have a list of fruits and want to print each fruit's -name, a \texttt{for} loop can iterate over the list and display each -item. - -Another useful feature of \texttt{for} loops is the \texttt{range()} -function, which generates a sequence of numbers. This is commonly used -when we need to repeat an action a specific number of times. For -example, iterating from 0 to 4 allows us to print a message five times. - -Additionally, the \texttt{enumerate()} function can be used to loop -through a list while keeping track of the index of each item. This is -useful when both the position and the value in a sequence are needed. - -\begin{Shaded} -\begin{Highlighting}[] -\NormalTok{fruits }\OperatorTok{=}\NormalTok{ [}\StringTok{"apple"}\NormalTok{, }\StringTok{"banana"}\NormalTok{, }\StringTok{"cherry"}\NormalTok{] } -\ControlFlowTok{for}\NormalTok{ x }\KeywordTok{in}\NormalTok{ fruits: } - \BuiltInTok{print}\NormalTok{(x)} -\end{Highlighting} -\end{Shaded} - -\begin{Shaded} -\begin{Highlighting}[] -\ControlFlowTok{for}\NormalTok{ x }\KeywordTok{in} \BuiltInTok{range}\NormalTok{(}\DecValTok{6}\NormalTok{): } - \BuiltInTok{print}\NormalTok{(x) } -\ControlFlowTok{else}\NormalTok{: } - \BuiltInTok{print}\NormalTok{(}\StringTok{"Finally finished!"}\NormalTok{)} -\end{Highlighting} -\end{Shaded} - -\subsubsection{\texorpdfstring{The \texttt{while} -Loop}{The while Loop}}\label{the-while-loop} - -Unlike \texttt{for} loops, which iterate over a sequence, \texttt{while} -loops continue running as long as a specified condition remains -\texttt{True}. This is useful when the number of iterations is not known -in advance. - -For example, a countdown timer can be implemented using a \texttt{while} -loop. The loop will continue decreasing the count until it reaches zero. - -It's important to be careful with \texttt{while} loops to avoid infinite -loops, which occur when the condition never becomes \texttt{False}. To -prevent this, ensure that the condition will eventually change during -the execution of the loop. - -A \texttt{while} loop can also be used to wait for a certain event to -occur. For example, in interactive programs, a \texttt{while\ True} loop -can keep running until the user provides a valid input, at which point -we break out of the loop. - -\begin{Shaded} -\begin{Highlighting}[] -\NormalTok{i }\OperatorTok{=} \DecValTok{1} -\ControlFlowTok{while}\NormalTok{ i }\OperatorTok{\textless{}} \DecValTok{6}\NormalTok{: } -\NormalTok{ print(i) } -\NormalTok{ i }\OperatorTok{+=} \DecValTok{1} -\end{Highlighting} -\end{Shaded} - -\subsection{Loop Control Statements}\label{loop-control-statements} - -Python provides special statements to control the behavior of loops. -These can be used to break out of a loop, skip certain iterations, or -simply include a placeholder for future code. - -\subsubsection{\texorpdfstring{The \texttt{break} -Statement}{The break Statement}}\label{the-break-statement} - -The \texttt{break} statement is used to exit a loop before it has -iterated through all its elements. When the \texttt{break} statement is -encountered, the loop stops immediately, and the program continues -executing the next statement outside the loop. - -For instance, if we are searching for a specific value in a list, we can -use a \texttt{break} statement to stop the loop as soon as we find the -item, instead of continuing unnecessary iterations. - -\subsubsection{\texorpdfstring{The \texttt{continue} -Statement}{The continue Statement}}\label{the-continue-statement} - -The \texttt{continue} statement is used to skip the current iteration -and proceed to the next one. Instead of exiting the loop entirely, it -simply moves on to the next cycle. - -For example, if we are iterating over numbers and want to skip -processing number 2, we can use \texttt{continue}. The loop will ignore -that iteration and proceed with the next number. - -\subsubsection{\texorpdfstring{The \texttt{pass} -Statement}{The pass Statement}}\label{the-pass-statement} - -The \texttt{pass} statement is a placeholder that does nothing. It is -useful when a block of code is syntactically required but no action -needs to be performed yet. - -For example, in a loop where a condition has not yet been implemented, -using \texttt{pass} ensures that the code remains valid while avoiding -errors. diff --git a/book/module1/functions.tex b/book/module1/functions.tex deleted file mode 100644 index 19a8a85..0000000 --- a/book/module1/functions.tex +++ /dev/null @@ -1,110 +0,0 @@ -\section{Functions}\label{functions} - -Like a traditional mathematical functions, python functions can take an -input, process it, and give an output. In python, the input variables -are referred to as \emph{arguments}. Functions are blocks of code that -is run every time it's called. This allows us to re-use code. - -Functions are defined by using the def keyword. Reminder: it is -important to keep track of indentations as it signifies the end of the -function when the indentation returns back to the same level. - -\subsection{Defining Functions}\label{defining-functions} - -\subsubsection{Simple function}\label{simple-function} - -A simple function with no input variable can be useful if you need to -re-use code multiple times without having to re-write it. - -\begin{Shaded} -\begin{Highlighting}[] - \KeywordTok{def}\NormalTok{ function\_name():} - \BuiltInTok{print}\NormalTok{(}\StringTok{"This is from a function"}\NormalTok{)} -\end{Highlighting} -\end{Shaded} - -\subsubsection{Defining a function with one -input}\label{defining-a-function-with-one-input} - -We can pass variables through to the function to be processed as -follows: - -\begin{Shaded} -\begin{Highlighting}[] - \KeywordTok{def}\NormalTok{ function(x):} - \BuiltInTok{print}\NormalTok{(x }\OperatorTok{+} \StringTok{" is best"}\NormalTok{)} -\end{Highlighting} -\end{Shaded} - -Note input variables can be of any data type (integer, float, string, -etc.). \#\#\# Returning values from a function If we want to calculate a -value and pass it back to the script for further use, we can use the -\texttt{return} keyword. Let's define a linear function that takes two -inputs, \texttt{x} and \texttt{b}, computes the corresponding \texttt{y} -value, and returns it so it can be used elsewhere in the code. - -\begin{Shaded} -\begin{Highlighting}[] - \KeywordTok{def}\NormalTok{ function(x, b):} -\NormalTok{ y }\OperatorTok{=} \DecValTok{3}\OperatorTok{*}\NormalTok{x}\OperatorTok{+}\NormalTok{b} - \ControlFlowTok{return}\NormalTok{ y} -\end{Highlighting} -\end{Shaded} - -For multiple output variables we can add \#\# Calling functions Now that -we've covered defining functions we want to call the function in order -to execute the block inside the function. To do this, we simply re-call -the function name as follows. - -\begin{Shaded} -\begin{Highlighting}[] -\NormalTok{function(}\DecValTok{2}\NormalTok{,}\OperatorTok{{-}}\DecValTok{1}\NormalTok{)} -\end{Highlighting} -\end{Shaded} - -Note that when running this code, nothing happens. This is because we -haven't told the computer what to do with the output. Hence, if we wish -to store the output then we need to use the assign operator \texttt{=}. - -\begin{Shaded} -\begin{Highlighting}[] -\NormalTok{output }\OperatorTok{=}\NormalTok{ function(}\DecValTok{2}\NormalTok{,}\OperatorTok{{-}}\DecValTok{1}\NormalTok{)} - -\BuiltInTok{print}\NormalTok{(output)} -\end{Highlighting} -\end{Shaded} - -In case you want to return multiple output variable from a single -function we will have\ldots{} - -\subsection{Summary}\label{summary} - -\begin{Shaded} -\begin{Highlighting}[] -\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} - -\NormalTok{[solution1, solution2] }\OperatorTok{=}\NormalTok{ function\_name(}\DecValTok{1}\NormalTok{,}\DecValTok{2}\NormalTok{,}\DecValTok{3}\NormalTok{)} -\end{Highlighting} -\end{Shaded} - -\begin{itemize} -\tightlist -\item - \texttt{def} - defines a function. All the code that is indented - underneath is considered inside the function block. -\item - \texttt{function\_name} - this is used to call the function block. -\item - \texttt{argument1} (optional) - input variable. This is data that can - be pass to the function. It is possible to have multiple variables - separated by a comma. As well as can be omitted if the function should - just give you an output such as. -\item - \texttt{return} (optional) - if you wish to return something to your - script, the return keyword is used. The keyword can be followed by an - output variable or a constant. For multiple output variables, separate - them by a comma. -\end{itemize} diff --git a/book/module1/fundamentals_of_programming.tex b/book/module1/fundamentals_of_programming.tex deleted file mode 100644 index 6a412d1..0000000 --- a/book/module1/fundamentals_of_programming.tex +++ /dev/null @@ -1,25 +0,0 @@ -\section{Fundamentals of programming}\label{fundamentals-of-programming} - -\subsection{Orientation of common -interfaces}\label{orientation-of-common-interfaces} - -In this section we will cover the use and purpose of some common -interfaces that you'll be using in this course. - -\subsubsection{Command window, terminal, console, command -prompt.}\label{command-window-terminal-console-command-prompt.} - -This is a text based interface that allows the users to interact with -the computer. It is used to execute commands, run scripts or programs. - -\subsubsection{Text Editor / Script}\label{text-editor-script} - -Your text editor is the program used to write a script which can be -re-run every time you call it from the command window. This can be a -built-in text editor such as Spyder and MATLAB provide or an external on -such a notepad++. - -\begin{verbatim} - Globe analogy: Hardware, Kernel, shell, Application software. -- Scripting -\end{verbatim} diff --git a/book/module1/installing_anaconda.tex b/book/module1/installing_anaconda.tex deleted file mode 100644 index 0ff3d6f..0000000 --- a/book/module1/installing_anaconda.tex +++ /dev/null @@ -1,158 +0,0 @@ -\section{Installing Anaconda}\label{installing-anaconda} - -This tutorial will cover the steps on how to install Anaconda. - -\emph{Note for Advanced users: For those who wish to have a lightweight -installation, can install miniconda or miniForge, however for this -course we will show you how to use Anaconda Navigator. If you've never -used the programs before then using Anaconda is recommended.} - -\subsubsection{What is Anaconda?}\label{what-is-anaconda} - -Anaconda Distribution is a popular open-source Python distribution -specifically designed for scientific computing, data science, machine -learning, and artificial intelligence applications. It simplifies the -set up and use of Python for data science, machine learning, and -scientific computing. It comes with all the important tools you need, -like NumPy, Pandas, and JupyterLab, so you don't have to install -everything separately. The Conda package manager helps you install and -update software without worrying about breaking other programs. It also -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. - -\subsection{Instructions}\label{instructions} - -\begin{enumerate} -\def\labelenumi{\arabic{enumi}.} -\item - Find the latest version of Navigator from the official Anaconda - Inc.~website: \href{https://www.anaconda.com/download}{Download - Anaconda} -\item - Press the \emph{Download Now} button. -\item - Press the \emph{Skip registration} button below the submit button, - otherwise submit your email address to subscribe to the Anaconda email - list. -\item - Under Anaconda Installers press \emph{Download} or find the - appropriate version for your operating system below. -\end{enumerate} - -Proceed to next section for your respective operating system. - -\subsubsection{Windows}\label{windows} - -\begin{enumerate} -\def\labelenumi{\arabic{enumi}.} -\setcounter{enumi}{4} -\tightlist -\item - Once the download is complete, double click the executable (.exe) file - to start the installer. Proceed with the installation instructions. -\end{enumerate} - -\begin{figure} -\centering -\includegraphics{figures/installingAnaconda_windows_installer_01_welcome.png} -\caption{Welcome screen} -\end{figure} - -\begin{figure} -\centering -\includegraphics{figures/installingAnaconda_windows_installer_02_terms.png} -\caption{Terms and conditions} -\end{figure} - -\begin{enumerate} -\def\labelenumi{\arabic{enumi}.} -\setcounter{enumi}{5} -\tightlist -\item - Select the \emph{Just Me} recommended option. -\end{enumerate} - -\begin{figure} -\centering -\includegraphics{figures/installingAnaconda_windows_installer_03_for.png} -\caption{Install for} -\end{figure} - -\begin{enumerate} -\def\labelenumi{\arabic{enumi}.} -\setcounter{enumi}{6} -\tightlist -\item - You can leave the destination folder as is, just make sure you have a - minimum of \textasciitilde5 GB available storage space. Press - \emph{Next} to proceed. -\end{enumerate} - -\begin{figure} -\centering -\includegraphics{figures/installingAnaconda_windows_installer_04_destination.png} -\caption{Installation destination} -\end{figure} - -\begin{enumerate} -\def\labelenumi{\arabic{enumi}.} -\setcounter{enumi}{7} -\tightlist -\item - It is recommended to register Anaconda3 as the default python version - if you already have an instance of python installed. Otherwise, you - can leave the checkboxes as defaults. -\end{enumerate} - -\begin{figure} -\centering -\includegraphics{figures/installingAnaconda_windows_installer_05_advanced.png} -\caption{Avanced Options} -\end{figure} - -\begin{figure} -\centering -\includegraphics{figures/installingAnaconda_windows_installer_06_installing.png} -\caption{Installing} -\end{figure} - -\begin{figure} -\centering -\includegraphics{figures/installingAnaconda_windows_installer_07_installing2.png} -\caption{Installing 2} -\end{figure} - -\begin{figure} -\centering -\includegraphics{figures/installingAnaconda_windows_installer_08_installing_complete.png} -\caption{Complete} -\end{figure} - -\begin{figure} -\centering -\includegraphics{figures/installingAnaconda_windows_installer_09_cloud.png} -\caption{Cloud} -\end{figure} - -\begin{figure} -\centering -\includegraphics{figures/installingAnaconda_windows_installer_10_finish.png} -\caption{Finish} -\end{figure} - -\begin{enumerate} -\def\labelenumi{\arabic{enumi}.} -\setcounter{enumi}{8} -\tightlist -\item - You made it! Anaconda is now installed, you are ready for launch. - Assuming that you didn't add Anaconda to PATH environment variable you - will need to start navigator from the start menu. -\end{enumerate} - -\subsubsection{Mac/Linux}\label{maclinux} - -Anaconda provides installation documentation for Mac and Linux users, -please refer to the -\href{https://docs.anaconda.com/anaconda/install/}{documentation page}. diff --git a/book/module1/intro_to_anaconda.tex b/book/module1/intro_to_anaconda.tex deleted file mode 100644 index 935b3f5..0000000 --- a/book/module1/intro_to_anaconda.tex +++ /dev/null @@ -1,191 +0,0 @@ -\section{Introduction to Anaconda -Navigator}\label{introduction-to-anaconda-navigator} - -Anaconda Navigator is a program that we will be using in this course to -manage Python environments, libraries and launch programs to help us -write our python code. - -The Anaconda website nicely describes \emph{Navigator} as: - -\emph{a graphical user interface (GUI) that enables you to work with -packages and environments without needing to type conda commands in a -terminal window.Find the packages you want, install them in an -environment, run the packages, and update them -- all inside Navigator.} - -To better understand how Navigator works and interacts with the anaconda -ecosystem see the figure below. -\includegraphics{figures/AnacondaSchematic.png} As you schematic -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. - -\subsection{Getting Started}\label{getting-started} - -Note to windows 10 users: Some installation instances do not allow users -to search the start menu for \emph{Navigator}, instead, you'll have to -find the program under the \emph{Anaconda (anaconda3)} folder. Expand -the folder and click on \emph{Anaconda Navigator} to launch the program. - -\begin{figure} -\centering -\includegraphics{figures/installingAnaconda_windows_launched.png} -\caption{Anaconda Navigator screen} -\end{figure} - -Once Navigator starts, under \emph{Home}, you'll see tiles of programs -that come with anaconda. The tab allows you to launch the programs we -will be using in this course. Before jumping straight into the programs -we will first need to configure our Python instance. - -The \emph{Environment} page allows us to install a variety of libraries -and configure our environments for different project, more on this in -the next section. - -\subsection{Environments}\label{environments} - -A Python environment can be thought of as a ``container'' where you can -have all the tools, libraries, and dependencies your Python project -needs without interfering with other projects. Think of it as a -dedicated toolbox for your project. - -Although the base environment comes with many libraries and programs -pre-installed, it's recommended to create a dedicated environment for -your projects. This protects the base environment from breaking due to -complex dependency conflicts. Let us go ahead and create a new -environment for us to use Spyder with. - -\begin{enumerate} -\def\labelenumi{\arabic{enumi}.} -\tightlist -\item - Click on the \emph{Environments} page located on the left hand side. -\end{enumerate} - -\begin{figure} -\centering -\includegraphics{figures/anaconda_environment_page.png} -\caption{Environment Page} -\end{figure} - -\begin{enumerate} -\def\labelenumi{\arabic{enumi}.} -\setcounter{enumi}{1} -\tightlist -\item - At the bottom of the environments list, click \emph{Create}. -\end{enumerate} - -\begin{figure} -\centering -\includegraphics{figures/anaconda_create_new_environment.png} -\caption{Create new environment} -\end{figure} - -\begin{enumerate} -\def\labelenumi{\arabic{enumi}.} -\setcounter{enumi}{2} -\item - Select the python checkbox. -\item - Select versions of python. At the time of making this tutorial the - latest version of Python is 3.xx.x. We will go ahead and use that one. -\item - Choose an appropriate name for your project. We will be creating an - environment for the Spyder IDE so we'll call it ``Spyder-env''. -\item - Click \emph{Create}. -\end{enumerate} - -For more information see -\href{https://docs.anaconda.com/working-with-conda/environments/}{Anaconda -Environments} and -\href{https://docs.anaconda.com/navigator/tutorials/manage-environments/}{Managing -environment}. - -\subsection{Package Management}\label{package-management} - -Now that we have a clean environment configured, let us install some -library we will be using for this class. - -\begin{enumerate} -\def\labelenumi{\arabic{enumi}.} -\tightlist -\item - Navigate to the environment page and select the environment we just - created in the previous section. -\end{enumerate} - -\begin{figure} -\centering -\includegraphics{figures/anaconda_select_package_to_manage.png} -\caption{Select environment to manage} -\end{figure} - -\begin{enumerate} -\def\labelenumi{\arabic{enumi}.} -\setcounter{enumi}{1} -\tightlist -\item - Use the search bar in the top right corner to search for the following - packages: -\end{enumerate} - -\begin{longtable}[]{@{}ll@{}} -\toprule\noalign{} -Library & Usage \\ -\midrule\noalign{} -\endhead -\bottomrule\noalign{} -\endlastfoot -numpy & Numerical computation \\ -scipy & Scientific and techical computing \\ -pandas & Data manipulation and analysis \\ -matplotlib & Plots and visualizations \\ -sympy & Symbolic mathematics \\ -\end{longtable} - -\emph{Note: The libraries list may change throughout the development of -this course} - -\begin{enumerate} -\def\labelenumi{\arabic{enumi}.} -\setcounter{enumi}{2} -\tightlist -\item - Check the boxes to install the selected packages to the current - environment. -\end{enumerate} - -\subsection{Installing Applications}\label{installing-applications} - -From the \emph{Home} page you can install applications, to the current -environment we created in the Environment section above. In this section -we will install Spyder IDE, but the process is exactly the same for -other applications. - -\begin{enumerate} -\def\labelenumi{\arabic{enumi}.} -\item - Go to the \emph{Home} page. -\item - Select the desired environment. In our case, we select - \emph{Spyder-env}. -\item - From the Home page find the Spyder IDE tile. Click the \emph{Install} - button to start the download. -\end{enumerate} - -\begin{figure} -\centering -\includegraphics{figures/anaconda_homepage.png} -\caption{Anaconda Home Page} -\end{figure} - -\begin{enumerate} -\def\labelenumi{\arabic{enumi}.} -\setcounter{enumi}{3} -\tightlist -\item - Once the download is complete, press \emph{Launch} to start the - applications. -\end{enumerate} diff --git a/book/module1/intro_to_programming.tex b/book/module1/intro_to_programming.tex deleted file mode 100644 index c73dcf4..0000000 --- a/book/module1/intro_to_programming.tex +++ /dev/null @@ -1,38 +0,0 @@ -\section{Introduction to Programming}\label{introduction-to-programming} - -\subsection{The Importance of Programming in -Engineering}\label{the-importance-of-programming-in-engineering} - -Engineering is all about solving problems, designing innovative -solutions, and making systems work efficiently. Whether you're designing -cars, airplanes, rockets, or even everyday machines, programming plays a -critical role in modern engineering. - -In mechanical engineering, programming helps us \textbf{analyze data, -model complex systems, automate repetitive tasks, and simulate -real-world physics.} For example, instead of spending hours solving -equations by hand, engineers can write a program that does it in -seconds. This saves time and therefore do more. - -With programming, mechanical engineers can: - -\begin{itemize} -\tightlist -\item - \textbf{Automate calculations:} Quickly solve equations for heat - transfer, fluid dynamics, and mechanical stresses. -\item - \textbf{Simulate systems:} Model how a bridge bends under weight or - how an engine burns fuel efficiently. -\item - \textbf{Analyze data:} Process thousands of test results to improve - designs. -\item - \textbf{Control machines:} Program robots, 3D printers, and CNC's. -\end{itemize} - -In this course, you'll see how computing and programming applies to -mechanical engineering and how they can make you a better problem -solver. By the end, you'll have the skills and understanding of how to -write programs that help you \textbf{think like an engineer in the -digital age.} diff --git a/book/module1/jupyter_lab_notebook.tex b/book/module1/jupyter_lab_notebook.tex deleted file mode 100644 index b7d9353..0000000 --- a/book/module1/jupyter_lab_notebook.tex +++ /dev/null @@ -1,146 +0,0 @@ -\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} - -Jupyter Notebooks can be installed either from the Anaconda Navigator -home page or directly from your Conda terminal. - -Terminal: \texttt{conda\ install\ conda-forge::jupyterlab} - -\subsection{Notebook Basics}\label{notebook-basics} - -Jupyter Notebooks are files which allows you to combine \emph{Code} and -\emph{Markdown} cells in one single document. The code cells, allow you -to interactively run python code and print and plot data in your -document. If you wish to update or change data your code you can re-run -the cell to update the output. The markdown cells allows you to write -text, titles and insert images in your documentation using the markup -language \emph{Markdown}. - -To start a new notebook select -\texttt{File\ \textgreater{}\ New\ \textgreater{}\ Notebook} or right -click the file browser and select \texttt{New\ notebook}, this will -prompt you to select a kernel (the Jupyter notebook ``engine''). For -now, just select the default Kernel 3. This will start a new fresh -kernel for us to use. Next, it's recommended to rename the file. - -Now that we have a blank notebook we can start to add cells. Add a cell -and change the type to Markdown. Add a title with the hash symbol -(\texttt{\#}). As shown below. - -\begin{Shaded} -\begin{Highlighting}[] -\FunctionTok{\# Title here} -\end{Highlighting} -\end{Shaded} - -Press \texttt{Shift\ +\ Enter} to run the cell. You just entered created -your first markdown cell. Now let's do the same but instead select code -as the cell type, we're going to add some python code to the document. - -\begin{Shaded} -\begin{Highlighting}[] -\NormalTok{x }\OperatorTok{=} \DecValTok{4} -\NormalTok{y }\OperatorTok{=} \DecValTok{3} - -\NormalTok{x}\OperatorTok{**}\DecValTok{2}\OperatorTok{+}\DecValTok{2}\OperatorTok{*}\NormalTok{y} -\end{Highlighting} -\end{Shaded} - -Again, run the cell and see what happens. You should've gotten an output -of \texttt{22}. You can now use the notebook as a calculator, but there -is so much more we can do. - -The order of running code matters. Think of the code cells as code -snippets. Every time you run a cell variable will be updated. This means -that the current state of all variables, functions, and imports depends -on the history of what cells have been executed and in what order. In -other words, if you run a later cell before running an earlier one that -defines a variable or function it needs, you will get an error. If you -change a variable in one cell and rerun it, that new value immediately -affects the results of any cells that use that variable afterward --- -but not any previously run results unless you rerun them too. Variables -and imports persist in memory between cells, but only based on the -current session state --- if you restart the kernel, you lose all -previous definitions unless you re-run the necessary cells. Therefore, -let's press the \texttt{Restart\ the\ kernel} button on the top window.3 - -Because of this, it's best practice to; Run cells in order, restart the -kernel and run all cells -(\texttt{Kernel\ -\textgreater{}\ Restart\ \&\ Run\ All}) to make sure -everything works cleanly and predictably and lastly, initialize -important variables or imports in early cells, so they are always -defined before they are needed. - -\subsection{Making your document look good with -Markdown}\label{making-your-document-look-good-with-markdown} - -Creating titles or headers is done with the hash symbol. The number of -hashes determines whether it's a sub-title \texttt{\#}, \texttt{\#\#}, -\texttt{\#\#\#} - -\subsubsection{Lists}\label{lists} - -There are two types of list in - Bullet lists: \texttt{-\ item} - -Numbered lists: \texttt{1.\ item} \#\#\# Style - Emphasis: -\emph{italic}, \textbf{bold}, \texttt{monospace} - -\subsubsection{Mathematical Equation}\label{mathematical-equation} - -Markdown supports LaTeX format equations. Inline equation is opened and -closed with a single \texttt{\$}. For a block math a double -\texttt{\$\$} is used instead of single. - -\begin{itemize} -\tightlist -\item - Inline: This equation is inline \texttt{\$E\ =\ mc\^{}2\$} in with the - markdown text. -\item - Block: Whilst this is a block: - \texttt{\$\$\textbackslash{}int\_0\^{}\textbackslash{}infty\ e\^{}\{-x\^{}2\}\ dx\ =\ \textbackslash{}frac\{\textbackslash{}sqrt\{\textbackslash{}pi\}\}\{2\}\$\$} -\end{itemize} - -\subsubsection{Links and images}\label{links-and-images} - -You can insert links to a different local file or online urls like this: -{[}Link{]}(file.md). I insert an image it's the same however start with -an exclamation mark \texttt{!} like this: !{[}Image -Caption{]}(picture.png) - -\subsection{Exporting and Sharing}\label{exporting-and-sharing} - -To export your notebook go to - -\texttt{File} \textgreater{} \texttt{Download\ As} - -You can then select these options. - -\begin{itemize} -\tightlist -\item - Notebook (\texttt{.ipynb}) -\item - HTML -\item - PDF (requires LaTeX) -\item - Markdown -\end{itemize} - -For homework assignments, download an HTML version of your document, -then from your browser, save or print as a PDF. Alternatively, you can -install the LaTeX typesetting system and export your document directly -as PDF from jupyter. diff --git a/book/module1/module1.tex b/book/module1/module1.tex deleted file mode 100644 index 27bf133..0000000 --- a/book/module1/module1.tex +++ /dev/null @@ -1,12 +0,0 @@ -\chapter{Module 1: Introductory Programming Concepts} -\input{module1/intro_to_anaconda} -\input{module1/jupyter_lab_notebook} -\input{module1/spyder_getting_started} -\input{module1/basics_of_python} -\input{module1/array} -\input{module1/control_structures} -\input{module1/functions} -\input{module1/classes_and_objects} -\input{module1/open_source_software} -\input{module1/1_excel_to_python} -\input{module1/computational_expense} diff --git a/book/module1/open_source_software.tex b/book/module1/open_source_software.tex deleted file mode 100644 index 96de292..0000000 --- a/book/module1/open_source_software.tex +++ /dev/null @@ -1,104 +0,0 @@ -\section{Open Source Software}\label{open-source-software} - -Open-source software (OSS) is a type of software that allows users to -access, modify, and distribute its source code freely. It is built on -principles of collaboration, transparency, and community-driven -development. - -You've probably heard of the saying ``Don't reinventing the wheel''. -This - -\subsubsection{Key Principles of Open Source -Software}\label{key-principles-of-open-source-software} - -\begin{itemize} -\tightlist -\item - \textbf{Free Distribution:} Anyone can download and use the software - without cost. -\item - \textbf{Access to Source Code:} Users can view and modify the code to - suit their needs. -\item - \textbf{Community Collaboration:} Developers from around the world - contribute to improvements and security fixes. -\end{itemize} - -\subsubsection{Benefits of Open Source -Software}\label{benefits-of-open-source-software} - -\begin{itemize} -\tightlist -\item - \textbf{Cost-effectiveness:} Open-source software is free to use, - making it accessible to individuals and organizations. -\item - \textbf{Transparency and Security:} Open code allows for peer review, - reducing security vulnerabilities. -\item - \textbf{Community Support:} Global developer communities provide - assistance, troubleshooting, and improvements. -\item - \textbf{Customization and Flexibility:} Users can modify software to - fit their specific requirements. -\end{itemize} - -\subsubsection{Challenges of Open Source -Software}\label{challenges-of-open-source-software} - -\begin{itemize} -\tightlist -\item - \textbf{Usability Issues:} Some open-source software may have a - steeper learning curve. -\item - \textbf{Compatibility Problems:} Integration with proprietary systems - may require additional effort. -\item - \textbf{Support and Documentation:} The quality of documentation and - support varies. -\item - \textbf{Sustainability:} Open-source projects often rely on - volunteers, which can lead to inconsistent updates. -\end{itemize} - -\subsubsection{Popular Open Source -Projects}\label{popular-open-source-projects} - -\begin{itemize} -\tightlist -\item - \textbf{Operating Systems:} Linux, Ubuntu -\item - \textbf{Web Browsers:} Mozilla Firefox -\item - \textbf{Programming Languages:} Python, JavaScript -\item - \textbf{Office Suites:} LibreOffice -\item - \textbf{Multimedia Tools:} Audacity, Blender -\item - \textbf{Software Development:} Git, GitHub, Apache -\end{itemize} - -\subsubsection{How to Contribute to Open -Source}\label{how-to-contribute-to-open-source} - -\begin{itemize} -\tightlist -\item - \textbf{Finding Projects:} Platforms like GitHub, GitLab, and - SourceForge host many open-source projects. -\item - \textbf{Understanding Licensing:} Common licenses include GPL, MIT, - and Apache. -\item - \textbf{Ways to Contribute:} Developers can contribute code, test - software, write documentation, translate, or help with design. -\item - \textbf{Best Practices for Contributions:} Using version control - (Git), writing clean code, and following community guidelines are - essential for successful collaboration. -\end{itemize} - -\subsection{Licensing}\label{licensing} diff --git a/book/module1/spyder_getting_started.tex b/book/module1/spyder_getting_started.tex deleted file mode 100644 index 3133b15..0000000 --- a/book/module1/spyder_getting_started.tex +++ /dev/null @@ -1,107 +0,0 @@ -\section{Getting started with Spyder}\label{getting-started-with-spyder} - -In this tutorial we will cover the basics of using the Spyder IDE -(Interactive Development Environment). If you've ever worked with MATLAB -before, then this will feel familiar. Spyder is a program that allows -you to write, run and de-bug code. - -\subsection{Launching Spyder}\label{launching-spyder} - -Using Anaconda we will select the environment we created earlier -\emph{spyder-dev} and then we can launch spyder from the Home page. - -\subsection{Spyder Interface}\label{spyder-interface} - -\begin{figure} -\centering -\includegraphics{figures/spyder_interface.png} -\caption{Spyder Interface} -\end{figure} - -Once you open up Spyder in it's default configuration, you'll see three -sections; the editor IPython Console, Help viewer. You can customize the -interface to suit your prefference and needs some of which include, -rearrange, undock, hide panes. Feel free to set up Spyder as you like. - -\subsubsection{Editor}\label{editor} - -This pane is used to write your scripts. The - -\begin{figure} -\centering -\includegraphics{figures/editor_key_components.png} -\caption{Editor key components} -\end{figure} - -\begin{enumerate} -\def\labelenumi{\arabic{enumi}.} -\tightlist -\item - The left sidebar shows line numbers and displays any code analysis - warnings that exist in the current file. Clicking a line number - selects the text on that line, and clicking to the right of it sets a - \href{https://docs.spyder-ide.org/5/panes/debugging.html\#debugging-breakpoints}{breakpoint}. -\item - The scrollbars allow vertical and horizontal navigation in a file. -\item - The context (right-click) menu displays actions relevant to whatever - was clicked. -\item - The options menu (``Hamburger'' icon at top right) includes useful - settings and actions relevant to the Editor. -\item - The location bar at the top of the Editor pane shows the full path of - the current file. -\item - The tab bar displays the names of all opened files. It also has a - Browse tabs button (at left) to show every open tab and switch between - them---which comes in handy if many are open. -\end{enumerate} - -\subsubsection{IPython Console}\label{ipython-console} - -This pane allows you to interactively run functions, do math -computations, assign and modify variables. - -\begin{figure} -\centering -\includegraphics{figures/spyder_ipython_console.png} -\caption{IPython Console} -\end{figure} - -\begin{itemize} -\tightlist -\item - Automatic code completion -\item - Real-time function calltips -\item - Full GUI integration with the enhanced Spyder - \href{https://docs.spyder-ide.org/5/panes/debugging.html}{Debugger}. -\item - The - \href{https://docs.spyder-ide.org/5/panes/variableexplorer.html}{Variable - Explorer}, with GUI-based editors for many built-in and third-party - Python objects. -\item - Display of Matplotlib graphics in Spyder's - \href{https://docs.spyder-ide.org/5/panes/plots.html}{Plots} pane, if - the Inline backend is selected under \texttt{Preferences} - \textgreater{} \texttt{IPython\ console} \textgreater{} - \texttt{Graphics} \textgreater{} \texttt{Graphics\ backend}, and - inline in the console if Mute inline plotting is unchecked under the - Plots pane's options menu. -\end{itemize} - -\subsubsection{Variable Explorer}\label{variable-explorer} - -This pane shows all the defined variables (objects) stored. This can be -used to identify the data type of variables, the size and inspect larger -arrays. Double clicking the value cell opens up a window which allowing -you to inspect the data in a spreadsheet like view. - -\begin{figure} -\centering -\includegraphics{figures/spyder_variable_explorer.png} -\caption{Variable Explorer} -\end{figure} |
