import numpy as np

# Selected vector operations (numpy arrays).

def cross(a, b):	# return a cross b
    if 0:
        c = np.zeros(3)
        c[0] = a[1]*b[2] - a[2]*b[1]
        c[1] = a[2]*b[0] - a[0]*b[2]
        c[2] = a[0]*b[1] - a[1]*b[0]
        return c
    else:
        return np.cross(a,b)

def square(a):		# return |a|^2
    if 0:
        return np.dot(a,a)
    else:
        return (a*a).sum()

def vabs(a):		# return |a|
    return np.sqrt(square(a))

def dist(a, b):		# return |a-b|
    return vabs(a-b)

def hat(a):		# return unit vector parallel to a
    return a/vabs(a)

if __name__ == "__main__":

    a = np.array([1.0, 2.0, 3.0])
    b = np.array([4.0, -2.0, 1.0])
    c = np.array([-1.0, 2.0, -3.0])
    
    print('a =', a)
    print('b =', b)
    print('c =', c)
    print('a x b =', cross(a, b))
    print('|b|^2 =', square(b))
    print('|c| =', vabs(c))
    print('b.c =', np.dot(b, c))
    print('ahat =', hat(a))
