equation_system 1.0.0 → 1.1.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 +4 -4
- data/README.md +3 -0
- data/lib/equation_system/system.rb +24 -16
- data/lib/equation_system/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e29f98511bdedc546b76fbf37e0c975d50bc1f4f
|
4
|
+
data.tar.gz: 3a5a96efe4464859fd86118755566e8bf9977c69
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 292cd64c26d6145a1ce3b7d035d68d53eae4a5f22ac1a2e534fe838e262aed13f89cde666563bd87b7f41e990f5b232e2e22efa507fce42989a554e35d8c4a78
|
7
|
+
data.tar.gz: 29702b56c76e575ff6d54a75d453e388f47a9bbc90146964b248c734e4be52161f6dbb5adf55632b618bcd3ed794e6833c5cbd88b224a84a749cd5438fc3fdb9
|
data/README.md
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
[](http://badge.fury.io/rb/equation_system)
|
2
|
+
[](https://travis-ci.org/R167/equation_system)
|
3
|
+
|
1
4
|
# EquationSystem
|
2
5
|
|
3
6
|
This gem will generate a system of equations up to an arbitrary size.
|
@@ -1,20 +1,25 @@
|
|
1
1
|
module EquationSystem
|
2
|
-
class System
|
2
|
+
class System
|
3
3
|
def initialize(variables, range: -10..10, allow_zero: false, failsafe: 10)
|
4
4
|
raise FailSafeError, "Failsafe cannot be less than 1" if failsafe < 1
|
5
5
|
@prng = Random.new
|
6
|
+
@coefficients = []
|
6
7
|
@answers = generate_answers(variables, range, allow_zero)
|
7
8
|
@equations = generate_equations(@answers, range, allow_zero, failsafe)
|
8
9
|
end
|
9
|
-
|
10
|
+
|
10
11
|
def equations
|
11
12
|
@equations
|
12
13
|
end
|
13
|
-
|
14
|
+
|
14
15
|
def answers
|
15
16
|
@answers
|
16
17
|
end
|
17
|
-
|
18
|
+
|
19
|
+
def coefficients
|
20
|
+
@coefficients
|
21
|
+
end
|
22
|
+
|
18
23
|
def method_missing(method, *args, &block)
|
19
24
|
match = method.to_s.match(/^solution_for_([a-z]+)$/)
|
20
25
|
if match && @answers[match[1].to_sym]
|
@@ -23,24 +28,26 @@ module EquationSystem
|
|
23
28
|
super
|
24
29
|
end
|
25
30
|
end
|
26
|
-
|
31
|
+
|
27
32
|
def to_json
|
28
33
|
require 'json' unless defined?(JSON)
|
29
34
|
{:equations => @equations, :variables => @answers}.to_json
|
30
35
|
end
|
31
|
-
|
36
|
+
|
32
37
|
private
|
33
|
-
|
38
|
+
|
34
39
|
def generate_equations(answers, range, allow_zero, failsafe)
|
35
40
|
equations = []
|
36
41
|
answers.length.times do
|
37
42
|
failsafe.times do |fails|
|
43
|
+
temp = []
|
38
44
|
equation = ""
|
39
45
|
total = 0
|
40
46
|
answers.each do |var, val|
|
41
|
-
|
42
|
-
|
43
|
-
|
47
|
+
coefficient = random_num(range, allow_zero)
|
48
|
+
temp << coefficient
|
49
|
+
total += coefficient * val
|
50
|
+
equation << " %+#{val.is_a?(Float) ? 'f' : 'd' }%s" % [coefficient, var]
|
44
51
|
end
|
45
52
|
equation = equation.gsub(/(\+|-)/, '\1 ').gsub(/1([a-z])/, '\1').gsub(/^ (-)?\+? /, '\1')
|
46
53
|
equation << " = #{total}"
|
@@ -48,18 +55,19 @@ module EquationSystem
|
|
48
55
|
raise RangeTooSmallError, "Unable to get unique equation after #{failsafe} tries" unless fails == failsafe - 1
|
49
56
|
else
|
50
57
|
equations << equation
|
58
|
+
@coefficients << {variables: temp, sum: total}
|
51
59
|
break
|
52
60
|
end
|
53
|
-
end
|
61
|
+
end
|
54
62
|
end
|
55
63
|
equations
|
56
64
|
end
|
57
|
-
|
65
|
+
|
58
66
|
def generate_answers(variables, range, allow_zero)
|
59
67
|
ret = {}
|
60
68
|
vars = []
|
61
|
-
if variables <=
|
62
|
-
vars = %w(x y z)[0, variables]
|
69
|
+
if variables <= 4 && variables > 0
|
70
|
+
vars = %w(x y z w)[0, variables].sort
|
63
71
|
elsif variables > 3
|
64
72
|
cur = 'a'
|
65
73
|
variables.times do
|
@@ -73,10 +81,10 @@ module EquationSystem
|
|
73
81
|
vars.each do |var|
|
74
82
|
ret[var.to_sym] = random_num(range, allow_zero)
|
75
83
|
end
|
76
|
-
|
84
|
+
|
77
85
|
ret
|
78
86
|
end
|
79
|
-
|
87
|
+
|
80
88
|
def random_num(range, allow_zero)
|
81
89
|
r = 0
|
82
90
|
begin
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: equation_system
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Winston Durand
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-12-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -96,7 +96,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
96
96
|
version: '0'
|
97
97
|
requirements: []
|
98
98
|
rubyforge_project:
|
99
|
-
rubygems_version: 2.
|
99
|
+
rubygems_version: 2.4.5
|
100
100
|
signing_key:
|
101
101
|
specification_version: 4
|
102
102
|
summary: Generates a system of equations.
|
@@ -104,3 +104,4 @@ test_files:
|
|
104
104
|
- spec/equation_system_spec.rb
|
105
105
|
- spec/spec_helper.rb
|
106
106
|
- spec/system_spec.rb
|
107
|
+
has_rdoc:
|