linmeric 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/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +51 -0
- data/Rakefile +10 -0
- data/bin/help/Help.rb +184 -0
- data/bin/help/Help_inst.rb +244 -0
- data/bin/linguide +23 -0
- data/bin/linmeric +36 -70
- data/doc/Archive.html +352 -0
- data/doc/Calculator/Evaluator.html +477 -0
- data/doc/Calculator/Lexer.html +186 -0
- data/doc/Calculator/Token.html +257 -0
- data/doc/Calculator.html +181 -0
- data/doc/Dim.html +257 -0
- data/doc/Filename.html +246 -0
- data/doc/Fixnum.html +253 -0
- data/doc/Float.html +253 -0
- data/doc/Function.html +784 -0
- data/doc/InputError.html +102 -0
- data/doc/Instructions_en.txt +6 -7
- data/doc/Instructions_it.txt +6 -9
- data/doc/Integrators.html +436 -0
- data/doc/LU.html +435 -0
- data/doc/Lexer.html +261 -0
- data/doc/Linmeric.html +109 -0
- data/doc/Listener.html +605 -0
- data/doc/Matrix.html +1719 -0
- data/doc/MyArgError.html +102 -0
- data/doc/NilClass.html +202 -0
- data/doc/Numeric.html +251 -0
- data/doc/Parser.html +389 -0
- data/doc/PrintError.html +622 -0
- data/doc/README_md.html +142 -0
- data/doc/Scp.html +530 -0
- data/doc/Sizer.html +652 -0
- data/doc/String.html +540 -0
- data/doc/Token.html +341 -0
- data/doc/Tool.html +394 -0
- data/doc/created.rid +18 -0
- data/doc/css/fonts.css +167 -0
- data/doc/css/rdoc.css +590 -0
- data/doc/fonts/Lato-Light.ttf +0 -0
- data/doc/fonts/Lato-LightItalic.ttf +0 -0
- data/doc/fonts/Lato-Regular.ttf +0 -0
- data/doc/fonts/Lato-RegularItalic.ttf +0 -0
- data/doc/fonts/SourceCodePro-Bold.ttf +0 -0
- data/doc/fonts/SourceCodePro-Regular.ttf +0 -0
- data/doc/images/add.png +0 -0
- data/doc/images/arrow_up.png +0 -0
- data/doc/images/brick.png +0 -0
- data/doc/images/brick_link.png +0 -0
- data/doc/images/bug.png +0 -0
- data/doc/images/bullet_black.png +0 -0
- data/doc/images/bullet_toggle_minus.png +0 -0
- data/doc/images/bullet_toggle_plus.png +0 -0
- data/doc/images/date.png +0 -0
- data/doc/images/delete.png +0 -0
- data/doc/images/find.png +0 -0
- data/doc/images/loadingAnimation.gif +0 -0
- data/doc/images/macFFBgHack.png +0 -0
- data/doc/images/package.png +0 -0
- data/doc/images/page_green.png +0 -0
- data/doc/images/page_white_text.png +0 -0
- data/doc/images/page_white_width.png +0 -0
- data/doc/images/plugin.png +0 -0
- data/doc/images/ruby.png +0 -0
- data/doc/images/tag_blue.png +0 -0
- data/doc/images/tag_green.png +0 -0
- data/doc/images/transparent.png +0 -0
- data/doc/images/wrench.png +0 -0
- data/doc/images/wrench_orange.png +0 -0
- data/doc/images/zoom.png +0 -0
- data/doc/index.html +190 -0
- data/doc/js/darkfish.js +161 -0
- data/doc/js/jquery.js +9404 -0
- data/doc/js/navigation.js +142 -0
- data/doc/js/navigation.js.gz +0 -0
- data/doc/js/search.js +109 -0
- data/doc/js/search_index.js +1 -0
- data/doc/js/search_index.js.gz +0 -0
- data/doc/js/searcher.js +228 -0
- data/doc/js/searcher.js.gz +0 -0
- data/doc/table_of_contents.html +834 -0
- data/lib/linmeric/Archive.rb +22 -1
- data/lib/linmeric/Calculator.rb +252 -0
- data/lib/linmeric/CnGal_Matrix_class.rb +130 -49
- data/lib/linmeric/CnGal_new_classes.rb +139 -37
- data/lib/linmeric/CnGal_tools.rb +23 -40
- data/lib/linmeric/Error_print.rb +35 -1
- data/lib/linmeric/Function_class.rb +70 -11
- data/lib/linmeric/Integrators.rb +81 -35
- data/lib/linmeric/LU.rb +26 -5
- data/lib/linmeric/Lexer.rb +19 -1
- data/lib/linmeric/Listener.rb +25 -5
- data/lib/linmeric/Parser.rb +23 -1
- data/lib/linmeric/Scopify.rb +52 -31
- data/lib/linmeric/Sizer.rb +43 -10
- data/lib/linmeric/Token.rb +23 -4
- data/lib/linmeric/version.rb +3 -0
- data/lib/linmeric.rb +10 -8
- data/lib/linmeric_bin.rb +12 -0
- metadata +126 -22
- data/doc/Instructions_en.html +0 -231
- data/doc/Instructions_it.html +0 -231
- data/doc/README_en.html +0 -177
- data/doc/README_en.txt +0 -30
- data/doc/README_it.html +0 -187
- data/doc/README_it.txt +0 -32
|
@@ -2,93 +2,141 @@
|
|
|
2
2
|
|
|
3
3
|
require_relative 'Function_class.rb'
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
#
|
|
7
|
-
|
|
5
|
+
##
|
|
6
|
+
# Overload of Numeric class
|
|
7
|
+
#
|
|
8
|
+
#
|
|
9
|
+
# Author:: Massimiliano Dal Mas (mailto:max.codeware@gmail.com)
|
|
10
|
+
# License:: Distributed under MIT license
|
|
8
11
|
class Numeric
|
|
9
|
-
|
|
12
|
+
|
|
13
|
+
# Compares the value with another object
|
|
14
|
+
#
|
|
15
|
+
# * **argument**: object for the comparison
|
|
16
|
+
# * **returns**: +true+ if the object is a `Numeric`; +false+ else.
|
|
10
17
|
def is_similar?(obj)
|
|
11
18
|
(obj.is_a? Numeric) ? (return true) : (return false)
|
|
12
19
|
end
|
|
13
20
|
|
|
21
|
+
# Checks if the numeric value can be multiplied by a given object
|
|
22
|
+
#
|
|
23
|
+
# * **argument**: object for the checking
|
|
24
|
+
# * **returns**: +true+ if the object is `Numeric` or Matrix; +false+ else.
|
|
14
25
|
def can_multiply?(obj)
|
|
15
26
|
(obj.is_a? Numeric) ? (return true) : ((obj.is_a? Matrix) ? (return true) : (return false))
|
|
16
27
|
end
|
|
17
28
|
|
|
29
|
+
# Checks if the numeric value can be divided by a given object
|
|
30
|
+
#
|
|
31
|
+
# * **argument**: object for the checking
|
|
32
|
+
# * **returns**: +true+ if the object is `Numeric` or Matrix; +false+ else.
|
|
18
33
|
def can_divide?(obj)
|
|
19
34
|
(obj.is_a? Numeric) ? (return true) : ((obj.is_a? Matrix) ? (return true) : (return false))
|
|
20
35
|
end
|
|
21
36
|
|
|
22
|
-
def show()
|
|
23
|
-
puts self
|
|
24
|
-
end
|
|
25
|
-
|
|
26
37
|
end
|
|
27
38
|
|
|
28
|
-
|
|
29
|
-
#
|
|
30
|
-
|
|
39
|
+
##
|
|
40
|
+
# Overload of Fixnum class
|
|
41
|
+
#
|
|
42
|
+
#
|
|
43
|
+
# Author:: Massimiliano Dal Mas (mailto:max.codeware@gmail.com)
|
|
44
|
+
# License:: Distributed under MIT license
|
|
31
45
|
class Fixnum
|
|
46
|
+
|
|
47
|
+
# Compares the value with another object
|
|
48
|
+
#
|
|
49
|
+
# * **argument**: object for the comparison
|
|
50
|
+
# * **returns**: +true+ if the object is a Numeric; +false+ else.
|
|
32
51
|
def similar_to?(obj)
|
|
33
52
|
(obj.is_a? Numeric) ? (return true) : (return false)
|
|
34
53
|
end
|
|
35
54
|
|
|
55
|
+
# Checks if the fixnum value can be multiplied by a given object
|
|
56
|
+
#
|
|
57
|
+
# * **argument**: object for the checking
|
|
58
|
+
# * **returns**: +true+ if the object is Numeric or Matrix; +false+ else.
|
|
36
59
|
def can_multiply?(obj)
|
|
37
60
|
(obj.is_a? Numeric) ? (return true) : ((obj.is_a? Matrix) ? (return true) : (return false))
|
|
38
61
|
end
|
|
39
62
|
|
|
63
|
+
# Checks if the fixnum value can be divided by a given object
|
|
64
|
+
#
|
|
65
|
+
# * **argument**: object for the checking
|
|
66
|
+
# * **returns**: +true+ if the object is Numeric or Matrix; +false+ else.
|
|
40
67
|
def can_divide?(obj)
|
|
41
68
|
(obj.is_a? Numeric) ? (return true) : ((obj.is_a? Matrix) ? (return true) : (return false))
|
|
42
69
|
end
|
|
43
70
|
|
|
44
|
-
def show()
|
|
45
|
-
puts self
|
|
46
|
-
end
|
|
47
71
|
end
|
|
48
72
|
|
|
49
73
|
|
|
50
|
-
|
|
51
|
-
#
|
|
52
|
-
|
|
74
|
+
##
|
|
75
|
+
# Overload of Float class
|
|
76
|
+
#
|
|
77
|
+
#
|
|
78
|
+
# Author:: Massimiliano Dal Mas (mailto:max.codeware@gmail.com)
|
|
79
|
+
# License:: Distributed under MIT license
|
|
53
80
|
class Float
|
|
81
|
+
|
|
82
|
+
# Compares the value with another object
|
|
83
|
+
#
|
|
84
|
+
# * **argument**: object for the comparison
|
|
85
|
+
# * **returns**: +true+ if the object is a Numeric; +false+ else.
|
|
54
86
|
def similar_to?(obj)
|
|
55
87
|
(obj.is_a? Numeric) ? (return true) : (return false)
|
|
56
88
|
end
|
|
57
89
|
|
|
90
|
+
# Checks if the float value can be multiplied by a given object
|
|
91
|
+
#
|
|
92
|
+
# * **argument**: object for the checking
|
|
93
|
+
# * **returns**: +true+ if the object is Numeric or Matrix; +false+ else.
|
|
58
94
|
def can_multiply?(obj)
|
|
59
95
|
(obj.is_a? Numeric) ? (return true) : ((obj.is_a? Matrix) ? (return true) : (return false))
|
|
60
96
|
end
|
|
61
97
|
|
|
98
|
+
# Checks if the float value can be divided by a given object
|
|
99
|
+
#
|
|
100
|
+
# * **argument**: object for the checking
|
|
101
|
+
# * **returns**: +true+ if the object is Numeric or Matrix; +false+ else.
|
|
62
102
|
def can_divide?(obj)
|
|
63
103
|
(obj.is_a? Numeric) ? (return true) : ((obj.is_a? Matrix) ? (return true) : (return false))
|
|
64
104
|
end
|
|
65
|
-
|
|
66
|
-
def show()
|
|
67
|
-
puts self
|
|
68
|
-
end
|
|
69
105
|
|
|
70
106
|
end
|
|
71
107
|
|
|
108
|
+
##
|
|
109
|
+
# Overload of NilClass
|
|
110
|
+
#
|
|
111
|
+
#
|
|
112
|
+
# Author:: Massimiliano Dal Mas (mailto:max.codeware@gmail.com)
|
|
113
|
+
# License:: Distributed under MIT license
|
|
72
114
|
class NilClass
|
|
73
|
-
|
|
74
|
-
|
|
115
|
+
|
|
116
|
+
# * **returns**: string representation of +nil+
|
|
117
|
+
def to_s
|
|
118
|
+
return "nil"
|
|
75
119
|
end
|
|
76
120
|
|
|
121
|
+
# * **returns**: 0
|
|
77
122
|
def size
|
|
78
123
|
return 0
|
|
79
124
|
end
|
|
80
125
|
|
|
81
126
|
end
|
|
82
127
|
|
|
83
|
-
|
|
84
|
-
#
|
|
85
|
-
#
|
|
86
|
-
#
|
|
87
|
-
#
|
|
88
|
-
#
|
|
89
|
-
|
|
128
|
+
##
|
|
129
|
+
# Overload of String class
|
|
130
|
+
#
|
|
131
|
+
#
|
|
132
|
+
# Author:: Massimiliano Dal Mas (mailto:max.codeware@gmail.com)
|
|
133
|
+
# License:: Distributed under MIT license
|
|
90
134
|
class String
|
|
91
135
|
|
|
136
|
+
# Checks if the string contains at least a character of the given one.
|
|
137
|
+
#
|
|
138
|
+
# * **argument**: string for the checking
|
|
139
|
+
# * **returns**: +true+ if a character in common is found; +false+ else.
|
|
92
140
|
def contain?(str)
|
|
93
141
|
raise ArgumentError, '' unless str.is_a? String
|
|
94
142
|
str.each_char do |ch|
|
|
@@ -101,17 +149,26 @@ class String
|
|
|
101
149
|
retry
|
|
102
150
|
end
|
|
103
151
|
|
|
152
|
+
# Checks if the string contains all the characters of the given one.
|
|
153
|
+
#
|
|
154
|
+
# * **argument**: string for the checking
|
|
155
|
+
# * **returns**: +true+ if the string contins all the chars of the argument;
|
|
156
|
+
# +false+ else
|
|
104
157
|
def contain_all?(str)
|
|
105
158
|
raise ArgumentError, '' unless str.is_a? String
|
|
106
159
|
str.each_char do |ch|
|
|
107
160
|
return false if not self.include? ch
|
|
108
161
|
end
|
|
109
162
|
return true
|
|
163
|
+
|
|
110
164
|
rescue ArgumentError
|
|
111
165
|
str = str.to_s
|
|
112
166
|
retry
|
|
113
167
|
end
|
|
114
168
|
|
|
169
|
+
# Deletes spaces from a string
|
|
170
|
+
#
|
|
171
|
+
# * **returns**: new string
|
|
115
172
|
def compact()
|
|
116
173
|
nstr = ''
|
|
117
174
|
self.each_char do |ch|
|
|
@@ -120,6 +177,10 @@ class String
|
|
|
120
177
|
return nstr
|
|
121
178
|
end
|
|
122
179
|
|
|
180
|
+
# Subtracts the substring to the main
|
|
181
|
+
#
|
|
182
|
+
# * **argument**: substring
|
|
183
|
+
# * **returns**: new string
|
|
123
184
|
def -(str)
|
|
124
185
|
myStr = str.to_s
|
|
125
186
|
temp = ''
|
|
@@ -139,34 +200,52 @@ class String
|
|
|
139
200
|
return temp
|
|
140
201
|
end
|
|
141
202
|
|
|
203
|
+
# Checks if the strings represents a `Fixnum` value
|
|
204
|
+
#
|
|
205
|
+
# * **returns**: +true+ if the string represents a `Fixnum`; +false+ else
|
|
142
206
|
def integer?
|
|
143
207
|
[ # In descending order of likeliness:
|
|
144
208
|
/^[-+]?[1-9]([0-9]*)?$/, # decimal
|
|
145
209
|
/^0[0-7]+$/, # octal
|
|
146
210
|
/^0x[0-9A-Fa-f]+$/, # hexadecimal
|
|
147
211
|
/^0b[01]+$/, # binary
|
|
148
|
-
/[0]/
|
|
212
|
+
/[0]/ # zero
|
|
149
213
|
].each do |match_pattern|
|
|
150
214
|
return true if self =~ match_pattern
|
|
151
215
|
end
|
|
152
216
|
return false
|
|
153
217
|
end
|
|
154
218
|
|
|
219
|
+
# Checks if the strings represents a `Float` value
|
|
220
|
+
#
|
|
221
|
+
# * **returns**: +true+ if the string represents a `Float`; +false+ else
|
|
155
222
|
def float?
|
|
156
223
|
pat = /^[-+]?[1-9]([0-9]*)?\.[0-9]+$/
|
|
157
224
|
return true if self=~pat
|
|
158
225
|
return false
|
|
159
226
|
end
|
|
160
227
|
|
|
228
|
+
# Checks if the string represents a number
|
|
229
|
+
#
|
|
230
|
+
# * **returns*: +true+ if the string represents a number; +false+ else
|
|
161
231
|
def number?
|
|
162
232
|
(self.integer? or self.float?) ? (return true) : (return false)
|
|
163
233
|
end
|
|
164
234
|
|
|
235
|
+
# Converts a string to a number
|
|
236
|
+
#
|
|
237
|
+
# * **returns**: `Float` value if the string represents a float
|
|
238
|
+
# `Fixnum` value if the string represents a fixnum; 0 else.
|
|
165
239
|
def to_n
|
|
166
240
|
return self.to_f if self.float?
|
|
167
241
|
return self.to_i if self.integer?
|
|
242
|
+
return 0
|
|
168
243
|
end
|
|
169
244
|
|
|
245
|
+
# Removes a character at the given index
|
|
246
|
+
#
|
|
247
|
+
# * **argument**: index of the character (`Fixnum`)
|
|
248
|
+
# * **returns**: new string
|
|
170
249
|
def remove(index)
|
|
171
250
|
return self if index < 0
|
|
172
251
|
n_str = ''
|
|
@@ -177,11 +256,19 @@ class String
|
|
|
177
256
|
end
|
|
178
257
|
end
|
|
179
258
|
|
|
259
|
+
##
|
|
260
|
+
# Defining a subclass of string to identify a fileneme
|
|
261
|
+
#
|
|
262
|
+
#
|
|
263
|
+
# Author:: Massimiliano Dal Mas (mailto:max.codeware@gmail.com)
|
|
264
|
+
# License:: Distributed under MIT license
|
|
180
265
|
class Filename < String
|
|
181
|
-
|
|
182
|
-
|
|
266
|
+
|
|
267
|
+
def to_s
|
|
268
|
+
return self
|
|
183
269
|
end
|
|
184
270
|
|
|
271
|
+
# * **returns**: name of the file (`String`)
|
|
185
272
|
def name
|
|
186
273
|
i = self.size - 1
|
|
187
274
|
nm = ''
|
|
@@ -192,28 +279,43 @@ class Filename < String
|
|
|
192
279
|
return nm.reverse
|
|
193
280
|
end
|
|
194
281
|
|
|
282
|
+
# * **argument**: class name
|
|
283
|
+
# * **returns**: +true+ if `self.class` = `obj`; +false+ else
|
|
195
284
|
def is_a?(obj)
|
|
196
|
-
self.class == obj
|
|
285
|
+
self.class == obj ? (return true) : (return false)
|
|
197
286
|
end
|
|
198
287
|
|
|
199
288
|
end
|
|
200
289
|
|
|
290
|
+
##
|
|
291
|
+
# Definition of a dimension (structure: from-to)
|
|
292
|
+
#
|
|
293
|
+
#
|
|
294
|
+
# Author:: Massimiliano Dal Mas (mailto:max.codeware@gmail.com)
|
|
295
|
+
# License:: Distributed under MIT license
|
|
201
296
|
class Dim
|
|
297
|
+
|
|
298
|
+
# * **argument**: string to be converted to a dimension
|
|
202
299
|
def initialize(str)
|
|
203
300
|
str = str - '"'
|
|
204
301
|
str = str.split(',')
|
|
205
302
|
if str.size == 2 and str[0] != "" and str[1] != "" then
|
|
206
|
-
|
|
207
|
-
|
|
303
|
+
str.each do |val|
|
|
304
|
+
puts ' Argument Error: wrong range format' unless val.number?
|
|
305
|
+
end
|
|
306
|
+
@sx = str[0].to_n
|
|
307
|
+
@dx = str[1].to_n
|
|
208
308
|
else
|
|
209
|
-
puts ' Argument Error: wrong range format'
|
|
309
|
+
puts ' Argument Error: wrong range format'
|
|
210
310
|
end
|
|
211
311
|
end
|
|
212
312
|
|
|
313
|
+
# * **returns**: left dimension value
|
|
213
314
|
def sx()
|
|
214
315
|
return @sx
|
|
215
316
|
end
|
|
216
317
|
|
|
318
|
+
# * **returns**: right dimension value
|
|
217
319
|
def dx()
|
|
218
320
|
return @dx
|
|
219
321
|
end
|
data/lib/linmeric/CnGal_tools.rb
CHANGED
|
@@ -2,79 +2,62 @@
|
|
|
2
2
|
|
|
3
3
|
require_relative 'CnGal_new_classes.rb'
|
|
4
4
|
|
|
5
|
+
##
|
|
6
|
+
# Provides some useful procedure for linmeric
|
|
7
|
+
#
|
|
8
|
+
#
|
|
9
|
+
# Author:: Massimiliano Dal Mas (mailto:max.codeware@gmail.com)
|
|
10
|
+
# License:: Distributed under MIT license
|
|
5
11
|
module Tool
|
|
12
|
+
|
|
13
|
+
# * **returns**: array of all the keywords used by linmeric
|
|
6
14
|
def self.keys
|
|
7
15
|
return ["mx:","t:", "shw:","shwvar:","det:","solve:","from:","as:","f:","norm:","id_mx:","integ:"]
|
|
8
16
|
end
|
|
9
17
|
|
|
18
|
+
# * **returns**: array of all the operators
|
|
10
19
|
def self.operators
|
|
11
20
|
return ["=","-","+","*","/","^",">"]
|
|
12
21
|
end
|
|
13
22
|
|
|
23
|
+
# * **returns**: alphabet in string format
|
|
14
24
|
def self.letters
|
|
15
25
|
return "abcdefghijklmnopqrstuvwxyz"
|
|
16
26
|
end
|
|
17
27
|
|
|
28
|
+
# **returns**: array of the supported math functions; see: Function
|
|
18
29
|
def self.f
|
|
19
30
|
return ["log","sin","cos","PI","tan","exp"]
|
|
20
31
|
end
|
|
21
32
|
|
|
33
|
+
# Checks if a string represents a keyword
|
|
34
|
+
#
|
|
35
|
+
# * **argument**: object to check
|
|
36
|
+
# * **returns**: +true+ if `ob` represents a keyword; +false+ else.
|
|
22
37
|
def self.is_keyword?(ob)
|
|
23
38
|
(self.keys.include? ob) ? (return true) : (return false)
|
|
24
39
|
end
|
|
25
40
|
|
|
41
|
+
# Dummy function to ckeck if a string represents an expression (may not work properly)
|
|
42
|
+
#
|
|
43
|
+
# * **argument**: string to be checked
|
|
44
|
+
# * **returns**: +true+ if `str` represents an expression; +false+ else.
|
|
26
45
|
def self.is_exp?(str)
|
|
27
46
|
(str.contain? "=+*-/^") ? (return true) : (('0123456789.'.contain_all? str) ?
|
|
28
47
|
(return true):((self.letters.contain? str) ? (return true):(return false)))
|
|
29
48
|
end
|
|
30
49
|
|
|
50
|
+
# Prints the hash of the variables
|
|
51
|
+
#
|
|
52
|
+
# * **argument**: hash to be printed
|
|
31
53
|
def self.print_stack(stack)
|
|
32
54
|
stack.keys.each do |k|
|
|
33
55
|
puts "#{k} = " if stack[k].is_a? Matrix
|
|
34
56
|
print "#{k} = " unless stack[k].is_a? Matrix
|
|
35
|
-
stack[k]
|
|
57
|
+
puts stack[k]
|
|
36
58
|
end
|
|
37
59
|
end
|
|
38
60
|
|
|
39
|
-
def self.convert(string)
|
|
40
|
-
raise ArgumentError, '' unless string.is_a? String
|
|
41
|
-
if string.contain? '*/-+^' then
|
|
42
|
-
case
|
|
43
|
-
when (string.include? '^')
|
|
44
|
-
string = string.split('^')
|
|
45
|
-
string << '**'
|
|
46
|
-
when (string.include? '*')
|
|
47
|
-
string = string.split('*')
|
|
48
|
-
string << '*'
|
|
49
|
-
when (string.include? '/')
|
|
50
|
-
string = string.split('/')
|
|
51
|
-
string << '/'
|
|
52
|
-
when (string.include? '-')
|
|
53
|
-
string = string.split('-')
|
|
54
|
-
string << '-'
|
|
55
|
-
when (string.include? '+')
|
|
56
|
-
string = string.split('+')
|
|
57
|
-
string << '+'
|
|
58
|
-
end
|
|
59
|
-
if string.size == 3 then
|
|
60
|
-
return eval("convert(string[0]) #{string[2]} convert(string[1])")
|
|
61
|
-
elsif string.size == 2 then
|
|
62
|
-
return eval("#{string[2]} convert(string[0])")
|
|
63
|
-
else
|
|
64
|
-
return nil
|
|
65
|
-
end
|
|
66
|
-
elsif '0123456789.'.contain_all? string then
|
|
67
|
-
return string.to_f
|
|
68
|
-
else
|
|
69
|
-
return nil
|
|
70
|
-
end
|
|
71
|
-
|
|
72
|
-
rescue ArgumentError
|
|
73
|
-
string = string.to_s
|
|
74
|
-
retry
|
|
75
|
-
end
|
|
76
|
-
|
|
77
|
-
|
|
78
61
|
end
|
|
79
62
|
|
|
80
63
|
|
data/lib/linmeric/Error_print.rb
CHANGED
|
@@ -1,63 +1,97 @@
|
|
|
1
1
|
#! /usr/bin/env ruby
|
|
2
2
|
|
|
3
|
+
##
|
|
4
|
+
# This modue provides a set of error messages for linmeric
|
|
5
|
+
#
|
|
6
|
+
#
|
|
7
|
+
# Author:: Massimiliano Dal Mas (mailto:max.codeware@gmail.com)
|
|
8
|
+
# License:: Distributed under MIT license
|
|
3
9
|
module PrintError
|
|
10
|
+
|
|
11
|
+
# Main function that prints the error message
|
|
12
|
+
#
|
|
13
|
+
# * **argument**: message to be printed (string)
|
|
14
|
+
# * **argument**: expression (commands) where the error has been found
|
|
15
|
+
# * **argument**: error position
|
|
4
16
|
def self.print(message,expression,pos)
|
|
5
17
|
puts message
|
|
6
18
|
puts expression
|
|
7
19
|
puts " " * pos + "^"
|
|
8
|
-
#puts
|
|
9
20
|
end
|
|
10
21
|
|
|
22
|
+
# * **argument**: unexpected Token
|
|
23
|
+
# * **argument**: expression (commands) where the error has been found
|
|
11
24
|
def self.default(token,expression)
|
|
12
25
|
message = " Sintax Error: unexpected #{token.attribute} token '#{token.me}' found"
|
|
13
26
|
self.print(message,expression,token.position)
|
|
14
27
|
end
|
|
15
28
|
|
|
29
|
+
# * **argument**: unexpected Token
|
|
30
|
+
# * **argument**: expression (commands) where the error has been found
|
|
16
31
|
def self.reduced(token,expression)
|
|
17
32
|
message = " Sintax Error: unexpected #{token.attribute} '#{token.me}' found"
|
|
18
33
|
self.print(message,expression,token.position)
|
|
19
34
|
end
|
|
20
35
|
|
|
36
|
+
# * **argument**: unknown Token found
|
|
37
|
+
# * **argument**: expression (commands) where the error has been found
|
|
21
38
|
def self.unknown(token,expression)
|
|
22
39
|
message = " Sintax Error: unknown #{token.attribute} '#{token.me}' found"
|
|
23
40
|
self.print(message,expression,token.position)
|
|
24
41
|
end
|
|
25
42
|
|
|
43
|
+
# * **argument**: missmatched Token found
|
|
44
|
+
# * **argument**: expression (commands) where the error has been found
|
|
45
|
+
# * **argument**: token type (String) expected
|
|
26
46
|
def self.missmatch(token,expression,expectation)
|
|
27
47
|
message = " Sintax Error: expecting #{expectation} but #{token.attribute} token '#{token.me}' found"
|
|
28
48
|
self.print(message,expression,token.position)
|
|
29
49
|
end
|
|
30
50
|
|
|
51
|
+
# * **argument**: Token arguments are missing for
|
|
52
|
+
# * **argument**: expression (commands) where the error has been found
|
|
31
53
|
def self.missing(token,expression)
|
|
32
54
|
message = " Sintax Error: missing argument for '#{token.me}' #{token.attribute}"
|
|
33
55
|
self.print(message,expression,token.position)
|
|
34
56
|
end
|
|
35
57
|
|
|
58
|
+
# * **argument**: expression (commands) where the error has been found
|
|
59
|
+
# * **argument**: position where the error is located
|
|
36
60
|
def self.no_final_quotes(expression,position)
|
|
37
61
|
message = " Sintax Error: missing quotes for block"
|
|
38
62
|
self.print(message,expression,position)
|
|
39
63
|
end
|
|
40
64
|
|
|
65
|
+
# * **argument**: position where the error is located
|
|
66
|
+
# * **argument**: expression (commands) where the error has been found
|
|
41
67
|
def self.missing_general_string(position,expression)
|
|
42
68
|
message = " Sintax Error: missing agument block"
|
|
43
69
|
self.print(message,expression,position)
|
|
44
70
|
end
|
|
45
71
|
|
|
72
|
+
# * **argument**: position where the error is located
|
|
73
|
+
# * **argument**: expression (commands) where the error has been found
|
|
46
74
|
def self.numPoint_missing(position,expression)
|
|
47
75
|
message = " Sintax Error: number of points is missing"
|
|
48
76
|
self.print(message,expression,position)
|
|
49
77
|
end
|
|
50
78
|
|
|
79
|
+
# * **argument**: position where the error is located
|
|
80
|
+
# * **argument**: expression (commands) where the error has been found
|
|
51
81
|
def self.missing_expression_after_equal(pos,expression)
|
|
52
82
|
message = " Sintax Error: missing expression after EQUAL operator"
|
|
53
83
|
self.print(message,expression,pos)
|
|
54
84
|
end
|
|
55
85
|
|
|
86
|
+
# * **argument**: unexpected Token found
|
|
87
|
+
# * **argument**: expression (commands) where the error has been found
|
|
56
88
|
def self.unexpected_token_in_solve(token,expression)
|
|
57
89
|
message = " Sintax Error: unexpected #{token.attribute} token '#{token.me}' in 'solve:' args"
|
|
58
90
|
self.print(message,expression,token.position)
|
|
59
91
|
end
|
|
60
92
|
|
|
93
|
+
# * **argument**: position where the error is located
|
|
94
|
+
# * **argument**: expression (commands) where the error has been found
|
|
61
95
|
def self.missing_integ_range(pos,expression)
|
|
62
96
|
message = " Sintax Error: missing integration range for 'integ:' method"
|
|
63
97
|
self.print(message,expression,pos)
|
|
@@ -3,7 +3,17 @@
|
|
|
3
3
|
require_relative 'CnGal_new_classes.rb'
|
|
4
4
|
require_relative 'Integrators.rb'
|
|
5
5
|
|
|
6
|
+
##
|
|
7
|
+
# This class provides a simple representation of a function and some method
|
|
8
|
+
# to manipulate it
|
|
9
|
+
#
|
|
10
|
+
#
|
|
11
|
+
# Author:: Massimiliano Dal Mas (mailto:max.codeware@gmail.com)
|
|
12
|
+
# License:: Distributed under MIT license
|
|
6
13
|
class Function
|
|
14
|
+
|
|
15
|
+
# Creates a new `Function` object
|
|
16
|
+
# * **argument**: string to be converted (and representing a function)
|
|
7
17
|
def initialize(str)
|
|
8
18
|
@inp = str
|
|
9
19
|
@vars = []
|
|
@@ -11,23 +21,37 @@ class Function
|
|
|
11
21
|
@funct = convert(str)
|
|
12
22
|
end
|
|
13
23
|
|
|
24
|
+
# Tells whether the function has been correctly converted from the string
|
|
25
|
+
# format
|
|
26
|
+
#
|
|
27
|
+
# * **returns**: +true+ if the function has been successfully converted; +false+ else
|
|
14
28
|
def ok?
|
|
15
29
|
@funct == nil ? (return false) : (return true)
|
|
16
30
|
end
|
|
17
31
|
|
|
32
|
+
# Returns the function as a ruby block ({ |...| ... })
|
|
33
|
+
# * **argument**: number of variables needed for the block; default value is set if n == 0
|
|
34
|
+
# * **returns**: string that represents the block; +nil+ if an error is found
|
|
18
35
|
def to_block(n = 0)
|
|
19
|
-
n = @vars.size if n == 0
|
|
20
36
|
return nil if not self.ok?
|
|
21
37
|
return nil if n < @vars.size
|
|
22
|
-
|
|
38
|
+
n = @vars.size if n == 0
|
|
39
|
+
b_v = "|#{vars(n)}|" if n > 0
|
|
23
40
|
return "{ #{b_v} #{@funct} }"
|
|
24
41
|
end
|
|
25
42
|
|
|
43
|
+
# Builds the list of the variables needed for the block
|
|
44
|
+
#
|
|
45
|
+
# * **argument**: number of variables needed for the block
|
|
46
|
+
# * **returns**::
|
|
47
|
+
# * +nil+ if an error occourred while converting the function
|
|
48
|
+
# * +nil+ if `n` is less than the number of variables of the function
|
|
49
|
+
# * string representation of the list
|
|
26
50
|
def vars(n = 0)
|
|
27
51
|
n = @vars.size if n == 0
|
|
28
52
|
return nil if not self.ok?
|
|
29
53
|
return nil if n < @vars.size
|
|
30
|
-
var = @vars[0]
|
|
54
|
+
var = (@vars.size > 0) ? @vars[0] : ""
|
|
31
55
|
for i in 1...@vars.length do
|
|
32
56
|
var += ',' + @vars[i]
|
|
33
57
|
end
|
|
@@ -36,7 +60,8 @@ class Function
|
|
|
36
60
|
('a'..'z').each do |ch|
|
|
37
61
|
unless @vars.include? ch
|
|
38
62
|
i +=1
|
|
39
|
-
var += ','
|
|
63
|
+
var += ',' unless var == ""
|
|
64
|
+
var += ch
|
|
40
65
|
break if i == (n - @vars.size)
|
|
41
66
|
end
|
|
42
67
|
end
|
|
@@ -44,10 +69,19 @@ class Function
|
|
|
44
69
|
return var
|
|
45
70
|
end
|
|
46
71
|
|
|
47
|
-
|
|
48
|
-
|
|
72
|
+
# * **returns**: string representation of the function
|
|
73
|
+
def to_s
|
|
74
|
+
return @inp
|
|
49
75
|
end
|
|
50
|
-
|
|
76
|
+
|
|
77
|
+
# Integrates the function according to the specified method (see: Integrators)
|
|
78
|
+
#
|
|
79
|
+
# * **argument**: dimension of the range; see: Dim
|
|
80
|
+
# * **argument**: number of points for integration
|
|
81
|
+
# * **argument**: method to integrate the function (optional). 'simpson' is default
|
|
82
|
+
# * **returns**:
|
|
83
|
+
# * Numeric value as result
|
|
84
|
+
# * +nil+ if an error occourred
|
|
51
85
|
def integrate(r,n,m = 'simpson')
|
|
52
86
|
return nil unless self.ok?
|
|
53
87
|
block = self.to_block(1)
|
|
@@ -67,25 +101,29 @@ class Function
|
|
|
67
101
|
when "boole"
|
|
68
102
|
return eval("Integrators.boole(r.sx,r.dx,n)#{block}")
|
|
69
103
|
else
|
|
70
|
-
puts "Argument Error: Invalid integration method";
|
|
104
|
+
puts "Argument Error: Invalid integration method";
|
|
71
105
|
return nil
|
|
72
106
|
end
|
|
73
107
|
end
|
|
74
|
-
puts "Argument Error: bad function for integration";
|
|
108
|
+
puts "Argument Error: bad function for integration";
|
|
75
109
|
return nil
|
|
76
110
|
end
|
|
77
111
|
|
|
78
112
|
private
|
|
79
113
|
|
|
114
|
+
# Splits the input string into its components (variables,operators...)
|
|
115
|
+
#
|
|
116
|
+
# * **argument**: input function in string format (inserted by the user)
|
|
117
|
+
# * **returns**: array containing the components of the function
|
|
80
118
|
def tokenize(str)
|
|
81
119
|
tokens = Array.new
|
|
82
|
-
x =
|
|
120
|
+
x = ""
|
|
83
121
|
str.each_char do |ch|
|
|
84
122
|
case ch
|
|
85
123
|
when /[\+\-\*\/\^\(\)]/
|
|
86
124
|
tokens.push ch
|
|
87
125
|
when /[a-zA-Z\0-9\.]/
|
|
88
|
-
x = tokens.pop if not '+-*/^()'.include? tokens[tokens.size - 1].to_s
|
|
126
|
+
x = tokens.pop if not '+-*/^()'.include? tokens[tokens.size - 1].to_s and tokens.size > 0
|
|
89
127
|
tokens.push (x + ch)
|
|
90
128
|
x = ""
|
|
91
129
|
else
|
|
@@ -95,6 +133,10 @@ class Function
|
|
|
95
133
|
return tokens
|
|
96
134
|
end
|
|
97
135
|
|
|
136
|
+
# Converts the function inerted by the user in a function written in Ruby language
|
|
137
|
+
#
|
|
138
|
+
# * **argument**: input function in string format (inserted by the user)
|
|
139
|
+
# * **returns**: function understandable by Ruby in string format
|
|
98
140
|
def convert(str)
|
|
99
141
|
return nil unless str.is_a? String
|
|
100
142
|
str = tokenize(str)
|
|
@@ -103,6 +145,12 @@ class Function
|
|
|
103
145
|
return translate(str)
|
|
104
146
|
end
|
|
105
147
|
|
|
148
|
+
# Checks if the brackets are balanced in the input function
|
|
149
|
+
#
|
|
150
|
+
# * **argument**: array of the function components
|
|
151
|
+
# * **returns**:
|
|
152
|
+
# * +nil+ if `expr` is not an array
|
|
153
|
+
# * +true+ if the brackets are balanced; +false+ else
|
|
106
154
|
def balanced_brackets?(expr)
|
|
107
155
|
return nil unless expr.is_a? Array
|
|
108
156
|
brakets = 0
|
|
@@ -117,12 +165,23 @@ class Function
|
|
|
117
165
|
brakets == 0 ? (return true) : (return false)
|
|
118
166
|
end
|
|
119
167
|
|
|
168
|
+
# Translates the function from linmeric language to Ruby language
|
|
169
|
+
# using a turing machine to detect possible errors
|
|
170
|
+
#
|
|
171
|
+
# * **argument**: array of the function components
|
|
172
|
+
# * **argument**: boolean to identify whether we are inside brackets or not. +false+ default
|
|
173
|
+
# (out brackets)
|
|
174
|
+
# * **returns**:
|
|
175
|
+
# * +nil+ if `str` is not an array
|
|
176
|
+
# * function in Ruby language in string format
|
|
120
177
|
def translate(str,open_b = false)
|
|
121
178
|
return nil unless str.is_a? Array
|
|
122
179
|
funct = ""
|
|
123
180
|
state = 0
|
|
124
181
|
while @i < str.size
|
|
125
182
|
case state
|
|
183
|
+
|
|
184
|
+
# beginning state
|
|
126
185
|
when 0
|
|
127
186
|
case str[@i]
|
|
128
187
|
when /[\+\-]/
|