qlang 0.0.14142 → 0.0.141421

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: 6671b553251375c1573838ac60c23a091a2022fa
4
- data.tar.gz: 9c8f8d7a43d80dd9af9c13505eaf1d9cecbf739a
3
+ metadata.gz: 5cd27fe1e7d111bcb1fb356c5b981b8b141c67a0
4
+ data.tar.gz: 56d3cea8d211ddf39f9f9ee7ea80d57a4885ac66
5
5
  SHA512:
6
- metadata.gz: d690071a0649fc5ea24a9a83c70f4e13022abb0d48387ceb17178976668ddbfbb05aee9e5c1e0513226a1c01db93a9c491f39901b1b2c8e79081804b86507e8b
7
- data.tar.gz: 14f0486e217a786d6a1845e7a4a3e265b2f0563d82d6b90f227dc3b61e772325a24c66bdbee72f7ca81d35712f3705d35507a67055168a207839f71feb92128b
6
+ metadata.gz: 05b9aae4ef17bfb8724fbdf3383c66ca4e4f44a811221793a0e99ad1e585f9ce56c111cb142fd84b58a24087785e87c21a6fc4e4cc12650cbae546ce3edef506
7
+ data.tar.gz: 81f79ee745b10ed0cd90e0680094e9ffdcfc41d630593c846bc5baca21fe3d3bf43d735624be7f09e638dba8a65b304fed5de613a676d05fb3f54a7acb715da0
data/lib/qlang/api.rb CHANGED
@@ -9,15 +9,31 @@ module Qlang
9
9
  # TODO:
10
10
  class ::String
11
11
  def rm(str_or_rgx)
12
+ gsub(str_or_rgx, '')
13
+ end
14
+
15
+ def rm!(str_or_rgx)
12
16
  gsub!(str_or_rgx, '')
17
+ self
13
18
  end
14
19
 
15
- def rms(*str_or_rgxs)
20
+ def rms!(*str_or_rgxs)
16
21
  str_or_rgxs.each do |str_or_rgx|
17
- rm(str_or_rgx)
22
+ rm!(str_or_rgx)
18
23
  end
19
24
  self
20
25
  end
26
+
27
+ # FIX:
28
+ def equalize!
29
+ rms!(/\A +/, / +\z/)
30
+ if self =~ /\A\(/ && self =~ /\)\z/
31
+ rms!(/\A\(/, /\)\z/)
32
+ rms!(/\A +/, / +\z/)
33
+ else
34
+ self
35
+ end
36
+ end
21
37
  end
22
38
 
23
39
  class ::Matrix
@@ -2,8 +2,14 @@ module Qlang
2
2
  module Api
3
3
  module ListApi
4
4
  def execute(arys)
5
- combineds_by_equal = arys.map { |ary| "#{ary[0]}=#{ary[1]}" }.join(', ')
6
- "list(#{combineds_by_equal})"
5
+ case $type
6
+ when :R
7
+ combineds_by_equal = arys.map { |ary| "#{ary[0]}=#{ary[1]}" }.join(', ')
8
+ "list(#{combineds_by_equal})"
9
+ when :Ruby
10
+ fail 'List is not implemented for Ruby'
11
+ end
12
+
7
13
  end
8
14
  module_function :execute
9
15
  end
data/lib/qlang/iq.rb CHANGED
@@ -1,16 +1,24 @@
1
1
  module Qlang
2
2
  module Iq
3
+ class Dydx::Algebra::Formula
4
+ # FIX:
5
+ def to_q
6
+ str = to_s.gsub(/\*\*/, '^').rm(' * ')
7
+ str.equalize!
8
+ end
9
+ end
3
10
  def execute(code)
4
11
  ruby_obj = eval Q.to_ruby.compile(code)
5
- case ruby_obj
6
- when Matrix, Vector
12
+ output = case ruby_obj
13
+ when Matrix, Vector, Dydx::Algebra::Formula
7
14
  ruby_obj.to_q
8
15
  when Float::INFINITY
9
16
  'oo'
10
17
  when - Float::INFINITY
11
18
  '-oo'
12
19
  else
13
- ruby_obj
20
+ str = ruby_obj.to_s
21
+ str.equalize!
14
22
  end
15
23
  end
16
24
  module_function :execute
@@ -7,7 +7,7 @@ module Qlang
7
7
  rule(/[ \t\f]/)
8
8
 
9
9
  rule(/\r\n/) { :NLIN }
10
- rule(/\w.*/) { :OTHER }
10
+ rule(/[\w\(].*/) { :FOML }
11
11
  end
12
12
  end
13
13
  end
data/lib/qlang/parser.rb CHANGED
@@ -49,7 +49,7 @@ module Qlang
49
49
  cont_lexed = Lexer::FuncLexer.new(lexed.get_value(cont_token_with_num))
50
50
 
51
51
  case cont_lexed.token_str
52
- when /:FDEF\d:EQL\d:OTHER\d/
52
+ when /:FDEF\d:EQL\d:FOML\d/
53
53
  cont = FuncParser.execute(cont_lexed)
54
54
  lexed.ch_value(cont_token_with_num, cont)
55
55
  lexed.ch_token(cont_token_with_num, :R)
@@ -6,11 +6,44 @@ module Qlang
6
6
  lexed.fix_r_txt!
7
7
  fdef_ary = lexed[0][:FDEF].split('')
8
8
  func_name = fdef_ary.shift
9
- args = fdef_ary.join.rms('(', ')', ',', ' ').split('')
9
+ args = fdef_ary.join.rms!('(', ')', ',', ' ').split('')
10
10
 
11
- FuncApi.execute(func_name, args, lexed[-1][:OTHER])
11
+ FuncApi.execute(func_name, args, FomlParser.execute(lexed[-1][:FOML]))
12
12
  end
13
13
  module_function :execute
14
+
15
+ # FIX:
16
+ module FomlParser
17
+ def execute(lexed)
18
+ ss = StringScanner.new(lexed)
19
+ result = ''
20
+ until ss.eos?
21
+ { EXP: /\^/, BFUNC: /sin|cos|tan|log/, MUL: /(pi|[1-9a-z]){2,}/, SNGL: /(pi|[1-9a-z])/, OTHER: /([^\^1-9a-z]|^pi)+/ }.each do |token , rgx|
22
+ if ss.scan(rgx)
23
+ item = case token
24
+ when :EXP
25
+ $type == :Ruby ? '**' : '^'
26
+ when :MUL
27
+ sss = StringScanner.new(ss[0])
28
+ ary = []
29
+ until sss.eos?
30
+ [/pi/, /[1-9a-z]/].each do |rgx2|
31
+ ary << sss[0] if sss.scan(rgx2)
32
+ end
33
+ end
34
+ ary.join(' * ')
35
+ else
36
+ ss[0]
37
+ end
38
+ result += item
39
+ break
40
+ end
41
+ end
42
+ end
43
+ result
44
+ end
45
+ module_function :execute
46
+ end
14
47
  end
15
48
  end
16
49
  end
@@ -5,7 +5,7 @@ module Qlang
5
5
  def execute(string)
6
6
  integrated, range = string.scan(/S *\((.+)\)\[(.+)\]/).first
7
7
 
8
- integrated.rm(' ')
8
+ integrated.rm!(' ')
9
9
 
10
10
  IntegralApi.execute(integrated[0..-3], integrated[-2..-1], range)
11
11
  end
data/lib/qlang/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Qlang
2
- VERSION = "0.0.14142"
2
+ VERSION = "0.0.141421"
3
3
  end
data/spec/iq_spec.rb CHANGED
@@ -29,29 +29,73 @@ describe Qlang do
29
29
 
30
30
  describe 'Diff' do
31
31
  it do
32
- expect(Iq.execute('d/dx(e ** x)')).to eq(e ** x)
33
- expect(Iq.execute('d/dx(x ** 2)')).to eq(2 * x)
34
- expect(Iq.execute('d/dx(x * 2)')).to eq(2)
35
- expect(Iq.execute('d/dx( sin(x) )')).to eq(cos(x))
36
- expect(Iq.execute('d/dx(log( x ))')).to eq(1/x)
32
+ expect(Iq.execute('d/dx(e ** x)')).to eq('e ^ x')
33
+ expect(Iq.execute('d/dx(x ** 2)')).to eq('2x')
34
+ expect(Iq.execute('d/dx(x * 2)')).to eq('2')
35
+ expect(Iq.execute('d/dx( sin(x) )')).to eq('cos( x )')
36
+ expect(Iq.execute('d/dx(log( x ))')).to eq('1 / x')
37
37
  end
38
38
  end
39
39
 
40
40
  describe 'Integral' do
41
41
  it do
42
42
  expect(Iq.execute('S( log(x)dx )[0..1]')).to eq('-oo')
43
- expect(Iq.execute('S( sin(x)dx )[0..pi]')).to eq(2.0)
44
- expect(Iq.execute('S( cos(x)dx )[0..pi]')).to eq(0.0)
43
+ expect(Iq.execute('S( sin(x)dx )[0..pi]')).to eq('2.0')
44
+ expect(Iq.execute('S( cos(x)dx )[0..pi]')).to eq('0.0')
45
+ expect(Iq.execute('S( cos(x)dx )[0..pi]')).to eq('0.0')
45
46
  end
46
47
  end
47
48
 
48
- describe 'Function' do
49
- it do
50
- expect(Iq.execute('f(x, y) = x + y')).to eq(f(x, y) <= x + y)
51
- expect(Iq.execute('f( 4, 5 )')).to eq(9)
52
- expect(Iq.execute('g(x) = x ** 2')).to eq(g(x) <= x ** 2)
53
- expect(Iq.execute('g(2)')).to eq(4)
49
+ def self.def_test(name, input, output)
50
+ it name + '_def' do
51
+ expect(Iq.execute(input)).to eq(output)
52
+ end
53
+ end
54
+
55
+ def self.cal_test(name, input, output)
56
+ it name + '_cal' do
57
+ expect(Iq.execute(input)).to eq(output)
58
+ reset
54
59
  end
55
60
  end
61
+
62
+
63
+ describe 'Function' do
64
+ def_test('ex1', 'f(x, y) = x + y', 'x + y')
65
+ cal_test('ex1', 'f( 4, 5 )', '9.0')
66
+
67
+ def_test('ex2', 'f(x, y) = xy', 'x * y')
68
+ cal_test('ex2', 'f( 3, 9 )', '27.0')
69
+
70
+ def_test('ex3', 'f(x, y) = xy^2', 'x * ( y ** 2 )')
71
+ cal_test('ex3', 'f( 3, 2 )', '12.0')
72
+
73
+ def_test('ex4', 'f(x, y) = xy^2', 'x * ( y ** 2 )')
74
+ cal_test('ex4', 'df/dx', 'y ^ 2')
75
+
76
+ def_test('ex5', 'g(x) = x ^ 2', 'x ** 2')
77
+ cal_test('ex5', 'g(2)', '4.0')
78
+
79
+ def_test('ex6', 'h(x) = e ^ 2', 'e ** 2')
80
+ cal_test('ex6', 'h(2)', '7.3890560989306495')
81
+
82
+ def_test('ex7', 'h(x) = pix', 'pi * x')
83
+ cal_test('ex7', 'h(3)', '9.42477796076938')
84
+
85
+ def_test('ex8', 'h(x) = pie', 'pi * e')
86
+ cal_test('ex8', 'h(2)', '8.539734222673566')
87
+
88
+ def_test('ex9', 'h(x) = ( 1 / ( 2pi ) ^ ( 1 / 2.0 ) ) * e ^ ( - x ^ 2 / 2 )', '( ( 4503599627370496 / 6369051672525773 ) / ( pi ** 0.5 ) ) * ( e ** ( ( - ( x ** 2 ) ) / 2 ) )')
89
+ cal_test('ex9', 'S( h(x)dx )[-oo..oo]', '1.0')
90
+
91
+ def_test('ex10', 'f(x) = sin(x)', 'sin( x )')
92
+ cal_test('ex10', 'f(pi)', '0.0')
93
+
94
+ def_test('ex11', 'f(x) = cos(x)', 'cos( x )')
95
+ cal_test('ex11', 'f(pi)', '-1.0')
96
+
97
+ def_test('ex11', 'f(x) = log(x)', 'log( x )')
98
+ cal_test('ex11', 'f(e)', '1.0')
99
+ end
56
100
  end
57
101
  end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe Qlang do
4
+ describe 'Function' do
5
+ context 'into R' do
6
+ it do
7
+ expect(
8
+ Q.to_r.compile('f(x, y) = x + y')
9
+ ).to eq(
10
+ "f <- function(x ,y) x + y"
11
+ )
12
+
13
+ expect(
14
+ Q.to_r.compile('g(x) = x ^ 2')
15
+ ).to eq(
16
+ "g <- function(x) x ^ 2"
17
+ )
18
+
19
+ expect(
20
+ Q.to_r.compile('g(x) = x ^ (2 + 2)')
21
+ ).to eq(
22
+ "g <- function(x) x ^ (2 + 2)"
23
+ )
24
+
25
+ expect(
26
+ Q.to_r.compile('h(a, b, c) = a ^ 2 + b ^ 2 + c ^ 2')
27
+ ).to eq(
28
+ "h <- function(a ,b ,c) a ^ 2 + b ^ 2 + c ^ 2"
29
+ )
30
+ end
31
+ end
32
+ end
33
+ end
@@ -7,6 +7,7 @@ describe Qlang do
7
7
 
8
8
  end
9
9
  end
10
+
10
11
  context 'into Ruby' do
11
12
  it do
12
13
  expect(
File without changes
File without changes
File without changes
data/spec/spec_helper.rb CHANGED
@@ -4,4 +4,5 @@ include Qlang
4
4
 
5
5
  require 'coveralls'
6
6
  Coveralls.wear!
7
+
7
8
  require 'pry'
metadata CHANGED
@@ -1,69 +1,69 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: qlang
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.14142
4
+ version: 0.0.141421
5
5
  platform: ruby
6
6
  authors:
7
7
  - gogotanaka
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-12 00:00:00.000000000 Z
11
+ date: 2014-08-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dydx
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: 0.1.412
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: 0.1.412
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ~>
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
33
  version: '1.6'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ~>
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '1.6'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rspec
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - '>='
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - '>='
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  description: Enjoy MATH!
@@ -74,11 +74,11 @@ executables:
74
74
  extensions: []
75
75
  extra_rdoc_files: []
76
76
  files:
77
- - .coveralls.yml
78
- - .gitignore
79
- - .rspec
80
- - .rubocop.yml
81
- - .travis.yml
77
+ - ".coveralls.yml"
78
+ - ".gitignore"
79
+ - ".rspec"
80
+ - ".rubocop.yml"
81
+ - ".travis.yml"
82
82
  - Gemfile
83
83
  - LICENSE.txt
84
84
  - README.md
@@ -107,14 +107,14 @@ files:
107
107
  - lib/qlang/parser/vector_parser.rb
108
108
  - lib/qlang/version.rb
109
109
  - q_lang.gemspec
110
- - spec/function_spec.rb
111
- - spec/integral_spec.rb
112
110
  - spec/iq_spec.rb
113
- - spec/list_spec.rb
114
- - spec/matrix_spec.rb
111
+ - spec/objects/function_spec.rb
112
+ - spec/objects/integral_spec.rb
113
+ - spec/objects/list_spec.rb
114
+ - spec/objects/matrix_spec.rb
115
+ - spec/objects/vector_spec.rb
115
116
  - spec/q_lang_spec.rb
116
117
  - spec/spec_helper.rb
117
- - spec/vector_spec.rb
118
118
  homepage: http://q-language.org/
119
119
  licenses:
120
120
  - MIT
@@ -125,26 +125,26 @@ require_paths:
125
125
  - lib
126
126
  required_ruby_version: !ruby/object:Gem::Requirement
127
127
  requirements:
128
- - - '>='
128
+ - - ">="
129
129
  - !ruby/object:Gem::Version
130
130
  version: '0'
131
131
  required_rubygems_version: !ruby/object:Gem::Requirement
132
132
  requirements:
133
- - - '>='
133
+ - - ">="
134
134
  - !ruby/object:Gem::Version
135
135
  version: '0'
136
136
  requirements: []
137
137
  rubyforge_project:
138
- rubygems_version: 2.0.14
138
+ rubygems_version: 2.4.1
139
139
  signing_key:
140
140
  specification_version: 4
141
141
  summary: Enjoy MATH!
142
142
  test_files:
143
- - spec/function_spec.rb
144
- - spec/integral_spec.rb
145
143
  - spec/iq_spec.rb
146
- - spec/list_spec.rb
147
- - spec/matrix_spec.rb
144
+ - spec/objects/function_spec.rb
145
+ - spec/objects/integral_spec.rb
146
+ - spec/objects/list_spec.rb
147
+ - spec/objects/matrix_spec.rb
148
+ - spec/objects/vector_spec.rb
148
149
  - spec/q_lang_spec.rb
149
150
  - spec/spec_helper.rb
150
- - spec/vector_spec.rb
@@ -1,31 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Qlang do
4
- describe 'Function' do
5
- it do
6
- expect(
7
- Q.compile('f(x, y) = x + y')
8
- ).to eq(
9
- "f <- function(x ,y) x + y"
10
- )
11
-
12
- expect(
13
- Q.compile('g(x) = x ** 2')
14
- ).to eq(
15
- "g <- function(x) x ** 2"
16
- )
17
-
18
- expect(
19
- Q.compile('g(x) = x ** (2 + 2)')
20
- ).to eq(
21
- "g <- function(x) x ** (2 + 2)"
22
- )
23
-
24
- expect(
25
- Q.compile('h(a, b, c) = a ** 2 + b ** 2 + c ** 2')
26
- ).to eq(
27
- "h <- function(a ,b ,c) a ** 2 + b ** 2 + c ** 2"
28
- )
29
- end
30
- end
31
- end