-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaths.cpp
More file actions
647 lines (552 loc) · 15.6 KB
/
Copy pathmaths.cpp
File metadata and controls
647 lines (552 loc) · 15.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
// final.utils - Utility functions used by final projects.
// Copyright (c) 2004 - 2012 by Marek Sestak, marek.sestak@gmail.com
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
#include <math.h>
#include <float.h>
#include <string.h>
#include <stdlib.h>
#include "maths.h"
#include "utils.h"
namespace utils {
// ----------------------------------------------------------------------------
double Round( double anum, int an )
{
double zeros;
double sign;
if( CompareDoubles( anum, 0.0 )==-1 )
sign = -1.0;
else
sign = 1.0;
#ifdef __CYGWIN__
zeros = pow( 10.0, an );
#else
zeros = powl( 10.0, an );
#endif
anum = anum * zeros * sign;
#ifdef __CYGWIN__
double floorval = floor(anum);
#else
double floorval = floorl(anum);
#endif
if( CompareDoubles( anum-floorval, 0.5 )==-1 )
return sign*floorval/zeros;
else
return sign*(floorval+1.0)/zeros;
}
double RoundDown( double anum, int an )
{
double zeros;
double sign;
if( CompareDoubles( anum, 0.0 )==-1 )
sign = -1.0;
else
sign = 1.0;
#ifdef __CYGWIN__
zeros = pow( 10.0, an );
#else
zeros = powl( 10.0, an );
#endif
anum = anum * zeros * sign;
#ifdef __CYGWIN__
double floorval = floor(anum);
#else
double floorval = floorl(anum);
#endif
if( CompareDoubles((anum-floorval), 1.0)==-1 )
return sign*floorval/zeros;
else
return sign*(floorval+1.0)/zeros;
}
double RoundUp( double anum, int an )
{
double zeros;
double sign;
if( CompareDoubles( anum, 0.0 )==-1 )
sign = -1.0;
else
sign = 1.0;
#ifdef __CYGWIN__
zeros = pow( 10.0, an );
#else
zeros = powl( 10.0, an );
#endif
anum = anum * zeros * sign;
#ifdef __CYGWIN__
double ceilval = ceil(anum);
#else
double ceilval = ceill(anum);
#endif
if( CompareDoubles((ceilval-anum), 1.0)==1 )
return sign*(ceilval-1.0)/zeros;
else
return sign*ceilval/zeros;
}
int Random( int num )
{
return num ? (int)(rand()%(num)) : 0;
}
int CompareDoubles( double a, double b )
{
int exp1, exp2;
double man1, man2;
man1 = frexp( a, &exp1 );
man2 = frexp( b, &exp2 );
if( (exp1==exp2) && (fabs( man1-man2 )<DBL_EPSILON) )
return 0;
if( a<b )
return -1;
return 1;
}
#ifndef __CYGWIN__
int CompareDoubles( long double a, long double b )
{
int exp1, exp2;
long double man1, man2;
man1 = frexpl( a, &exp1 );
man2 = frexpl( b, &exp2 );
if( (exp1==exp2) && (fabsl( man1-man2 )<LDBL_EPSILON) )
return 0;
if( a<b )
return -1;
return 1;
}
#else
int CompareDoubles( long double a, long double b )
{
return CompareDoubles( (double)a, (double)b );
}
#endif
bool IsEqual( double a, double b )
{
return CompareDoubles( a, b )==0;
}
bool IsZero( double a )
{
return CompareDoubles( a, 0.0 )==0;
}
//---------------------------------------------------------------------------
// TMatrix implementation
TMatrix::TMatrix( int acols, int arows )
{
data = 0;
rows = arows;
cols = acols;
AllocateData();
};
TMatrix::TMatrix( const TMatrix& amatrix )
{
data = 0;
rows = amatrix.rows;
cols = amatrix.cols;
AllocateData();
CopyCellsFromMatrix( amatrix );
}
TMatrix::~TMatrix()
{
if( data )
delete[] data;
}
TMatrix& TMatrix::operator=( const TMatrix& amatrix )
{
if( cols==amatrix.cols && rows==amatrix.rows )
{
memcpy( data, amatrix.data, rows*cols*sizeof(double) );
}
else
{
cols = amatrix.cols;
rows = amatrix.rows;
AllocateData();
CopyCellsFromMatrix( amatrix );
}
return *this;
}
TMatrix operator+( const TMatrix& m1, const TMatrix& m2 )
{
if( m1.rows!=m2.rows || m1.cols!=m2.cols )
throw TException( "Matrices' dimensions aren't equal." );
TMatrix ret(m1);
for( int ic=1; ic<=m1.cols; ic++ )
for( int ir=1; ir<=m1.rows; ir++ )
ret.Cell(ic,ir) += m2.const_Cell(ic,ir);
return ret;
}
TMatrix operator-( const TMatrix& m1, const TMatrix& m2 )
{
if( m1.rows!=m2.rows || m1.cols!=m2.cols )
throw TException( "Matrices' dimensions aren't equal." );
TMatrix ret(m1);
for( int ic=1; ic<=m1.cols; ic++ )
for( int ir=1; ir<=m1.rows; ir++ )
ret.Cell(ic,ir) -= m2.const_Cell(ic,ir);
return ret;
}
TMatrix operator*( const TMatrix& m1, const TMatrix& m2 )
{
if( m1.cols!=m2.rows )
throw TException( "Cannot multiply matrices, number of columns of the first matrix doesn't equal the number of rows of the second one." );
int cr = m1.cols;
TMatrix ret(m2.cols,m1.rows);
for( int ic=1; ic<=ret.cols; ic++ ) {
for( int ir=1; ir<=ret.rows; ir++ ) {
for( int i=1; i<=cr; i++ ) {
ret.Cell(ic,ir) += m1.const_Cell(i,ir)*m2.const_Cell(ic,i);
}
}
}
return ret;
}
void TMatrix::AllocateData()
{
if( rows<=0 )
throw TException( "Number of rows of a matrix has to be positive." );
if( cols<=0 )
throw TException( "Number of rows of a matrix has to be positive." );
if( data )
delete[] data;
data = new double[ rows*cols ];
memset( data, 0, rows*cols*sizeof(double) );
}
double& TMatrix::Cell( int acol, int arow )
{
if( !data )
throw TException( "Attempt to access uninitialized matrix." );
if( arow<1 || arow>rows )
throw TException( "Attempt to access row that doesn't exist." );
if( acol<1 || acol>cols )
throw TException( "Attempt to access column that doesn't exist." );
return (data[(arow-1)*cols+(acol-1)]);
}
const double& TMatrix::const_Cell( int acol, int arow ) const
{
if( !data )
throw TException( "Attempt to access uninitialized matrix." );
if( arow<1 || arow>rows )
throw TException( "Attempt to access row that doesn't exist." );
if( acol<1 || acol>cols )
throw TException( "Attempt to access column that doesn't exist." );
return (data[(arow-1)*cols+(acol-1)]);
}
void TMatrix::SetNCols( int acols )
{
if( acols == cols )
return;
TMatrix newmatrix( acols, rows );
newmatrix.CopyCellsFromMatrix( *this );
*this = newmatrix;
}
void TMatrix::SetNRows( int arows )
{
if( arows == rows )
return;
double *newdata = new double[ cols*arows ];
if( data ) {
if( arows<rows ) {
memcpy( newdata, data, cols*arows*sizeof(double) );
}
else {
memcpy( newdata, data, cols*rows*sizeof(double) );
memset( &(newdata[cols*arows]), 0, cols*(arows-rows) );
}
delete[] data;
}
else {
memset( newdata, 0, cols*arows*sizeof(double) );
}
data = newdata;
rows = arows;
}
void TMatrix::CopyCellsFromMatrix( const TMatrix& amatrix )
{
if( !data )
throw TException( "Destination matrix has no elements." );
if( amatrix.data == NULL )
return;
if( rows == amatrix.rows && cols == amatrix.cols )
memcpy( data, amatrix.data, rows*cols*sizeof(double) );
else
{
int nr = rows < amatrix.rows ? rows : amatrix.rows;
int nc = cols < amatrix.cols ? cols : amatrix.cols;
if( nr<1 || nc<1 )
return;
for( int r=1; r<=nr; r++ ) {
for( int c=1; c<=nc; c++ ) {
Cell( c,r ) = amatrix.const_Cell( c,r );
}
}
}
}
TMatrix TMatrix::Transpose()
{
TMatrix ret( rows, cols );
for( int r=1; r<=rows; r++ ) {
for( int c=1; c<=cols; c++ ) {
ret.Cell( r, c ) = Cell( c, r );
}
}
return ret;
}
void TMatrix::Clear()
{
if( data )
memset( data, 0, rows*cols*sizeof(double) );
}
TMatrix TMatrix::Inverse()
{
if( rows!=cols )
throw TException( "Inverse matrix can be returned only for matrices that has the same number of rows and columns." );
if( !data )
throw TException( "Matrix not defined, cannot return an inverse matrix." );
int i;
TMatrix m( cols*2, rows );
m.CopyCellsFromMatrix( *this );
for( i=1; i<=m.rows; i++ )
m.Cell( i+m.rows, i ) = 1;
try
{
m.MakeMatrixTriangular();
m.MakeTriangularMatrixDiagonal();
}
catch( TException& ex )
{
throw TException( "Matrix cannot be made inverse because it's singular. (" +
ex.Message + ")" );
}
return m.SubMatrix( rows+1, 1, 2*rows, rows );
}
TMatrix TMatrix::SubMatrix( int leftcol, int toprow, int rightcol, int bottomrow )
{
if( leftcol>rightcol )
throw TException( "Cannot create submatrix, left column's index is greater than right one's." );
if( toprow>bottomrow )
throw TException( "Cannot create submatrix, left column's index is greater than right one's." );
if( leftcol<1 || rightcol>cols || toprow<1 || bottomrow>rows )
throw TException( "Cannot create submatrix, dimensions don't fit in the original matrix." );
TMatrix m( rightcol-leftcol+1, bottomrow-toprow+1 );
for( int ic=1; ic<=m.cols; ic++ ) {
for( int ir=1; ir<=m.rows; ir++ ) {
m.Cell(ic,ir) = Cell( leftcol+ic-1, toprow+ir-1 );
}
}
return m;
}
void TMatrix::MakeMatrixTriangular()
{
for( int ir=1; ir<=rows; ir++ ) {
if( IsZero(Cell(ir,ir)) ) {
if( ir==rows )
throw TException( "Matrix cannot be made triangular because it's singular." );
int nonzero = FindNonZeroCellBelow(ir+1,ir);
if( nonzero==-1 )
throw TException( "Matrix cannot be made triangular because it's singular." );
SwapRows( ir, nonzero );
}
MakeCellsBellowDiagonalZero( ir );
}
}
void TMatrix::MakeTriangularMatrixDiagonal()
{
for( int ir=rows; ir>=1; ir-- ) {
if( IsZero(Cell(ir,ir)) )
throw TException( "Triangular matrix expected." );
MultiplyRow( ir, (long double)1.0/(long double)Cell(ir,ir) );
MakeCellsAboveDiagonalZero( ir );
}
}
int TMatrix::FindNonZeroCellBelow( int acol, int arow )
{
if( acol<1 || arow<1 || acol>cols || arow>rows )
throw TException( "Cell coordinates out of bounds." );
for( int ir=arow; ir<=rows; ir++ ) {
if( !IsZero( Cell(acol,ir) ) ) {
return ir;
}
}
return -1;
}
void TMatrix::SwapRows( int arow1, int arow2 )
{
if( arow1==arow2 )
return;
if( !data )
throw TException( "Cannot swap rows, matrix not initialized." );
if( arow1<1 || arow1>rows || arow2<1 || arow2>rows )
throw TException( "Cannot swap rows, index out of bounds." );
double *buffer = new double[cols];
memcpy( buffer, &data[ (arow1-1)*cols ], cols*sizeof(double) );
memcpy( &data[ (arow1-1)*cols ], &data[ (arow2-1)*cols ], cols*sizeof(double) );
memcpy( &data[ (arow2-1)*cols ], buffer, cols*sizeof(double) );
delete[] buffer;
}
void TMatrix::MakeCellsBellowDiagonalZero( int arow )
{
if( arow<1 || arow>rows )
throw TException( "Row's index out of bounds." );
double pivot = Cell(arow,arow);
if( pivot==0 )
throw TException( "Zero found on a diagonal, cells bellow cannot be made zero." );
for( int ir=arow+1; ir<=rows; ir++ )
AddRowToRow( arow, ir, (long double)-Cell(arow,ir)/pivot );
}
void TMatrix::MakeCellsAboveDiagonalZero( int arow )
{
if( arow<1 || arow>rows )
throw TException( "Row's index out of bounds." );
double pivot = Cell(arow,arow);
if( pivot==0 )
throw TException( "Zero found on a diagonal, cells bellow cannot be made zero." );
for( int ir=arow-1; ir>=1; ir-- )
AddRowToRow( arow, ir, (long double)-Cell(arow,ir)/pivot );
}
void TMatrix::AddRowToRow( int afrom, int ato, long double amultiple )
{
for( int ic=1; ic<=cols; ic++ )
Cell( ic, ato ) += (((long double)amultiple)*(long double)Cell(ic,afrom));
}
void TMatrix::MultiplyRow( int arow, long double amultiple )
{
for( int ic=1; ic<=cols; ic++ )
Cell( ic, arow ) = amultiple * (long double) Cell( ic, arow );
}
// ----------------------------------------------------------------------------
TRegression::TRegression( int n, int i )
{
Y = TMatrix( 1, n );
X = TMatrix( i, n );
}
TRegression::TRegression( const TMatrix& matrix )
{
SetYXMatrix( matrix );
}
void TRegression::SetYXMatrix( const TMatrix& matrix )
{
if( matrix.Cols()<2 )
{
throw TException( "Cannot create regression, matrix has to have at least two columns." );
}
needrecalc = true;
int nr = matrix.Rows();
int nc = matrix.Cols();
Y = TMatrix( 1, nr );
X = TMatrix( nc-1, nr );
for( int ir=1; ir<=nr; ir++ ) {
SetY( ir, matrix.const_Cell( 1, ir ) );
for( int ic=1; ic<nc; ic++ ) {
SetX( ir, ic, matrix.const_Cell( ic+1, ir ) );
}
}
}
void TRegression::SetN( int n )
{
needrecalc = true;
Y.SetNRows( n );
X.SetNRows( n );
}
int TRegression::N() const
{
return X.Rows();
}
int TRegression::I() const
{
return X.Cols();
}
void TRegression::SetX( int n, int i, double value )
{
needrecalc = true;
X.Cell( i, n ) = value;
}
void TRegression::SetY( int n, double value )
{
needrecalc = true;
Y.Cell( 1, n ) = value;
}
double TRegression::GetX( int n, int i ) const
{
return X.const_Cell( i, n );
}
double TRegression::GetY( int n ) const
{
return Y.const_Cell( 1, n );
}
void TRegression::Recalc()
{
RecalcRealX();
RecalcB();
needrecalc = false;
}
TMatrix TRegression::GetRealX()
{
if( needrecalc )
Recalc();
return _RealX;
}
TMatrix TRegression::GetB()
{
if( needrecalc )
RecalcB();
return B;
}
void TRegression::RecalcRealX()
{
int ir, ic;
int nr = N();
int nc = I()+1;
_RealX = TMatrix( nc, nr );
for( ir=1; ir<=nr; ir++ )
_RealX.Cell(1,ir) = 1.0;
for( ir=1; ir<=nr; ir++ )
for( ic=2; ic<=nc; ic++ )
_RealX.Cell(ic,ir) = X.Cell(ic-1,ir);
}
void TRegression::RecalcB()
{
TMatrix Xt = _RealX.Transpose();
B = ((Xt*_RealX).Inverse())*Xt*Y;
}
double TRegression::Regress( double X )
{
if( needrecalc )
Recalc();
return B.Cell(1,1) + B.Cell(1,2)*X;
}
// ----------------------------------------------------------------------------
// --------------------------------------------------------------------------
double ExponentialInterpolation( double alow, double alowvalue,
double ahigh, double ahighvalue, double avaluedate, double abase )
{
double r = ahigh-alow;
double rb = avaluedate-abase;
return pow(alowvalue,((ahigh-avaluedate)*rb)/(r*(alow-abase))) *
pow(ahighvalue,((avaluedate-alow)*rb)/(r*(ahigh-abase)));
}
double ExponentialInterpolation( const TDate& alow, double alowvalue,
const TDate &ahigh, double ahighvalue, const TDate &avaluedate,
const TDate &abase )
{
return ExponentialInterpolation( (double)alow.Serial(), alowvalue,
(double)ahigh.Serial(), ahighvalue, avaluedate,
(double)abase.Serial() );
}
double ExponentialInterpolation( const TDate& alow, double alowvalue,
const TDate &ahigh, double ahighvalue, const TDate &avaluedate )
{
return ExponentialInterpolation( (double)alow.Serial(), alowvalue,
(double)ahigh.Serial(), ahighvalue, avaluedate, 0.0 );
}
// --------------------------------------------------------------------------
} // end of namespace utils