alglib 0.1.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,6 @@
1
1
  #include "alglib_util.h"
2
2
 
3
+
3
4
  VALUE real1d_to_array(ap::real_1d_array *x) {
4
5
  VALUE ar=rb_ary_new2(x->gethighbound()-x->getlowbound());
5
6
  for(uint i=x->getlowbound();i<=x->gethighbound();i++) {
@@ -7,12 +8,3 @@ VALUE real1d_to_array(ap::real_1d_array *x) {
7
8
  }
8
9
  return ar;
9
10
  }
10
-
11
- void array_to_real1d(VALUE ary, ap::real_1d_array *x) {
12
- x->setlength(RARRAY_LEN(ary));
13
- //std::cout << "Largo:" << RARRAY_LEN(ary) << "\n";
14
- for(long i=0;i<RARRAY_LEN(ary);i++) {
15
- // std::cout << "i:" << i << "=" << rb_num2dbl(RARRAY_PTR(ary)[i]) <<"\n";
16
- x->operator()(i)=rb_num2dbl(RARRAY_PTR(ary)[i]);
17
- }
18
- }
@@ -4,11 +4,9 @@
4
4
 
5
5
  #include "ap.h"
6
6
  #include "linreg.h"
7
- #include "logit.h"
8
-
9
7
  #include "ruby.h"
10
8
 
11
9
  VALUE real1d_to_array(ap::real_1d_array *x);
12
- void array_to_real1d(VALUE ary, ap::real_1d_array *x);
10
+
13
11
 
14
12
  #endif
@@ -40,7 +40,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40
40
  #include <string>
41
41
  #include <cstring>
42
42
  #include <math.h>
43
- #include <cstdio>
43
+
44
44
  #ifdef __BORLANDC__
45
45
  #include <list.h>
46
46
  #include <vector.h>
@@ -1,7 +1,5 @@
1
1
  require 'mkmf'
2
- require 'fileutils'
3
- $CFLAGS+=" -DEBUG "
4
2
  $libs = append_library($libs, "supc++")
5
3
  $libs = append_library($libs, "stdc++")
6
4
  create_makefile("alglib_ext","alglib")
7
- FileUtils.mv("Makefile","Makefile.#{RUBY_VERSION}")
5
+
@@ -1,5 +1,3 @@
1
- $:.unshift(File.expand_path(File.dirname(__FILE__)+"/../ext"))
2
- $:.unshift(File.expand_path(File.dirname(__FILE__)+"/../lib"))
3
1
  require 'alglib_ext'
4
2
  require 'matrix'
5
3
  class Matrix
@@ -14,58 +12,67 @@ class Matrix
14
12
  a_matrix
15
13
  end
16
14
  end
17
- require 'alglib_version'
18
- require 'alglib/linearregression'
19
- require 'alglib/logit'
20
- require 'alglib/correlation'
21
-
22
- module Alglib_ext
23
- class Real1dArray
24
- def to_a
25
- Alglib_ext.real1d_to_array(self)
26
- end
27
- end
28
- class Real2dArray
29
- def to_2d_a
30
- a=self
31
- lr=a.getlowbound(1)
32
- lc=a.getlowbound(2)
33
- hr=a.gethighbound(1)
34
- hc=a.gethighbound(2)
35
- out=[]
36
- (lr..hr).each{|x|
37
- row=[]
38
- (lc..hc).each{|y|
39
- row[y]=a[x,y]
40
- }
41
- out[x]=row
42
- }
43
- out
44
- end
45
- end
46
- end
47
-
48
15
  module Alglib
49
-
50
- def self.array_to_real1darray(a)
51
- v=Alglib_ext::Real1dArray.new
52
- Alglib_ext.array_to_real1d(a,v)
53
- v
54
- end
55
-
56
- def self.real2darray_to_array(a)
57
- lr=a.getlowbound(1)
58
- lc=a.getlowbound(2)
59
- hr=a.gethighbound(1)
60
- hc=a.gethighbound(2)
61
- out=[]
62
- (lr..hr).each{|x|
63
- row=[]
64
- (lc..hc).each{|y|
65
- row[y]=a[x,y]
16
+ VERSION = '1.0.0'
17
+ class LinearRegression
18
+ attr_reader :model, :cases, :ivars
19
+ # Creates a Linear Regression object based on a Matrix
20
+ # EXAMPLE:
21
+ # require 'alglib'
22
+ # matrix=Matrix[[2,3,4],[2,5,5],[1,5,3],[4,6,5]]
23
+ # lr=Alglib::LinearRegression.build_from_matrix(matrix)
24
+ # => #<Alglib::LinearRegression:0x7ffaf6c05dc0 @model=#<Alglib_ext::LinearModel:0x7ffaf6c05e60>, @cases=4, @report=#<Alglib_ext::LrReport:0x7ffaf6c05e10>, @ivars=2>
25
+ # lr.coeffs
26
+ # => [0.585714285714286, -0.0142857142857142]
27
+
28
+ def self.build_from_matrix(matrix)
29
+ raise "Argument should be a matrix" unless matrix.is_a? Matrix
30
+ cases=matrix.row_size
31
+ ivars=matrix.column_size-1
32
+ lm=Alglib_ext::LinearModel.new
33
+ lr=Alglib_ext::LrReport.new
34
+ am=matrix.to_alglib_matrix
35
+ Alglib_ext::lrbuild(am,cases,ivars,lm,lr)
36
+ self.new(lm,lr,cases,ivars)
37
+ end
38
+ # Use with care...
39
+ def initialize(lm,lr,cases,ivars)
40
+ @model=lm
41
+ @report=lr
42
+ @cases=cases
43
+ @ivars=ivars
44
+ end
45
+ # Constant value. a on
46
+ # y= a+b1x1+b2x2...
47
+
48
+ def constant
49
+ v=Alglib_ext::Real1dArray.new
50
+ Alglib_ext::lrunpack(@model,v, @ivars);
51
+ Alglib_ext.real1d_to_array(v).slice(-1)
52
+ end
53
+ # Array with b coeffs.
54
+ def coeffs
55
+ v=Alglib_ext::Real1dArray.new
56
+ Alglib_ext::lrunpack(@model,v, @ivars);
57
+ Alglib_ext.real1d_to_array(v)[0,v.size]
58
+ end
59
+ # Predict an y value based on an array of predictors.
60
+ def process(vector)
61
+ Alglib_ext::lrprocess(@model,vector);
62
+ end
63
+ # A wrapper for lm_report
64
+ def report
65
+ {:c=>@report.c,
66
+ :rmserror=>@report.rmserror,
67
+ :avgerror=>@report.avgerror,
68
+ :avgrelerror=>@report.avgrelerror,
69
+ :cvrmserror=>@report.cvrmserror,
70
+ :cvavgerror=>@report.cvavgerror,
71
+ :cvavgrelerror=>@report.cvavgrelerror,
72
+ :ncvdefects=>@report.ncvdefects,
73
+ :cvdefects=>@report.cvdefects
66
74
  }
67
- out[x]=row
68
- }
69
- out
75
+ end
76
+
70
77
  end
71
78
  end
@@ -1,52 +1,16 @@
1
- $:.unshift(File.expand_path(File.dirname(__FILE__)+"/../lib"))
2
- $:.unshift(File.expand_path(File.dirname(__FILE__)+"/../ext"))
3
-
4
1
  require "test/unit"
5
- begin
6
- require 'statsample'
7
- HAS_SS=true
8
- rescue
9
- HAS_SS=false
10
- end
11
-
12
- if(HAS_GSL.nil?)
13
- begin
14
- require 'rbgsl'
15
- HAS_GSL=true
16
- rescue
17
- HAS_GSL=false
18
- end
19
- end
20
-
21
2
  require "alglib"
22
3
  require 'matrix'
23
4
  class TestAlglib < Test::Unit::TestCase
24
5
  def test_linear_regression
25
- a=[1,3,2,4,3,5,4,6,5,7]
26
- b=[3,3,4,4,5,5,6,6,4,4]
27
- c=[11,22,30,40,50,65,78,79,99,100]
28
- y=[3,4,5,6,7,8,9,10,20,30]
29
- matrix=Matrix.columns([a,b,c,y])
30
- lr=Alglib::LinearRegression.build_from_matrix(matrix)
31
- assert_in_delta(0.695,lr.coeffs[0],0.001)
32
- assert_in_delta(11.027,lr.constant,0.001)
33
- assert_in_delta(1.785,lr.process([1,3,11]),0.001)
34
- end
35
- def test_array_to_real1darray
36
- v=[1,2,3]
37
- r1d=Alglib.array_to_real1darray(v)
38
- assert_equal(3,r1d.size)
39
- assert_equal(1,r1d[0])
40
- assert_equal(2,r1d[1])
41
- assert_equal(3,r1d[2])
42
- end
43
- def test_real2darray_to_array
44
- m=Matrix[[0,3,4],[1,2,5],[3,5,6]]
45
- v2=m.to_alglib_matrix
46
- m2=Alglib.real2darray_to_array(v2)
47
- assert_equal(3,m2.size)
48
- assert_equal([0,3,4],m2[0])
49
- assert_equal([1,2,5],m2[1])
50
- assert_equal([3,5,6],m2[2])
51
- end
6
+ a=[1,3,2,4,3,5,4,6,5,7]
7
+ b=[3,3,4,4,5,5,6,6,4,4]
8
+ c=[11,22,30,40,50,65,78,79,99,100]
9
+ y=[3,4,5,6,7,8,9,10,20,30]
10
+ matrix=Matrix.columns([a,b,c,y])
11
+ lr=Alglib::LinearRegression.build_from_matrix(matrix)
12
+ assert_in_delta(0.695,lr.coeffs[0],0.001)
13
+ assert_in_delta(11.027,lr.constant,0.001)
14
+ assert_in_delta(1.785,lr.process([1,3,11]),0.001)
52
15
  end
16
+ end
metadata CHANGED
@@ -1,12 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alglib
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 1
8
- - 2
9
- version: 0.1.2
4
+ version: 1.0.0
10
5
  platform: ruby
11
6
  authors:
12
7
  - Claudio Bustos
@@ -14,51 +9,19 @@ autorequire:
14
9
  bindir: bin
15
10
  cert_chain: []
16
11
 
17
- date: 2010-03-22 00:00:00 -03:00
12
+ date: 2009-08-03 00:00:00 -04:00
18
13
  default_executable:
19
14
  dependencies:
20
- - !ruby/object:Gem::Dependency
21
- name: rubyforge
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
24
- requirements:
25
- - - ">="
26
- - !ruby/object:Gem::Version
27
- segments:
28
- - 2
29
- - 0
30
- - 4
31
- version: 2.0.4
32
- type: :development
33
- version_requirements: *id001
34
- - !ruby/object:Gem::Dependency
35
- name: gemcutter
36
- prerelease: false
37
- requirement: &id002 !ruby/object:Gem::Requirement
38
- requirements:
39
- - - ">="
40
- - !ruby/object:Gem::Version
41
- segments:
42
- - 0
43
- - 5
44
- - 0
45
- version: 0.5.0
46
- type: :development
47
- version_requirements: *id002
48
15
  - !ruby/object:Gem::Dependency
49
16
  name: hoe
50
- prerelease: false
51
- requirement: &id003 !ruby/object:Gem::Requirement
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
52
20
  requirements:
53
21
  - - ">="
54
22
  - !ruby/object:Gem::Version
55
- segments:
56
- - 2
57
- - 5
58
- - 1
59
- version: 2.5.1
60
- type: :development
61
- version_requirements: *id003
23
+ version: 2.3.2
24
+ version:
62
25
  description: ALGLIB - is a multilingual collection of algorithms designed to solve problems in the field of numeric analysis and data processing. ALGLIB is distributed under a 3-clause BSD license. This license is unrestrictive and allows using the package both in open and proprietary programs and even relicensing the code (if the new license doesn't conflict with the terms of the 3-clause BSD license).
63
26
  email:
64
27
  - clbustos@gmail.com
@@ -311,19 +274,9 @@ files:
311
274
  - ext/alglib/variancetests.h
312
275
  - ext/alglib/wsr.cpp
313
276
  - ext/alglib/wsr.h
314
- - ext/ap.i
315
- - ext/correlation.i
316
277
  - ext/extconf.rb
317
- - ext/logit.i
318
278
  - lib/alglib.rb
319
- - lib/alglib/correlation.rb
320
- - lib/alglib/linearregression.rb
321
- - lib/alglib/logit.rb
322
279
  - test/test_alglib.rb
323
- - test/test_correlation.rb
324
- - test/test_correlationtest.rb
325
- - test/test_linreg.rb
326
- - test/test_logit.rb
327
280
  has_rdoc: true
328
281
  homepage: http://rubyforge.org/projects/ruby-statsample/
329
282
  licenses: []
@@ -339,27 +292,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
339
292
  requirements:
340
293
  - - ">="
341
294
  - !ruby/object:Gem::Version
342
- segments:
343
- - 0
344
295
  version: "0"
296
+ version:
345
297
  required_rubygems_version: !ruby/object:Gem::Requirement
346
298
  requirements:
347
299
  - - ">="
348
300
  - !ruby/object:Gem::Version
349
- segments:
350
- - 0
351
301
  version: "0"
302
+ version:
352
303
  requirements: []
353
304
 
354
305
  rubyforge_project: ruby-statsample
355
- rubygems_version: 1.3.6
306
+ rubygems_version: 1.3.5
356
307
  signing_key:
357
308
  specification_version: 3
358
309
  summary: ALGLIB - is a multilingual collection of algorithms designed to solve problems in the field of numeric analysis and data processing
359
310
  test_files:
360
- - test/test_correlation.rb
361
- - test/test_correlationtest.rb
362
311
  - test/test_alglib.rb
363
- - test/test_pca.rb
364
- - test/test_logit.rb
365
- - test/test_linreg.rb
data/ext/ap.i DELETED
@@ -1,97 +0,0 @@
1
- %typemap(in) ap::real_1d_array {
2
- $1 = new ap::real_1d_array();
3
- $1->setlength(RARRAY_LEN($input));
4
- for(long i=0;i<RARRAY_LEN($input);i++) {
5
- $1->operator()(i)=rb_num2dbl(RARRAY_PTR($input)[i]);
6
- }
7
- }
8
-
9
-
10
- %apply ap::real_1d_array {ap::real_1d_array const &};
11
-
12
- namespace ap {
13
- /********************************************************************
14
- Template of a dynamical one-dimensional array
15
- ********************************************************************/
16
- template<class T, bool Aligned = false>
17
- class template_1d_array
18
- {
19
- public:
20
- template_1d_array();
21
- ~template_1d_array();
22
- template_1d_array(const template_1d_array &rhs);
23
- %extend {
24
- T __getitem__(int i) {
25
- return $self->operator()(i);
26
- };
27
- T __setitem__(int i, T v) {
28
- return $self->operator()(i)=v;
29
- };
30
- int size() {
31
- return $self->gethighbound()-$self->getlowbound()+1;
32
- }
33
- }
34
-
35
- void setbounds( int iLow, int iHigh );
36
- void setlength(int iLen);
37
- void setcontent( int iLow, int iHigh, const T *pContent );
38
- T* getcontent();
39
- int getlowbound(int iBoundNum = 0);
40
- int gethighbound(int iBoundNum = 0);
41
- };
42
-
43
- template<class T, bool Aligned = false>
44
- class template_2d_array
45
- {
46
- public:
47
- template_2d_array();
48
-
49
- ~template_2d_array();
50
-
51
- template_2d_array(const template_2d_array &rhs);
52
- T& operator()(int i1, int i2);
53
-
54
- void setbounds( int iLow1, int iHigh1, int iLow2, int iHigh2 );
55
-
56
- void setlength(int iLen1, int iLen2);
57
-
58
- void setcontent( int iLow1, int iHigh1, int iLow2, int iHigh2, const T *pContent );
59
-
60
- int getlowbound(int iBoundNum) const;
61
-
62
- int gethighbound(int iBoundNum) const;
63
-
64
- %extend {
65
- double __getitem__(int x,int y) {
66
- return $self->operator()(x,y);
67
- }
68
- void __setitem__(int x,int y, T val) {
69
- $self->operator()(x,y)=val;
70
- }
71
-
72
- }
73
-
74
- };
75
-
76
-
77
-
78
- typedef template_1d_array<int> integer_1d_array;
79
- typedef template_1d_array<double,true> real_1d_array;
80
- typedef template_1d_array<complex> complex_1d_array;
81
- typedef template_1d_array<bool> boolean_1d_array;
82
-
83
- typedef template_2d_array<int> integer_2d_array;
84
- typedef template_2d_array<double,true> real_2d_array;
85
- typedef template_2d_array<complex> complex_2d_array;
86
- typedef template_2d_array<bool> boolean_2d_array;
87
-
88
- %template(Integer1dArray) template_1d_array<int>;
89
- %template(Real1dArray) template_1d_array<double,true>;
90
- %template(Boolean1dArray) template_1d_array<bool>;
91
- %template(Integer2dArray) template_2d_array<int>;
92
- %template(Real2dArray) template_2d_array<double,true>;
93
- };
94
-
95
-
96
- VALUE real1d_to_array(ap::real_1d_array *x);
97
- void array_to_real1d(VALUE ary, ap::real_1d_array *x);