Module: Digiproc::CoreExtensions::ArrayExtension::DotProduct

Defined in:
lib/extensions/core_extensions.rb

Instance Method Summary collapse

Instance Method Details

#dot(arr) ⇒ Object

Take the dot product of self another Array (allow complex numbers). They must be the same size. Returns a scalar myArray.dot(anotherArray) # => Float

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
# File 'lib/extensions/core_extensions.rb', line 9

def dot(arr)
    raise ArgumentError.new("Array sizes must be equal") if self.size != arr.size
    output = []
    self.each_with_index do |o,i|
        output << o * arr[i].conjugate
    end
    output.sum
end