diff --git a/ejemyr_lab1.ipynb b/ejemyr_lab1.ipynb deleted file mode 100644 index 0df87a6..0000000 --- a/ejemyr_lab1.ipynb +++ /dev/null @@ -1,449 +0,0 @@ -{ - "nbformat": 4, - "nbformat_minor": 0, - "metadata": { - "colab": { - "name": "ejemyr_lab1.ipynb", - "provenance": [], - "collapsed_sections": [], - "toc_visible": true, - "include_colab_link": true - }, - "kernelspec": { - "name": "python3", - "display_name": "Python 3" - } - }, - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "id": "view-in-github", - "colab_type": "text" - }, - "source": [ - "\"Open" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "6RgtXlfYO_i7", - "colab_type": "text" - }, - "source": [ - "# **Lab 1: Title**\n", - "**Christoffer Ejemyr**" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "9x_J5FVuPzbm", - "colab_type": "text" - }, - "source": [ - "# **Abstract**" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "OkT8J7uOWpT3", - "colab_type": "text" - }, - "source": [ - "#**About the code**" - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "Pdll1Xc9WP0e", - "colab_type": "code", - "outputId": "ce1a945e-2dae-4530-cb4d-1236274284c0", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - } - }, - "source": [ - "\"\"\"This program is a template for lab reports in the course\"\"\"\n", - "\"\"\"DD2363 Methods in Scientific Computing, \"\"\"\n", - "\"\"\"KTH Royal Institute of Technology, Stockholm, Sweden.\"\"\"\n", - "\n", - "# Copyright (C) 2019 Christoffer Ejemyr (ejemyr@kth.se)\n", - "\n", - "# This file is part of the course DD2363 Methods in Scientific Computing\n", - "# KTH Royal Institute of Technology, Stockholm, Sweden\n", - "#\n", - "# This is free software: you can redistribute it and/or modify\n", - "# it under the terms of the GNU Lesser General Public License as published by\n", - "# the Free Software Foundation, either version 3 of the License, or\n", - "# (at your option) any later version." - ], - "execution_count": 0, - "outputs": [ - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "'KTH Royal Institute of Technology, Stockholm, Sweden.'" - ] - }, - "metadata": { - "tags": [] - }, - "execution_count": 8 - } - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "28xLGz8JX3Hh", - "colab_type": "text" - }, - "source": [ - "# **Set up environment**" - ] - }, - { - "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", - "import unittest\n", - "import sys\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\n", - "\n", - "class Tests(unittest.TestCase):\n", - " @staticmethod\n", - " def check_accuracy(est, true, decimal):\n", - " np.testing.assert_almost_equal(est, true, decimal=decimal)\n", - "\n", - " @staticmethod\n", - " def check_accuracy_multiple_random(num_of_tests, generating_func, decimal):\n", - " for i in range(num_of_tests):\n", - " est, true = generating_func()\n", - " Tests.check_accuracy(est, true, decimal)\n" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "gnO3lhAigLev", - "colab_type": "text" - }, - "source": [ - "# **Introduction**" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "WeFO9QMeUOAu", - "colab_type": "text" - }, - "source": [ - "# **Methods**" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "uwVrMhGQ4viZ", - "colab_type": "text" - }, - "source": [ - "### Scalar product" - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "jqrNhl6Np2Ze", - "colab_type": "code", - "colab": {} - }, - "source": [ - "def scalar_product(x, y):\n", - " if type(x) != np.ndarray:\n", - " try:\n", - " x = np.array(x)\n", - " except:\n", - " raise Exception(\"Vector format error.\\n\" + \"x: \" + str(x) + \"\\ny: \" + str(y))\n", - " if type(y) != np.ndarray:\n", - " try:\n", - " y = np.array(y)\n", - " except:\n", - " raise Exception(\"Vector format error.\\n\" + \"x: \" + str(x) + \"\\ny: \" + str(y))\n", - "\n", - " if x.ndim != 1 or y.ndim != 1:\n", - " raise Exception(\"Vector dimentions error.\\n\" + \"x: \" + str(x) + \"\\ny: \" + str(y))\n", - " if x.size != y.size:\n", - " raise Exception(\"Vector formats don't agree.\\n\" + \"x: \" + str(x) + \"\\ny: \" + str(y))\n", - " \n", - " sum = 0\n", - " for i in range(len(x)):\n", - " sum += x[i] * y[i]\n", - "\n", - " return sum" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "id": "CDxXDYtrrQ-a", - "colab_type": "code", - "colab": {} - }, - "source": [ - "class Tests(Tests):\n", - " def test_scalar_product(self):\n", - " with self.assertRaises(Exception):\n", - " scalar_product([1,2], [1,2,3])\n", - " with self.assertRaises(Exception):\n", - " scalar_product([[0,0],[0,0]], [[0,0],[0,0]])\n", - " with self.assertRaises(Exception):\n", - " scalar_product([\"s\",2], [1,3])\n", - " with self.assertRaises(Exception):\n", - " scalar_product(\"Hej\", [1,2,3])\n", - "\n", - " min_length = 1\n", - " max_length = 100\n", - " def genetator():\n", - " n = np.random.randint(min_length, max_length)\n", - " a = np.random.rand(n)\n", - " b = np.random.rand(n)\n", - " return scalar_product(a, b), a.dot(b)\n", - "\n", - " Tests.check_accuracy_multiple_random(1000, genetator, 7)" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "FNWnhrnD4jmh", - "colab_type": "text" - }, - "source": [ - "### Matrix-vector product" - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "j7rPWYYP4iUV", - "colab_type": "code", - "colab": {} - }, - "source": [ - "def matrix_vector_product(M, x):\n", - " if type(M) != np.ndarray:\n", - " try:\n", - " M = np.array(M)\n", - " except:\n", - " raise Exception(\"Matrix format error.\\n\" + \"M: \" + str(M))\n", - " if type(x) != np.ndarray:\n", - " try:\n", - " x = np.array(x)\n", - " except:\n", - " raise Exception(\"Vector format error.\\n\" + \"x: \" + str(x))\n", - "\n", - " if x.ndim != 1 or M.ndim != 2:\n", - " raise Exception(\"Matrix or vector dimentions error.\\n\" + \"M: \" + str(M) + \"\\nx: \" + str(x))\n", - " if M.shape[1] != x.size:\n", - " raise Exception(\"Matrix and vector formats don't agree.\\n\" + \"M: \" + str(M) + \"\\nx: \" + str(x))\n", - "\n", - " b = np.zeros(M.shape[0])\n", - " for i in range(M.shape[0]):\n", - " for j in range(M.shape[1]):\n", - " b[i] += M[i, j] * x[j]\n", - "\n", - " return b" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "id": "53zAuKI06Ona", - "colab_type": "code", - "colab": {} - }, - "source": [ - "class Tests(Tests):\n", - " def test_matrix_vector_product(self):\n", - " with self.assertRaises(Exception):\n", - " scalar_product([[0,0],[0,0]], [1,2,3])\n", - " with self.assertRaises(Exception):\n", - " scalar_product([[0,0],[0,0]], [\"Hej\", \"Hå\"])\n", - "\n", - " min_length = 1\n", - " max_length = 100\n", - " def genetator():\n", - " n = np.random.randint(min_length, max_length)\n", - " m = np.random.randint(min_length, max_length)\n", - " x = np.random.rand(n)\n", - " M = np.random.rand(m, n)\n", - " return matrix_vector_product(M, x), M.dot(x)\n", - "\n", - " Tests.check_accuracy_multiple_random(1000, genetator, 7)" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "mEWh21BX8mzG", - "colab_type": "text" - }, - "source": [ - "### Matrix-Matrix product" - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "2UAZ0QxV8mLb", - "colab_type": "code", - "colab": {} - }, - "source": [ - "def matrix_matrix_product(A, B):\n", - " def format_test(M, name):\n", - " if type(M) != np.ndarray:\n", - " try:\n", - " M = np.array(M)\n", - " except:\n", - " raise Exception(\"Matrix format error.\\n\" + name + \": \" + str(M))\n", - "\n", - " if M.ndim != 2:\n", - " raise Exception(\"Matrix dimentions error.\\n\" + name + \": \" + str(M))\n", - " format_test(A, \"A\")\n", - " format_test(B, \"B\")\n", - " \n", - " if A.shape[1] != B.shape[0]:\n", - " raise Exception(\"Matrix formats don't agree.\\n\" + \"A: \" + str(A) + \"\\nB: \" + str(B))\n", - " \n", - " \n", - " C = np.zeros((A.shape[0], B.shape[1]))\n", - " for i in range(A.shape[0]):\n", - " for j in range(B.shape[1]):\n", - " for k in range(A.shape[1]):\n", - " C[i, j] += A[i, k] * B[k, j]\n", - "\n", - " return C" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "id": "bI2zuBOP9lIx", - "colab_type": "code", - "colab": {} - }, - "source": [ - "class Tests(Tests):\n", - " def test_matrix_matrix_product(self):\n", - " with self.assertRaises(Exception):\n", - " scalar_product([[0,0,0],[0,0,0]], [[0,0],[0,0]])\n", - " with self.assertRaises(Exception):\n", - " scalar_product([[0,0],[0,0]], [[\"Hej\"], [\"Hå\"]])\n", - "\n", - " min_length = 1\n", - " max_length = 100\n", - " def genetator():\n", - " i = np.random.randint(min_length, max_length)\n", - " j = np.random.randint(min_length, max_length)\n", - " k = np.random.randint(min_length, max_length)\n", - " A = np.random.rand(i, j)\n", - " B = np.random.rand(j, k)\n", - " return matrix_matrix_product(A, B), A.dot(B)\n", - "\n", - " Tests.check_accuracy_multiple_random(100, genetator, 7)" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "SsQLT38gVbn_", - "colab_type": "text" - }, - "source": [ - "# **Results**" - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "G1hVxfti4Ib-", - "colab_type": "code", - "outputId": "159525fa-e6f3-4acb-c401-f8b9d8ab9928", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 102 - } - }, - "source": [ - "suite = unittest.TestSuite()\n", - "suite.addTest(Tests('test_scalar_product'))\n", - "suite.addTest(Tests('test_matrix_vector_product'))\n", - "suite.addTest(Tests('test_matrix_matrix_product'))\n", - "\n", - "if __name__ == '__main__':\n", - " runner = unittest.TextTestRunner()\n", - " runner.run(suite)" - ], - "execution_count": 0, - "outputs": [ - { - "output_type": "stream", - "text": [ - "...\n", - "----------------------------------------------------------------------\n", - "Ran 3 tests in 13.451s\n", - "\n", - "OK\n" - ], - "name": "stderr" - } - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "_4GLBv0zWr7m", - "colab_type": "text" - }, - "source": [ - "# **Discussion**" - ] - } - ] -} \ No newline at end of file