dydx 0.1.25 → 0.1.28
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/README.md +62 -90
- data/lib/dydx/algebra/formula.rb +1 -0
- data/lib/dydx/helper.rb +13 -3
- data/lib/dydx/version.rb +1 -1
- data/spec/dydx_spec.rb +2 -2
- data/spec/lib/algebra/operator/parts/formula_spec.rb +4 -2
- data/spec/lib/helper_spec.rb +5 -0
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: a53690ed9d47e90702608c9276bdf2dd76af42f4
|
|
4
|
+
data.tar.gz: eb525a3c66c33109a6c70a621c1dfaff1fe31bb4
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: afc1fabc722750af74ff12f064479928cbc3863066d76f20bb74aa46317bd556c3d90d92abf0365c528feaa76029c00e245b5ee31140a5ad2721e9145172b6c6
|
|
7
|
+
data.tar.gz: a1f587f80289788da9b414af04c3efe270a49b5a4e84287c88a6c58a3d7fc2bcb6619fb6e95b9a00650e2782f4ef0cf4749bac894d34790b5b80144561c094fe
|
data/README.md
CHANGED
|
@@ -1,115 +1,95 @@
|
|
|
1
|
-
# Dydx
|
|
2
|
-
It always happens you want to differentiate some formulas with ruby. right?.....
|
|
1
|
+
# Dydx is new math DSL in Ruby
|
|
3
2
|
|
|
4
|
-
|
|
3
|
+
### Since you report a bug, I will fix it within 24 hours.
|
|
4
|
+
|
|
5
|
+
The most important thing in this DSL is
|
|
5
6
|
|
|
6
|
-
|
|
7
|
+
we can handle math in the same sense sense of the math on paper.
|
|
7
8
|
|
|
8
|
-
|
|
9
|
+
ex. limit, trigonometric functions and logarithmic.
|
|
9
10
|
|
|
10
11
|
|
|
12
|
+
After `inlcude Dydx` , ruby become like other language.
|
|
11
13
|
|
|
14
|
+
## Outline
|
|
12
15
|
```ruby:
|
|
13
16
|
require 'dydx'
|
|
14
17
|
include Dydx
|
|
15
18
|
|
|
19
|
+
# Define the function. syntax is not good enough...
|
|
16
20
|
f(x) <= x ^ 2
|
|
17
21
|
|
|
18
22
|
f(3)
|
|
19
|
-
|
|
23
|
+
=> 9
|
|
20
24
|
|
|
21
25
|
f(x).to_s
|
|
22
|
-
|
|
26
|
+
=> "( x ^ 2 )"
|
|
23
27
|
|
|
24
28
|
f(x) == eval('f(x).to_s')
|
|
25
|
-
|
|
29
|
+
=> true
|
|
26
30
|
|
|
31
|
+
# Differentiate
|
|
27
32
|
g(x) <= d/dx(f(x))
|
|
28
33
|
|
|
29
34
|
g(3)
|
|
30
|
-
|
|
35
|
+
=> 6
|
|
31
36
|
|
|
32
|
-
|
|
37
|
+
g(x).to_s
|
|
38
|
+
=> '2 * x'
|
|
33
39
|
|
|
40
|
+
# Integrate
|
|
34
41
|
S(f(x), dx)[0, 1]
|
|
35
|
-
|
|
42
|
+
=> 0.3333333333333334
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
#### limit, trigonometric functions and logarithmic.
|
|
47
|
+
```ruby:
|
|
48
|
+
|
|
49
|
+
f(z) <= log(z)
|
|
50
|
+
S(f(z), dz)[0,1]
|
|
51
|
+
=> -Infinity
|
|
36
52
|
|
|
37
53
|
( d/dx(log(x)) ).to_s
|
|
38
|
-
|
|
54
|
+
=> "( 1 / x )"
|
|
39
55
|
|
|
40
56
|
( d/dx(cos(x)) ).to_s
|
|
41
|
-
|
|
57
|
+
=> "( - sin( x ) )"
|
|
42
58
|
|
|
43
59
|
( d/dx(e ^ x) ).to_s
|
|
44
|
-
|
|
60
|
+
=> "( e ^ x )"
|
|
45
61
|
|
|
46
62
|
f(x) <= sin(x)
|
|
47
63
|
S(f(x), dx)[0, Math::PI/2]
|
|
48
|
-
|
|
64
|
+
=> 1.000000000021139
|
|
49
65
|
|
|
66
|
+
# standard normal distribution;
|
|
50
67
|
f(x) <= (1.0 / ( ( 2.0 * pi ) ^ 0.5 ) ) * ( e ^ (- (x ^ 2) / 2) )
|
|
51
68
|
S(f(x), dx)[-oo, oo]
|
|
52
|
-
|
|
69
|
+
=> 0.9952054164466917
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
#### it's like a magic...
|
|
53
73
|
|
|
74
|
+
```ruby:
|
|
54
75
|
f(x) <= x ^ 2
|
|
55
76
|
|
|
56
77
|
f(a + b).to_s
|
|
57
|
-
|
|
78
|
+
=> "( ( a + b ) ^ 2 )"
|
|
58
79
|
|
|
80
|
+
#↓it"s magic!!!
|
|
59
81
|
g(a, b) <= f(a + b)
|
|
60
82
|
|
|
61
83
|
g(a, b).to_s
|
|
62
|
-
|
|
84
|
+
=> "( ( a + b ) ^ 2 )"
|
|
63
85
|
|
|
64
86
|
g(2, 2)
|
|
65
|
-
|
|
87
|
+
=> 16
|
|
66
88
|
|
|
67
89
|
( d/da(g(a, b)) ).to_s
|
|
68
90
|
=> "( 2 * ( a + b ) )"
|
|
69
|
-
```
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
```
|
|
76
|
-
require 'dydx'
|
|
77
|
-
include Dydx
|
|
78
|
-
|
|
79
|
-
# There are three types of differential interface
|
|
80
|
-
|
|
81
|
-
( d/dx(x^2) ).to_s
|
|
82
|
-
=> "( 2 * x )"
|
|
83
|
-
|
|
84
|
-
log(z).d(z).to_s
|
|
85
|
-
=> "( 1 / z )"
|
|
86
91
|
|
|
87
|
-
|
|
88
|
-
(dy/dx).to_s
|
|
89
|
-
=> "( e ^ x )"
|
|
90
|
-
|
|
91
|
-
```
|
|
92
|
-
|
|
93
|
-
You may wonder why undefined `x` , `e` and `z` are handleable?
|
|
94
|
-
|
|
95
|
-
`method_missing` solve this problem by converting undefine variable into internal class object.
|
|
96
|
-
|
|
97
|
-
Like this.
|
|
98
|
-
|
|
99
|
-
```
|
|
100
|
-
x + x
|
|
101
|
-
=> #<Dydx::Algebra::Formula:0x007fb0a4039fb0 @f=#<Dydx::Algebra::Set::Num:0x007fb0a48169e0 @n=2>, @operator=:*, @g=:x>
|
|
102
|
-
|
|
103
|
-
e
|
|
104
|
-
=> #<Dydx::Algebra::Set::E:0x007fb0a383e9f0>
|
|
105
|
-
|
|
106
|
-
log(sin(x))
|
|
107
|
-
=> #<Dydx::Algebra::Set::Log:0x007fe7cd971528 @f=#<Dydx::Algebra::Set::Sin:0x007fe7cd971550 @x=:x>>
|
|
108
|
-
```
|
|
109
|
-
|
|
110
|
-
And this DSL has strong simplify.
|
|
111
|
-
|
|
112
|
-
```
|
|
92
|
+
# simplify
|
|
113
93
|
((x * y) + (z * x)).to_s
|
|
114
94
|
=> "( x * ( y + z ) )"
|
|
115
95
|
|
|
@@ -120,38 +100,30 @@ And this DSL has strong simplify.
|
|
|
120
100
|
=> "( 2 * x )"
|
|
121
101
|
```
|
|
122
102
|
|
|
123
|
-
I show some differential calculus.
|
|
124
|
-
|
|
125
|
-
```
|
|
126
|
-
# pretermit '#to_s'
|
|
127
|
-
|
|
128
|
-
d/dz(log(z))
|
|
129
|
-
=> "( 1 / z )"
|
|
130
|
-
|
|
131
|
-
d/dx(x^n)
|
|
132
|
-
=> "( n * ( x ^ ( n - 1 ) ) )"
|
|
133
|
-
|
|
134
|
-
$y = cos(x)
|
|
135
|
-
dy/dx
|
|
136
|
-
=> "( - sin( x ) )"
|
|
137
103
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
=> "( a * t )"
|
|
104
|
+
## Documents
|
|
105
|
+
I'm going to write now...cominng soon....
|
|
141
106
|
|
|
142
|
-
|
|
143
|
-
=>"a"
|
|
144
|
-
|
|
145
|
-
((x ^ 2) * y).d(x)
|
|
146
|
-
=> "( ( 2 * x ) * y )"
|
|
147
|
-
|
|
148
|
-
((x ^ 2) * y).d(x).d(y)
|
|
149
|
-
=> "( 2 * x )"
|
|
107
|
+
### Module, class configuration
|
|
150
108
|
|
|
151
109
|
```
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
110
|
+
Dydx
|
|
111
|
+
|- Algebra
|
|
112
|
+
| |- Set
|
|
113
|
+
| | |- Num
|
|
114
|
+
| | |- ....
|
|
115
|
+
| |
|
|
116
|
+
| |- Operator
|
|
117
|
+
| | |- Interface
|
|
118
|
+
| | |- ....
|
|
119
|
+
| |
|
|
120
|
+
| |- Formula
|
|
121
|
+
| |- inverse
|
|
122
|
+
|
|
|
123
|
+
|- Function
|
|
124
|
+
|- Delta
|
|
125
|
+
|- Integrand
|
|
126
|
+
```
|
|
155
127
|
|
|
156
128
|
## Installation
|
|
157
129
|
|
data/lib/dydx/algebra/formula.rb
CHANGED
data/lib/dydx/helper.rb
CHANGED
|
@@ -61,10 +61,8 @@ module Dydx
|
|
|
61
61
|
def combinable?(x, operator)
|
|
62
62
|
case operator
|
|
63
63
|
when :+
|
|
64
|
-
self == x ||
|
|
65
64
|
(is_num? && x.is_num?) ||
|
|
66
|
-
|
|
67
|
-
(x.multiplication? && (x.f == self || x.g == self)) ||
|
|
65
|
+
like_term?(x) ||
|
|
68
66
|
inverse?(:+, x)
|
|
69
67
|
when :*
|
|
70
68
|
self == x ||
|
|
@@ -75,6 +73,18 @@ module Dydx
|
|
|
75
73
|
end
|
|
76
74
|
end
|
|
77
75
|
|
|
76
|
+
def like_term?(x)
|
|
77
|
+
boolean = if self == x
|
|
78
|
+
elsif formula?(:*) && include?(x)
|
|
79
|
+
elsif x.formula?(:*) && x.include?(self)
|
|
80
|
+
elsif ((formula?(:*) && formula?(:*)) && (([f, g] & [x.f, x.g]).any?{|x| x.is_a?(Symbol)}))
|
|
81
|
+
else
|
|
82
|
+
true
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
!boolean
|
|
86
|
+
end
|
|
87
|
+
|
|
78
88
|
def is_multiple_of(x)
|
|
79
89
|
if is_0?
|
|
80
90
|
e0
|
data/lib/dydx/version.rb
CHANGED
data/spec/dydx_spec.rb
CHANGED
|
@@ -17,8 +17,8 @@ describe Dydx do
|
|
|
17
17
|
$b = (:x ^ (:x * 2))
|
|
18
18
|
let(:d1){ db/dx }
|
|
19
19
|
let(:d2){ d/dx($b) }
|
|
20
|
-
it{ expect(d1.to_s).to eq('( (
|
|
21
|
-
it{ expect(d2.to_s).to eq('( (
|
|
20
|
+
it{ expect(d1.to_s).to eq('( ( 2 * x ) * ( x ^ ( ( 2 * x ) - 1 ) ) )') }
|
|
21
|
+
it{ expect(d2.to_s).to eq('( ( 2 * x ) * ( x ^ ( ( 2 * x ) - 1 ) ) )') }
|
|
22
22
|
end
|
|
23
23
|
|
|
24
24
|
context 'ex3' do
|
|
@@ -47,10 +47,12 @@ describe Dydx::Algebra::Operator::Parts::Formula do
|
|
|
47
47
|
it{ expect((:y - (:x - :y)).to_s).to eq('( ( 2 * y ) - x )') }
|
|
48
48
|
it{ expect((:y - (:y - :x)).to_s).to eq('x') }
|
|
49
49
|
|
|
50
|
-
it{ expect(((:x * 2) ^ 2).to_s).to eq('( ( x ^ 2 )
|
|
50
|
+
it{ expect(((:x * 2) ^ 2).to_s).to eq('( 4 * ( x ^ 2 ) )') }
|
|
51
51
|
it{ expect(((:x / 2) ^ 2).to_s).to eq('( ( x ^ 2 ) / 4 )') }
|
|
52
52
|
|
|
53
|
+
it{ expect((3*x + 4*(x^2)+ 4*x).to_s).to eq('( ( 7 * x ) + ( 4 * ( x ^ 2 ) ) )') }
|
|
54
|
+
|
|
53
55
|
# TODO:
|
|
54
|
-
it{ expect((2 ^ (:x * 2)).to_s).to eq('( 2 ^ (
|
|
56
|
+
it{ expect((2 ^ (:x * 2)).to_s).to eq('( 2 ^ ( 2 * x ) )') }
|
|
55
57
|
it{ expect((2 ^ (:x / 2)).to_s).to eq('( 2 ^ ( x / 2 ) )') }
|
|
56
58
|
end
|
data/spec/lib/helper_spec.rb
CHANGED
|
@@ -25,6 +25,11 @@ describe Helper do
|
|
|
25
25
|
it{ expect((:x * :y).is_multiple_of(:z)).to be_false }
|
|
26
26
|
end
|
|
27
27
|
|
|
28
|
+
context '#like_term?' do
|
|
29
|
+
it{ expect(x.like_term?(x)).to be_true }
|
|
30
|
+
it{ expect((2 * x).like_term?((3 * x))).to be_true }
|
|
31
|
+
end
|
|
32
|
+
|
|
28
33
|
context '#combinable?' do
|
|
29
34
|
it{ expect(:x.combinable?(:x, :+)).to be_true }
|
|
30
35
|
it{ expect(:x.combinable?(2 * :x, :+)).to be_true }
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: dydx
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.28
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- gogotanaka
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2014-05-
|
|
11
|
+
date: 2014-05-27 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: bundler
|