#Adapted from
#http://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html#surface-plots

from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter

from matplotlib.pyplot import *
from numpy import *
from matplotlib.colors import ListedColormap        

close('all')



def normalize_color_range(coefs):
    min_c = min(coefs.flatten())
    max_c = max(coefs.flatten())
    return 255*(coefs-min_c)/(max_c-min_c)

def get_quantized_colors(coefs):
    q = zeros((coefs.shape[0], 1))
    for i in range(coefs.shape[0]):
        q[i] = quantize_color(coefs[i,:])
    

def get_vecs(V, coefs):
    #dot for 2D arrays is matrix multiplication in numpy
    return dot(V, coefs.T)
    
#The same as above, but with less matrix multiplication magic
def get_vecs_slow(V, coefs):
    res = zeros((coefs.shape[1], coefs.shape[0]))
    for i in range(coefs.shape[0]):     #for every triple of coefficients
        for j in range(coefs.shape[1]): #add up c_0 v_0 , c_1 v_1, c_2 v_2
            res[:, i] += coefs[i][j]*V[:, j]
        
    return res    

#linearly dependent basis vectors

fig = figure()
ax = fig.gca(projection='3d')


V = array([[1 ,  0, 1],
           [-1,  1, 0],
           [2 , -1, 1]])



Npoints = 5000
coefs = 10*(random.rand(Npoints, V.shape[1])-0.5) #range: -1.5, 1.5

vecs = get_vecs(V, coefs)



ax.scatter(vecs[0,:], vecs[1,:], vecs[2,:], c=normalize_color_range(coefs)/256)

show()






#linearly independent basis vectors
fig = figure()
ax = fig.gca(projection='3d')

V = array([[1 ,  0, -1],
           [-1,  1, -1],
           [2 , -1, 5]])



coefs = 10*(random.rand(Npoints, V.shape[1])-0.5) #range: -1.5, 1.5
vecs = get_vecs(V, coefs)

ax.scatter(vecs[0,:], vecs[1,:], vecs[2,:], c=normalize_color_range(coefs)/256)
show()


#linearly independent basis vectors
fig = figure()
ax = fig.gca(projection='3d')


V = array([[1 , 0, 0],
           [0,  1, 0],
           [0 , 0, 1]])



coefs = 10*(random.rand(Npoints, V.shape[1])-0.5) #range: -1.5, 1.5
vecs = get_vecs(V, coefs)

ax.scatter(vecs[0,:], vecs[1,:], vecs[2,:], c=normalize_color_range(coefs)/256)
show()