-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathNEWMAT.CPP
More file actions
8996 lines (7718 loc) · 240 KB
/
NEWMAT.CPP
File metadata and controls
8996 lines (7718 loc) · 240 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
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//$$ newmat1.cpp Matrix type functions
// Copyright (C) 1991,2,3,4: R B Davies
//#define WANT_STREAM
#include "newmat.h"
#ifdef use_namespace
namespace NEWMAT {
#endif
#ifdef DO_REPORT
#define REPORT { static ExeCounter ExeCount(__LINE__,1); ++ExeCount; }
#else
#define REPORT {}
#endif
#include <stdio.h>
#include <iostream>
//$$ newmat8.cpp Advanced LU transform, scalar functions
// Copyright (C) 1991,2,3,4,8: R B Davies
#define WANT_MATH
//#include "include.h"
#include <string.h>
#include "newmat.h"
//$$ newmatrc.h definition file for row/column classes
// Copyright (C) 1991,2,3,4,7: R B Davies
#ifndef NEWMATRC_LIB
#define NEWMATRC_LIB 0
#ifdef use_namespace
namespace NEWMAT {
#endif
#include "controlw.h"
/************** classes MatrixRowCol, MatrixRow, MatrixCol *****************/
// Used for accessing the rows and columns of matrices
// All matrix classes must provide routines for calculating matrix rows and
// columns. Assume rows can be found very efficiently.
enum LSF { LoadOnEntry=1,StoreOnExit=2,DirectPart=4,StoreHere=8,HaveStore=16 };
class LoadAndStoreFlag : public ControlWord
{
public:
LoadAndStoreFlag() {}
LoadAndStoreFlag(int i) : ControlWord(i) {}
LoadAndStoreFlag(LSF lsf) : ControlWord(lsf) {}
LoadAndStoreFlag(const ControlWord& cwx) : ControlWord(cwx) {}
};
class MatrixRowCol
// the row or column of a matrix
{
public: // these are public to avoid
// numerous friend statements
int length; // row or column length
int skip; // initial number of zeros
int storage; // number of stored elements
int rowcol; // row or column number
GeneralMatrix* gm; // pointer to parent matrix
Real* data; // pointer to local storage
LoadAndStoreFlag cw; // Load? Store? Is a Copy?
void IncrMat() { rowcol++; data += storage; } // used by NextRow
void IncrDiag() { rowcol++; skip++; data++; }
void IncrId() { rowcol++; skip++; }
void IncrUT() { rowcol++; data += storage; storage--; skip++; }
void IncrLT() { rowcol++; data += storage; storage++; }
public:
void Zero(); // set elements to zero
void Add(const MatrixRowCol&); // add a row/col
void AddScaled(const MatrixRowCol&, Real); // add a multiple of a row/col
void Add(const MatrixRowCol&, const MatrixRowCol&);
// add two rows/cols
void Add(const MatrixRowCol&, Real); // add a row/col
void NegAdd(const MatrixRowCol&, Real); // Real - a row/col
void Sub(const MatrixRowCol&); // subtract a row/col
void Sub(const MatrixRowCol&, const MatrixRowCol&);
// sub a row/col from another
void RevSub(const MatrixRowCol&); // subtract from a row/col
void ConCat(const MatrixRowCol&, const MatrixRowCol&);
// concatenate two row/cols
void Multiply(const MatrixRowCol&); // multiply a row/col
void Multiply(const MatrixRowCol&, const MatrixRowCol&);
// multiply two row/cols
void KP(const MatrixRowCol&, const MatrixRowCol&);
// Kronecker Product two row/cols
void Copy(const MatrixRowCol&); // copy a row/col
void CopyCheck(const MatrixRowCol&); // ... check for data loss
void Check(const MatrixRowCol&); // just check for data loss
void Check(); // check full row/col present
void Copy(const double*&); // copy from an array
void Copy(const float*&); // copy from an array
void Copy(const int*&); // copy from an array
void Copy(Real); // copy from constant
void Add(Real); // add a constant
void Multiply(Real); // multiply by constant
Real SumAbsoluteValue(); // sum of absolute values
Real MaximumAbsoluteValue1(Real r, int& i); // maximum of absolute values
Real MinimumAbsoluteValue1(Real r, int& i); // minimum of absolute values
Real Maximum1(Real r, int& i); // maximum
Real Minimum1(Real r, int& i); // minimum
Real Sum(); // sum of values
void Inject(const MatrixRowCol&); // copy stored els of a row/col
void Negate(const MatrixRowCol&); // change sign of a row/col
void Multiply(const MatrixRowCol&, Real); // scale a row/col
friend Real DotProd(const MatrixRowCol&, const MatrixRowCol&);
// sum of pairwise product
Real* Data() { return data; }
int Skip() { return skip; } // number of elements skipped
int Storage() { return storage; } // number of elements stored
int Length() { return length; } // length of row or column
void Skip(int i) { skip=i; }
void Storage(int i) { storage=i; }
void Length(int i) { length=i; }
void SubRowCol(MatrixRowCol&, int, int) const;
// get part of a row or column
MatrixRowCol() {} // to stop warning messages
~MatrixRowCol();
FREE_CHECK(MatrixRowCol)
};
class MatrixRow : public MatrixRowCol
{
public:
// bodies for these are inline at the end of this .h file
MatrixRow(GeneralMatrix*, LoadAndStoreFlag, int=0);
// extract a row
~MatrixRow();
void Next(); // get next row
FREE_CHECK(MatrixRow)
};
class MatrixCol : public MatrixRowCol
{
public:
// bodies for these are inline at the end of this .h file
MatrixCol(GeneralMatrix*, LoadAndStoreFlag, int=0);
// extract a col
MatrixCol(GeneralMatrix*, Real*, LoadAndStoreFlag, int=0);
// store/retrieve a col
~MatrixCol();
void Next(); // get next row
FREE_CHECK(MatrixCol)
};
// MatrixColX is an alternative to MatrixCol where the complete
// column is stored externally
class MatrixColX : public MatrixRowCol
{
public:
// bodies for these are inline at the end of this .h file
MatrixColX(GeneralMatrix*, Real*, LoadAndStoreFlag, int=0);
// store/retrieve a col
~MatrixColX();
void Next(); // get next row
Real* store; // pointer to local storage
// less skip
FREE_CHECK(MatrixColX)
};
/**************************** inline bodies ****************************/
inline MatrixRow::MatrixRow(GeneralMatrix* gmx, LoadAndStoreFlag cwx, int row)
{ gm=gmx; cw=cwx; rowcol=row; gm->GetRow(*this); }
inline void MatrixRow::Next() { gm->NextRow(*this); }
inline MatrixCol::MatrixCol(GeneralMatrix* gmx, LoadAndStoreFlag cwx, int col)
{ gm=gmx; cw=cwx; rowcol=col; gm->GetCol(*this); }
inline MatrixCol::MatrixCol(GeneralMatrix* gmx, Real* r,
LoadAndStoreFlag cwx, int col)
{ gm=gmx; data=r; cw=cwx+StoreHere; rowcol=col; gm->GetCol(*this); }
inline MatrixColX::MatrixColX(GeneralMatrix* gmx, Real* r,
LoadAndStoreFlag cwx, int col)
{ gm=gmx; store=data=r; cw=cwx+StoreHere; rowcol=col; gm->GetCol(*this); }
inline void MatrixCol::Next() { gm->NextCol(*this); }
inline void MatrixColX::Next() { gm->NextCol(*this); }
#ifdef use_namespace
}
#endif
#endif
//$$ precisio.h floating point constants
#ifndef PRECISION_LIB
#define PRECISION_LIB 0
#define WANT_MATH
//#include "include.h" // in case being used as stand alone
#ifdef _STANDARD_ // standard library available
#include <limits>
#endif
#ifdef use_namespace
namespace NEWMAT {
#endif
#ifdef _STANDARD_ // standard library available
#ifdef OPT_COMPATIBLE
#include <cfloat> // for FLT_MAX
#endif
using namespace std;
class FloatingPointPrecision
{
public:
static int Dig() // number of decimal digits or precision
{ return numeric_limits<Real>::digits10 ; }
static Real Epsilon() // smallest number such that 1+Eps!=Eps
{ return numeric_limits<Real>::epsilon(); }
static int Mantissa() // bits in mantisa
{ return numeric_limits<Real>::digits; }
static Real Maximum() // maximum value
{ return numeric_limits<Real>::max(); }
static int MaximumDecimalExponent() // maximum decimal exponent
{ return numeric_limits<Real>::max_exponent10; }
static int MaximumExponent() // maximum binary exponent
{ return numeric_limits<Real>::max_exponent; }
static Real LnMaximum() // natural log of maximum
{ return (Real)log(Maximum()); }
static Real Minimum() // minimum positive value
{ return numeric_limits<Real>::min(); }
static int MinimumDecimalExponent() // minimum decimal exponent
{ return numeric_limits<Real>::min_exponent10; }
static int MinimumExponent() // minimum binary exponent
{ return numeric_limits<Real>::min_exponent; }
static Real LnMinimum() // natural log of minimum
{ return (Real)log(Minimum()); }
static int Radix() // exponent radix
{ return numeric_limits<Real>::radix; }
static int Rounds() // addition rounding (1 = does round)
{
return numeric_limits<Real>::round_style ==
round_to_nearest ? 1 : 0;
}
};
#else // _STANDARD_ not defined
#ifndef SystemV // if there is float.h
#ifdef USING_FLOAT
class FloatingPointPrecision
{
public:
static int Dig()
{ return FLT_DIG; } // number of decimal digits or precision
static Real Epsilon()
{ return FLT_EPSILON; } // smallest number such that 1+Eps!=Eps
static int Mantissa()
{ return FLT_MANT_DIG; } // bits in mantisa
static Real Maximum()
{ return FLT_MAX; } // maximum value
static int MaximumDecimalExponent()
{ return FLT_MAX_10_EXP; } // maximum decimal exponent
static int MaximumExponent()
{ return FLT_MAX_EXP; } // maximum binary exponent
static Real LnMaximum()
{ return (Real)log(Maximum()); } // natural log of maximum
static Real Minimum()
{ return FLT_MIN; } // minimum positive value
static int MinimumDecimalExponent()
{ return FLT_MIN_10_EXP; } // minimum decimal exponent
static int MinimumExponent()
{ return FLT_MIN_EXP; } // minimum binary exponent
static Real LnMinimum()
{ return (Real)log(Minimum()); } // natural log of minimum
static int Radix()
{ return FLT_RADIX; } // exponent radix
static int Rounds()
{ return FLT_ROUNDS; } // addition rounding (1 = does round)
};
#endif // USING_FLOAT
#ifdef USING_DOUBLE
class FloatingPointPrecision
{
public:
static int Dig()
{ return DBL_DIG; } // number of decimal digits or precision
static Real Epsilon()
{ return DBL_EPSILON; } // smallest number such that 1+Eps!=Eps
static int Mantissa()
{ return DBL_MANT_DIG; } // bits in mantisa
static Real Maximum()
{ return DBL_MAX; } // maximum value
static int MaximumDecimalExponent()
{ return DBL_MAX_10_EXP; } // maximum decimal exponent
static int MaximumExponent()
{ return DBL_MAX_EXP; } // maximum binary exponent
static Real LnMaximum()
{ return (Real)log(Maximum()); } // natural log of maximum
static Real Minimum()
{
//#ifdef __BCPLUSPLUS__
// return 2.225074e-308; // minimum positive value
//#else
return DBL_MIN;
//#endif
}
static int MinimumDecimalExponent()
{ return DBL_MIN_10_EXP; } // minimum decimal exponent
static int MinimumExponent()
{ return DBL_MIN_EXP; } // minimum binary exponent
static Real LnMinimum()
{ return (Real)log(Minimum()); } // natural log of minimum
static int Radix()
{ return FLT_RADIX; } // exponent radix
static int Rounds()
{ return FLT_ROUNDS; } // addition rounding (1 = does round)
};
#endif // USING_DOUBLE
#else // if there is no float.h
#ifdef OPT_COMPATIBLE
#define FLT_MAX MAXFLOAT
#endif
#ifdef USING_FLOAT
class FloatingPointPrecision
{
public:
static Real Epsilon()
{ return pow(2.0,(int)(1-FSIGNIF)); }
// smallest number such that 1+Eps!=Eps
static Real Maximum()
{ return MAXFLOAT; } // maximum value
static Real LnMaximum()
{ return (Real)log(Maximum()); } // natural log of maximum
static Real Minimum()
{ return MINFLOAT; } // minimum positive value
static Real LnMinimum()
{ return (Real)log(Minimum()); } // natural log of minimum
};
#endif // USING_FLOAT
#ifdef USING_DOUBLE
class FloatingPointPrecision
{
public:
static Real Epsilon()
{ return pow(2.0,(int)(1-DSIGNIF)); }
// smallest number such that 1+Eps!=Eps
static Real Maximum()
{ return MAXDOUBLE; } // maximum value
static Real LnMaximum()
{ return LN_MAXDOUBLE; } // natural log of maximum
static Real Minimum()
{ return MINDOUBLE; }
static Real LnMinimum()
{ return LN_MINDOUBLE; } // natural log of minimum
};
#endif // USING_DOUBLE
#endif // SystemV
#endif // _STANDARD_
#ifdef use_namespace
}
#endif // use_namespace
#endif // PRECISION_LIB
static int tristore(int n) // elements in triangular matrix
{ return (n*(n+1))/2; }
#ifdef use_namespace
namespace NEWMAT {
#endif
#ifdef DO_REPORT
#define REPORT { static ExeCounter ExeCount(__LINE__,8); ++ExeCount; }
#else
#define REPORT {}
#endif
// ************************* general utilities *************************/
/************************** LU transformation ****************************/
void CroutMatrix::ludcmp()
// LU decomposition from Golub & Van Loan, algorithm 3.4.1, (the "outer
// product" version).
// This replaces the code derived from Numerical Recipes in C in previous
// versions of newmat and being row oriented runs much faster with large
// matrices.
{
REPORT
Tracer tr( "Crout(ludcmp)" ); sing = false;
Real* akk = store; // runs down diagonal
Real big = fabs(*akk); int mu = 0; Real* ai = akk; int k;
for (k = 1; k < nrows_val; k++)
{
ai += nrows_val; const Real trybig = fabs(*ai);
if (big < trybig) { big = trybig; mu = k; }
}
if (nrows_val) for (k = 0;;)
{
/*
int mu1;
{
Real big = fabs(*akk); mu1 = k; Real* ai = akk; int i;
for (i = k+1; i < nrows_val; i++)
{
ai += nrows_val; const Real trybig = fabs(*ai);
if (big < trybig) { big = trybig; mu1 = i; }
}
}
if (mu1 != mu) cout << k << " " << mu << " " << mu1 << endl;
*/
indx[k] = mu;
if (mu != k) //row swap
{
Real* a1 = store + nrows_val * k;
Real* a2 = store + nrows_val * mu; d = !d;
int j = nrows_val;
while (j--) { const Real temp = *a1; *a1++ = *a2; *a2++ = temp; }
}
Real diag = *akk; big = 0; mu = k + 1;
if (diag != 0)
{
ai = akk; int i = nrows_val - k - 1;
while (i--)
{
ai += nrows_val; Real* al = ai;
Real mult = *al / diag; *al = mult;
int l = nrows_val - k - 1; Real* aj = akk;
// work out the next pivot as part of this loop
// this saves a column operation
if (l-- != 0)
{
*(++al) -= (mult * *(++aj));
const Real trybig = fabs(*al);
if (big < trybig) { big = trybig; mu = nrows_val - i - 1; }
while (l--) *(++al) -= (mult * *(++aj));
}
}
}
else sing = true;
if (++k == nrows_val) break; // so next line won't overflow
akk += nrows_val + 1;
}
}
void CroutMatrix::lubksb(Real* B, int mini)
{
REPORT
// this has been adapted from Numerical Recipes in C. The code has been
// substantially streamlined, so I do not think much of the original
// copyright remains. However there is not much opportunity for
// variation in the code, so it is still similar to the NR code.
// I follow the NR code in skipping over initial zeros in the B vector.
Tracer tr("Crout(lubksb)");
if (sing) Throw(SingularException(*this));
int i, j, ii = nrows_val; // ii initialised : B might be all zeros
// scan for first non-zero in B
for (i = 0; i < nrows_val; i++)
{
int ip = indx[i]; Real temp = B[ip]; B[ip] = B[i]; B[i] = temp;
if (temp != 0.0) { ii = i; break; }
}
Real* bi; Real* ai;
i = ii + 1;
if (i < nrows_val)
{
bi = B + ii; ai = store + ii + i * nrows_val;
for (;;)
{
int ip = indx[i]; Real sum = B[ip]; B[ip] = B[i];
Real* aij = ai; Real* bj = bi; j = i - ii;
while (j--) sum -= *aij++ * *bj++;
B[i] = sum;
if (++i == nrows_val) break;
ai += nrows_val;
}
}
ai = store + nrows_val * nrows_val;
for (i = nrows_val - 1; i >= mini; i--)
{
Real* bj = B+i; ai -= nrows_val; Real* ajx = ai+i;
Real sum = *bj; Real diag = *ajx;
j = nrows_val - i; while(--j) sum -= *(++ajx) * *(++bj);
B[i] = sum / diag;
}
}
/****************************** scalar functions ****************************/
inline Real square(Real x) { return x*x; }
Real GeneralMatrix::sum_square() const
{
REPORT
Real sum = 0.0; int i = storage; Real* s = store;
while (i--) sum += square(*s++);
((GeneralMatrix&)*this).tDelete(); return sum;
}
Real GeneralMatrix::sum_absolute_value() const
{
REPORT
Real sum = 0.0; int i = storage; Real* s = store;
while (i--) sum += fabs(*s++);
((GeneralMatrix&)*this).tDelete(); return sum;
}
Real GeneralMatrix::sum() const
{
REPORT
Real sm = 0.0; int i = storage; Real* s = store;
while (i--) sm += *s++;
((GeneralMatrix&)*this).tDelete(); return sm;
}
// maxima and minima
// There are three sets of routines
// maximum_absolute_value, minimum_absolute_value, maximum, minimum
// ... these find just the maxima and minima
// maximum_absolute_value1, minimum_absolute_value1, maximum1, minimum1
// ... these find the maxima and minima and their locations in a
// one dimensional object
// maximum_absolute_value2, minimum_absolute_value2, maximum2, minimum2
// ... these find the maxima and minima and their locations in a
// two dimensional object
// If the matrix has no values throw an exception
// If we do not want the location find the maximum or minimum on the
// array stored by GeneralMatrix
// This won't work for BandMatrices. We call ClearCorner for
// maximum_absolute_value but for the others use the absolute_minimum_value2
// version and discard the location.
// For one dimensional objects, when we want the location of the
// maximum or minimum, work with the array stored by GeneralMatrix
// For two dimensional objects where we want the location of the maximum or
// minimum proceed as follows:
// For rectangular matrices use the array stored by GeneralMatrix and
// deduce the location from the location in the GeneralMatrix
// For other two dimensional matrices use the Matrix Row routine to find the
// maximum or minimum for each row.
static void NullMatrixError(const GeneralMatrix* gm)
{
((GeneralMatrix&)*gm).tDelete();
Throw(ProgramException("Maximum or minimum of null matrix"));
}
Real GeneralMatrix::maximum_absolute_value() const
{
REPORT
if (storage == 0) NullMatrixError(this);
Real maxval = 0.0; int l = storage; Real* s = store;
while (l--) { Real a = fabs(*s++); if (maxval < a) maxval = a; }
((GeneralMatrix&)*this).tDelete(); return maxval;
}
Real GeneralMatrix::maximum_absolute_value1(int& i) const
{
REPORT
if (storage == 0) NullMatrixError(this);
Real maxval = 0.0; int l = storage; Real* s = store; int li = storage;
while (l--)
{ Real a = fabs(*s++); if (maxval <= a) { maxval = a; li = l; } }
i = storage - li;
((GeneralMatrix&)*this).tDelete(); return maxval;
}
Real GeneralMatrix::minimum_absolute_value() const
{
REPORT
if (storage == 0) NullMatrixError(this);
int l = storage - 1; Real* s = store; Real minval = fabs(*s++);
while (l--) { Real a = fabs(*s++); if (minval > a) minval = a; }
((GeneralMatrix&)*this).tDelete(); return minval;
}
Real GeneralMatrix::minimum_absolute_value1(int& i) const
{
REPORT
if (storage == 0) NullMatrixError(this);
int l = storage - 1; Real* s = store; Real minval = fabs(*s++); int li = l;
while (l--)
{ Real a = fabs(*s++); if (minval >= a) { minval = a; li = l; } }
i = storage - li;
((GeneralMatrix&)*this).tDelete(); return minval;
}
Real GeneralMatrix::maximum() const
{
REPORT
if (storage == 0) NullMatrixError(this);
int l = storage - 1; Real* s = store; Real maxval = *s++;
while (l--) { Real a = *s++; if (maxval < a) maxval = a; }
((GeneralMatrix&)*this).tDelete(); return maxval;
}
Real GeneralMatrix::maximum1(int& i) const
{
REPORT
if (storage == 0) NullMatrixError(this);
int l = storage - 1; Real* s = store; Real maxval = *s++; int li = l;
while (l--) { Real a = *s++; if (maxval <= a) { maxval = a; li = l; } }
i = storage - li;
((GeneralMatrix&)*this).tDelete(); return maxval;
}
Real GeneralMatrix::minimum() const
{
REPORT
if (storage == 0) NullMatrixError(this);
int l = storage - 1; Real* s = store; Real minval = *s++;
while (l--) { Real a = *s++; if (minval > a) minval = a; }
((GeneralMatrix&)*this).tDelete(); return minval;
}
Real GeneralMatrix::minimum1(int& i) const
{
REPORT
if (storage == 0) NullMatrixError(this);
int l = storage - 1; Real* s = store; Real minval = *s++; int li = l;
while (l--) { Real a = *s++; if (minval >= a) { minval = a; li = l; } }
i = storage - li;
((GeneralMatrix&)*this).tDelete(); return minval;
}
Real GeneralMatrix::maximum_absolute_value2(int& i, int& j) const
{
REPORT
if (storage == 0) NullMatrixError(this);
Real maxval = 0.0; int nr = Nrows();
MatrixRow mr((GeneralMatrix*)this, LoadOnEntry+DirectPart);
for (int r = 1; r <= nr; r++)
{
int c; maxval = mr.MaximumAbsoluteValue1(maxval, c);
if (c > 0) { i = r; j = c; }
mr.Next();
}
((GeneralMatrix&)*this).tDelete(); return maxval;
}
Real GeneralMatrix::minimum_absolute_value2(int& i, int& j) const
{
REPORT
if (storage == 0) NullMatrixError(this);
Real minval = FloatingPointPrecision::Maximum(); int nr = Nrows();
MatrixRow mr((GeneralMatrix*)this, LoadOnEntry+DirectPart);
for (int r = 1; r <= nr; r++)
{
int c; minval = mr.MinimumAbsoluteValue1(minval, c);
if (c > 0) { i = r; j = c; }
mr.Next();
}
((GeneralMatrix&)*this).tDelete(); return minval;
}
Real GeneralMatrix::maximum2(int& i, int& j) const
{
REPORT
if (storage == 0) NullMatrixError(this);
Real maxval = -FloatingPointPrecision::Maximum(); int nr = Nrows();
MatrixRow mr((GeneralMatrix*)this, LoadOnEntry+DirectPart);
for (int r = 1; r <= nr; r++)
{
int c; maxval = mr.Maximum1(maxval, c);
if (c > 0) { i = r; j = c; }
mr.Next();
}
((GeneralMatrix&)*this).tDelete(); return maxval;
}
Real GeneralMatrix::minimum2(int& i, int& j) const
{
REPORT
if (storage == 0) NullMatrixError(this);
Real minval = FloatingPointPrecision::Maximum(); int nr = Nrows();
MatrixRow mr((GeneralMatrix*)this, LoadOnEntry+DirectPart);
for (int r = 1; r <= nr; r++)
{
int c; minval = mr.Minimum1(minval, c);
if (c > 0) { i = r; j = c; }
mr.Next();
}
((GeneralMatrix&)*this).tDelete(); return minval;
}
Real Matrix::maximum_absolute_value2(int& i, int& j) const
{
REPORT
int k; Real m = GeneralMatrix::maximum_absolute_value1(k); k--;
i = k / Ncols(); j = k - i * Ncols(); i++; j++;
return m;
}
Real Matrix::minimum_absolute_value2(int& i, int& j) const
{
REPORT
int k; Real m = GeneralMatrix::minimum_absolute_value1(k); k--;
i = k / Ncols(); j = k - i * Ncols(); i++; j++;
return m;
}
Real Matrix::maximum2(int& i, int& j) const
{
REPORT
int k; Real m = GeneralMatrix::maximum1(k); k--;
i = k / Ncols(); j = k - i * Ncols(); i++; j++;
return m;
}
Real Matrix::minimum2(int& i, int& j) const
{
REPORT
int k; Real m = GeneralMatrix::minimum1(k); k--;
i = k / Ncols(); j = k - i * Ncols(); i++; j++;
return m;
}
Real SymmetricMatrix::sum_square() const
{
REPORT
Real sum1 = 0.0; Real sum2 = 0.0; Real* s = store; int nr = nrows_val;
for (int i = 0; i<nr; i++)
{
int j = i;
while (j--) sum2 += square(*s++);
sum1 += square(*s++);
}
((GeneralMatrix&)*this).tDelete(); return sum1 + 2.0 * sum2;
}
Real SymmetricMatrix::sum_absolute_value() const
{
REPORT
Real sum1 = 0.0; Real sum2 = 0.0; Real* s = store; int nr = nrows_val;
for (int i = 0; i<nr; i++)
{
int j = i;
while (j--) sum2 += fabs(*s++);
sum1 += fabs(*s++);
}
((GeneralMatrix&)*this).tDelete(); return sum1 + 2.0 * sum2;
}
Real IdentityMatrix::sum_absolute_value() const
{ REPORT return fabs(trace()); } // no need to do tDelete?
Real SymmetricMatrix::sum() const
{
REPORT
Real sum1 = 0.0; Real sum2 = 0.0; Real* s = store; int nr = nrows_val;
for (int i = 0; i<nr; i++)
{
int j = i;
while (j--) sum2 += *s++;
sum1 += *s++;
}
((GeneralMatrix&)*this).tDelete(); return sum1 + 2.0 * sum2;
}
Real IdentityMatrix::sum_square() const
{
Real sum = *store * *store * nrows_val;
((GeneralMatrix&)*this).tDelete(); return sum;
}
Real BaseMatrix::sum_square() const
{
REPORT GeneralMatrix* gm = ((BaseMatrix&)*this).Evaluate();
Real s = gm->sum_square(); return s;
}
Real BaseMatrix::norm_Frobenius() const
{ REPORT return sqrt(sum_square()); }
Real BaseMatrix::sum_absolute_value() const
{
REPORT GeneralMatrix* gm = ((BaseMatrix&)*this).Evaluate();
Real s = gm->sum_absolute_value(); return s;
}
Real BaseMatrix::sum() const
{
REPORT GeneralMatrix* gm = ((BaseMatrix&)*this).Evaluate();
Real s = gm->sum(); return s;
}
Real BaseMatrix::maximum_absolute_value() const
{
REPORT GeneralMatrix* gm = ((BaseMatrix&)*this).Evaluate();
Real s = gm->maximum_absolute_value(); return s;
}
Real BaseMatrix::maximum_absolute_value1(int& i) const
{
REPORT GeneralMatrix* gm = ((BaseMatrix&)*this).Evaluate();
Real s = gm->maximum_absolute_value1(i); return s;
}
Real BaseMatrix::maximum_absolute_value2(int& i, int& j) const
{
REPORT GeneralMatrix* gm = ((BaseMatrix&)*this).Evaluate();
Real s = gm->maximum_absolute_value2(i, j); return s;
}
Real BaseMatrix::minimum_absolute_value() const
{
REPORT GeneralMatrix* gm = ((BaseMatrix&)*this).Evaluate();
Real s = gm->minimum_absolute_value(); return s;
}
Real BaseMatrix::minimum_absolute_value1(int& i) const
{
REPORT GeneralMatrix* gm = ((BaseMatrix&)*this).Evaluate();
Real s = gm->minimum_absolute_value1(i); return s;
}
Real BaseMatrix::minimum_absolute_value2(int& i, int& j) const
{
REPORT GeneralMatrix* gm = ((BaseMatrix&)*this).Evaluate();
Real s = gm->minimum_absolute_value2(i, j); return s;
}
Real BaseMatrix::maximum() const
{
REPORT GeneralMatrix* gm = ((BaseMatrix&)*this).Evaluate();
Real s = gm->maximum(); return s;
}
Real BaseMatrix::maximum1(int& i) const
{
REPORT GeneralMatrix* gm = ((BaseMatrix&)*this).Evaluate();
Real s = gm->maximum1(i); return s;
}
Real BaseMatrix::maximum2(int& i, int& j) const
{
REPORT GeneralMatrix* gm = ((BaseMatrix&)*this).Evaluate();
Real s = gm->maximum2(i, j); return s;
}
Real BaseMatrix::minimum() const
{
REPORT GeneralMatrix* gm = ((BaseMatrix&)*this).Evaluate();
Real s = gm->minimum(); return s;
}
Real BaseMatrix::minimum1(int& i) const
{
REPORT GeneralMatrix* gm = ((BaseMatrix&)*this).Evaluate();
Real s = gm->minimum1(i); return s;
}
Real BaseMatrix::minimum2(int& i, int& j) const
{
REPORT GeneralMatrix* gm = ((BaseMatrix&)*this).Evaluate();
Real s = gm->minimum2(i, j); return s;
}
Real dotproduct(const Matrix& A, const Matrix& B)
{
REPORT
int n = A.storage;
if (n != B.storage)
{
Tracer tr("dotproduct");
Throw(IncompatibleDimensionsException(A,B));
}
Real sum = 0.0; Real* a = A.store; Real* b = B.store;
while (n--) sum += *a++ * *b++;
return sum;
}
Real Matrix::trace() const
{
REPORT
Tracer tr("trace");
int i = nrows_val; int d = i+1;