equation_system 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b01551b37db2aaf2b29ccb33ed8b056d800a6119
4
+ data.tar.gz: 5e25df4ba443e5bb39bb0f5ff60958bd33fdd893
5
+ SHA512:
6
+ metadata.gz: 85c9843699b8a08e54b3e37eb9a4979405c3ab2b2dc2aa1c32999668ffdfa72343b42e34732c117567c7ab99ee9cc73efa718f69be4f350f29c3711540f9e392
7
+ data.tar.gz: d60d47944d1c957499981acd78901691ae37bd0bf62a8aada69102616cb6ff892025b064e5c42fda49754bcdb18a6adeb6168e33897559e194fce84c6cb57e29
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format progress
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in equation_system.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Winston Durand
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # EquationSystem
2
+
3
+ This gem will generate a system of equations up to an arbitrary size.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'equation_system'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install equation_system
18
+
19
+ ## Usage
20
+
21
+ For ruby usage:
22
+ ``` ruby
23
+ var_count = 3 # number of variables for the system of equations (required)
24
+
25
+ equation_system = EquationSystem.new(var_count, range: -10..10, allow_zero: false, failsafe: 10)
26
+ # => #<EquationSystem::System:0x007faa498...>
27
+
28
+ # Get the equations
29
+ equation_system.equations
30
+ # => ["5x + 2y + 2z = -24", "-5x - 7y - 5z = -8", "-7x - 5y - 2z = 28"]
31
+
32
+ # Get the answers
33
+ equation_system.answers
34
+ # => {:x=>-8, :y=>4, :z=>4}
35
+
36
+ # Use to get any one variable value
37
+ equation_system.solution_for_x
38
+ # => -8
39
+ ```
40
+
41
+ This gem also adds the commandline tool `equation_system`.
42
+ It can return the output in the json format.
43
+ ``` json
44
+ {"equations":["7x - 8y - 5z = -21","-9x - 10y - 10z = -202","6x - 8y + 4z = 52"],"variables":{"x":8,"y":4,"z":9}}
45
+ ```
46
+
47
+ Call with `-h` or `--help` for the help menu
48
+ ```
49
+ Usage: equation_system.rb [options]
50
+ -v, --variables COUNT Number of variables for the system of equations (required)
51
+ -r, --range RANGE Range of function given a whole number
52
+ -j, --json Output in JSON
53
+ -h, --help Shows this help message
54
+ ```
55
+
56
+ ## Contributing
57
+
58
+ 1. Fork it ( https://github.com/R167/equation_system/fork )
59
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
60
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
61
+ 4. Push to the branch (`git push origin my-new-feature`)
62
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
@@ -0,0 +1,49 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'equation_system'
4
+
5
+ require 'optparse'
6
+
7
+ options = {}
8
+ OptionParser.new do |opts|
9
+ opts.banner = "Usage: equation_system [options]"
10
+
11
+ opts.on("-v", "--variables COUNT", Integer, "Number of variables for the system of equations (required)") do |vars|
12
+ options[:variables] = vars
13
+ end
14
+
15
+ opts.on("-r", "--range RANGE", Integer, "Range of function given a whole number") do |range|
16
+ options[:range] = range
17
+ end
18
+
19
+ opts.on("-j", "--json", "Output in JSON") do
20
+ options[:json] = true
21
+ end
22
+
23
+ opts.on("-h", "--help", "Shows this help message") do
24
+ puts opts
25
+ exit
26
+ end
27
+ end.parse!
28
+
29
+ unless options[:variables].is_a?(Integer)
30
+ puts "Missing number of variables for equation"
31
+ puts "Call with -h or --help for help"
32
+ exit(1)
33
+ end
34
+
35
+ options[:range] ||= Math.log(options[:variables], 1.12).ceil+1
36
+ range = options[:range].abs
37
+
38
+ equation_system = EquationSystem::System.new(options[:variables], range: (-range..range))
39
+
40
+ if options[:json]
41
+ require 'json'
42
+ puts equation_system.to_json
43
+ else
44
+ puts equation_system.equations
45
+ puts
46
+ equation_system.answers.each do |var, val|
47
+ puts "%#{Math.log(options[:variables], 26).ceil}s => %+03d" % [var, val]
48
+ end
49
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'equation_system/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "equation_system"
8
+ spec.version = EquationSystem::VERSION
9
+ spec.authors = ["Winston Durand"]
10
+ spec.email = ["me@winstondurand.com"]
11
+ spec.summary = %q{Generates a system of equations.}
12
+ spec.description = %q{Generates a system of equations made solely of integer values.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.required_ruby_version = '>= 2.0.0'
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.6"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec", "~> 3.0.0"
26
+ end
@@ -0,0 +1,9 @@
1
+ module EquationSystem
2
+ class Error < StandardError; end
3
+
4
+ class TooFewVariablesError < Error; end
5
+
6
+ class RangeTooSmallError < Error; end
7
+
8
+ class FailSafeError < Error; end
9
+ end
@@ -0,0 +1,88 @@
1
+ module EquationSystem
2
+ class System
3
+ def initialize(variables, range: -10..10, allow_zero: false, failsafe: 10)
4
+ raise FailSafeError, "Failsafe cannot be less than 1" if failsafe < 1
5
+ @prng = Random.new
6
+ @answers = generate_answers(variables, range, allow_zero)
7
+ @equations = generate_equations(@answers, range, allow_zero, failsafe)
8
+ end
9
+
10
+ def equations
11
+ @equations
12
+ end
13
+
14
+ def answers
15
+ @answers
16
+ end
17
+
18
+ def method_missing(method, *args, &block)
19
+ match = method.to_s.match(/^solution_for_([a-z]+)$/)
20
+ if match && @answers[match[1].to_sym]
21
+ @answers[match[1].to_sym]
22
+ else
23
+ super
24
+ end
25
+ end
26
+
27
+ def to_json
28
+ require 'json' unless defined?(JSON)
29
+ {:equations => @equations, :variables => @answers}.to_json
30
+ end
31
+
32
+ private
33
+
34
+ def generate_equations(answers, range, allow_zero, failsafe)
35
+ equations = []
36
+ answers.length.times do
37
+ failsafe.times do |fails|
38
+ equation = ""
39
+ total = 0
40
+ answers.each do |var, val|
41
+ coeffecient = random_num(range, allow_zero)
42
+ total += coeffecient * val
43
+ equation << " %+#{val.is_a?(Float) ? 'f' : 'd' }%s" % [coeffecient, var]
44
+ end
45
+ equation = equation.gsub(/(\+|-)/, '\1 ').gsub(/1([a-z])/, '\1').gsub(/^ (-)?\+? /, '\1')
46
+ equation << " = #{total}"
47
+ if equations.include?(equation)
48
+ raise RangeTooSmallError, "Unable to get unique equation after #{failsafe} tries" unless fails == failsafe - 1
49
+ else
50
+ equations << equation
51
+ break
52
+ end
53
+ end
54
+ end
55
+ equations
56
+ end
57
+
58
+ def generate_answers(variables, range, allow_zero)
59
+ ret = {}
60
+ vars = []
61
+ if variables <= 3 && variables > 0
62
+ vars = %w(x y z)[0, variables]
63
+ elsif variables > 3
64
+ cur = 'a'
65
+ variables.times do
66
+ vars << cur.dup
67
+ cur.next!
68
+ end
69
+ else
70
+ raise TooFewVariablesError, "Too few variables, '#{variables}' is not a valid number"
71
+ end
72
+
73
+ vars.each do |var|
74
+ ret[var.to_sym] = random_num(range, allow_zero)
75
+ end
76
+
77
+ ret
78
+ end
79
+
80
+ def random_num(range, allow_zero)
81
+ r = 0
82
+ begin
83
+ r = @prng.rand(range)
84
+ end until r != 0 || allow_zero
85
+ r
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,3 @@
1
+ module EquationSystem
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,9 @@
1
+ require_relative "equation_system/version"
2
+ require_relative "equation_system/system"
3
+ require_relative "equation_system/exceptions"
4
+
5
+ module EquationSystem
6
+ def self.new(*args)
7
+ EquationSystem::System.new(*args)
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe EquationSystem do
4
+ it 'should have a version number' do
5
+ EquationSystem::VERSION.should_not be_nil
6
+ end
7
+
8
+ it 'should respond to #new' do
9
+ EquationSystem.new(1).should be_a(EquationSystem::System)
10
+ end
11
+ end
@@ -0,0 +1,8 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'equation_system'
3
+
4
+ RSpec.configure do |config|
5
+ config.expect_with :rspec do |c|
6
+ c.syntax = [:should, :expect] # default, enables both `should` and `expect`
7
+ end
8
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+ require 'json'
3
+
4
+ describe EquationSystem::System do
5
+ describe "known scenario" do
6
+ let(:equation_system){ EquationSystem::System.new(1, range: 1..1) }
7
+
8
+ it "should generate valid json" do
9
+ JSON.parse(equation_system.to_json).should == {"equations"=>["x = 1"], "variables"=>{"x"=>1}}
10
+ end
11
+
12
+ it "should have answers" do
13
+ equation_system.answers.should be_truthy
14
+ end
15
+
16
+ it "should have equations" do
17
+ equation_system.equations.should be_truthy
18
+ end
19
+
20
+ it "should respond to solution_for" do
21
+ equation_system.solution_for_x.should be_truthy
22
+ end
23
+ end
24
+
25
+ describe "errors" do
26
+ it "should raise an error when too small of range" do
27
+ expect { EquationSystem::System.new(10, range: 1..1) }.to raise_error EquationSystem::RangeTooSmallError
28
+ end
29
+
30
+ it "should raise an error when too few variables" do
31
+ expect { EquationSystem::System.new(0) }.to raise_error EquationSystem::TooFewVariablesError
32
+ end
33
+
34
+ it "should raise an error when the failsafe is too small" do
35
+ expect { EquationSystem::System.new(3, failsafe: 0) }.to raise_error EquationSystem::FailSafeError
36
+ end
37
+ end
38
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: equation_system
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Winston Durand
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 3.0.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 3.0.0
55
+ description: Generates a system of equations made solely of integer values.
56
+ email:
57
+ - me@winstondurand.com
58
+ executables:
59
+ - equation_system
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".travis.yml"
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/equation_system
71
+ - equation_system.gemspec
72
+ - lib/equation_system.rb
73
+ - lib/equation_system/exceptions.rb
74
+ - lib/equation_system/system.rb
75
+ - lib/equation_system/version.rb
76
+ - spec/equation_system_spec.rb
77
+ - spec/spec_helper.rb
78
+ - spec/system_spec.rb
79
+ homepage: ''
80
+ licenses:
81
+ - MIT
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: 2.0.0
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 2.2.2
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: Generates a system of equations.
103
+ test_files:
104
+ - spec/equation_system_spec.rb
105
+ - spec/spec_helper.rb
106
+ - spec/system_spec.rb