alglib 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +7 -0
- data/Manifest.txt +253 -0
- data/README.txt +33 -0
- data/Rakefile +27 -0
- data/ext/Rakefile +24 -0
- data/ext/alglib.i +24 -0
- data/ext/alglib/Makefile +157 -0
- data/ext/alglib/airyf.cpp +372 -0
- data/ext/alglib/airyf.h +81 -0
- data/ext/alglib/alglib.cpp +8558 -0
- data/ext/alglib/alglib_util.cpp +19 -0
- data/ext/alglib/alglib_util.h +14 -0
- data/ext/alglib/ap.cpp +877 -0
- data/ext/alglib/ap.english.html +364 -0
- data/ext/alglib/ap.h +666 -0
- data/ext/alglib/ap.russian.html +442 -0
- data/ext/alglib/apvt.h +754 -0
- data/ext/alglib/bdss.cpp +1500 -0
- data/ext/alglib/bdss.h +251 -0
- data/ext/alglib/bdsvd.cpp +1339 -0
- data/ext/alglib/bdsvd.h +164 -0
- data/ext/alglib/bessel.cpp +1226 -0
- data/ext/alglib/bessel.h +331 -0
- data/ext/alglib/betaf.cpp +105 -0
- data/ext/alglib/betaf.h +74 -0
- data/ext/alglib/bidiagonal.cpp +1328 -0
- data/ext/alglib/bidiagonal.h +350 -0
- data/ext/alglib/binomialdistr.cpp +247 -0
- data/ext/alglib/binomialdistr.h +153 -0
- data/ext/alglib/blas.cpp +576 -0
- data/ext/alglib/blas.h +132 -0
- data/ext/alglib/cblas.cpp +226 -0
- data/ext/alglib/cblas.h +57 -0
- data/ext/alglib/cdet.cpp +138 -0
- data/ext/alglib/cdet.h +92 -0
- data/ext/alglib/chebyshev.cpp +216 -0
- data/ext/alglib/chebyshev.h +76 -0
- data/ext/alglib/chisquaredistr.cpp +157 -0
- data/ext/alglib/chisquaredistr.h +144 -0
- data/ext/alglib/cholesky.cpp +285 -0
- data/ext/alglib/cholesky.h +86 -0
- data/ext/alglib/cinverse.cpp +298 -0
- data/ext/alglib/cinverse.h +111 -0
- data/ext/alglib/clu.cpp +337 -0
- data/ext/alglib/clu.h +120 -0
- data/ext/alglib/correlation.cpp +280 -0
- data/ext/alglib/correlation.h +77 -0
- data/ext/alglib/correlationtests.cpp +726 -0
- data/ext/alglib/correlationtests.h +134 -0
- data/ext/alglib/crcond.cpp +826 -0
- data/ext/alglib/crcond.h +148 -0
- data/ext/alglib/creflections.cpp +310 -0
- data/ext/alglib/creflections.h +165 -0
- data/ext/alglib/csolve.cpp +312 -0
- data/ext/alglib/csolve.h +99 -0
- data/ext/alglib/ctrinverse.cpp +387 -0
- data/ext/alglib/ctrinverse.h +98 -0
- data/ext/alglib/ctrlinsolve.cpp +297 -0
- data/ext/alglib/ctrlinsolve.h +81 -0
- data/ext/alglib/dawson.cpp +234 -0
- data/ext/alglib/dawson.h +74 -0
- data/ext/alglib/descriptivestatistics.cpp +436 -0
- data/ext/alglib/descriptivestatistics.h +112 -0
- data/ext/alglib/det.cpp +140 -0
- data/ext/alglib/det.h +94 -0
- data/ext/alglib/dforest.cpp +1819 -0
- data/ext/alglib/dforest.h +316 -0
- data/ext/alglib/elliptic.cpp +497 -0
- data/ext/alglib/elliptic.h +217 -0
- data/ext/alglib/estnorm.cpp +429 -0
- data/ext/alglib/estnorm.h +107 -0
- data/ext/alglib/expintegrals.cpp +422 -0
- data/ext/alglib/expintegrals.h +108 -0
- data/ext/alglib/faq.english.html +258 -0
- data/ext/alglib/faq.russian.html +272 -0
- data/ext/alglib/fdistr.cpp +202 -0
- data/ext/alglib/fdistr.h +163 -0
- data/ext/alglib/fresnel.cpp +211 -0
- data/ext/alglib/fresnel.h +91 -0
- data/ext/alglib/gammaf.cpp +338 -0
- data/ext/alglib/gammaf.h +104 -0
- data/ext/alglib/gqgengauss.cpp +235 -0
- data/ext/alglib/gqgengauss.h +92 -0
- data/ext/alglib/gqgenhermite.cpp +268 -0
- data/ext/alglib/gqgenhermite.h +63 -0
- data/ext/alglib/gqgenjacobi.cpp +297 -0
- data/ext/alglib/gqgenjacobi.h +72 -0
- data/ext/alglib/gqgenlaguerre.cpp +265 -0
- data/ext/alglib/gqgenlaguerre.h +69 -0
- data/ext/alglib/gqgenlegendre.cpp +300 -0
- data/ext/alglib/gqgenlegendre.h +62 -0
- data/ext/alglib/gqgenlobatto.cpp +305 -0
- data/ext/alglib/gqgenlobatto.h +97 -0
- data/ext/alglib/gqgenradau.cpp +232 -0
- data/ext/alglib/gqgenradau.h +95 -0
- data/ext/alglib/hbisinv.cpp +480 -0
- data/ext/alglib/hbisinv.h +183 -0
- data/ext/alglib/hblas.cpp +228 -0
- data/ext/alglib/hblas.h +64 -0
- data/ext/alglib/hcholesky.cpp +339 -0
- data/ext/alglib/hcholesky.h +91 -0
- data/ext/alglib/hermite.cpp +114 -0
- data/ext/alglib/hermite.h +49 -0
- data/ext/alglib/hessenberg.cpp +370 -0
- data/ext/alglib/hessenberg.h +152 -0
- data/ext/alglib/hevd.cpp +247 -0
- data/ext/alglib/hevd.h +107 -0
- data/ext/alglib/hsschur.cpp +1316 -0
- data/ext/alglib/hsschur.h +108 -0
- data/ext/alglib/htridiagonal.cpp +734 -0
- data/ext/alglib/htridiagonal.h +180 -0
- data/ext/alglib/ialglib.cpp +6 -0
- data/ext/alglib/ialglib.h +9 -0
- data/ext/alglib/ibetaf.cpp +960 -0
- data/ext/alglib/ibetaf.h +125 -0
- data/ext/alglib/igammaf.cpp +430 -0
- data/ext/alglib/igammaf.h +157 -0
- data/ext/alglib/inv.cpp +274 -0
- data/ext/alglib/inv.h +115 -0
- data/ext/alglib/inverseupdate.cpp +480 -0
- data/ext/alglib/inverseupdate.h +185 -0
- data/ext/alglib/jacobianelliptic.cpp +164 -0
- data/ext/alglib/jacobianelliptic.h +94 -0
- data/ext/alglib/jarquebera.cpp +2271 -0
- data/ext/alglib/jarquebera.h +80 -0
- data/ext/alglib/kmeans.cpp +356 -0
- data/ext/alglib/kmeans.h +76 -0
- data/ext/alglib/laguerre.cpp +94 -0
- data/ext/alglib/laguerre.h +48 -0
- data/ext/alglib/lbfgs.cpp +1167 -0
- data/ext/alglib/lbfgs.h +218 -0
- data/ext/alglib/lda.cpp +434 -0
- data/ext/alglib/lda.h +133 -0
- data/ext/alglib/ldlt.cpp +1130 -0
- data/ext/alglib/ldlt.h +124 -0
- data/ext/alglib/leastsquares.cpp +1252 -0
- data/ext/alglib/leastsquares.h +290 -0
- data/ext/alglib/legendre.cpp +107 -0
- data/ext/alglib/legendre.h +49 -0
- data/ext/alglib/linreg.cpp +1185 -0
- data/ext/alglib/linreg.h +380 -0
- data/ext/alglib/logit.cpp +1523 -0
- data/ext/alglib/logit.h +333 -0
- data/ext/alglib/lq.cpp +399 -0
- data/ext/alglib/lq.h +160 -0
- data/ext/alglib/lu.cpp +462 -0
- data/ext/alglib/lu.h +119 -0
- data/ext/alglib/mannwhitneyu.cpp +4490 -0
- data/ext/alglib/mannwhitneyu.h +115 -0
- data/ext/alglib/minlm.cpp +918 -0
- data/ext/alglib/minlm.h +312 -0
- data/ext/alglib/mlpbase.cpp +3375 -0
- data/ext/alglib/mlpbase.h +589 -0
- data/ext/alglib/mlpe.cpp +1369 -0
- data/ext/alglib/mlpe.h +552 -0
- data/ext/alglib/mlptrain.cpp +1056 -0
- data/ext/alglib/mlptrain.h +283 -0
- data/ext/alglib/nearunityunit.cpp +91 -0
- data/ext/alglib/nearunityunit.h +17 -0
- data/ext/alglib/normaldistr.cpp +377 -0
- data/ext/alglib/normaldistr.h +175 -0
- data/ext/alglib/nsevd.cpp +1869 -0
- data/ext/alglib/nsevd.h +140 -0
- data/ext/alglib/pca.cpp +168 -0
- data/ext/alglib/pca.h +87 -0
- data/ext/alglib/poissondistr.cpp +143 -0
- data/ext/alglib/poissondistr.h +130 -0
- data/ext/alglib/polinterpolation.cpp +685 -0
- data/ext/alglib/polinterpolation.h +206 -0
- data/ext/alglib/psif.cpp +173 -0
- data/ext/alglib/psif.h +88 -0
- data/ext/alglib/qr.cpp +414 -0
- data/ext/alglib/qr.h +168 -0
- data/ext/alglib/ratinterpolation.cpp +134 -0
- data/ext/alglib/ratinterpolation.h +72 -0
- data/ext/alglib/rcond.cpp +705 -0
- data/ext/alglib/rcond.h +140 -0
- data/ext/alglib/reflections.cpp +504 -0
- data/ext/alglib/reflections.h +165 -0
- data/ext/alglib/rotations.cpp +473 -0
- data/ext/alglib/rotations.h +128 -0
- data/ext/alglib/rsolve.cpp +221 -0
- data/ext/alglib/rsolve.h +99 -0
- data/ext/alglib/sbisinv.cpp +217 -0
- data/ext/alglib/sbisinv.h +171 -0
- data/ext/alglib/sblas.cpp +185 -0
- data/ext/alglib/sblas.h +64 -0
- data/ext/alglib/schur.cpp +156 -0
- data/ext/alglib/schur.h +102 -0
- data/ext/alglib/sdet.cpp +193 -0
- data/ext/alglib/sdet.h +101 -0
- data/ext/alglib/sevd.cpp +116 -0
- data/ext/alglib/sevd.h +99 -0
- data/ext/alglib/sinverse.cpp +672 -0
- data/ext/alglib/sinverse.h +138 -0
- data/ext/alglib/spddet.cpp +138 -0
- data/ext/alglib/spddet.h +96 -0
- data/ext/alglib/spdgevd.cpp +842 -0
- data/ext/alglib/spdgevd.h +200 -0
- data/ext/alglib/spdinverse.cpp +509 -0
- data/ext/alglib/spdinverse.h +122 -0
- data/ext/alglib/spdrcond.cpp +421 -0
- data/ext/alglib/spdrcond.h +118 -0
- data/ext/alglib/spdsolve.cpp +275 -0
- data/ext/alglib/spdsolve.h +105 -0
- data/ext/alglib/spline2d.cpp +1192 -0
- data/ext/alglib/spline2d.h +301 -0
- data/ext/alglib/spline3.cpp +1264 -0
- data/ext/alglib/spline3.h +290 -0
- data/ext/alglib/srcond.cpp +595 -0
- data/ext/alglib/srcond.h +127 -0
- data/ext/alglib/ssolve.cpp +895 -0
- data/ext/alglib/ssolve.h +139 -0
- data/ext/alglib/stdafx.h +0 -0
- data/ext/alglib/stest.cpp +131 -0
- data/ext/alglib/stest.h +94 -0
- data/ext/alglib/studenttdistr.cpp +222 -0
- data/ext/alglib/studenttdistr.h +115 -0
- data/ext/alglib/studentttests.cpp +377 -0
- data/ext/alglib/studentttests.h +178 -0
- data/ext/alglib/svd.cpp +620 -0
- data/ext/alglib/svd.h +126 -0
- data/ext/alglib/tdbisinv.cpp +2608 -0
- data/ext/alglib/tdbisinv.h +228 -0
- data/ext/alglib/tdevd.cpp +1229 -0
- data/ext/alglib/tdevd.h +115 -0
- data/ext/alglib/tridiagonal.cpp +594 -0
- data/ext/alglib/tridiagonal.h +171 -0
- data/ext/alglib/trigintegrals.cpp +490 -0
- data/ext/alglib/trigintegrals.h +131 -0
- data/ext/alglib/trinverse.cpp +345 -0
- data/ext/alglib/trinverse.h +98 -0
- data/ext/alglib/trlinsolve.cpp +926 -0
- data/ext/alglib/trlinsolve.h +73 -0
- data/ext/alglib/tsort.cpp +405 -0
- data/ext/alglib/tsort.h +54 -0
- data/ext/alglib/variancetests.cpp +245 -0
- data/ext/alglib/variancetests.h +134 -0
- data/ext/alglib/wsr.cpp +6285 -0
- data/ext/alglib/wsr.h +96 -0
- data/ext/ap.i +97 -0
- data/ext/correlation.i +24 -0
- data/ext/extconf.rb +6 -0
- data/ext/logit.i +89 -0
- data/lib/alglib.rb +71 -0
- data/lib/alglib/correlation.rb +26 -0
- data/lib/alglib/linearregression.rb +63 -0
- data/lib/alglib/logit.rb +42 -0
- data/test/test_alglib.rb +52 -0
- data/test/test_correlation.rb +44 -0
- data/test/test_correlationtest.rb +45 -0
- data/test/test_linreg.rb +35 -0
- data/test/test_logit.rb +43 -0
- data/test/test_pca.rb +27 -0
- metadata +326 -0
@@ -0,0 +1,19 @@
|
|
1
|
+
#include "alglib_util.h"
|
2
|
+
#include <iostream>
|
3
|
+
|
4
|
+
VALUE real1d_to_array(ap::real_1d_array *x) {
|
5
|
+
VALUE ar=rb_ary_new2(x->gethighbound()-x->getlowbound());
|
6
|
+
for(uint i=x->getlowbound();i<=x->gethighbound();i++) {
|
7
|
+
rb_ary_push(ar,rb_float_new(x->operator()(i)));
|
8
|
+
}
|
9
|
+
return ar;
|
10
|
+
}
|
11
|
+
|
12
|
+
void array_to_real1d(VALUE ary, ap::real_1d_array *x) {
|
13
|
+
x->setlength(RARRAY_LEN(ary));
|
14
|
+
//std::cout << "Largo:" << RARRAY_LEN(ary) << "\n";
|
15
|
+
for(long i=0;i<RARRAY_LEN(ary);i++) {
|
16
|
+
// std::cout << "i:" << i << "=" << rb_num2dbl(RARRAY_PTR(ary)[i]) <<"\n";
|
17
|
+
x->operator()(i)=rb_num2dbl(RARRAY_PTR(ary)[i]);
|
18
|
+
}
|
19
|
+
}
|
@@ -0,0 +1,14 @@
|
|
1
|
+
#ifndef _alglib_util_h
|
2
|
+
#define _alglib_util_h
|
3
|
+
|
4
|
+
|
5
|
+
#include "ap.h"
|
6
|
+
#include "linreg.h"
|
7
|
+
#include "logit.h"
|
8
|
+
|
9
|
+
#include "ruby.h"
|
10
|
+
|
11
|
+
VALUE real1d_to_array(ap::real_1d_array *x);
|
12
|
+
void array_to_real1d(VALUE ary, ap::real_1d_array *x);
|
13
|
+
|
14
|
+
#endif
|
data/ext/alglib/ap.cpp
ADDED
@@ -0,0 +1,877 @@
|
|
1
|
+
/********************************************************************
|
2
|
+
AP Library version 1.2
|
3
|
+
Copyright (c) 2003-2007, Sergey Bochkanov (ALGLIB project).
|
4
|
+
See www.alglib.net or alglib.sources.ru for details.
|
5
|
+
|
6
|
+
Redistribution and use in source and binary forms, with or without
|
7
|
+
modification, are permitted provided that the following conditions are
|
8
|
+
met:
|
9
|
+
|
10
|
+
- Redistributions of source code must retain the above copyright
|
11
|
+
notice, this list of conditions and the following disclaimer.
|
12
|
+
|
13
|
+
- Redistributions in binary form must reproduce the above copyright
|
14
|
+
notice, this list of conditions and the following disclaimer listed
|
15
|
+
in this license in the documentation and/or other materials
|
16
|
+
provided with the distribution.
|
17
|
+
|
18
|
+
- Neither the name of the copyright holders nor the names of its
|
19
|
+
contributors may be used to endorse or promote products derived from
|
20
|
+
this software without specific prior written permission.
|
21
|
+
|
22
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
23
|
+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
24
|
+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
25
|
+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
26
|
+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
27
|
+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
28
|
+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
29
|
+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
30
|
+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
31
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
32
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
33
|
+
********************************************************************/
|
34
|
+
|
35
|
+
#include "stdafx.h"
|
36
|
+
#include "ap.h"
|
37
|
+
|
38
|
+
|
39
|
+
/********************************************************************
|
40
|
+
Optimized ABLAS interface
|
41
|
+
********************************************************************/
|
42
|
+
#ifdef AP_WIN32
|
43
|
+
#include <windows.h>
|
44
|
+
extern "C"
|
45
|
+
{
|
46
|
+
typedef double (*_ddot1)(const double*, const double*, long);
|
47
|
+
typedef void (*_dmove1)(const double*, const double*, long);
|
48
|
+
typedef void (*_dmoves1)(const double*, const double*, long, double);
|
49
|
+
typedef void (*_dmoveneg1)(const double*, const double*, long);
|
50
|
+
typedef void (*_dadd1)(const double*, const double*, long);
|
51
|
+
typedef void (*_dadds1)(const double*, const double*, long, double);
|
52
|
+
typedef void (*_dsub1)(const double*, const double*, long);
|
53
|
+
typedef void (*_dmuls1)(const double*, long, double);
|
54
|
+
}
|
55
|
+
HINSTANCE ABLAS = LoadLibrary("ablas.dll");
|
56
|
+
|
57
|
+
static _ddot1 ddot1 = ABLAS==NULL ? NULL : (_ddot1) GetProcAddress(ABLAS, "ASMDotProduct1");
|
58
|
+
static _dmove1 dmove1 = ABLAS==NULL ? NULL : (_dmove1) GetProcAddress(ABLAS, "ASMMove1");
|
59
|
+
static _dmoves1 dmoves1 = ABLAS==NULL ? NULL : (_dmoves1) GetProcAddress(ABLAS, "ASMMoveS1");
|
60
|
+
static _dmoveneg1 dmoveneg1 = ABLAS==NULL ? NULL : (_dmoveneg1) GetProcAddress(ABLAS, "ASMMoveNeg1");
|
61
|
+
static _dadd1 dadd1 = ABLAS==NULL ? NULL : (_dadd1) GetProcAddress(ABLAS, "ASMAdd1");
|
62
|
+
static _dadds1 dadds1 = ABLAS==NULL ? NULL : (_dadds1) GetProcAddress(ABLAS, "ASMAddS1");
|
63
|
+
static _dsub1 dsub1 = ABLAS==NULL ? NULL : (_dsub1) GetProcAddress(ABLAS, "ASMSub1");
|
64
|
+
static _dmuls1 dmuls1 = ABLAS==NULL ? NULL : (_dmuls1) GetProcAddress(ABLAS, "ASMMulS1");
|
65
|
+
#endif
|
66
|
+
|
67
|
+
const double ap::machineepsilon = 5E-16;
|
68
|
+
const double ap::maxrealnumber = 1E300;
|
69
|
+
const double ap::minrealnumber = 1E-300;
|
70
|
+
|
71
|
+
/********************************************************************
|
72
|
+
ap::complex operations
|
73
|
+
********************************************************************/
|
74
|
+
const bool ap::operator==(const ap::complex& lhs, const ap::complex& rhs)
|
75
|
+
{ return lhs.x==rhs.x && lhs.y==rhs.y; }
|
76
|
+
|
77
|
+
const bool ap::operator!=(const ap::complex& lhs, const ap::complex& rhs)
|
78
|
+
{ return lhs.x!=rhs.x || lhs.y!=rhs.y; }
|
79
|
+
|
80
|
+
const ap::complex ap::operator+(const ap::complex& lhs)
|
81
|
+
{ return lhs; }
|
82
|
+
|
83
|
+
const ap::complex ap::operator-(const ap::complex& lhs)
|
84
|
+
{ return ap::complex(-lhs.x, -lhs.y); }
|
85
|
+
|
86
|
+
const ap::complex ap::operator+(const ap::complex& lhs, const ap::complex& rhs)
|
87
|
+
{ ap::complex r = lhs; r += rhs; return r; }
|
88
|
+
|
89
|
+
const ap::complex ap::operator+(const ap::complex& lhs, const double& rhs)
|
90
|
+
{ ap::complex r = lhs; r += rhs; return r; }
|
91
|
+
|
92
|
+
const ap::complex ap::operator+(const double& lhs, const ap::complex& rhs)
|
93
|
+
{ ap::complex r = rhs; r += lhs; return r; }
|
94
|
+
|
95
|
+
const ap::complex ap::operator-(const ap::complex& lhs, const ap::complex& rhs)
|
96
|
+
{ ap::complex r = lhs; r -= rhs; return r; }
|
97
|
+
|
98
|
+
const ap::complex ap::operator-(const ap::complex& lhs, const double& rhs)
|
99
|
+
{ ap::complex r = lhs; r -= rhs; return r; }
|
100
|
+
|
101
|
+
const ap::complex ap::operator-(const double& lhs, const ap::complex& rhs)
|
102
|
+
{ ap::complex r = lhs; r -= rhs; return r; }
|
103
|
+
|
104
|
+
const ap::complex ap::operator*(const ap::complex& lhs, const ap::complex& rhs)
|
105
|
+
{ return ap::complex(lhs.x*rhs.x - lhs.y*rhs.y, lhs.x*rhs.y + lhs.y*rhs.x); }
|
106
|
+
|
107
|
+
const ap::complex ap::operator*(const ap::complex& lhs, const double& rhs)
|
108
|
+
{ return ap::complex(lhs.x*rhs, lhs.y*rhs); }
|
109
|
+
|
110
|
+
const ap::complex ap::operator*(const double& lhs, const ap::complex& rhs)
|
111
|
+
{ return ap::complex(lhs*rhs.x, lhs*rhs.y); }
|
112
|
+
|
113
|
+
const ap::complex ap::operator/(const ap::complex& lhs, const ap::complex& rhs)
|
114
|
+
{
|
115
|
+
ap::complex result;
|
116
|
+
double e;
|
117
|
+
double f;
|
118
|
+
if( fabs(rhs.y)<fabs(rhs.x) )
|
119
|
+
{
|
120
|
+
e = rhs.y/rhs.x;
|
121
|
+
f = rhs.x+rhs.y*e;
|
122
|
+
result.x = (lhs.x+lhs.y*e)/f;
|
123
|
+
result.y = (lhs.y-lhs.x*e)/f;
|
124
|
+
}
|
125
|
+
else
|
126
|
+
{
|
127
|
+
e = rhs.x/rhs.y;
|
128
|
+
f = rhs.y+rhs.x*e;
|
129
|
+
result.x = (lhs.y+lhs.x*e)/f;
|
130
|
+
result.y = (-lhs.x+lhs.y*e)/f;
|
131
|
+
}
|
132
|
+
return result;
|
133
|
+
}
|
134
|
+
|
135
|
+
const ap::complex ap::operator/(const double& lhs, const ap::complex& rhs)
|
136
|
+
{
|
137
|
+
ap::complex result;
|
138
|
+
double e;
|
139
|
+
double f;
|
140
|
+
if( fabs(rhs.y)<fabs(rhs.x) )
|
141
|
+
{
|
142
|
+
e = rhs.y/rhs.x;
|
143
|
+
f = rhs.x+rhs.y*e;
|
144
|
+
result.x = lhs/f;
|
145
|
+
result.y = -lhs*e/f;
|
146
|
+
}
|
147
|
+
else
|
148
|
+
{
|
149
|
+
e = rhs.x/rhs.y;
|
150
|
+
f = rhs.y+rhs.x*e;
|
151
|
+
result.x = lhs*e/f;
|
152
|
+
result.y = -lhs/f;
|
153
|
+
}
|
154
|
+
return result;
|
155
|
+
}
|
156
|
+
|
157
|
+
const ap::complex ap::operator/(const ap::complex& lhs, const double& rhs)
|
158
|
+
{ return ap::complex(lhs.x/rhs, lhs.y/rhs); }
|
159
|
+
|
160
|
+
const double ap::abscomplex(const ap::complex &z)
|
161
|
+
{
|
162
|
+
double w;
|
163
|
+
double xabs;
|
164
|
+
double yabs;
|
165
|
+
double v;
|
166
|
+
|
167
|
+
xabs = fabs(z.x);
|
168
|
+
yabs = fabs(z.y);
|
169
|
+
w = xabs>yabs ? xabs : yabs;
|
170
|
+
v = xabs<yabs ? xabs : yabs;
|
171
|
+
if( v==0 )
|
172
|
+
return w;
|
173
|
+
else
|
174
|
+
{
|
175
|
+
double t = v/w;
|
176
|
+
return w*sqrt(1+t*t);
|
177
|
+
}
|
178
|
+
}
|
179
|
+
|
180
|
+
const ap::complex ap::conj(const ap::complex &z)
|
181
|
+
{ return ap::complex(z.x, -z.y); }
|
182
|
+
|
183
|
+
const ap::complex ap::csqr(const ap::complex &z)
|
184
|
+
{ return ap::complex(z.x*z.x-z.y*z.y, 2*z.x*z.y); }
|
185
|
+
|
186
|
+
/********************************************************************
|
187
|
+
BLAS functions
|
188
|
+
********************************************************************/
|
189
|
+
double ap::vdotproduct(const double *v1, const double *v2, int N)
|
190
|
+
{
|
191
|
+
#ifdef AP_WIN32
|
192
|
+
if( ddot1!=NULL )
|
193
|
+
return ddot1(v1, v2, N);
|
194
|
+
#endif
|
195
|
+
return ap::_vdotproduct<double>(v1, v2, N);
|
196
|
+
}
|
197
|
+
|
198
|
+
ap::complex ap::vdotproduct(const ap::complex *v1, const ap::complex *v2, int N)
|
199
|
+
{
|
200
|
+
return ap::_vdotproduct<ap::complex>(v1, v2, N);
|
201
|
+
}
|
202
|
+
|
203
|
+
void ap::vmove(double *vdst, const double* vsrc, int N)
|
204
|
+
{
|
205
|
+
#ifdef AP_WIN32
|
206
|
+
if( dmove1!=NULL )
|
207
|
+
{
|
208
|
+
dmove1(vdst, vsrc, N);
|
209
|
+
return;
|
210
|
+
}
|
211
|
+
#endif
|
212
|
+
ap::_vmove<double>(vdst, vsrc, N);
|
213
|
+
}
|
214
|
+
|
215
|
+
void ap::vmove(ap::complex *vdst, const ap::complex* vsrc, int N)
|
216
|
+
{
|
217
|
+
ap::_vmove<ap::complex>(vdst, vsrc, N);
|
218
|
+
}
|
219
|
+
|
220
|
+
void ap::vmoveneg(double *vdst, const double *vsrc, int N)
|
221
|
+
{
|
222
|
+
#ifdef AP_WIN32
|
223
|
+
if( dmoveneg1!=NULL )
|
224
|
+
{
|
225
|
+
dmoveneg1(vdst, vsrc, N);
|
226
|
+
return;
|
227
|
+
}
|
228
|
+
#endif
|
229
|
+
ap::_vmoveneg<double>(vdst, vsrc, N);
|
230
|
+
}
|
231
|
+
|
232
|
+
void ap::vmoveneg(ap::complex *vdst, const ap::complex *vsrc, int N)
|
233
|
+
{
|
234
|
+
ap::_vmoveneg<ap::complex>(vdst, vsrc, N);
|
235
|
+
}
|
236
|
+
|
237
|
+
void ap::vmove(double *vdst, const double *vsrc, int N, double alpha)
|
238
|
+
{
|
239
|
+
#ifdef AP_WIN32
|
240
|
+
if( dmoves1!=NULL )
|
241
|
+
{
|
242
|
+
dmoves1(vdst, vsrc, N, alpha);
|
243
|
+
return;
|
244
|
+
}
|
245
|
+
#endif
|
246
|
+
ap::_vmove<double,double>(vdst, vsrc, N, alpha);
|
247
|
+
}
|
248
|
+
|
249
|
+
void ap::vmove(ap::complex *vdst, const ap::complex *vsrc, int N, double alpha)
|
250
|
+
{
|
251
|
+
ap::_vmove<ap::complex,double>(vdst, vsrc, N, alpha);
|
252
|
+
}
|
253
|
+
|
254
|
+
void ap::vmove(ap::complex *vdst, const ap::complex *vsrc, int N, ap::complex alpha)
|
255
|
+
{
|
256
|
+
ap::_vmove<ap::complex,ap::complex>(vdst, vsrc, N, alpha);
|
257
|
+
}
|
258
|
+
|
259
|
+
void ap::vadd(double *vdst, const double *vsrc, int N)
|
260
|
+
{
|
261
|
+
#ifdef AP_WIN32
|
262
|
+
if( dadd1!=NULL )
|
263
|
+
{
|
264
|
+
dadd1(vdst, vsrc, N);
|
265
|
+
return;
|
266
|
+
}
|
267
|
+
#endif
|
268
|
+
ap::_vadd<double>(vdst, vsrc, N);
|
269
|
+
}
|
270
|
+
|
271
|
+
void ap::vadd(ap::complex *vdst, const ap::complex *vsrc, int N)
|
272
|
+
{
|
273
|
+
ap::_vadd<ap::complex>(vdst, vsrc, N);
|
274
|
+
}
|
275
|
+
|
276
|
+
void ap::vadd(double *vdst, const double *vsrc, int N, double alpha)
|
277
|
+
{
|
278
|
+
#ifdef AP_WIN32
|
279
|
+
if( dadds1!=NULL )
|
280
|
+
{
|
281
|
+
dadds1(vdst, vsrc, N, alpha);
|
282
|
+
return;
|
283
|
+
}
|
284
|
+
#endif
|
285
|
+
ap::_vadd<double,double>(vdst, vsrc, N, alpha);
|
286
|
+
}
|
287
|
+
|
288
|
+
void ap::vadd(ap::complex *vdst, const ap::complex *vsrc, int N, double alpha)
|
289
|
+
{
|
290
|
+
ap::_vadd<ap::complex,double>(vdst, vsrc, N, alpha);
|
291
|
+
}
|
292
|
+
|
293
|
+
void ap::vadd(ap::complex *vdst, const ap::complex *vsrc, int N, ap::complex alpha)
|
294
|
+
{
|
295
|
+
ap::_vadd<ap::complex,ap::complex>(vdst, vsrc, N, alpha);
|
296
|
+
}
|
297
|
+
|
298
|
+
void ap::vsub(double *vdst, const double *vsrc, int N)
|
299
|
+
{
|
300
|
+
#ifdef AP_WIN32
|
301
|
+
if( dsub1!=NULL )
|
302
|
+
{
|
303
|
+
dsub1(vdst, vsrc, N);
|
304
|
+
return;
|
305
|
+
}
|
306
|
+
#endif
|
307
|
+
ap::_vsub<double>(vdst, vsrc, N);
|
308
|
+
}
|
309
|
+
|
310
|
+
void ap::vsub(ap::complex *vdst, const ap::complex *vsrc, int N)
|
311
|
+
{
|
312
|
+
ap::_vsub<ap::complex>(vdst, vsrc, N);
|
313
|
+
}
|
314
|
+
|
315
|
+
void ap::vsub(double *vdst, const double *vsrc, int N, double alpha)
|
316
|
+
{
|
317
|
+
#ifdef AP_WIN32
|
318
|
+
if( dadds1!=NULL )
|
319
|
+
{
|
320
|
+
dadds1(vdst, vsrc, N, -alpha);
|
321
|
+
return;
|
322
|
+
}
|
323
|
+
#endif
|
324
|
+
ap::_vsub<double,double>(vdst, vsrc, N, alpha);
|
325
|
+
}
|
326
|
+
|
327
|
+
void ap::vsub(ap::complex *vdst, const ap::complex *vsrc, int N, double alpha)
|
328
|
+
{
|
329
|
+
ap::_vsub<ap::complex,double>(vdst, vsrc, N, alpha);
|
330
|
+
}
|
331
|
+
|
332
|
+
void ap::vsub(ap::complex *vdst, const ap::complex *vsrc, int N, ap::complex alpha)
|
333
|
+
{
|
334
|
+
ap::_vsub<ap::complex,ap::complex>(vdst, vsrc, N, alpha);
|
335
|
+
}
|
336
|
+
|
337
|
+
void ap::vmul(double *vdst, int N, double alpha)
|
338
|
+
{
|
339
|
+
#ifdef AP_WIN32
|
340
|
+
if( dmuls1!=NULL )
|
341
|
+
{
|
342
|
+
dmuls1(vdst, N, alpha);
|
343
|
+
return;
|
344
|
+
}
|
345
|
+
#endif
|
346
|
+
ap::_vmul<double,double>(vdst, N, alpha);
|
347
|
+
}
|
348
|
+
|
349
|
+
void ap::vmul(ap::complex *vdst, int N, double alpha)
|
350
|
+
{
|
351
|
+
ap::_vmul<ap::complex,double>(vdst, N, alpha);
|
352
|
+
}
|
353
|
+
|
354
|
+
void ap::vmul(ap::complex *vdst, int N, ap::complex alpha)
|
355
|
+
{
|
356
|
+
ap::_vmul<ap::complex,ap::complex>(vdst, N, alpha);
|
357
|
+
}
|
358
|
+
|
359
|
+
/********************************************************************
|
360
|
+
standard functions
|
361
|
+
********************************************************************/
|
362
|
+
int ap::sign(double x)
|
363
|
+
{
|
364
|
+
if( x>0 ) return 1;
|
365
|
+
if( x<0 ) return -1;
|
366
|
+
return 0;
|
367
|
+
}
|
368
|
+
|
369
|
+
double ap::randomreal()
|
370
|
+
{
|
371
|
+
int i1 = rand();
|
372
|
+
int i2 = rand();
|
373
|
+
while(i1==RAND_MAX)
|
374
|
+
i1 =rand();
|
375
|
+
while(i2==RAND_MAX)
|
376
|
+
i2 =rand();
|
377
|
+
double mx = RAND_MAX;
|
378
|
+
return (i1+i2/mx)/mx;
|
379
|
+
}
|
380
|
+
|
381
|
+
int ap::randominteger(int maxv)
|
382
|
+
{ return rand()%maxv; }
|
383
|
+
|
384
|
+
int ap::round(double x)
|
385
|
+
{ return int(floor(x+0.5)); }
|
386
|
+
|
387
|
+
int ap::trunc(double x)
|
388
|
+
{ return int(x>0 ? floor(x) : ceil(x)); }
|
389
|
+
|
390
|
+
int ap::ifloor(double x)
|
391
|
+
{ return int(floor(x)); }
|
392
|
+
|
393
|
+
int ap::iceil(double x)
|
394
|
+
{ return int(ceil(x)); }
|
395
|
+
|
396
|
+
double ap::pi()
|
397
|
+
{ return 3.14159265358979323846; }
|
398
|
+
|
399
|
+
double ap::sqr(double x)
|
400
|
+
{ return x*x; }
|
401
|
+
|
402
|
+
int ap::maxint(int m1, int m2)
|
403
|
+
{
|
404
|
+
return m1>m2 ? m1 : m2;
|
405
|
+
}
|
406
|
+
|
407
|
+
int ap::minint(int m1, int m2)
|
408
|
+
{
|
409
|
+
return m1>m2 ? m2 : m1;
|
410
|
+
}
|
411
|
+
|
412
|
+
double ap::maxreal(double m1, double m2)
|
413
|
+
{
|
414
|
+
return m1>m2 ? m1 : m2;
|
415
|
+
}
|
416
|
+
|
417
|
+
double ap::minreal(double m1, double m2)
|
418
|
+
{
|
419
|
+
return m1>m2 ? m2 : m1;
|
420
|
+
}
|
421
|
+
|
422
|
+
/********************************************************************
|
423
|
+
Dataset functions
|
424
|
+
********************************************************************/
|
425
|
+
bool ap::readstrings(std::string file, std::list<std::string> *pOutput)
|
426
|
+
{
|
427
|
+
return readstrings(file, pOutput, "");
|
428
|
+
}
|
429
|
+
|
430
|
+
bool ap::readstrings(std::string file, std::list<std::string> *pOutput, std::string comment)
|
431
|
+
{
|
432
|
+
std::string cmd, s;
|
433
|
+
FILE *f;
|
434
|
+
char buf[32768];
|
435
|
+
char *str;
|
436
|
+
|
437
|
+
f = fopen(file.c_str(), "rb");
|
438
|
+
if( !f )
|
439
|
+
return false;
|
440
|
+
s = "";
|
441
|
+
pOutput->clear();
|
442
|
+
while(str=fgets(buf, sizeof(buf), f))
|
443
|
+
{
|
444
|
+
// TODO: read file by small chunks, combine in one large string
|
445
|
+
if( strlen(str)==0 )
|
446
|
+
continue;
|
447
|
+
|
448
|
+
//
|
449
|
+
// trim trailing newline chars
|
450
|
+
//
|
451
|
+
char *eos = str+strlen(str)-1;
|
452
|
+
if( *eos=='\n' )
|
453
|
+
{
|
454
|
+
*eos = 0;
|
455
|
+
eos--;
|
456
|
+
}
|
457
|
+
if( *eos=='\r' )
|
458
|
+
{
|
459
|
+
*eos = 0;
|
460
|
+
eos--;
|
461
|
+
}
|
462
|
+
s = str;
|
463
|
+
|
464
|
+
//
|
465
|
+
// skip comments
|
466
|
+
//
|
467
|
+
if( comment.length()>0 )
|
468
|
+
if( strncmp(s.c_str(), comment.c_str(), comment.length())==0 )
|
469
|
+
{
|
470
|
+
s = "";
|
471
|
+
continue;
|
472
|
+
}
|
473
|
+
|
474
|
+
//
|
475
|
+
// read data
|
476
|
+
//
|
477
|
+
if( s.length()<1 )
|
478
|
+
{
|
479
|
+
fclose(f);
|
480
|
+
throw ap::ap_error("internal error in read_strings");
|
481
|
+
}
|
482
|
+
pOutput->push_back(s);
|
483
|
+
}
|
484
|
+
fclose(f);
|
485
|
+
return true;
|
486
|
+
}
|
487
|
+
|
488
|
+
void ap::explodestring(std::string s, char sep, std::vector<std::string> *pOutput)
|
489
|
+
{
|
490
|
+
std::string tmp;
|
491
|
+
int i;
|
492
|
+
tmp = "";
|
493
|
+
pOutput->clear();
|
494
|
+
for(i=0; i<s.length(); i++)
|
495
|
+
{
|
496
|
+
if( s[i]!=sep )
|
497
|
+
{
|
498
|
+
tmp += s[i];
|
499
|
+
continue;
|
500
|
+
}
|
501
|
+
//if( tmp.length()!=0 )
|
502
|
+
pOutput->push_back(tmp);
|
503
|
+
tmp = "";
|
504
|
+
}
|
505
|
+
if( tmp.length()!=0 )
|
506
|
+
pOutput->push_back(tmp);
|
507
|
+
}
|
508
|
+
|
509
|
+
std::string ap::strtolower(const std::string &s)
|
510
|
+
{
|
511
|
+
std::string r = s;
|
512
|
+
for(int i=0; i<r.length(); i++)
|
513
|
+
r[i] = tolower(r[i]);
|
514
|
+
return r;
|
515
|
+
}
|
516
|
+
|
517
|
+
std::string ap::xtrim(std::string s)
|
518
|
+
{
|
519
|
+
char *pstr = (char*)malloc(s.length()+1);
|
520
|
+
char *p2 = pstr;
|
521
|
+
if( pstr==NULL )
|
522
|
+
throw "xalloc in xtrim()";
|
523
|
+
try
|
524
|
+
{
|
525
|
+
bool bws;
|
526
|
+
int i;
|
527
|
+
|
528
|
+
//
|
529
|
+
// special cases:
|
530
|
+
// * zero length string
|
531
|
+
// * string includes only spaces
|
532
|
+
//
|
533
|
+
if( s.length()==0 )
|
534
|
+
{
|
535
|
+
free(pstr);
|
536
|
+
return "";
|
537
|
+
}
|
538
|
+
bws = true;
|
539
|
+
for(i=0; i<s.length(); i++)
|
540
|
+
if( s[i]!=' ' )
|
541
|
+
bws = false;
|
542
|
+
if( bws )
|
543
|
+
{
|
544
|
+
free(pstr);
|
545
|
+
return "";
|
546
|
+
}
|
547
|
+
|
548
|
+
//
|
549
|
+
// merge internal spaces
|
550
|
+
//
|
551
|
+
bws = false;
|
552
|
+
for(i=0; i<s.length(); i++)
|
553
|
+
{
|
554
|
+
if( s[i]==' ' && bws )
|
555
|
+
continue;
|
556
|
+
if( s[i]==' ' )
|
557
|
+
{
|
558
|
+
*p2 = ' ';
|
559
|
+
p2++;
|
560
|
+
bws = true;
|
561
|
+
continue;
|
562
|
+
}
|
563
|
+
*p2 = s[i];
|
564
|
+
bws = false;
|
565
|
+
p2++;
|
566
|
+
}
|
567
|
+
*p2 = 0;
|
568
|
+
|
569
|
+
//
|
570
|
+
// trim leading/trailing spaces.
|
571
|
+
// we expect at least one non-space character in the string
|
572
|
+
//
|
573
|
+
p2--;
|
574
|
+
while(*p2==' ')
|
575
|
+
{
|
576
|
+
*p2 = 0;
|
577
|
+
p2--;
|
578
|
+
}
|
579
|
+
p2 = pstr;
|
580
|
+
while((*p2)==' ')
|
581
|
+
p2++;
|
582
|
+
|
583
|
+
//
|
584
|
+
// result
|
585
|
+
//
|
586
|
+
std::string r = p2;
|
587
|
+
free(pstr);
|
588
|
+
return r;
|
589
|
+
}
|
590
|
+
catch(...)
|
591
|
+
{
|
592
|
+
free(pstr);
|
593
|
+
throw "unknown exception in xtrim()";
|
594
|
+
}
|
595
|
+
}
|
596
|
+
|
597
|
+
bool ap::opendataset(std::string file, dataset *pdataset)
|
598
|
+
{
|
599
|
+
std::list<std::string> Lines;
|
600
|
+
std::vector<std::string> Values, RowsArr, ColsArr, VarsArr, HeadArr;
|
601
|
+
std::list<std::string>::iterator i;
|
602
|
+
std::string s;
|
603
|
+
int TrnFirst, TrnLast, ValFirst, ValLast, TstFirst, TstLast, LinesRead, j;
|
604
|
+
|
605
|
+
//
|
606
|
+
// Read data
|
607
|
+
//
|
608
|
+
if( pdataset==NULL )
|
609
|
+
return false;
|
610
|
+
if( !readstrings(file, &Lines, "//") )
|
611
|
+
return false;
|
612
|
+
i = Lines.begin();
|
613
|
+
*pdataset = dataset();
|
614
|
+
|
615
|
+
//
|
616
|
+
// Read header
|
617
|
+
//
|
618
|
+
if( i==Lines.end() )
|
619
|
+
return false;
|
620
|
+
s = ap::xtrim(*i);
|
621
|
+
ap::explodestring(s, '#', &HeadArr);
|
622
|
+
if( HeadArr.size()!=2 )
|
623
|
+
return false;
|
624
|
+
|
625
|
+
//
|
626
|
+
// Rows info
|
627
|
+
//
|
628
|
+
ap::explodestring(ap::xtrim(HeadArr[0]), ' ', &RowsArr);
|
629
|
+
if( RowsArr.size()==0 || RowsArr.size()>3 )
|
630
|
+
return false;
|
631
|
+
if( RowsArr.size()==1 )
|
632
|
+
{
|
633
|
+
pdataset->totalsize = atol(RowsArr[0].c_str());
|
634
|
+
pdataset->trnsize = pdataset->totalsize;
|
635
|
+
}
|
636
|
+
if( RowsArr.size()==2 )
|
637
|
+
{
|
638
|
+
pdataset->trnsize = atol(RowsArr[0].c_str());
|
639
|
+
pdataset->tstsize = atol(RowsArr[1].c_str());
|
640
|
+
pdataset->totalsize = pdataset->trnsize + pdataset->tstsize;
|
641
|
+
}
|
642
|
+
if( RowsArr.size()==3 )
|
643
|
+
{
|
644
|
+
pdataset->trnsize = atol(RowsArr[0].c_str());
|
645
|
+
pdataset->valsize = atol(RowsArr[1].c_str());
|
646
|
+
pdataset->tstsize = atol(RowsArr[2].c_str());
|
647
|
+
pdataset->totalsize = pdataset->trnsize + pdataset->valsize + pdataset->tstsize;
|
648
|
+
}
|
649
|
+
if( pdataset->totalsize<=0 || pdataset->trnsize<0 || pdataset->valsize<0 || pdataset->tstsize<0 )
|
650
|
+
return false;
|
651
|
+
TrnFirst = 0;
|
652
|
+
TrnLast = TrnFirst + pdataset->trnsize;
|
653
|
+
ValFirst = TrnLast;
|
654
|
+
ValLast = ValFirst + pdataset->valsize;
|
655
|
+
TstFirst = ValLast;
|
656
|
+
TstLast = TstFirst + pdataset->tstsize;
|
657
|
+
|
658
|
+
//
|
659
|
+
// columns
|
660
|
+
//
|
661
|
+
ap::explodestring(ap::xtrim(HeadArr[1]), ' ', &ColsArr);
|
662
|
+
if( ColsArr.size()!=1 && ColsArr.size()!=4 )
|
663
|
+
return false;
|
664
|
+
if( ColsArr.size()==1 )
|
665
|
+
{
|
666
|
+
pdataset->nin = atoi(ColsArr[0].c_str());
|
667
|
+
if( pdataset->nin<=0 )
|
668
|
+
return false;
|
669
|
+
}
|
670
|
+
if( ColsArr.size()==4 )
|
671
|
+
{
|
672
|
+
if( ap::strtolower(ColsArr[0])!="reg" && ap::strtolower(ColsArr[0])!="cls" )
|
673
|
+
return false;
|
674
|
+
if( ColsArr[2]!="=>" )
|
675
|
+
return false;
|
676
|
+
pdataset->nin = atol(ColsArr[1].c_str());
|
677
|
+
if( pdataset->nin<1 )
|
678
|
+
return false;
|
679
|
+
if( ap::strtolower(ColsArr[0])=="reg" )
|
680
|
+
{
|
681
|
+
pdataset->nclasses = 0;
|
682
|
+
pdataset->nout = atol(ColsArr[3].c_str());
|
683
|
+
if( pdataset->nout<1 )
|
684
|
+
return false;
|
685
|
+
}
|
686
|
+
else
|
687
|
+
{
|
688
|
+
pdataset->nclasses = atol(ColsArr[3].c_str());
|
689
|
+
pdataset->nout = 1;
|
690
|
+
if( pdataset->nclasses<2 )
|
691
|
+
return false;
|
692
|
+
}
|
693
|
+
}
|
694
|
+
|
695
|
+
//
|
696
|
+
// initialize arrays
|
697
|
+
//
|
698
|
+
pdataset->all.setlength(pdataset->totalsize, pdataset->nin+pdataset->nout);
|
699
|
+
if( pdataset->trnsize>0 ) pdataset->trn.setlength(pdataset->trnsize, pdataset->nin+pdataset->nout);
|
700
|
+
if( pdataset->valsize>0 ) pdataset->val.setlength(pdataset->valsize, pdataset->nin+pdataset->nout);
|
701
|
+
if( pdataset->tstsize>0 ) pdataset->tst.setlength(pdataset->tstsize, pdataset->nin+pdataset->nout);
|
702
|
+
|
703
|
+
//
|
704
|
+
// read data
|
705
|
+
//
|
706
|
+
for(LinesRead=0, i++; i!=Lines.end() && LinesRead<pdataset->totalsize; i++, LinesRead++)
|
707
|
+
{
|
708
|
+
std::string sss = *i;
|
709
|
+
ap::explodestring(ap::xtrim(*i), ' ', &VarsArr);
|
710
|
+
if( VarsArr.size()!=pdataset->nin+pdataset->nout )
|
711
|
+
return false;
|
712
|
+
int tmpc = ap::round(atof(VarsArr[pdataset->nin+pdataset->nout-1].c_str()));
|
713
|
+
if( pdataset->nclasses>0 && (tmpc<0 || tmpc>=pdataset->nclasses) )
|
714
|
+
return false;
|
715
|
+
for(j=0; j<pdataset->nin+pdataset->nout; j++)
|
716
|
+
{
|
717
|
+
pdataset->all(LinesRead,j) = atof(VarsArr[j].c_str());
|
718
|
+
if( LinesRead>=TrnFirst && LinesRead<TrnLast )
|
719
|
+
pdataset->trn(LinesRead-TrnFirst,j) = atof(VarsArr[j].c_str());
|
720
|
+
if( LinesRead>=ValFirst && LinesRead<ValLast )
|
721
|
+
pdataset->val(LinesRead-ValFirst,j) = atof(VarsArr[j].c_str());
|
722
|
+
if( LinesRead>=TstFirst && LinesRead<TstLast )
|
723
|
+
pdataset->tst(LinesRead-TstFirst,j) = atof(VarsArr[j].c_str());
|
724
|
+
}
|
725
|
+
}
|
726
|
+
if( LinesRead!=pdataset->totalsize )
|
727
|
+
return false;
|
728
|
+
return true;
|
729
|
+
}
|
730
|
+
|
731
|
+
/*bool ap::opendataset(std::string file, dataset *pdataset)
|
732
|
+
{
|
733
|
+
std::list<std::string> Lines;
|
734
|
+
std::vector<std::string> Values;
|
735
|
+
std::list<std::string>::iterator i;
|
736
|
+
int nCol, nRow, nSplitted;
|
737
|
+
int nColumns, nRows;
|
738
|
+
|
739
|
+
//
|
740
|
+
// Read data
|
741
|
+
//
|
742
|
+
if( pdataset==NULL )
|
743
|
+
return false;
|
744
|
+
if( !readstrings(file, &Lines, "//") )
|
745
|
+
return false;
|
746
|
+
i = Lines.begin();
|
747
|
+
*pdataset = dataset();
|
748
|
+
|
749
|
+
//
|
750
|
+
// Read columns info
|
751
|
+
//
|
752
|
+
if( i==Lines.end() )
|
753
|
+
return false;
|
754
|
+
if( sscanf(i->c_str(), " columns = %d %d ", &pdataset->nin, &pdataset->nout)!=2 )
|
755
|
+
return false;
|
756
|
+
if( pdataset->nin<=0 || pdataset->nout==0 || pdataset->nout==-1)
|
757
|
+
return false;
|
758
|
+
if( pdataset->nout<0 )
|
759
|
+
{
|
760
|
+
pdataset->nclasses = -pdataset->nout;
|
761
|
+
pdataset->nout = 1;
|
762
|
+
pdataset->iscls = true;
|
763
|
+
}
|
764
|
+
else
|
765
|
+
{
|
766
|
+
pdataset->isreg = true;
|
767
|
+
}
|
768
|
+
nColumns = pdataset->nin+pdataset->nout;
|
769
|
+
i++;
|
770
|
+
|
771
|
+
//
|
772
|
+
// Read rows info
|
773
|
+
//
|
774
|
+
if( i==Lines.end() )
|
775
|
+
return false;
|
776
|
+
if( sscanf(i->c_str(), " rows = %d %d %d ", &pdataset->trnsize, &pdataset->valsize, &pdataset->tstsize)!=3 )
|
777
|
+
return false;
|
778
|
+
if( (pdataset->trnsize<0) || (pdataset->valsize<0) || (pdataset->tstsize<0) )
|
779
|
+
return false;
|
780
|
+
if( (pdataset->trnsize==0) && (pdataset->valsize==0) && (pdataset->tstsize==0) )
|
781
|
+
return false;
|
782
|
+
nRows = pdataset->trnsize+pdataset->valsize+pdataset->tstsize;
|
783
|
+
pdataset->size = nRows;
|
784
|
+
if( Lines.size()!=nRows+2 )
|
785
|
+
return false;
|
786
|
+
i++;
|
787
|
+
|
788
|
+
//
|
789
|
+
// Read all cases
|
790
|
+
//
|
791
|
+
ap::real_2d_array &arr = pdataset->all;
|
792
|
+
arr.setbounds(0, nRows-1, 0, nColumns-1);
|
793
|
+
for(nRow=0; nRow<nRows; nRow++)
|
794
|
+
{
|
795
|
+
ap::ap_error::make_assertion(i!=Lines.end());
|
796
|
+
explodestring(*i, '\t', &Values);
|
797
|
+
if( Values.size()!=nColumns )
|
798
|
+
return false;
|
799
|
+
for(nCol=0; nCol<nColumns; nCol++)
|
800
|
+
{
|
801
|
+
double v;
|
802
|
+
if( sscanf(Values[nCol].c_str(), "%lg", &v)!=1 )
|
803
|
+
return false;
|
804
|
+
if( (nCol==nColumns-1) && pdataset->iscls && ((round(v)<0) || (round(v)>=pdataset->nclasses)) )
|
805
|
+
return false;
|
806
|
+
if( (nCol==nColumns-1) && pdataset->iscls )
|
807
|
+
arr(nRow, nCol) = round(v);
|
808
|
+
else
|
809
|
+
arr(nRow, nCol) = v;
|
810
|
+
}
|
811
|
+
i++;
|
812
|
+
}
|
813
|
+
|
814
|
+
//
|
815
|
+
// Split to training, validation and test sets
|
816
|
+
//
|
817
|
+
if( pdataset->trnsize>0 )
|
818
|
+
pdataset->trn.setbounds(0, pdataset->trnsize-1, 0, nColumns-1);
|
819
|
+
if( pdataset->valsize>0 )
|
820
|
+
pdataset->val.setbounds(0, pdataset->valsize-1, 0, nColumns-1);
|
821
|
+
if( pdataset->tstsize>0 )
|
822
|
+
pdataset->tst.setbounds(0, pdataset->tstsize-1, 0, nColumns-1);
|
823
|
+
nSplitted=0;
|
824
|
+
for(nRow=0; nRow<=pdataset->trnsize-1; nRow++, nSplitted++)
|
825
|
+
for(nCol=0; nCol<=nColumns-1; nCol++)
|
826
|
+
pdataset->trn(nRow,nCol) = arr(nSplitted,nCol);
|
827
|
+
for(nRow=0; nRow<=pdataset->valsize-1; nRow++, nSplitted++)
|
828
|
+
for(nCol=0; nCol<=nColumns-1; nCol++)
|
829
|
+
pdataset->val(nRow,nCol) = arr(nSplitted,nCol);
|
830
|
+
for(nRow=0; nRow<=pdataset->tstsize-1; nRow++, nSplitted++)
|
831
|
+
for(nCol=0; nCol<=nColumns-1; nCol++)
|
832
|
+
pdataset->tst(nRow,nCol) = arr(nSplitted,nCol);
|
833
|
+
return true;
|
834
|
+
}*/
|
835
|
+
|
836
|
+
/********************************************************************
|
837
|
+
Service routines:
|
838
|
+
********************************************************************/
|
839
|
+
void* ap::amalloc(size_t size, size_t alignment)
|
840
|
+
{
|
841
|
+
if( alignment<=1 )
|
842
|
+
{
|
843
|
+
//
|
844
|
+
// no alignment, just call malloc
|
845
|
+
//
|
846
|
+
void *block = malloc(sizeof(void*)+size);
|
847
|
+
void **p = (void**)block;
|
848
|
+
*p = block;
|
849
|
+
return (void*)((char*)block+sizeof(void*));
|
850
|
+
}
|
851
|
+
else
|
852
|
+
{
|
853
|
+
//
|
854
|
+
// align.
|
855
|
+
//
|
856
|
+
void *block = malloc(alignment-1+sizeof(void*)+size);
|
857
|
+
char *result = (char*)block+sizeof(void*);
|
858
|
+
//if( ((unsigned int)(result))%alignment!=0 )
|
859
|
+
// result += alignment - ((unsigned int)(result))%alignment;
|
860
|
+
if( (result-(char*)0)%alignment!=0 )
|
861
|
+
result += alignment - (result-(char*)0)%alignment;
|
862
|
+
*((void**)(result-sizeof(void*))) = block;
|
863
|
+
return result;
|
864
|
+
}
|
865
|
+
}
|
866
|
+
|
867
|
+
void ap::afree(void *block)
|
868
|
+
{
|
869
|
+
void *p = *((void**)((char*)block-sizeof(void*)));
|
870
|
+
free(p);
|
871
|
+
}
|
872
|
+
|
873
|
+
int ap::vlen(int n1, int n2)
|
874
|
+
{
|
875
|
+
return n2-n1+1;
|
876
|
+
}
|
877
|
+
|