module ArrayArithmetic
Constants
- VERSION
Public Instance Methods
add(arr_one, arr_two)
click to toggle source
Adds the values of corresponding indices of two arrays
# File lib/array_arithmetic.rb, line 7 def add(arr_one, arr_two) raise ArgumentError, 'First argument is not an array' unless arr_one.is_a? Array result = [] arr_two = arr_two.class != Array ? make_array(arr_two) : arr_two if arr_one.length != arr_two.length length_matched_arrays = update_array_length(arr_one, arr_two) length_matched_arrays[0].each_with_index do |value, index| result << value + length_matched_arrays[1][index] end else arr_one.each_with_index do |value, index| result << value + arr_two[index] end end return result end
divide(arr_one, arr_two, sig_dig=2)
click to toggle source
Divides the values of corresponding indices of two arrays Returns values as a float
# File lib/array_arithmetic.rb, line 80 def divide(arr_one, arr_two, sig_dig=2) raise ArgumentError, 'First argument is not an array' unless arr_one.is_a? Array result = [] arr_two = arr_two.class != Array ? make_array(arr_two) : arr_two if arr_one.length != arr_two.length length_matched_arrays = update_array_length(arr_one, arr_two) length_matched_arrays[0].each_with_index do |value, index| if length_matched_arrays[1][index] == 0 result << "Divide by 0 error" else result << (value.to_f / length_matched_arrays[1][index]).round(sig_dig) end end else arr_one.each_with_index do |value, index| if arr_two[index] == 0 result << "Divide by 0 error" else result << (value.to_f / arr_two[index]).round(sig_dig) end end end return result end
exponent(arr_one, arr_two)
click to toggle source
Raises the values of arr_one to the power of the value at corresponding indices in arr_two
# File lib/array_arithmetic.rb, line 144 def exponent(arr_one, arr_two) raise ArgumentError, 'First argument is not an array' unless arr_one.is_a? Array result = [] arr_two = arr_two.class != Array ? make_array(arr_two) : arr_two if arr_one.length != arr_two.length length_matched_arrays = update_array_length(arr_one, arr_two) length_matched_arrays[0].each_with_index do |value, index| result << value ** length_matched_arrays[1][index] end else arr_one.each_with_index do |value, index| result << value ** arr_two[index] end end return result end
multiply(arr_one, arr_two)
click to toggle source
Multiplies the values of corresponding indices of two arrays
# File lib/array_arithmetic.rb, line 55 def multiply(arr_one, arr_two) raise ArgumentError, 'First argument is not an array' unless arr_one.is_a? Array result = [] arr_two = arr_two.class != Array ? make_array(arr_two) : arr_two if arr_one.length != arr_two.length length_matched_arrays = update_array_length(arr_one, arr_two) length_matched_arrays[0].each_with_index do |value, index| result << value * length_matched_arrays[1][index] end else arr_one.each_with_index do |value, index| result << value * arr_two[index] end end return result end
remainder(arr_one, arr_two, sig_dig=2)
click to toggle source
Calculates the remainder of corresponding inidices of two arrays
# File lib/array_arithmetic.rb, line 112 def remainder(arr_one, arr_two, sig_dig=2) raise ArgumentError, 'First argument is not an array' unless arr_one.is_a? Array result = [] arr_two = arr_two.class != Array ? make_array(arr_two) : arr_two if arr_one.length != arr_two.length length_matched_arrays = update_array_length(arr_one, arr_two) length_matched_arrays[0].each_with_index do |value, index| if length_matched_arrays[1][index] == 0 result << "Divide by 0 error" else result << (value.to_f % length_matched_arrays[1][index]).round(sig_dig) end end else arr_one.each_with_index do |value, index| if arr_two[index] == 0 result << "Divide by 0 error" else result << (value.to_f % arr_two[index]).round(sig_dig) end end end return result end
square(array)
click to toggle source
Squares the values of an array
# File lib/array_arithmetic.rb, line 168 def square(array) return exponent(array, 2) end
square_root(array)
click to toggle source
Returns the square root of each value of an array
# File lib/array_arithmetic.rb, line 174 def square_root(array) return exponent(array, 0.5) end
subtract(arr_one, arr_two)
click to toggle source
Subtracts the values of corresponding indices of two arrays
# File lib/array_arithmetic.rb, line 31 def subtract(arr_one, arr_two) raise ArgumentError, 'First argument is not an array' unless arr_one.is_a? Array result = [] arr_two = arr_two.class != Array ? make_array(arr_two) : arr_two if arr_one.length != arr_two.length length_matched_arrays = update_array_length(arr_one, arr_two) length_matched_arrays[0].each_with_index do |value, index| result << value - length_matched_arrays[1][index] end else arr_one.each_with_index do |value, index| result << value - arr_two[index] end end return result end