M500 0.9.2 → 0.9.3

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: 2cb0b2efe7e5502e3cb7fac1fc3d1112cf9645ba
4
- data.tar.gz: 6b2c11cb1099ab9ee98922eb937c094a71ea78e5
3
+ metadata.gz: f192b4fe1ad28c8858892daba9c685b3b8113700
4
+ data.tar.gz: 514c03db3500f1091f8bd4d363f8af27ddf65848
5
5
  SHA512:
6
- metadata.gz: 19c82406a324b55fe0d46de4354fb1a083ce733d325a60f7cb52165a089790ed4c08534e7a22687f3479e5bfe06cdab84a921ea85c2e4fe2584f72ae9d4bd1da
7
- data.tar.gz: c158461ff3b8c4c8b11cbf2d0c0b6b7b29b5651be8792ec4854c73d6e4b37e685115c083bac561de8a33e49adb317399abb2415d0600433c052ac89e181e58c3
6
+ metadata.gz: 8800d206c4ddfd7caa61defc8dba29ab5e8b8bc5b7a267916e332b214ac1c7209fb9e3d9cf51ac10c809d8b90ef0b1329eefef76c82f5c2014b461f0b69cbcdf
7
+ data.tar.gz: 382234406fa4e5907ca2a83f61b48ebc382d486fd8c67c42a8ddf0d9753c2a36de6385cfdcfd061f9b7d721a39df66e6a7c55dea49137ebecc19596f1d8cc305
@@ -0,0 +1,448 @@
1
+ # -*- coding: utf-8 -*-
2
+ module Enumerable
3
+ def to_list(klass = List, *args, &block)
4
+ klass.new!(self, *args, &block)
5
+ end
6
+ end
7
+ def List(a=nil,&b)
8
+ b ? List.new!(a) {b} : List.new!(a)
9
+ end
10
+ def Set(a,&b)
11
+ b ? Set.new!(a) {b} : Set.new!(a)
12
+ end
13
+ def Sequence(a)
14
+ Sequence.new(a)
15
+ end
16
+ def Sigma(a,&block)
17
+ if a.kind_of?(Sigma)
18
+ a
19
+ elsif a.kind_of?(Quotient)
20
+ Sigma.new!(a,block)
21
+ elsif a.kind_of?(Natural) or a.kind_of?(Counting) or a.kind_of?(Zahlen) or a.kind_of?(Fraction) or a.kind_of?(Decimal)
22
+ elsif
23
+ Sigma.new!(a.to_Q,block)
24
+ elsif a.kind_of?(NaughtClass) or a.kind_of?(EmptyListClass) or a.kind_of?(NilClass)
25
+ a unless b
26
+ new(Quotinet(0,1),block)
27
+ elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass)
28
+ a
29
+ end
30
+ end
31
+ class List
32
+ def List.new!(a=nil, &b)
33
+ b ? new(a) {b} : new(a)
34
+ end
35
+ private_class_method :new
36
+ include Enumerable
37
+ def self.[](*ary)
38
+ new(ary)
39
+ end
40
+ def initialize(a, &block)
41
+ @hash ||= Hash.new
42
+ a.nil? and return
43
+ if block
44
+ with_enum(a) {|o| add(block[o]) }
45
+ else
46
+ merge(a)
47
+ end
48
+ end
49
+ private :initialize
50
+ def with_enum(enum, &block)
51
+ if enum.respond_to?(:each_entry)
52
+ enum.each_entry(&block)
53
+ elsif enum.respond_to?(:each)
54
+ enum.each(&block)
55
+ else
56
+ raise ArgumentError, "value must be enumerable"
57
+ end
58
+ end
59
+ private :with_enum
60
+ def initialize_copy(orig)
61
+ @hash = orig.instance_variable_get(:@hash).dup
62
+ end
63
+ def freeze
64
+ super
65
+ @hash.freeze
66
+ self
67
+ end
68
+ def taint
69
+ super
70
+ @hash.taint
71
+ self
72
+ end
73
+ def untaint
74
+ super
75
+ @hash.untaint
76
+ self
77
+ end
78
+ def size
79
+ @hash.size
80
+ end
81
+ alias length size
82
+ def empty?
83
+ @hash.empty?
84
+ end
85
+ def clear
86
+ @hash.clear
87
+ self
88
+ end
89
+ def replace(enum)
90
+ if enum.instance_of?(self.class)
91
+ @hash.replace(enum.instance_variable_get(:@hash))
92
+ else
93
+ clear
94
+ merge(enum)
95
+ end
96
+ self
97
+ end
98
+ def to_a
99
+ @hash.keys
100
+ end
101
+ def flatten_merge(set, seen = List.new!)
102
+ set.each { |e|
103
+ if e.is_a?(List)
104
+ if seen.include?(e_id = e.object_id)
105
+ raise ArgumentError, "tried to flatten recursive List"
106
+ end
107
+ seen.add(e_id)
108
+ flatten_merge(e, seen)
109
+ seen.delete(e_id)
110
+ else
111
+ add(e)
112
+ end
113
+ }
114
+ self
115
+ end
116
+ protected :flatten_merge
117
+ def flatten
118
+ self.class.new!.flatten_merge(self)
119
+ end
120
+ def flatten!
121
+ detect {|e| e.is_a?(List) }? replace(flatten()) : nil
122
+ end
123
+ def member?(a)
124
+ @hash.include?(a)
125
+ end
126
+ alias include? member?
127
+ def contianing_list?(li)
128
+ li.is_a?(List) or raise ArgumentError, "value must be a List"
129
+ return false if size < li.size
130
+ li.all? {|a| include?(a) }
131
+ end
132
+ alias superset? contianing_list?
133
+ alias >= contianing_list?
134
+ def strict_contianing_list?(li)
135
+ li.is_a?(List) or raise ArgumentError, "value must be a List"
136
+ return false if size <= li.size
137
+ li.all? {|a| include?(a) }
138
+ end
139
+ alias proper_superset? strict_contianing_list?
140
+ alias > strict_contianing_list?
141
+ def contianed_list?(li)
142
+ li.is_a?(List) or raise ArgumentError, "value must be a List"
143
+ return false if li.size < size
144
+ all? {|a| li.include?(a) }
145
+ end
146
+ alias subset? contianed_list?
147
+ alias <= contianed_list?
148
+ def strictly_contianed_list?(li)
149
+ li.is_a?(List) or raise ArgumentError, "value must be a set"
150
+ return false if li.size <= size
151
+ all? {|a| li.include?(a) }
152
+ end
153
+ alias proper_subset? strictly_contianed_list?
154
+ alias < strictly_contianed_list?
155
+ def each(&block)
156
+ block or return enum_for(__method__)
157
+ @hash.each_key(&block)
158
+ self
159
+ end
160
+ def add(a)
161
+ @hash[a] = true
162
+ self
163
+ end
164
+ alias << add
165
+ def add?(a)
166
+ include?(a) ? nil : add(a)
167
+ end
168
+ def delete(a)
169
+ @hash.delete(a)
170
+ self
171
+ end
172
+ def delete?(a)
173
+ include?(a) ? delete(a) : nil
174
+ end
175
+ def delete_if
176
+ block_given? or return enum_for(__method__)
177
+ to_a.each {|a| @hash.delete(a) if yield(a) }
178
+ self
179
+ end
180
+ def keep_if
181
+ block_given? or return enum_for(__method__)
182
+ to_a.each {|a| @hash.delete(a) unless yield(a) }
183
+ self
184
+ end
185
+ def collect!
186
+ block_given? or return enum_for(__method__)
187
+ li = self.class.new!
188
+ each {|a| li << yield(a) }
189
+ replace(li)
190
+ end
191
+ alias map! collect!
192
+ def reject!(&block)
193
+ block or return enum_for(__method__)
194
+ n = size
195
+ delete_if(&block)
196
+ size == n ? nil : self
197
+ end
198
+ def select!(&block)
199
+ block or return enum_for(__method__)
200
+ n = size
201
+ keep_if(&block)
202
+ size == n ? nil : self
203
+ end
204
+ def merge(enum)
205
+ enum.instance_of?(self.class) ? @hash.update(enum.instance_variable_get(:@hash)) : with_enum(enum) { |o| add(o) }
206
+ self
207
+ end
208
+ def subtract(enum)
209
+ with_enum(enum) {|a| delete(a) }
210
+ self
211
+ end
212
+ def union(a)
213
+ dup.merge(a)
214
+ end
215
+ alias + union
216
+ alias | union
217
+ def difference(a)
218
+ dup.subtract(a)
219
+ end
220
+ alias - difference
221
+ def intersection(a)
222
+ n = self.class.new!
223
+ with_enum(a) {|b| n.add(b) if include?(b) }
224
+ n
225
+ end
226
+ alias & intersection
227
+ def symetric_difference(a)
228
+ n = List.new!(a)
229
+ each {|b| if n.include?(b) then n.delete(b) else n.add(b) end }
230
+ n
231
+ end
232
+ alias ^ symetric_difference
233
+ alias sym_diff symetric_difference
234
+ def ==(other)
235
+ if self.equal?(other)
236
+ true
237
+ elsif other.instance_of?(self.class)
238
+ @hash == other.instance_variable_get(:@hash)
239
+ elsif other.is_a?(List) && self.size == other.size
240
+ other.all? {|a| @hash.include?(a) }
241
+ else
242
+ false
243
+ end
244
+ end
245
+ def hash
246
+ @hash.hash
247
+ end
248
+ def eql?(a)
249
+ return false unless a.is_a?(List)
250
+ @hash.eql?(a.instance_variable_get(:@hash))
251
+ end
252
+ def cartisian(a)
253
+ # E.g.
254
+ # {1, 2} × {red, white} = {(1, red), (1, white), (2, red), (2, white)}.
255
+ # {1, 2, green} × {red, white, green} = {(1, red), (1, white), (1, green), (2, red), (2, white), (2, green), (green, red), (green, white), (green, green)}.
256
+ # {1, 2} × {1, 2} = {(1, 1), (1, 2), (2, 1), (2, 2)}.
257
+ #Some basic properties of cartesian products:
258
+ #A × ∅ = ∅.
259
+ #A × (B ∪ C) = (A × B) ∪ (A × C).
260
+ #(A ∪ B) × C = (A × C) ∪ (B × C).
261
+ #Let A and B be finite sets. Then
262
+ #| A × B | = | B × A | = | A | × | B |.
263
+ end
264
+ alias * cartisian
265
+ alias x cartisian
266
+ def categorise_list
267
+ # e.g.:
268
+ # files = List.new(Dir.glob("*.rb"))
269
+ # ret = files.classify { |f| File.mtime(f).year }
270
+ # p ret # => {2010=>#<List: {"a.rb", "b.rb"}>,2011=>#<List: {"c.rb", "d.rb", "e.rb"}>,2012=>#<List: {"f.rb"}>}
271
+ block_given? or return enum_for(__method__)
272
+ h = {}
273
+ each { |i|
274
+ x = yield(i)
275
+ (h[x] ||= self.class.new!).add(i)
276
+ }
277
+ h
278
+ end
279
+ alias classify categorise_list
280
+ def contain_by_rule(&rule)
281
+ rule or return enum_for(__method__)
282
+ if rule.arity == 2
283
+ require 'tsort'
284
+ class << dig = {} # :nodoc:
285
+ include TSort
286
+ alias tsort_each_node each_key
287
+ def tsort_each_child(node, &block)
288
+ fetch(node).each(&block)
289
+ end
290
+ end
291
+ each { |u|
292
+ dig[u] = a = []
293
+ each{ |v| rule.call(u, v) and a << v }
294
+ }
295
+ li = List.new!()
296
+ dig.each_strongly_connected_component { |css|
297
+ li.add(self.class.new!(css))
298
+ }
299
+ li
300
+ else
301
+ List.new!(classify(&rule).values)
302
+ end
303
+ alias contain_by contain_by_rule
304
+ alias divide contain_by_rule
305
+ end
306
+ def inspect
307
+ ids = (Thread.current[:__inspect_key__] ||= [])
308
+ return sprintf('#<%s:: {...}>', self.class.name) if ids.include?(object_id)
309
+ begin
310
+ ids << object_id
311
+ return sprintf('#<%s:: {%s}>', self.class, to_a.inspect[1..-2])
312
+ ensure
313
+ ids.pop
314
+ end
315
+ end
316
+ def pretty_print(pp)
317
+ pp.text sprintf('#<%s:: {', self.class.name)
318
+ pp.nest(1) {
319
+ pp.seplist(self) { |o|
320
+ pp.pp o
321
+ }
322
+ }
323
+ pp.text "}>"
324
+ end
325
+ alias pp pretty_print
326
+ def pretty_print_cycle(pp)
327
+ pp.text sprintf('#<%s:: {%s}>', self.class.name, empty? ? '' : '...')
328
+ end
329
+ alias pp_cycle pretty_print_cycle
330
+ end
331
+ class Set < List
332
+ @@setup = false
333
+ def Set.new!(*args)
334
+ new(args)
335
+ end
336
+ private_class_method :new
337
+ private :initialize
338
+ def initialize(*args, &block)
339
+ initialize(*args, &block)
340
+ end
341
+ end
342
+ class Sigma<Numeric
343
+ def Sigma.new!(a,b)
344
+ if a.kind_of?(Sigma)
345
+ a
346
+ elsif a.kind_of?(Quotient)
347
+ new(a,b)
348
+ elsif a.kind_of?(Natural) or a.kind_of?(Counting) or a.kind_of?(Zahlen) or a.kind_of?(Fraction) or a.kind_of?(Decimal)
349
+ elsif
350
+ new(a.to_Q,b)
351
+ elsif a.kind_of?(NaughtClass) or a.kind_of?(EmptyListClass) or a.kind_of?(NilClass)
352
+ a unless b
353
+ new(Quotinet(0,1),b) if b
354
+ elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass)
355
+ a
356
+ end
357
+ end
358
+ private_class_method :new
359
+ @@epsilon = Quotient(1,100000000000)
360
+ @@upsilon = "a very large number ceiling to signal change to scientific format"
361
+ def Sigma::epsilon(e)
362
+ #on epsilon change flag for dirty and recalculate
363
+ end
364
+ def initialize(rat,irrat,abs=1)
365
+ @absolute = abs
366
+ @rat = rat
367
+ @irrat = irrat
368
+ @k = 1
369
+ @k0 = 0
370
+ @update_proc = proc{ ret = Quotient(0,1) ; (@k0..@k).to_a.each{|n| ret += @irrat.call(n)}; ret}
371
+ end
372
+ attr_accessor :k, :k0
373
+ def convergents
374
+ []
375
+ end
376
+ def irrational
377
+ true ? false : true
378
+ end
379
+ def rational?
380
+ true ? true : false
381
+ end
382
+ def to_a
383
+ convergents
384
+ end
385
+ def to_s
386
+ e = ""
387
+ e = "-" if @absolute == -1
388
+ # @update_proc
389
+ end
390
+ def updateApprox(&block)
391
+ instance_exec(&block)
392
+ end
393
+ def updateApprox1
394
+ @rat += @update_proc.call
395
+ # instance_eval(@update_eval.call)
396
+ end
397
+ def next
398
+ t = @k
399
+ @k0 = t
400
+ @k += 1
401
+ @rat += @update_proc.call
402
+ t = @k
403
+ @k0 = t
404
+ @k += 1
405
+ end
406
+ private :initialize
407
+ attr_accessor :update_proc
408
+ def to_Q
409
+ @rat
410
+ end
411
+ def to_Dec
412
+ @rat.to_Dec
413
+ end
414
+ end
415
+ #class List
416
+ #end
417
+ class Sequence
418
+ def initialize
419
+ @index0 = 0
420
+ @indexn = 50
421
+ @func = proc{|k|
422
+ @a_n_minus1 = @a_n
423
+ @a_n *= (Quotient(1) + Quotient(Zahlen(1),Natural(k+1)))
424
+ }
425
+ @a_n_minus1 = Quotient(1)
426
+ @a_n = Quotient(1)
427
+ @sequence = List.new!#([])
428
+ (@index0..@indexn).to_a.each{|n|
429
+ @sequence << @func.call(n)}
430
+ end
431
+ def to_h
432
+ end
433
+ def to_cols
434
+ result = [[],[]]
435
+ (@index0..@indexn).to_a.each{|n|
436
+ result.at(0)<< n
437
+ result.at(1)<< @func.call(n)}
438
+ end
439
+ def to_Set
440
+ Set(@sequence)
441
+ end
442
+ def to_Series
443
+ a = Sigma(@sequence.inject(:+).to_Q) {@func}
444
+ a.k0 = @index0
445
+ a.k = @indexn
446
+ a
447
+ end
448
+ end
@@ -1,7 +1,7 @@
1
1
  #
2
2
  # m500.rb -
3
3
  # $Release Version: 0.9 $
4
- # $Revision: 1.44 $
4
+ # $Revision: 1.45 $
5
5
  # by mark ingram
6
6
  #
7
7
  # --
@@ -122,12 +122,15 @@ class String
122
122
  ret = naught
123
123
  if md then
124
124
  t = 1
125
- ret = Quotient(Zahlen(md[1].to_i),Natural(md[2].to_i))
125
+ Natural(md[2].to_i).kind_of?(EmptySetClass) ? ret = infinity : ret = Quotient(Zahlen(md[1].to_i),Natural(md[2].to_i))
126
126
  else
127
- ret = Quotient(Zahlen(md[1].to_i),Natural(md[2].to_i))
127
+ Natural(md[2].to_i).kind_of?(EmptySetClass) ? ret = infinity : ret = Quotient(Zahlen(md[1].to_i),Natural(md[2].to_i))
128
128
  end
129
129
  ret
130
130
  end
131
+ def to_ST
132
+ ST.new(self)
133
+ end
131
134
  def to_Dec
132
135
  re = /^-/
133
136
  md = re.match(self.to_s)
@@ -433,6 +436,63 @@ class Rational
433
436
  "<mn #{sgml_id}class='rational'>#{self.to_s}</mn>"
434
437
  end
435
438
  end
439
+ class ST<Numeric
440
+ #class SymbolicTerm<Numeric
441
+ def initialize(a,b=0,c='t') # a*(c) + b
442
+ if a.kind_of?(String)
443
+ regex = /(\d+[a-z])*(\+)?(\d+)?/
444
+ m = regex.match(a)
445
+ n = /(\d+)([a-z])/.match(m[1]) if m[1]
446
+ @a = [n.nil? ? 0 : n[1] ? n[1] : 0, m[3] ? m[3] : 0, n.nil? ? c : n[2] ? n[2] : 0]
447
+ else
448
+ @a = [a,b,c]
449
+ end
450
+ end
451
+ attr_reader :a
452
+ def +(o)
453
+ if o.kind_of?(ST) then
454
+ "#{@a.at(0).to_i+o.a.at(0).to_i}#{@a.at(2)}+#{@a.at(1).to_i+o.a.at(1).to_i}"
455
+ elsif o == 0
456
+ self
457
+ else
458
+ a.at(0).to_i ==0 ? o : "#{@a.at(0)}#{@a.at(2)}+#{@a.at(1).to_i+o}" if o.kind_of?(Numeric)
459
+ end
460
+ end
461
+ def *(o)
462
+ if o.kind_of?(ST) then
463
+ "#{@a.at(0).to_i*o.a.at(0).to_i}#{@a.at(2)}+#{@a.at(1).to_i*o.a.at(1)}"
464
+ elsif o == 0
465
+ "0"
466
+ else
467
+ "#{@a.at(0).to_i*o}#{@a.at(2)}+#{@a.at(1).to_i*o}" if o.kind_of?(Numeric)
468
+ end
469
+ end
470
+ def -(o)
471
+ if o.kind_of?(ST) then
472
+ "#{@a.at(0).to_i-o.a.at(0).to_i}#{@a.at(2)}+#{@a.at(1)-o.a.at(1)}"
473
+ else
474
+ "#{@a.at(0)}#{@a.at(2)}+#{@a.at(1)-o}" if o.kind_of?(Numeric)
475
+ end
476
+ end
477
+ def /(o)
478
+ if o.kind_of?(ST) then
479
+ "#{@a.at(0).to_i/o.a.at(0).to_i}#{@a.at(2)}+#{@a.at(1).to_i/o.a.at(1)}"
480
+ elsif o == 0
481
+ "OO"
482
+ else
483
+ "#{@a.at(0).to_i/o}#{@a.at(2)}+#{@a.at(1).to_i/o}" if o.kind_of?(Numeric)
484
+ end
485
+ end
486
+ def coerce(o)
487
+ [self, o]
488
+ end
489
+ def to_s
490
+ "'#{@a.at(0)}#{@a.at(2)}+#{@a.at(1)}'"
491
+ end
492
+ def inspect
493
+ "'#{@a.at(0)}#{@a.at(2)}+#{@a.at(1)}'.to_ST"
494
+ end
495
+ end
436
496
  def naught
437
497
  NaughtClass.new!
438
498
  end
@@ -1157,6 +1217,16 @@ class Counting < Natural
1157
1217
  Counting(x ** y)
1158
1218
  end
1159
1219
  end
1220
+ def factorial_1
1221
+ (Counting(1)..self).inject {|product, n| product * n }
1222
+ end
1223
+ def factorial
1224
+ if self == Counting(0)
1225
+ Counting(1)
1226
+ else
1227
+ self * Counting(self.to_i-1).factorial
1228
+ end
1229
+ end
1160
1230
  def % (a)
1161
1231
  self.to_i%(a.to_i)
1162
1232
  end
@@ -2483,7 +2553,17 @@ t << zz
2483
2553
  elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass)
2484
2554
  infinity
2485
2555
  else
2486
- ((self.to_Q) + (a.to_Q)).to_K #not irrational
2556
+ op = Matrix(2,4)
2557
+ op.at_1_1 = 1
2558
+ op.at_2_1 = 0
2559
+ op.at_1_2 = 0
2560
+ op.at_2_2 = 1
2561
+ op.at_1_3 = 0
2562
+ op.at_2_3 = 0
2563
+ op.at_1_4 = 1
2564
+ op.at_2_4 = 0
2565
+ res = self.operation(op,a)
2566
+ res.to_K
2487
2567
  end
2488
2568
  end
2489
2569
  def -(a)
@@ -2492,7 +2572,17 @@ t << zz
2492
2572
  elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass)
2493
2573
  infinity
2494
2574
  else
2495
- ((self.to_Q) - (a.to_Q)).to_K #not irrational
2575
+ op = Matrix(2,4)
2576
+ op.at_1_1 = 1
2577
+ op.at_2_1 = 0
2578
+ op.at_1_2 = 0
2579
+ op.at_2_2 = -1
2580
+ op.at_1_3 = 0
2581
+ op.at_2_3 = 0
2582
+ op.at_1_4 = 1
2583
+ op.at_2_4 = 0
2584
+ res = self.operation(op,a)
2585
+ res.to_K
2496
2586
  end
2497
2587
  end
2498
2588
  def * (a)
@@ -2501,7 +2591,17 @@ t << zz
2501
2591
  elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass)
2502
2592
  infinity
2503
2593
  else
2504
- ((self.to_Q) * (a.to_Q)).to_K
2594
+ op = Matrix(2,4)
2595
+ op.at_1_1 = 0
2596
+ op.at_2_1 = 1
2597
+ op.at_1_2 = 0
2598
+ op.at_2_2 = 0
2599
+ op.at_1_3 = 0
2600
+ op.at_2_3 = 0
2601
+ op.at_1_4 = 1
2602
+ op.at_2_4 = 0
2603
+ res = self.operation(op,a)
2604
+ res.to_K
2505
2605
  end
2506
2606
  end
2507
2607
  def / (a)
@@ -2510,11 +2610,134 @@ t << zz
2510
2610
  elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass)
2511
2611
  NAN.Class.new
2512
2612
  elsif a.kind_of?(Kuttenbruch)
2513
- self * a.reciprical
2613
+ op = Matrix(2,4)
2614
+ op.at_1_1 = 1
2615
+ op.at_2_1 = 0
2616
+ op.at_1_2 = 0
2617
+ op.at_2_2 = 0
2618
+ op.at_1_3 = 0
2619
+ op.at_2_3 = 0
2620
+ op.at_1_4 = 0
2621
+ op.at_2_4 = 1
2622
+ res = self.operation(op,a)
2623
+ res.to_K
2514
2624
  else
2515
2625
  (self.to_Q/a.to_Q).to_K
2516
2626
  end
2517
2627
  end
2628
+ def operation(z,other)
2629
+ oa = other.to_a
2630
+ sa = self.to_a
2631
+ a0 = nil
2632
+ b0 = nil
2633
+ c0 = nil
2634
+ d0 = nil
2635
+ e0 = nil
2636
+ f0 = nil
2637
+ g0 = nil
2638
+ h0 = nil
2639
+ a1 = nil
2640
+ b1 = nil
2641
+ c1 = nil
2642
+ d1 = nil
2643
+ e1 = nil
2644
+ f1 = nil
2645
+ g1 = nil
2646
+ h1 = nil
2647
+ x = 0
2648
+ y =0
2649
+ res_a = []
2650
+ if z.kind_of?(Matrix) then
2651
+ # check is z = Matrix(2,4)
2652
+ a0 = z.at_1_1
2653
+ e0 = z.at_2_1
2654
+ b0 = z.at_1_2
2655
+ f0 = z.at_2_2
2656
+ c0 = z.at_1_3
2657
+ g0 = z.at_2_3
2658
+ d0 = z.at_1_4
2659
+ h0 = z.at_2_4
2660
+ else
2661
+ p "raise an input error NOT a MATRIX 2x4"
2662
+ end
2663
+ tmp = 0
2664
+ self.to_pix(a0,b0,c0,d0,e0,f0,g0,h0)
2665
+ p "init"
2666
+ while tmp <19 #true # size of k_res is less than epsilon
2667
+ eggs = true
2668
+ if e0 == 0 or f0 == 0 or g0 == 0 or h0 == 0 then
2669
+ eggs = false if f0 == 0
2670
+ else # all are positive
2671
+ p "positive"
2672
+ #p a0.div(e0).at(0)
2673
+ eggs = false if a0.divmod(e0).at(0) == c0.divmod(g0).at(0)
2674
+ end
2675
+ #p (f > 0 and e > 0 and g > 0 )? (b/f-a/e).abs : f,e,g
2676
+ #p (f > 0 and e > 0 and g > 0 ) ? (c/g-a/e).abs : f,e,g
2677
+ p "### #{eggs}"
2678
+ if eggs then
2679
+ p "to the right"
2680
+ x = sa.shift
2681
+ a1 = (a0*x)+c0
2682
+ b1 = (b0*x)+d0
2683
+ c1 = a0
2684
+ d1 = b0
2685
+ e1 = (e0*x)+g0
2686
+ f1 = (f0*x)+h0
2687
+ g1 = e0
2688
+ h1 = f0
2689
+ self.to_pix(a1,b1,c1,d1,e1,f1,g1,h1)
2690
+ else
2691
+ p "downwards"
2692
+ y = oa.shift
2693
+ a1 = (a0*y)+b0
2694
+ b1 = a0
2695
+ c1 = (c0*y)+d0
2696
+ d1 = c0
2697
+ e1 = (e0*y)+f0
2698
+ f1 = e0
2699
+ g1 = (g0*y)+h0
2700
+ h1 = g0
2701
+ self.to_pix(a1,b1,c1,d1,e1,f1,g1,h1)
2702
+ end
2703
+ test = false
2704
+ eucint = a0.divmod(e0).at(0) if e0 > 0
2705
+ if y > 1 and x > 1 and f0 > 0 and g0 > 0 and h0>0
2706
+ test = eucint == b0.divmod(f0).at(0) and eucint == c0.divmod(g0).at(0) and eucint == d0.divmod(h0).at(0) and eucint > 0
2707
+ end
2708
+ if test then
2709
+ p "here"
2710
+ res_a << eucint
2711
+ a0 = e1
2712
+ b0 = f1
2713
+ c0 = g1
2714
+ d0 = h1
2715
+ e0 = a1-(e1*eucint)
2716
+ f0 = b1-(f1*eucint)
2717
+ g0 = c1-(g1*eucint)
2718
+ h0 = d1-(h1*eucint)
2719
+ a1 = a0
2720
+ b1 = b0
2721
+ c1 = c0
2722
+ d1 = d0
2723
+ e1 = e0
2724
+ f1 = f0
2725
+ g1 = g0
2726
+ h1 = h0
2727
+ self.to_pix(a1,b1,c1,d1,e1,f1,g1,h1)
2728
+ end
2729
+ tmp += 1
2730
+ a0 = a1
2731
+ b0 = b1
2732
+ c0 = c1
2733
+ d0 = d1
2734
+ e0 = e1
2735
+ f0 = f1
2736
+ g0 = g1
2737
+ h0 = h1
2738
+ end
2739
+ res_a
2740
+ end
2518
2741
  def ** (a)
2519
2742
  self.to_f ** a.to_f
2520
2743
  end
@@ -2648,6 +2871,7 @@ class Real < Numeric
2648
2871
  end
2649
2872
  class Matrix
2650
2873
  include SGML
2874
+ include Comparable
2651
2875
  def to_sgml
2652
2876
  tmp = "<mn #{sgml_id}class='matrix'><mrow><mo>(</mo><mtable>"
2653
2877
  (1..@nsize).to_a.each{|a|
@@ -2745,6 +2969,9 @@ attr_accessor :msize, :nsize
2745
2969
  def og!
2746
2970
  @tr = false
2747
2971
  end
2972
+ def size
2973
+ self.msize * self.nsize
2974
+ end
2748
2975
  def +(nm)
2749
2976
  a = ""
2750
2977
  if not(nm.is_tr? or @tr) then
@@ -2781,14 +3008,14 @@ attr_accessor :msize, :nsize
2781
3008
  end
2782
3009
  return a
2783
3010
  end
2784
- def *(nm)
3011
+ def -(nm)
2785
3012
  a = ""
2786
3013
  if not(nm.is_tr? or @tr) then
2787
3014
  if nm.msize == @msize and nm.nsize == @nsize then
2788
3015
  a= Matrix(@nsize, @msize)
2789
3016
  (1..@nsize).to_a.each{|x|
2790
3017
  (1..@msize).to_a.each{|y|
2791
- eval("a.at_#{x}_#{y}= self.at_#{x}_#{y} * nm.at_#{x}_#{y}")
3018
+ eval("a.at_#{x}_#{y}= self.at_#{x}_#{y} - nm.at_#{x}_#{y}")
2792
3019
  }
2793
3020
  }
2794
3021
  else
@@ -2798,7 +3025,7 @@ attr_accessor :msize, :nsize
2798
3025
  a= Matrix(@nsize, @msize)
2799
3026
  (1..@nsize).to_a.each{|x|
2800
3027
  (1..@msize).to_a.each{|y|
2801
- eval("a.at_#{x}_#{y}= self.at_#{x}_#{y} * nm.tr_#{x}_#{y}")
3028
+ eval("a.at_#{x}_#{y}= self.at_#{x}_#{y} - nm.tr_#{x}_#{y}")
2802
3029
  }
2803
3030
  }
2804
3031
  else
@@ -2808,7 +3035,7 @@ attr_accessor :msize, :nsize
2808
3035
  a= Matrix(@msize, @nsize)
2809
3036
  (1..@msize).to_a.each{|x|
2810
3037
  (1..@nsize).to_a.each{|y|
2811
- eval("a.at_#{x}_#{y}= self.tr_#{x}_#{y} + nm.at_#{x}_#{y}")
3038
+ eval("a.at_#{x}_#{y}= self.tr_#{x}_#{y} - nm.at_#{x}_#{y}")
2812
3039
  }
2813
3040
  }
2814
3041
  else
@@ -2817,6 +3044,111 @@ attr_accessor :msize, :nsize
2817
3044
  end
2818
3045
  return a
2819
3046
  end
3047
+ def *(nm)
3048
+ a = nil
3049
+ cmd = ""
3050
+ if nm.kind_of?(Matrix) then
3051
+ if not(nm.is_tr? or @tr) then
3052
+ if nm.msize == @msize and nm.nsize == @nsize then
3053
+ a= Matrix(@nsize, @msize)
3054
+ (1..@nsize).to_a.each{|x|
3055
+ (1..@msize).to_a.each{|y|
3056
+ cmd += "a.at_#{x}_#{y}= self.at_#{x}_#{y} * nm.at_#{x}_#{y};"
3057
+ }
3058
+ }
3059
+ else
3060
+ end
3061
+ elsif nm.is_tr? and not @tr then
3062
+ if nm.msize == @nsize and nm.nsize == @msize then
3063
+ a= Matrix(@nsize, @msize)
3064
+ (1..@nsize).to_a.each{|x|
3065
+ (1..@msize).to_a.each{|y|
3066
+ cmd += "a.at_#{x}_#{y}= self.at_#{x}_#{y} * nm.tr_#{x}_#{y};"
3067
+ }
3068
+ }
3069
+ else
3070
+ end
3071
+ elsif not nm.is_tr? and @tr then
3072
+ if nm.msize == @nsize and nm.nsize == @msize then
3073
+ a= Matrix(@msize, @nsize)
3074
+ (1..@msize).to_a.each{|x|
3075
+ (1..@nsize).to_a.each{|y|
3076
+ cmd += "a.at_#{x}_#{y}= self.tr_#{x}_#{y} + nm.at_#{x}_#{y};"
3077
+ }
3078
+ }
3079
+ else
3080
+ end
3081
+ else
3082
+ end
3083
+ elsif nm.kind_of?(Numeric)
3084
+ a= Matrix(@msize, @nsize)
3085
+ (1..@msize).to_a.each{|x|
3086
+ (1..@nsize).to_a.each{|y|
3087
+ cmd += "a.at_#{x}_#{y}= self.at_#{x}_#{y} * nm;"
3088
+ }
3089
+ }
3090
+ end
3091
+ eval(cmd)
3092
+ return a
3093
+ end
3094
+ def <=>(o)
3095
+ r = -1 if self.size < o.size
3096
+ r = 0 if self.size == o.size
3097
+ r = 1 if self.size > o.size
3098
+ r
3099
+ end
3100
+ def ==(o)
3101
+ t = []
3102
+ if self.msize == o.msize and self.nsize == o.nsize then
3103
+ m = (1..self.msize)
3104
+ n = (1..self.nsize)
3105
+ for x in m
3106
+ for y in n
3107
+ self.send("at_#{x}_#{y}") == o.send("at_#{x}_#{y}") ? t << true : t << false
3108
+ end
3109
+ end
3110
+ end
3111
+ t.include?(false) ? false : true
3112
+ end
3113
+ def each
3114
+ m = (1..self.msize)
3115
+ n = (1..self.nsize)
3116
+ for x in m
3117
+ for y in n
3118
+ #p ".at_#{x}_#{y}"
3119
+ yield self.send("at_#{x}_#{y}")
3120
+ end
3121
+ end
3122
+ end
3123
+ def column_vectors
3124
+ #each_m
3125
+ end
3126
+ def row_vectors
3127
+ r = []
3128
+ # self.each_n{||}
3129
+ end
3130
+ def each_m
3131
+ m = (1..self.msize)
3132
+ n = (1..self.nsize)
3133
+ for x in m
3134
+ r = Matrix(1,self.nsize)
3135
+ for y in n
3136
+ r.send("at_1_#{y}=",self.send("at_#{x}_#{y}"))
3137
+ end
3138
+ yield r
3139
+ end
3140
+ end
3141
+ def each_n
3142
+ m = (1..self.msize)
3143
+ n = (1..self.nsize)
3144
+ for x in n
3145
+ r = Matrix(self.msize,1)
3146
+ for y in m
3147
+ r.send("at_#{y}_1=",self.send("at_#{y}_#{x}"))
3148
+ end
3149
+ yield r
3150
+ end
3151
+ end
2820
3152
  def to_s
2821
3153
  t = ""
2822
3154
  (1..@nsize).to_a.each{|a|
@@ -2987,16 +3319,29 @@ tu += "\na\n}"
2987
3319
  return a
2988
3320
  end
2989
3321
  def to_s
2990
- tmp = "{"
2991
- c = 0
2992
- indicies.each{|q|
2993
- p = "at#{q}"
2994
- tmp << "'#{p}' => '#{send(p)}'" if c == 0
2995
- tmp << ",'#{p}' => '#{send(p)}'" unless c==0
2996
- c += 1
3322
+ t = ""
3323
+ page = 4
3324
+ (1..@order.at(0)).to_a.each{|a|
3325
+ t << "|"
3326
+ (1..@order.at(1)).to_a.each{|b|
3327
+ t << eval("at_#{a}_#{b}_#{page}").to_s
3328
+ unless b == @order.at(1) then t << 9 end
3329
+ }
3330
+ t << "|"
3331
+ unless a == @order.at(1) then t << 10 end
2997
3332
  }
2998
- tmp << "}"
2999
- return tmp
3333
+ t
3334
+ end
3335
+ def to_csv
3336
+ t = ""
3337
+ (1..@order.a(0)).to_a.each{|a|
3338
+ (1..@order.at(1)).to_a.each{|b|
3339
+ t << eval("at_#{a}_#{b}_1").to_s
3340
+ unless b == @order.at(1) then t << 44 end
3341
+ }
3342
+ t << 10
3343
+ }
3344
+ t
3000
3345
  end
3001
3346
  def to_sgml
3002
3347
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: M500
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.2
4
+ version: 0.9.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - mark ingram
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-24 00:00:00.000000000 Z
11
+ date: 2015-02-16 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: ''
14
14
  email:
@@ -17,7 +17,8 @@ executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
- - lib/M500.rb
20
+ - lib/M500_containers.rb
21
+ - lib/m500.rb
21
22
  homepage:
22
23
  licenses:
23
24
  - Ruby