linear1 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 31aee81c2882bacd6ef1a027d30a18285e13ad48
4
- data.tar.gz: aff302ca97029bc3ca08288609a6da4e7acbec51
3
+ metadata.gz: 4c5ae06f9ef7e39d7113088124eeace11de9d2dc
4
+ data.tar.gz: fcce646488b19ee6e5964703e55062621d690ac9
5
5
  SHA512:
6
- metadata.gz: 6a224cec4069c0ce61512d5dc99fb208f8a56388436f7789bccf2007ad8d0e4654e2967d96f185db7c0184f3033c34e00a3aaf968bdaec5bc8718e7e307a7319
7
- data.tar.gz: 7e934f04c40b81c1f07f77ff4e2f633d17a00fb95182db37c41ff77383bc0b60fca256ffd63761f10adb8b570be27554812dbd133c7391641a42bb372ba1e1d7
6
+ metadata.gz: c0c7cc6131028b8d3062086905dca9a939cf026d429143b46bead734fef0ef6d3452f447d559034c6d80c061a6107860dfae3aedc9d01229ec3a856d02e6e1c1
7
+ data.tar.gz: 0d935cc343c3854bcc9396ceb181ff9453c68c415610d07cb31beb711dbc630c90c5e1561ad1ce3292a4af336bcef62068ee4933f194f5e1240eac4286d2d356
@@ -3,6 +3,7 @@ require "linear1"
3
3
  include Linear1
4
4
  equation = proc do |index|
5
5
  case ARGV[index]
6
+ when "arithmetic_sequence" then ArithmeticSequence
6
7
  when "slope-intercept" then SlopeIntercept
7
8
  when "standard" then Standard
8
9
  when "function" then Function
@@ -13,6 +14,6 @@ end
13
14
  puts case ARGV[0]
14
15
  when "graph" then Graph.new equation.call 1
15
16
  when "display" then equation.call 1
16
- when "execute" then equation.call(2).execute ARGV[1].to_f
17
+ when "execute" then equation.call(2).execute ARGV[1]
17
18
  when "help" then "Help is not available. Please refer to the README."
18
19
  end
@@ -1,12 +1,15 @@
1
1
  require "linear1/function"
2
2
  module Linear1
3
- class ArthmeticSequence < Function
3
+ class ArithmeticSequence < Function
4
+ def self.find index
5
+ new *(ARGV[index..(ARGV.length - 1)])
6
+ end
4
7
  attr_reader :a, :common_difference
8
+ alias d slope
5
9
  alias d common_difference
6
10
  private :slope, :y_intercept
7
11
  alias to_a a
8
- alias slope d
9
- def initialize a
12
+ def initialize *a
10
13
  i = 0
11
14
  while i < a.length - 2
12
15
  raise ArgumentError, "Elements must have a common difference" unless a[i+1] - a[i] == a[i+2] - a[i+1]
@@ -7,7 +7,7 @@ module Linear1
7
7
  new ARGV[index1]
8
8
  end
9
9
  def initialize k=1
10
- super k, 0
10
+ super display_num(k), 0
11
11
  end
12
12
  alias k slope
13
13
  alias constant_of_variation k
@@ -1,4 +1,7 @@
1
+ autoload :BigDecimal, "bigdecimal"
1
2
  module Linear1
3
+ autoload :SlopeIntercept, "linear1/slope_intercept"
4
+ autoload :DirectVariation, "linear1/direct_variation"
2
5
  class Function
3
6
  attr_reader :slope, :y_intercept, :power
4
7
  # @param i1 [Fixnum] the index to start search
@@ -12,8 +15,7 @@ module Linear1
12
15
  # @param x [Integer, Float]
13
16
  # @return [Integer, Float]
14
17
  def execute x
15
- raise ArgumentError unless x.kind_of? Numeric
16
- return slope * x ** power + y_intercept
18
+ slope * display_num(x) ** power + y_intercept
17
19
  end
18
20
  alias f execute
19
21
  def x_intercept
@@ -34,14 +36,12 @@ module Linear1
34
36
  # @raise [TypeError]
35
37
  def to_direct_variation
36
38
  if direct_variation?
37
- require "linear1/direct_variation"
38
39
  DirectVariation.new slope
39
- else
40
- raise TypeError, "Unable to convert to DirectVariation"
40
+ else raise TypeError, "Unable to convert to DirectVariation"
41
41
  end
42
42
  end
43
43
  def to_slope_intercept
44
- raise "power must be 1" unless power == 1
44
+ raise TypeError, "power must be 1" unless power == 1
45
45
  SlopeIntercept.new slope, y_intercept
46
46
  end
47
47
  alias to_dv to_direct_variation
@@ -61,25 +61,24 @@ module Linear1
61
61
  when 8 then "\u2078"
62
62
  when 9 then "\u2079"
63
63
  end
64
- return final
64
+ final
65
65
  end
66
66
  def idx(s)
67
- return case s
67
+ case s
68
68
  when 1 then String.new
69
69
  when -1 then ?-
70
- else
71
- s
72
- end
70
+ else s end
73
71
  end
74
72
  def display_num num
75
- if num.to_r.to_s == num.to_s
76
- num.to_r
77
- elsif num.to_i.to_s == num.to_s
78
- num.to_i
79
- elsif num.to_f.to_s == num.to_s
80
- num.to_f
81
- elsif num.to_c.to_s == num.to_s
82
- num.to_c
73
+ unless num.is_a? String then num
74
+ else
75
+ case num
76
+ when Complex(num).to_s then Complex(num)
77
+ when Rational(num).to_s then Rational(num)
78
+ when num.to_i.to_s then num.to_i
79
+ when num.to_f.to_s then num.to_f
80
+ when BigDecimal(num).to_s then BigDecimal(num)
81
+ end
83
82
  end
84
83
  end
85
84
  end
@@ -6,25 +6,23 @@ module Linear1
6
6
  end
7
7
  @@axis = {x: 75, y: 25}
8
8
 
9
- def self.axis(key)
9
+ def self.axis key
10
10
  @@axis[key]
11
11
  end
12
12
 
13
13
  ORIGIN = {x: @@axis[:x] / 2, y: @@axis[:y] / 2} # The center of the graph
14
14
 
15
15
  def to_a
16
- final = Array.new
17
- y = 0
16
+ final, y = Array.new, 0
18
17
  @@axis[:y].times do
19
- final[y] = Array.new
20
- x = 0
18
+ final[y], x = Array.new, 0
21
19
  @@axis[:x].times do
22
- final[y][x] = (!to_hash[y].nil? && to_hash[y] == x - ORIGIN[:x] ) ? ?\u2022 : format_grid(x - ORIGIN[:x], y - ORIGIN[:y])
20
+ final[y][x] = init_coord(x, y)
23
21
  x += 1
24
22
  end
25
23
  y += 1
26
24
  end
27
- return final.reverse
25
+ final.reverse
28
26
  end
29
27
 
30
28
 
@@ -36,7 +34,7 @@ module Linear1
36
34
  table[y] = x_exec if x_exec == y_exec
37
35
  end
38
36
  end
39
- return table
37
+ table
40
38
  end
41
39
  alias to_h to_hash
42
40
  alias xy to_hash
@@ -57,16 +55,18 @@ module Linear1
57
55
  end
58
56
  result << ?\n
59
57
  end
60
- return result.center(100)
58
+ result.center 100
61
59
  end
62
60
 
63
61
  private
62
+
63
+ def init_coord x, y
64
+ (!to_hash[y].nil? && to_hash[y] == x - ORIGIN[:x]) ? ?\u2022 : format_grid(x - ORIGIN[:x], y - ORIGIN[:y])
65
+ end
64
66
 
65
67
  def check_axis_argument(arg)
66
- if !(num.kind_of?(Integer) )
67
- raise ArgumentError, "Argument must be a kind of Integer"
68
- elsif num % 2 != 0
69
- raise ArgumentError, "Argument must be even"
68
+ if !(num.kind_of?(Integer) ) then raise ArgumentError, "Argument must be a kind of Integer"
69
+ elsif num % 2 != 0 then raise ArgumentError, "Argument must be even"
70
70
  end
71
71
  end
72
72
 
@@ -74,15 +74,10 @@ module Linear1
74
74
 
75
75
  # @return [String, nil]
76
76
  def format_grid x, y
77
- if x.zero? && y.zero?
78
- "+"
79
- elsif x.zero? && !y.zero?
80
- "|"
81
- elsif y.zero? && !x.zero?
82
- "-"
83
- else
84
- " "
85
- end
77
+ if x.zero? && y.zero? then "+"
78
+ elsif x.zero? && !y.zero? then "|"
79
+ elsif y.zero? && !x.zero? then "-"
80
+ else " " end
86
81
  end
87
82
 
88
83
  end
@@ -2,15 +2,10 @@ require "linear1/function"
2
2
  module Linear1
3
3
  class SlopeIntercept < Function
4
4
  def self.find(index1, array=ARGV)
5
- i = 0
6
- until i == 2
7
- raise ArgumentError, "array[#{index1 + i}] must be a number" unless (array[index1 + i].to_i.to_s == array[index1 + i] || array[index1 + i].to_f.to_s == array[index1 + i])
8
- i += 1
9
- end
10
- return new array[index1+0].to_f, array[index1+1].to_f
5
+ new array[index1 + 0], array[index1 + 1]
11
6
  end
12
7
  def to_s
13
- return super.sub "f(x)", "y"
8
+ super.sub "f(x)", "y"
14
9
  end
15
10
  def initialize(slope, y_intercept)
16
11
  super
@@ -4,12 +4,11 @@ module Linear1
4
4
  attr_reader :a, :b, :c
5
5
  private :slope, :y_intercept
6
6
  def self.find index
7
- array = ARGV[index..(index + 2)]
8
- $equation_index = index + 3
9
- return new array[0], array[1], array[2]
7
+ array, $equation_index = ARGV[index..(index + 2)], $equation_index = index + 3
8
+ new array[0], array[1], array[2]
10
9
  end
11
10
  def initialize a, b, c
12
- @a, @b, @c = a.to_f, b.to_f, c.to_f
11
+ @a, @b, @c = display_num(a), display_num(b), display_num(c)
13
12
  super @c / @b / @a, @c / @b
14
13
  end
15
14
  def multiply int
@@ -29,7 +28,7 @@ module Linear1
29
28
  new @a + int, @b + int, @c + int
30
29
  end
31
30
  def to_s
32
- "#{idx a}x + #{idx b} = #{c}"
31
+ "#{idx a}x + #{idx b}y = #{c}"
33
32
  end
34
33
  def to_slope_intercept
35
34
  require "linear1/slope_intercept"
@@ -7,7 +7,7 @@ module Linear1
7
7
  def execute x
8
8
  final = Array.new
9
9
  @equations.each {|equation| final << equation.execute(x)}
10
- return final
10
+ final
11
11
  end
12
12
  end
13
13
  end
@@ -0,0 +1,14 @@
1
+ require 'linear1/arithmetic_sequence'
2
+ require 'bigdecimal'
3
+ include Linear1
4
+ RSpec.describe ArithmeticSequence do
5
+ describe "#new" do
6
+ [ [2, 3, 4, 5, 6], [1_000_000, 1_500_000, 2_000_000], [Complex(1, 2), Complex(2, 2), Complex(3, 2)] ].each do |args|
7
+ context "given #{args}" do
8
+ subject {ArithmeticSequence.new *args}
9
+ it "should not raise error" do expect{ArithmeticSequence.new *args}.to_not raise_error end
10
+ it {is_expected.to be_kind_of Function}
11
+ end
12
+ end
13
+ end
14
+ end
@@ -7,7 +7,6 @@ RSpec.describe Graph do
7
7
  it "should not raise error" do
8
8
  expect{Graph.new Function.new}.to_not raise_error
9
9
  end
10
- %i[range domain to_a].each {|method| its(method) {is_expected.to be_an_instance_of Array} }
11
10
  end
12
11
  end
13
12
  end
@@ -1,5 +1,4 @@
1
1
  require "linear1/slope_intercept"
2
- require "rational"
3
2
  include Linear1
4
3
  RSpec.describe SlopeIntercept do
5
4
  describe "#new" do
@@ -17,7 +16,7 @@ RSpec.describe SlopeIntercept do
17
16
  it {is_expected.to_not be_direct_variation}
18
17
  its(:to_s) {is_expected.to eq "y = 3x + 6"}
19
18
  end
20
- [ [3, 5], [-2, -9], [Rational("2/3"), 3], [Rational("-5/8"), -2], [-5, -3] ].each do |spec|
19
+ [ [3, 5], [-2, -9], [4.7, 5.3], [Rational("2/3"), 3], [Rational("-5/8"), -2], [-5, -3] ].each do |spec|
21
20
  context "given #{spec[0]}, #{spec[1]}" do
22
21
  subject {SlopeIntercept.new spec[0], spec[1]}
23
22
  its(:to_s) {is_expected.to eq "y = #{spec[0]}x + #{spec[1]}"}
@@ -25,6 +24,8 @@ RSpec.describe SlopeIntercept do
25
24
  require "linear1/graph"
26
25
  expect{Graph.new subject}.to_not raise_error
27
26
  end
27
+
28
+
28
29
  end
29
30
  end
30
31
 
@@ -2,9 +2,15 @@ require "linear1/standard"
2
2
  include Linear1
3
3
  RSpec.describe Standard do
4
4
  describe "#new" do
5
- subject {Standard.new rand, rand, rand}
6
- it {is_expected.to be_kind_of Function}
7
- it {is_expected.to respond_to :execute, :a, :b, :c, :direct_variation?}
5
+ [ [5_000_000, 3_969_567, 4_000_456], [2, 3, 4], [Rational(5, 4), Rational(3, 7), Rational(9, 11)], [2.3, 5.4, 3.6], [Complex(3, 4), Complex(9, 7), Complex(10, 13)] ].each do |args|
6
+ describe "given #{args[0]}, #{args[1]}, #{args[2]}" do
7
+ subject {Standard.new args.first, args[1], args[2]}
8
+ it {is_expected.to be_kind_of Function}
9
+ its(:to_s) {is_expected.to eq "#{args.first}x + #{args[1]}y = #{args[2]}"}
10
+ it {is_expected.to respond_to :execute, :add, :a, :b, :c, :direct_variation?}
11
+
12
+ end
13
+ end
8
14
  end
9
15
  end
10
16
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: linear1
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zachary Perlmutter
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-03 00:00:00.000000000 Z
11
+ date: 2015-02-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -51,7 +51,7 @@ files:
51
51
  - README.md
52
52
  - bin/linear1
53
53
  - lib/linear1.rb
54
- - lib/linear1/arthmetic_sequence.rb
54
+ - lib/linear1/arithmetic_sequence.rb
55
55
  - lib/linear1/direct_variation.rb
56
56
  - lib/linear1/function.rb
57
57
  - lib/linear1/graph.rb
@@ -59,6 +59,7 @@ files:
59
59
  - lib/linear1/slope_intercept.rb
60
60
  - lib/linear1/standard.rb
61
61
  - lib/linear1/system.rb
62
+ - spec/lib/linear1/arithmetic_sequence_spec.rb
62
63
  - spec/lib/linear1/direct_variation_spec.rb
63
64
  - spec/lib/linear1/function_spec.rb
64
65
  - spec/lib/linear1/graph_spec.rb
@@ -90,6 +91,7 @@ signing_key:
90
91
  specification_version: 4
91
92
  summary: Does linear algebra, to an extent
92
93
  test_files:
94
+ - spec/lib/linear1/arithmetic_sequence_spec.rb
93
95
  - spec/lib/linear1/direct_variation_spec.rb
94
96
  - spec/lib/linear1/function_spec.rb
95
97
  - spec/lib/linear1/graph_spec.rb