danica 2.0.6 → 2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 819638dac151e3f8b6ebff32f9cbe3d0e5478d79
4
- data.tar.gz: 4bc0a877096317ab2e3a85eb481526057c3e5c9c
3
+ metadata.gz: 429321643b0113576822386a859ccac11543b36f
4
+ data.tar.gz: 2f34a7c65a4f47429b09a088cf5194a54fe139c1
5
5
  SHA512:
6
- metadata.gz: f9342e9487fa8c29b7b507f669798acde6a8e5b5884abb096e8286317dc4ae2edf05b7d5113caeec7ac6fc422d04041d994665d86656033efc122c7fa752c9b8
7
- data.tar.gz: a5c184b88248538b8b538cfa875f172a6efc41b1145175ddbb696fb013da2d63f1c5952ce8aa3cf7fcd4af3b57b3455408a13966868f5bf8dbeb0742bf8ba88b
6
+ metadata.gz: 25f35ba1b18ef3ba0df7fcc2d9341524e4b79fbd40e55695bca92e9484ec2bd47eced63740ee3673b1d5364f0feeeb0af8065809de22cad5d1ee4c4ae5fc688a
7
+ data.tar.gz: 24c2034b52eaaeb1905b9b5b8ab9d938d014eece4d51130404d24bf883a286bfd85f304a2780a4ddf73109e4b07b55c62b62087da50f11a9b19431853ad81de2
data/danica.gemspec CHANGED
@@ -17,7 +17,6 @@ Gem::Specification.new do |spec|
17
17
 
18
18
  spec.add_runtime_dependency 'activesupport', '~> 5.1.4'
19
19
  spec.add_runtime_dependency 'activemodel', '~> 5.1.4'
20
- spec.add_runtime_dependency 'concern_builder', '~> 0.0.3'
21
20
  spec.add_runtime_dependency 'darthjee-core_ext', '~> 1.5.0'
22
21
 
23
22
  spec.add_development_dependency 'bundler', '~> 1.6'
data/lib/danica.rb CHANGED
@@ -1,26 +1,27 @@
1
1
  require 'active_model'
2
2
 
3
3
  module Danica
4
- autoload :BaseOperations, 'danica/base_operations'
5
- autoload :VariablesHolder, 'danica/variables_holder'
6
- autoload :Common, 'danica/common'
7
- autoload :Number, 'danica/number'
8
- autoload :Negative, 'danica/negative'
9
- autoload :Group, 'danica/group'
10
- autoload :Variable, 'danica/variable'
11
- autoload :Operator, 'danica/operator'
12
- autoload :Function, 'danica/function'
13
- autoload :Exception, 'danica/exception'
14
- autoload :Constant, 'danica/constant'
4
+ autoload :BaseOperations, 'danica/base_operations'
5
+ autoload :VariablesHolder, 'danica/variables_holder'
6
+ autoload :Common, 'danica/common'
7
+ autoload :Number, 'danica/number'
8
+ autoload :Negative, 'danica/negative'
9
+ autoload :PositiveNegative, 'danica/positive_negative'
10
+ autoload :Group, 'danica/group'
11
+ autoload :Variable, 'danica/variable'
12
+ autoload :Operator, 'danica/operator'
13
+ autoload :Function, 'danica/function'
14
+ autoload :Exception, 'danica/exception'
15
+ autoload :Constant, 'danica/constant'
15
16
 
16
- autoload :Product, 'danica/product'
17
- autoload :Sum, 'danica/sum'
18
- autoload :Division, 'danica/division'
19
- autoload :Power, 'danica/power'
20
- autoload :SquaredRoot, 'danica/squared_root'
21
- autoload :Exponential, 'danica/exponential'
22
- autoload :Sin, 'danica/sin'
23
- autoload :Cos, 'danica/cos'
17
+ autoload :Product, 'danica/product'
18
+ autoload :Sum, 'danica/sum'
19
+ autoload :Division, 'danica/division'
20
+ autoload :Power, 'danica/power'
21
+ autoload :SquaredRoot, 'danica/squared_root'
22
+ autoload :Exponential, 'danica/exponential'
23
+ autoload :Sin, 'danica/sin'
24
+ autoload :Cos, 'danica/cos'
24
25
 
25
26
  E = Constant.new(Math::E, :e, 'exp(1)')
26
27
  PI = Constant.new(Math::PI, '\pi', :pi)
@@ -13,7 +13,7 @@ module Danica
13
13
  end
14
14
 
15
15
  def to_gnu
16
- "#{numerator.to_gnu}/#{denominator.to_gnu}"
16
+ "(#{numerator.to_gnu})/(#{denominator.to_gnu})"
17
17
  end
18
18
  end
19
19
  end
@@ -1,12 +1,13 @@
1
1
  module Danica
2
2
  class Negative
3
3
  include Common
4
+ include BaseOperations
4
5
  include VariablesHolder
5
6
  include ActiveModel::Model
6
7
 
7
8
  attr_accessor :value
8
9
 
9
- default_value :priority, 1
10
+ default_value :priority, 2
10
11
  default_value :is_grouped?, false
11
12
 
12
13
  delegate :valued?, to: :value
@@ -20,11 +21,11 @@ module Danica
20
21
  end
21
22
 
22
23
  def to_tex
23
- "-#{value.to_tex}"
24
+ "-#{wrap_as_group(value).to_tex}"
24
25
  end
25
26
 
26
27
  def to_gnu
27
- "-#{value.to_gnu}"
28
+ "-#{wrap_as_group(value).to_gnu}"
28
29
  end
29
30
 
30
31
  def ==(other)
@@ -0,0 +1,37 @@
1
+ module Danica
2
+ class PositiveNegative
3
+ include Common
4
+ include BaseOperations
5
+ include VariablesHolder
6
+ include ActiveModel::Model
7
+
8
+ attr_accessor :value
9
+
10
+ default_value :priority, 2
11
+ default_value :is_grouped?, false
12
+
13
+ delegate :valued?, to: :value
14
+
15
+ def initialize(value)
16
+ @value = wrap_value(value)
17
+ end
18
+
19
+ def to_f
20
+ value.to_f
21
+ end
22
+
23
+ def to_tex
24
+ "\\pm #{wrap_as_group(value).to_tex}"
25
+ end
26
+
27
+ def to_gnu
28
+ "+ #{wrap_as_group(value).to_gnu}"
29
+ end
30
+
31
+ def ==(other)
32
+ return false unless other.class == self.class
33
+ value == other.value
34
+ end
35
+ end
36
+ end
37
+
data/lib/danica/sum.rb CHANGED
@@ -22,7 +22,7 @@ module Danica
22
22
 
23
23
  def join_proc(symbol)
24
24
  proc do |_, value|
25
- value.is_a?(Negative) ? ' ' : " #{symbol} "
25
+ value.is_a?(Negative) || value.is_a?(PositiveNegative) ? ' ' : " #{symbol} "
26
26
  end
27
27
  end
28
28
  end
@@ -1,33 +1,41 @@
1
- require 'concern_builder'
2
1
  require 'darthjee/core_ext'
3
2
 
4
3
  module Danica::VariablesHolder
5
- class VariablesBuilder < ::ConcernBuilder
6
- attr_reader :instance
4
+ class VariablesBuilder
5
+ attr_reader :instance, :attr_names
6
+
7
+ def initialize(attr_names, instance)
8
+ @instance = instance
9
+ @attr_names = attr_names
10
+ end
11
+
12
+ def build
13
+ attr_names.extract_options!.each do |name, default|
14
+ add_setter(name)
15
+ add_reader(name, default)
16
+ instance.variables_names << name
17
+ end
7
18
 
8
- def init
9
19
  attr_names.each do |name|
10
20
  add_setter(name)
11
- add_reader(name)
12
- instance.send(:variables_names) << name
21
+ add_reader(name, name)
22
+ instance.variables_names << name
13
23
  end
14
24
  end
15
25
 
16
26
  private
17
27
 
18
28
  def add_setter(name)
19
- code = <<-CODE
20
- variables_hash[:#{name}] = wrap_value(value)
29
+ instance.send(:define_method, "#{name}=") do |value|
30
+ variables_hash[name.to_sym] = wrap_value(value)
21
31
  @variables = variables_hash.values
22
- CODE
23
- add_method("#{name}=(value)", code)
32
+ end
24
33
  end
25
34
 
26
- def add_reader(name)
27
- code = <<-CODE
28
- variables_hash[:#{name}]
29
- CODE
30
- add_method("#{name}", code)
35
+ def add_reader(name, default)
36
+ instance.send(:define_method, name) do
37
+ variables_hash[name.to_sym] ||= wrap_value(default)
38
+ end
31
39
  end
32
40
  end
33
41
  end
@@ -1,3 +1,3 @@
1
1
  module Danica
2
- VERSION = '2.0.6'
2
+ VERSION = '2.1.0'
3
3
  end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'integration of negative' do
4
+ describe 'with a sum' do
5
+ subject do
6
+ Danica::Negative.new(
7
+ Danica::Sum.new(1,2,3)
8
+ )
9
+ end
10
+
11
+ describe '#to_gnu' do
12
+ it 'returns the correct string' do
13
+ expect(subject.to_gnu).to eq('-(1 + 2 + 3)')
14
+ end
15
+ end
16
+
17
+ describe '#to_tex' do
18
+ it 'returns the correct string' do
19
+ expect(subject.to_tex).to eq('-\left(1 + 2 + 3\right)')
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'integration of positive negative' do
4
+ describe 'with a sum' do
5
+ subject do
6
+ Danica::PositiveNegative.new(
7
+ Danica::Sum.new(1,2,3)
8
+ )
9
+ end
10
+
11
+ describe '#to_gnu' do
12
+ it 'returns the correct string' do
13
+ expect(subject.to_gnu).to eq('+ (1 + 2 + 3)')
14
+ end
15
+ end
16
+
17
+ describe '#to_tex' do
18
+ it 'returns the correct string' do
19
+ expect(subject.to_tex).to eq('\pm \left(1 + 2 + 3\right)')
20
+ end
21
+ end
22
+ end
23
+ end
24
+
@@ -1,20 +1,26 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe 'integration of sum' do
4
- describe 'with negative numbers' do
4
+ describe 'with negative and positivenegative (+/-) numbers' do
5
5
  subject do
6
- Danica::Sum.new(Danica::Negative.new(1), 2, Danica::Negative.new(3), 4)
6
+ Danica::Sum.new(
7
+ Danica::Negative.new(1),
8
+ 2,
9
+ Danica::Negative.new(3),
10
+ 4,
11
+ Danica::PositiveNegative.new(5)
12
+ )
7
13
  end
8
14
 
9
15
  describe '#to_gnu' do
10
16
  it 'returns the correct string' do
11
- expect(subject.to_gnu).to eq('-1 + 2 -3 + 4')
17
+ expect(subject.to_gnu).to eq('-1 + 2 -3 + 4 + 5')
12
18
  end
13
19
  end
14
20
 
15
21
  describe '#to_tex' do
16
22
  it 'returns the correct string' do
17
- expect(subject.to_tex).to eq('-1 + 2 -3 + 4')
23
+ expect(subject.to_tex).to eq('-1 + 2 -3 + 4 \pm 5')
18
24
  end
19
25
  end
20
26
  end
@@ -16,10 +16,31 @@ describe Danica::Division do
16
16
  partial_string_expected: '\frac{2}{X2}'
17
17
  },
18
18
  to_gnu: {
19
- string_expected: 'X1/X2',
20
- numeric_string_expected: '2/4',
21
- partial_string_expected: '2/X2'
19
+ string_expected: '(X1)/(X2)',
20
+ numeric_string_expected: '(2)/(4)',
21
+ partial_string_expected: '(2)/(X2)'
22
22
  }
23
23
  }
24
+
25
+ describe 'more complex division' do
26
+ describe 'of two sums' do
27
+ subject do
28
+ Danica::Division.new(
29
+ Danica::Sum.new(2, :x),
30
+ Danica::Sum.new(3, :y)
31
+ )
32
+ end
33
+
34
+ describe 'to_gnu' do
35
+ let(:expected) do
36
+ '(2 + x)/(3 + y)'
37
+ end
38
+
39
+ it 'groups sum' do
40
+ expect(subject.to_gnu).to eq(expected)
41
+ end
42
+ end
43
+ end
44
+ end
24
45
  end
25
46
 
@@ -26,7 +26,7 @@ describe Danica::Function do
26
26
 
27
27
  describe '#to_gnu' do
28
28
  context 'when creating the spatial operator for constantly accelerated movement' do
29
- let(:expected) { 'S0 + V0 * t + a * t**(2)/2' }
29
+ let(:expected) { 'S0 + V0 * t + (a * t**(2))/(2)' }
30
30
 
31
31
  it 'return the latex format CAM' do
32
32
  expect(subject.to_gnu).to eq(expected)
@@ -222,7 +222,7 @@ describe Danica::Function do
222
222
  {
223
223
  x: :x,
224
224
  median: :u,
225
- variance_root: :v
225
+ variance_root: { latex: '\theta', gnu: :v }
226
226
  }
227
227
  end
228
228
 
@@ -231,7 +231,7 @@ describe Danica::Function do
231
231
 
232
232
  describe '#to_tex' do
233
233
  context 'when creating the spatial operator for constantly accelerated movement' do
234
- let(:expected) { '\frac{1}{\sqrt{2 \cdot \pi \cdot v^{2}}} \cdot e^{-\frac{\left(x -u\right)^{2}}{2 \cdot v^{2}}}' }
234
+ let(:expected) { '\frac{1}{\sqrt{2 \cdot \pi \cdot \theta^{2}}} \cdot e^{-\frac{\left(x -u\right)^{2}}{2 \cdot \theta^{2}}}' }
235
235
 
236
236
  it 'return the latex format CAM' do
237
237
  expect(subject.to_tex).to eq(expected)
@@ -241,9 +241,52 @@ describe Danica::Function do
241
241
 
242
242
  describe '#to_gnu' do
243
243
  context 'when creating the spatial operator for constantly accelerated movement' do
244
- let(:expected) { '1/sqrt(2 * pi * v**(2)) * exp(-(x -u)**(2)/2 * v**(2))' }
244
+ let(:expected) { '(1)/(sqrt(2 * pi * v**(2))) * exp(-((x -u)**(2))/(2 * v**(2)))' }
245
+
246
+ it 'return the gnu format CAM' do
247
+ expect(subject.to_gnu).to eq(expected)
248
+ end
249
+ end
250
+ end
251
+
252
+ context 'when not passing variables' do
253
+ subject { described_class::Gauss.new }
254
+
255
+ describe '#to_tex' do
256
+ let(:expected) { '\frac{1}{\sqrt{2 \cdot \pi \cdot \theta^{2}}} \cdot e^{-\frac{\left(x -u\right)^{2}}{2 \cdot \theta^{2}}}' }
257
+
258
+ it 'rely on default variables definition' do
259
+ expect(subject.to_tex).to eq(expected)
260
+ end
261
+ end
262
+
263
+ describe '#to_gnu' do
264
+ let(:expected) { '(1)/(sqrt(2 * pi * v**(2))) * exp(-((x -u)**(2))/(2 * v**(2)))' }
265
+
266
+ it 'rely on default variables definition' do
267
+ expect(subject.to_gnu).to eq(expected)
268
+ end
269
+ end
270
+ end
271
+ end
272
+
273
+ describe 'baskara' do
274
+ context 'when using the default value for variables' do
275
+ subject { described_class::Baskara.new }
276
+ it_behaves_like 'an object that respond to basic_methods'
277
+
278
+ describe '#to_tex' do
279
+ let(:expected) { '\frac{-b \pm \sqrt{b^{2} -4 \cdot a \cdot c}}{2 \cdot a}' }
245
280
 
246
281
  it 'return the latex format CAM' do
282
+ expect(subject.to_tex).to eq(expected)
283
+ end
284
+ end
285
+
286
+ describe '#to_gnu' do
287
+ let(:expected) { '(-b + sqrt(b**(2) -4 * a * c))/(2 * a)' }
288
+
289
+ it 'return the gnu format CAM' do
247
290
  expect(subject.to_gnu).to eq(expected)
248
291
  end
249
292
  end
@@ -5,6 +5,7 @@ describe Danica::Negative do
5
5
  subject { described_class.new(value) }
6
6
 
7
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 'returns the float of value' do
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+
3
+ describe Danica::PositiveNegative 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
+ it_behaves_like 'an object with basic operation'
9
+
10
+ describe '#to_f' do
11
+ it 'returns the float of value' do
12
+ expect(subject.to_f).to eq(10)
13
+ end
14
+
15
+ it { expect(subject.to_f).to be_a(Float) }
16
+ end
17
+
18
+ describe '#to_tex' do
19
+ context 'when value should be integer' do
20
+ let(:value) { 10.0 }
21
+
22
+ it 'returns the positive negative string' do
23
+ expect(subject.to_tex).to eq('\pm 10')
24
+ end
25
+ end
26
+
27
+ context 'when value should be float' do
28
+ let(:value) { 10.5 }
29
+
30
+ it 'returns the positive negative string' do
31
+ expect(subject.to_tex).to eq('\pm 10.5')
32
+ end
33
+ end
34
+ end
35
+
36
+ describe '#to_gnu' do
37
+ context 'when value should be integer' do
38
+ let(:value) { 10.0 }
39
+
40
+ it 'returns the value integer string' do
41
+ expect(subject.to_gnu).to eq('+ 10')
42
+ end
43
+ end
44
+
45
+ context 'when value should be a float' do
46
+ let(:value) { 10.5 }
47
+
48
+ it 'returns the value float string' do
49
+ expect(subject.to_gnu).to eq('+ 10.5')
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,25 @@
1
+ module Danica
2
+ class Function::Baskara < Function
3
+ variables :a, :b, :c
4
+ delegate :to_f, :to_tex, :to_gnu, to: :division
5
+
6
+ private
7
+
8
+ def division
9
+ numerator / denominator
10
+ end
11
+
12
+ def numerator
13
+ Negative.new(b) + PositiveNegative.new(SquaredRoot.new(delta))
14
+ end
15
+
16
+ def denominator
17
+ Number.new(2) * a
18
+ end
19
+
20
+ def delta
21
+ Power.new(b, 2) - Product.new(4, a, c)
22
+ end
23
+ end
24
+ end
25
+
@@ -1,6 +1,6 @@
1
1
  module Danica
2
2
  class Function::Gauss < Function
3
- variables :x, :median, :variance_root
3
+ variables :x, median: :u, variance_root: { latex: '\theta', gnu: :v }
4
4
  delegate :to_f, :to_tex, :to_gnu, to: :product
5
5
 
6
6
  private
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.6
4
+ version: 2.1.0
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-26 00:00:00.000000000 Z
11
+ date: 2017-10-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -38,20 +38,6 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: 5.1.4
41
- - !ruby/object:Gem::Dependency
42
- name: concern_builder
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - "~>"
46
- - !ruby/object:Gem::Version
47
- version: 0.0.3
48
- type: :runtime
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - "~>"
53
- - !ruby/object:Gem::Version
54
- version: 0.0.3
55
41
  - !ruby/object:Gem::Dependency
56
42
  name: darthjee-core_ext
57
43
  requirement: !ruby/object:Gem::Requirement
@@ -180,6 +166,7 @@ files:
180
166
  - lib/danica/number.rb
181
167
  - lib/danica/operator.rb
182
168
  - lib/danica/operator/chained.rb
169
+ - lib/danica/positive_negative.rb
183
170
  - lib/danica/power.rb
184
171
  - lib/danica/product.rb
185
172
  - lib/danica/sin.rb
@@ -189,6 +176,8 @@ files:
189
176
  - lib/danica/variables_holder.rb
190
177
  - lib/danica/variables_holder/variables_builder.rb
191
178
  - lib/danica/version.rb
179
+ - spec/integration/negative_spec.rb
180
+ - spec/integration/positive_negative_spec.rb
192
181
  - spec/integration/power_spec.rb
193
182
  - spec/integration/product_spec.rb
194
183
  - spec/integration/sum_spec.rb
@@ -199,6 +188,7 @@ files:
199
188
  - spec/lib/danica/function_spec.rb
200
189
  - spec/lib/danica/negative_spec.rb
201
190
  - spec/lib/danica/number_spec.rb
191
+ - spec/lib/danica/positive_negative_spec.rb
202
192
  - spec/lib/danica/power_spec.rb
203
193
  - spec/lib/danica/product_spec.rb
204
194
  - spec/lib/danica/sin_spec.rb
@@ -206,6 +196,7 @@ files:
206
196
  - spec/lib/danica/sum_spec.rb
207
197
  - spec/lib/danica/variable_spec.rb
208
198
  - spec/spec_helper.rb
199
+ - spec/support/models/functions/baskara.rb
209
200
  - spec/support/models/functions/gauss.rb
210
201
  - spec/support/models/functions/spatial.rb
211
202
  - spec/support/shared_contexts/common.rb
@@ -239,6 +230,8 @@ signing_key:
239
230
  specification_version: 4
240
231
  summary: Danica
241
232
  test_files:
233
+ - spec/integration/negative_spec.rb
234
+ - spec/integration/positive_negative_spec.rb
242
235
  - spec/integration/power_spec.rb
243
236
  - spec/integration/product_spec.rb
244
237
  - spec/integration/sum_spec.rb
@@ -249,6 +242,7 @@ test_files:
249
242
  - spec/lib/danica/function_spec.rb
250
243
  - spec/lib/danica/negative_spec.rb
251
244
  - spec/lib/danica/number_spec.rb
245
+ - spec/lib/danica/positive_negative_spec.rb
252
246
  - spec/lib/danica/power_spec.rb
253
247
  - spec/lib/danica/product_spec.rb
254
248
  - spec/lib/danica/sin_spec.rb
@@ -256,6 +250,7 @@ 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/baskara.rb
259
254
  - spec/support/models/functions/gauss.rb
260
255
  - spec/support/models/functions/spatial.rb
261
256
  - spec/support/shared_contexts/common.rb