darvin_functions 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc ADDED
@@ -0,0 +1,7 @@
1
+ = Darvin_Functions
2
+
3
+ Various functions for data processing, clean up and analysis
4
+
5
+ == Copyright
6
+
7
+ Copyright (c) 2010 Pedal Brain LLC.
@@ -0,0 +1,69 @@
1
+ #include <stdio.h>
2
+ #include <memory.h>
3
+ #include <stdlib.h>
4
+
5
+ #include "darvin_functions.h"
6
+
7
+ VALUE mDarvinFunctions;
8
+
9
+ /********************************************************************************
10
+ * roundNumber
11
+ ********************************************************************************/
12
+ long roundNumber(double value) {
13
+ if (value == 0.0) return (0); // don't mess with zero
14
+ if (value > 0.0) return ((long)(value+.5)); // positive number
15
+ return ((long)(value - .5)); // negative number
16
+ }
17
+
18
+ /* vself is a reference to the module or itself
19
+ varray is a Ruby array */
20
+ VALUE mDarvinFunctions_workout_mmp(VALUE vself, VALUE varray) {
21
+ if (NIL_P(varray) || RARRAY_LEN(varray) == 0)
22
+ return varray;
23
+
24
+ VALUE new_array = rb_ary_new2(RARRAY_LEN(varray));
25
+
26
+ int i, j, trail, offset;
27
+ long *tot; // total for specific interval
28
+ long *maxTot; // current sum for (i-1)
29
+
30
+ tot = ALLOCA_N(long, RARRAY_LEN(varray)); // allocate memory
31
+ maxTot = ALLOCA_N(long, RARRAY_LEN(varray)); // allocate memory
32
+
33
+ trail = 0; // trailing value
34
+ memset(maxTot, '\0', sizeof maxTot); // init max totals by slot
35
+
36
+ maxTot[0] = tot[0] = FIX2INT(RARRAY_PTR(varray)[0]); // need first total (for single cell average), seed for others
37
+
38
+ /*
39
+ * this loop initializes the totals for all slot averages (1-n)
40
+ * assume numbers are 1 2 3 4 5
41
+ * maxTot[] will be 1, 3, 6, 10, 15
42
+ * The idea is each slot is initialized to the earliers total values,
43
+ * that is slot0 is initialized to 1
44
+ * slot1 is initialized to 1 + 2
45
+ * ...
46
+ * slot4 is initialized to 1 + 2 + 3 + 4 + 5
47
+ */
48
+ for (i=1; i<RARRAY_LEN(varray); i++) // loop thru all the numbers
49
+ maxTot[i] = tot[i] = (tot[i-1] + FIX2INT(RARRAY_PTR(varray)[i])); // compute tot for subsequent cells (starting at cell 0)
50
+
51
+ for (offset=1; offset<RARRAY_LEN(varray); offset++) { // loop starting at index 1 (index-1 is the value to subtract)
52
+ trail = FIX2INT(RARRAY_PTR(varray)[offset-1]); // this is the value to subtract from the beginning of the list
53
+ for (i=offset; i<RARRAY_LEN(varray); i++) { // first time subtract 1 from all (except n list)
54
+ j = i - offset; // subtract trail and add next value
55
+ tot[j] += (FIX2INT(RARRAY_PTR(varray)[i]) - trail); // to temp variable
56
+ if (tot[j] > maxTot[j]) maxTot[j] = tot[j];// set maxTot for slot if larger
57
+ }
58
+ }
59
+
60
+ for (i=0; i<RARRAY_LEN(varray); i++) // first value divided by 1 (ignore it)
61
+ rb_ary_push(new_array, INT2FIX(roundNumber((double)maxTot[i] / (double)(i+1))));
62
+
63
+ return new_array;
64
+ }
65
+
66
+ void Init_darvin_functions(){
67
+ mDarvinFunctions = rb_define_module("DarvinFunctions");
68
+ rb_define_module_function(mDarvinFunctions, "workout_mmp", mDarvinFunctions_workout_mmp, 1);
69
+ }
@@ -0,0 +1,20 @@
1
+ #ifndef RUBY_DARVINFUNCTIONS
2
+ #define RUBY_DARVINFUNCTIONS
3
+
4
+ #include <ruby.h>
5
+ #include <string.h>
6
+ #include <math.h>
7
+ #include <time.h>
8
+ #include <locale.h>
9
+
10
+ #if !defined(RSTRING_PTR)
11
+ #define RSTRING_PTR(TC_s) (RSTRING(TC_s)->ptr)
12
+ #endif
13
+ #if !defined(RSTRING_LEN)
14
+ #define RSTRING_LEN(TC_s) (RSTRING(TC_s)->len)
15
+ #endif
16
+ #if !defined(RARRAY_LEN)
17
+ #define RARRAY_LEN(TC_a) (RARRAY(TC_a)->len)
18
+ #endif
19
+
20
+ #endif
data/test/helper.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'darvin_functions'
8
+
9
+ class Test::Unit::TestCase
10
+ end
data/test/test_mmp.rb ADDED
@@ -0,0 +1,44 @@
1
+ require 'helper'
2
+ require 'benchmark'
3
+
4
+ class TestMMP < Test::Unit::TestCase
5
+ MAX_POWER = 150000 # 1500.00 Watts as an integer
6
+
7
+ context "test case" do
8
+ should "handle an empty array" do
9
+ assert_equal [], DarvinFunctions::workout_mmp([])
10
+ end
11
+
12
+ should "handle an array of one" do
13
+ assert_equal [10], DarvinFunctions::workout_mmp([10])
14
+ end
15
+
16
+ should "handle an array of two" do
17
+ assert_equal [10, 7], DarvinFunctions::workout_mmp([10, 4])
18
+ end
19
+
20
+ should "handle an array of three" do
21
+ assert_equal [10, 7, 5], DarvinFunctions::workout_mmp([10, 4, 2])
22
+ end
23
+ end
24
+
25
+ context "Benchmark" do
26
+
27
+ setup do
28
+ @size = 14400
29
+ @data = []
30
+ @size.times do |i|
31
+ value = Math.sin(i % Math::PI / 2)
32
+ @data << (value * MAX_POWER * 100 * rand).to_i / 100.0
33
+ end
34
+ end
35
+
36
+ should "run under a minute" do
37
+ tms = Benchmark.bmbm(10) do |x|
38
+ x.report { DarvinFunctions::workout_mmp(@data) }
39
+ end.last
40
+ assert tms.total < 60, "#{@size} array took too long: #{tms.total} seconds is too long" # seconds
41
+ end
42
+ end
43
+
44
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 1
9
- version: 0.0.1
8
+ - 2
9
+ version: 0.0.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Darvin Bauer
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-08-09 00:00:00 -05:00
18
+ date: 2010-06-10 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -36,13 +36,15 @@ executables: []
36
36
 
37
37
  extensions:
38
38
  - ext/extconf.rb
39
- extra_rdoc_files: []
40
-
39
+ extra_rdoc_files:
40
+ - README.rdoc
41
41
  files:
42
- - ext/darvin_functions.bundle
42
+ - ext/darvin_functions.h
43
+ - ext/darvin_functions.c
43
44
  - ext/extconf.rb
45
+ - README.rdoc
44
46
  has_rdoc: true
45
- homepage: http://github.com/pedalbrain/darvin_functions
47
+ homepage: http://github.com/pedalbrain/Darvin_Functions
46
48
  licenses: []
47
49
 
48
50
  post_install_message:
@@ -71,5 +73,6 @@ rubygems_version: 1.3.6
71
73
  signing_key:
72
74
  specification_version: 3
73
75
  summary: Various functions for data processing, clean up and analysis
74
- test_files: []
75
-
76
+ test_files:
77
+ - test/helper.rb
78
+ - test/test_mmp.rb
Binary file