diff options
Diffstat (limited to 'book/module1/array.tex')
| -rw-r--r-- | book/module1/array.tex | 293 |
1 files changed, 0 insertions, 293 deletions
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} |
