class SparseTensor

A simple sparse tensor

Attributes

rank[R]

class Key < Array

def ==

end

shape[R]

class Key < Array

def ==

end

Public Class Methods

diagonal(rank, array) click to toggle source

Create a new diagonal tensor from an array. E.g. if rank was 2, then

tensor[0,0] = array[0]
tensor[1,1] = array[1]

Etc.

# File lib/graphkit.rb, line 66
def self.diagonal(rank, array)
        tensor = new(rank)
        for index in 0...array.size
                tensor[[index] * rank] = array[index]
        end
        tensor
end
from_hash(hash) click to toggle source
# File lib/graphkit.rb, line 161
def self.from_hash(hash)
        st = new(hash.keys[0].size)
        hash.each{|k,v| st[k] = v}
        st
end
new(rank = 2) click to toggle source

Create a new tensor.

Calls superclass method
# File lib/graphkit.rb, line 55
def initialize(rank = 2)
        @rank = rank
        @shape = [0]*rank
        super()
end

Public Instance Methods

+(other) click to toggle source
# File lib/graphkit.rb, line 135
def +(other)
        scalar_binary(other){|a, b| a + b}
end
-(other) click to toggle source
# File lib/graphkit.rb, line 138
def -(other)
        scalar_binary(other){|a, b| a - b}
end
[](*args) click to toggle source

Access an element of the tensor. E.g. for a rank 2 tensor

a = tensor[1,3]
Calls superclass method
# File lib/graphkit.rb, line 78
        def [](*args)
                args = args[0] if args.size == 1 and not args.size == @rank and args[0].size == @rank
#               p args

                raise RankError.new("Rank is #@rank, not #{args.size}") unless args.size == @rank
                return nil unless keys.include? args
                #if self.keys.include?(args) or @default_val == nil
                        #eputs args.pretty_inspect
                        #eputs self.pretty_inspect
                        #eputs self.class, self.class.ancestors
                        super(args)
                #else
                        #return @default_val
                #end

        end
[]=(*args) click to toggle source

Set an element of the tensor. E.g. for a rank 2 tensor

tensor[1,3] = a_variable
Calls superclass method
# File lib/graphkit.rb, line 99
def []=(*args)
        value = args.pop
        args = args[0] if args.size == 1 and args[0].size == @rank
        raise RankError.new("Rank is #@rank, not #{args.size}") unless args.size == @rank
        args.each_with_index do |arg, index|
                @shape[index] = [@shape[index], arg + 1].max
        end
        super(args, value)
end
alter!() { |self| ... } click to toggle source
# File lib/graphkit.rb, line 156
def alter!(&block)
        self.keys.each do |k|
                self[k] = yield(self[k])
        end
end
inspect() click to toggle source
# File lib/graphkit.rb, line 166
def inspect
        "SparseTensor.from_hash(#{super})"
end
max(&block) click to toggle source

Find the maximum element of the tensor. See Enumerable#max.

# File lib/graphkit.rb, line 144
def max(&block)
        return self.values.max(&block)
end
min(&block) click to toggle source

Find the minimum element of the tensor. See Enumerable#max.

# File lib/graphkit.rb, line 150
def min(&block)
        return self.values.min(&block)
end
scalar_binary(other) { |self, other| ... } click to toggle source

Perform some action involving all the elements of this tensor and another.

E.g.

tensor_1.scalar_binary(tensor_2) do |element_1, element_2|
        element_1 + element_2
end

will add every element of tensor_1 to every corresponding element of tensor_2.

# File lib/graphkit.rb, line 120
def scalar_binary(other, &block)
        raise ArgumentError unless other.class == self.class
        raise RankError.new("Different ranks: #@rank, #{other.rank}") unless other.rank == @rank

        new = self.class.new(@rank)
        self.keys.each do |key|
                if other[key]
                        new[key] = yield(self[key], other[key])
                else
                        new[key] = self[key]
                end
        end
        (other.keys - self.keys).each{|key| new[key] = other[key]}
        new
end