hilbert 0.0.2700000

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. checksums.yaml +15 -0
  2. data/.coveralls.yml +2 -0
  3. data/.gitignore +23 -0
  4. data/.rubocop.yml +25 -0
  5. data/.travis.yml +5 -0
  6. data/Gemfile +9 -0
  7. data/Guardfile +33 -0
  8. data/LICENSE.txt +22 -0
  9. data/README.md +159 -0
  10. data/Rakefile +20 -0
  11. data/bin/qlang +29 -0
  12. data/core/Q/Lexer.hs +9 -0
  13. data/core/Q/Parser.hs +9 -0
  14. data/core/Q.hs +19 -0
  15. data/ext/qlang/QMatrix/q_matrix.c +0 -0
  16. data/ext/qlang/extconf.rb +3 -0
  17. data/ext/qlang/qlang.c +65 -0
  18. data/ext/qlang/qlang.h +6 -0
  19. data/legacy_rspec/langs/Haskell/ex1_after.hs +74 -0
  20. data/legacy_rspec/langs/Haskell/ex1_before.hs +74 -0
  21. data/legacy_rspec/langs/Python/ex1_after.py +93 -0
  22. data/legacy_rspec/langs/Python/ex1_before.py +95 -0
  23. data/legacy_rspec/langs/R/ex1_after.R +89 -0
  24. data/legacy_rspec/langs/R/ex1_before.R +89 -0
  25. data/legacy_rspec/objects/list_spec.rb +31 -0
  26. data/legacy_rspec/objects/matrix_spec.rb +55 -0
  27. data/legacy_rspec/objects/vector_spec.rb +47 -0
  28. data/lib/qlang/api/func_api.rb +17 -0
  29. data/lib/qlang/api/integral_api.rb +16 -0
  30. data/lib/qlang/api/limit_api.rb +22 -0
  31. data/lib/qlang/api/list_api.rb +16 -0
  32. data/lib/qlang/api/matrix_api.rb +20 -0
  33. data/lib/qlang/api/sigma_api.rb +16 -0
  34. data/lib/qlang/api/vector_api.rb +19 -0
  35. data/lib/qlang/api.rb +23 -0
  36. data/lib/qlang/exec.rb +64 -0
  37. data/lib/qlang/iq.rb +45 -0
  38. data/lib/qlang/lexer/base.rb +107 -0
  39. data/lib/qlang/lexer/formula_lexer.rb +20 -0
  40. data/lib/qlang/lexer/main_lexer.rb +34 -0
  41. data/lib/qlang/lexer/tokens.rb +94 -0
  42. data/lib/qlang/lexer.rb +11 -0
  43. data/lib/qlang/meta_info.rb +27 -0
  44. data/lib/qlang/parser/base.rb +7 -0
  45. data/lib/qlang/parser/formula_parser.rb +36 -0
  46. data/lib/qlang/parser/func_parser.rb +15 -0
  47. data/lib/qlang/parser/integral_parser.rb +15 -0
  48. data/lib/qlang/parser/limit_parser.rb +16 -0
  49. data/lib/qlang/parser/list_parser.rb +12 -0
  50. data/lib/qlang/parser/matrix_parser.rb +17 -0
  51. data/lib/qlang/parser/sigma_parser.rb +17 -0
  52. data/lib/qlang/parser/vector_parser.rb +13 -0
  53. data/lib/qlang/parser.rb +101 -0
  54. data/lib/qlang/utils/langs.yml +7 -0
  55. data/lib/qlang/utils/ruby_ext.rb +54 -0
  56. data/lib/qlang/version.rb +3 -0
  57. data/lib/qlang.rb +37 -0
  58. data/qlang.gemspec +28 -0
  59. data/test/internal/test_tokens.rb +35 -0
  60. data/test/interpreter/base.rb +17 -0
  61. data/test/interpreter/test_differential.rb +43 -0
  62. data/test/interpreter/test_function.rb +44 -0
  63. data/test/interpreter/test_general.rb +12 -0
  64. data/test/interpreter/test_integral.rb +38 -0
  65. data/test/interpreter/test_limit.rb +37 -0
  66. data/test/interpreter/test_matrix.rb +70 -0
  67. data/test/interpreter/test_sigma.rb +25 -0
  68. data/test/interpreter/test_vector.rb +28 -0
  69. data/test/langs/test_r.rb +32 -0
  70. data/test/langs/test_ruby.rb +9 -0
  71. data/test/minitest_helper.rb +8 -0
  72. data/test/q_matrix/test_q_matrix.rb +13 -0
  73. data/test/test_qlang.rb +24 -0
  74. metadata +203 -0
@@ -0,0 +1,54 @@
1
+ # module ::Kernel
2
+ # def recursive_require(str)
3
+ # Dir["#{$:.first}/#{str}"].each do |file|
4
+ # require file
5
+ # end
6
+ # end
7
+ # end
8
+
9
+ class ::String
10
+ def parentheses
11
+ "(#{self})"
12
+ end
13
+
14
+ def braces
15
+ "{#{self}}"
16
+ end
17
+
18
+ def rm(str_or_rgx)
19
+ gsub(str_or_rgx, '')
20
+ end
21
+
22
+ def rm!(str_or_rgx)
23
+ gsub!(str_or_rgx, '')
24
+ self
25
+ end
26
+
27
+ def rms!(*str_or_rgxs)
28
+ str_or_rgxs.each do |str_or_rgx|
29
+ rm!(str_or_rgx)
30
+ end
31
+ self
32
+ end
33
+
34
+ def split_by_sp
35
+ split(/ +/)
36
+ end
37
+
38
+ # FIX:
39
+ def equalize!
40
+ rms!(/\A +/, / +\z/)
41
+ if self =~ /\A\(/ && self =~ /\)\z/
42
+ rms!(/\A\(/, /\)\z/)
43
+ rms!(/\A +/, / +\z/)
44
+ else
45
+ self
46
+ end
47
+ end
48
+ end
49
+
50
+ class ::Array
51
+ def join_by_sp
52
+ join(' ')
53
+ end
54
+ end
@@ -0,0 +1,3 @@
1
+ module Qlang
2
+ VERSION = '0.0.2700000'
3
+ end
data/lib/qlang.rb ADDED
@@ -0,0 +1,37 @@
1
+ # Ruby stlib
2
+ require 'kconv'
3
+ require 'matrix'
4
+ require 'singleton'
5
+ require 'yaml'
6
+
7
+ $:.unshift(File.dirname(__FILE__))
8
+ # Q core
9
+ require 'qlang/meta_info'
10
+ require 'qlang/utils/ruby_ext'
11
+ require 'qlang/lexer'
12
+ require 'qlang/parser'
13
+
14
+ module Qlang
15
+ $meta_info = MetaInfo.instance
16
+
17
+ class << self
18
+
19
+ def compile(str)
20
+ lexed = Lexer.execute(str)
21
+ Kconv.tosjis(Parser.execute(lexed))
22
+ end
23
+
24
+ $meta_info.langs_hash.keys.each do |lang_name|
25
+ define_method("to_#{lang_name}") do |*opts|
26
+ $meta_info.lang = lang_name.to_sym
27
+ $meta_info.opts = opts
28
+ Qlang
29
+ end
30
+ end
31
+
32
+ end
33
+
34
+ end
35
+
36
+ # Make alias as Q
37
+ Q = Qlang
data/qlang.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'qlang/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'hilbert'
8
+ spec.version = Qlang::VERSION
9
+ spec.authors = ['gogotanaka']
10
+ spec.email = ['mail@tanakakazuki.com']
11
+ spec.extensions = ['ext/qlang/extconf.rb']
12
+ spec.summary = %q{Enjoy MATH!}
13
+ spec.description = %q{Enjoy MATH!}
14
+ spec.homepage = 'http://q-language.org/'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0")
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_dependency 'dydx', '~> 0.1.41421'
23
+
24
+ spec.add_development_dependency 'bundler'
25
+ spec.add_development_dependency 'rake'
26
+ spec.add_development_dependency 'rake-compiler'
27
+ spec.add_development_dependency 'minitest'
28
+ end
@@ -0,0 +1,35 @@
1
+ require 'minitest_helper'
2
+
3
+ class TestTokens < MiniTest::Unit::TestCase
4
+ include Qlang::Lexer::Tokens
5
+ def setup
6
+ end
7
+
8
+ def full_match(rgx, str)
9
+ assert_equal(0, rgx =~ str)
10
+ assert_equal(str, $&)
11
+ end
12
+
13
+ def not_match(rgx, str)
14
+ assert_equal(nil, rgx =~ str)
15
+ end
16
+
17
+ def test_nums
18
+ full_match(NUM, '1')
19
+ full_match(NUM, '234987423')
20
+ full_match(NUM, '23423948.298743')
21
+ full_match(NUM, 'e')
22
+ full_match(NUM, 'pi')
23
+ not_match(NUM, 'a')
24
+ end
25
+
26
+ def test_function
27
+ full_match(/[fgh]\(\w( ?, ?\w)*\) ?= ?[^\r\n]+/, 'f(x) = xy')
28
+ end
29
+
30
+ def test_differentiate
31
+ rgx = /d\/d[a-zA-Z] .*/
32
+ full_match(rgx, 'd/dx sin(x)')
33
+ full_match(rgx, 'd/dz z^2')
34
+ end
35
+ end
@@ -0,0 +1,17 @@
1
+ require 'qlang/iq'
2
+
3
+ class TestInterpreterBase < MiniTest::Unit::TestCase
4
+ # TODO: opposite
5
+ def assert_iq_equal(input, output)
6
+ assert_equal(Qlang::Iq.execute(input), output)
7
+ end
8
+
9
+ def assert_def_func(input, output)
10
+ assert_equal(Qlang::Iq.execute(input), output)
11
+ end
12
+
13
+ def assert_cal_func(input, output)
14
+ assert_equal(Qlang::Iq.execute(input), output)
15
+ reset
16
+ end
17
+ end
@@ -0,0 +1,43 @@
1
+ require 'minitest_helper'
2
+
3
+ class TestDifferential < TestInterpreterBase
4
+ def setup
5
+ end
6
+
7
+ def test_general
8
+ assert_iq_equal(
9
+ 'd/dx(e ** x)',
10
+ 'e ^ x'
11
+ )
12
+
13
+ assert_iq_equal(
14
+ 'd/dx(x ** 2)',
15
+ '2x'
16
+ )
17
+
18
+ assert_iq_equal(
19
+ 'd/dx(x * 2)',
20
+ '2'
21
+ )
22
+
23
+ assert_iq_equal(
24
+ 'd/dx( sin(x) )',
25
+ 'cos( x )'
26
+ )
27
+
28
+ assert_iq_equal(
29
+ 'd/dx(log( x ))',
30
+ '1 / x'
31
+ )
32
+
33
+ assert_iq_equal(
34
+ 'd/dx cos(x)',
35
+ '- sin( x )'
36
+ )
37
+
38
+ assert_iq_equal(
39
+ 'd/dx xx',
40
+ '2x'
41
+ )
42
+ end
43
+ end
@@ -0,0 +1,44 @@
1
+ require 'minitest_helper'
2
+
3
+ class TestFunction < TestInterpreterBase
4
+ def setup
5
+ end
6
+
7
+ def test_general
8
+ assert_def_func('f(x, y) = x + y', 'x + y')
9
+ assert_cal_func('f( 4, 5 )', '9.0')
10
+
11
+ assert_def_func('f( x , y) = xy', 'x * y')
12
+ assert_cal_func('f( 3, 9 )', '27.0')
13
+
14
+ assert_def_func('f(x, y) = xy^2', 'x * ( y ** 2 )')
15
+ assert_cal_func('f( 3, 2 )', '12.0')
16
+
17
+ assert_def_func('f(x, y) = xy^2', 'x * ( y ** 2 )')
18
+ assert_cal_func('df/dx', 'y ^ 2')
19
+
20
+ assert_def_func('g(x) = x ^ 2', 'x ** 2')
21
+ assert_cal_func('g(2)', '4.0')
22
+
23
+ assert_def_func('h(x) = e ^ 2', 'e ** 2')
24
+ assert_cal_func('h(2)', '7.3890560989306495')
25
+
26
+ assert_def_func('h(x) = pix', 'pi * x')
27
+ assert_cal_func('h(3)', '9.42477796076938')
28
+
29
+ assert_def_func('h(x) = pie', 'pi * e')
30
+ assert_cal_func('h(2)', '8.539734222673566')
31
+
32
+ assert_def_func('h(x) = ( 1 / ( 2pi ) ^ ( 1 / 2.0 ) ) * e ^ ( - x ^ 2 / 2 )', '( ( 4503599627370496 / 6369051672525773 ) / ( pi ** 0.5 ) ) * ( e ** ( ( - ( x ** 2 ) ) / 2 ) )')
33
+ assert_cal_func('S( h(x)dx )[-oo..oo]', '1.0')
34
+
35
+ assert_def_func('f(x) = sin(x)', 'sin( x )')
36
+ assert_cal_func('f(pi)', '0.0')
37
+
38
+ assert_def_func('f(x) = cos(x)', 'cos( x )')
39
+ assert_cal_func('f(pi)', '-1.0')
40
+
41
+ assert_def_func('f(x) = log(x)', 'log( x )')
42
+ assert_cal_func('f(e)', '1.0')
43
+ end
44
+ end
@@ -0,0 +1,12 @@
1
+ require 'minitest_helper'
2
+
3
+ class TestGeneral < TestInterpreterBase
4
+ def setup
5
+ end
6
+
7
+ def test_general
8
+ assert_iq_equal('2x', '2x')
9
+ assert_iq_equal('x + x', '2x')
10
+ assert_iq_equal('x * y', 'xy')
11
+ end
12
+ end
@@ -0,0 +1,38 @@
1
+ require 'minitest_helper'
2
+
3
+ class TestIntegral < TestInterpreterBase
4
+ def setup
5
+ end
6
+
7
+ def test_general
8
+ assert_iq_equal(
9
+ 'S( log(x)dx )[0..1]',
10
+ '-oo'
11
+ )
12
+
13
+ assert_iq_equal(
14
+ 'S( sin(x)dx )[0..pi]',
15
+ '2.0'
16
+ )
17
+
18
+ assert_iq_equal(
19
+ 'S( cos(x)dx )[0..pi]',
20
+ '0.0'
21
+ )
22
+
23
+ assert_iq_equal(
24
+ 'S( cos(x)dx )[0..pi]',
25
+ '0.0'
26
+ )
27
+
28
+ assert_iq_equal(
29
+ 'S(2pi dx)[0..1]',
30
+ '6.28318531'
31
+ )
32
+
33
+ assert_iq_equal(
34
+ 'S(xx dx)[0..1]',
35
+ '0.33333333'
36
+ )
37
+ end
38
+ end
@@ -0,0 +1,37 @@
1
+ require 'minitest_helper'
2
+
3
+ class TestLimit < TestInterpreterBase
4
+ def setup
5
+ end
6
+
7
+ def assert_iq_equal(output, input)
8
+ assert_equal(Qlang::Iq.execute(input), output)
9
+ end
10
+
11
+ def test_general
12
+ assert_iq_equal(
13
+ 'oo',
14
+ 'lim[x->0] 1/x'
15
+ )
16
+
17
+ assert_iq_equal(
18
+ '10.0',
19
+ 'lim[x->10] x'
20
+ )
21
+
22
+ # assert_iq_equal(
23
+ # '2.7182682371744895',
24
+ # 'lim[x->oo] (1 + 1/x)^x'
25
+ # )
26
+
27
+ assert_iq_equal(
28
+ 'oo',
29
+ 'lim[x->oo] x'
30
+ )
31
+
32
+ assert_iq_equal(
33
+ '0.0',
34
+ 'lim[x->0] x'
35
+ )
36
+ end
37
+ end
@@ -0,0 +1,70 @@
1
+ require 'minitest_helper'
2
+
3
+ class TestMatrix < TestInterpreterBase
4
+ def setup
5
+ end
6
+
7
+ def test_integer
8
+ assert_iq_equal(
9
+ '(1 2 3; 4 5 6)',
10
+ '(1 2 3; 4 5 6)'
11
+ )
12
+
13
+ assert_iq_equal(
14
+ '(1 2 3; 4 5 6) + (1 2 3; 4 5 6)',
15
+ '(2 4 6; 8 10 12)'
16
+ )
17
+
18
+ assert_iq_equal(
19
+ '(1 2 3; 4 5 6) - (2 4 1; 8 3 9)',
20
+ '(-1 -2 2; -4 2 -3)'
21
+ )
22
+
23
+ assert_iq_equal(
24
+ '(1 2; 3 4) * (1 2; 3 4)',
25
+ '(7 10; 15 22)'
26
+ )
27
+
28
+ assert_iq_equal(
29
+ '(1 2; 3 4) ** 2',
30
+ '(7 10; 15 22)'
31
+ )
32
+
33
+ assert_iq_equal(
34
+ '(1 2; 3 4) ** 2',
35
+ '(7 10; 15 22)'
36
+ )
37
+
38
+ assert_iq_equal(
39
+ '(1 2; 3 4) * (1 2)',
40
+ '(5 11)'
41
+ )
42
+
43
+ assert_iq_equal(
44
+ '(1 2 3; 4 5 6)t',
45
+ '(1 4; 2 5; 3 6)'
46
+ )
47
+
48
+ assert_iq_equal(
49
+ '(1 2 3
50
+ 4 5 6) +
51
+ (1 2 3
52
+ 4 5 6)',
53
+ '(2 4 6; 8 10 12)'
54
+ )
55
+ end
56
+
57
+ def test_float
58
+ assert_iq_equal(
59
+ '(1.0 2.0 3
60
+ 4.2 5.3 6)t',
61
+ '(1.0 4.2; 2.0 5.3; 3 6)'
62
+ )
63
+
64
+ assert_iq_equal(
65
+ '(1.0 2.0 3
66
+ 4.2 5.3 6)t',
67
+ '(1.0 4.2; 2.0 5.3; 3 6)'
68
+ )
69
+ end
70
+ end
@@ -0,0 +1,25 @@
1
+ #!/bin/env ruby
2
+ # encoding: utf-8
3
+ require 'minitest_helper'
4
+
5
+ class TestSigma < TestInterpreterBase
6
+ def setup
7
+ end
8
+
9
+ def test_general
10
+ assert_iq_equal(
11
+ '∑[x=0,10] x',
12
+ '55.0'
13
+ )
14
+
15
+ assert_iq_equal(
16
+ '∑[x=0, 10] x^2',
17
+ '385.0'
18
+ )
19
+
20
+ assert_iq_equal(
21
+ '∑[x=0, 10] x^3',
22
+ '3025.0'
23
+ )
24
+ end
25
+ end
@@ -0,0 +1,28 @@
1
+ require 'minitest_helper'
2
+
3
+ class TestVector < TestInterpreterBase
4
+ def setup
5
+ end
6
+
7
+ def test_integer
8
+ assert_iq_equal(
9
+ '(1 2 3)',
10
+ '(1 2 3)'
11
+ )
12
+
13
+ assert_iq_equal(
14
+ '(1 2 3) + (1 2 3)',
15
+ '(2 4 6)'
16
+ )
17
+
18
+ assert_iq_equal(
19
+ '(1 2 3 ) + ( 1 2 3 )',
20
+ '(2 4 6)'
21
+ )
22
+
23
+ assert_iq_equal(
24
+ '(1 2 3) - (1 2 3) - (1 2 3)',
25
+ '(-1 -2 -3)'
26
+ )
27
+ end
28
+ end
@@ -0,0 +1,32 @@
1
+ require 'minitest_helper'
2
+
3
+ class TestR < TestInterpreterBase
4
+ def setup
5
+ end
6
+
7
+ def assert_r_compl_eq(output, input)
8
+ assert_equal(output, Q.to_r.compile(input))
9
+ end
10
+
11
+ def test_function
12
+ assert_r_compl_eq(
13
+ "f <- function(x, y) x + y",
14
+ 'f(x, y) = x + y'
15
+ )
16
+
17
+ assert_r_compl_eq(
18
+ "g <- function(x) x ^ 2",
19
+ 'g(x) = x ^ 2'
20
+ )
21
+
22
+ assert_r_compl_eq(
23
+ "g <- function(x) x ^ (2 + 2)",
24
+ 'g(x) = x ^ (2 + 2)'
25
+ )
26
+
27
+ assert_r_compl_eq(
28
+ "h <- function(a, b, c) a ^ 2 + b ^ 2 + c ^ 2",
29
+ 'h(a, b, c) = a ^ 2 + b ^ 2 + c ^ 2'
30
+ )
31
+ end
32
+ end
@@ -0,0 +1,9 @@
1
+ require 'minitest_helper'
2
+
3
+ class TestRuby < TestInterpreterBase
4
+ def setup
5
+ end
6
+
7
+ def test_integer
8
+ end
9
+ end
@@ -0,0 +1,8 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'qlang'
3
+
4
+ require 'minitest/autorun'
5
+ require 'pp'
6
+ require 'pry'
7
+
8
+ require 'interpreter/base'
@@ -0,0 +1,13 @@
1
+ require 'minitest_helper'
2
+ require 'qlang/qlang'
3
+
4
+ class TestQMatrix < TestInterpreterBase
5
+ def setup
6
+ end
7
+
8
+ def test_main
9
+ # assert_equal(50.0, QMatrix.new.func(10))
10
+
11
+ assert_equal(8.0, QMatrix.new.execute(0, 2, 100))
12
+ end
13
+ end
@@ -0,0 +1,24 @@
1
+ require 'minitest_helper'
2
+
3
+ class TestQlang < MiniTest::Unit::TestCase
4
+ def setup
5
+ end
6
+
7
+ def assert_to_ruby(input, output)
8
+ assert_equal(Q.to_ruby.compile(input), output)
9
+ end
10
+
11
+ def test_basis
12
+ refute_nil ::Qlang::VERSION
13
+ assert_equal(Qlang, Q)
14
+ end
15
+
16
+ def test_demo_code
17
+ assert_to_ruby('d/dx(sin(x))', 'd/dx(sin(x))')
18
+ assert_to_ruby('d/dx(log(x))', 'd/dx(log(x))')
19
+ assert_to_ruby('f(x, y) = x + y', 'f(x, y) <= x + y')
20
+ assert_equal(Matrix[[1, 2, 3], [4, 5, 6]].to_q, '(1 2 3; 4 5 6)')
21
+ assert_equal(Vector[1, 2, 3].to_q, '(1 2 3)')
22
+ end
23
+ end
24
+