linear1 1.0.0.pre

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0958577ede7ba8fa57fd241fc9a29aae34cd911b
4
+ data.tar.gz: c8266881e23d9eab12c5538f0ee408eaf2271fe0
5
+ SHA512:
6
+ metadata.gz: f1311e4c1482095f42a3f034d32ec7a87c00f45b8d779d8fd004e5ee2356d76d88c1a613f18e78e5bc9e78bdc2a792696485c7e2c2b889e78fc876fdc28cc1e8
7
+ data.tar.gz: 41fff4658e2866970b790045db11b208a5a4bc19ae2e1ee56695d640f3e4cd7d6aeb8acc3f181f950fdb9501278581c503fa368c4f8286543d633b9cd741aa69
data/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Zachary Roth Perlmutter
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # linear1
2
+ A gem that does linear algebra to an extent
3
+ ## Installation
4
+
5
+ |Method|Runtime|Development|
6
+ |:----:|:-----:|:---------:|
7
+ |`Gemfile`|`gem 'linear1'`| `gem 'linear1', group: :development`|
8
+ |RubyGems|`$ gem install linear1` | `$ gem install linear1 --development` |
9
+ |`.gemspec`|`gem.add_runtime_dependency "linear1"`|`gem.add_development_dependency "linear1"` |
10
+
11
+ ## Usage
12
+ `$ linear1 help`
13
+
14
+ `$ linear1 graph _equation_`
15
+
16
+ `$ linear1 display _equation_`
17
+
18
+ `$ linear1 execute x _equation_`
19
+
20
+ ### equations
21
+ Replace `_equation_` with one of these equations:
22
+
23
+ `standard A B C`
24
+
25
+ `point-slope slope x1 y1`
26
+
27
+ `slope-intercept slope y_intercept`
28
+
29
+ `function slope y_intercept power`
30
+
31
+ `direct-variation k`
32
+
33
+ ## Badges
34
+ [![Build Status](https://travis-ci.org/Zrp200/linear1.svg?branch=master)](https://travis-ci.org/Zrp200/linear1)
35
+ [![Code Climate](https://codeclimate.com/github/Zrp200/linear1/badges/gpa.svg)](https://codeclimate.com/github/Zrp200/linear1)
36
+ [![Test Coverage](https://codeclimate.com/github/Zrp200/linear1/badges/coverage.svg)](https://codeclimate.com/github/Zrp200/linear1)
data/bin/linear1 ADDED
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+ require "linear1"
3
+ include Linear1
4
+ equation = proc do |index|
5
+ case ARGV[index]
6
+ when "slope-intercept" then SlopeIntercept
7
+ when "standard" then Standard
8
+ when "function" then Function
9
+ when "direct-variation" then DirectVariation
10
+ when "point-slope" then PointSlope
11
+ end.find index + 1
12
+ end
13
+ puts case ARGV[0]
14
+ when "graph" then Graph.new equation.call 1
15
+ when "display" then equation.call 1
16
+ when "execute" then equation.call(2).execute ARGV[1].to_f
17
+ when "help" then "Help is not available. Please refer to the README."
18
+ end
@@ -0,0 +1,21 @@
1
+ require "linear1/function"
2
+ module Linear1
3
+ class ArthmeticSequence < Function
4
+ attr_reader :a, :common_difference
5
+ alias d common_difference
6
+ alias to_a a
7
+ alias slope d
8
+ def initialize a
9
+ i = 0
10
+ while i < a.length - 2
11
+ raise ArgumentError, "Elements must have a common difference" unless a[i+1] - a[i] == a[i+2] - a[i+1]
12
+ i += 1
13
+ end
14
+ @common_difference, @a = a[1] - a[0], a
15
+ super @common_difference, @a[0]
16
+ end
17
+ def to_s # Modified to count from zero instead of one
18
+ "a[n] = #{a[0]} + #{idx d}n"
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,15 @@
1
+ require "linear1/slope_intercept"
2
+ module Linear1
3
+ class DirectVariation < SlopeIntercept
4
+ # @param index1 [Fixnum] the index of the first argument
5
+ # @return [DirectVariation]
6
+ def self.find index1
7
+ new ARGV[index1]
8
+ end
9
+ def initialize k=1
10
+ super k, 0
11
+ end
12
+ alias k slope
13
+ alias constant_of_variation k
14
+ end
15
+ end
@@ -0,0 +1,73 @@
1
+ module Linear1
2
+ class Function
3
+ attr_reader :slope, :y_intercept, :power
4
+ # @param i1 [Fixnum] the index to start search
5
+ # @return [Function]
6
+ def self.find i1
7
+ Function.new ARGV[i1], ARGV[i1 + 1], ARGV[i1 + 2]
8
+ end
9
+ def initialize(slope=1, y_intercept=0, power=1)
10
+ @slope, @y_intercept, @power = slope.to_f, y_intercept.to_f, power.to_i
11
+ end
12
+ # @param x [Integer, Float]
13
+ # @return [Integer, Float]
14
+ def execute x
15
+ raise ArgumentError unless x.kind_of? Numeric
16
+ return slope * x ** power + y_intercept
17
+ end
18
+ alias f execute
19
+ def x_intercept
20
+ f(0)
21
+ end
22
+ alias zero x_intercept
23
+ alias solution x_intercept
24
+ alias root x_intercept
25
+ def to_s
26
+ "f(x) = #{idx slope.to_i if slope.to_i == slope}x#{power_string unless power == 1}#{" + #{@y_intercept.to_i if @y_intercept.to_i == @y_intercept}" unless direct_variation?}"
27
+ end
28
+ def direct_variation?
29
+ y_intercept.zero? and power == 1
30
+ end
31
+ alias dv? direct_variation?
32
+ def to_direct_variation
33
+ if direct_variation?
34
+ require "linear1/direct_variation"
35
+ DirectVariation.new slope
36
+ else
37
+ raise TypeError, "Unable to convert to DirectVariation"
38
+ end
39
+ end
40
+ def to_slope_intercept
41
+ raise "power must be 1" unless power == 1
42
+ SlopeIntercept.new slope, y_intercept
43
+ end
44
+ alias to_dv to_direct_variation
45
+ private
46
+ def power_string
47
+ final = String.new
48
+ final += "\u207b" if @power < 0
49
+ final << case @power
50
+ when 0 then "\u2070"
51
+ when 1 then "\u00b9"
52
+ when 2 then "\u00b2"
53
+ when 3 then "\u00b3"
54
+ when 4 then "\u2074"
55
+ when 5 then "\u2075"
56
+ when 6 then "\u2076"
57
+ when 7 then "\u2077"
58
+ when 8 then "\u2078"
59
+ when 9 then "\u2079"
60
+ end
61
+ return final
62
+ end
63
+ def idx(s)
64
+ return case s
65
+ when 1 then String.new
66
+ when -1 then ?-
67
+ else
68
+ s
69
+ end
70
+ end
71
+ end
72
+ end
73
+
@@ -0,0 +1,89 @@
1
+ require "linear1/function"
2
+ module Linear1
3
+ class Graph
4
+ def initialize equation
5
+ @equation = equation
6
+ end
7
+ @@axis = {x: 75, y: 25}
8
+
9
+ def self.axis(key)
10
+ @@axis[key]
11
+ end
12
+
13
+ ORIGIN = {x: @@axis[:x] / 2, y: @@axis[:y] / 2} # The center of the graph
14
+
15
+ def to_a
16
+ final = Array.new
17
+ y = 0
18
+ @@axis[:y].times do
19
+ final[y] = Array.new
20
+ x = 0
21
+ @@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])
23
+ x += 1
24
+ end
25
+ y += 1
26
+ end
27
+ return final.reverse
28
+ end
29
+
30
+
31
+ def to_hash
32
+ table = Hash.new
33
+ for y in (0..@@axis[:y]).to_a
34
+ for x in (0..@@axis[:x]).to_a
35
+ x_exec, y_exec = @equation.execute(x - ORIGIN[:x]), y - ORIGIN[:y]
36
+ table[y] = x_exec if x_exec == y_exec
37
+ end
38
+ end
39
+ return table
40
+ end
41
+ alias to_h to_hash
42
+ alias xy to_hash
43
+ def domain # @return [Array<Integer>] the values of the xy hash
44
+ xy.values
45
+ end
46
+
47
+ def range # @return [Array<Integer>] the keys of the xy hash
48
+ xy.keys
49
+ end
50
+
51
+ # @return [String] the graph
52
+ def to_s
53
+ result = String.new
54
+ for y_index in to_a
55
+ for x_index in y_index
56
+ result << x_index
57
+ end
58
+ result << ?\n
59
+ end
60
+ return result.center(100)
61
+ end
62
+
63
+ private
64
+
65
+ 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"
70
+ end
71
+ end
72
+
73
+ alias caa check_axis_argument
74
+
75
+ # @return [String, nil]
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
86
+ end
87
+
88
+ end
89
+ end
@@ -0,0 +1,22 @@
1
+ require "linear1/function"
2
+ module Linear1
3
+ class PointSlope < Function
4
+ attr_reader :x1, :y1
5
+ private :y_intercept, :power
6
+ def initialize slope, x, y
7
+ @slope, @x1, @y1 = slope.to_f, x.to_f, y.to_f
8
+ super @slope, @x1 - @y1
9
+ end
10
+ def self.find index
11
+ new ARGV[index], ARGV[index + 1], ARGV[index + 2]
12
+ end
13
+ def to_slope_intercept
14
+ require "linear1/slope_intercept"
15
+ SlopeIntercept.new slope, y_intercept
16
+ end
17
+ alias to_si to_slope_intercept
18
+ def to_standard
19
+ to_si.to_standard
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,23 @@
1
+ require "linear1/function"
2
+ module Linear1
3
+ class SlopeIntercept < Function
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
11
+ end
12
+ def to_s
13
+ return super.sub "f(x)", "y"
14
+ end
15
+ def initialize(slope, y_intercept)
16
+ super
17
+ end
18
+ def to_standard
19
+ require "linear1/standard"
20
+ Standard.new slope, -1, -y_intercept
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,41 @@
1
+ require "linear1/function"
2
+ module Linear1
3
+ class Standard < Function
4
+ attr_reader :a, :b, :c
5
+ private :slope, :y_intercept
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]
10
+ end
11
+ def initialize a, b, c
12
+ @a, @b, @c = a.to_f, b.to_f, c.to_f
13
+ super @c / @b / @a, @c / @b
14
+ end
15
+ def multiply int
16
+ new @a * int, @b * int, @c * int
17
+ end
18
+ def multiply! factor
19
+ @a *= factor
20
+ @b *= factor
21
+ @c *= factor
22
+ end
23
+ def add! int
24
+ @a += int
25
+ @b += int
26
+ @c += int
27
+ end
28
+ def add int
29
+ new @a + int, @b + int, @c + int
30
+ end
31
+ def to_s
32
+ "#{idx a}x + #{idx b} = #{c}"
33
+ end
34
+ def to_slope_intercept
35
+ require "linear1/slope_intercept"
36
+ SlopeIntercept.new slope, y_intercept
37
+ end
38
+ alias to_si to_slope_intercept
39
+ end
40
+ end
41
+
@@ -0,0 +1,17 @@
1
+ module Linear1
2
+ class System
3
+ attr_reader :e1, :e2
4
+ def initialize equation1, equation2
5
+ @e1, @e2 = equation1, equation2
6
+ end
7
+ def execute x
8
+ final = Array.new
9
+ @equations.each {|equation| final << equation.execute(x)}
10
+ return final
11
+ end
12
+ end
13
+ end
14
+
15
+
16
+
17
+
data/lib/linear1.rb ADDED
@@ -0,0 +1,8 @@
1
+ require "linear1/system"
2
+ require "linear1/standard"
3
+ require "linear1/slope_intercept"
4
+ require "linear1/point_slope"
5
+ require "linear1/function"
6
+ require "linear1/graph"
7
+ require "linear1/direct_variation"
8
+
@@ -0,0 +1,9 @@
1
+ require "linear1/direct_variation"
2
+ include Linear1
3
+ RSpec.describe DirectVariation do
4
+ describe "#new" do
5
+ subject {DirectVariation.new rand}
6
+ it {is_expected.to be_direct_variation}
7
+ it {is_expected.to be_kind_of SlopeIntercept}
8
+ end
9
+ end
@@ -0,0 +1,13 @@
1
+ require "linear1/function"
2
+ include Linear1
3
+ RSpec.describe Function do
4
+ describe "#new" do
5
+ context "given no arguments" do
6
+ subject {Function.new}
7
+ its(:to_s) {is_expected.to eq "f(x) = x"}
8
+ its(:power) {is_expected.to eq 1}
9
+ its(:slope) {is_expected.to eq 1}
10
+ its(:y_intercept) {is_expected.to be_zero}
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ require "linear1/graph"
2
+ include Linear1
3
+ RSpec.describe Graph do
4
+ describe "#new" do
5
+ context "given Function" do
6
+ subject {Graph.new Function.new}
7
+ it "should not raise error" do
8
+ expect{Graph.new Function.new}.to_not raise_error
9
+ end
10
+ %i[range domain to_a].each {|method| its(method) {is_expected.to be_an_instance_of Array} }
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,20 @@
1
+ require "linear1/slope_intercept"
2
+ include Linear1
3
+ RSpec.describe SlopeIntercept do
4
+ describe "#new" do
5
+ subject {SlopeIntercept.new rand, rand}
6
+ it {is_expected.to be_kind_of Function}
7
+ its(:power) {is_expected.to eq 1}
8
+ it {is_expected.to respond_to :execute, :y_intercept, :to_direct_variation, :to_standard, :slope}
9
+ context "given 1" do
10
+ it "should raise error" do
11
+ expect{SlopeIntercept.new 1}.to raise_error
12
+ end
13
+ end
14
+ context "given 1, 0" do
15
+ subject {SlopeIntercept.new 3, 6}
16
+ it {is_expected.to_not be_direct_variation}
17
+ its(:to_s) {is_expected.to eq "y = 3x + 6"}
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,10 @@
1
+ require "linear1/standard"
2
+ include Linear1
3
+ RSpec.describe Standard do
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?}
8
+ end
9
+ end
10
+
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: linear1
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0.pre
5
+ platform: ruby
6
+ authors:
7
+ - Zachary Perlmutter
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.1'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec-its
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.1'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.1'
41
+ description:
42
+ email: zrp200@gmail.com
43
+ executables:
44
+ - linear1
45
+ extensions: []
46
+ extra_rdoc_files:
47
+ - LICENSE.md
48
+ - README.md
49
+ files:
50
+ - LICENSE.md
51
+ - README.md
52
+ - bin/linear1
53
+ - lib/linear1.rb
54
+ - lib/linear1/arthmetic_sequence.rb
55
+ - lib/linear1/direct_variation.rb
56
+ - lib/linear1/function.rb
57
+ - lib/linear1/graph.rb
58
+ - lib/linear1/point_slope.rb
59
+ - lib/linear1/slope_intercept.rb
60
+ - lib/linear1/standard.rb
61
+ - lib/linear1/system.rb
62
+ - spec/lib/linear1/direct_variation_spec.rb
63
+ - spec/lib/linear1/function_spec.rb
64
+ - spec/lib/linear1/graph_spec.rb
65
+ - spec/lib/linear1/slope_intercept_spec.rb
66
+ - spec/lib/linear1/standard_spec.rb
67
+ homepage:
68
+ licenses:
69
+ - MIT
70
+ metadata: {}
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">"
83
+ - !ruby/object:Gem::Version
84
+ version: 1.3.1
85
+ requirements: []
86
+ rubyforge_project:
87
+ rubygems_version: 2.2.2
88
+ signing_key:
89
+ specification_version: 4
90
+ summary: Does linear algebra, to an extent
91
+ test_files:
92
+ - spec/lib/linear1/direct_variation_spec.rb
93
+ - spec/lib/linear1/function_spec.rb
94
+ - spec/lib/linear1/graph_spec.rb
95
+ - spec/lib/linear1/slope_intercept_spec.rb
96
+ - spec/lib/linear1/standard_spec.rb