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.



2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
# File 'lib/m500.rb', line 2931

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.



2952
2953
2954
# File 'lib/m500.rb', line 2952

def msize
  @msize
end

#nsizeObject

Returns the value of attribute nsize.



2952
2953
2954
# File 'lib/m500.rb', line 2952

def nsize
  @nsize
end

Class Method Details

.cols(cols) ⇒ Object



2902
2903
2904
2905
2906
2907
2908
2909
2910
# File 'lib/m500.rb', line 2902

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



2889
2890
2891
2892
# File 'lib/m500.rb', line 2889

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

.diagonal(v) ⇒ Object



2911
2912
2913
2914
2915
2916
2917
# File 'lib/m500.rb', line 2911

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



2921
2922
2923
# File 'lib/m500.rb', line 2921

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

.instanciate(n, m) ⇒ Object



2886
2887
2888
# File 'lib/m500.rb', line 2886

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

.rows(rows) ⇒ Object



2893
2894
2895
2896
2897
2898
2899
2900
2901
# File 'lib/m500.rb', line 2893

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



2918
2919
2920
# File 'lib/m500.rb', line 2918

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

.zero(n) ⇒ Object



2928
2929
2930
# File 'lib/m500.rb', line 2928

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

Instance Method Details

#*(nm) ⇒ Object



3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
# File 'lib/m500.rb', line 3047

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



2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
# File 'lib/m500.rb', line 2975

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



3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
# File 'lib/m500.rb', line 3011

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



3094
3095
3096
3097
3098
3099
# File 'lib/m500.rb', line 3094

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



3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
# File 'lib/m500.rb', line 3100

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



3123
3124
3125
# File 'lib/m500.rb', line 3123

def column_vectors
  #each_m
end

#eachObject



3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
# File 'lib/m500.rb', line 3113

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{||}



3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
# File 'lib/m500.rb', line 3130

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



3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
# File 'lib/m500.rb', line 3141

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)


2962
2963
2964
# File 'lib/m500.rb', line 2962

def is_tr?
  @tr
end

#og!Object



2969
2970
2971
# File 'lib/m500.rb', line 2969

def og!
  @tr =  false
end

#row_vectorsObject

each_m



3126
3127
3128
3129
# File 'lib/m500.rb', line 3126

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

#sizeObject



2972
2973
2974
# File 'lib/m500.rb', line 2972

def size
  self.msize * self.nsize
end

#to_csvObject



3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
# File 'lib/m500.rb', line 3165

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



3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
# File 'lib/m500.rb', line 3152

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



2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
# File 'lib/m500.rb', line 2875

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



2965
2966
2967
2968
# File 'lib/m500.rb', line 2965

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

#transposeObject



2953
2954
2955
2956
2957
2958
2959
2960
2961
# File 'lib/m500.rb', line 2953

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