Class: FastMatrix::Vector

Inherits:
Data
  • Object
show all
Defined in:
lib/scalar.rb,
lib/vector/vector.rb,
lib/vector/constructors.rb,
ext/fast_matrix/vector.c

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'ext/fast_matrix/vector.c', line 48

VALUE vector_initialize(VALUE self, VALUE size)
{
  struct vector* data;
    int n = raise_rb_value_to_int(size);

    if(n <= 0)
        rb_raise(fm_eIndexError, "Size cannot be negative or zero");

  TypedData_Get_Struct(self, struct vector, &vector_type, data);

    c_vector_init(data, n);

  return self;
}

Class Method Details

.[](*elems) ⇒ Object

Creates a Vector from a list of elements.

Vector[7, 4, ...]


10
11
12
13
14
# File 'lib/vector/constructors.rb', line 10

def self.[](*elems)
  vector = new(elems.size)
  vector.each_with_index! { |_, idx| elems[idx] }
  vector
end

.basis(size, index) ⇒ Object

Returns a standard basis n-vector, where k is the index.

Vector.basis(size, index) # => Vector[0, 1, 0]


31
32
33
34
35
# File 'lib/vector/constructors.rb', line 31

def self.basis(size, index)
  result = zero(size)
  result[index] = 1
  result
end

.convert(vector) ⇒ Object

Create fast vector from standard vector



9
10
11
# File 'lib/vector/vector.rb', line 9

def self.convert(vector)
  elements(vector)
end

.elements(array, copy = true) ⇒ Object

Creates a vector from an Array. The optional argument copy exists only for compatibility with standard. The optional argument copy cannot be false, unlike standard.



21
22
23
24
# File 'lib/vector/constructors.rb', line 21

def self.elements(array, copy = true)
  check_flag_copy(copy)
  self[*array]
end

.zero(size) ⇒ Object

Return a zero vector.

Vector.zero(3) => Vector[0, 0, 0]


42
43
44
45
46
# File 'lib/vector/constructors.rb', line 42

def self.zero(size)
  result = new(size)
  result.each_with_index! { 0 }
  result
end

Instance Method Details

#*(v) ⇒ Object



294
295
296
297
298
299
300
301
302
303
304
# File 'ext/fast_matrix/vector.c', line 294

VALUE vector_multiply(VALUE self, VALUE v)
{
    if(RB_FLOAT_TYPE_P(v) || FIXNUM_P(v)
        || RB_TYPE_P(v, T_BIGNUM))
        return vector_multiply_vn(self, v);
    if(RBASIC_CLASS(v) == cMatrix)
        return vector_multiply_vm(self, v);
    if(RBASIC_CLASS(v) == cVector)
        return vector_multiply_vv(self, v);
    rb_raise(fm_eTypeError, "Invalid klass for multiply");
}

#+(value) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'ext/fast_matrix/vector.c', line 103

VALUE vector_add_with(VALUE self, VALUE value)
{
    raise_check_rbasic(value, cVector, "vector");
  struct vector* A;
    struct vector* B;
  TypedData_Get_Struct(self, struct vector, &vector_type, A);
  TypedData_Get_Struct(value, struct vector, &vector_type, B);

    if(A->n != B->n)
        rb_raise(fm_eIndexError, "Different sizes matrices");

    int n = A->n;

    struct vector* C;
    VALUE result = TypedData_Make_Struct(cVector, struct vector, &vector_type, C);

    c_vector_init(C, n);
    add_d_arrays_to_result(n, A->data, B->data, C->data);

    return result;
}

#+=(value) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'ext/fast_matrix/vector.c', line 126

VALUE vector_add_from(VALUE self, VALUE value)
{
    raise_check_rbasic(value, cVector, "vector");
  struct vector* A;
    struct vector* B;
  TypedData_Get_Struct(self, struct vector, &vector_type, A);
  TypedData_Get_Struct(value, struct vector, &vector_type, B);

    if(A->n != B->n)
        rb_raise(fm_eIndexError, "Different sizes matrices");

    int n = A->n;

    add_d_arrays_to_first(n, A->data, B->data);

    return self;
}

#-(value) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'ext/fast_matrix/vector.c', line 144

VALUE vector_sub_with(VALUE self, VALUE value)
{
    raise_check_rbasic(value, cVector, "vector");
  struct vector* A;
    struct vector* B;
  TypedData_Get_Struct(self, struct vector, &vector_type, A);
  TypedData_Get_Struct(value, struct vector, &vector_type, B);

    if(A->n != B->n)
        rb_raise(fm_eIndexError, "Different sizes matrices");

    int n = A->n;

    struct vector* C;
    VALUE result = TypedData_Make_Struct(cVector, struct vector, &vector_type, C);

    c_vector_init(C, n);
    sub_d_arrays_to_result(n, A->data, B->data, C->data);

    return result;
}

#-=(value) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'ext/fast_matrix/vector.c', line 167

VALUE vector_sub_from(VALUE self, VALUE value)
{
    raise_check_rbasic(value, cVector, "vector");
  struct vector* A;
    struct vector* B;
  TypedData_Get_Struct(self, struct vector, &vector_type, A);
  TypedData_Get_Struct(value, struct vector, &vector_type, B);

    if(A->n != B->n)
        rb_raise(fm_eIndexError, "Different sizes matrices");

    int n = A->n;

    sub_d_arrays_to_first(n, A->data, B->data);

    return self;
}

#==(other) ⇒ Object

FIXME: for compare with standard vector



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/vector/vector.rb', line 58

def ==(other)
  return eql?(other) if other.class == Vector
  return false unless i[size \[\]].all? { |x| other.respond_to? x }
  return false unless self.size == other.size

  result = true
  each_with_index do |elem, i|
    result &&= elem == other[i].to_f
  end
  result
end

#[](idx) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'ext/fast_matrix/vector.c', line 80

VALUE vector_get(VALUE self, VALUE idx)
{
    int i = raise_rb_value_to_int(idx);

  struct vector* data;
  TypedData_Get_Struct(self, struct vector, &vector_type, data);

    i = (i < 0) ? data->n + i : i;
    
    if(i < 0 || i >= data->n)
        return Qnil;

    return DBL2NUM(data->data[i]);
}

#[]=(idx, v) ⇒ Object

[]=



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'ext/fast_matrix/vector.c', line 64

VALUE vector_set(VALUE self, VALUE idx, VALUE v)
{
    int i = raise_rb_value_to_int(idx);
    double x = raise_rb_value_to_double(v);

  struct vector* data;
  TypedData_Get_Struct(self, struct vector, &vector_type, data);

    i = (i < 0) ? data->n + i : i;
    raise_check_range(i, 0, data->n);

    data->data[i] = x;
    return v;
}

#cloneObject



204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'ext/fast_matrix/vector.c', line 204

VALUE vector_copy(VALUE v)
{
  struct vector* V;
  TypedData_Get_Struct(v, struct vector, &vector_type, V);

    struct vector* R;
    VALUE result = TypedData_Make_Struct(cVector, struct vector, &vector_type, R);

    c_vector_init(R, V->n);
    copy_d_array(R->n, V->data, R->data);

    return result;
}

#coerce(other) ⇒ Object

The coerce method provides support for Ruby type coercion. This coercion mechanism is used by Ruby to handle mixed-type numeric operations: it is intended to find a compatible common type between the two operands of the operator. See also Numeric#coerce.



31
32
33
34
35
36
37
38
# File 'lib/scalar.rb', line 31

def coerce(other)
  case other
  when Numeric
    return Scalar.new(other), self
  else
    raise TypeError, "#{self.class} can't be coerced into #{other.class}"
  end
end

#convertObject

Convert to standard ruby vector.



16
17
18
# File 'lib/vector/vector.rb', line 16

def convert
  ::Vector.elements(self)
end

#eachObject

Iterate over the elements of this vector

Raises:



33
34
35
36
37
38
39
40
# File 'lib/vector/vector.rb', line 33

def each
  raise NotSupportedError unless block_given?

  (0...size).each do |i|
    yield self[i]
  end
  self
end

#each_with_indexObject



42
43
44
45
46
47
# File 'lib/vector/vector.rb', line 42

def each_with_index
  (0...size).each do |i|
    yield self[i], i
  end
  self
end

#each_with_index!Object

don’t use (Issue#1)



50
51
52
53
54
55
# File 'lib/vector/vector.rb', line 50

def each_with_index!
  (0...size).each do |i|
    self[i] = yield self[i], i
  end
  self
end

#eql?(value) ⇒ Boolean

Returns:

  • (Boolean)


185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'ext/fast_matrix/vector.c', line 185

VALUE vector_equal(VALUE self, VALUE value)
{
    if(RBASIC_CLASS(value) != cVector)
        return Qfalse;
  struct vector* A;
    struct vector* B;
  TypedData_Get_Struct(self, struct vector, &vector_type, A);
  TypedData_Get_Struct(value, struct vector, &vector_type, B);

    if(A->n != B->n)
    return Qfalse;

    int n = A->n;

    if(equal_d_arrays(n, A->data, B->data))
    return Qtrue;
  return Qfalse;
}

#sizeObject



95
96
97
98
99
100
# File 'ext/fast_matrix/vector.c', line 95

VALUE c_vector_size(VALUE self)
{
  struct vector* data;
  TypedData_Get_Struct(self, struct vector, &vector_type, data);
    return INT2NUM(data->n);
}

#to_aryObject



20
21
22
# File 'lib/vector/vector.rb', line 20

def to_ary
  Array.new(size) { |i| self[i] }
end

#to_sObject Also known as: to_str



24
25
26
# File 'lib/vector/vector.rb', line 24

def to_s
  "#{self.class}[#{to_ary.join(', ')}]"
end