Class: GSL::Tensor

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

Direct Known Subclasses

TensorComplex

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(narray) ⇒ Tensor

Returns a new instance of Tensor.



32
33
34
# File 'lib/gs2crmod/gsl_data_3d.rb', line 32

def initialize(narray)
	@narray = narray
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'lib/gs2crmod/gsl_data_3d.rb', line 70

def method_missing(meth, *args)
	result = @narray.send(meth, *args.reverse)
	if result.kind_of? NArray
		self.class.new(result)
	else
		result
	end
rescue NoMethodError
	self.class.new(NMath.send(meth, @narray))
end

Instance Attribute Details

#narrayObject (readonly)

Returns the value of attribute narray.



28
29
30
# File 'lib/gs2crmod/gsl_data_3d.rb', line 28

def narray
  @narray
end

Class Method Details

.alloc(*args) ⇒ Object



29
30
31
# File 'lib/gs2crmod/gsl_data_3d.rb', line 29

def self.alloc(*args)
	new(NArray.float(*args.reverse))
end

.method_missing(meth, *args) ⇒ Object



21
22
23
24
25
26
# File 'lib/gs2crmod/gsl_data_3d.rb', line 21

def method_missing(meth, *args)
	#ep 'calling... ', meth, args
	ans = new(NArray.send(meth, *args.reverse))
	#ep 'got', ans
	ans
end

Instance Method Details

#[](*args) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/gs2crmod/gsl_data_3d.rb', line 38

def [](*args)
	#if args.inject(true){|b,i| b and i.kind_of? Integer}
		#@narray[*args.reverse]
	#else
		#self.class.new(@narray[*args.reverse])
	#end
 	case 	ans =	@narray[*args.reverse]
	when Numeric
		ans
	else
		self.class.new(@narray[*args.reverse])
	end
		
end

#[]=(*args, value) ⇒ Object



52
53
54
55
56
# File 'lib/gs2crmod/gsl_data_3d.rb', line 52

def []=(*args, value)
#def []=(*args)
	#ep 'args', args, value
	@narray[*args.reverse] = value
end

#inspectObject



35
36
37
# File 'lib/gs2crmod/gsl_data_3d.rb', line 35

def inspect
	"GSL::Tensor.new(#{@narray.inspect})"
end

#iterate(&block) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/gs2crmod/gsl_data_3d.rb', line 80

def iterate(&block)
	shp = shape
	cumul = 1
	cumulshp = []
	for i in 1..shape.size
		cumulshp[shp.size-i] = cumul
		cumul *= shp[shp.size-i]
	end
	 #= shape.reverse.map{|dim| cumul*=(dim); cumul.to_i}.reverse
	#ep cumulshp; gets
	(cumulshp[0]*shp[0]).times do |n|
		#indexes = cumulshp.reverse.map{|cumul| rem = n%cumul; n-=rem; rem}.reverse
		indexes = cumulshp.map{|cumul| idx = (n/cumul).floor; n -= idx*cumul; idx}
		yield(*indexes)
	end
end

#iterate_row_maj(&block) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/gs2crmod/gsl_data_3d.rb', line 96

def iterate_row_maj(&block)
	shp = shape
	cumul = 1
	cumulshp = []
	for i in 0...shape.size
		cumulshp[i] = cumul
		cumul *= shp[i]
	end
	 #= shape.reverse.map{|dim| cumul*=(dim); cumul.to_i}.reverse
	#ep cumulshp; gets
	(cumulshp[-1]*shp[-1]).times do |n|
		#indexes = cumulshp.reverse.map{|cumul| rem = n%cumul; n-=rem; rem}.reverse
		indexes = cumulshp.reverse.map{|cumul| idx = (n/cumul).floor; n -= idx*cumul; idx}.reverse
		yield(*indexes)
	end
end

#reshape!(*args) ⇒ Object



60
61
62
63
# File 'lib/gs2crmod/gsl_data_3d.rb', line 60

def reshape!(*args)
	#ep 'rags', args
	@narray.reshape!(*args.reverse)
end

#shapeObject



57
58
59
# File 'lib/gs2crmod/gsl_data_3d.rb', line 57

def shape
	@narray.shape.reverse
end

#to_aObject



64
65
66
# File 'lib/gs2crmod/gsl_data_3d.rb', line 64

def to_a
	@narray.transpose(*(0...@narray.shape.size).to_a.reverse).to_a
end

#transpose(*args) ⇒ Object



67
68
69
# File 'lib/gs2crmod/gsl_data_3d.rb', line 67

def transpose(*args)
	self.class.new(@narray.transpose(*args))
end