class Newral::Functions::Polynomial

Attributes

factors[R]

Public Class Methods

create_random( length: 3, low_range: -9, high_range: 9 ) click to toggle source
# File lib/newral/functions/polynomial.rb, line 27
def self.create_random(  length: 3, low_range: -9, high_range: 9 )
   factors = []
   length.times do 
     factors << low_range+rand(high_range-low_range)
   end
   self.new( factors: factors )
end
new( factors:nil ) click to toggle source
# File lib/newral/functions/polynomial.rb, line 5
def initialize(  factors:nil )
  @factors = factors.dup || [1]
  @length = @factors.size
end

Public Instance Methods

calculate( input ) click to toggle source
# File lib/newral/functions/polynomial.rb, line 10
def calculate( input ) 
  result = 0
  @factors.each_with_index do |factor, idx|
    result = result+input**(@length-idx-1)*factor # this way its more readable
  end 
  result
end
calculate_descent( input ) click to toggle source

caluates descent at input

# File lib/newral/functions/polynomial.rb, line 19
def calculate_descent( input )
  descent = 0
  @factors.each_with_index do |factor, idx|
    descent = descent+input**(@length-idx-2)*factor*(@length-idx-1) if @length-idx-2>= 0 # this way its more readable
  end 
  descent
end
move( direction: 0, step:0.01, step_percentage: nil ) click to toggle source
# File lib/newral/functions/polynomial.rb, line 39
def move( direction: 0, step:0.01, step_percentage: nil )
  raise Errors::InvalidDirection if direction >= number_of_directions
  @factors = @factors.dup
  @factors[direction] = step_percentage ?  @factors[direction]*(1+step_percentage.to_f/100) : @factors[direction]+step
  self
end
number_of_directions() click to toggle source
# File lib/newral/functions/polynomial.rb, line 35
def number_of_directions
  @factors.size 
end