Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Lab1 report #12

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
374 changes: 374 additions & 0 deletions Lab-1/viktorme-report.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,374 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "lab1_matrixalgorithms.ipynb",
"provenance": [],
"collapsed_sections": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "6RgtXlfYO_i7",
"colab_type": "text"
},
"source": [
"# **Lab 1: Matrix algorithms**\n",
"**Viktor Meyer - DD2363 Methods in Scientific Computing**"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "9x_J5FVuPzbm",
"colab_type": "text"
},
"source": [
"# **Abstract**"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "6UFTSzW7P8kL",
"colab_type": "text"
},
"source": [
"This lab is an exercise in matrix operations. The mandatory part includes scalar product, matrix-vector product and matrix-matrix product. There is also an extra assignment euclidean norm or euclidian distance."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "28xLGz8JX3Hh",
"colab_type": "text"
},
"source": [
"# **Environment**"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "D2PYNusD08Wa",
"colab_type": "text"
},
"source": [
"To have access to the neccessary modules you have to run this cell."
]
},
{
"cell_type": "code",
"metadata": {
"id": "Xw7VlErAX7NS",
"colab_type": "code",
"colab": {}
},
"source": [
"# Load neccessary modules.\n",
"from google.colab import files\n",
"\n",
"import time\n",
"import numpy as np\n",
"\n",
"from matplotlib import pyplot as plt\n",
"from matplotlib import tri\n",
"from matplotlib import axes\n",
"from mpl_toolkits.mplot3d import Axes3D"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "gnO3lhAigLev",
"colab_type": "text"
},
"source": [
"# **Introduction**"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "l5zMzgPlRAF6",
"colab_type": "text"
},
"source": [
"The methods to be implemented were covered in the first lectures and definitions are available in the lecture notes: LN-DD2363-part1.pdf\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "WeFO9QMeUOAu",
"colab_type": "text"
},
"source": [
"# **Methods**"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "I5Kf3qRyY5J5",
"colab_type": "text"
},
"source": [
"### Scalar Product ###"
]
},
{
"cell_type": "code",
"metadata": {
"colab_type": "code",
"outputId": "5463ca4e-73b9-40c0-bd06-958e76b35400",
"id": "PbnT724f5mfQ",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 68
}
},
"source": [
"# LN-DD2363-part1.pdf, pp. 7\n",
"x = np.array([0, 3, -7])\n",
"y = np.array([2, 3, 1])\n",
"\n",
"def scalar(x, y):\n",
" result = 0\n",
" assert(x.size == y.size)\n",
" for i in range(x.size):\n",
" result += x[i]*y[i]\n",
" \n",
" return result\n",
"\n",
"print(\"x = \", x)\n",
"print(\"y = \", y)\n",
"print('scalar(x, y) =',scalar(x, y))"
],
"execution_count": 65,
"outputs": [
{
"output_type": "stream",
"text": [
"x = [ 0 3 -7]\n",
"y = [2 3 1]\n",
"scalar(x, y) = 2\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "k9b0yn9qF_Jq"
},
"source": [
"### Matrix-Vector product ###"
]
},
{
"cell_type": "code",
"metadata": {
"colab_type": "code",
"outputId": "2ad783ee-22d5-46bb-a184-44e6036d1ee1",
"id": "401b1-yLGFaH",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 85
}
},
"source": [
"# LN-DD2363-part1.pdf, pp. 22\n",
"x = np.array([2, 1, 0])\n",
"A = np.matrix(\"1 -1 2; 0 -3 1\")\n",
"\n",
"def matrixvectorproduct(x, A):\n",
" b = np.zeros(A.shape[0])\n",
" assert(A.shape[1] == x.size)\n",
" for colIndex in range(A.shape[0]):\n",
" for rowIndex in range(A.shape[1]):\n",
" b[colIndex] += x[rowIndex]*A[colIndex, rowIndex]\n",
" return b\n",
"\n",
"print(\"x = \", x)\n",
"print(\"A = \", A)\n",
"print('matrixvectorproduct(x, A) =', matrixvectorproduct(x, A))"
],
"execution_count": 66,
"outputs": [
{
"output_type": "stream",
"text": [
"x = [2 1 0]\n",
"A = [[ 1 -1 2]\n",
" [ 0 -3 1]]\n",
"matrixvectorproduct(x, A) = [ 1. -3.]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "lVE-mL2SDDZu"
},
"source": [
"### Matrix-Matrix product ###"
]
},
{
"cell_type": "code",
"metadata": {
"colab_type": "code",
"outputId": "8e285832-68da-43ad-bde6-4f37d93b265a",
"id": "BBZekOW0DGxN",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 136
}
},
"source": [
"# LN-DD2363-part1.pdf, pp. 24\n",
"A = np.matrix(\"0 4 -2; -4 -3 0\")\n",
"B = np.matrix(\"0 1; 1 -1; 2 3\")\n",
"\n",
"def matrixmatrixproduct(A, B):\n",
" C = np.matrix(np.zeros((A.shape[0], B.shape[1])))\n",
" assert(A.shape[1] == B.shape[0])\n",
" for rowIndex in range(A.shape[0]):\n",
" for colIndex in range(B.shape[1]):\n",
" for offset in range(A.shape[1]):\n",
" C[rowIndex, colIndex] += A[rowIndex, offset]*B[offset, colIndex]\n",
" return C\n",
"\n",
"print(\"A = \", A)\n",
"print(\"B = \", B)\n",
"print('matrixmatrixproduct(A, B) =', matrixmatrixproduct(A, B))"
],
"execution_count": 67,
"outputs": [
{
"output_type": "stream",
"text": [
"A = [[ 0 4 -2]\n",
" [-4 -3 0]]\n",
"B = [[ 0 1]\n",
" [ 1 -1]\n",
" [ 2 3]]\n",
"matrixmatrixproduct(A, B) = [[ 0. -10.]\n",
" [ -3. -1.]]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "l9zEl6bG6lg3"
},
"source": [
"### Euclidian distance ###"
]
},
{
"cell_type": "code",
"metadata": {
"colab_type": "code",
"outputId": "e8351a3e-d446-481d-9bd4-5ef5eb35bff9",
"id": "b52PSKkj6nbm",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 68
}
},
"source": [
"# LN-DD2363-part1.pdf, pp. 24\n",
"x = np.array([-1, 2, 3])\n",
"y = np.array([4, 0, -3])\n",
"\n",
"def euclideandistance(x, y):\n",
" result = 0\n",
" assert(x.size == y.size)\n",
" for i in range(x.size):\n",
" result += (x[i]-y[i])*(x[i]-y[i])\n",
" result = np.sqrt(result)\n",
" return result\n",
"\n",
"print(\"x = \", x)\n",
"print(\"y = \", y)\n",
"print('euclideandistance(x, y) =', euclideandistance(x, y))"
],
"execution_count": 68,
"outputs": [
{
"output_type": "stream",
"text": [
"x = [-1 2 3]\n",
"y = [ 4 0 -3]\n",
"euclideandistance(x, y) = 8.06225774829855\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "SsQLT38gVbn_",
"colab_type": "text"
},
"source": [
"# **Results**"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "RLwlnOzuV-Cd",
"colab_type": "text"
},
"source": [
"The results appear to be correctly implemented and match what is found in e.g the examples at: \n",
"* http://tutorial.math.lamar.edu/Classes/CalcII/DotProduct.aspx\n",
"* https://mathinsight.org/matrix_vector_multiplication\n",
"* http://rosalind.info/glossary/euclidean-distance/"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "_4GLBv0zWr7m",
"colab_type": "text"
},
"source": [
"# **Discussion**"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "6bcsDSoRXHZe",
"colab_type": "text"
},
"source": [
"The results were as expected since the operations were rather simple and basic in terms of linear algebra."
]
}
]
}
Loading