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.



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

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.



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

def msize
  @msize
end

#nsizeObject

Returns the value of attribute nsize.



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

def nsize
  @nsize
end

Class Method Details

.cols(cols) ⇒ Object



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

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



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

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

.diagonal(v) ⇒ Object



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

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



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

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

.instanciate(n, m) ⇒ Object



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

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

.rows(rows) ⇒ Object



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

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



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

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

.zero(n) ⇒ Object



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

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

Instance Method Details

#*(nm) ⇒ Object



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
3094
# File 'lib/m500.rb', line 3048

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



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
3011
# File 'lib/m500.rb', line 2976

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



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
3047
# File 'lib/m500.rb', line 3012

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



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

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



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

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



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

def column_vectors
  #each_m
end

#eachObject



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

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



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

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



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

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)


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

def is_tr?
  @tr
end

#og!Object



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

def og!
  @tr =  false
end

#row_vectorsObject

each_m



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

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

#sizeObject



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

def size
  self.msize * self.nsize
end

#to_csvObject



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

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



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

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



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

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



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

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

#transposeObject



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

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