In [23]:
%matplotlib inline
import pylab
from numpy import array, linalg, random, sqrt, inf
from matplotlib import pyplot as plt
def plotUnitCircle(p):
""" plot some 2D vectors with p-norm < 1 """
for i in range(5000):
x = array([random.rand()*2-1,random.rand()*2-1])
if linalg.norm(x,p) < 1:
# preserve correct aspect ratio no matter what you do to your browser window
plt.gca().set_aspect('equal', adjustable='box')
pylab.plot(x[0],x[1],'o')
pylab.axis([-1.5, 1.5, -1.5, 1.5])
pylab.show()
In [24]:
plotUnitCircle(1)
In [25]:
plotUnitCircle(2)
In [21]:
linalg.norm([0.375,0.75],2)
Out[21]:
In [22]:
linalg.norm([0.375,0.75],1)
Out[22]:
In [16]:
random.rand()*2-1
Out[16]:
In [ ]: