danica 2.2.0 → 2.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1a4ece25f84b8d2b7cadc2b68fbd1a6ee4e7d82d
4
- data.tar.gz: 00476bd0e01921256126c28be5b08781ed223137
3
+ metadata.gz: fe5a9965099461f931104adf9ad6e0372b7a88b4
4
+ data.tar.gz: d2fe7fd8afe885d6e2064c3e9a80449052d04d4d
5
5
  SHA512:
6
- metadata.gz: 430fa8144c3d9ef83f41c1a9c0a5d63699d5d8cac5c89c2ed79d97835ebec39a1beb4da6f1cbd68a9540846e93174ec1acfd82ca2c4c50bf90cace854beac782
7
- data.tar.gz: '09317ebf09542e77ca54afb320e495143b796689052e5541d532de58b1e12a00fe6879fe663c0481dce008e0c554fe643d14b2bf419734719d9e6bb1be9a5e42'
6
+ metadata.gz: dd2f74aabff6aeb3cd57839ac622b26391f0b251bd167a9d693671e1b869968686890641c54a2689e46994f81d39204127b0805873c41912c3fa8c79ee80d539
7
+ data.tar.gz: a11fa801c71ccf5abbc6086c63c09ba28bf37d9b5d86e2cb7d898c6fde6d07c477f91b1079d1894c6a10e8607dc40055f76fe032922580c61fe68b9d82152c7e
data/README.md CHANGED
@@ -193,11 +193,9 @@ fx.calculate(10, 3)
193
193
 
194
194
  or
195
195
 
196
-
197
196
  ```ruby
198
197
  fx.calculate(time: 10, acceleration: 3)
199
198
  ```
200
-
201
199
  or
202
200
 
203
201
  ```ruby
@@ -209,3 +207,99 @@ all return
209
207
  ```ruby
210
208
  171.0
211
209
  ```
210
+
211
+ #### Danica::Function.build
212
+ Alternativily, a function can be created through ```Danica::Function.build(*variables, &block)```
213
+
214
+ ```ruby
215
+ class Saddle < Danica::Function.build(:x, :y) { power(x, 2) - power(y, 2) }
216
+ end
217
+
218
+ fx = Saddle.new
219
+ ```
220
+
221
+ or
222
+
223
+ ```ruby
224
+ fx = Danica::Function.build(:x, :y) { power(x, 2) - power(y, 2) }.new
225
+ ```
226
+
227
+ ##### to_tex
228
+ ```ruby
229
+ fx.to_tex
230
+ ```
231
+
232
+ returns
233
+ ```string
234
+ x^{2} -y^{2}
235
+ ```
236
+
237
+ ##### to_gnu
238
+ ```ruby
239
+ fx.to_gnu
240
+ ```
241
+
242
+ returns
243
+ ```string
244
+ x**(2) -y**(2)
245
+ ```
246
+
247
+ ### DSL and building
248
+ A function can be created using the DSL direct from ```Danica```
249
+
250
+ ```ruby
251
+ Danica.build do
252
+ power(:x, -1)
253
+ end
254
+ ```
255
+
256
+ will result into a ```Danica::Power``` object
257
+
258
+ ```ruby
259
+ Danica::Power.new(:x, -1)
260
+ ```
261
+
262
+ #### Operator registering on DSL
263
+
264
+ Any operator created can be added to the DSL by running ```DSL.register```
265
+
266
+ ```ruby
267
+ module Danica
268
+ class Inverse < Danica::Operator
269
+ include DSL
270
+ variables :value
271
+
272
+ delegate :to_f, :to_tex, :to_gnu, to: :pow
273
+
274
+ def pow
275
+ @pow ||= power(value, -1)
276
+ end
277
+ end
278
+ end
279
+ ```
280
+
281
+ In order to add the new operator, DSL cna infer by the name ```inverse``` which results in ```Danica::Inverse```
282
+
283
+ ```ruby
284
+ Danica::DSL.register(:inverse)
285
+ ```
286
+
287
+ or
288
+
289
+ ```ruby
290
+ Danica::DSL.register(:inverse, Danica::inverse)
291
+ ```
292
+
293
+ This will allow the usage of the inverse function
294
+
295
+ ```ruby
296
+ Danica.build do
297
+ inverse(:x)
298
+ end
299
+ ```
300
+
301
+ will result into a ```Danica::Inverse``` object
302
+
303
+ ```ruby
304
+ Danica::Inverse.new(:x)
305
+ ```
@@ -9,7 +9,9 @@ module Danica
9
9
  end
10
10
 
11
11
  def variables_names
12
- @variables_names ||= []
12
+ @variables_names ||= (
13
+ (superclass.try(:variables_names) || []) + []
14
+ )
13
15
  end
14
16
  end
15
17
  end
@@ -1,3 +1,3 @@
1
1
  module Danica
2
- VERSION = '2.2.0'
2
+ VERSION = '2.2.1'
3
3
  end
@@ -43,6 +43,54 @@ describe Danica::Function do
43
43
  expect(function_class.superclass).to eq(described_class)
44
44
  end
45
45
  end
46
+
47
+ context 'when creating a class using build' do
48
+ let(:function_class) { Danica::Hyperbole }
49
+
50
+ it 'has the defined variables on class definition' do
51
+ expect(function_class.variables_names).to eq([:x])
52
+ end
53
+
54
+ it 'has the defined variables' do
55
+ expect(function.variables_hash).to eq(x: nil)
56
+ end
57
+
58
+ context 'when calling to_tex' do
59
+ it 'build function from block' do
60
+ expect(function.to_tex).to eq('x^{2}')
61
+ end
62
+ end
63
+
64
+ context 'when calling to_gnu' do
65
+ it 'build function from block' do
66
+ expect(function.to_gnu).to eq('x**(2)')
67
+ end
68
+ end
69
+ end
70
+
71
+ context 'when using a class that inherits from another class' do
72
+ let(:function_class) { Danica::SaddleHyperbole }
73
+
74
+ it 'has the defined variables on class definition' do
75
+ expect(function_class.variables_names).to eq([:x, :y])
76
+ end
77
+
78
+ it 'has the defined variables' do
79
+ expect(function.variables_hash).to eq(x: nil, y: nil)
80
+ end
81
+
82
+ context 'when calling to_tex' do
83
+ it 'build function from block' do
84
+ expect(function.to_tex).to eq('x^{2} -y^{2}')
85
+ end
86
+ end
87
+
88
+ context 'when calling to_gnu' do
89
+ it 'build function from block' do
90
+ expect(function.to_gnu).to eq('x**(2) -y**(2)')
91
+ end
92
+ end
93
+ end
46
94
  end
47
95
 
48
96
  describe 'spatial' do
@@ -1,13 +1,8 @@
1
1
  module Danica
2
- class Function::Baskara < Function
3
- variables :a, :b, :c
2
+ class Function::Baskara < Function.build(:a, :b, :c) { numerator / denominator }
4
3
 
5
4
  private
6
5
 
7
- def function_block
8
- @function_block ||= numerator / denominator
9
- end
10
-
11
6
  def numerator
12
7
  negative(b) + PositiveNegative.new(squared_root(delta))
13
8
  end
@@ -1,13 +1,8 @@
1
1
  module Danica
2
- class Function::Gauss < Function
3
- variables :x, median: :u, variance_root: { latex: '\theta', gnu: :v }
2
+ class Function::Gauss < Function.build(:x, median: :u, variance_root: { latex: '\theta', gnu: :v }) { product(parcels) }
4
3
 
5
4
  private
6
5
 
7
- def function_block
8
- @function_block ||= product(parcels)
9
- end
10
-
11
6
  def parcels
12
7
  [
13
8
  division(1, denominator),
@@ -0,0 +1,14 @@
1
+ module Danica
2
+ class Hyperbole < Function.build(:x) { power(x, 2) }
3
+ end
4
+ end
5
+
6
+ module Danica
7
+ class SaddleHyperbole < Hyperbole
8
+ variables :y
9
+
10
+ def function_block
11
+ super - power(y, 2)
12
+ end
13
+ end
14
+ end
@@ -1,13 +1,8 @@
1
1
  module Danica
2
- class Function::Spatial < Function
3
- variables :time, :acceleration, :initial_space, :initial_velocity
2
+ class Function::Spatial < Function.build(:time, :acceleration, :initial_space, :initial_velocity) { sum(parcels) }
4
3
 
5
4
  private
6
5
 
7
- def function_block
8
- @function_block ||= sum(parcels)
9
- end
10
-
11
6
  def parcels
12
7
  [
13
8
  initial_space,
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: danica
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.0
4
+ version: 2.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Darthjee
@@ -202,6 +202,7 @@ files:
202
202
  - spec/spec_helper.rb
203
203
  - spec/support/models/functions/baskara.rb
204
204
  - spec/support/models/functions/gauss.rb
205
+ - spec/support/models/functions/hyperbole.rb
205
206
  - spec/support/models/functions/spatial.rb
206
207
  - spec/support/shared_contexts/common.rb
207
208
  - spec/support/shared_examples/base_operations.rb
@@ -258,6 +259,7 @@ test_files:
258
259
  - spec/spec_helper.rb
259
260
  - spec/support/models/functions/baskara.rb
260
261
  - spec/support/models/functions/gauss.rb
262
+ - spec/support/models/functions/hyperbole.rb
261
263
  - spec/support/models/functions/spatial.rb
262
264
  - spec/support/shared_contexts/common.rb
263
265
  - spec/support/shared_examples/base_operations.rb