danica 2.0.3 → 2.0.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 988951405b8e2e23338da2bb9a47c090e27c42e8
4
- data.tar.gz: 5542a1e13081409b7fd5c318c3092d08acd70849
3
+ metadata.gz: 2c3aa11c78083f2cf4dbf106b5edc4ae7370dc2a
4
+ data.tar.gz: d16d37bfebfd039a1469dec4029f927cd5374fed
5
5
  SHA512:
6
- metadata.gz: 1ddcef37ee4cc2c80f566a64829645fb9f8ad3a3189fa0470c2bdee03d31d035bd00fd900e70cba579bfbe7a7ab37a13c850f4f855cf8c775b5a2988cb5b7e24
7
- data.tar.gz: b8d6b8d3c6d590b4b5d9a76e4cc41cc0fb9b3fd06b7dd48253a162b9c2b7dd2d2b0b5e9b23f73b3703c6fa6b4cf549944cc21eaa0aab108017da646146db1342
6
+ metadata.gz: ec1d1e75b021be3608dd5f2aae2662fc8104d9cbd4b10e27fb4d6ca8aeb8c5c09542e58443799284c30965f4d3d5b5d2d5f5dbd659645c7b36d87b143f1d933a
7
+ data.tar.gz: de6db957793d4f43a74b5c9c311ed82cafdd6f016dbe260b7d09d66418297d1d35eacf47a6909d8376efaf3ab33f3201c190d5bc48e40dcbe546b1a6d1729083
data/README.md CHANGED
@@ -125,11 +125,6 @@ end
125
125
 
126
126
  #### Sample
127
127
  ```ruby
128
- require 'danica/operator/product'
129
- require 'danica/operator/sum'
130
- require 'danica/operator/division'
131
- require 'danica/operator/power'
132
-
133
128
  module Danica
134
129
  class Function::Spatial < Function
135
130
  variables :time, :acceleration, :initial_space, :initial_velocity
@@ -0,0 +1,10 @@
1
+ dependencies:
2
+ post:
3
+ - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
4
+ - chmod +x ./cc-test-reporter
5
+
6
+ test:
7
+ pre:
8
+ - ./cc-test-reporter before-build
9
+ override:
10
+ - bundle exec rspec; ./cc-test-reporter after-build --exit-code $?
@@ -26,6 +26,4 @@ Gem::Specification.new do |spec|
26
26
  spec.add_development_dependency 'rspec-mocks'
27
27
  spec.add_development_dependency 'pry-nav'
28
28
  spec.add_development_dependency 'simplecov'
29
- spec.add_development_dependency 'codeclimate-test-reporter'
30
-
31
29
  end
@@ -4,6 +4,8 @@ module Danica
4
4
  autoload :BaseOperations, 'danica/base_operations'
5
5
  autoload :Common, 'danica/common'
6
6
  autoload :Number, 'danica/number'
7
+ autoload :Negative, 'danica/negative'
8
+ autoload :Group, 'danica/group'
7
9
  autoload :Variable, 'danica/variable'
8
10
  autoload :Operator, 'danica/operator'
9
11
  autoload :Function, 'danica/function'
@@ -18,5 +20,8 @@ module Danica
18
20
  autoload :Exponential, 'danica/exponential'
19
21
  autoload :Sin, 'danica/sin'
20
22
  autoload :Cos, 'danica/cos'
23
+
24
+ E = Constant.new(Math::E, :e, 'exp(1)')
25
+ PI = Constant.new(Math::PI, '\pi', :pi)
21
26
  end
22
27
 
@@ -1,7 +1,21 @@
1
1
  module Danica
2
2
  module BaseOperations
3
3
  def +(other)
4
- return Sum.new(self, other)
4
+ return other + self if other.is_a?(Sum)
5
+ Sum.new(self, other)
6
+ end
7
+
8
+ def *(other)
9
+ return other * self if other.is_a?(Product)
10
+ Product.new(self, other)
11
+ end
12
+
13
+ def /(other)
14
+ Division.new(self, other)
15
+ end
16
+
17
+ def -(other)
18
+ self + Negative.new(other)
5
19
  end
6
20
  end
7
21
  end
@@ -5,22 +5,10 @@ module Danica
5
5
  require 'danica/common/variables_builder'
6
6
 
7
7
  attr_accessor :variables
8
-
8
+
9
9
  def to_f
10
10
  raise 'Not IMplemented yet'
11
11
  end
12
-
13
- def calculate(*args)
14
- vars_map = args.extract_options!
15
- vars_map = variables_value_hash.merge(vars_map)
16
- vars_map.each do |k, v|
17
- unless v && (v.is_a?(Integer) || v.valued?)
18
- vars_map[k] = args.shift
19
- end
20
- end
21
-
22
- self.class.new(vars_map).to_f
23
- end
24
12
 
25
13
  def to_tex
26
14
  Number.new(to_f).to_tex
@@ -74,7 +62,7 @@ module Danica
74
62
  return Number.new(value) if value.is_a?(Numeric)
75
63
  return Variable.new(value) if value.is_a?(Hash)
76
64
  return Variable.new(name: value) if [ String, Symbol ].any? { |c| value.is_a?(c) }
77
- return Variable.new if value == nil
65
+ return Variable.new if value.nil?
78
66
  value
79
67
  end
80
68
  end
@@ -31,8 +31,5 @@ module Danica
31
31
  true
32
32
  end
33
33
  end
34
-
35
- E = Constant.new(Math::E, :e, 'exp(1)')
36
- PI = Constant.new(Math::PI, '\pi', :pi)
37
34
  end
38
35
 
@@ -9,5 +9,17 @@ module Danica
9
9
 
10
10
  super({ variables: args.flatten }.merge(options))
11
11
  end
12
+
13
+ def calculate(*args)
14
+ vars_map = args.extract_options!
15
+ vars_map = variables_value_hash.merge(vars_map)
16
+ vars_map.each do |k, v|
17
+ unless v && (v.is_a?(Integer) || v.valued?)
18
+ vars_map[k] = args.shift
19
+ end
20
+ end
21
+
22
+ self.class.new(vars_map).to_f
23
+ end
12
24
  end
13
25
  end
@@ -0,0 +1,21 @@
1
+ module Danica
2
+ class Group < Common
3
+ include ActiveModel::Model
4
+
5
+ attr_accessor :value
6
+
7
+ delegate :to_f, :valued?, to: :value
8
+
9
+ def initialize(value)
10
+ @value = wrap_value(value)
11
+ end
12
+
13
+ def to_tex
14
+ "(#{value.to_tex})"
15
+ end
16
+
17
+ alias_method :to_gnu, :to_tex
18
+ end
19
+ end
20
+
21
+
@@ -0,0 +1,31 @@
1
+ module Danica
2
+ class Negative < Common
3
+ include ActiveModel::Model
4
+
5
+ attr_accessor :value
6
+
7
+ delegate :valued?, to: :value
8
+
9
+ def initialize(value)
10
+ @value = wrap_value(value)
11
+ end
12
+
13
+ def to_f
14
+ -value.to_f
15
+ end
16
+
17
+ def to_tex
18
+ "-#{value.to_tex}"
19
+ end
20
+
21
+ def to_gnu
22
+ "-#{value.to_gnu}"
23
+ end
24
+
25
+ def ==(other)
26
+ return false unless other.class == self.class
27
+ value == other.value
28
+ end
29
+ end
30
+ end
31
+
@@ -20,8 +20,12 @@ module Danica
20
20
  value.present?
21
21
  end
22
22
 
23
+ def ==(other)
24
+ return false unless other.class == self.class
25
+ value == other.value
26
+ end
27
+
23
28
  alias_method :to_gnu, :to_tex
24
29
  end
25
30
  end
26
31
 
27
-
@@ -5,8 +5,18 @@ module Danica
5
5
  chain(variables.map(&:to_f))
6
6
  end
7
7
 
8
+ def include?(value)
9
+ value = wrap_value(value)
10
+ variables.include?(value)
11
+ end
12
+
8
13
  private
9
14
 
15
+ def repack(other)
16
+ other_variables = other.is_a?(self.class) ? other.variables : [ other ]
17
+ self.class.new(variables + other_variables)
18
+ end
19
+
10
20
  def tex_string
11
21
  (numeric_to_tex + non_numeric_variables.map(&:to_tex)).join(" #{tex_symbol} ")
12
22
  end
@@ -2,6 +2,10 @@ require 'danica/operator/chained'
2
2
 
3
3
  module Danica
4
4
  class Product < Operator::Chained
5
+ def *(other)
6
+ repack(other)
7
+ end
8
+
5
9
  private
6
10
 
7
11
  def tex_symbol
@@ -2,6 +2,10 @@ require 'danica/operator/chained'
2
2
 
3
3
  module Danica
4
4
  class Sum < Operator::Chained
5
+ def +(other)
6
+ repack(other)
7
+ end
8
+
5
9
  private
6
10
 
7
11
  def tex_symbol
@@ -1,3 +1,3 @@
1
1
  module Danica
2
- VERSION = '2.0.3'
2
+ VERSION = '2.0.4'
3
3
  end
@@ -4,7 +4,8 @@ describe Danica::Constant do
4
4
  subject { described_class.new(2.5, :M, :m) }
5
5
  let(:other) { described_class.new(3, :N, :n) }
6
6
 
7
- it_behaves_like 'an object with + operation'
7
+ it_behaves_like 'an object that respond to basic_methods'
8
+ it_behaves_like 'an object with basic operation'
8
9
 
9
10
  describe '#to_f' do
10
11
  it 'has a value' do
@@ -1,9 +1,10 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Danica::Cos do
4
- it_behaves_like 'an object with + operation' do
5
- subject { described_class.new(:x) }
6
- end
4
+ subject { described_class.new(:x) }
5
+
6
+ it_behaves_like 'an object that respond to basic_methods'
7
+ it_behaves_like 'an object with basic operation'
7
8
 
8
9
  it_behaves_like 'a operator with a single input value', {
9
10
  variable_value: Math::PI,
@@ -4,7 +4,8 @@ describe Danica::Division do
4
4
  let(:variables) { [2, 4] }
5
5
  subject { described_class.new(*variables) }
6
6
 
7
- it_behaves_like 'an object with + operation'
7
+ it_behaves_like 'an object that respond to basic_methods'
8
+ it_behaves_like 'an object with basic operation'
8
9
 
9
10
  it_behaves_like 'a operator that has two terms', :division, {
10
11
  values: [ 2, 4 ],
@@ -3,7 +3,8 @@ require 'spec_helper'
3
3
  describe Danica::Exponential do
4
4
  subject { described_class.new(2) }
5
5
 
6
- it_behaves_like 'an object with + operation'
6
+ it_behaves_like 'an object that respond to basic_methods'
7
+ it_behaves_like 'an object with basic operation'
7
8
 
8
9
  it_behaves_like 'a operator with a single input value', {
9
10
  variable_value: 2,
@@ -1,247 +1,250 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Danica::Function do
4
- module Danica
5
- class Function::Spatial < Function
6
- variables :time, :acceleration, :initial_space, :initial_velocity
7
- delegate :to_f, :to_tex, :to_gnu, to: :sum
8
-
9
- private
10
-
11
- def sum
12
- @sum ||= Sum.new(parcels)
13
- end
14
-
15
- def parcels
16
- [
17
- initial_space,
18
- spatial_velocity,
19
- spatial_acceleration
20
- ]
21
- end
22
-
23
- def spatial_velocity
24
- Product.new(initial_velocity, time)
25
- end
26
-
27
- def spatial_acceleration
28
- Division.new(Product.new(acceleration, time_squared), 2)
29
- end
30
-
31
- def time_squared
32
- Power.new(time, 2)
33
- end
34
- end
35
- end
36
-
37
- let(:variables) do
38
- {
39
- time: :t,
40
- acceleration: 'a',
41
- initial_space: { name: :S0, latex: 'S_0' },
42
- initial_velocity: { name: :V0, latex: 'V_0' }
43
- }
44
- end
45
-
46
- subject { described_class::Spatial.new(variables) }
47
-
48
- describe '#to_tex' do
49
- context 'when creating the spatial operator for constantly accelerated movement' do
50
- let(:expected) { 'S_0 + V_0 \cdot t + \frac{a \cdot t^{2}}{2}' }
51
-
52
- it 'return the latex format CAM' do
53
- expect(subject.to_tex).to eq(expected)
54
- end
55
- end
56
- end
57
-
58
- describe '#to_gnu' do
59
- context 'when creating the spatial operator for constantly accelerated movement' do
60
- let(:expected) { 'S0 + V0 * t + a * t**2/2' }
61
-
62
- it 'return the latex format CAM' do
63
- expect(subject.to_gnu).to eq(expected)
64
- end
65
- end
66
- end
67
-
68
- describe '#variables_hash' do
69
- let(:expected) do
4
+ describe 'spatial' do
5
+ let(:variables) do
70
6
  {
71
- time: Danica::Variable.new(name: :t),
72
- acceleration: Danica::Variable.new(name: 'a'),
73
- initial_space: Danica::Variable.new( name: :S0, latex: 'S_0' ),
74
- initial_velocity: Danica::Variable.new( name: :V0, latex: 'V_0' )
7
+ time: :t,
8
+ acceleration: 'a',
9
+ initial_space: { name: :S0, latex: 'S_0' },
10
+ initial_velocity: { name: :V0, latex: 'V_0' }
75
11
  }
76
12
  end
77
13
 
78
- context 'when variables are already wrapped with DanicaVariable' do
79
- let(:variables) { expected }
80
- it 'returns a hash with the variabels' do
81
- expect(subject.variables_hash).to eq(expected)
82
- end
83
- end
14
+ subject { described_class::Spatial.new(variables) }
15
+ it_behaves_like 'an object that respond to basic_methods'
84
16
 
85
- context 'when variables have been defined with string name' do
86
- before do
87
- variables.change_keys!(&:to_s)
88
- end
17
+ describe '#to_tex' do
18
+ context 'when creating the spatial operator for constantly accelerated movement' do
19
+ let(:expected) { 'S_0 + V_0 \cdot t + \frac{a \cdot t^{2}}{2}' }
89
20
 
90
- it 'returns a hash with the variabels' do
91
- expect(subject.variables_hash).to eq(expected)
21
+ it 'return the latex format CAM' do
22
+ expect(subject.to_tex).to eq(expected)
23
+ end
92
24
  end
93
25
  end
94
26
 
95
- context 'when variables are not wrapped yet' do
96
- it 'returns a hash with the variabels' do
97
- expect(subject.variables_hash).to eq(expected)
27
+ describe '#to_gnu' do
28
+ context 'when creating the spatial operator for constantly accelerated movement' do
29
+ let(:expected) { 'S0 + V0 * t + a * t**2/2' }
30
+
31
+ it 'return the latex format CAM' do
32
+ expect(subject.to_gnu).to eq(expected)
33
+ end
98
34
  end
99
35
  end
100
36
 
101
- context 'when changing a variable' do
102
- before do
103
- subject.time = :x
104
- expected[:time] = Danica::Variable.new(name: :x)
37
+ describe '#variables_hash' do
38
+ let(:expected) do
39
+ {
40
+ time: Danica::Variable.new(name: :t),
41
+ acceleration: Danica::Variable.new(name: 'a'),
42
+ initial_space: Danica::Variable.new( name: :S0, latex: 'S_0' ),
43
+ initial_velocity: Danica::Variable.new( name: :V0, latex: 'V_0' )
44
+ }
105
45
  end
106
46
 
107
- it do
108
- expect(subject.variables_hash).to eq(expected)
47
+ context 'when variables are already wrapped with DanicaVariable' do
48
+ let(:variables) { expected }
49
+ it 'returns a hash with the variabels' do
50
+ expect(subject.variables_hash).to eq(expected)
51
+ end
109
52
  end
110
- end
111
53
 
112
- context 'when initializing with array' do
113
- context 'as hash' do
114
- let(:variables) { [ :t, 'a', {name: :S0, latex: 'S_0'}, { name: :V0, latex: 'V_0' } ] }
115
- subject { described_class::Spatial.new(variables) }
54
+ context 'when variables have been defined with string name' do
55
+ before do
56
+ variables.change_keys!(&:to_s)
57
+ end
116
58
 
117
59
  it 'returns a hash with the variabels' do
118
60
  expect(subject.variables_hash).to eq(expected)
119
61
  end
120
62
  end
121
- end
122
-
123
- context 'when initializing with sequence' do
124
- context 'as hash' do
125
- let(:variables) { [ :t, 'a', {name: :S0, latex: 'S_0'}, { name: :V0, latex: 'V_0' } ] }
126
- subject { described_class::Spatial.new(*variables, {}) }
127
63
 
64
+ context 'when variables are not wrapped yet' do
128
65
  it 'returns a hash with the variabels' do
129
66
  expect(subject.variables_hash).to eq(expected)
130
67
  end
131
68
  end
132
- end
133
69
 
134
- context 'when initializing with variables array' do
135
- context 'as hash' do
136
- let(:variables) { [ :t, 'a', {name: :S0, latex: 'S_0'}, { name: :V0, latex: 'V_0' } ] }
137
- subject { described_class::Spatial.new(variables: variables) }
70
+ context 'when changing a variable' do
71
+ before do
72
+ subject.time = :x
73
+ expected[:time] = Danica::Variable.new(name: :x)
74
+ end
138
75
 
139
- it 'returns a hash with the variabels' do
76
+ it do
140
77
  expect(subject.variables_hash).to eq(expected)
141
78
  end
142
79
  end
143
- end
144
- end
145
80
 
146
- describe '#variables' do
147
- context 'when initialized with an array of variables' do
148
- subject { described_class::Spatial.new(variables: variables.values) }
149
- let(:expected) { variables.values.map { |v| subject.send(:wrap_value, v)} }
150
- it do
151
- expect(subject.variables.compact).to eq(expected)
81
+ context 'when initializing with array' do
82
+ context 'as hash' do
83
+ let(:variables) { [ :t, 'a', {name: :S0, latex: 'S_0'}, { name: :V0, latex: 'V_0' } ] }
84
+ subject { described_class::Spatial.new(variables) }
85
+
86
+ it 'returns a hash with the variabels' do
87
+ expect(subject.variables_hash).to eq(expected)
88
+ end
89
+ end
152
90
  end
153
- end
154
91
 
155
- context 'when not initializing all variables' do
156
- subject { described_class::Spatial.new }
157
- let(:time) { Danica::Variable.new(name: :t) }
92
+ context 'when initializing with sequence' do
93
+ context 'as hash' do
94
+ let(:variables) { [ :t, 'a', {name: :S0, latex: 'S_0'}, { name: :V0, latex: 'V_0' } ] }
95
+ subject { described_class::Spatial.new(*variables, {}) }
158
96
 
159
- context 'when initialized with an empty variable set' do
160
- it do
161
- expect(subject.variables.compact).to be_empty
97
+ it 'returns a hash with the variabels' do
98
+ expect(subject.variables_hash).to eq(expected)
99
+ end
162
100
  end
163
101
  end
164
102
 
165
- context 'when changing a variable' do
166
- before do
167
- subject.time = time
103
+ context 'when initializing with variables array' do
104
+ context 'as hash' do
105
+ let(:variables) { [ :t, 'a', {name: :S0, latex: 'S_0'}, { name: :V0, latex: 'V_0' } ] }
106
+ subject { described_class::Spatial.new(variables: variables) }
107
+
108
+ it 'returns a hash with the variabels' do
109
+ expect(subject.variables_hash).to eq(expected)
110
+ end
168
111
  end
112
+ end
113
+ end
169
114
 
170
- it 'returns the list of variables' do
171
- expect(subject.variables.compact).to eq([ time ])
115
+ describe '#variables' do
116
+ context 'when initialized with an array of variables' do
117
+ subject { described_class::Spatial.new(variables: variables.values) }
118
+ let(:expected) { variables.values.map { |v| subject.send(:wrap_value, v)} }
119
+ it do
120
+ expect(subject.variables.compact).to eq(expected)
172
121
  end
173
122
  end
174
123
 
175
- context 'when initializing with a variable set' do
176
- let(:names) { [ :t, :a, :s0, :v0 ] }
177
- subject { described_class::Spatial.new *names }
124
+ context 'when not initializing all variables' do
125
+ subject { described_class::Spatial.new }
126
+ let(:time) { Danica::Variable.new(name: :t) }
178
127
 
179
- it 'returns the variables given oin initialization' do
180
- expect(subject.variables.map(&:name)).to eq(names)
128
+ context 'when initialized with an empty variable set' do
129
+ it do
130
+ expect(subject.variables.compact).to be_empty
131
+ end
181
132
  end
182
133
 
183
- context 'when initializing variables with a hash out of order' do
184
- let(:variables) do
185
- {
186
- initial_velocity: :v0,
187
- initial_space: :s0,
188
- acceleration: :a,
189
- time: :t
190
- }
134
+ context 'when changing a variable' do
135
+ before do
136
+ subject.time = time
191
137
  end
192
- subject { described_class::Spatial.new variables }
193
138
 
194
- it 'returns the variables given on initialization' do
139
+ it 'returns the list of variables' do
140
+ expect(subject.variables.compact).to eq([ time ])
141
+ end
142
+ end
143
+
144
+ context 'when initializing with a variable set' do
145
+ let(:names) { [ :t, :a, :s0, :v0 ] }
146
+ subject { described_class::Spatial.new *names }
147
+
148
+ it 'returns the variables given oin initialization' do
195
149
  expect(subject.variables.map(&:name)).to eq(names)
196
150
  end
151
+
152
+ context 'when initializing variables with a hash out of order' do
153
+ let(:variables) do
154
+ {
155
+ initial_velocity: :v0,
156
+ initial_space: :s0,
157
+ acceleration: :a,
158
+ time: :t
159
+ }
160
+ end
161
+ subject { described_class::Spatial.new variables }
162
+
163
+ it 'returns the variables given on initialization' do
164
+ expect(subject.variables.map(&:name)).to eq(names)
165
+ end
166
+ end
197
167
  end
198
168
  end
199
169
  end
200
- end
201
170
 
202
- describe '#calculate' do
203
- context 'when all variables have value' do
204
- let(:time_value) { 2 }
205
- let(:time) { time_value }
206
- let(:acceleration) { 3 }
207
- let(:initial_space) { 1 }
208
- let(:initial_velocity) { 1 }
209
- subject { described_class::Spatial.new(time, acceleration, initial_space, initial_velocity) }
210
- let(:expected) { initial_space + initial_velocity * time_value + acceleration * (time_value ** 2) / 2.0 }
211
-
212
- it 'retuirns the calculated value' do
213
- expect(subject.calculate).to eq(expected)
214
- end
171
+ describe '#calculate' do
172
+ context 'when all variables have value' do
173
+ let(:time_value) { 2 }
174
+ let(:time) { time_value }
175
+ let(:acceleration) { 3 }
176
+ let(:initial_space) { 1 }
177
+ let(:initial_velocity) { 1 }
178
+ subject { described_class::Spatial.new(time, acceleration, initial_space, initial_velocity) }
179
+ let(:expected) { initial_space + initial_velocity * time_value + acceleration * (time_value ** 2) / 2.0 }
180
+
181
+ it 'retuirns the calculated value' do
182
+ expect(subject.calculate).to eq(expected)
183
+ end
215
184
 
216
- context 'when not all variables have value' do
217
- let(:time) { :t }
185
+ context 'when not all variables have value' do
186
+ let(:time) { :t }
218
187
 
219
- it do
220
- expect { subject.calculate }.to raise_error(Danica::Exception::NotDefined)
221
- end
188
+ it do
189
+ expect { subject.calculate }.to raise_error(Danica::Exception::NotDefined)
190
+ end
222
191
 
223
- context 'but calling calculate with a value for the variables' do
224
- it 'calculate using the given value' do
225
- expect(subject.calculate(time_value)).to eq(expected)
192
+ context 'but calling calculate with a value for the variables' do
193
+ it 'calculate using the given value' do
194
+ expect(subject.calculate(time_value)).to eq(expected)
195
+ end
196
+
197
+ it 'does not change the values of then valued variables' do
198
+ expect do
199
+ subject.calculate(time_value)
200
+ end.not_to change(subject.time, :valued?)
201
+ end
226
202
  end
227
203
 
228
- it 'does not change the values of then valued variables' do
229
- expect do
230
- subject.calculate(time_value)
231
- end.not_to change(subject.time, :valued?)
204
+ context 'when calling with a hash for the values' do
205
+ it 'calculate using the given value' do
206
+ expect(subject.calculate(time: time_value)).to eq(expected)
207
+ end
208
+
209
+ it 'does not change the values of then valued variables' do
210
+ expect do
211
+ subject.calculate(time: time_value)
212
+ end.not_to change(subject.time, :valued?)
213
+ end
232
214
  end
233
215
  end
216
+ end
217
+ end
218
+ end
234
219
 
235
- context 'when calling with a hash for the values' do
236
- it 'calculate using the given value' do
237
- expect(subject.calculate(time: time_value)).to eq(expected)
238
- end
220
+ describe 'gauss' do
221
+ let(:variables) do
222
+ {
223
+ x: :x,
224
+ median: :u,
225
+ variance_root: :v
226
+ }
227
+ end
239
228
 
240
- it 'does not change the values of then valued variables' do
241
- expect do
242
- subject.calculate(time: time_value)
243
- end.not_to change(subject.time, :valued?)
244
- end
229
+ subject { described_class::Gauss.new(variables) }
230
+ it_behaves_like 'an object that respond to basic_methods'
231
+
232
+ describe '#to_tex' do
233
+ context 'when creating the spatial operator for constantly accelerated movement' do
234
+ let(:expected) { '\frac{1}{\sqrt{6.283185307179586 \cdot v^{2}}} \cdot e^{-\frac{(x + -u)^{2}}{2 \cdot v^{2}}}' }
235
+
236
+ it 'return the latex format CAM' do
237
+ expect(subject.to_tex).to eq(expected)
238
+ end
239
+ end
240
+ end
241
+
242
+ describe '#to_gnu' do
243
+ context 'when creating the spatial operator for constantly accelerated movement' do
244
+ let(:expected) { '1/sqrt(6.283185307179586 * v**2) * exp(-(x + -u)**2/2 * v**2)' }
245
+
246
+ it 'return the latex format CAM' do
247
+ expect(subject.to_gnu).to eq(expected)
245
248
  end
246
249
  end
247
250
  end
@@ -0,0 +1,52 @@
1
+ require 'spec_helper'
2
+
3
+ describe Danica::Negative do
4
+ let(:value) { Danica::Number.new(10) }
5
+ subject { described_class.new(value) }
6
+
7
+ it_behaves_like 'an object that respond to basic_methods'
8
+
9
+ describe '#to_f' do
10
+ it 'returns the float of value' do
11
+ expect(subject.to_f).to eq(-10)
12
+ end
13
+
14
+ it { expect(subject.to_f).to be_a(Float) }
15
+ end
16
+
17
+ describe '#to_tex' do
18
+ context 'when value should be integer' do
19
+ let(:value) { 10.0 }
20
+
21
+ it 'returns the value integer string' do
22
+ expect(subject.to_tex).to eq('-10')
23
+ end
24
+ end
25
+
26
+ context 'when value should be float' do
27
+ let(:value) { 10.5 }
28
+
29
+ it 'returns the value float string' do
30
+ expect(subject.to_tex).to eq('-10.5')
31
+ end
32
+ end
33
+ end
34
+
35
+ describe '#to_gnu' do
36
+ context 'when value should be integer' do
37
+ let(:value) { 10.0 }
38
+
39
+ it 'returns the value integer string' do
40
+ expect(subject.to_gnu).to eq('-10')
41
+ end
42
+ end
43
+
44
+ context 'when value should be a float' do
45
+ let(:value) { 10.5 }
46
+
47
+ it 'returns the value float string' do
48
+ expect(subject.to_gnu).to eq('-10.5')
49
+ end
50
+ end
51
+ end
52
+ end
@@ -4,7 +4,9 @@ describe Danica::Number do
4
4
  let(:value) { 10 }
5
5
  subject { described_class.new(value) }
6
6
 
7
- it_behaves_like 'an object with + operation'
7
+ it_behaves_like 'an object that respond to basic_methods'
8
+
9
+ it_behaves_like 'an object with basic operation'
8
10
 
9
11
  describe '#to_f' do
10
12
  it 'returns the float of value' do
@@ -23,10 +25,10 @@ describe Danica::Number do
23
25
  end
24
26
  end
25
27
 
26
- context 'when value should be integer' do
28
+ context 'when value should be a float' do
27
29
  let(:value) { 10.5 }
28
30
 
29
- it 'returns the value integer string' do
31
+ it 'returns the value float string' do
30
32
  expect(subject.to_tex).to eq('10.5')
31
33
  end
32
34
  end
@@ -4,7 +4,8 @@ describe Danica::Power do
4
4
  let(:variables) { [2, 4] }
5
5
  subject { described_class.new(*variables) }
6
6
 
7
- it_behaves_like 'an object with + operation'
7
+ it_behaves_like 'an object that respond to basic_methods'
8
+ it_behaves_like 'an object with basic operation'
8
9
 
9
10
  it_behaves_like 'a operator that has two terms', :power, {
10
11
  values: [ 3, 2 ],
@@ -2,8 +2,20 @@ require 'spec_helper'
2
2
 
3
3
  describe Danica::Product do
4
4
  subject { described_class.new(2,4) }
5
+ it_behaves_like 'an object that respond to basic_methods'
5
6
 
6
- it_behaves_like 'an object with + operation'
7
+ it_behaves_like 'an object with * operation' do
8
+ let(:subject_included) { 4 }
9
+
10
+ context 'when other is also a sum' do
11
+ let(:other) { described_class.new(200, 5) }
12
+
13
+ it 'includes the sum parcels' do
14
+ expect(result).to include(200)
15
+ end
16
+ end
17
+ end
18
+ it_behaves_like 'an object with basic operation', ignore: :*
7
19
 
8
20
  it_behaves_like 'a operator that joins many variables with same operation', {
9
21
  calculated: 24,
@@ -3,7 +3,8 @@ require 'spec_helper'
3
3
  describe Danica::Sin do
4
4
  subject { described_class.new(10) }
5
5
 
6
- it_behaves_like 'an object with + operation'
6
+ it_behaves_like 'an object that respond to basic_methods'
7
+ it_behaves_like 'an object with basic operation'
7
8
 
8
9
  it_behaves_like 'a operator with a single input value', {
9
10
  variable_value: Math::PI / 2.0,
@@ -3,7 +3,8 @@ require 'spec_helper'
3
3
  describe Danica::SquaredRoot do
4
4
  subject { described_class.new(9) }
5
5
 
6
- it_behaves_like 'an object with + operation'
6
+ it_behaves_like 'an object that respond to basic_methods'
7
+ it_behaves_like 'an object with basic operation'
7
8
 
8
9
  it_behaves_like 'a operator with a single input value', {
9
10
  variable_value: 9,
@@ -3,7 +3,23 @@ require 'spec_helper'
3
3
  describe Danica::Sum do
4
4
  subject { described_class.new(10, 2) }
5
5
 
6
- it_behaves_like 'an object with + operation'
6
+ it_behaves_like 'an object that respond to basic_methods'
7
+
8
+ it_behaves_like 'an object with basic operation', operations: %i(* /)
9
+ it_behaves_like 'an object with + operation' do
10
+ let(:subject_included) { 10 }
11
+
12
+ context 'when other is also a sum' do
13
+ let(:other) { described_class.new(200, 5) }
14
+
15
+ it 'includes the sum parcels' do
16
+ expect(result).to include(200)
17
+ end
18
+ end
19
+ end
20
+ it_behaves_like 'an object with - operation' do
21
+ let(:subject_included) { 10 }
22
+ end
7
23
 
8
24
  it_behaves_like 'a operator that joins many variables with same operation', {
9
25
  calculated: 10,
@@ -1,7 +1,9 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Danica::Variable do
4
- it_behaves_like 'an object with + operation' do
4
+ it_behaves_like 'an object that respond to basic_methods'
5
+
6
+ it_behaves_like 'an object with basic operation' do
5
7
  subject { described_class.new(value: 100) }
6
8
  end
7
9
 
@@ -4,11 +4,6 @@ SimpleCov.profiles.define 'gem' do
4
4
  add_filter '/spec/'
5
5
  end
6
6
 
7
- if ENV['CODECLIMATE_REPO_TOKEN']
8
- require 'codeclimate-test-reporter'
9
- CodeClimate::TestReporter.start
10
- end
11
-
12
7
  SimpleCov.start 'gem'
13
8
  require 'pry-nav'
14
9
  require 'danica'
@@ -0,0 +1,41 @@
1
+ module Danica
2
+ class Function::Gauss < Function
3
+ variables :x, :median, :variance_root
4
+ delegate :to_f, :to_tex, :to_gnu, to: :product
5
+
6
+ private
7
+
8
+ def product
9
+ @sum ||= Product.new(parcels)
10
+ end
11
+
12
+ def parcels
13
+ [
14
+ Division.new(1, denominator),
15
+ Exponential.new(exponential)
16
+ ]
17
+ end
18
+
19
+ def denominator
20
+ SquaredRoot.new(
21
+ Product.new(2, PI, variance),
22
+ )
23
+ end
24
+
25
+ def exponential
26
+ Negative.new(
27
+ Division.new(
28
+ Power.new(Group.new(
29
+ Sum.new(x, Negative.new(median))
30
+ ), 2),
31
+ Product.new(2, variance)
32
+ )
33
+ )
34
+ end
35
+
36
+ def variance
37
+ @variance ||= Power.new(variance_root, 2)
38
+ end
39
+ end
40
+ end
41
+
@@ -0,0 +1,32 @@
1
+ module Danica
2
+ class Function::Spatial < Function
3
+ variables :time, :acceleration, :initial_space, :initial_velocity
4
+ delegate :to_f, :to_tex, :to_gnu, to: :sum
5
+
6
+ private
7
+
8
+ def sum
9
+ @sum ||= Sum.new(parcels)
10
+ end
11
+
12
+ def parcels
13
+ [
14
+ initial_space,
15
+ spatial_velocity,
16
+ spatial_acceleration
17
+ ]
18
+ end
19
+
20
+ def spatial_velocity
21
+ Product.new(initial_velocity, time)
22
+ end
23
+
24
+ def spatial_acceleration
25
+ Division.new(Product.new(acceleration, time_squared), 2)
26
+ end
27
+
28
+ def time_squared
29
+ Power.new(time, 2)
30
+ end
31
+ end
32
+ end
@@ -1,4 +1,77 @@
1
+ shared_examples 'an object with basic operation' do |operations:%i(+ - * /), ignore:[]|
2
+ (operations - [ ignore ].flatten).each do |operation|
3
+ it_behaves_like "an object with #{operation} operation"
4
+ end
5
+ end
6
+
7
+ shared_examples 'an object with an operation' do |clazz|
8
+ it { expect(result).to be_a(clazz) }
9
+
10
+ it 'includes other as parcel' do
11
+ expect(result).to be_include(other)
12
+ end
13
+
14
+ it 'includes the subject as parcel' do
15
+ expect(result).to be_include(subject_included)
16
+ end
17
+ end
18
+
1
19
  shared_examples 'an object with + operation' do
2
20
  let(:other) { 104 }
3
- it { expect(subject + other).to be_a(Danica::Sum) }
21
+ let(:result) { subject + other }
22
+ let(:subject_included) { subject }
23
+
24
+ it_behaves_like 'an object with an operation', Danica::Sum
25
+
26
+ context 'when operating as reverse' do
27
+ let(:result) { Danica::Number.new(other) + subject }
28
+ it_behaves_like 'an object with an operation', Danica::Sum
29
+ end
30
+ end
31
+
32
+ shared_examples 'an object with * operation' do
33
+ let(:other) { 104 }
34
+ let(:result) { subject * other }
35
+ let(:subject_included) { subject }
36
+
37
+ it_behaves_like 'an object with an operation', Danica::Product
38
+
39
+ context 'when operating as reverse' do
40
+ let(:result) { Danica::Number.new(other) * subject }
41
+ it_behaves_like 'an object with an operation', Danica::Product
42
+ end
43
+ end
44
+
45
+ shared_examples 'an object with / operation' do
46
+ let(:other) { 104 }
47
+ let(:other_number) { Danica::Number.new(104) }
48
+ let(:result) { subject / other }
49
+
50
+ it { expect(result).to be_a(Danica::Division) }
51
+
52
+ it 'includes other as denominator' do
53
+ expect(result.denominator).to eq(other_number)
54
+ end
55
+
56
+ it 'includes the subject as numerator' do
57
+ expect(result.numerator).to eq(subject)
58
+ end
59
+ end
60
+
61
+ shared_examples 'an object with - operation' do
62
+ let(:other) { 104 }
63
+ let(:negative_other) { Danica::Negative.new(other) }
64
+ let(:result) { subject - other }
65
+ let(:subject_included) { subject }
66
+
67
+ it { expect(result).to be_a(Danica::Sum) }
68
+
69
+ it 'includes other as negative parcel' do
70
+ expect(result).to be_include(negative_other)
71
+ end
72
+
73
+ it 'includes the subject as parcel' do
74
+ expect(result).to be_include(subject_included)
75
+ end
4
76
  end
77
+
@@ -0,0 +1,6 @@
1
+ shared_examples 'an object that respond to basic_methods' do |ignore: [], methods: %i(to_f to_tex to_gnu valued?)|
2
+ (methods - ignore).each do |method|
3
+ it {expect(subject).to respond_to(method) }
4
+ end
5
+ end
6
+
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: danica
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.3
4
+ version: 2.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Darthjee
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-09-10 00:00:00.000000000 Z
11
+ date: 2017-09-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -150,20 +150,6 @@ dependencies:
150
150
  - - ">="
151
151
  - !ruby/object:Gem::Version
152
152
  version: '0'
153
- - !ruby/object:Gem::Dependency
154
- name: codeclimate-test-reporter
155
- requirement: !ruby/object:Gem::Requirement
156
- requirements:
157
- - - ">="
158
- - !ruby/object:Gem::Version
159
- version: '0'
160
- type: :development
161
- prerelease: false
162
- version_requirements: !ruby/object:Gem::Requirement
163
- requirements:
164
- - - ">="
165
- - !ruby/object:Gem::Version
166
- version: '0'
167
153
  description:
168
154
  email:
169
155
  - darthjee@gmail.com
@@ -176,6 +162,7 @@ files:
176
162
  - Gemfile
177
163
  - README.md
178
164
  - Rakefile
165
+ - circle.yml
179
166
  - danica.gemspec
180
167
  - docker-compose.yml
181
168
  - lib/danica.rb
@@ -190,6 +177,8 @@ files:
190
177
  - lib/danica/exception/not_defined.rb
191
178
  - lib/danica/exponential.rb
192
179
  - lib/danica/function.rb
180
+ - lib/danica/group.rb
181
+ - lib/danica/negative.rb
193
182
  - lib/danica/number.rb
194
183
  - lib/danica/operator.rb
195
184
  - lib/danica/operator/chained.rb
@@ -205,6 +194,7 @@ files:
205
194
  - spec/lib/danica/division_spec.rb
206
195
  - spec/lib/danica/exponential_spec.rb
207
196
  - spec/lib/danica/function_spec.rb
197
+ - spec/lib/danica/negative_spec.rb
208
198
  - spec/lib/danica/number_spec.rb
209
199
  - spec/lib/danica/power_spec.rb
210
200
  - spec/lib/danica/product_spec.rb
@@ -213,8 +203,11 @@ files:
213
203
  - spec/lib/danica/sum_spec.rb
214
204
  - spec/lib/danica/variable_spec.rb
215
205
  - spec/spec_helper.rb
206
+ - spec/support/models/functions/gauss.rb
207
+ - spec/support/models/functions/spatial.rb
216
208
  - spec/support/shared_contexts/common.rb
217
209
  - spec/support/shared_examples/base_operations.rb
210
+ - spec/support/shared_examples/common.rb
218
211
  - spec/support/shared_examples/operator/chained.rb
219
212
  - spec/support/shared_examples/operator/dual_term.rb
220
213
  - spec/support/shared_examples/operator/single_input.rb
@@ -238,7 +231,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
238
231
  version: '0'
239
232
  requirements: []
240
233
  rubyforge_project:
241
- rubygems_version: 2.6.11
234
+ rubygems_version: 2.6.8
242
235
  signing_key:
243
236
  specification_version: 4
244
237
  summary: Danica
@@ -248,6 +241,7 @@ test_files:
248
241
  - spec/lib/danica/division_spec.rb
249
242
  - spec/lib/danica/exponential_spec.rb
250
243
  - spec/lib/danica/function_spec.rb
244
+ - spec/lib/danica/negative_spec.rb
251
245
  - spec/lib/danica/number_spec.rb
252
246
  - spec/lib/danica/power_spec.rb
253
247
  - spec/lib/danica/product_spec.rb
@@ -256,8 +250,11 @@ test_files:
256
250
  - spec/lib/danica/sum_spec.rb
257
251
  - spec/lib/danica/variable_spec.rb
258
252
  - spec/spec_helper.rb
253
+ - spec/support/models/functions/gauss.rb
254
+ - spec/support/models/functions/spatial.rb
259
255
  - spec/support/shared_contexts/common.rb
260
256
  - spec/support/shared_examples/base_operations.rb
257
+ - spec/support/shared_examples/common.rb
261
258
  - spec/support/shared_examples/operator/chained.rb
262
259
  - spec/support/shared_examples/operator/dual_term.rb
263
260
  - spec/support/shared_examples/operator/single_input.rb