Skip to content
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
116 changes: 116 additions & 0 deletions Population Growth.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,122 @@
"Using the above math plus your matrix library, calculate how many drakes of each age I will have in 20 2-year cycles."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from matrix_math import *"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"leslie = Matrix([[0,0.25,0.6,0.8,0.15,0],\n",
"[0.7,0,0,0,0,0],\n",
"[0,0.95,0,0,0,0],\n",
"[0,0,0.9,0,0,0],\n",
"[0,0,0,0.9,0,0],\n",
"[0,0,0,0,0.5,0]])"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"Vector([[0, 0.25, 0.6, 0.8, 0.15, 0], [0.7, 0, 0, 0, 0, 0], [0, 0.95, 0, 0, 0, 0], [0, 0, 0.9, 0, 0, 0], [0, 0, 0, 0.9, 0, 0], [0, 0, 0, 0, 0.5, 0]])"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"leslie"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"Vector([10, 0, 0, 0, 0, 0])"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"drakes = Vector([10,0,0,0,0,0])\n",
"drakes"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[10, 0, 0, 0, 0, 0] Drakes in year 0 \n",
"[0.0, 7.0, 0.0, 0.0, 0.0, 0.0] Drakes in year 2 \n",
"[1.0, 0.0, 6.0, 0.0, 0.0, 0.0] Drakes in year 4 \n",
"[3.0, 1.0, 0.0, 5.0, 0.0, 0.0] Drakes in year 6 \n",
"[5.0, 2.0, 1.0, 0.0, 5.0, 0.0] Drakes in year 8 \n",
"[2.0, 3.0, 2.0, 1.0, 0.0, 2.0] Drakes in year 10 \n",
"[3.0, 1.0, 3.0, 2.0, 0.0, 0.0] Drakes in year 12 \n",
"[4.0, 2.0, 1.0, 3.0, 2.0, 0.0] Drakes in year 14 \n",
"[4.0, 3.0, 2.0, 1.0, 2.0, 1.0] Drakes in year 16 \n",
"[3.0, 2.0, 2.0, 1.0, 1.0, 1.0] Drakes in year 18 \n",
"[4.0, 2.0, 2.0, 2.0, 1.0, 0.0] Drakes in year 20 \n",
"[4.0, 3.0, 2.0, 2.0, 2.0, 0.0] Drakes in year 22 \n",
"[4.0, 3.0, 2.0, 2.0, 2.0, 1.0] Drakes in year 24 \n",
"[4.0, 3.0, 3.0, 2.0, 1.0, 1.0] Drakes in year 26 \n",
"[5.0, 3.0, 3.0, 2.0, 2.0, 0.0] Drakes in year 28 \n",
"[5.0, 3.0, 3.0, 2.0, 2.0, 1.0] Drakes in year 30 \n",
"[5.0, 3.0, 3.0, 2.0, 2.0, 1.0] Drakes in year 32 \n",
"[5.0, 3.0, 3.0, 3.0, 2.0, 1.0] Drakes in year 34 \n",
"[5.0, 3.0, 3.0, 3.0, 2.0, 1.0] Drakes in year 36 \n",
"[5.0, 4.0, 3.0, 3.0, 2.0, 1.0] Drakes in year 38 \n",
"[6.0, 4.0, 3.0, 3.0, 2.0, 1.0] Drakes in year 40 \n"
]
}
],
"source": [
"drakes = Vector([10,0,0,0,0,0])\n",
"multiplier = leslie\n",
"for i in range(0,41,2):\n",
" print('{} Drakes in year {} '.format([drake//1 for drake in drakes.my_list], i))\n",
" drakes = leslie * drakes\n",
" \n"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
This module may be used with "import matrix_math" within a python script.

test_matrix_math.py is nose and py.test compatible (run with 'nosetests' or 'py.test' respectively)

===========================
# Vector and Matrix Objects

## Description
Expand Down
112 changes: 112 additions & 0 deletions matrix_math.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
class ShapeException(Exception):
pass


class Vector():
def __init__(self, my_list):
self.my_list = my_list
self.my_shape = self.shape()

def __add__(self, other):
if self.shape() != other.shape():
raise ShapeException
return Vector([self.my_list[i] + other.my_list[i]
for i in range(self.shape()[0])])

def __sub__(self, other):
other = other * -1
return self + other

def __mul__(self, other):
if type(other) is int or type(other) is float:
return Vector([i * other for i in self.my_list])
else:
raise ValueError('Vector can only be multiplied by \
scalar (int/float)')

def __eq__(self, other):
return self.my_list == other.my_list

def __repr__(self):
return 'Vector({})'.format(self.my_list)

def shape(self):
try:
if len(self.my_list[0]) > 1:
raise ValueError('Not a 1d vector')
except TypeError:
return (len(self.my_list),)
except:
raise ValueError('Not a 1d vector')

def dot(self, other):
if self.shape() != other.shape():
raise ShapeException
return sum([self.my_list[i] * other.my_list[i]
for i in range(len(self.my_list))])

def magnitude(self):
return self.dot(self)**0.5


class Matrix():
def __init__(self, my_list):
self.my_list = my_list
self.my_shape = self.shape()

@classmethod
def create(cls, size=(2, 2), fn = lambda x, y: 1):
return Matrix([[fn(i, j) for j in range(size[0])]
for i in range(size[1])])

def __add__(self, other):
if self.shape() != other.shape():
raise ShapeException
return Matrix([self.my_list[i] + other.my_list[i]
for i in range(self.shape()[0])])

def __sub__(self, other):
other = other * -1
return self + other

def __mul__(self, other):
if type(other) is int or type(other) is float:
return Matrix([[i * other for i in row] for row in self.my_list])

elif type(other) is Vector:
if self.shape()[1] != other.shape()[0]:
raise ShapeException

step1 = [[val * other.my_list[idx] for idx, val in enumerate(row)]
for row in self.my_list]
return Vector([sum(x) for x in step1])

elif type(other) is Matrix:
if self.shape()[1] != other.shape()[0]:
raise ShapeException

y_transposed = [[row[i] for row in other.my_list]
for i in range(len(other.my_list[0]))]

return Matrix([[Vector(row).dot(Vector(col))
for col in y_transposed] for row in self.my_list])

else:
raise ValueError('Matrix can only be multiplied by '
'scalar, Vector, or Matrix')

def __eq__(self, other):
return self.my_list == other.my_list

def __repr__(self):
return 'Matrix({})'.format(self.my_list)

def shape(self):
try:
return (len(self.my_list), len(self.my_list[0]))
except (TypeError, IndexError):
raise ValueError('Not a 2d matrix')

def rotate(self):
tuple_list = list(zip(*self.my_list[::-1]))
return Matrix([list(row) for row in tuple_list])
2 changes: 2 additions & 0 deletions nt-coverage.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
nosetests --with-coverage --cover-html --cover-package=matrix_math
open cover/index.html
Loading