fast_matrix 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3980488dbfc0f63bc9c5581eb26026e79e6b1a7e7a0215e610c19e0b2c403341
4
+ data.tar.gz: 19c9066e3d2dcd0200a30e17eeeb757f62622cdf004e4df5610c7b2e80b67da8
5
+ SHA512:
6
+ metadata.gz: f8ad8b5b92b9deaee09e3dff8c1834459711fda976b83b26347ae154ad34bdebdf519228f121ada1a706d3beafe332ecc2259cbefaf77f4054f52f7b3db1fea0
7
+ data.tar.gz: feea35ee89d7510a221ba19e7c232f178e885b25afa2e051f3578dbcd6da3891e60472b3c5267ab2ec7450d509140bfaffaf97c1b1f8065cd01825a8148a034d
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ .idea/
10
+ *.gem
11
+ Gemfile.lock
12
+ *.so
13
+ *.md
14
+ *Dockerfile
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ *.bundle
10
+ *.so
11
+ *.o
12
+ *.a
13
+ mkmf.log
14
+ .idea/
15
+ Gemfile.lock
@@ -0,0 +1,12 @@
1
+ language: ruby
2
+ cache: bundler
3
+ rvm:
4
+ - 2.6.5
5
+ before_install: gem install bundler -v 2.0.2
6
+ deploy:
7
+ provider: rubygems
8
+ api_key: $API_KEY
9
+ gem: fast_matrix
10
+ on:
11
+ tags: true
12
+ repo: mmcs-ruby/fast_matrix
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at wolwerine@yandex.ru. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
@@ -0,0 +1,18 @@
1
+ FROM ruby:2.6.5
2
+
3
+ RUN gem install bundler -v 2.0.2
4
+
5
+ WORKDIR /usr/src/app
6
+
7
+ COPY Gemfile fast_matrix.gemspec ./
8
+ COPY lib/fast_matrix ./lib/fast_matrix
9
+ RUN bundle install
10
+
11
+ COPY Rakefile ./
12
+ COPY ext/ ./ext
13
+ RUN rake compile
14
+
15
+ COPY . .
16
+ RUN rake test
17
+
18
+ CMD /bin/bash
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in fast_matrix.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 wolwer1ne
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,43 @@
1
+ [![Build Status](https://travis-ci.org/mmcs-ruby/fast_matrix.svg?branch=master)](https://travis-ci.org/mmcs-ruby/fast_matrix)
2
+
3
+ # FastMatrix
4
+
5
+ Ruby wrapper around C matrices implementation written as exercise by two MMCS students.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'fast_matrix'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install fast_matrix
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/mmcs-ruby/fast_matrix. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the FastMatrix project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/mmcs-ruby/fast_matrix/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,18 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ require "rake/extensiontask"
11
+
12
+ task :build => :compile
13
+
14
+ Rake::ExtensionTask.new("fast_matrix") do |ext|
15
+ ext.lib_dir = "lib/fast_matrix"
16
+ end
17
+
18
+ task :default => [:clobber, :compile, :test]
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "fast_matrix"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,31 @@
1
+ #include "c_array_operations.h"
2
+
3
+ void fill_d_array(int len, double* a, double v)
4
+ {
5
+ for(int i = 0; i < len; ++i)
6
+ a[i] = v;
7
+ }
8
+
9
+ void multiply_d_array(int len, double* a, double v)
10
+ {
11
+ for(int i = 0; i < len; ++i)
12
+ a[i] *= v;
13
+ }
14
+
15
+ void copy_d_array(int len, const double* input, double* output)
16
+ {
17
+ for(int i = 0; i < len; ++i)
18
+ output[i] = input[i];
19
+ }
20
+
21
+ void add_d_arrays_to_result(int len, const double* a1, const double* a2, double* result)
22
+ {
23
+ for(int i = 0; i < len; ++i)
24
+ result[i] = a1[i] + a2[i];
25
+ }
26
+
27
+ void add_d_arrays_to_first(int len, double* sum, const double* added)
28
+ {
29
+ for(int i = 0; i < len; ++i)
30
+ sum[i] += added[i];
31
+ }
@@ -0,0 +1,10 @@
1
+ #ifndef C_ARRAY_OPERATIONS
2
+ #define C_ARRAY_OPERATIONS
3
+
4
+ void fill_d_array(int len, double* a, double v);
5
+ void multiply_d_array(int len, double* a, double v);
6
+ void copy_d_array(int len, const double* input, double* output);
7
+ void add_d_arrays_to_result(int len, const double* a1, const double* a2, double* result);
8
+ void add_d_arrays_to_first(int len, double* sum, const double* added);
9
+
10
+ #endif /*C_ARRAY_OPERATIONS*/
@@ -0,0 +1,3 @@
1
+ require "mkmf"
2
+
3
+ create_makefile("fast_matrix/fast_matrix")
@@ -0,0 +1,8 @@
1
+ #include "fast_matrix.h"
2
+ #include <stdio.h>
3
+
4
+
5
+ void Init_fast_matrix()
6
+ {
7
+ init_fm_matrix();
8
+ }
@@ -0,0 +1,8 @@
1
+ #ifndef FAST_MATRIX_H
2
+ #define FAST_MATRIX_H 1
3
+
4
+ #include "matrix.h"
5
+
6
+ void Init_fast_matrix();
7
+
8
+ #endif /* FAST_MATRIX_H */
@@ -0,0 +1,289 @@
1
+ #include "matrix.h"
2
+ #include "c_array_operations.h"
3
+
4
+ void matrix_free(void* data)
5
+ {
6
+ free(((*(struct matrix*)data)).data);
7
+ free(data);
8
+ }
9
+
10
+ size_t matrix_size(const void* data)
11
+ {
12
+ return sizeof(struct matrix);
13
+ }
14
+
15
+ VALUE matrix_alloc(VALUE self)
16
+ {
17
+ struct matrix* mtx = malloc(sizeof(struct matrix));
18
+
19
+ return TypedData_Wrap_Struct(self, &matrix_type, mtx);
20
+ }
21
+
22
+ double raise_rb_value_to_double(VALUE v)
23
+ {
24
+ if(RB_FLOAT_TYPE_P(v) || FIXNUM_P(v)
25
+ || RB_TYPE_P(v, T_BIGNUM))
26
+ return NUM2DBL(v);
27
+
28
+ rb_raise(matrix_eTypeError, "Value is not number");
29
+ return 0;
30
+ }
31
+
32
+ int raise_rb_value_to_int(VALUE v)
33
+ {
34
+ if(FIXNUM_P(v))
35
+ return NUM2INT(v);
36
+
37
+ rb_raise(matrix_eTypeError, "Index is not integer");
38
+ return 0;
39
+ }
40
+
41
+ void raise_check_range(int v, int min, int max)
42
+ {
43
+ if(v < min || v >= max)
44
+ rb_raise(matrix_eIndexError, "Index out of range");
45
+ }
46
+
47
+ void c_matrix_init(struct matrix* mtr, int m, int n)
48
+ {
49
+ mtr->m = m;
50
+ mtr->n = n;
51
+ mtr->data = malloc(m * n * sizeof(double));
52
+ }
53
+
54
+ VALUE matrix_initialize(VALUE self, VALUE rows_count, VALUE columns_count)
55
+ {
56
+ struct matrix* data;
57
+ int m = raise_rb_value_to_int(columns_count);
58
+ int n = raise_rb_value_to_int(rows_count);
59
+
60
+ if(m <= 0 || n <= 0)
61
+ rb_raise(matrix_eIndexError, "Size cannot be negative or zero");
62
+
63
+ TypedData_Get_Struct(self, struct matrix, &matrix_type, data);
64
+
65
+ c_matrix_init(data, m, n);
66
+
67
+ return self;
68
+ }
69
+
70
+ // []=
71
+ VALUE matrix_set(VALUE self, VALUE row, VALUE column, VALUE v)
72
+ {
73
+ int m = raise_rb_value_to_int(column);
74
+ int n = raise_rb_value_to_int(row);
75
+ double x = raise_rb_value_to_double(v);
76
+
77
+ struct matrix* data;
78
+ TypedData_Get_Struct(self, struct matrix, &matrix_type, data);
79
+
80
+ raise_check_range(m, 0, data->m);
81
+ raise_check_range(n, 0, data->n);
82
+
83
+ data->data[m + data->m * n] = x;
84
+ return v;
85
+ }
86
+
87
+ // []
88
+ VALUE matrix_get(VALUE self, VALUE row, VALUE column)
89
+ {
90
+ int m = raise_rb_value_to_int(column);
91
+ int n = raise_rb_value_to_int(row);
92
+
93
+ struct matrix* data;
94
+ TypedData_Get_Struct(self, struct matrix, &matrix_type, data);
95
+
96
+ raise_check_range(m, 0, data->m);
97
+ raise_check_range(n, 0, data->n);
98
+
99
+ return DBL2NUM(data->data[m + data->m * n]);
100
+ }
101
+
102
+ // in - matrix m x n
103
+ // out - matrix n x m
104
+ void matrix_transpose(int m, int n, const double* in, double* out)
105
+ {
106
+ for(int i = 0; i < m; ++i)
107
+ for(int j = 0; j < n; ++j)
108
+ out[j + n * i] = in[i + m * j];
109
+ }
110
+
111
+ // A - matrix k x n
112
+ // B - matrix m x k
113
+ // C - matrix m x n
114
+ void c_matrix_multiply(int n, int k, int m, const double* A, const double* B, double* C)
115
+ {
116
+
117
+ fill_d_array(m * n, C, 0);
118
+
119
+ for(int j = 0; j < n; ++j)
120
+ {
121
+ double* p_c = C + m * j;
122
+ const double* p_a = A + k * j;
123
+
124
+ for(int t = 0; t < k; ++t)
125
+ {
126
+ const double* p_b = B + m * t;
127
+ double d_a = p_a[t];
128
+ for(int i = 0; i < m; ++i)
129
+ {
130
+ p_c[i] += d_a * p_b[i];
131
+ }
132
+ }
133
+ }
134
+ }
135
+
136
+ VALUE matrix_multiply_mm(VALUE self, VALUE other)
137
+ {
138
+ struct matrix* A;
139
+ struct matrix* B;
140
+ TypedData_Get_Struct(self, struct matrix, &matrix_type, A);
141
+ TypedData_Get_Struct(other, struct matrix, &matrix_type, B);
142
+
143
+ if(A->m != B->n)
144
+ rb_raise(matrix_eIndexError, "First columns differs from second rows");
145
+
146
+ int m = B->m;
147
+ int k = A->m;
148
+ int n = A->n;
149
+
150
+ struct matrix* C;
151
+ VALUE result = TypedData_Make_Struct(cMatrix, struct matrix, &matrix_type, C);
152
+
153
+ c_matrix_init(C, m, n);
154
+ c_matrix_multiply(n, k, m, A->data, B->data, C->data);
155
+
156
+ return result;
157
+ }
158
+
159
+ VALUE matrix_multiply_mn(VALUE self, VALUE value)
160
+ {
161
+ struct matrix* A;
162
+ TypedData_Get_Struct(self, struct matrix, &matrix_type, A);
163
+
164
+ double d = NUM2DBL(value);
165
+
166
+ struct matrix* R;
167
+ VALUE result = TypedData_Make_Struct(cMatrix, struct matrix, &matrix_type, R);
168
+
169
+ c_matrix_init(R, A->m, A->n);
170
+ copy_d_array(A->m * A->n, A->data, R->data);
171
+ multiply_d_array(R->m * R->n, R->data, d);
172
+
173
+ return result;
174
+ }
175
+
176
+ VALUE matrix_multiply(VALUE self, VALUE v)
177
+ {
178
+ if(RB_FLOAT_TYPE_P(v) || FIXNUM_P(v)
179
+ || RB_TYPE_P(v, T_BIGNUM))
180
+ return matrix_multiply_mn(self, v);
181
+ return matrix_multiply_mm(self, v);
182
+ }
183
+
184
+ VALUE matrix_copy(VALUE mtrx)
185
+ {
186
+ struct matrix* M;
187
+ TypedData_Get_Struct(mtrx, struct matrix, &matrix_type, M);
188
+
189
+ struct matrix* R;
190
+ VALUE result = TypedData_Make_Struct(cMatrix, struct matrix, &matrix_type, R);
191
+
192
+ c_matrix_init(R, M->m, M->n);
193
+ copy_d_array(M->m * M->n, M->data, R->data);
194
+
195
+ return result;
196
+ }
197
+
198
+ VALUE row_size(VALUE self)
199
+ {
200
+ struct matrix* data;
201
+ TypedData_Get_Struct(self, struct matrix, &matrix_type, data);
202
+ return INT2NUM(data->m);
203
+ }
204
+
205
+ VALUE column_size(VALUE self)
206
+ {
207
+ struct matrix* data;
208
+ TypedData_Get_Struct(self, struct matrix, &matrix_type, data);
209
+ return INT2NUM(data->n);
210
+ }
211
+
212
+ VALUE transpose(VALUE self)
213
+ {
214
+ struct matrix* M;
215
+ TypedData_Get_Struct(self, struct matrix, &matrix_type, M);
216
+
217
+ struct matrix* R;
218
+ VALUE result = TypedData_Make_Struct(cMatrix, struct matrix, &matrix_type, R);
219
+
220
+ c_matrix_init(R, M->n, M->m);
221
+ matrix_transpose(M->m, M->n, M->data, R->data);
222
+
223
+ return result;
224
+ }
225
+
226
+ VALUE add_with(VALUE self, VALUE value)
227
+ {
228
+ struct matrix* A;
229
+ struct matrix* B;
230
+ TypedData_Get_Struct(self, struct matrix, &matrix_type, A);
231
+ TypedData_Get_Struct(value, struct matrix, &matrix_type, B);
232
+
233
+ if(A->m != B->m && A->m != B->m)
234
+ rb_raise(matrix_eIndexError, "Different sizes matrices");
235
+
236
+ int m = B->m;
237
+ int n = A->n;
238
+
239
+ struct matrix* C;
240
+ VALUE result = TypedData_Make_Struct(cMatrix, struct matrix, &matrix_type, C);
241
+
242
+ c_matrix_init(C, m, n);
243
+ add_d_arrays_to_result(n * m, A->data, B->data, C->data);
244
+
245
+ return result;
246
+ }
247
+
248
+
249
+ VALUE add_from(VALUE self, VALUE value)
250
+ {
251
+ struct matrix* A;
252
+ struct matrix* B;
253
+ TypedData_Get_Struct(self, struct matrix, &matrix_type, A);
254
+ TypedData_Get_Struct(value, struct matrix, &matrix_type, B);
255
+
256
+ if(A->m != B->m && A->m != B->m)
257
+ rb_raise(matrix_eIndexError, "Different sizes matrices");
258
+
259
+ int m = B->m;
260
+ int n = A->n;
261
+
262
+ add_d_arrays_to_first(n * m, A->data, B->data);
263
+
264
+ return self;
265
+ }
266
+
267
+ void init_fm_matrix()
268
+ {
269
+ VALUE mod = rb_define_module("FastMatrix");
270
+ cMatrix = rb_define_class_under(mod, "Matrix", rb_cData);
271
+
272
+ matrix_eTypeError = rb_define_class_under(cMatrix, "TypeError", rb_eTypeError);
273
+ matrix_eIndexError = rb_define_class_under(cMatrix, "IndexError", rb_eIndexError);
274
+
275
+
276
+ rb_define_alloc_func(cMatrix, matrix_alloc);
277
+
278
+ rb_define_method(cMatrix, "initialize", matrix_initialize, 2);
279
+ rb_define_method(cMatrix, "[]", matrix_get, 2);
280
+ rb_define_method(cMatrix, "[]=", matrix_set, 3);
281
+ rb_define_method(cMatrix, "*", matrix_multiply, 1);
282
+ rb_define_method(cMatrix, "column_count", row_size, 0);
283
+ rb_define_method(cMatrix, "row_count", column_size, 0);
284
+ rb_define_method(cMatrix, "copy", matrix_copy, 0);
285
+ rb_define_method(cMatrix, "transpose", transpose, 0);
286
+
287
+ rb_define_method(cMatrix, "+", add_with, 1);
288
+ rb_define_method(cMatrix, "+=", add_from, 1);
289
+ }
@@ -0,0 +1,43 @@
1
+ #ifndef FAST_MATRIX_MATRIX_H
2
+ #define FAST_MATRIX_MATRIX_H 1
3
+
4
+ #include "ruby.h"
5
+
6
+ static VALUE matrix_eTypeError;
7
+ static VALUE matrix_eIndexError;
8
+ static VALUE cMatrix;
9
+
10
+ // matrix
11
+ // m --->
12
+ // [ 0, 1, .., m-1]
13
+ // n [ m, m+1, .., 2m-1]
14
+ // | [2m, 2m+1, .., 3m-1]
15
+ // V [ . . . . .
16
+ // . . . . nm-1]
17
+ struct matrix
18
+ {
19
+ int m;
20
+ int n;
21
+
22
+ double* data;
23
+ };
24
+
25
+ void matrix_free(void* data);
26
+ size_t matrix_size(const void* data);
27
+
28
+ static const rb_data_type_t matrix_type =
29
+ {
30
+ .wrap_struct_name = "matrix",
31
+ .function =
32
+ {
33
+ .dmark = NULL,
34
+ .dfree = matrix_free,
35
+ .dsize = matrix_size,
36
+ },
37
+ .data = NULL,
38
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
39
+ };
40
+
41
+ void init_fm_matrix();
42
+
43
+ #endif /* FAST_MATRIX_MATRIX_H */
@@ -0,0 +1,33 @@
1
+ lib = File.expand_path("lib", __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "fast_matrix/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "fast_matrix"
7
+ spec.version = FastMatrix::VERSION
8
+ spec.authors = ["mmcs_ruby"]
9
+ spec.email = ["poganesyan@sfedu.ru"]
10
+
11
+ spec.summary = %q{Ruby wrapper around C matrices implementation}
12
+ spec.description = %q{Ruby wrapper around C matrices implementation}
13
+ spec.homepage = "https://github.com/mmcs-ruby/fast_matrix"
14
+ spec.license = "MIT"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = "https://github.com/mmcs-ruby/fast_matrix"
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.bindir = "exe"
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ["lib"]
27
+ spec.extensions = ["ext/fast_matrix/extconf.rb"]
28
+
29
+ spec.add_development_dependency "bundler", "~> 2.0"
30
+ spec.add_development_dependency "rake", "~> 10.0"
31
+ spec.add_development_dependency "rake-compiler"
32
+ spec.add_development_dependency "minitest", "~> 5.0"
33
+ end
@@ -0,0 +1,109 @@
1
+ require "fast_matrix/version"
2
+
3
+ module FastMatrix
4
+ class Matrix
5
+
6
+ #
7
+ # Creates a matrix of size +row_count+ x +column_count+.
8
+ # It fills the values by calling the given block,
9
+ # passing the current row and column.
10
+ # Returns an enumerator if no block is given.
11
+ #
12
+ # m = Matrix.build(2, 4) {|row, col| col - row }
13
+ # => Matrix[[0, 1, 2, 3], [-1, 0, 1, 2]]
14
+ # m = Matrix.build(3) { rand }
15
+ # => a 3x3 matrix with random elements
16
+ #
17
+ def self.build(row_count, column_count = row_count, &block)
18
+ matrix = self.new(row_count, column_count)
19
+ matrix.each_with_index! { |_, i, j| block.call(i, j) }
20
+ end
21
+ #
22
+ # Creates a matrix where each argument is a row.
23
+ # Matrix[ [25, 93], [-1, 66] ]
24
+ # => 25 93
25
+ # -1 66
26
+ #
27
+ def self.[](*rows)
28
+ columns_count = rows[0].size
29
+ rows_count = rows.size
30
+ matrix = Matrix.new(rows_count, columns_count)
31
+ matrix.each_with_index! { |_, i, j| rows[i][j] }
32
+ matrix
33
+ end
34
+
35
+ #
36
+ # Creates a single-column matrix where the values of that column are as given
37
+ # in +column+.
38
+ # Matrix.column_vector([4,5,6])
39
+ # => 4
40
+ # 5
41
+ # 6
42
+ #
43
+ def self.column_vector(column)
44
+ matrix = Matrix.new(column.size, 1)
45
+ column.each_with_index { |elem, i| matrix[i, 0] = elem }
46
+ matrix
47
+ end
48
+
49
+ #
50
+ # Creates a single-row matrix where the values of that row are as given in
51
+ # +row+.
52
+ # Matrix.row_vector([4,5,6])
53
+ # => 4 5 6
54
+ #
55
+ def self.row_vector(row)
56
+ matrix = Matrix.new(1, row.size)
57
+ row.each_with_index { |elem, j| matrix[0, j] = elem }
58
+ matrix
59
+ end
60
+
61
+ #
62
+ # Creates a matrix where the diagonal elements are composed of +values+.
63
+ # Matrix.diagonal(9, 5, -3)
64
+ # => 9 0 0
65
+ # 0 5 0
66
+ # 0 0 -3
67
+ #
68
+ def self.diagonal(*values)
69
+ matrix = Matrix.new(values.size, values.size)
70
+ matrix.each_with_index! { |_, i, j| i == j ? values[i] : 0 }
71
+ matrix
72
+ end
73
+
74
+ #
75
+ # Creates an +n+ by +n+ diagonal matrix where each diagonal element is
76
+ # +value+.
77
+ # Matrix.scalar(2, 5)
78
+ # => 5 0
79
+ # 0 5
80
+ #
81
+ def self.scalar(n, value)
82
+ matrix = Matrix.new(n, n)
83
+ matrix.each_with_index! { |_, i, j| i == j ? value : 0 }
84
+ matrix
85
+ end
86
+
87
+ #
88
+ # Creates an +n+ by +n+ identity matrix.
89
+ # Matrix.identity(2)
90
+ # => 1 0
91
+ # 0 1
92
+ #
93
+ def self.identity(n)
94
+ scalar(n, 1)
95
+ end
96
+
97
+ #
98
+ # Creates a zero matrix +n+ by +n+.
99
+ # Matrix.zero(2)
100
+ # => 0 0
101
+ # 0 0
102
+ #
103
+ def self.zero(n)
104
+ matrix = Matrix.new(n, n)
105
+ matrix.each_with_index! { |_, _, _| 0 }
106
+ matrix
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,72 @@
1
+ require "fast_matrix/version"
2
+ require "fast_matrix/fast_matrix"
3
+ require "constructors"
4
+
5
+ module FastMatrix
6
+ class Error < StandardError; end
7
+
8
+ # Matrix with fast implementations of + - * determinate in C
9
+ class Matrix
10
+
11
+ # Aliases just for capability with standard matrix
12
+ #
13
+ # Returns the number of rows.
14
+ #
15
+ alias_method :row_size, :row_count
16
+ #
17
+ # Returns the number of columns.
18
+ #
19
+ alias_method :column_size, :column_count
20
+
21
+ #
22
+ # Create fast matrix from standard matrix
23
+ #
24
+ def self.convert(matrix)
25
+ fast_matrix = Matrix.new(matrix.row_count, matrix.column_count)
26
+ (0...matrix.row_count).each do |i|
27
+ (0...matrix.column_count).each do |j|
28
+ fast_matrix[i, j] = matrix[i, j]
29
+ end
30
+ end
31
+ fast_matrix
32
+ end
33
+
34
+ def each_with_index
35
+ (0...row_count).each do |i|
36
+ (0...column_count).each do |j|
37
+ yield self[i, j], i, j
38
+ end
39
+ end
40
+ end
41
+
42
+ def each_with_index!
43
+ (0...row_count).each do |i|
44
+ (0...column_count).each do |j|
45
+ self[i, j] = yield self[i, j], i, j
46
+ end
47
+ end
48
+ self
49
+ end
50
+
51
+ #
52
+ # Convert to standard ruby matrix.
53
+ #
54
+ def convert
55
+ ::Matrix.build(row_size, column_size) { |i, j| self[i, j] }
56
+ end
57
+
58
+
59
+ # FIXME for compare with standard matrix
60
+ def ==(other)
61
+ # TODO check class and use fast compare from C if possibly
62
+ return false unless %i[row_size column_size \[\]].all? { |x| other.respond_to? x }
63
+ return false unless self.row_size == other.row_size && self.column_size == other.column_size
64
+
65
+ result = true
66
+ each_with_index do |elem, i, j|
67
+ result &&= elem == other[i, j].to_f
68
+ end
69
+ result
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,3 @@
1
+ module FastMatrix
2
+ VERSION = "0.1.2"
3
+ end
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fast_matrix
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - mmcs_ruby
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-10-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake-compiler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.0'
69
+ description: Ruby wrapper around C matrices implementation
70
+ email:
71
+ - poganesyan@sfedu.ru
72
+ executables: []
73
+ extensions:
74
+ - ext/fast_matrix/extconf.rb
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".dockerignore"
78
+ - ".gitignore"
79
+ - ".travis.yml"
80
+ - CODE_OF_CONDUCT.md
81
+ - Dockerfile
82
+ - Gemfile
83
+ - LICENSE.txt
84
+ - README.md
85
+ - Rakefile
86
+ - bin/console
87
+ - bin/setup
88
+ - ext/fast_matrix/c_array_opeartions.c
89
+ - ext/fast_matrix/c_array_operations.h
90
+ - ext/fast_matrix/extconf.rb
91
+ - ext/fast_matrix/fast_matrix.c
92
+ - ext/fast_matrix/fast_matrix.h
93
+ - ext/fast_matrix/matrix.c
94
+ - ext/fast_matrix/matrix.h
95
+ - fast_matrix.gemspec
96
+ - lib/constructors.rb
97
+ - lib/fast_matrix.rb
98
+ - lib/fast_matrix/version.rb
99
+ homepage: https://github.com/mmcs-ruby/fast_matrix
100
+ licenses:
101
+ - MIT
102
+ metadata:
103
+ homepage_uri: https://github.com/mmcs-ruby/fast_matrix
104
+ source_code_uri: https://github.com/mmcs-ruby/fast_matrix
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 2.7.6
122
+ signing_key:
123
+ specification_version: 4
124
+ summary: Ruby wrapper around C matrices implementation
125
+ test_files: []