-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #296
- Loading branch information
Showing
5 changed files
with
75 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
require_relative '../function' | ||
|
||
Dentaku::AST::Function.register(:intercept, :list, ->(*args) { | ||
flatten_args = args.flatten | ||
if flatten_args.length != 2 || !flatten_args.all? { |arg| arg.is_a?(Array) } || flatten_args.any? { |arg| arg.empty? } | ||
raise Dentaku::ArgumentError.for( | ||
:wrong_number_of_arguments, | ||
function_name: 'INTERCEPT()', exact: 2, given: flatten_args.length | ||
), 'INTERCEPT() requires exactly two arrays of numbers' | ||
end | ||
|
||
x_values, y_values = flatten_args | ||
if x_values.length != y_values.length | ||
raise Dentaku::ArgumentError.for( | ||
:unequal_array_lengths, | ||
function_name: 'INTERCEPT()' | ||
), 'INTERCEPT() requires arrays of equal length' | ||
end | ||
|
||
x_values = x_values.map { |arg| Dentaku::AST::Function.numeric(arg) } | ||
y_values = y_values.map { |arg| Dentaku::AST::Function.numeric(arg) } | ||
|
||
x_sum = x_values.reduce(0, :+) | ||
y_sum = y_values.reduce(0, :+) | ||
xy_sum = x_values.zip(y_values).map { |x, y| x * y }.reduce(0, :+) | ||
x_square_sum = x_values.map { |x| x**2 }.reduce(0, :+) | ||
|
||
n = x_values.length | ||
|
||
slope = (n * xy_sum - x_sum * y_sum) / (n * x_square_sum - x_sum**2) | ||
intercept = y_values.reduce(:+) / n - slope * x_values.reduce(:+) / n | ||
|
||
BigDecimal(intercept) | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
require 'spec_helper' | ||
require 'dentaku/ast/functions/intercept' | ||
require 'dentaku' | ||
|
||
describe 'Dentaku::AST::Function::Intercept' do | ||
it 'returns the correct intercept for given x and y arrays' do | ||
x_values = [1, 2, 3, 4, 5] | ||
y_values = [2, 3, 5, 4, 6] | ||
result = Dentaku('INTERCEPT(?, ?)', x_values, y_values) | ||
expect(result).to be_within(0.001).of(1.2) | ||
end | ||
|
||
context 'checking errors' do | ||
it 'raises an error if arguments are not arrays' do | ||
expect { Dentaku!("INTERCEPT(1, 2)") }.to raise_error(Dentaku::ArgumentError) | ||
end | ||
|
||
it 'raises an error if the arrays are not of equal length' do | ||
x_values = [1, 2, 3] | ||
y_values = [2, 3, 5, 4] | ||
expect { Dentaku!("INTERCEPT(?, ?)", x_values, y_values) }.to raise_error(Dentaku::ArgumentError) | ||
end | ||
|
||
it 'raises an error if any of the arrays is empty' do | ||
x_values = [] | ||
y_values = [2, 3, 5, 4] | ||
expect { Dentaku!("INTERCEPT(?, ?)", x_values, y_values) }.to raise_error(Dentaku::ArgumentError) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters