linmeric 0.1.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.
@@ -0,0 +1,358 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ require_relative 'CnGal_new_classes.rb'
4
+ require_relative 'CnGal_tools.rb'
5
+ require_relative 'Listener.rb'
6
+ # Definition of Matrix class
7
+ #
8
+
9
+ class InputError < ArgumentError; end
10
+ class MyArgError < RuntimeError; end
11
+
12
+ class Matrix
13
+
14
+ def initialize(rws = 0,cls = 0)
15
+ listener = Listener.new
16
+ # @exception = "Matrix "
17
+ if rws == 0 or cls == 0 or rws.is_a? Float or cls.is_a? Float then
18
+ raise MyArgError," Argument Error: invalid dimension #{rws}x#{cls} for Matrix object"
19
+ elsif !(rws.is_a? Fixnum) or !(cls.is_a? Fixnum) then
20
+ e = rws unless e.is_a? Fixnum
21
+ e = cls unless cls.is_a? Fixnum
22
+ raise MyArgError," Argument Error: colums and rows in Integer format expected but #{e.class} found"
23
+ else
24
+ @MyRws = rws; @MyCls = cls
25
+ @mx=[]
26
+ if block_given? then
27
+ for i in 0...@MyRws
28
+ for j in 0...@MyCls
29
+ value = yield(i,j) if block_given?
30
+ @mx << value
31
+ end
32
+ end
33
+ else
34
+ # if @exception == "Matrix " then
35
+ puts "Insert the line values (separated by space) and press return to go on"
36
+ for i in 0...rws
37
+ listener.reset
38
+ cl = listener.gets.split
39
+ raise ArgumentError, " #{cls} element(s) expected but #{cl.size} found. Retry" unless cl.size==cls
40
+ cl.map! { |e|
41
+ cv = Tool.convert(e)
42
+ err = e if cv == nil
43
+ raise MyArgError, " Argument Error: Invalid operation '#{err}' found in matrix rows" unless cv != nil
44
+ e = cv
45
+ }
46
+ @mx +=cl
47
+ # end
48
+ # puts
49
+ end
50
+ end
51
+ end
52
+
53
+
54
+ rescue ArgumentError => message
55
+ puts message
56
+ @mx=[]
57
+ retry
58
+
59
+ end
60
+
61
+ def Matrix.from_file(filename = "")
62
+ # @exception = "Matrix "
63
+ @mx = []
64
+ @MyCls = 0
65
+ @MyRws = 0
66
+ if !(File.exists? filename) or filename == "" then
67
+ raise MyArgError, " Argument Error: Invalid directory or filename"
68
+ else
69
+ File.open(filename, "r").each_line do |ln|
70
+ @MyRws += 1
71
+ ln = ln.chomp.split(',')
72
+ ln.map! do |el|
73
+ cv = Tool.convert(el)
74
+ err = el if cv == nil
75
+ raise InputError, " Argument Error: Invalid operation '#{err}' found in matrix rows" unless cv != nil
76
+ el = cv
77
+ end
78
+ @mx += ln
79
+ if @mx.size % ln.size != 0 then
80
+ raise MyArgError, " Argument Error: Irregular matrix rows"
81
+ # break
82
+ end
83
+ @MyCls = ln.length
84
+ end
85
+ end
86
+ # mat = Matrix.new(1,1) {1}
87
+ # mat.update(@mx,@MyRws,@MyCls)
88
+ # mat.inherit_exception(@exception)
89
+ return mat = Matrix.new(@MyRws,@MyCls) {|i,j| @mx[i*@MyCls+j]}
90
+
91
+ # rescue InputError => error
92
+ # @exception += "#{error}"
93
+ # mat = Matrix.new(1,1) {1}
94
+ # mat.inherit_exception(@exception)
95
+ # return mat
96
+ end
97
+
98
+ def to_file(filename = "")
99
+ if !(Dir.exist? File.dirname(filename)) or filename == "" then
100
+ raise MyArgError, " Argument Error: Invalid directory; Directory not found"
101
+ else
102
+ File.open(filename,"w") do |rw|
103
+ for i in 0...@MyRws do
104
+ for j in 0...(@MyCls - 1) do
105
+ rw.print "#{self[i,j]},"
106
+ end
107
+ rw.puts "#{self[i,j+1]}"
108
+ end
109
+ end
110
+ end
111
+ end
112
+
113
+ # def raised_exception?
114
+ # return nil if @exception == "Matrix "
115
+ # return @exception
116
+ # end
117
+
118
+ # def got_exception()
119
+ # @exception = "Matrix "
120
+ # end
121
+
122
+ def show()
123
+ max = 0
124
+ @mx.each{ |n| max = n.to_s.size if n.to_s.size > max}
125
+ for i in 0...@MyRws do
126
+ print '|'
127
+ for j in (i*(@MyCls))...(i*(@MyCls)+@MyCls) do
128
+ print " #{" "*(max-@mx[j].to_s.size)}#{@mx[j] }"
129
+ end
130
+ puts ' |'
131
+ end
132
+ return nil
133
+ end
134
+
135
+ def getCls()
136
+ return @MyCls
137
+ end
138
+
139
+ def getRws()
140
+ return @MyRws
141
+ end
142
+
143
+ def export()
144
+ return @mx
145
+ end
146
+
147
+ def update(mat,rws,cls)
148
+ if !(mat.is_a? Array) then
149
+ raise MyArgError, " Argument Error: invalid matrix array found"
150
+ else
151
+ [cls,rws].each do |e|
152
+ raise MyArgError, " Argument Error: colums and rows in Integer format expected but #{e.class} found" unless e.is_a? Numeric
153
+ end
154
+ end
155
+ # if @exception == "Matrix " then
156
+ @mx = mat
157
+ @MyRws = rws
158
+ @MyCls = cls
159
+ # end
160
+ end
161
+
162
+ # def inherit_exception(exception)
163
+ # @exception = exception
164
+ # end
165
+
166
+ def [](x,y)
167
+ if x.is_a? Numeric and y.is_a? Numeric then
168
+ return @mx[x*@MyCls + y]
169
+ elsif x.is_a? Range and y.is_a? Numeric
170
+ nm = []
171
+ x.each do |i|
172
+ nm << @mx[i*@MyCls + y]
173
+ end
174
+ return Matrix.new(x.count,1){|i,j| nm[i + j]}
175
+ elsif x.is_a? Numeric and y.is_a? Range
176
+ nm = []
177
+ y.each do |j|
178
+ nm << @mx[x*@MyCls + j]
179
+ end
180
+ return Matrix.new(1,y.count){|i,j| nm[i + j]}
181
+ elsif x.is_a? Range and y.is_a? Range
182
+ nm = []
183
+ x.each do |i|
184
+ y.each do |j|
185
+ nm << @mx[i*@MyCls + j]
186
+ end
187
+ end
188
+ return Matrix.new(x.count,y.count){ |i,j| nm[i*y.count + j]}
189
+ end
190
+ return nil
191
+ end
192
+
193
+ def []=(i,j,val)
194
+ @mx[i*@MyCls + j] = val
195
+ end
196
+
197
+ def ==(obj)
198
+ if obj.is_a? Matrix then
199
+ if (self.getCls == obj.getCls && self.getRws == obj.getRws ) then
200
+ (@mx == obj.export) ? (return true) : (return false)
201
+ end
202
+ end
203
+ return false
204
+ end
205
+
206
+ def !=(obj)
207
+ if obj.is_a? Matrix then
208
+ self == obj ? (return false) : (return true)
209
+ else
210
+ return true
211
+ end
212
+ end
213
+
214
+ def similar_to?(obj)
215
+ if obj.is_a? Matrix then
216
+ (self.getCls == obj.getCls &&
217
+ self.getRws == obj.getRws ) ? (return true) : (return false)
218
+ end
219
+ return false
220
+ end
221
+
222
+ def +(m)
223
+ if m.is_a? Matrix then
224
+ if m.similar_to? self then
225
+ return Matrix.new(@MyRws, @MyCls){ |i,j| self[i,j] + m[i,j]}
226
+ else
227
+ raise MyArgError, " Argument Error: invalid matrix for + operation"
228
+ end
229
+ else
230
+ raise MyArgError, " Argument Error: can't sum #{m.class} to Matrix"
231
+ end
232
+ end
233
+
234
+ def -(m)
235
+ if m.is_a? Matrix then
236
+ if m.similar_to? self then
237
+ return Matrix.new(@MyRws, @MyCls){ |i,j| self[i,j] - m[i,j]}
238
+ else
239
+ raise MyArgError, " Argument Error: invalid matrix for - operation"
240
+ end
241
+ else
242
+ raise MyArgError, " Argument Error: can't subtact #{m.class} to Matrix"
243
+ end
244
+ end
245
+
246
+ def can_multiply?(obj)
247
+ if obj.is_a? Matrix then
248
+ (self.getCls == obj.getRws) ? (return true) : (return false)
249
+ elsif obj.is_a? Numeric
250
+ return true
251
+ end
252
+ return false
253
+ end
254
+
255
+ def *(ob)
256
+ case
257
+ when (ob.is_a? Numeric)
258
+ return Matrix.new(@MyRws,@MyCls){ |i,j| self[i,j] * ob}
259
+ when (ob.is_a? Matrix)
260
+ if self.can_multiply? ob then
261
+ temp = []
262
+ for i in 0...@MyRws
263
+ for j in 0...ob.getCls()
264
+ temp[i*@MyCls + j] = 0
265
+ for k in 0...@MyCls
266
+ temp[i*@MyCls + j] += @mx[i*@MyCls + k] * ob[k,j]
267
+ end
268
+ end
269
+ end
270
+ return Matrix.new(@MyRws,ob.getCls()){ |i,j| temp[i * @MyCls + j]}
271
+ else
272
+ raise MyArgError, " Argument Error: Cannot multiply #{@MyRws}x#{@MyCls} Matrix with #{ob.getRws}x#{ob.getCls}"
273
+ end
274
+ else
275
+ raise MyArgError, " Argument Error: Cannot multiply Matrix with #{ob.class}"
276
+ end
277
+ end
278
+
279
+
280
+ def can_divide?(obj)
281
+ return (obj.is_a? Numeric)
282
+ end
283
+
284
+
285
+ def /(obj)
286
+ return Matrix.new(@MyRws,@MyCls) { |i,j| self[i,j] / obj.to_f} if obj.is_a? Numeric
287
+ raise MyArgError, " Argument Error: Invalid division between Matrix and #{obj.class} "
288
+ end
289
+
290
+ def **(obj)
291
+ return Matrix.new(@MyRws,@MyCls) { |i,j| self[i,j] ** obj} if obj.is_a? Numeric
292
+ raise MyArgError, " Argument Error: Invalid power Matrix-#{obj.class} "
293
+ end
294
+
295
+ def tr()
296
+ return Matrix.new(@MyCls,@MyRws) { |i,j| @mx[j * @MyCls + i]}
297
+ end
298
+
299
+ def norma()
300
+ sum=0
301
+ @mx.map do |e|
302
+ sum +=e**2
303
+ end
304
+ return Math.sqrt(sum)
305
+ end
306
+
307
+ def is_squared?()
308
+ self.getCls == self.getRws ? (return true) : (return false)
309
+ end
310
+
311
+ def laplace
312
+ if self.is_squared? then
313
+ return (self[0,0] * self[1,1]) - (self[0,1] * self[1,0]) if self.getRws == 2
314
+ res = 0
315
+ for i in 0...@MyRws
316
+ res += (((-1) ** i) * self[i,0] * del_rwcl(self,i,0).laplace) if self[i,0] != 0
317
+ end
318
+ return res
319
+ else
320
+ raise MyArgError, ' Argument Error: Cannot calculate determinat on a non-squared matrix'
321
+ return nil
322
+ end
323
+ end
324
+
325
+ def Matrix.identity(n)
326
+ return Matrix.new(n,n) { |i,j| (i == j) ? 1 : 0 }
327
+ end
328
+
329
+ def coerce(val)
330
+ return [self, val]
331
+ end
332
+
333
+ private
334
+
335
+ def del_rwcl(mx,rw,cl)
336
+ rd = mx.export.clone
337
+ cols = mx.getCls
338
+ for i in 0...cols
339
+ rd[i*cols+rw] = nil
340
+ rd[cl*(cols)+i] = nil
341
+ end
342
+ rd = rd.compact
343
+ rw = mx.getRws - 1
344
+ cl = mx.getCls - 1
345
+ return Matrix.new(rw,cl) { |i,j| rd[i*cl+j]}
346
+ end
347
+
348
+ protected
349
+
350
+
351
+ end
352
+
353
+
354
+
355
+
356
+
357
+
358
+
@@ -0,0 +1,231 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ require_relative 'Function_class.rb'
4
+
5
+ # Adding some method to Numeric class
6
+ # to work properly with CnGal project
7
+
8
+ class Numeric
9
+
10
+ def is_similar?(obj)
11
+ (obj.is_a? Numeric) ? (return true) : (return false)
12
+ end
13
+
14
+ def can_multiply?(obj)
15
+ (obj.is_a? Numeric) ? (return true) : ((obj.is_a? Matrix) ? (return true) : (return false))
16
+ end
17
+
18
+ def can_divide?(obj)
19
+ (obj.is_a? Numeric) ? (return true) : ((obj.is_a? Matrix) ? (return true) : (return false))
20
+ end
21
+
22
+ def show()
23
+ puts self
24
+ end
25
+
26
+ end
27
+
28
+ # Adding some method to Fixnum class
29
+ # to work properly with CnGal project
30
+
31
+ class Fixnum
32
+ def similar_to?(obj)
33
+ (obj.is_a? Numeric) ? (return true) : (return false)
34
+ end
35
+
36
+ def can_multiply?(obj)
37
+ (obj.is_a? Numeric) ? (return true) : ((obj.is_a? Matrix) ? (return true) : (return false))
38
+ end
39
+
40
+ def can_divide?(obj)
41
+ (obj.is_a? Numeric) ? (return true) : ((obj.is_a? Matrix) ? (return true) : (return false))
42
+ end
43
+
44
+ def show()
45
+ puts self
46
+ end
47
+ end
48
+
49
+
50
+ # Adding some method to Float class
51
+ # to work properly with CnGal project
52
+
53
+ class Float
54
+ def similar_to?(obj)
55
+ (obj.is_a? Numeric) ? (return true) : (return false)
56
+ end
57
+
58
+ def can_multiply?(obj)
59
+ (obj.is_a? Numeric) ? (return true) : ((obj.is_a? Matrix) ? (return true) : (return false))
60
+ end
61
+
62
+ def can_divide?(obj)
63
+ (obj.is_a? Numeric) ? (return true) : ((obj.is_a? Matrix) ? (return true) : (return false))
64
+ end
65
+
66
+ def show()
67
+ puts self
68
+ end
69
+
70
+ end
71
+
72
+ class NilClass
73
+ def show
74
+ puts "nil"
75
+ end
76
+
77
+ def size
78
+ return 0
79
+ end
80
+
81
+ end
82
+
83
+ # Adding some method to String class:
84
+ # contain? returns true whether the current string contains one of the
85
+ # chars in str
86
+ # contain_all? returns true whether the current string contains all the
87
+ # chars in str
88
+ # compact returns a string without spaces
89
+
90
+ class String
91
+
92
+ def contain?(str)
93
+ raise ArgumentError, '' unless str.is_a? String
94
+ str.each_char do |ch|
95
+ return true if self.include? ch
96
+ end
97
+ return false
98
+
99
+ rescue ArgumentError
100
+ str = str.to_s
101
+ retry
102
+ end
103
+
104
+ def contain_all?(str)
105
+ raise ArgumentError, '' unless str.is_a? String
106
+ str.each_char do |ch|
107
+ return false if not self.include? ch
108
+ end
109
+ return true
110
+ rescue ArgumentError
111
+ str = str.to_s
112
+ retry
113
+ end
114
+
115
+ def compact()
116
+ nstr = ''
117
+ self.each_char do |ch|
118
+ nstr += ch if ch != ' '
119
+ end
120
+ return nstr
121
+ end
122
+
123
+ def -(str)
124
+ myStr = str.to_s
125
+ temp = ''
126
+ i = 0
127
+ if self.include? myStr then
128
+ while i < self.length do
129
+ if self[i...(i + myStr.size)] == myStr then
130
+ i += myStr.size
131
+ else
132
+ temp += self[i]
133
+ i += 1
134
+ end
135
+ end
136
+ else
137
+ return self
138
+ end
139
+ return temp
140
+ end
141
+
142
+ def integer?
143
+ [ # In descending order of likeliness:
144
+ /^[-+]?[1-9]([0-9]*)?$/, # decimal
145
+ /^0[0-7]+$/, # octal
146
+ /^0x[0-9A-Fa-f]+$/, # hexadecimal
147
+ /^0b[01]+$/, # binary
148
+ /[0]/ # zero
149
+ ].each do |match_pattern|
150
+ return true if self =~ match_pattern
151
+ end
152
+ return false
153
+ end
154
+
155
+ def float?
156
+ pat = /^[-+]?[1-9]([0-9]*)?\.[0-9]+$/
157
+ return true if self=~pat
158
+ return false
159
+ end
160
+
161
+ def number?
162
+ (self.integer? or self.float?) ? (return true) : (return false)
163
+ end
164
+
165
+ def to_n
166
+ return self.to_f if self.float?
167
+ return self.to_i if self.integer?
168
+ end
169
+
170
+ def remove(index)
171
+ return self if index < 0
172
+ n_str = ''
173
+ for i in 0...self.size
174
+ n_str += self[i] unless i == index
175
+ end
176
+ return n_str
177
+ end
178
+ end
179
+
180
+ class Filename < String
181
+ def show
182
+ puts self
183
+ end
184
+
185
+ def name
186
+ i = self.size - 1
187
+ nm = ''
188
+ while self[i] != '/' do
189
+ nm += self[i]
190
+ i -= 1
191
+ end
192
+ return nm.reverse
193
+ end
194
+
195
+ def is_a?(obj)
196
+ self.class == obj.class ? (return true) : (return false)
197
+ end
198
+
199
+ end
200
+
201
+ class Dim
202
+ def initialize(str)
203
+ str = str - '"'
204
+ str = str.split(',')
205
+ if str.size == 2 and str[0] != "" and str[1] != "" then
206
+ @sx = str[0].to_n if str[0].integer? or str[0].float?
207
+ @dx = str[1].to_n if str[1].integer? or str[1].float?
208
+ else
209
+ puts ' Argument Error: wrong range format'#; puts
210
+ end
211
+ end
212
+
213
+ def sx()
214
+ return @sx
215
+ end
216
+
217
+ def dx()
218
+ return @dx
219
+ end
220
+ end
221
+
222
+
223
+
224
+
225
+
226
+
227
+
228
+
229
+
230
+
231
+
@@ -0,0 +1,80 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ require_relative 'CnGal_new_classes.rb'
4
+
5
+ module Tool
6
+ def self.keys
7
+ return ["mx:","t:", "shw:","shwvar:","det:","solve:","from:","as:","f:","norm:","id_mx:","integ:"]
8
+ end
9
+
10
+ def self.operators
11
+ return ["=","-","+","*","/","^",">"]
12
+ end
13
+
14
+ def self.letters
15
+ return "abcdefghijklmnopqrstuvwxyz"
16
+ end
17
+
18
+ def self.f
19
+ return ["log","sin","cos","PI","tan","exp"]
20
+ end
21
+
22
+ def self.is_keyword?(ob)
23
+ (self.keys.include? ob) ? (return true) : (return false)
24
+ end
25
+
26
+ def self.is_exp?(str)
27
+ (str.contain? "=+*-/^") ? (return true) : (('0123456789.'.contain_all? str) ?
28
+ (return true):((self.letters.contain? str) ? (return true):(return false)))
29
+ end
30
+
31
+ def self.print_stack(stack)
32
+ stack.keys.each do |k|
33
+ puts "#{k} = " if stack[k].is_a? Matrix
34
+ print "#{k} = " unless stack[k].is_a? Matrix
35
+ stack[k].show
36
+ end
37
+ end
38
+
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
+ end
79
+
80
+