innerProduct method
Computes the inner product of two vectors.
This is the operation of multiplying two vectors of the same size element-wise and summing up the results, resulting in a scalar.
The vectors must have the same length, otherwise an ArgumentError is thrown.
Example:
var x = Vector([1, 2, 3]);
var y = Vector([4, 5, 6]);
double z = x.innerProduct(y);
print(z); // 32
Implementation
dynamic innerProduct(Vector other) {
// Check that the vectors have the same length
if (length != other.length) {
throw ArgumentError('Vectors must have the same length');
}
// Initialize the result to zero
dynamic result = Complex.zero();
// Loop over the elements and add their products
for (int i = 0; i < length; i++) {
result += this[i] * other[i];
}
// Return the result
return result;
}