danica 0.1.0 → 0.2.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/.gitignore +2 -0
- data/Gemfile.lock +1 -1
- data/README.md +12 -5
- data/lib/danica/function.rb +5 -1
- data/lib/danica/function/division.rb +3 -3
- data/lib/danica/function/power.rb +24 -0
- data/lib/danica/version.rb +1 -1
- data/spec/lib/danica/function/division_spec.rb +4 -6
- data/spec/lib/danica/function/power_spec.rb +92 -0
- data/spec/lib/danica/function_spec.rb +10 -6
- data/spec/support/shared_examples/function.rb +3 -5
- metadata +4 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d679c98744cf0b50bc7cdb70cb70e2d08c99dc29
|
4
|
+
data.tar.gz: d21601f2005622dd2cb2ef9248c1fc42ee4bc9b7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9b1add172a4a3486cd170bd410a163c613684a6c96897be6b83be1cbe3525e02d78f8efcb259cef544f4ed518460a47fbf545ead9849f6749b97a696083705c3
|
7
|
+
data.tar.gz: e2402e6dbcd8b71f8ed81c9029aba06ab87a418239a7927ec54f7a631cc1a0d4bb8116d1bf8e477e4cb1e0804197dde9353d50f68c07b3183a26fdcebe2071ff
|
data/.gitignore
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -50,19 +50,26 @@ class Danica::Function
|
|
50
50
|
]
|
51
51
|
end
|
52
52
|
|
53
|
-
def
|
54
|
-
Product.new(variables: [
|
53
|
+
def spatial_acceleration
|
54
|
+
Division.new(numerator: Product.new(variables: [ acceleration, time_squared ]), denominator: 2)
|
55
55
|
end
|
56
56
|
|
57
|
-
def
|
58
|
-
|
57
|
+
def time_squared
|
58
|
+
Power.new(base: time, exponent: 2)
|
59
59
|
end
|
60
60
|
end
|
61
61
|
end
|
62
|
+
|
63
|
+
Danica::Function.new(
|
64
|
+
time: :t,
|
65
|
+
acceleration: 'a',
|
66
|
+
initial_space: { name: :S0, latex: 'S_0' },
|
67
|
+
initial_velocity: { name: :V0, latex: 'V_0' }
|
68
|
+
).to_tex
|
62
69
|
```
|
63
70
|
|
64
71
|
returns
|
65
72
|
```string
|
66
|
-
S_0 + V_0 \cdot t + \frac{a \cdot t
|
73
|
+
S_0 + V_0 \cdot t + \frac{a \cdot t^2}{2}
|
67
74
|
```
|
68
75
|
|
data/lib/danica/function.rb
CHANGED
@@ -5,6 +5,7 @@ class Danica::Function
|
|
5
5
|
require 'danica/function/product'
|
6
6
|
require 'danica/function/sum'
|
7
7
|
require 'danica/function/division'
|
8
|
+
require 'danica/function/power'
|
8
9
|
|
9
10
|
attr_accessor :name, :variables
|
10
11
|
|
@@ -29,6 +30,9 @@ class Danica::Function
|
|
29
30
|
private
|
30
31
|
|
31
32
|
def wrap_value(value)
|
32
|
-
|
33
|
+
return Danica::Number.new(value) if value.is_a?(Numeric)
|
34
|
+
return Danica::Variable.new(value) if value.is_a?(Hash)
|
35
|
+
return Danica::Variable.new(name: value) if [ String, Symbol ].any? { |c| value.is_a?(c) }
|
36
|
+
value
|
33
37
|
end
|
34
38
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module Danica
|
2
2
|
class Function
|
3
3
|
class Division < Function
|
4
|
-
|
4
|
+
attr_reader :numerator, :denominator
|
5
5
|
|
6
6
|
def to_f
|
7
7
|
numerator.to_f / denominator.to_f
|
@@ -12,11 +12,11 @@ module Danica
|
|
12
12
|
end
|
13
13
|
|
14
14
|
def numerator=(value)
|
15
|
-
@numerator
|
15
|
+
@numerator = wrap_value(value)
|
16
16
|
end
|
17
17
|
|
18
18
|
def denominator=(value)
|
19
|
-
@denominator
|
19
|
+
@denominator = wrap_value(value)
|
20
20
|
end
|
21
21
|
end
|
22
22
|
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Danica
|
2
|
+
class Function
|
3
|
+
class Power < Function
|
4
|
+
attr_reader :base, :exponent
|
5
|
+
|
6
|
+
def to_f
|
7
|
+
base.to_f ** exponent.to_f
|
8
|
+
end
|
9
|
+
|
10
|
+
def to_tex
|
11
|
+
[ base, exponent ].map(&:to_tex).join('^')
|
12
|
+
end
|
13
|
+
|
14
|
+
def base=(value)
|
15
|
+
@base = wrap_value(value)
|
16
|
+
end
|
17
|
+
|
18
|
+
def exponent=(value)
|
19
|
+
@exponent = wrap_value(value)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
data/lib/danica/version.rb
CHANGED
@@ -5,7 +5,7 @@ describe Danica::Function::Division do
|
|
5
5
|
let(:values) { [ 2, 4 ] }
|
6
6
|
let(:variables) do
|
7
7
|
[ 1, 2 ].map do |i|
|
8
|
-
|
8
|
+
{ name: "X#{i}", value: values[i-1] }
|
9
9
|
end
|
10
10
|
end
|
11
11
|
let(:subject) do
|
@@ -53,9 +53,7 @@ describe Danica::Function::Division do
|
|
53
53
|
describe 'to_tex' do
|
54
54
|
context 'when variables have no value' do
|
55
55
|
let(:variables) do
|
56
|
-
[ 1,2 ].map
|
57
|
-
Danica::Variable.new(name: "X#{i}")
|
58
|
-
end
|
56
|
+
[ 1,2 ].map { |i| "X#{i}" }
|
59
57
|
end
|
60
58
|
|
61
59
|
it 'returns a latex format fraction' do
|
@@ -65,7 +63,7 @@ describe Danica::Function::Division do
|
|
65
63
|
|
66
64
|
context 'when one of the variables has value' do
|
67
65
|
before do
|
68
|
-
|
66
|
+
subject.denominator.value = nil
|
69
67
|
end
|
70
68
|
|
71
69
|
it 'returns the number instead of the value' do
|
@@ -81,8 +79,8 @@ describe Danica::Function::Division do
|
|
81
79
|
|
82
80
|
context 'when one of the variables is a number' do
|
83
81
|
before do
|
84
|
-
variables[1].value = nil
|
85
82
|
variables[0] = 1
|
83
|
+
subject.denominator.value = nil
|
86
84
|
end
|
87
85
|
|
88
86
|
it 'prints both numbers' do
|
@@ -0,0 +1,92 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Danica::Function::Power do
|
4
|
+
let(:variables_number) { 4 }
|
5
|
+
let(:values) { [ 3, 2 ] }
|
6
|
+
let(:variables) do
|
7
|
+
[ 1, 2 ].map do |i|
|
8
|
+
{ name: "X#{i}", value: values[i-1] }
|
9
|
+
end
|
10
|
+
end
|
11
|
+
let(:subject) do
|
12
|
+
described_class.new(base: variables[0], exponent: variables[1])
|
13
|
+
end
|
14
|
+
|
15
|
+
describe 'to_f' do
|
16
|
+
context 'when variables are not numbers but have value' do
|
17
|
+
it 'returns the division of the values' do
|
18
|
+
expect(subject.to_f).to eq(9.0)
|
19
|
+
end
|
20
|
+
|
21
|
+
it do
|
22
|
+
expect(subject.to_f).to be_a(Float)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context 'when all the variables are numbers' do
|
27
|
+
let(:variables) { [ 3, 2 ] }
|
28
|
+
|
29
|
+
it 'returns the division of the values' do
|
30
|
+
expect(subject.to_f).to eq(9.0)
|
31
|
+
end
|
32
|
+
|
33
|
+
it do
|
34
|
+
expect(subject.to_f).to be_a(Float)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
context 'when one the variables are numbers' do
|
39
|
+
before do
|
40
|
+
variables[0] = 3
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'returns the division of the values' do
|
44
|
+
expect(subject.to_f).to eq(9.0)
|
45
|
+
end
|
46
|
+
|
47
|
+
it do
|
48
|
+
expect(subject.to_f).to be_a(Float)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe 'to_tex' do
|
54
|
+
context 'when variables have no value' do
|
55
|
+
let(:variables) do
|
56
|
+
[ 1,2 ].map { |i| "X#{i}" }
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'returns a latex format fraction' do
|
60
|
+
expect(subject.to_tex).to eq('X1^X2')
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
context 'when one of the variables has value' do
|
65
|
+
before do
|
66
|
+
subject.exponent.value = nil
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'returns the number instead of the value' do
|
70
|
+
expect(subject.to_tex).to eq('3^X2')
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
context 'when both variables are numeric' do
|
75
|
+
it 'prints both numbers' do
|
76
|
+
expect(subject.to_tex).to eq('3^2')
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
context 'when one of the variables is a number' do
|
81
|
+
before do
|
82
|
+
variables[0] = 1
|
83
|
+
subject.exponent.value = nil
|
84
|
+
end
|
85
|
+
|
86
|
+
it 'prints both numbers' do
|
87
|
+
expect(subject.to_tex).to eq('1^X2')
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
@@ -25,7 +25,11 @@ describe Danica::Function do
|
|
25
25
|
end
|
26
26
|
|
27
27
|
def spatial_acceleration
|
28
|
-
Division.new(numerator: Product.new(variables: [ acceleration,
|
28
|
+
Division.new(numerator: Product.new(variables: [ acceleration, time_squared ]), denominator: 2)
|
29
|
+
end
|
30
|
+
|
31
|
+
def time_squared
|
32
|
+
Power.new(base: time, exponent: 2)
|
29
33
|
end
|
30
34
|
end
|
31
35
|
end
|
@@ -34,15 +38,15 @@ describe Danica::Function do
|
|
34
38
|
context 'when creating the spatial function for constantly accelerated movement' do
|
35
39
|
let(:variables) do
|
36
40
|
{
|
37
|
-
time:
|
38
|
-
acceleration:
|
39
|
-
initial_space:
|
40
|
-
initial_velocity:
|
41
|
+
time: :t,
|
42
|
+
acceleration: 'a',
|
43
|
+
initial_space: { name: :S0, latex: 'S_0' },
|
44
|
+
initial_velocity: { name: :V0, latex: 'V_0' }
|
41
45
|
}
|
42
46
|
end
|
43
47
|
|
44
48
|
let(:subject) { described_class::Spatial.new(variables) }
|
45
|
-
let(:expected) { 'S_0 + V_0 \cdot t + \frac{a \cdot t
|
49
|
+
let(:expected) { 'S_0 + V_0 \cdot t + \frac{a \cdot t^2}{2}' }
|
46
50
|
|
47
51
|
it 'return the latex format CAM' do
|
48
52
|
expect(subject.to_tex).to eq(expected)
|
@@ -9,7 +9,7 @@ shared_examples 'a function that knows how to calculate' do |arguments|
|
|
9
9
|
end
|
10
10
|
let(:variables) do
|
11
11
|
(1..4).map do |i|
|
12
|
-
|
12
|
+
{ name: "X#{i}", value: numeric_variables[i-1] }
|
13
13
|
end
|
14
14
|
end
|
15
15
|
let(:numeric_variables){ (1..4).to_a }
|
@@ -50,9 +50,7 @@ shared_examples 'a function that knows how to write to tex' do |arguments|
|
|
50
50
|
|
51
51
|
describe 'to_tex' do
|
52
52
|
let(:variables) do
|
53
|
-
(1..4).map
|
54
|
-
Danica::Variable.new(name: "X#{i}")
|
55
|
-
end
|
53
|
+
(1..4).map { |i| "X#{i}" }
|
56
54
|
end
|
57
55
|
|
58
56
|
context 'when variables have no value' do
|
@@ -65,7 +63,7 @@ shared_examples 'a function that knows how to write to tex' do |arguments|
|
|
65
63
|
let(:numeric_variables_index) { 1 }
|
66
64
|
before do
|
67
65
|
(0..numeric_variables_index).each do |i|
|
68
|
-
variables[i].value = numeric_variables[i]
|
66
|
+
subject.variables[i].value = numeric_variables[i]
|
69
67
|
end
|
70
68
|
end
|
71
69
|
|
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: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Darthjee
|
@@ -140,6 +140,7 @@ files:
|
|
140
140
|
- lib/danica/function.rb
|
141
141
|
- lib/danica/function/chained.rb
|
142
142
|
- lib/danica/function/division.rb
|
143
|
+
- lib/danica/function/power.rb
|
143
144
|
- lib/danica/function/product.rb
|
144
145
|
- lib/danica/function/sum.rb
|
145
146
|
- lib/danica/not_defined.rb
|
@@ -147,6 +148,7 @@ files:
|
|
147
148
|
- lib/danica/variable.rb
|
148
149
|
- lib/danica/version.rb
|
149
150
|
- spec/lib/danica/function/division_spec.rb
|
151
|
+
- spec/lib/danica/function/power_spec.rb
|
150
152
|
- spec/lib/danica/function/product_spec.rb
|
151
153
|
- spec/lib/danica/function/sum_spec.rb
|
152
154
|
- spec/lib/danica/function_spec.rb
|
@@ -178,6 +180,7 @@ specification_version: 4
|
|
178
180
|
summary: Danica
|
179
181
|
test_files:
|
180
182
|
- spec/lib/danica/function/division_spec.rb
|
183
|
+
- spec/lib/danica/function/power_spec.rb
|
181
184
|
- spec/lib/danica/function/product_spec.rb
|
182
185
|
- spec/lib/danica/function/sum_spec.rb
|
183
186
|
- spec/lib/danica/function_spec.rb
|