def project(v, w): """return the projection of v onto the direction w""" projection_length = dot(v, w) return scalar_multiply(projection_length, w)
def directional_variance_i(x_i, w): """the variance of the row x_i in the direction determined by w""" return dot(x_i, direction(w)) ** 2
def directional_variance_gradient_i(x_i, w): """the contribution of row x_i to the gradient of the direction-w variance""" projection_length = dot(x_i, direction(w)) return [ 2 * projection_length * x_ij for x_ij in x_i]
def transform_vector(v, components): return [dot(v, w) for w in components]