fast_matrix 0.1.66 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.dockerignore +0 -1
- data/.travis.yml +2 -1
- data/README.md +3 -8
- data/ext/fast_matrix/{c_array_opeartions.c → Helper/c_array_opeartions.c} +39 -1
- data/ext/fast_matrix/{c_array_operations.h → Helper/c_array_operations.h} +6 -1
- data/ext/fast_matrix/{errors.c → Helper/errors.c} +8 -2
- data/ext/fast_matrix/{errors.h → Helper/errors.h} +3 -1
- data/ext/fast_matrix/Matrix/c_matrix.c +606 -0
- data/ext/fast_matrix/Matrix/c_matrix.h +49 -0
- data/ext/fast_matrix/Matrix/errors.h +28 -0
- data/ext/fast_matrix/Matrix/helper.h +27 -0
- data/ext/fast_matrix/Matrix/matrix.c +633 -0
- data/ext/fast_matrix/Matrix/matrix.h +11 -0
- data/ext/fast_matrix/Vector/c_vector.c +97 -0
- data/ext/fast_matrix/Vector/c_vector.h +24 -0
- data/ext/fast_matrix/Vector/errors.h +14 -0
- data/ext/fast_matrix/Vector/helper.h +26 -0
- data/ext/fast_matrix/Vector/vector.c +476 -0
- data/ext/fast_matrix/{vector.h → Vector/vector.h} +0 -10
- data/ext/fast_matrix/c_include.c +8 -0
- data/ext/fast_matrix/fast_matrix.h +3 -3
- data/fast_matrix.gemspec +1 -1
- data/lib/fast_matrix/version.rb +1 -1
- data/lib/matrix/constructors.rb +0 -63
- data/lib/matrix/matrix.rb +105 -4
- data/lib/vector/constructors.rb +5 -4
- data/lib/vector/vector.rb +29 -0
- metadata +21 -12
- data/ext/fast_matrix/matrix.c +0 -678
- data/ext/fast_matrix/matrix.h +0 -28
- data/ext/fast_matrix/vector.c +0 -277
data/ext/fast_matrix/matrix.h
DELETED
@@ -1,28 +0,0 @@
|
|
1
|
-
#ifndef FAST_MATRIX_MATRIX_H
|
2
|
-
#define FAST_MATRIX_MATRIX_H 1
|
3
|
-
|
4
|
-
#include "ruby.h"
|
5
|
-
|
6
|
-
extern VALUE cMatrix;
|
7
|
-
extern const rb_data_type_t matrix_type;
|
8
|
-
|
9
|
-
// matrix
|
10
|
-
// m --->
|
11
|
-
// [ 0, 1, .., m-1]
|
12
|
-
// n [ m, m+1, .., 2m-1]
|
13
|
-
// | [2m, 2m+1, .., 3m-1]
|
14
|
-
// V [ . . . . .
|
15
|
-
// . . . . nm-1]
|
16
|
-
struct matrix
|
17
|
-
{
|
18
|
-
int m;
|
19
|
-
int n;
|
20
|
-
|
21
|
-
double* data;
|
22
|
-
};
|
23
|
-
|
24
|
-
void c_matrix_init(struct matrix* mtr, int m, int n);
|
25
|
-
|
26
|
-
void init_fm_matrix();
|
27
|
-
|
28
|
-
#endif /* FAST_MATRIX_MATRIX_H */
|
data/ext/fast_matrix/vector.c
DELETED
@@ -1,277 +0,0 @@
|
|
1
|
-
#include "vector.h"
|
2
|
-
#include "c_array_operations.h"
|
3
|
-
#include "errors.h"
|
4
|
-
#include "matrix.h"
|
5
|
-
|
6
|
-
VALUE cVector;
|
7
|
-
|
8
|
-
void vector_free(void* data);
|
9
|
-
size_t vector_size(const void* data);
|
10
|
-
|
11
|
-
const rb_data_type_t vector_type =
|
12
|
-
{
|
13
|
-
.wrap_struct_name = "vector",
|
14
|
-
.function =
|
15
|
-
{
|
16
|
-
.dmark = NULL,
|
17
|
-
.dfree = vector_free,
|
18
|
-
.dsize = vector_size,
|
19
|
-
},
|
20
|
-
.data = NULL,
|
21
|
-
.flags = RUBY_TYPED_FREE_IMMEDIATELY,
|
22
|
-
};
|
23
|
-
|
24
|
-
void vector_free(void* data)
|
25
|
-
{
|
26
|
-
free(((*(struct vector*)data)).data);
|
27
|
-
free(data);
|
28
|
-
}
|
29
|
-
|
30
|
-
size_t vector_size(const void* data)
|
31
|
-
{
|
32
|
-
return sizeof(struct vector);
|
33
|
-
}
|
34
|
-
|
35
|
-
VALUE vector_alloc(VALUE self)
|
36
|
-
{
|
37
|
-
struct vector* vct = malloc(sizeof(struct vector));
|
38
|
-
vct->data = NULL;
|
39
|
-
return TypedData_Wrap_Struct(self, &vector_type, vct);
|
40
|
-
}
|
41
|
-
|
42
|
-
void c_vector_init(struct vector* vect, int n)
|
43
|
-
{
|
44
|
-
vect->n = n;
|
45
|
-
vect->data = malloc(n * sizeof(double));
|
46
|
-
}
|
47
|
-
|
48
|
-
VALUE vector_initialize(VALUE self, VALUE size)
|
49
|
-
{
|
50
|
-
struct vector* data;
|
51
|
-
int n = raise_rb_value_to_int(size);
|
52
|
-
|
53
|
-
if(n <= 0)
|
54
|
-
rb_raise(fm_eIndexError, "Size cannot be negative or zero");
|
55
|
-
|
56
|
-
TypedData_Get_Struct(self, struct vector, &vector_type, data);
|
57
|
-
|
58
|
-
c_vector_init(data, n);
|
59
|
-
|
60
|
-
return self;
|
61
|
-
}
|
62
|
-
|
63
|
-
// []=
|
64
|
-
VALUE vector_set(VALUE self, VALUE idx, VALUE v)
|
65
|
-
{
|
66
|
-
int i = raise_rb_value_to_int(idx);
|
67
|
-
double x = raise_rb_value_to_double(v);
|
68
|
-
|
69
|
-
struct vector* data;
|
70
|
-
TypedData_Get_Struct(self, struct vector, &vector_type, data);
|
71
|
-
|
72
|
-
i = (i < 0) ? data->n + i : i;
|
73
|
-
raise_check_range(i, 0, data->n);
|
74
|
-
|
75
|
-
data->data[i] = x;
|
76
|
-
return v;
|
77
|
-
}
|
78
|
-
|
79
|
-
// []
|
80
|
-
VALUE vector_get(VALUE self, VALUE idx)
|
81
|
-
{
|
82
|
-
int i = raise_rb_value_to_int(idx);
|
83
|
-
|
84
|
-
struct vector* data;
|
85
|
-
TypedData_Get_Struct(self, struct vector, &vector_type, data);
|
86
|
-
|
87
|
-
i = (i < 0) ? data->n + i : i;
|
88
|
-
|
89
|
-
if(i < 0 || i >= data->n)
|
90
|
-
return Qnil;
|
91
|
-
|
92
|
-
return DBL2NUM(data->data[i]);
|
93
|
-
}
|
94
|
-
|
95
|
-
VALUE c_vector_size(VALUE self)
|
96
|
-
{
|
97
|
-
struct vector* data;
|
98
|
-
TypedData_Get_Struct(self, struct vector, &vector_type, data);
|
99
|
-
return INT2NUM(data->n);
|
100
|
-
}
|
101
|
-
|
102
|
-
|
103
|
-
VALUE vector_add_with(VALUE self, VALUE value)
|
104
|
-
{
|
105
|
-
struct vector* A;
|
106
|
-
struct vector* B;
|
107
|
-
TypedData_Get_Struct(self, struct vector, &vector_type, A);
|
108
|
-
TypedData_Get_Struct(value, struct vector, &vector_type, B);
|
109
|
-
|
110
|
-
if(A->n != B->n)
|
111
|
-
rb_raise(fm_eIndexError, "Different sizes matrices");
|
112
|
-
|
113
|
-
int n = A->n;
|
114
|
-
|
115
|
-
struct vector* C;
|
116
|
-
VALUE result = TypedData_Make_Struct(cVector, struct vector, &vector_type, C);
|
117
|
-
|
118
|
-
c_vector_init(C, n);
|
119
|
-
add_d_arrays_to_result(n, A->data, B->data, C->data);
|
120
|
-
|
121
|
-
return result;
|
122
|
-
}
|
123
|
-
|
124
|
-
|
125
|
-
VALUE vector_add_from(VALUE self, VALUE value)
|
126
|
-
{
|
127
|
-
struct vector* A;
|
128
|
-
struct vector* B;
|
129
|
-
TypedData_Get_Struct(self, struct vector, &vector_type, A);
|
130
|
-
TypedData_Get_Struct(value, struct vector, &vector_type, B);
|
131
|
-
|
132
|
-
if(A->n != B->n)
|
133
|
-
rb_raise(fm_eIndexError, "Different sizes matrices");
|
134
|
-
|
135
|
-
int n = A->n;
|
136
|
-
|
137
|
-
add_d_arrays_to_first(n, A->data, B->data);
|
138
|
-
|
139
|
-
return self;
|
140
|
-
}
|
141
|
-
|
142
|
-
VALUE vector_equal(VALUE self, VALUE value)
|
143
|
-
{
|
144
|
-
struct vector* A;
|
145
|
-
struct vector* B;
|
146
|
-
TypedData_Get_Struct(self, struct vector, &vector_type, A);
|
147
|
-
TypedData_Get_Struct(value, struct vector, &vector_type, B);
|
148
|
-
|
149
|
-
if(A->n != B->n)
|
150
|
-
return Qfalse;
|
151
|
-
|
152
|
-
int n = A->n;
|
153
|
-
|
154
|
-
if(equal_d_arrays(n, A->data, B->data))
|
155
|
-
return Qtrue;
|
156
|
-
return Qfalse;
|
157
|
-
}
|
158
|
-
|
159
|
-
VALUE vector_copy(VALUE v)
|
160
|
-
{
|
161
|
-
struct vector* V;
|
162
|
-
TypedData_Get_Struct(v, struct vector, &vector_type, V);
|
163
|
-
|
164
|
-
struct vector* R;
|
165
|
-
VALUE result = TypedData_Make_Struct(cVector, struct vector, &vector_type, R);
|
166
|
-
|
167
|
-
c_vector_init(R, V->n);
|
168
|
-
copy_d_array(R->n, V->data, R->data);
|
169
|
-
|
170
|
-
return result;
|
171
|
-
}
|
172
|
-
|
173
|
-
// V - vector n
|
174
|
-
// M - matrix m x 1
|
175
|
-
// R - matrix m x n
|
176
|
-
void c_vector_matrix_multiply(int n, int m, const double* V, const double* M, double* R)
|
177
|
-
{
|
178
|
-
fill_d_array(m * n, R, 0);
|
179
|
-
|
180
|
-
for(int j = 0; j < n; ++j)
|
181
|
-
{
|
182
|
-
double* p_r = R + m * j;
|
183
|
-
double d_v = V[j];
|
184
|
-
|
185
|
-
for(int i = 0; i < m; ++i)
|
186
|
-
p_r[i] += d_v * M[i];
|
187
|
-
}
|
188
|
-
}
|
189
|
-
|
190
|
-
VALUE vector_multiply_vm(VALUE self, VALUE other)
|
191
|
-
{
|
192
|
-
struct vector* V;
|
193
|
-
struct matrix* M;
|
194
|
-
TypedData_Get_Struct(self, struct vector, &vector_type, V);
|
195
|
-
TypedData_Get_Struct(other, struct matrix, &matrix_type, M);
|
196
|
-
|
197
|
-
if(M->n != 1)
|
198
|
-
rb_raise(fm_eIndexError, "Number of rows must be 1");
|
199
|
-
|
200
|
-
int m = M->m;
|
201
|
-
int n = V->n;
|
202
|
-
|
203
|
-
struct matrix* R;
|
204
|
-
VALUE result = TypedData_Make_Struct(cMatrix, struct matrix, &matrix_type, R);
|
205
|
-
|
206
|
-
c_matrix_init(R, m, n);
|
207
|
-
c_vector_matrix_multiply(n, m, V->data, M->data, R->data);
|
208
|
-
|
209
|
-
return result;
|
210
|
-
}
|
211
|
-
|
212
|
-
VALUE vector_multiply_vn(VALUE self, VALUE value)
|
213
|
-
{
|
214
|
-
struct vector* A;
|
215
|
-
TypedData_Get_Struct(self, struct vector, &vector_type, A);
|
216
|
-
|
217
|
-
double d = NUM2DBL(value);
|
218
|
-
|
219
|
-
struct vector* R;
|
220
|
-
VALUE result = TypedData_Make_Struct(cVector, struct vector, &vector_type, R);
|
221
|
-
|
222
|
-
c_vector_init(R, A->n);
|
223
|
-
copy_d_array(R->n, A->data, R->data);
|
224
|
-
multiply_d_array(R->n, R->data, d);
|
225
|
-
|
226
|
-
return result;
|
227
|
-
}
|
228
|
-
|
229
|
-
VALUE vector_multiply_vv(VALUE self, VALUE other)
|
230
|
-
{
|
231
|
-
struct vector* A;
|
232
|
-
struct vector* B;
|
233
|
-
TypedData_Get_Struct(self, struct vector, &vector_type, A);
|
234
|
-
TypedData_Get_Struct(other, struct vector, &vector_type, B);
|
235
|
-
|
236
|
-
if(B->n != 1)
|
237
|
-
rb_raise(fm_eIndexError, "Length of vector must be equal to 1");
|
238
|
-
|
239
|
-
struct vector* R;
|
240
|
-
VALUE result = TypedData_Make_Struct(cVector, struct vector, &vector_type, R);
|
241
|
-
|
242
|
-
c_vector_init(R, A->n);
|
243
|
-
copy_d_array(A->n, A->data, R->data);
|
244
|
-
multiply_d_array(R->n, R->data, B->data[0]);
|
245
|
-
|
246
|
-
return result;
|
247
|
-
}
|
248
|
-
|
249
|
-
VALUE vector_multiply(VALUE self, VALUE v)
|
250
|
-
{
|
251
|
-
if(RB_FLOAT_TYPE_P(v) || FIXNUM_P(v)
|
252
|
-
|| RB_TYPE_P(v, T_BIGNUM))
|
253
|
-
return vector_multiply_vn(self, v);
|
254
|
-
if(RBASIC_CLASS(v) == cMatrix)
|
255
|
-
return vector_multiply_vm(self, v);
|
256
|
-
if(RBASIC_CLASS(v) == cVector)
|
257
|
-
return vector_multiply_vv(self, v);
|
258
|
-
rb_raise(fm_eTypeError, "Invalid klass for multiply");
|
259
|
-
}
|
260
|
-
|
261
|
-
void init_fm_vector()
|
262
|
-
{
|
263
|
-
VALUE mod = rb_define_module("FastMatrix");
|
264
|
-
cVector = rb_define_class_under(mod, "Vector", rb_cData);
|
265
|
-
|
266
|
-
rb_define_alloc_func(cVector, vector_alloc);
|
267
|
-
|
268
|
-
rb_define_method(cVector, "initialize", vector_initialize, 1);
|
269
|
-
rb_define_method(cVector, "[]", vector_get, 1);
|
270
|
-
rb_define_method(cVector, "[]=", vector_set, 2);
|
271
|
-
rb_define_method(cVector, "size", c_vector_size, 0);
|
272
|
-
rb_define_method(cVector, "+", vector_add_with, 1);
|
273
|
-
rb_define_method(cVector, "+=", vector_add_from, 1);
|
274
|
-
rb_define_method(cVector, "eql?", vector_equal, 1);
|
275
|
-
rb_define_method(cVector, "clone", vector_copy, 0);
|
276
|
-
rb_define_method(cVector, "*", vector_multiply, 1);
|
277
|
-
}
|