danica 2.2.1 → 2.3.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.
Files changed (59) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +39 -15
  3. data/lib/danica.rb +3 -18
  4. data/lib/danica/base_operations.rb +6 -6
  5. data/lib/danica/common.rb +7 -5
  6. data/lib/danica/dsl.rb +21 -5
  7. data/lib/danica/exception.rb +1 -1
  8. data/lib/danica/operator.rb +11 -0
  9. data/lib/danica/{sum.rb → operator/addition.rb} +2 -4
  10. data/lib/danica/operator/cos.rb +5 -0
  11. data/lib/danica/{division.rb → operator/division.rb} +1 -1
  12. data/lib/danica/operator/exponential.rb +5 -0
  13. data/lib/danica/operator/functional.rb +29 -0
  14. data/lib/danica/{product.rb → operator/multiplication.rb} +1 -3
  15. data/lib/danica/{power.rb → operator/power.rb} +1 -1
  16. data/lib/danica/operator/sin.rb +5 -0
  17. data/lib/danica/operator/squared_root.rb +5 -0
  18. data/lib/danica/variables_holder/variables_builder.rb +0 -2
  19. data/lib/danica/version.rb +1 -1
  20. data/lib/danica/wrapper.rb +10 -0
  21. data/lib/danica/{constant.rb → wrapper/constant.rb} +4 -1
  22. data/lib/danica/{group.rb → wrapper/group.rb} +2 -1
  23. data/lib/danica/{negative.rb → wrapper/negative.rb} +1 -1
  24. data/lib/danica/{number.rb → wrapper/number.rb} +1 -1
  25. data/lib/danica/{positive_negative.rb → wrapper/plus_minus.rb} +1 -1
  26. data/lib/danica/{variable.rb → wrapper/variable.rb} +3 -3
  27. data/spec/integration/negative_spec.rb +3 -3
  28. data/spec/integration/positive_negative_spec.rb +3 -3
  29. data/spec/integration/power_spec.rb +4 -4
  30. data/spec/integration/product_spec.rb +8 -8
  31. data/spec/integration/sum_spec.rb +5 -5
  32. data/spec/lib/danica/dsl_spec.rb +14 -11
  33. data/spec/lib/danica/function_spec.rb +8 -8
  34. data/spec/lib/danica/{sum_spec.rb → operator/addition_spec.rb} +3 -3
  35. data/spec/lib/danica/{cos_spec.rb → operator/cos_spec.rb} +1 -1
  36. data/spec/lib/danica/{division_spec.rb → operator/division_spec.rb} +6 -6
  37. data/spec/lib/danica/{exponential_spec.rb → operator/exponential_spec.rb} +1 -1
  38. data/spec/lib/danica/{product_spec.rb → operator/multiplication_spec.rb} +3 -3
  39. data/spec/lib/danica/{power_spec.rb → operator/power_spec.rb} +1 -1
  40. data/spec/lib/danica/{sin_spec.rb → operator/sin_spec.rb} +1 -1
  41. data/spec/lib/danica/{squared_root_spec.rb → operator/squared_root_spec.rb} +1 -1
  42. data/spec/lib/danica/{constant_spec.rb → wrapper/constant_spec.rb} +1 -1
  43. data/spec/lib/danica/wrapper/group_spec.rb +53 -0
  44. data/spec/lib/danica/{negative_spec.rb → wrapper/negative_spec.rb} +2 -2
  45. data/spec/lib/danica/{number_spec.rb → wrapper/number_spec.rb} +1 -1
  46. data/spec/lib/danica/{positive_negative_spec.rb → wrapper/plus_minus_spec.rb} +2 -2
  47. data/spec/lib/danica/{variable_spec.rb → wrapper/variable_spec.rb} +1 -1
  48. data/spec/lib/danica_spec.rb +10 -10
  49. data/spec/support/models/functions/baskara.rb +2 -2
  50. data/spec/support/models/functions/gauss.rb +4 -4
  51. data/spec/support/models/functions/spatial.rb +3 -3
  52. data/spec/support/shared_examples/base_operations.rb +10 -10
  53. data/spec/support/shared_examples/operator/chained.rb +1 -1
  54. metadata +46 -43
  55. data/lib/danica/cos.rb +0 -20
  56. data/lib/danica/exception/not_defined.rb +0 -2
  57. data/lib/danica/exponential.rb +0 -20
  58. data/lib/danica/sin.rb +0 -19
  59. data/lib/danica/squared_root.rb +0 -19
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fe5a9965099461f931104adf9ad6e0372b7a88b4
4
- data.tar.gz: d2fe7fd8afe885d6e2064c3e9a80449052d04d4d
3
+ metadata.gz: ad28c3d7ea149780e400d116c83de57d427766fd
4
+ data.tar.gz: 03df4bd255f7bd7f3ea1574ff3d762ee79ebcbb5
5
5
  SHA512:
6
- metadata.gz: dd2f74aabff6aeb3cd57839ac622b26391f0b251bd167a9d693671e1b869968686890641c54a2689e46994f81d39204127b0805873c41912c3fa8c79ee80d539
7
- data.tar.gz: a11fa801c71ccf5abbc6086c63c09ba28bf37d9b5d86e2cb7d898c6fde6d07c477f91b1079d1894c6a10e8607dc40055f76fe032922580c61fe68b9d82152c7e
6
+ metadata.gz: 6d5df83e1861e86792509782fe1c79767ee032d93dfe5b50a353f38960e2897ff1740cfe0876bb958fc1e23a82eb7959df35f3237aa14bff19964755b9953a99
7
+ data.tar.gz: 239199aeb9ee44347f4721cb65b336926c9f38fef5678c384411c48bcbc57a41426d08aebebe896fbbb11abff5ac6764d8fed5927c58752a3693b5338a2ded6f
data/README.md CHANGED
@@ -17,6 +17,30 @@ bundle install danica
17
17
 
18
18
  Now you can use in your project
19
19
 
20
+ ###Quick Use
21
+ Use Danica build to build and use your formula using ```Danica.build```
22
+ ```ruby
23
+ formula = Danica.build do
24
+ (number(1) + 2) * power(3,4)
25
+ end
26
+
27
+ formula.to_tex
28
+ ```
29
+
30
+ create and use functions
31
+
32
+ ```ruby
33
+ func = Danica.build do
34
+ formula(:x, :y) do
35
+ (number(1) + x) * power(3, y)
36
+ end
37
+ end
38
+
39
+ func.to_gnu
40
+ ```
41
+
42
+ create gnu or tex output strings to be used on yopur template
43
+
20
44
  ### Operators
21
45
  Operators are much like function, but they do not have a name.
22
46
 
@@ -43,7 +67,7 @@ end
43
67
 
44
68
  #### Sample
45
69
  ```ruby
46
- class Danica::Inverse < Danica::Operator
70
+ class Danica::Operator::Inverse < Danica::Operator
47
71
 
48
72
  variables :value
49
73
 
@@ -60,7 +84,7 @@ class Danica::Inverse < Danica::Operator
60
84
  end
61
85
  end
62
86
 
63
- fx = Danica::Inverse.new(:x)
87
+ fx = Danica::Operator::Inverse.new(:x)
64
88
  ```
65
89
 
66
90
  ##### to_tex
@@ -89,7 +113,7 @@ fx.calculate(2)
89
113
  ```
90
114
  or
91
115
  ```ruby
92
- Danica::Inverse.new(2).to_f
116
+ Danica::Operator::Inverse.new(2).to_f
93
117
  ```
94
118
 
95
119
  both return
@@ -124,7 +148,7 @@ module Danica
124
148
  private
125
149
 
126
150
  def function_block
127
- @function_block ||= sum(parcels)
151
+ @function_block ||= addition(parcels)
128
152
  end
129
153
 
130
154
  def parcels
@@ -136,11 +160,11 @@ module Danica
136
160
  end
137
161
 
138
162
  def spatial_velocity
139
- product(initial_velocity, time)
163
+ multiplication(initial_velocity, time)
140
164
  end
141
165
 
142
166
  def spatial_acceleration
143
- division(product(acceleration, time_squared), 2)
167
+ division(multiplication(acceleration, time_squared), 2)
144
168
  end
145
169
 
146
170
  def time_squared
@@ -253,19 +277,19 @@ Danica.build do
253
277
  end
254
278
  ```
255
279
 
256
- will result into a ```Danica::Power``` object
280
+ will result into a ```Danica::Operator::Power``` object
257
281
 
258
282
  ```ruby
259
- Danica::Power.new(:x, -1)
283
+ Danica::Operator::Power.new(:x, -1)
260
284
  ```
261
285
 
262
286
  #### Operator registering on DSL
263
287
 
264
- Any operator created can be added to the DSL by running ```DSL.register```
288
+ Any operator created can be added to the DSL by running ```DSL.register_operator```
265
289
 
266
290
  ```ruby
267
291
  module Danica
268
- class Inverse < Danica::Operator
292
+ class Operator::Inverse < Danica::Operator
269
293
  include DSL
270
294
  variables :value
271
295
 
@@ -278,16 +302,16 @@ module Danica
278
302
  end
279
303
  ```
280
304
 
281
- In order to add the new operator, DSL cna infer by the name ```inverse``` which results in ```Danica::Inverse```
305
+ In order to add the new operator, DSL cna infer by the name ```inverse``` which results in ```Danica::Operator::Inverse```
282
306
 
283
307
  ```ruby
284
- Danica::DSL.register(:inverse)
308
+ Danica::DSL.register_operator(:inverse)
285
309
  ```
286
310
 
287
311
  or
288
312
 
289
313
  ```ruby
290
- Danica::DSL.register(:inverse, Danica::inverse)
314
+ Danica::DSL.register(:inverse, Danica::Operator::Inverse)
291
315
  ```
292
316
 
293
317
  This will allow the usage of the inverse function
@@ -298,8 +322,8 @@ Danica.build do
298
322
  end
299
323
  ```
300
324
 
301
- will result into a ```Danica::Inverse``` object
325
+ will result into a ```Danica::Operator::Inverse``` object
302
326
 
303
327
  ```ruby
304
- Danica::Inverse.new(:x)
328
+ Danica::Operator::Inverse.new(:x)
305
329
  ```
@@ -1,29 +1,17 @@
1
1
  require 'active_model'
2
+ require 'darthjee/core_ext'
2
3
 
3
4
  module Danica
4
5
  autoload :Builder, 'danica/builder'
5
6
  autoload :BaseOperations, 'danica/base_operations'
6
7
  autoload :VariablesHolder, 'danica/variables_holder'
7
8
  autoload :Common, 'danica/common'
8
- autoload :Number, 'danica/number'
9
- autoload :Negative, 'danica/negative'
10
- autoload :PositiveNegative, 'danica/positive_negative'
11
- autoload :Group, 'danica/group'
12
- autoload :Variable, 'danica/variable'
13
- autoload :Operator, 'danica/operator'
14
9
  autoload :Function, 'danica/function'
15
10
  autoload :Exception, 'danica/exception'
16
- autoload :Constant, 'danica/constant'
17
11
 
18
- autoload :Product, 'danica/product'
19
- autoload :Sum, 'danica/sum'
20
- autoload :Division, 'danica/division'
21
- autoload :Power, 'danica/power'
22
- autoload :SquaredRoot, 'danica/squared_root'
23
- autoload :Exponential, 'danica/exponential'
24
- autoload :Sin, 'danica/sin'
25
- autoload :Cos, 'danica/cos'
26
12
  autoload :DSL, 'danica/dsl'
13
+ autoload :Wrapper, 'danica/wrapper'
14
+ autoload :Operator, 'danica/operator'
27
15
 
28
16
  class << self
29
17
  delegate :build, to: :builder
@@ -32,8 +20,5 @@ module Danica
32
20
  @builder ||= Builder.new
33
21
  end
34
22
  end
35
-
36
- E = Constant.new(Math::E, :e, 'exp(1)')
37
- PI = Constant.new(Math::PI, '\pi', :pi)
38
23
  end
39
24
 
@@ -1,21 +1,21 @@
1
1
  module Danica
2
2
  module BaseOperations
3
3
  def +(other)
4
- return other + self if other.is_a?(Sum)
5
- Sum.new(self, other)
4
+ return other + self if other.is_a?(Operator::Addition)
5
+ addition(self, other)
6
6
  end
7
7
 
8
8
  def *(other)
9
- return other * self if other.is_a?(Product)
10
- Product.new(self, other)
9
+ return other * self if other.is_a?(Operator::Multiplication)
10
+ multiplication(self, other)
11
11
  end
12
12
 
13
13
  def /(other)
14
- Division.new(self, other)
14
+ division(self, other)
15
15
  end
16
16
 
17
17
  def -(other)
18
- self + Negative.new(other)
18
+ self + negative(other)
19
19
  end
20
20
  end
21
21
  end
@@ -1,6 +1,8 @@
1
1
  module Danica
2
2
  module Common extend ::ActiveSupport::Concern
3
3
  included do
4
+ include DSL
5
+
4
6
  class << self
5
7
  def default_value(name, value)
6
8
  define_method(name) { value }
@@ -21,16 +23,16 @@ module Danica
21
23
  private
22
24
 
23
25
  def wrap_value(value)
24
- return wrap_value(Number.new(value)) if value.is_a?(Numeric)
25
- return wrap_value(Variable.new(value)) if value.is_a?(Hash)
26
- return wrap_value(Variable.new(name: value)) if [ String, Symbol ].any? { |c| value.is_a?(c) }
27
- return wrap_value(Variable.new) if value.nil?
26
+ return wrap_value(number(value)) if value.is_a?(Numeric)
27
+ return wrap_value(variable(value)) if value.is_a?(Hash)
28
+ return wrap_value(variable(name: value)) if [ String, Symbol ].any? { |c| value.is_a?(c) }
29
+ return wrap_value(variable) if value.nil?
28
30
  value
29
31
  end
30
32
 
31
33
  def wrap_as_group(value)
32
34
  return value if is_grouped? || value.priority >= priority
33
- Group.new(value)
35
+ group(value)
34
36
  end
35
37
  end
36
38
  end
@@ -1,8 +1,17 @@
1
1
  module Danica
2
2
  module DSL
3
- def self.register(method, clazz=nil)
3
+ def self.register_operator(method, clazz=nil)
4
+ register(method, clazz, 'Danica::Operator')
5
+ end
6
+
7
+ def self.register_wrapper(method, clazz=nil)
8
+ register(method, clazz, 'Danica::Wrapper')
9
+ end
10
+
11
+ def self.register(method, clazz=nil, base=nil)
4
12
  define_method method do |*args|
5
- clazz = "Danica::#{method.to_s.camelize}".constantize unless clazz
13
+ clazz = [base.to_s, method.to_s.camelize].compact.join('::').constantize unless clazz
14
+ clazz = [base, clazz.to_s].compact.join('::').constantize unless clazz.is_a? Class
6
15
  clazz.new(*args)
7
16
  end
8
17
  end
@@ -13,9 +22,16 @@ module Danica
13
22
  end
14
23
 
15
24
  %i(
16
- sum product division sin cos power number
17
- squared_root exponential group negative
25
+ addition multiplication division sin cos power
26
+ squared_root exponential
18
27
  ).each do |method|
19
- DSL.register(method)
28
+ DSL.register_operator(method)
20
29
  end
30
+
31
+ %i(number group negative plus_minus variable).each do |method|
32
+ DSL.register_wrapper(method)
33
+ end
34
+
35
+ DSL.register_operator :sum, :Addition
36
+ DSL.register_operator :product, :Multiplication
21
37
  end
@@ -1,4 +1,4 @@
1
1
  class Danica::Exception < ::Exception
2
- require 'danica/exception/not_defined'
2
+ class Danica::Exception::NotDefined < self; end
3
3
  end
4
4
 
@@ -16,5 +16,16 @@ module Danica
16
16
  return false unless other.class == self.class
17
17
  variables == other.variables
18
18
  end
19
+
20
+ autoload :Functional, 'danica/operator/functional'
21
+ autoload :Chained, 'danica/operator/chained'
22
+ autoload :Multiplication, 'danica/operator/multiplication'
23
+ autoload :Addition, 'danica/operator/addition'
24
+ autoload :Division, 'danica/operator/division'
25
+ autoload :Power, 'danica/operator/power'
26
+ autoload :Cos, 'danica/operator/cos'
27
+ autoload :Sin, 'danica/operator/sin'
28
+ autoload :SquaredRoot, 'danica/operator/squared_root'
29
+ autoload :Exponential, 'danica/operator/exponential'
19
30
  end
20
31
  end
@@ -1,7 +1,5 @@
1
- require 'danica/operator/chained'
2
-
3
1
  module Danica
4
- class Sum < Operator::Chained
2
+ class Operator::Addition < Operator::Chained
5
3
  default_value :priority, 1
6
4
 
7
5
  def +(other)
@@ -22,7 +20,7 @@ module Danica
22
20
 
23
21
  def join_proc(symbol)
24
22
  proc do |_, value|
25
- value.is_a?(Negative) || value.is_a?(PositiveNegative) ? ' ' : " #{symbol} "
23
+ value.is_a?(Wrapper::Negative) || value.is_a?(Wrapper::PlusMinus) ? ' ' : " #{symbol} "
26
24
  end
27
25
  end
28
26
  end
@@ -0,0 +1,5 @@
1
+ module Danica
2
+ class Operator::Cos < Operator::Functional.build(operator: :cos)
3
+ end
4
+ end
5
+
@@ -1,5 +1,5 @@
1
1
  module Danica
2
- class Division < Operator
2
+ class Operator::Division < Operator
3
3
  variables :numerator, :denominator
4
4
  default_value :priority, 2
5
5
  default_value :is_grouped?, true
@@ -0,0 +1,5 @@
1
+ module Danica
2
+ class Operator::Exponential < Operator::Functional.build(operator: :exp, tex: 'e^{:value:}')
3
+ end
4
+ end
5
+
@@ -0,0 +1,29 @@
1
+ module Danica
2
+ class Operator::Functional < Operator
3
+ variables :value
4
+ default_value :is_grouped?, true
5
+
6
+ def self.build(operator:, text: nil, tex: nil, gnu: nil)
7
+ text = "#{operator}(:value:)" if text.nil?
8
+ tex = text if tex.nil?
9
+ gnu = text if gnu.nil?
10
+
11
+ Class.new(self) do
12
+ module_eval(%Q(
13
+ def to_f
14
+ Math.#{operator}(value.to_f)
15
+ end
16
+
17
+ def to_tex
18
+ '#{tex.gsub(':value:', "' + value.to_tex + '")}'
19
+ end
20
+
21
+ def to_gnu
22
+ '#{gnu.gsub(':value:', "' + value.to_gnu + '")}'
23
+ end
24
+ ))
25
+ end
26
+ end
27
+ end
28
+ end
29
+
@@ -1,7 +1,5 @@
1
- require 'danica/operator/chained'
2
-
3
1
  module Danica
4
- class Product < Operator::Chained
2
+ class Operator::Multiplication < Operator::Chained
5
3
  default_value :priority, 2
6
4
 
7
5
  def *(other)
@@ -1,5 +1,5 @@
1
1
  module Danica
2
- class Power < Operator
2
+ class Operator::Power < Operator
3
3
  variables :base, :exponent
4
4
  default_value :is_grouped?, false
5
5
 
@@ -0,0 +1,5 @@
1
+ module Danica
2
+ class Operator::Sin < Operator::Functional.build(operator: :sin)
3
+ end
4
+ end
5
+
@@ -0,0 +1,5 @@
1
+ module Danica
2
+ class Operator::SquaredRoot < Operator::Functional.build(operator: :sqrt, tex: '\sqrt{:value:}')
3
+ end
4
+ end
5
+
@@ -1,5 +1,3 @@
1
- require 'darthjee/core_ext'
2
-
3
1
  module Danica::VariablesHolder
4
2
  class VariablesBuilder
5
3
  attr_reader :instance, :attr_names
@@ -1,3 +1,3 @@
1
1
  module Danica
2
- VERSION = '2.2.1'
2
+ VERSION = '2.3.0'
3
3
  end
@@ -0,0 +1,10 @@
1
+ module Danica
2
+ module Wrapper
3
+ autoload :Number, 'danica/wrapper/number'
4
+ autoload :Group, 'danica/wrapper/group'
5
+ autoload :Negative, 'danica/wrapper/negative'
6
+ autoload :PlusMinus, 'danica/wrapper/plus_minus'
7
+ autoload :Constant, 'danica/wrapper/constant'
8
+ autoload :Variable, 'danica/wrapper/variable'
9
+ end
10
+ end
@@ -1,5 +1,5 @@
1
1
  module Danica
2
- class Constant
2
+ class Wrapper::Constant
3
3
  include BaseOperations
4
4
  include Common
5
5
 
@@ -32,5 +32,8 @@ module Danica
32
32
  gnu.to_s
33
33
  end
34
34
  end
35
+
36
+ E = Wrapper::Constant.new(Math::E, :e, 'exp(1)')
37
+ PI = Wrapper::Constant.new(Math::PI, '\pi', :pi)
35
38
  end
36
39