Class: Tensor
- Inherits:
-
Object
- Object
- Tensor
- Defined in:
- lib/m500.rb
Constant Summary collapse
- @@script =
"covarient"- @@t1 =
"a"
Instance Attribute Summary collapse
-
#order ⇒ Object
readonly
Returns the value of attribute order.
-
#p ⇒ Object
readonly
Returns the value of attribute p.
-
#q ⇒ Object
readonly
Returns the value of attribute q.
-
#qorder ⇒ Object
readonly
Returns the value of attribute qorder.
-
#rank ⇒ Object
readonly
Returns the value of attribute rank.
Class Method Summary collapse
Instance Method Summary collapse
- #*(other) ⇒ Object
- #+(other) ⇒ Object
- #contra(n, *args) ⇒ Object
- #indicies ⇒ Object
-
#initialize(a, p) ⇒ Tensor
constructor
A new instance of Tensor.
- #product(&block) ⇒ Object
- #to_csv ⇒ Object
- #to_h ⇒ Object
- #to_s ⇒ Object
- #to_sgml ⇒ Object
Constructor Details
#initialize(a, p) ⇒ Tensor
Returns a new instance of Tensor.
3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 |
# File 'lib/m500.rb', line 3286 def initialize(a,p) eval(a) @tr = false @p = @@script == "covarient" ? p.length : 0 @q = @@script == "covarient" ? 0 : q.length @order = @@script == "covarient" ? p : [] @qorder = @@script == "covarient" ? [] : q @rank = @p + @q @rframe= [] end |
Instance Attribute Details
#order ⇒ Object (readonly)
Returns the value of attribute order.
3296 3297 3298 |
# File 'lib/m500.rb', line 3296 def order @order end |
#p ⇒ Object (readonly)
Returns the value of attribute p.
3296 3297 3298 |
# File 'lib/m500.rb', line 3296 def p @p end |
#q ⇒ Object (readonly)
Returns the value of attribute q.
3296 3297 3298 |
# File 'lib/m500.rb', line 3296 def q @q end |
#qorder ⇒ Object (readonly)
Returns the value of attribute qorder.
3296 3297 3298 |
# File 'lib/m500.rb', line 3296 def qorder @qorder end |
#rank ⇒ Object (readonly)
Returns the value of attribute rank.
3296 3297 3298 |
# File 'lib/m500.rb', line 3296 def rank @rank end |
Class Method Details
.builder(a, *args) ⇒ Object
3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 |
# File 'lib/m500.rb', line 3182 def Tensor.builder(a,*args) tmp = "" script = "_" if @@script == "covarient" script = "U" if @@script == "contravarient" m = [a]+args unless a.class == Array m = a if a.class == Array # assume this is an @order from + call z1 = "" z2 = "" x1, x2 = m, "INNER" m.reverse.to_a.each_index{|c| t2 = @@t1 + "#{m.length-c}" t3 = @@t1 + "#{c+1}" x1 = "#{x2}" x1 , x2 = x2, "(1..#{m.reverse.at(c)}).to_a.each{|#{t2}|\n#{x1}\n}" z1 << "#{script}\#{#{t3}}" z2 << "#{script}\\\#{#{t3}}" } t3 = "@at#{z1} = 0 \n" t4 = "def at#{z1}\n @at#{z1}\nend \n" t5 = "def at#{z1}=(a)\n @at#{z1} = a\nend \n" t6a = "def +(mn)\n if mn.order == @order then a= Tensor(@order)\n" t6b = "\nreturn a\n else\n # #raise error\n end\n end" t6 = t6a + x2.gsub("INNER"," z = \\\"a.at#{z2} = self.at#{z2} + mn.at#{z2}\\\"\n eval(z)") + t6b t7 = [x2,z1] tmp = x2.gsub("INNER","z = \"#{t3}#{t4}#{t5}\"\n instance_eval(z)\n #puts z") return tmp,m,t6,t7 end |
.instanciate(a, *args) ⇒ Object
3263 3264 3265 3266 3267 |
# File 'lib/m500.rb', line 3263 def Tensor.instanciate(a,*args) t = Tensor.builder(a,*args) t6 =t.at(2) # for information o n t6 see T::builder new(t.at(0) + "\n instance_eval(\"#{t6}\")",t.at(1)) end |
.new!(a, *args) ⇒ Object
3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 |
# File 'lib/m500.rb', line 3210 def Tensor.new!(a,*args) t1, t2 = [],[] u1, u2 = [],[] if a.kind_of?(Hash) then a.each{|x,y| x =~ %r{(_(?!_.*)(?!.*=$).*)} unless $1 == nil then t1 << $1 ; t2 << y end x =~ %r{(U(?!U.*)(?!.*=$).*)} unless $1 == nil then u1 << $1 ; u2 << y end } m,n = nil,nil o,p = [],[] t1.each{|a,b| s = a.split('_'); s.shift o << s } u1.each{|a,b| t = a.split('U'); t.shift p << t } m = Array.new(o.at(0).length,Array.new) n = Array.new(p.at(0).length,Array.new) o.each{|t| t.each_index{|h| m[h] += [t.at(h)] } } m.each_index{|i| m[i] = [m.at(i).max] } p.each{|t| t.each_index{|h| n[h] += [t.at(h)] } } n.each_index{|i| n[i] = [n.at(i).max] } tu = Tensor.instanciate(m) t1.each_index{|s| eval("tu.at#{t1.at(s)} = #{t2.at(s)}") } if n then tu.contra(n) u1.each_index{|s| eval("tu.at#{u1.at(s)} = #{u2.at(s)}") } end tu else Tensor.instanciate(a,*args) end end |
Instance Method Details
#*(other) ⇒ Object
3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 |
# File 'lib/m500.rb', line 3302 def *(other) ta1 ="a= Tensor(x+y)# @ order+other.order)\n a.contra(s+t) # @qorder+other.qorder)\n #puts a.indicies\n" t1= Tensor.builder(@order+other.order).at(3) t2= Tensor.builder(@order).at(3) t3= Tensor.builder(other.order).at(3) u1= Tensor.builder(@qorder+other.qorder).at(3) if @q+other.q <0 u2= Tensor.builder(@qorder).at(3) if @q+other.q <0 u3= Tensor.builder(other.qorder).at(3)if @q+other.q <0 tu = " Proc.new{|x,y,z,s,t| \n" + ta1 + t1.at(0).gsub("INNER"," z = \"a.at#{t1.at(1)} = self.at#{t2.at(1)} * other.at#{t3.at(1)}\"\n eval(z)") tu += "\n"+ u1.at(0).gsub("INNER"," z = \"a.at#{u1.at(1)} = self.at#{u2.at(1)} * other.at#{u3.at(1)}\"\n #eval(z)") if @q+other.q <0 tu += "\na\n}" tp = eval(tu) t = tp.call(@order, other.order,"",@qorder,other.qorder) return t.inspect end |
#+(other) ⇒ Object
3297 3298 3299 3300 3301 |
# File 'lib/m500.rb', line 3297 def +(other) t6a = "if other.order == @order && other.qorder == @qorder then a= Tensor(@order)\n" t6b = "\nreturn a\n else\n # #raise error\n end\n end" end |
#contra(n, *args) ⇒ Object
3280 3281 3282 3283 3284 3285 |
# File 'lib/m500.rb', line 3280 def contra(n,*args) @@script = "contravarient" t = Tensor.builder(n,*args) @@script = "covarient" eval(t.at(0)) end |
#indicies ⇒ Object
3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 |
# File 'lib/m500.rb', line 3268 def indicies z = self.public_methods x = [] z.each{|y| y =~ %r{(_(?!_.*)(?!methods)(?!variable.*)(?!defined.)(?!eval)(?!send)(?!at)(?!get|set)(?!a|s|h)(?!id.*)(?!.*=$)(?!.*\?$).*)} unless $1 == nil then x << $1 end y =~ %r{(U(?!U.*)(?!.*=$).*)} unless $1 == nil then x << $1 end } x.delete("_") return x end |
#product(&block) ⇒ Object
3318 3319 3320 3321 |
# File 'lib/m500.rb', line 3318 def product(&block) a = block.call return a end |
#to_csv ⇒ Object
3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 |
# File 'lib/m500.rb', line 3336 def to_csv t = "" (1..@order.a(0)).to_a.each{|a| (1..@order.at(1)).to_a.each{|b| t << eval("at_#{a}_#{b}_1").to_s unless b == @order.at(1) then t << 44 end } t << 10 } t end |
#to_h ⇒ Object
3349 3350 3351 |
# File 'lib/m500.rb', line 3349 def to_h eval(to_s) end |
#to_s ⇒ Object
3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 |
# File 'lib/m500.rb', line 3322 def to_s t = "" page = 4 (1..@order.at(0)).to_a.each{|a| t << "|" (1..@order.at(1)).to_a.each{|b| t << eval("at_#{a}_#{b}_#{page}").to_s unless b == @order.at(1) then t << 9 end } t << "|" unless a == @order.at(1) then t << 10 end } t end |
#to_sgml ⇒ Object
3347 3348 |
# File 'lib/m500.rb', line 3347 def to_sgml end |