Class: LinearAlgebra::ArthmeticSequence

Inherits:
Function
  • Object
show all
Defined in:
lib/linear_algebra/arthmetic_sequence.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(a) ⇒ ArthmeticSequence



8
9
10
11
12
13
14
15
16
# File 'lib/linear_algebra/arthmetic_sequence.rb', line 8

def initialize a
	i = 0
	while i < a.length - 2 
		raise ArgumentError, "Elements must have a common difference" unless a[i+1] - a[i] == a[i+2] - a[i+1]
		i += 1
	end
	@common_difference, @a = a[1] - a[0], a
	super @common_difference, @a[0]
end

Instance Attribute Details

#aObject (readonly) Also known as: to_a

Returns the value of attribute a



4
5
6
# File 'lib/linear_algebra/arthmetic_sequence.rb', line 4

def a
  @a
end

#common_differenceObject (readonly) Also known as: d

Returns the value of attribute common_difference



4
5
6
# File 'lib/linear_algebra/arthmetic_sequence.rb', line 4

def common_difference
  @common_difference
end

Instance Method Details

#to_sObject

Modified to count from zero instead of one



17
18
19
# File 'lib/linear_algebra/arthmetic_sequence.rb', line 17

def to_s # Modified to count from zero instead of one
	"a[n] = #{a[0]} + #{idx d}n"
end