Class: Matrix

Inherits:
Object
  • Object
show all
Includes:
Comparable, SGML
Defined in:
lib/m500.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SGML

#sgml_id, #tog_sgml_id

Constructor Details

#initialize(a, b) ⇒ Matrix

Returns a new instance of Matrix.



3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
# File 'lib/m500.rb', line 3591

def initialize(a,b)
  (1..a).to_a.each{|n|
    (1..b).to_a.each{|m|
      z = " @at_#{n}_#{m} = 0
    def at_#{n}_#{m}
    @at_#{n}_#{m}
    end
    def tr_#{m}_#{n}
    @at_#{n}_#{m}
    end
    def at_#{n}_#{m}=(a)
    @at_#{n}_#{m} = a
    end
    "
  instance_eval(z)
}
}
  @nsize = a
  @msize = b
  @tr = false
end

Instance Attribute Details

#msizeObject

Returns the value of attribute msize.



3612
3613
3614
# File 'lib/m500.rb', line 3612

def msize
  @msize
end

#nsizeObject

Returns the value of attribute nsize.



3612
3613
3614
# File 'lib/m500.rb', line 3612

def nsize
  @nsize
end

Class Method Details

.cols(cols) ⇒ Object



3562
3563
3564
3565
3566
3567
3568
3569
3570
# File 'lib/m500.rb', line 3562

def Matrix.cols(cols)
  a = instanciate(cols.length,cols.at(0).length)
  cols.each_index{|x|
    cols.at(x).each_index{|y|
      eval("a.at_#{x+1}_#{y+1}= cols.at(#{y}).at(#{x})")
    }
  }
  return a
end

.csv(a) ⇒ Object



3549
3550
3551
3552
# File 'lib/m500.rb', line 3549

def Matrix.csv(a)
  t =  "[[" << a.strip! << "]]"
  return Matrix.rows(t.gsub(/\s+/, '],['))
end

.diagonal(v) ⇒ Object



3571
3572
3573
3574
3575
3576
3577
# File 'lib/m500.rb', line 3571

def Matrix.diagonal(v)
  a = Matrix(v.length,v.length)
  v.each_index{|i|
    eval("a.at_#{i+1}_#{i+1} = v.at(i)")
  }
  return a
end

.identity(n) ⇒ Object Also known as: unit, I



3581
3582
3583
# File 'lib/m500.rb', line 3581

def Matrix.identity(n)
  Matrix.scalar(n, 1)
end

.instanciate(n, m) ⇒ Object



3546
3547
3548
# File 'lib/m500.rb', line 3546

def Matrix.instanciate(n,m)
  new(n,m)
end

.rows(rows) ⇒ Object



3553
3554
3555
3556
3557
3558
3559
3560
3561
# File 'lib/m500.rb', line 3553

def Matrix.rows(rows)
  a = instanciate(rows.length,rows.at(0).length)
  rows.each_index{|x|
    rows.at(x).each_index{|y|
      eval("a.at_#{x+1}_#{y+1}= rows.at(#{x}).at(#{y})")
    }
  }
  return a
end

.scalar(n, v) ⇒ Object



3578
3579
3580
# File 'lib/m500.rb', line 3578

def Matrix.scalar(n,v)
  Matrix.diagonal(Array.new(n).fill(v, 0, n))
end

.zero(n) ⇒ Object



3588
3589
3590
# File 'lib/m500.rb', line 3588

def Matrix.zero(n)
  Matrix.scalar(n, 0)
end

Instance Method Details

#*(nm) ⇒ Object



3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
# File 'lib/m500.rb', line 3707

def *(nm)
  a = nil
  cmd = ""
  if nm.kind_of?(Matrix) then
    if  not(nm.is_tr? or @tr) then
      if nm.msize == @msize and nm.nsize == @nsize then
        a= Matrix(@nsize, @msize)
        (1..@nsize).to_a.each{|x|
          (1..@msize).to_a.each{|y|
            cmd += "a.at_#{x}_#{y}= self.at_#{x}_#{y} * nm.at_#{x}_#{y};"
          }
        }
      else
      end
    elsif nm.is_tr? and not @tr then
      if nm.msize == @nsize and nm.nsize == @msize then
        a= Matrix(@nsize, @msize)
        (1..@nsize).to_a.each{|x|
          (1..@msize).to_a.each{|y|
            cmd += "a.at_#{x}_#{y}= self.at_#{x}_#{y} * nm.tr_#{x}_#{y};"
          }
        }
      else
      end
    elsif not nm.is_tr? and @tr then
      if nm.msize == @nsize and nm.nsize == @msize then
        a= Matrix(@msize, @nsize)
        (1..@msize).to_a.each{|x|
          (1..@nsize).to_a.each{|y|
            cmd += "a.at_#{x}_#{y}= self.tr_#{x}_#{y} + nm.at_#{x}_#{y};"
          }
        }
      else
      end
    else
    end
  elsif nm.kind_of?(Numeric)
    a= Matrix(@msize, @nsize)
    (1..@msize).to_a.each{|x|
      (1..@nsize).to_a.each{|y|
        cmd += "a.at_#{x}_#{y}= self.at_#{x}_#{y} * nm;"
      }
    }
  end
  eval(cmd)
  return a
end

#+(nm) ⇒ Object



3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
# File 'lib/m500.rb', line 3635

def +(nm)
  a = ""
  if  not(nm.is_tr? or @tr) then
    if nm.msize == @msize and nm.nsize == @nsize then
      a= Matrix(@nsize, @msize)
      (1..@nsize).to_a.each{|x|
        (1..@msize).to_a.each{|y|
          eval("a.at_#{x}_#{y}= self.at_#{x}_#{y} + nm.at_#{x}_#{y}")
        }
      }
    else
    end
  elsif nm.is_tr? and not @tr then
    if nm.msize == @nsize and nm.nsize == @msize then
      a= Matrix(@nsize, @msize)
      (1..@nsize).to_a.each{|x|
        (1..@msize).to_a.each{|y|
          eval("a.at_#{x}_#{y}= self.at_#{x}_#{y} + nm.tr_#{x}_#{y}")
        }
      }
    else
    end
  elsif not nm.is_tr? and @tr then
    if nm.msize == @nsize and nm.nsize == @msize then
      a= Matrix(@msize, @nsize)
      (1..@msize).to_a.each{|x|
        (1..@nsize).to_a.each{|y|
          eval("a.at_#{x}_#{y}= self.tr_#{x}_#{y} + nm.at_#{x}_#{y}")
        }
      }
    else
    end
  else
  end
  return a
end

#-(nm) ⇒ Object



3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
# File 'lib/m500.rb', line 3671

def -(nm)
  a = ""
  if  not(nm.is_tr? or @tr) then
    if nm.msize == @msize and nm.nsize == @nsize then
      a= Matrix(@nsize, @msize)
      (1..@nsize).to_a.each{|x|
        (1..@msize).to_a.each{|y|
          eval("a.at_#{x}_#{y}= self.at_#{x}_#{y} - nm.at_#{x}_#{y}")
        }
      }
    else
    end
  elsif nm.is_tr? and not @tr then
    if nm.msize == @nsize and nm.nsize == @msize then
      a= Matrix(@nsize, @msize)
      (1..@nsize).to_a.each{|x|
        (1..@msize).to_a.each{|y|
          eval("a.at_#{x}_#{y}= self.at_#{x}_#{y} - nm.tr_#{x}_#{y}")
        }
      }
    else
    end
  elsif not nm.is_tr? and @tr then
    if nm.msize == @nsize and nm.nsize == @msize then
      a= Matrix(@msize, @nsize)
      (1..@msize).to_a.each{|x|
        (1..@nsize).to_a.each{|y|
          eval("a.at_#{x}_#{y}= self.tr_#{x}_#{y} - nm.at_#{x}_#{y}")
        }
      }
    else
    end
  else
  end
  return a
end

#<=>(o) ⇒ Object



3754
3755
3756
3757
3758
3759
# File 'lib/m500.rb', line 3754

def <=>(o)
  r = -1 if self.size < o.size
  r = 0 if self.size == o.size
  r = 1 if self.size > o.size
  r
end

#==(o) ⇒ Object



3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
# File 'lib/m500.rb', line 3760

def ==(o)
  t = []
  if self.msize == o.msize and self.nsize == o.nsize then
    m = (1..self.msize)
    n = (1..self.nsize)
    for x in m
      for y in n
        self.send("at_#{x}_#{y}") == o.send("at_#{x}_#{y}") ? t << true : t << false
      end
    end
  end
  t.include?(false) ? false : true
end

#column_vectorsObject



3783
3784
3785
# File 'lib/m500.rb', line 3783

def column_vectors
  #each_m
end

#eachObject



3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
# File 'lib/m500.rb', line 3773

def each
  m = (1..self.msize)
  n = (1..self.nsize)
  for x in m
    for y in n
      #p  ".at_#{x}_#{y}"
      yield self.send("at_#{x}_#{y}")
    end
  end
end

#each_mObject

self.each_n{||}



3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
# File 'lib/m500.rb', line 3790

def each_m
  m = (1..self.msize)
  n = (1..self.nsize)
  for x in m
    r = Matrix(1,self.nsize)
    for y in n
      r.send("at_1_#{y}=",self.send("at_#{x}_#{y}"))
    end
    yield r
  end  
end

#each_nObject



3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
# File 'lib/m500.rb', line 3801

def each_n
  m = (1..self.msize)
  n = (1..self.nsize)
  for x in n
    r = Matrix(self.msize,1)
    for y in m
      r.send("at_#{y}_1=",self.send("at_#{y}_#{x}"))
    end
    yield r
  end  
end

#is_tr?Boolean

Returns:

  • (Boolean)


3622
3623
3624
# File 'lib/m500.rb', line 3622

def is_tr?
  @tr
end

#og!Object



3629
3630
3631
# File 'lib/m500.rb', line 3629

def og!
  @tr =  false
end

#row_vectorsObject

each_m



3786
3787
3788
3789
# File 'lib/m500.rb', line 3786

def row_vectors
  r = []
  # self.each_n{||}
end

#sizeObject



3632
3633
3634
# File 'lib/m500.rb', line 3632

def size
  self.msize * self.nsize
end

#to_csvObject



3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
# File 'lib/m500.rb', line 3825

def to_csv
  t = ""
  (1..@nsize).to_a.each{|a|
    (1..@msize).to_a.each{|b|
      t << eval("at_#{a}_#{b}").to_s
    unless b == @msize then  t << 44 end
    }
     t << 10
  }
  t
end

#to_sObject



3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
# File 'lib/m500.rb', line 3812

def to_s
  t = ""
  (1..@nsize).to_a.each{|a|
    t << "|"
    (1..@msize).to_a.each{|b|
      t << eval("at_#{a}_#{b}").to_s
      unless b == @msize then t << 9 end
    }
    t << "|"
      unless a == @nsize then t << 10 end
  }
  t
end

#to_sgmlObject



3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
# File 'lib/m500.rb', line 3535

def to_sgml
  tmp =     "<mn #{sgml_id}class='matrix'><mrow><mo>(</mo><mtable>"
  (1..@nsize).to_a.each{|a|
    (1..@msize).to_a.each{|b|
      tmp += "<mn>" + eval("at_#{a}_#{b}").to_s + "</mn>"
    }
    tmp += "</mtr>"
  }
  tmp += "</mtable><mo>)</mo></mrow></mn>"
end

#tr!Object



3625
3626
3627
3628
# File 'lib/m500.rb', line 3625

def tr!
  unless @tr then @tr = true else @tr = false end
  return self
end

#transposeObject



3613
3614
3615
3616
3617
3618
3619
3620
3621
# File 'lib/m500.rb', line 3613

def transpose
  a= Matrix(@msize, @nsize)
  (1..@msize).to_a.each{|x|
    (1..@nsize).to_a.each{|y|
      eval("a.at_#{x}_#{y}= self.tr_#{x}_#{y}")
    }
  }
  return a
end