| def compute_dot(vector1, vector2): | |
| """ Compute dot product of two lists | |
| @brief Compute the dot product of two numeric lists by iterating over | |
| the two lists and adding the elementwise product. | |
| Ideally, numpy dot product functionality should be employed. This function | |
| can be used in cases where numpy is not accessible. | |
| Args: | |
| @param vector1 list, List of numbers | |
| @param vector2 list, List of numbers | |
| Returns: | |
| Numeric. Float or int (depending on type of input) | |
| If either of the two inputs are invalid or empty then zero is returned. | |
| If the lists are of unequal length then the longer list is truncated to | |
| the length of the shorter list and then the dot product is returned. | |
| """ | |
| if not vector1 or not vector2: | |
| return 0 | |
| # zip is used to truncate longer list to shorter length | |
| return sum(p*q for p,q in zip(vector1, vector2)) |