The code consists of three functions:
Spans arbitrary vector (takes dimension as an argument)
Spans vector orthogonal to the one passed in the argument
Finds cross product between two vectors.
The code is following:
def span_vector(n):
'''n represents dimension of the vector.'''
return [random.randrange(-1000,1000) for x in range(n)]
def span_orthogonal(vec):
'''vec represents n-dimensional vector'''
'''Function spans an arbitrary vector that is orthogonal to vec'''
dimension = len(vec)
orthogonal_vec = []
for k in range(dimension-1):
orthogonal_vec.append(random.randrange(-1000,1000))
last_k = ((-1)*sum([vec[x]*orthogonal_vec[x] for x in range(dimension-1)]))/vec[-1]
orthogonal_vec.append(last_k)
return orthogonal_vec
def cross_product(v1,v2):
return sum(v1[x]*v2[x] for x in range(len(v1)))
What can be improved?
EDIT The last function must read dot_product
, but not cross_product
. I made a mistake.
span_vector
andspan_orthogonal
are appropriate (sth. likegenerate_vector
should be fine); (2) if it is not required thatspan_orthogonal
returns a random vector, one can directly construct a vector orthogonal to the input from the input's coordinates. \$\endgroup\$ – GZ0 Sep 18 '19 at 14:41