In [2]:
from matplotlib import pylab as plt
import numpy as np
import math
%matplotlib inline

Defining Vector and its operations


In [3]:
class Vector(object):
    def __init__(self, x, y):
        super(Vector, self).__init__()
        self.x = float(x)
        self.y = float(y)
        
    def magnitude(self):
        return math.sqrt(self.x ** 2 + self.y **2)
    
    def direction(self):
        """
        calculate direction of this vector and returns it as unit vector
        """
        magnitude = self.magnitude()
        return Vector(self.x / magnitude, self.y / magnitude)
    
    def __add__(self, v):
        return Vector(self.x + v.x, self.y + v.y)
    
    def __sub__(self, v):
        return Vector(self.x - v.x, self.y - v.y)
    
    def __mul__(self, v):
        print "mul"
        return (self.x * v.x) + (self.y * v.y)
    
    def __rmul__(self, v):
        return Vector(v * self.x, v * self.y)
    
    def __str__(self):
        return "vector({}, {})".format(self.x, self.y)

Plotting vector


In [4]:
def drawVector(v, s="b-"):
    if v.x < 0:
        x_axis = np.arange(v.x, 1)
    else:    
        x_axis = np.arange(0, v.x+1)
    
    #print x_axis
    plt.plot(x_axis, x_axis * float(v.y)/float(v.x), s)
    plt.xticks(range(-12, 12))
    plt.yticks(range(-6, 12))
    plt.rcParams["figure.figsize"] = [12, 9]

Unit vector


In [244]:
drawVector(Vector(3, 9), "b-")
drawVector(Vector(3, 9).direction(), "r-") # unit vector
plt.grid(True)
plt.title("the red one is the unit vector of the blue vector")
print Vector(3, 9).direction()
plt.show()


vector(0.316227766017, 0.948683298051)

Addition and subtraction


In [204]:
v1 = Vector(8, 3)
v2 = Vector(2, 4)
a = v1 + v2
s1 = v1 - v2
s2 = v2 - v1

drawVector(v1, "y-")
drawVector(v2, "g-")
drawVector(a, "b--")
drawVector(s1, "r--")
drawVector(s2, "c--")

plt.grid()



In [205]:
v1 = Vector(3, 4)
v2 = Vector(2, -2)
a = v1 + v2
s1 = v1 - v2
s2 = v2 - v1

drawVector(v1, "y-")
drawVector(v2, "g-")
drawVector(a, "b--")
drawVector(s1, "r--")
drawVector(s2, "c--")

plt.grid()


Dot product and projection


In [206]:
v1 = Vector(7, 0)
v2 = Vector(8, 4)

u = v1.direction()
z = (u * v2) * u

drawVector(v1, "b-")
drawVector(v2, "g-")
drawVector(z, "r--")
plt.title("projection of te green vector on the blue vector")
plt.grid()
print v1* v2



In [255]:
v1 = Vector(8, 2)
v2 = Vector(3, 5)

u = v1.direction()
z = (u * v2) * u

drawVector(v1, "b-")
drawVector(v2, "g-")
drawVector(z, "r-")
drawVector(z - v2, "c--")
plt.title("projection of te blue vector on the green vector")
plt.show()
plt.grid()



In [336]:
v1 = Vector(2, 1)
v2 = Vector(-5, 7)

u = v1.direction()
p = (u * v2) * u
print (v1 * v2)
drawVector(v1, "b-")
drawVector(v2, "g-")
drawVector(p, "r--")

plt.grid()


mul
mul
-1.3416407865

In [6]:
x1 = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
y1 = [12.0, 18.0, 14.0, 8.0, 10.0, 6.0]

x2 = [4, 5, 7, 8]
y2 = [47, 37, 22, 24]
plt.plot(x1, y1, "o", color="yellow")
plt.plot(x2, y2, "x", color="green")

plt.plot([4], [40], "x", color="red")
plt.plot([2], [22], "o", color="red")

#plt.plot(range(9),[ i * - 4.6 + 54 for i in range(9)], ":", color="blue")
plt.plot(range(9),[ i * - 4.6 + 44 for i in range(9)], color="blue")
#plt.plot(range(8),[ i * - 4.6 + 34 for i in range(8)], ":", color="blue")
plt.title("Figure 3: maximum margin")


Out[6]:
<matplotlib.text.Text at 0x1041d51d0>

In [8]:
5, 5 * - 4.6 + 44


Out[8]:
(5, 21.0)

In [28]:
w = Vector(2, 2)
v1 = Vector(3, 4)

u = w.direction()
print u
p = (u * v1) * u
#print (v1 * v2)
drawVector(w, "b-")
drawVector(v1, "g-")
drawVector(p, "r--")
plt.plot(range(-7, 7), [-i * 1.  for i in range(-7, 7)], "k")
plt.grid()


vector(0.707106781187, 0.707106781187)
mul

In [ ]: