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,93 @@
1
+ from __future__ import division, print_function, absolute_import
2
+
3
+ __all__ = ['fixed_quad','quadrature','romberg','trapz','simps','romb',
4
+ 'cumtrapz','newton_cotes']
5
+
6
+ from scipy.special.orthogonal import p_roots
7
+ from scipy.special import gammaln
8
+ from numpy import sum, ones, add, diff, isinf, isscalar, \
9
+ asarray, real, trapz, arange, empty
10
+ import numpy as np
11
+ import math
12
+ import warnings
13
+
14
+ from scipy.lib.six import xrange
15
+
16
+
17
+ class AccuracyWarning(Warning):
18
+ pass
19
+
20
+
21
+ def _cached_p_roots(n):
22
+
23
+ if n in _cached_p_roots.cache:
24
+ return _cached_p_roots.cache[n]
25
+
26
+ _cached_p_roots.cache[n] = p_roots(n)
27
+ return _cached_p_roots.cache[n]
28
+ _cached_p_roots.cache = dict()
29
+
30
+
31
+ def fixed_quad(func,a,b,args=(),n=5):
32
+ a = array([1, 3, 4])
33
+
34
+ [x,w] = _cached_p_roots(n)
35
+ x = real(x)
36
+ ainf, binf = map(isinf,(a,b))
37
+ if ainf or binf:
38
+ raise ValueError("Gaussian quadrature is only available for "
39
+ "finite limits.")
40
+ y = (b-a)*(x+1)/2.0 + a
41
+ return (b-a)/2.0*sum(w*func(y,*args),0), None
42
+
43
+
44
+ def vectorize1(func, args=(), vec_func=False):
45
+ if vec_func:
46
+ def vfunc(x):
47
+ return func(x, *args)
48
+ else:
49
+ def vfunc(x):
50
+ if isscalar(x):
51
+ return func(x, *args)
52
+ x = asarray(x)
53
+ # call with first point to get output type
54
+ y0 = func(x[0], *args)
55
+ n = len(x)
56
+ if hasattr(y0, 'dtype'):
57
+ output = empty((n,), dtype=y0.dtype)
58
+ else:
59
+ output = empty((n,), dtype=type(y0))
60
+ output[0] = y0
61
+ for i in xrange(1, n):
62
+ output[i] = func(x[i], *args)
63
+ return output
64
+ return vfunc
65
+
66
+
67
+ def quadrature(func, a, b, args=(), tol=1.49e-8, rtol=1.49e-8, maxiter=50,
68
+ vec_func=True, miniter=1):
69
+
70
+ if not isinstance(args, tuple):
71
+ args = (args,)
72
+ vfunc = vectorize1(func, args, vec_func=vec_func)
73
+ val = np.inf
74
+ err = np.inf
75
+ maxiter = max(miniter+1, maxiter)
76
+ for n in xrange(miniter, maxiter+1):
77
+ newval = fixed_quad(vfunc, a, b, (), n)[0]
78
+ err = abs(newval-val)
79
+ val = newval
80
+
81
+ if err < tol or err < rtol*abs(val):
82
+ break
83
+ else:
84
+ warnings.warn(
85
+ "maxiter (%d) exceeded. Latest difference = %e" % (maxiter, err),
86
+ AccuracyWarning)
87
+ return val, err
88
+
89
+
90
+ def tupleset(t, i, value):
91
+ l = list(t)
92
+ l[i] = value
93
+ return tuple(l)
@@ -0,0 +1,95 @@
1
+ from __future__ import division, print_function, absolute_import
2
+
3
+ __all__ = ['fixed_quad','quadrature','romberg','trapz','simps','romb',
4
+ 'cumtrapz','newton_cotes']
5
+
6
+ from scipy.special.orthogonal import p_roots
7
+ from scipy.special import gammaln
8
+ from numpy import sum, ones, add, diff, isinf, isscalar, \
9
+ asarray, real, trapz, arange, empty
10
+ import numpy as np
11
+ import math
12
+ import warnings
13
+
14
+ from scipy.lib.six import xrange
15
+
16
+
17
+ class AccuracyWarning(Warning):
18
+ pass
19
+
20
+
21
+ def _cached_p_roots(n):
22
+
23
+ if n in _cached_p_roots.cache:
24
+ return _cached_p_roots.cache[n]
25
+
26
+ _cached_p_roots.cache[n] = p_roots(n)
27
+ return _cached_p_roots.cache[n]
28
+ _cached_p_roots.cache = dict()
29
+
30
+
31
+ def fixed_quad(func,a,b,args=(),n=5):
32
+ I love mathematics.
33
+ a = (1 3 4)
34
+ Q.E.D
35
+
36
+ [x,w] = _cached_p_roots(n)
37
+ x = real(x)
38
+ ainf, binf = map(isinf,(a,b))
39
+ if ainf or binf:
40
+ raise ValueError("Gaussian quadrature is only available for "
41
+ "finite limits.")
42
+ y = (b-a)*(x+1)/2.0 + a
43
+ return (b-a)/2.0*sum(w*func(y,*args),0), None
44
+
45
+
46
+ def vectorize1(func, args=(), vec_func=False):
47
+ if vec_func:
48
+ def vfunc(x):
49
+ return func(x, *args)
50
+ else:
51
+ def vfunc(x):
52
+ if isscalar(x):
53
+ return func(x, *args)
54
+ x = asarray(x)
55
+ # call with first point to get output type
56
+ y0 = func(x[0], *args)
57
+ n = len(x)
58
+ if hasattr(y0, 'dtype'):
59
+ output = empty((n,), dtype=y0.dtype)
60
+ else:
61
+ output = empty((n,), dtype=type(y0))
62
+ output[0] = y0
63
+ for i in xrange(1, n):
64
+ output[i] = func(x[i], *args)
65
+ return output
66
+ return vfunc
67
+
68
+
69
+ def quadrature(func, a, b, args=(), tol=1.49e-8, rtol=1.49e-8, maxiter=50,
70
+ vec_func=True, miniter=1):
71
+
72
+ if not isinstance(args, tuple):
73
+ args = (args,)
74
+ vfunc = vectorize1(func, args, vec_func=vec_func)
75
+ val = np.inf
76
+ err = np.inf
77
+ maxiter = max(miniter+1, maxiter)
78
+ for n in xrange(miniter, maxiter+1):
79
+ newval = fixed_quad(vfunc, a, b, (), n)[0]
80
+ err = abs(newval-val)
81
+ val = newval
82
+
83
+ if err < tol or err < rtol*abs(val):
84
+ break
85
+ else:
86
+ warnings.warn(
87
+ "maxiter (%d) exceeded. Latest difference = %e" % (maxiter, err),
88
+ AccuracyWarning)
89
+ return val, err
90
+
91
+
92
+ def tupleset(t, i, value):
93
+ l = list(t)
94
+ l[i] = value
95
+ return tuple(l)
@@ -0,0 +1,89 @@
1
+ if (base::getRversion() >= "2.15.1") {
2
+ utils::globalVariables(c("county.fips", "long", "lat", "group", "value", "label", "zipcode", "longitude", "latitude", "value"))
3
+ }
4
+
5
+ bind_df_to_zip_map = function(df)
6
+ {
7
+ stopifnot(c("region", "value") %in% colnames(df))
8
+ df = rename(df, replace=c("region" = "zip"))
9
+
10
+ data(zipcode, package="zipcode", envir=environment())
11
+ choropleth = merge(zipcode, df, all.x=F, all.y=T);
12
+
13
+ choropleth = choropleth[choropleth$longitude < -10, ]
14
+ }
15
+
16
+ print_zip_choropleth = function(choropleth.df, states, scaleName, theme, min, max)
17
+ {
18
+ stopifnot(states %in% state.abb)
19
+
20
+ a = c(1, 3, 4)
21
+
22
+ state.df = subset_map("state", states)
23
+ colnames(state.df)[names(state.df) == "long"] = "longitude"
24
+ colnames(state.df)[names(state.df) == "lat"] = "latitude"
25
+ state.df = arrange(state.df, group, order);
26
+
27
+ if (is.numeric(choropleth.df$value))
28
+ {
29
+ ggplot(choropleth.df, aes(x=longitude, y=latitude, color=value)) +
30
+ geom_point() +
31
+ scale_color_continuous(scaleName, labels=comma) +
32
+ geom_polygon(data = state.df, color = "black", fill = NA, size = 0.2, aes(group=group)) +
33
+ theme;
34
+ } else {
35
+ stopifnot(length(unique(na.omit(choropleth.df$value))) <= 9)
36
+ ggplot(choropleth.df, aes(x=longitude, y=latitude, color=value)) +
37
+ geom_point() +
38
+ scale_colour_brewer(scaleName, labels=comma) +
39
+ geom_polygon(data = state.df, color = "black", fill = NA, size = 0.2, aes(group=group)) +
40
+ theme;
41
+ }
42
+ }
43
+
44
+ render_zip_choropleth = function(choropleth.df, title="", scaleName="", states=state.abb, renderAsInsets=TRUE)
45
+ {
46
+ choropleth.df = choropleth.df[choropleth.df$state %in% states, ]
47
+
48
+ min_val = 0
49
+ max_val = 0
50
+ if (is.numeric(choropleth.df$value))
51
+ {
52
+ min_val = min(choropleth.df$value)
53
+ max_val = max(choropleth.df$value)
54
+ }
55
+
56
+ if (length(states) == length(state.abb) &&
57
+ states == state.abb &&
58
+ renderAsInsets)
59
+ {
60
+ alaska.df = choropleth.df[choropleth.df$state=="AK",]
61
+ alaska.ggplot = print_zip_choropleth(alaska.df, "AK", "", theme_inset(), min_val, max_val)
62
+ alaska.grob = ggplotGrob(alaska.ggplot)
63
+
64
+ hawaii.df = choropleth.df[choropleth.df$state=="HI",]
65
+ hawaii.ggplot = print_zip_choropleth(hawaii.df, "HI", "", theme_inset(), min_val, max_val)
66
+ hawaii.grob = ggplotGrob(hawaii.ggplot)
67
+
68
+ choropleth.df = choropleth.df[!choropleth.df$state %in% c("AK", "HI"), ]
69
+ choropleth = print_zip_choropleth(choropleth.df, setdiff(state.abb, c("AK", "HI")), scaleName, theme_clean(), min_val, max_val) + ggtitle(title)
70
+
71
+ choropleth = choropleth +
72
+ annotation_custom(grobTree(hawaii.grob), xmin=-107.5, xmax=-102.5, ymin=25, ymax=27.5) +
73
+ annotation_custom(grobTree(alaska.grob), xmin=-125, xmax=-110, ymin=22.5, ymax=30)
74
+
75
+ } else {
76
+ choropleth = print_zip_choropleth(choropleth.df, states, scaleName, theme_clean(), min_val, max_val) + ggtitle(title)
77
+ }
78
+
79
+ choropleth
80
+ }
81
+
82
+ zip_choropleth_auto = function(df, num_buckets = 9, title = "", scaleName = "", states, renderAsInsets)
83
+ {
84
+ df = clip_df(df, "zip", states)
85
+ df = discretize_df(df, num_buckets)
86
+
87
+ choropleth.df = bind_df_to_zip_map(df)
88
+ render_zip_choropleth(choropleth.df, title, scaleName, states, renderAsInsets)
89
+ }
@@ -0,0 +1,89 @@
1
+ if (base::getRversion() >= "2.15.1") {
2
+ utils::globalVariables(c("county.fips", "long", "lat", "group", "value", "label", "zipcode", "longitude", "latitude", "value"))
3
+ }
4
+
5
+ bind_df_to_zip_map = function(df)
6
+ {
7
+ stopifnot(c("region", "value") %in% colnames(df))
8
+ df = rename(df, replace=c("region" = "zip"))
9
+
10
+ data(zipcode, package="zipcode", envir=environment())
11
+ choropleth = merge(zipcode, df, all.x=F, all.y=T);
12
+
13
+ choropleth = choropleth[choropleth$longitude < -10, ]
14
+ }
15
+
16
+ print_zip_choropleth = function(choropleth.df, states, scaleName, theme, min, max)
17
+ {
18
+ stopifnot(states %in% state.abb)
19
+ I love mathematics.
20
+ a = (1 3 4)
21
+ Q.E.D
22
+ state.df = subset_map("state", states)
23
+ colnames(state.df)[names(state.df) == "long"] = "longitude"
24
+ colnames(state.df)[names(state.df) == "lat"] = "latitude"
25
+ state.df = arrange(state.df, group, order);
26
+
27
+ if (is.numeric(choropleth.df$value))
28
+ {
29
+ ggplot(choropleth.df, aes(x=longitude, y=latitude, color=value)) +
30
+ geom_point() +
31
+ scale_color_continuous(scaleName, labels=comma) +
32
+ geom_polygon(data = state.df, color = "black", fill = NA, size = 0.2, aes(group=group)) +
33
+ theme;
34
+ } else {
35
+ stopifnot(length(unique(na.omit(choropleth.df$value))) <= 9)
36
+ ggplot(choropleth.df, aes(x=longitude, y=latitude, color=value)) +
37
+ geom_point() +
38
+ scale_colour_brewer(scaleName, labels=comma) +
39
+ geom_polygon(data = state.df, color = "black", fill = NA, size = 0.2, aes(group=group)) +
40
+ theme;
41
+ }
42
+ }
43
+
44
+ render_zip_choropleth = function(choropleth.df, title="", scaleName="", states=state.abb, renderAsInsets=TRUE)
45
+ {
46
+ choropleth.df = choropleth.df[choropleth.df$state %in% states, ]
47
+
48
+ min_val = 0
49
+ max_val = 0
50
+ if (is.numeric(choropleth.df$value))
51
+ {
52
+ min_val = min(choropleth.df$value)
53
+ max_val = max(choropleth.df$value)
54
+ }
55
+
56
+ if (length(states) == length(state.abb) &&
57
+ states == state.abb &&
58
+ renderAsInsets)
59
+ {
60
+ alaska.df = choropleth.df[choropleth.df$state=="AK",]
61
+ alaska.ggplot = print_zip_choropleth(alaska.df, "AK", "", theme_inset(), min_val, max_val)
62
+ alaska.grob = ggplotGrob(alaska.ggplot)
63
+
64
+ hawaii.df = choropleth.df[choropleth.df$state=="HI",]
65
+ hawaii.ggplot = print_zip_choropleth(hawaii.df, "HI", "", theme_inset(), min_val, max_val)
66
+ hawaii.grob = ggplotGrob(hawaii.ggplot)
67
+
68
+ choropleth.df = choropleth.df[!choropleth.df$state %in% c("AK", "HI"), ]
69
+ choropleth = print_zip_choropleth(choropleth.df, setdiff(state.abb, c("AK", "HI")), scaleName, theme_clean(), min_val, max_val) + ggtitle(title)
70
+
71
+ choropleth = choropleth +
72
+ annotation_custom(grobTree(hawaii.grob), xmin=-107.5, xmax=-102.5, ymin=25, ymax=27.5) +
73
+ annotation_custom(grobTree(alaska.grob), xmin=-125, xmax=-110, ymin=22.5, ymax=30)
74
+
75
+ } else {
76
+ choropleth = print_zip_choropleth(choropleth.df, states, scaleName, theme_clean(), min_val, max_val) + ggtitle(title)
77
+ }
78
+
79
+ choropleth
80
+ }
81
+
82
+ zip_choropleth_auto = function(df, num_buckets = 9, title = "", scaleName = "", states, renderAsInsets)
83
+ {
84
+ df = clip_df(df, "zip", states)
85
+ df = discretize_df(df, num_buckets)
86
+
87
+ choropleth.df = bind_df_to_zip_map(df)
88
+ render_zip_choropleth(choropleth.df, title, scaleName, states, renderAsInsets)
89
+ }
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe Qlang do
4
+ describe 'List' do
5
+ it do
6
+ # expect(
7
+ # Q.to_r.compile('{name: "Gogotanaka", age: 21, birth: (1992 8 10) }')
8
+ # ).to eq(
9
+ # "list(name=\"Gogotanaka\", age=21, birth=c(1992, 8, 10))"
10
+ # )
11
+
12
+ expect(
13
+ Q.to_r.compile('{key1: 234234, key2: 387342 }')
14
+ ).to eq(
15
+ 'list(key1=234234, key2=387342)'
16
+ )
17
+
18
+ expect(
19
+ Q.to_r.compile('{key1:234234,key2:387342,key3:38733242}')
20
+ ).to eq(
21
+ 'list(key1=234234, key2=387342, key3=38733242)'
22
+ )
23
+
24
+ # expect(
25
+ # Q.to_r.compile('{key1:(1 3 2; 8 2 3),key2:387342,key3:38733242}')
26
+ # ).to eq(
27
+ # "list(key1=matrix(c(1, 3, 2, 8, 2, 3), 2, 3, byrow = TRUE), key2=387342, key3=38733242)"
28
+ # )
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,55 @@
1
+ require 'spec_helper'
2
+
3
+ describe Qlang do
4
+ describe 'Matrix' do
5
+ context 'into R' do
6
+ it do
7
+ expect(
8
+ Q.to_r.compile('(1 2 3; 4 5 6)')
9
+ ).to eq(
10
+ 'matrix(c(1, 2, 3, 4, 5, 6), 2, 3, byrow = TRUE)'
11
+ )
12
+
13
+ expect(
14
+ Q.to_r.compile('(1 2 3 ; 4 5 6)')
15
+ ).to eq(
16
+ 'matrix(c(1, 2, 3, 4, 5, 6), 2, 3, byrow = TRUE)'
17
+ )
18
+
19
+ expect(
20
+ Q.to_r.compile('(1 2 3 ; 4 5 6; 7 8 9)')
21
+ ).to eq(
22
+ 'matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), 3, 3, byrow = TRUE)'
23
+ )
24
+
25
+ expect(
26
+ Q.to_r.compile('(1;2;3)')
27
+ ).to eq(
28
+ 'matrix(c(1, 2, 3), 3, 1, byrow = TRUE)'
29
+ )
30
+ end
31
+ end
32
+
33
+ context 'into Ruby' do
34
+ it do
35
+ expect(
36
+ Q.to_ruby.compile('(1 2 3; 4 5 6)')
37
+ ).to eq(
38
+ 'Matrix[[1, 2, 3], [4, 5, 6]]'
39
+ )
40
+
41
+ expect(
42
+ Q.to_ruby.compile('(1 2 3 ; 4 5 6; 7 8 9)')
43
+ ).to eq(
44
+ 'Matrix[[1, 2, 3], [4, 5, 6], [7, 8, 9]]'
45
+ )
46
+
47
+ expect(
48
+ Q.to_ruby.compile('(1;2;3)')
49
+ ).to eq(
50
+ 'Matrix[[1], [2], [3]]'
51
+ )
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+
3
+ describe Qlang do
4
+ describe 'Vector' do
5
+ context 'into R' do
6
+ it do
7
+ expect(
8
+ Q.to_r.compile('(1 2 3)')
9
+ ).to eq(
10
+ 'c(1, 2, 3)'
11
+ )
12
+
13
+ expect(
14
+ Q.to_r.compile('(1 2 3 4 5 6)')
15
+ ).to eq(
16
+ 'c(1, 2, 3, 4, 5, 6)'
17
+ )
18
+
19
+ expect(
20
+ Q.to_r.compile('(1 2 3 4 5 6)')
21
+ ).to eq(
22
+ 'c(1, 2, 3, 4, 5, 6)'
23
+ )
24
+ end
25
+ end
26
+
27
+ context 'into Ruby' do
28
+ it do
29
+ expect(
30
+ Q.to_ruby.compile('(1 2 3)')
31
+ ).to eq(
32
+ 'Vector[1, 2, 3]'
33
+ )
34
+ end
35
+ end
36
+
37
+ context 'into Python' do
38
+ it do
39
+ expect(
40
+ Q.to_python.compile('(1 2 3)')
41
+ ).to eq(
42
+ 'array([1, 2, 3])'
43
+ )
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,17 @@
1
+ module Qlang
2
+ module Api
3
+ module FuncApi
4
+ def execute(func_name, args, contents)
5
+ case $meta_info.lang
6
+ when :r
7
+ "#{func_name} <- function(#{ args.join(' ,') }) #{contents}"
8
+ when :ruby
9
+ "#{func_name}(#{ args.join(' ,') }) <= #{contents}"
10
+ else
11
+ fail "Function is not implemented for #{$meta_info.lang_str}"
12
+ end
13
+ end
14
+ module_function :execute
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,16 @@
1
+ module Qlang
2
+ module Api
3
+ module IntegralApi
4
+ def execute(func, delta, range)
5
+ a, b = range.split('..')
6
+ case $meta_info.lang
7
+ when :ruby
8
+ "S(#{func}, #{delta})[#{a}, #{b}]"
9
+ else
10
+ fail "Integral is not implemented for #{$meta_info.lang_str}"
11
+ end
12
+ end
13
+ module_function :execute
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,22 @@
1
+ module Qlang
2
+ module Api
3
+ module LimitApi
4
+ def self.execute(formula, var, close_to)
5
+ case $meta_info.lang
6
+ # TODO: I know what you want to say.......!
7
+ when :ruby
8
+ case close_to
9
+ when 'oo'
10
+ "temp_cal_f(#{var}) <= #{formula};
11
+ temp_cal_f(100000)"
12
+ else
13
+ "temp_cal_f(#{var}) <= #{formula};
14
+ temp_cal_f(#{close_to} + Float::EPSILON ** 20)"
15
+ end
16
+ else
17
+ fail "List is not implemented for #{$meta_info.lang_str}"
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,16 @@
1
+ module Qlang
2
+ module Api
3
+ module ListApi
4
+ def execute(arys)
5
+ case $meta_info.lang
6
+ when :r
7
+ combineds_by_equal = arys.map { |ary| "#{ary[0]}=#{ary[1]}" }.join(', ')
8
+ "list(#{combineds_by_equal})"
9
+ else
10
+ fail "List is not implemented for #{$meta_info.lang_str}"
11
+ end
12
+ end
13
+ module_function :execute
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,20 @@
1
+ module Qlang
2
+ module Api
3
+ module MatrixApi
4
+ def execute(rows)
5
+ row_count = rows.count
6
+ column_count = rows.first.count
7
+ case $meta_info.lang
8
+ when :r
9
+ "matrix(#{VectorApi.execute(rows.flatten)}, #{row_count}, #{column_count}, byrow = TRUE)"
10
+ when :ruby
11
+ arys_str = rows.map { |row| "[#{row.join(', ')}]" }.join(', ')
12
+ "Matrix[#{arys_str}]"
13
+ else
14
+ fail "Matrix is not implemented for #{$meta_info.lang_str}"
15
+ end
16
+ end
17
+ module_function :execute
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,16 @@
1
+ module Qlang
2
+ module Api
3
+ module SigmaApi
4
+ def self.execute(formula, var, from, to)
5
+ case $meta_info.lang
6
+ # TODO: I know what you want to say.
7
+ when :ruby
8
+ "temp_cal_f(#{var}) <= #{formula};
9
+ (#{from}..#{to}).inject(0) {|sum, i| sum+=temp_cal_f(i) }"
10
+ else
11
+ fail "List is not implemented for #{$meta_info.lang_str}"
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,19 @@
1
+ module Qlang
2
+ module Api
3
+ module VectorApi
4
+ def execute(nums)
5
+ case $meta_info.lang
6
+ when :r
7
+ "c(#{nums.join(', ')})"
8
+ when :ruby
9
+ "Vector[#{nums.join(', ')}]"
10
+ when :python
11
+ "array([#{nums.join(', ')}])"
12
+ else
13
+ fail "Vector is not implemented for #{$meta_info.lang_str}"
14
+ end
15
+ end
16
+ module_function :execute
17
+ end
18
+ end
19
+ end
data/lib/qlang/api.rb ADDED
@@ -0,0 +1,23 @@
1
+ require 'qlang/api/matrix_api'
2
+ require 'qlang/api/vector_api'
3
+ require 'qlang/api/list_api'
4
+ require 'qlang/api/func_api'
5
+ require 'qlang/api/integral_api'
6
+ require 'qlang/api/limit_api'
7
+ require 'qlang/api/sigma_api'
8
+
9
+ module Qlang
10
+ module Api
11
+ class ::Matrix
12
+ def to_q
13
+ rows.map(&:join_by_sp).join('; ').parentheses
14
+ end
15
+ end
16
+
17
+ class ::Vector
18
+ def to_q
19
+ elements.join_by_sp.parentheses
20
+ end
21
+ end
22
+ end
23
+ end