Class: Tensor

Inherits:
Object
  • Object
show all
Defined in:
lib/m500.rb

Constant Summary collapse

@@script =
"covarient"
@@t1 =
"a"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(a, p) ⇒ Tensor

Returns a new instance of Tensor.



3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
# File 'lib/m500.rb', line 3945

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

#orderObject (readonly)

Returns the value of attribute order.



3955
3956
3957
# File 'lib/m500.rb', line 3955

def order
  @order
end

#pObject (readonly)

Returns the value of attribute p.



3955
3956
3957
# File 'lib/m500.rb', line 3955

def p
  @p
end

#qObject (readonly)

Returns the value of attribute q.



3955
3956
3957
# File 'lib/m500.rb', line 3955

def q
  @q
end

#qorderObject (readonly)

Returns the value of attribute qorder.



3955
3956
3957
# File 'lib/m500.rb', line 3955

def qorder
  @qorder
end

#rankObject (readonly)

Returns the value of attribute rank.



3955
3956
3957
# File 'lib/m500.rb', line 3955

def rank
  @rank
end

Class Method Details

.builder(a, *args) ⇒ Object



3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
# File 'lib/m500.rb', line 3841

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



3922
3923
3924
3925
3926
# File 'lib/m500.rb', line 3922

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



3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
# File 'lib/m500.rb', line 3869

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



3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
# File 'lib/m500.rb', line 3961

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



3956
3957
3958
3959
3960
# File 'lib/m500.rb', line 3956

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



3939
3940
3941
3942
3943
3944
# File 'lib/m500.rb', line 3939

def contra(n,*args)
  @@script = "contravarient"
  t = Tensor.builder(n,*args)
  @@script = "covarient"
  eval(t.at(0))
end

#indiciesObject



3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
# File 'lib/m500.rb', line 3927

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



3977
3978
3979
3980
# File 'lib/m500.rb', line 3977

def product(&block)
  a = block.call
  return a
end

#to_csvObject



3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
# File 'lib/m500.rb', line 3995

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_hObject



4008
4009
4010
# File 'lib/m500.rb', line 4008

def to_h
  eval(to_s)
end

#to_sObject



3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
# File 'lib/m500.rb', line 3981

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_sgmlObject



4006
4007
# File 'lib/m500.rb', line 4006

def to_sgml
end