Class: Rubynetic::Tensor

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Tensor

Returns a new instance of Tensor.

Raises:

  • (TypeError)


6
7
8
9
# File 'lib/rubynetic/tensor.rb', line 6

def initialize(data)
  raise TypeError, "Expected an array, got #{data.class}" unless data.is_a?(Array)
  @tensor = Rubynetic.tensor_from_data(data)
end

Instance Attribute Details

#tensorObject (readonly)

Returns the value of attribute tensor.



4
5
6
# File 'lib/rubynetic/tensor.rb', line 4

def tensor
  @tensor
end

Class Method Details

.ones(*shape) ⇒ Object



65
66
67
68
# File 'lib/rubynetic/tensor.rb', line 65

def self.ones(*shape)
  ones_tensor = Rubynetic.tensor_ones(shape)
  Tensor.allocate.tap { |t| t.instance_variable_set(:@tensor, ones_tensor) }
end

.rand(*shape, min: 0.0, max: 1.0) ⇒ Object



71
72
73
74
# File 'lib/rubynetic/tensor.rb', line 71

def self.rand(*shape, min: 0.0, max: 1.0)
  rand_tensor = Rubynetic.tensor_rand(*shape.map(&:to_i), min.to_f, max.to_f)  
  Tensor.allocate.tap { |t| t.instance_variable_set(:@tensor, rand_tensor) }
end

.valid_data?(data) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rubynetic/tensor.rb', line 11

def self.valid_data?(data)
  return false unless data.is_a?(Array)

  # Проверяем, содержит ли массив только числа или массивы одинаковой длины
  if data.all? { |item| item.is_a?(Numeric) }
    return true
  elsif data.all? { |item| item.is_a?(Array) && item.all? { |num| num.is_a?(Numeric) } }
    return true
  end

  false
end

.zeros(*shape) ⇒ Object



60
61
62
63
# File 'lib/rubynetic/tensor.rb', line 60

def self.zeros(*shape)
  zeros_tensor = Rubynetic.tensor_zeros(shape)
  Tensor.allocate.tap { |t| t.instance_variable_set(:@tensor, zeros_tensor) }
end

Instance Method Details

#*(other) ⇒ Object



84
85
86
# File 'lib/rubynetic/tensor.rb', line 84

def *(other)
  Tensor.allocate.tap { |t| t.instance_variable_set(:@tensor, Rubynetic.tensor_mul(@tensor, other.tensor)) }
end

#+(other) ⇒ Object



76
77
78
# File 'lib/rubynetic/tensor.rb', line 76

def +(other)
  Tensor.allocate.tap { |t| t.instance_variable_set(:@tensor, Rubynetic.tensor_add(@tensor, other.tensor)) }
end

#-(other) ⇒ Object



80
81
82
# File 'lib/rubynetic/tensor.rb', line 80

def -(other)
  Tensor.allocate.tap { |t| t.instance_variable_set(:@tensor, Rubynetic.tensor_sub(@tensor, other.tensor)) }
end

#/(other) ⇒ Object



88
89
90
# File 'lib/rubynetic/tensor.rb', line 88

def /(other)
  Tensor.allocate.tap { |t| t.instance_variable_set(:@tensor, Rubynetic.tensor_div(@tensor, other.tensor)) }
end

#dot(other) ⇒ Object



92
93
94
# File 'lib/rubynetic/tensor.rb', line 92

def dot(other)
  Tensor.allocate.tap { |t| t.instance_variable_set(:@tensor, Rubynetic.tensor_dot(@tensor, other.tensor)) }
end

#inspect(precision = 2) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rubynetic/tensor.rb', line 24

def inspect(precision = 2)
  array = Rubynetic.tensor_to_array(@tensor) rescue @tensor

  case array
  when Float, Integer
    return "tensor(#{format("%.#{precision}f", array)})"
  when Array
    if array.all? { |e| e.is_a?(Array) }
      # ✅ Многомерный тензор (матрица)
      formatted_values = array.map do |row|
        "  [" + row.map { |v| format("%.#{precision}f", v) }.join(', ') + "]"
      end
      return "tensor([\n" + formatted_values.join("\n") + "\n])"
    else
      # ✅ Одномерный тензор (вектор)
      formatted_values = array.map { |v| format("%.#{precision}f", v) }
      return "tensor([" + formatted_values.join(', ') + "])"
    end
  else
    return "tensor(#{array})"
  end
end


52
53
54
# File 'lib/rubynetic/tensor.rb', line 52

def print
  puts inspect
end

#shapeObject



56
57
58
# File 'lib/rubynetic/tensor.rb', line 56

def shape
  Rubynetic.tensor_shape(@tensor)  # ✅ Получаем shape тензора через C++ API
end

#to_sObject



48
49
50
# File 'lib/rubynetic/tensor.rb', line 48

def to_s
  inspect
end