- K =
Random.rand
- WIDTH =
40
- V_f =
(0..1_024*1_024).to_a.map{Random.rand}
- V_s =
(0..1_024*1_024).to_a.map{Random.rand(1_024)}
- P_SMALL =
(0..1_024).to_a.map{Random.rand}
- P_LARGE =
(0..1_024*1_024).to_a.map{Random.rand}
- X =
Random.rand
%{
// Include the Ruby headers and goodies
#include "ruby.h"
#include "assert.h"
#include "yepCore.h"
#include "yepLibrary.h"
#include "yepMath.h"
}.strip.freeze
- PRIMARY =
%{
// Defining a space for information and references about the module to be stored
// internally
VALUE cRyeppp;
}.strip.freeze
- FUNCS =
verb_name is in [Add, Subtract, Multiply]
Proc.new do |verb_name|
%{#{if verb_name == 'Multiply'
typed_variants(%{
static VALUE multiply_iv64{{type}}s64{{type}}_iv64{{type}}(VALUE self, VALUE x, VALUE multiply_by) {
enum YepStatus status;
long i;
VALUE new_ary;
VALUE *x_a;
long l;
Yep64{{type}} mult_by;
#{declare_yep64_typed_array(%w{x})}
#{ensure_array_argument('x', 'first')}
if (TYPE(multiply_by) != T_FIXNUM && TYPE(multiply_by) != T_BIGNUM && TYPE(multiply_by) != T_FLOAT) {
rb_raise(rb_eArgError, "second argument was not an integer or a float");
}
x_a = RARRAY_PTR(x);
l = RARRAY_LEN(x);
mult_by = (Yep64{{type}})NUM2DBL(multiply_by);
/* Allocate arrays of inputs and outputs */
#{allocate_yep64_typed_array(%w{x}, 'l')}
#{initialize_yeppp}
#{load_ruby_array_into_yeppp_array_parameterized('x', 'i', 'l', :allocated_arrays => %w{x})}
/* Perform the operation */
status = yepCore_Multiply_IV64{{type}}S64{{type}}_IV64{{type}}(yep_x, mult_by, (YepSize)l);
assert(status == YepStatusOk);
#{load_ruby_array_from_yeppp_array_parameterized('x', 'i', 'l')}
#{deinitialize_yeppp}
#{release_array_memory(%w{x})}
return new_ary;
}
}).strip.freeze
end
}
#{typed_variants(%{
// #{verb_name} Arrays of Fixnums.
static VALUE #{verb_name.downcase}_iv64{{type}}v64{{type}}_iv64{{type}}(VALUE self, VALUE x, VALUE y) {
enum YepStatus status;
VALUE new_ary;
long i;
VALUE *x_a;
VALUE *y_a;
long l;
#{declare_yep64_typed_array(%w{x y})}
#{ensure_array_argument('x', 'first')}
#{ensure_array_argument('y', 'second')}
x_a = RARRAY_PTR(x);
y_a = RARRAY_PTR(y);
l = RARRAY_LEN(x);
if (l != RARRAY_LEN(y)) {
rb_raise(rb_eArgError, "given array arguments have different lengths");
}
/* Allocate arrays of inputs and outputs */
#{allocate_yep64_typed_array(%w{x y}, 'l')}
#{initialize_yeppp}
#{load_ruby_array_into_yeppp_array_parameterized('x', 'i', 'l', :allocated_arrays => %w{x y})}
#{load_ruby_array_into_yeppp_array_parameterized('y', 'i', 'l', :allocated_arrays => %w{x y})}
/* Perform the #{verb_name} */
status = yepCore_#{verb_name}_IV64{{type}}V64{{type}}_IV64{{type}}(yep_x, yep_y, (YepSize)l);
assert(status == YepStatusOk);
#{load_ruby_array_from_yeppp_array_parameterized('x', 'i', 'l')}
#{deinitialize_yeppp}
#{release_array_memory(%w{x y})}
return new_ary;
}
})}
}
end
- DOT_PRODUCT =
%{
// Get the dot product of 2 Arrays.
static VALUE dotproduct_v64fv64f_s64f(VALUE self, VALUE x, VALUE y) {
enum YepStatus status;
long i;
Yep64f dp;
VALUE *x_a;
VALUE *y_a;
long l;
#{declare_yep64_typed_array(%w{x y}, :type => 'f')}
#{ensure_array_argument('x', 'first')}
#{ensure_array_argument('y', 'second')}
x_a = RARRAY_PTR(x);
y_a = RARRAY_PTR(y);
l = RARRAY_LEN(x);
if (l != RARRAY_LEN(y)) {
rb_raise(rb_eArgError, "given array arguments have different lengths");
}
/* Allocate arrays of inputs and outputs */
#{allocate_yep64_typed_array(%w{x y}, 'l', :type => 'f')}
#{initialize_yeppp}
#{load_ruby_array_into_yeppp_array('x', 'i', 'l', 'f', [:integer, :float], :allocated_arrays => %w{x y})}
#{load_ruby_array_into_yeppp_array('y', 'i', 'l', 'f', [:integer, :float], :allocated_arrays => %w{x y})}
/* Perform the operation */
status = yepCore_DotProduct_V64fV64f_S64f(yep_x, yep_y, &dp, (YepSize)l);
assert(status == YepStatusOk);
#{deinitialize_yeppp}
#{release_array_memory(%w{x y})}
return DBL2NUM((double)dp);
}
}.strip
- MIN_MAX =
typed_variants(%w{Min Max}.map do |kind|
%{
// Get the #{kind.downcase} value from an Array.
static VALUE #{kind.downcase}_v64{{type}}_s64{{type}}(VALUE self, VALUE x) {
enum YepStatus status;
long i;
Yep64{{type}} #{kind.downcase};
VALUE *x_a;
long l;
#{declare_yep64_typed_array(%w{x})}
#{ensure_array_argument('x', 'first')}
x_a = RARRAY_PTR(x);
l = RARRAY_LEN(x);
/* Allocate arrays of inputs and outputs */
#{allocate_yep64_typed_array('x', 'l')}
#{initialize_yeppp}
#{load_ruby_array_into_yeppp_array_parameterized('x', 'i', 'l', :allocated_arrays => %w{x})}
/* Perform the operation */
status = yepCore_#{kind}_V64{{type}}_S64{{type}}(yep_x, &#{kind.downcase}, (YepSize)l);
assert(status == YepStatusOk);
#{deinitialize_yeppp}
#{release_array_memory(%w{x})}
return {{ruby_type}}2NUM(({{c_type}})#{kind.downcase});
}
}.strip
end.join("\n\n"))
- PAIRWISE_MIN_MAX =
typed_variants(%w{Min Max}.map do |kind|
%{
// Get the pairwise #{kind.downcase}ima from Arrays.
static VALUE #{kind.downcase}_iv64{{type}}v64{{type}}_iv64{{type}}(VALUE self, VALUE x, VALUE y) {
enum YepStatus status;
long i;
VALUE new_ary;
VALUE *x_a;
VALUE *y_a;
long l;
#{declare_yep64_typed_array(%w{x y})}
#{ensure_array_argument('x', 'first')}
#{ensure_array_argument('y', 'second')}
x_a = RARRAY_PTR(x);
y_a = RARRAY_PTR(y);
l = RARRAY_LEN(x);
if (l != RARRAY_LEN(y)) {
rb_raise(rb_eArgError, "given array arguments have different lengths");
}
/* Allocate arrays of inputs and outputs */
#{allocate_yep64_typed_array(%w{x y}, 'l')}
#{initialize_yeppp}
#{load_ruby_array_into_yeppp_array_parameterized('x', 'i', 'l', :allocated_arrays => %w{x y})}
#{load_ruby_array_into_yeppp_array_parameterized('y', 'i', 'l', :allocated_arrays => %w{x y})}
/* Perform the operation */
status = yepCore_#{kind}_IV64{{type}}V64{{type}}_IV64{{type}}(yep_x, yep_y, (YepSize)l);
assert(status == YepStatusOk);
#{load_ruby_array_from_yeppp_array_parameterized('x', 'i', 'l')}
#{deinitialize_yeppp}
#{release_array_memory(%w{x y})}
return new_ary;
}
}.strip
end.join("\n\n"), :only_type => 'f')
- CONSTANT_MIN_MAX =
typed_variants(%w{Min Max}.map do |kind|
%{
// Get the #{kind.downcase}ima from an Array and a constant.
static VALUE #{kind.downcase}_iv64{{type}}s64{{type}}_iv64{{type}}(VALUE self, VALUE x, VALUE c) {
enum YepStatus status;
long i;
VALUE new_ary;
VALUE *x_a;
long l;
Yep64f konst;
#{declare_yep64_typed_array(%w{x})}
#{ensure_array_argument('x', 'first')}
if (TYPE(c) != T_FIXNUM && TYPE(c) != T_BIGNUM && TYPE(c) != T_FLOAT) {
rb_raise(rb_eArgError, "second argument was not a number");
}
x_a = RARRAY_PTR(x);
l = RARRAY_LEN(x);
konst = (Yep64f)NUM2{{ruby_type}}(c);
/* Allocate arrays of inputs and outputs */
#{allocate_yep64_typed_array(%w{x}, 'l')}
#{initialize_yeppp}
#{load_ruby_array_into_yeppp_array_parameterized('x', 'i', 'l', :allocated_arrays => %w{x})}
/* Perform the operation */
status = yepCore_#{kind}_IV64{{type}}S64{{type}}_IV64{{type}}(yep_x, konst, (YepSize)l);
assert(status == YepStatusOk);
#{load_ruby_array_from_yeppp_array_parameterized('x', 'i', 'l')}
#{deinitialize_yeppp}
#{release_array_memory(%w{x})}
return new_ary;
}
}.strip
end.join("\n\n"), :only_type => 'f')
- NEGATE =
typed_variants(%{
// Negate an Array.
static VALUE negate_iv64{{type}}_is64{{type}}(VALUE self, VALUE x) {
enum YepStatus status;
long i;
VALUE new_ary;
VALUE *x_a;
long l;
#{declare_yep64_typed_array(%w{x})}
#{ensure_array_argument('x', 'first')}
x_a = RARRAY_PTR(x);
l = RARRAY_LEN(x);
/* Allocate arrays of inputs and outputs */
#{allocate_yep64_typed_array(%w{x}, 'l')}
#{initialize_yeppp}
#{load_ruby_array_into_yeppp_array_parameterized('x', 'i', 'l', :allocated_arrays => %w{x})}
/* Perform the negation */
status = yepCore_Negate_IV64{{type}}_IV64{{type}}(yep_x, (YepSize)l);
assert(status == YepStatusOk);
#{load_ruby_array_from_yeppp_array_parameterized('x', 'i', 'l')}
#{deinitialize_yeppp}
#{release_array_memory(%w{x})}
return new_ary;
}
}).freeze
- SUMS =
%w{Sum SumAbs SumSquares}.map do |kind|
%{
static VALUE #{kind.downcase}_v64f_s64f(VALUE self, VALUE x) {
enum YepStatus status;
long i;
Yep64f sum;
VALUE *x_a;
long l;
#{declare_yep64_typed_array(%w{x}, :type => 'f')}
#{ensure_array_argument('x', 'first')}
x_a = RARRAY_PTR(x);
l = RARRAY_LEN(x);
/* Allocate arrays of inputs and outputs */
#{allocate_yep64_typed_array('x', 'l', :type => 'f')}
#{initialize_yeppp}
#{load_ruby_array_into_yeppp_array('x', 'i', 'l', 'f', [:integer, :float], :allocated_arrays => %w{x})}
/* Perform the operation */
status = yepCore_#{kind}_V64f_S64f(yep_x, &sum, (YepSize)l);
assert(status == YepStatusOk);
#{deinitialize_yeppp}
#{release_array_memory(%w{x})}
return DBL2NUM((double)sum);
}
}.strip
end.join("\n\n").freeze
- MATHS_KINDS =
%w{Log Exp Sin Cos Tan}.freeze
- MATHS =
MATHS_KINDS.map do |kind|
%{
static VALUE #{kind.downcase}_v64f_v64f(VALUE self, VALUE x) {
enum YepStatus status;
long i;
VALUE new_ary;
VALUE *x_a;
long l;
#{declare_yep64_typed_array(%w{x y}, :type => 'f')}
#{ensure_array_argument('x', 'first')}
x_a = RARRAY_PTR(x);
l = RARRAY_LEN(x);
/* Allocate arrays of inputs and outputs */
#{allocate_yep64_typed_array(%w{x y}, 'l', :type => 'f')}
#{initialize_yeppp}
#{load_ruby_array_into_yeppp_array('x', 'i', 'l', 'f', [:integer, :float], :allocated_arrays => %w{x y})}
/* Perform the operation */
status = yepMath_#{kind}_V64f_V64f(yep_x, yep_y, (YepSize)l);
assert(status == YepStatusOk);
#{load_ruby_array_from_yeppp_array('y', 'i', 'l', 'f')}
#{deinitialize_yeppp}
#{release_array_memory(%w{x y})}
return new_ary;
}
}.strip
end.join("\n\n").freeze
- POLYNOMIAL =
%{
// x is the coefficients in standard form
// where is the set of points at which to evaluate x
static VALUE evaluatepolynomial_v64fv64f_v64f(VALUE self, VALUE x, VALUE where) {
enum YepStatus status;
long i;
VALUE new_ary;
VALUE *x_a;
VALUE *y_a;
long x_l;
long y_l;
#{declare_yep64_typed_array(%w{x y z}, :type => 'f')}
#{ensure_array_argument('x', 'first')}
#{ensure_array_argument('where', 'second')}
x_a = RARRAY_PTR(x);
y_a = RARRAY_PTR(where);
x_l = RARRAY_LEN(x);
y_l = RARRAY_LEN(where);
/* Allocate arrays of inputs and outputs */
#{allocate_yep64_typed_array(%w{x}, 'x_l', :type => 'f')}
#{allocate_yep64_typed_array(%w{y z}, 'y_l', :type => 'f')}
#{initialize_yeppp}
// Yeppp! polynomial evaluation works in reverse standard form, so we have
// to load yep_x in reverse.
#{load_ruby_array_into_yeppp_array('x', 'i', 'x_l', 'f', [:integer, :float], :reverse => true, :allocated_arrays => %w{x y z})}
#{load_ruby_array_into_yeppp_array('y', 'i', 'y_l', 'f', [:integer, :float], :allocated_arrays => %w{x y z})}
/* Perform the operation */
status = yepMath_EvaluatePolynomial_V64fV64f_V64f(yep_x, yep_y, yep_z, (YepSize)x_l, (YepSize)y_l);
assert(status == YepStatusOk);
#{load_ruby_array_from_yeppp_array('z', 'i', 'y_l', 'f')}
#{deinitialize_yeppp}
#{release_array_memory(%w{x y z})}
return new_ary;
}
}.strip.freeze
- INITIALIZER =
%{
// The initialization method for this module
void Init_ryeppp() {
cRyeppp = rb_define_class("Ryeppp", rb_cObject);
/* Addition */
rb_define_singleton_method(cRyeppp, "add_v64fv64f_v64f", add_iv64fv64f_iv64f, 2);
rb_define_singleton_method(cRyeppp, "add_v64sv64s_v64s", add_iv64sv64s_iv64s, 2);
/* Subtraction */
rb_define_singleton_method(cRyeppp, "subtract_v64fv64f_v64f", subtract_iv64fv64f_iv64f, 2);
rb_define_singleton_method(cRyeppp, "subtract_v64sv64s_v64s", subtract_iv64sv64s_iv64s, 2);
/* Multiplication */
rb_define_singleton_method(cRyeppp, "multiply_v64fs64f_v64f", multiply_iv64fs64f_iv64f, 2);
rb_define_singleton_method(cRyeppp, "multiply_v64sv64s_v64s", multiply_iv64sv64s_iv64s, 2);
rb_define_singleton_method(cRyeppp, "multiply_v64fv64f_v64f", multiply_iv64fv64f_iv64f, 2);
rb_define_singleton_method(cRyeppp, "multiply_v64ss64s_v64s", multiply_iv64ss64s_iv64s, 2);
/* Dot Product */
rb_define_singleton_method(cRyeppp, "dotproduct_v64fv64f_s64f", dotproduct_v64fv64f_s64f, 2);
/* Minimum */
rb_define_singleton_method(cRyeppp, "min_v64f_s64f", min_v64f_s64f, 1);
rb_define_singleton_method(cRyeppp, "min_v64s_s64s", min_v64s_s64s, 1);
/* Maximum */
rb_define_singleton_method(cRyeppp, "max_v64f_s64f", max_v64f_s64f, 1);
rb_define_singleton_method(cRyeppp, "max_v64s_s64s", max_v64s_s64s, 1);
/* Pairwise Minima */
rb_define_singleton_method(cRyeppp, "min_v64fv64f_v64f", min_iv64fv64f_iv64f, 2);
// Pairwise signed min is not available.
/* Pairwise Maxima */
rb_define_singleton_method(cRyeppp, "max_v64fv64f_v64f", max_iv64fv64f_iv64f, 2);
// Pairwise signed max is not available.
/* Constant Minima */
rb_define_singleton_method(cRyeppp, "min_v64fs64f_v64f", min_iv64fs64f_iv64f, 2);
// Constant signed min is not available.
/* Constant Maxima */
rb_define_singleton_method(cRyeppp, "max_v64fs64f_v64f", max_iv64fs64f_iv64f, 2);
// Constant signed max is not available.
/* Negation */
rb_define_singleton_method(cRyeppp, "negate_v64f_s64f", negate_iv64f_is64f, 1);
rb_define_singleton_method(cRyeppp, "negate_v64s_s64s", negate_iv64s_is64s, 1);
/* Sums */
rb_define_singleton_method(cRyeppp, "sum_v64f_s64f", sum_v64f_s64f, 1);
// Signed sum is not available.
rb_define_singleton_method(cRyeppp, "sumabs_v64f_s64f", sumabs_v64f_s64f, 1);
// Signed abs sum is not available.
rb_define_singleton_method(cRyeppp, "sumsquares_v64f_s64f", sumsquares_v64f_s64f, 1);
// Signed squares sum is not available.
/* Maths */
#{MATHS_KINDS.map do |kind|
%{rb_define_singleton_method(cRyeppp, "#{kind.downcase}_v64f_v64f", #{kind.downcase}_v64f_v64f, 1);}
end.join("\n")}
/* Polynomial */
rb_define_singleton_method(cRyeppp, "evaluatepolynomial_v64fv64f_v64f", evaluatepolynomial_v64fv64f_v64f, 2);
}
}.strip.freeze