{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Working with two-dimensional arrays\n",
"\n",
"
"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"\n",
"Numpy enables you do to matrix calculations on two-dimensional arrays. In exercise, you will practice doing matrix calculations on arrays. We'll start by making a matrix and a vector to practice with. You can copy and paste the code below."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"A = np.array(\n",
" [\n",
" [6.7, 1.3, 0.6, 0.7],\n",
" [0.1, 5.5, 0.4, 2.4],\n",
" [1.1, 0.8, 4.5, 1.7],\n",
" [0.0, 1.5, 3.4, 7.5],\n",
" ]\n",
")\n",
"\n",
"b = np.array([1.1, 2.3, 3.3, 3.9])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**a)** First, let's practice slicing.\n",
"\n",
"1. Print row 1 (remember, indexing starts at zero) of `A`.\n",
"2. Print columns 1 and 3 of `A`.\n",
"3. Print the values of every entry in `A` that is greater than 2.\n",
"4. Print the diagonal of `A`. using the `np.diag()` function.\n",
"\n",
"**b)** The `np.linalg` module has some powerful linear algebra tools. \n",
"\n",
"1. First, we'll solve the linear system $\\mathsf{A}\\cdot \\mathbf{x} = \\mathbf{b}$. Try it out: use `np.linalg.solve()`. Store your answer in the Numpy array `x`.\n",
"2. Now do `np.dot(A, x)` to verify that $\\mathsf{A}\\cdot \\mathbf{x} = \\mathbf{b}$.\n",
"3. Use `np.transpose()` to compute the transpose of `A`.\n",
"4. Use `np.linalg.inv()` to compute the inverse of `A`.\n",
"\n",
"**c)** Sometimes you want to convert a two-dimensional array to a one-dimensional array. This can be done with `np.ravel()`. \n",
"\n",
"1. See what happens when you do `B = np.ravel(A)`.\n",
"2. Look of the documentation for `np.reshape()`. Then, reshape `B` to make it look like `A` again."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}