Skip to content

Commit

Permalink
Matrix Computations algorithms, chapter one added.
Browse files Browse the repository at this point in the history
  • Loading branch information
Emanuel D R Sena committed Sep 28, 2017
1 parent 946bd97 commit 1d4380d
Show file tree
Hide file tree
Showing 2 changed files with 414 additions and 0 deletions.
313 changes: 313 additions & 0 deletions Chapter One.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,313 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Matrix Multiplication"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Variables for tests"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Constants for tests"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"c_x = np.random.randn(1, 1)\n",
"c_y = np.random.randn(1, 1)\n",
"c_z = np.random.randn(1, 1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Vectors for test"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"v_x = np.random.randn(1000, 1)\n",
"v_y = np.random.randn(1000, 1)\n",
"v_z = np.random.randn(1000, 1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Matrices for test"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"m_x = np.random.randn(1000, 1000)\n",
"m_y = np.random.randn(1000, 1000)\n",
"m_z = np.random.randn(1000, 1000)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Algorithm Implementation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Algorithm 1.1.1: Dot Product"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def dot_product(x, y):\n",
" c = 0\n",
" \n",
" for i in np.arange(x.size):\n",
" c = c + x[i]*y[i]\n",
" \n",
" return c"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Algorithm 1.1.2: Saxpy"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def saxpy(a, x, y):\n",
" for i in np.arange(x.size):\n",
" y[i] = y[i] + a*x[i]\n",
" \n",
" return y"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Testing algorithms 1.1.1 and 1.1.2"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 0 ns, sys: 0 ns, total: 0 ns\n",
"Wall time: 1.49 ms\n"
]
}
],
"source": [
"# Test Algorithm 1.1.1 (Dot Product)\n",
"%time temp = dot_product(v_x, v_y)"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 4 ms, sys: 0 ns, total: 4 ms\n",
"Wall time: 3.92 ms\n"
]
}
],
"source": [
"# Test Algorithm 1.1.2 (Saxpy)\n",
"%time temp = saxpy(c_x, v_x, v_y)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Matrix-Vector Multiplication and the Gaxpy"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Algorithm 1.1.3: Row-Oriented Gaxpy"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [],
"source": [
"def row_gaxpy(A, x, y):\n",
" for i in np.arange(y.size):\n",
" for j in np.arange(x.size):\n",
" y[i] = y[i] + A[i, j]*x[j]\n",
" \n",
" return y"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Algorithm 1.1.4: Column-Oriented Gaxpy"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [],
"source": [
"def column_gaxpy(A, x, y):\n",
" for j in np.arange(x.size):\n",
" for i in np.arange(y.size):\n",
" y[i] = y[i] + A[i, j]*x[j]\n",
" \n",
" return y"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"#### Testing algorithms 1.1.3 and 1.1.4"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 1.89 s, sys: 0 ns, total: 1.89 s\n",
"Wall time: 1.89 s\n"
]
}
],
"source": [
"# Test Row-Oriented Gaxpy\n",
"%time temp = row_gaxpy(m_x, v_x, v_y)"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 1.96 s, sys: 24 ms, total: 1.98 s\n",
"Wall time: 1.95 s\n"
]
}
],
"source": [
"# Test Column-Oriented Gaxpy\n",
"%time temp = column_gaxpy(m_x, v_x, v_y)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [default]",
"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.5.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading

0 comments on commit 1d4380d

Please sign in to comment.