File: smat.cpp

package info (click to toggle)
r-cran-pcapp 1.9-73-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 800 kB
  • sloc: cpp: 5,961; ansic: 917; sh: 13; makefile: 2
file content (1076 lines) | stat: -rw-r--r-- 25,205 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
/*
    SMat - Simple Matrix Classes v0.1beta
    Copyright (C) 2011 by Heinrich Fritz (heinrich_fritz@hotmail.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/>.
*/

#define SMAT_FLAG_NO_INI
#include "smat.h"
#undef SMAT_FLAG_NO_INI

//#include <cstdlib>
#include "smat.meal.h"

	void *smat_malloc (t_size dwSize)	//	2do: move to meal (use e.g. R_malloc)
	{
		return new char [dwSize] ;
	}

	void smat_free (void *pPtr)
	{
		delete [] (char *) pPtr ;
	}

/*	void *smat_realloc (void *pOld, t_size dwOld, t_size dwNew)	//	2do: remove
	{
		void *pNew = smat_malloc (dwNew) ;

		if (dwOld && pOld)
			memcpy (pNew, pOld, sm_min (dwOld, dwNew)) ;
		delete [] (char *) pOld ;
		return pNew ;

	}
*/

////////////////
//	SMat_EXC  //
////////////////

	void SMat_EXC::OnException ()
	{
		meal_OnException (GetDate (), GetFile (), GetLine ()) ;
	}

	void SMat_EXC::OnUException ()
	{
		meal_OnUException () ;
	}

////////////////
//	SDataRef  //
////////////////

	SDataRef::SDataRef ()	//	2do: rename to SDataRef or CDataRef
		: m_pData (NULL), m_pDataEnd (NULL), m_dwRef (0), m_dwCount (0), m_bOwner  (TRUE), m_bStatic (FALSE)
	{

	}

	SDataRef::SDataRef (const t_size dwCount)
		: m_dwRef (0), m_bOwner (TRUE), m_bStatic (FALSE)
	{
		Alloc_NF (dwCount) ;
	}


	SDataRef::SDataRef (const t_size dwCount, void * const pData)
		: m_pData (pData), m_pDataEnd ((char *) pData + dwCount), m_dwRef (0), m_dwCount (dwCount), m_bOwner (FALSE), m_bStatic (FALSE)
	{
		ASSERT (pData) ;
	}

	SDataRef::~SDataRef ()
	{
		Free () ;
	}

	void SDataRef::Free ()
	{
		if (IsOwner ())
			//free (m_pData) ;
			smat_free (m_pData) ;
			//delete [] (char *) m_pData ;
		m_pData = NULL ;
		m_pDataEnd = NULL ;
		m_dwCount = 0 ;
	}

	void SDataRef::FreeIfIdle ()
	{
		if ((m_bStatic && m_dwRef <= 1) || 
			!m_dwRef)
			Free () ;
	}

	void Deref (SDataRef *&pRef)
	{
		SDataRef::sDeref (pRef) ;
	}

	void SDataRef::sDeref (SDataRef *&pRef)
	{
		if (pRef->Deref ())
			delete pRef ;
		pRef = NULL ;
	}

	SDataRef *SDataRef::Ref (SDataRef *&pRef)
	{
		if (pRef == this)
			return this ; 
		if (pRef)
			pRef->Deref () ;
		return Ref_NDR (pRef) ;
	}

	SDataRef *SDataRef::Ref_NDR (SDataRef *&pRef)	//	No DeRereference
	{
		IncRef () ;
		return pRef = this ;
	}

	BOOL SDataRef::Deref ()
	{
		--m_dwRef ;
		return !m_dwRef/* && !m_bStatic*/ ;
	}

	SDataRef *SDataRef::Recreate (t_size dwSize, SDataRef *&pRef)
	{
		THROW (IsOwner ()) ;

		if (GetRef () <= 1 || IsStatic ())
			Alloc (dwSize) ;
		else
			(new SDataRef (dwSize))->Ref (pRef) ;
		return pRef ;
	}

	void SDataRef::Alloc (t_size dwSize)
	{
		Free () ;
		Alloc_NF (dwSize) ;
	}

	void SDataRef::Alloc_NF (t_size dwSize)
	{
		if (dwSize)
		{
			GetDataRef () = smat_malloc (dwSize) ;
//			GetDataRef () = new char [dwSize] ;
			GetSizeRef () = dwSize ;
			GetDataEndRef () = (char *) GetData () + GetSize () ;
		}
		else
		{
			GetDataRef () = NULL ;
			GetSizeRef () = 0 ;
			GetDataEndRef () = NULL ;
		}
	}

	BOOL SDataRef::Require (t_size dwSize, SDataRef *&pRef)
	{
		if (dwSize <= GetSize ())
			return FALSE ;
		Recreate (dwSize, pRef) ;
		return TRUE ;
	}

	SDataRef &SDataRef::Empty ()
	{
		static SDataRef_Static emptyDR (0, FALSE) ;
		return emptyDR ;
	}

///////////////////////
//	SDataRef_Static  //
///////////////////////
	
	SDataRef_Static::SDataRef_Static (const t_size dwCount, const BOOL bStatic)
		: t_base (dwCount)
	{
		IncRef () ;
		if (bStatic)
			SetStatic () ;
	}

	SDataRef_Static::~SDataRef_Static ()
	{
		DecRef () ;
		ASSERT (!m_dwRef) ;
	}

	SDataRef_Static &SDataRef_Static::Require (t_size dwSize)
	{
		SDataRef *pFoo ;
		t_base::Require (dwSize, pFoo) ;
		return *this ;
	}

////////////////////
//	SDataRefCont  //
////////////////////

	SDataRefCont::SDataRefCont ()
		:m_ppData (NULL), m_dwSize (0)
	{
		
	}

	SDataRefCont::~SDataRefCont ()
	{
		Free () ;
	}

	void SDataRefCont::Require (t_size dwCount)
	{
		if (dwCount <= GetSize ())
			return ;

		t_pitem *pNew = new t_pitem [dwCount] ;			//	do a realloc for dataRef () with size dwCount
		if (GetSize ())
			memcpy (pNew, GetData (), GetMemSize ()) ;
		delete [] GetData () ;
		dataRef () = pNew ;

		//m_ppData = (t_pitem *) realloc (m_ppData, dwCount * sizeof (t_pitem)) ;
		//m_ppData = (t_pitem *) smat_realloc (m_ppData, m_dwSize * sizeof (t_pitem), dwCount * sizeof (t_pitem)) ;

		t_size i ;
		for (i = GetSize (); i < dwCount; ++i)
			GetData () [i] = new t_item ;
		sizeRef () = dwCount ;
	}

	SDataRef_Static &SDataRefCont::Item (t_size dwIdx)
	{
		Require (dwIdx + 1) ;
		return *GetData ()[dwIdx] ;
	}

	void SDataRefCont::Free ()
	{
		t_size i ;
		for (i = GetSize () - 1; i != NAI; i--)
			delete GetData () [i] ;
		//free (GetData ()) ;
		//smat_free (GetData ()) ;
		delete [] GetData () ;
		dataRef () = NULL ;
		sizeRef () = 0 ;
	}

	void SDataRefCont::FreeIfIdle ()
	{
		t_size i ;
		for (i = GetSize () - 1; i != NAI; i--)
			Item (i).FreeIfIdle () ;
	}

////////////////////
//	CDataCont_NT  //
////////////////////

	t_size &CDataCont_NT::GetInstanceCount ()
	{
		static t_size dwInstanceCount = 0 ;
		return dwInstanceCount ;
	}

////////////////////////
//	Temp - Functions  //
////////////////////////

	SDataRefCont &GetTempCont ()
	{
		static SDataRefCont cont;
		return cont ;
	}

	void RequireTemp (t_size dwCount)
	{
		GetTempCont().Require (dwCount) ;
	}

	SDataRef_Static &tempRef (t_size dwIdx)
	{
		return GetTempCont().Item (dwIdx) ;
	}


	SDataRefCont::CRefRange &GetPermTempRefRange ()
	{
		static SDataRefCont::CRefRange obj ;
		return obj ;
	}

	void FreeTempCont ()
	{
		GetTempCont ().FreeIfIdle () ;
	}

////////
////////
////////	//	smateasymath.h
////////		//	2do: sort this file!
////////

	void sme_matmult_R (const SCMatD &a, const SCMatD &b, SVMatD &c)
	{
		THROW (a.ncol () == b.nrow ()) ;
		c.Require (a.nrow (), b.ncol ()) ;

		sme_matmult_NC (a, b, c) ;
	}

	void sme_matmult (const SCMatD &a, const SCMatD &b, const SVMatD &c)
	{
		THROW (a.ncol () == b.nrow ()) ;
		THROW (a.nrow () == c.nrow () && c.ncol () == b.ncol ()) ;

		sme_matmult_NC (a, b, c) ;
	}

	void sme_matmult_NC (const SCMatD &a, const SCMatD &b, const SVMatD &c)
	{
		ASSERT (a.ncol () == b.nrow ()) ;
		ASSERT (a.nrow () == c.nrow () && c.ncol () == b.ncol ()) ;

		double one = 1.0, zero = 0.0;
		if (a.nrow () > 0 && a.ncol () > 0 && b.nrow () > 0 && b.ncol () > 0) {
		meal_gemm(
			"N", "N", 
			a.nrowPtrS (), b.ncolPtrS (), b.nrowPtrS (), 
			&one,
			a.GetData (), a.nrowPtrS (), 
			b.GetData (), b.nrowPtrS (), 
			&zero, c.GetData (), a.nrowPtrS ()
			);
		}
		else 
			c.Reset (0) ;
	}

	void sme_tmatmult_R (const SCMatD &a, const SCMatD &b, SVMatD &c, const BOOL bTransA, const BOOL bTransB)
	{
		c.Require (a.GetDim (bTransA), b.GetDim (!bTransB)) ;

		sme_tmatmult_NC (a, b, c, bTransA, bTransB) ;
	}

	void sme_tmatmult (const SCMatD &a, const SCMatD &b, const SVMatD &c, const BOOL bTransA, const BOOL bTransB)
	{
		THROW (a.GetDim (!bTransA) == b.GetDim (bTransB)) ;
		THROW (a.GetDim (bTransA) == c.nrow () && c.ncol () == b.GetDim (!bTransB)) ;

		sme_tmatmult_NC (a, b, c, bTransA, bTransB) ;
	}

	void sme_tmatmult_NC (const SCMatD &a, const SCMatD &b, const SVMatD &c, const BOOL bTransA, const BOOL bTransB)
	{
		//ASSERT (a.ncol () == b.nrow ()) ;
		ASSERT (a.GetDim (!bTransA) == b.GetDim (bTransB)) ;
		ASSERT (a.GetDim (bTransA) == c.nrow () && c.ncol () == b.GetDim (!bTransB)) ;
		//ASSERT (a.nrow () == c.nrow () && c.ncol () == b.ncol ()) ;

		double one = 1.0, zero = 0.0;
		if (a.nrow () > 0 && a.ncol () > 0 && b.nrow () > 0 && b.ncol () > 0) {
		meal_gemm(
			bTransA ? "T" : "N", bTransB ? "T" : "N", 
			a.GetDimPtrS_NC (bTransA), b.GetDimPtrS_NC (!bTransB), b.GetDimPtrS_NC (bTransB), 
			&one,
			a.GetData (), a.nrowPtrS (), 
			b.GetData (), b.nrowPtrS (), 
			&zero, c.GetData (), a.GetDimPtrS_NC (bTransA)
			);
		}
		else 
			c.Reset (0) ;
	}

	void sme_diag_R (const SVMatD &a, SVecD &c)
	{
		c.Require (sm_min<t_size> (a.nrow (), a.ncol ())) ;
		sme_diag_NC (a, c) ;
	}

	void sme_diag (const SVMatD &a, SVecD &c)
	{
		THROW (c.size () == ::sm_min (a.nrow (), a.ncol ())) ;
		sme_diag_NC (a, c) ;
	}

	void sme_diag_NC (const SVMatD &a, SVecD &c)
	{
		const t_size inc = a.GetColInc () + 1 ;

		double *pCur = a.GetData () ;
		t_size i ;
		for (i = 0; i < c.size (); i++)
		{
			c(i) = *pCur ;
			pCur += inc ;
		}
	}

/////////////////////
//	smat.random.h  //
/////////////////////

	double runif () { return meal_unif_rand () ; }
	double rnorm () { return meal_norm_rand () ; }
	double rexp () { return meal_exp_rand () ; }

	void runif (const SVData<double> &a)
	{
		runif_raw (a, a.GetDataEnd ()) ;
	}

	void runif_raw (double *p, double *pEnd)
	{
		for (; p < pEnd; ++p)
			*p = runif () ;
	}

	void runif_r (const SVData<double> &a)
	{
		runif_r_raw (a, a.GetDataEnd ()) ;
	}

	void runif_r_raw (double *p, double *pEnd)
	{
		for (--pEnd; p <= pEnd; --pEnd)
			*pEnd = runif () ;
	}

	void rnorm (const SVData<double> &a)
	{
		rnorm_raw (a, a.GetDataEnd ()) ;
	}

	void rnorm_raw (double *p, double *pEnd)
	{
		for (; p < pEnd; ++p)
			*p = rnorm () ;
	}

	void runif_raw (double *d, int l, double dL, double dU)
	{
		runif_raw (d, d + l, dL, dU) ;	
	}

	void runif_raw (double *d, double * const pEnd, double dL, double dU)
	{
		dU -= dL ;

		for ( ;d < pEnd; ++d)
			*d = runif () * dU + dL ;
	}

	void SampleNoReplace(int k, int n, int *y, int *x)
	{
		int i, j;
		for (i = n - 1; i != -1; i--)
			x[i] = i ;

		for (i = 0; i < k; i++)
		{
			j = int (n * runif()) ;
			y[i] = x[j] ;
			x[j] = x[--n];
		}
	}

///////////////////
//	Eigenvalues  //
///////////////////

	void sme_eigen_sqr_R	(const SCMatD &A,		SVecD &vVal,		SVMatD &mVec, BOOL bOrder)	{	ASSERT_TEMPRANGE (4, 4) ;	sme_eigen_sqr_RV	(!SMatD (tempRef (4), A), vVal, mVec, bOrder) ;	}
	void sme_eigen_sqr		(const SCMatD &A, const SVecD &vVal, const	SVMatD &mVec, BOOL bOrder)	{	ASSERT_TEMPRANGE (4, 4) ;	sme_eigen_sqr_V		(!SMatD (tempRef (4), A), vVal, mVec, bOrder) ;	}
	void sme_eigen_sqr_NC	(const SCMatD &A, const SVecD &vVal, const	SVMatD &mVec, BOOL bOrder)	{	ASSERT_TEMPRANGE (4, 4) ;	sme_eigen_sqr_NCV	(!SMatD (tempRef (4), A), vVal, mVec, bOrder) ;	}

	void sme_eigen_sqr_RV (const SVMatD &A, SVecD &vVal, SVMatD &mVec, BOOL bOrder)
	{
		t_size p = A.nrow () ;
		THROW (p == A.ncol ()) ;
		vVal.Redim (p) ;
		mVec.Redim (p, p) ;

		sme_eigen_sqr_NC (A, vVal, mVec, bOrder) ;
	}

	void sme_eigen_sqr_V (const SVMatD &A, const SVecD &vVal, const SVMatD &mVec, BOOL bOrder)	//	no redim
	{
		t_size  p = A.nrow () ;
		THROW (p == A.ncol ()) ;
		THROW (vVal.size () == p) ;
		THROW (mVec.nrow () == p && mVec.ncol () == p) ;

		sme_eigen_sqr_NC (A, vVal, mVec, bOrder) ;
	}

	
	void sme_eigen_sqr_NCV (const SVMatD &A, const SVecD &vVal, const SVMatD &mVec, BOOL bOrder)	//	no check
	{
		{
			ASSERT (A.nrow () == A.ncol ()) ;
			ASSERT (vVal.size () == A.ncol ()) ;
			ASSERT (mVec.nrow () == A.ncol () && mVec.ncol () == A.ncol ()) ;
		}
		int p = A.ncol () ;

//		SVecD &vWi = SVecD::TempFree_NC (1, p) ;
		SVecD vWi (tempRef (0), p) ;

		int nInfo ;
		int nWork = -1 ;

		double dWork ;

		meal_geev ("V", "N", &p, NULL, &p, NULL, NULL, NULL, &p, NULL, &p, &dWork, &nWork, &nInfo) ;

		nWork = int(dWork) ;
//		SVecD &vTemp = SVecD::TempFree_NC (2, nWork) ;
		SVecD vTemp (tempRef (1), nWork) ;

		if (bOrder)
		{
			ASSERT_TEMPRANGE (0, 3) ;
//			SMatD &mTempVec = SMatD::TempFree_NC (3, p, p) ;
			SMatD mTempVec (tempRef (2), p, p) ;

			meal_geev ("V", "N", &p, A, &p, vVal, vWi, mTempVec, &p, NULL, &p, vTemp, &nWork, &nInfo) ;

			// re-order
//			SVecN &vOrder = SVecN::TempFree_NC (0, p) ;
			SVecN vOrder (tempRef (3), p) ;
			meal_sort_order_rev (vVal, vOrder, p) ;

			mVec.CopyCol_Order_NC (mTempVec, *vOrder) ;
		}
		else
		{
			ASSERT_TEMPRANGE (0, 1) ;
			meal_geev ("V", "N", &p, A, &p, vVal, vWi, mVec, &p, NULL, &p, vTemp, &nWork, &nInfo) ;
		}

		THROW (!nInfo) ;
	}

	void sme_eigen_sym_2x2_norm_raw (double * const pdEval, double *const pdEVec, const double *const pd, const double &dZeroTol)
	{
		THROW (pd[2] == pd[1]) ;
		sme_eigen_sym_2x2_norm_raw_NC (pdEval, pdEVec, pd, dZeroTol) ;
	}

	void sme_eigen_sym_2x2_norm_raw_NC (double * const pdEval, double *const pdEVec, const double *const pd, const double &dZeroTol)
	{
		const double &a = pd[0], &b = pd[2], &d = pd[3] ;
//		ASSERT (b == pd[1]) ;

		const double	&dDet = pdEVec[0] = a * d - b * b,	//	"abusing" pdEVec as temporary memory
						&dTrace = pdEVec[1] = a + d ;
		double			&dTemp = pdEVec[2] ;

		double &dL1 = pdEval[0],
				&dL2 = pdEval[1] ;

		dTemp = sqrt (sm_sqr (dTrace) / 4.0 - dDet) ;		//	calculating eigenvalues
		dL2 = dTrace / 2 ;
		dL1 = dL2 + dTemp ;
		dL2 -= dTemp ;

		if (fabs (b) / (fabs (a) + fabs (d)) > dZeroTol)	//	calculate some kind of condition number, which is checked against a zero tolerance
		{
			pdEVec[0] = dL1 - d ;							//	calculating and norming the eigenvalues
			pdEVec[1] = sqrt (sm_sqr (pdEVec[0]) + sm_sqr (b)) ;
			pdEVec[0] /= pdEVec[1] ;
			pdEVec[1] = b / pdEVec[1] ;

			pdEVec[2] = dL2 - d ;
			pdEVec[3] = sqrt (sm_sqr (pdEVec[2]) + sm_sqr (b)) ;
			pdEVec[2] /= pdEVec[3] ;
			pdEVec[3] = b / pdEVec[3] ;
		}
		else
		{
			pdEVec[0] = pdEVec[3] = 1 ;
			pdEVec[1] = pdEVec[2] = 0 ;
		}
	}

///////////////
//	MatMult  //
///////////////

	void sme_matmult_a_diagb_at_R (const SCMatD &a, const SCVecD &b, SVMatD &c)
	{
		THROW (a.ncol () == b.size ()) ;
		c.Require (a.nrow (), a.nrow ()) ;
		sme_matmult_a_diagb_at_NC (a, b, c) ;
	}

	void sme_matmult_a_diagb_at	 (const SCMatD &a, const SCVecD &b, const SVMatD &c)
	{
		THROW (a.ncol () == b.size ()) ;
		THROW (c.nrow () == a.nrow () && c.ncol () == a.nrow ()) ;
		sme_matmult_a_diagb_at_NC (a, b, c) ;
	}

	void sme_matmult_a_diagb_at_NC (const SCMatD &a, const SCVecD &b, const SVMatD &c)
	{
		ASSERT (a.ncol () == b.size ()) ;
		ASSERT (c.nrow () == a.nrow () && c.ncol () == a.nrow ()) ;

//		SMatD &temp = SMatD::TempFree (0, a.nrow (), a.ncol ()) ;
		SMatD temp (tempRef (0), a.nrow (), a.ncol ()) ;
//		EO<OP::multiply>::McVcMd_byrow_NC (a, b, temp) ;
		EO<SOP::multiply>::MMcVct_NC (!temp, a, b) ;
		sme_tmatmult_NC (temp, a, c, FALSE, TRUE) ;
	}

	void sme_matmult_at_diagb_a_R (const SCMatD &a, const SCVecD &b, SVMatD &c)
	{
		THROW (a.nrow () == b.size ()) ;
		c.Require (a.ncol (), a.ncol ()) ;
		sme_matmult_at_diagb_a_NC (a, b, c) ;
	}

	void sme_matmult_at_diagb_a (const SCMatD &a, const SCVecD &b, const SVMatD &c)
	{
		THROW (a.nrow () == b.size ()) ;
		THROW (c.nrow () == a.ncol () && c.ncol () == a.ncol ()) ;
		sme_matmult_at_diagb_a_NC (a, b, c) ;
	}

	void sme_matmult_at_diagb_a_NC (const SCMatD &a, const SCVecD &b, const SVMatD &c)
	{
		ASSERT (a.nrow () == b.size ()) ;
		ASSERT (c.nrow () == a.ncol () && c.ncol () == a.ncol ()) ;

//		SMatD &temp = SMatD::TempFree (0, a.nrow (), a.ncol ()) ;
		SMatD temp (tempRef (0), a.nrow (), a.ncol ()) ;
		//EO<OP::multiply>::McVcMd_byrow_NC (a, b, temp) ;
		EO<SOP::multiply>::MMcVc (!temp, a, b) ;
		sme_tmatmult_NC (temp, a, c, TRUE, FALSE) ;
	}


////////////////////
//	matmult_a_at  //
////////////////////

	void sme_matmult_a_at_R		(const SCMatD &a, SVMatD &b, BOOL bTransA)
	{
		b.Require (a.GetDim (bTransA), a.GetDim (bTransA)) ;
		sme_matmult_a_at_NC (a, b, bTransA) ;
	}

	void sme_matmult_a_at		(const SCMatD &a, const SVMatD &b, BOOL bTransA)
	{
		THROW (b.nrow () == b.GetDim (bTransA) && b.ncol () == a.GetDim (bTransA)) ;
		sme_matmult_a_at_NC (a, b, bTransA) ;
	}

	void sme_matmult_a_at_NC	(const SCMatD &a, const SVMatD &b, BOOL bTransA)
	{
		ASSERT (b.nrow () == a.GetDim (bTransA) && b.ncol () == a.GetDim (bTransA)) ;

		sme_tmatmult_NC (a, a, !b, bTransA, !bTransA) ;
	}


//////////////////////
//	matmult_a_b_at  //
//////////////////////

	void sme_matmult_a_b_at_R		(const SCMatD &a, const SCMatD &b, SVMatD &c, BOOL bTransA, BOOL bTransB)
	{
		THROW (b.nrow () == b.ncol ()) ;
		THROW (a.GetDim (!bTransA) == b.nrow ()) ;
		c.Require (a.GetDim (bTransA), a.GetDim (bTransA)) ;
		sme_matmult_a_b_at_NC (a, b, c, bTransA, bTransB) ;
	}

	void sme_matmult_a_b_at		(const SCMatD &a, const SCMatD &b, SVMatD &c, BOOL bTransA, BOOL bTransB)
	{
		THROW (b.nrow () == b.ncol ()) ;
		THROW (a.GetDim (!bTransA) == b.nrow ()) ;
		THROW (c.nrow () == a.GetDim (bTransA) && c.ncol () == a.GetDim (bTransA)) ;
		sme_matmult_a_b_at_NC (a, b, c, bTransA, bTransB) ;
	}

	void sme_matmult_a_b_at_NC	(const SCMatD &a, const SCMatD &b, SVMatD &c, BOOL bTransA, BOOL bTransB)
	{
		ASSERT (b.nrow () == b.ncol ()) ;
		ASSERT (a.GetDim (!bTransA) == b.nrow ()) ;
		ASSERT (c.nrow () == a.GetDim (bTransA) && c.ncol () == a.GetDim (bTransA)) ;

//		SMatD &mTemp = SMatD::TempFree_NC (0, a.GetDim (bTransA), b.GetDim (!bTransB)) ;
		SMatD mTemp (tempRef (0), a.GetDim (bTransA), b.GetDim (!bTransB)) ;

		sme_tmatmult_NC (a, b, !mTemp, bTransA, bTransB) ;
		sme_tmatmult_NC (mTemp, a, c, FALSE, !bTransA) ;
	}

////////////////////
//	matmult_diag  //
////////////////////
	
	void sme_matmult_diag_R (const SCMatD &a, const SCMatD &b, SVecD &c)
	{
		THROW (a.ncol () == b.nrow ()) ;
		c.Require (sm_min (a.nrow (), b.ncol ())) ; 
		sme_matmult_diag_NC (a, b, c) ;
	}

	void sme_matmult_diag (const SCMatD &a, const SCMatD &b, const SVecD &c)
	{
		THROW (a.ncol () == b.nrow ()) ;
		THROW (sm_min (a.nrow (), b.ncol ()) == c.size ()) ;
		sme_matmult_diag_NC (a, b, c) ;
	}

	void sme_matmult_diag_NC (const SCMatD &a, const SCMatD &b, const SVecD &c)
	{
		ASSERT (a.ncol () == b.nrow ()) ;
		const t_size dwMin = sm_min (a.nrow (), b.ncol ()) ; 
		ASSERT (dwMin == c.size ()) ;

		c.Reset (0) ;

		const double *pdB = b.GetDataEnd () ;

		t_size i, j ;
		double *pdC = c.GetDataEnd () ;
		for (j = dwMin - 1; j != NAI; j--)
		{
			pdC-- ;
			for (i = a.ncol () - 1; i != NAI; i--)
				*pdC += a (j, i) * *--pdB ;
		}

/*
		const double *pdB = b ;

		t_size i, j ;

		double *pdC = c ;
		for (j = 0; j < dwMin; j++)
		{
			for (i = 0; i < a.ncol (); i++)
			{
				*pdC += a (j, i) * *pdB ;
				pdB++ ;
			}
			pdC++ ;
		}

*/
	}

	double sme_sum_matmult_diag (const SCMatD &a, const SCMatD &b)
	{
		double c = 0 ;
		sme_sum_matmult_diag (a, b, c) ;
		return c ;
	}

	double sme_sum_matmult_diag_NC (const SCMatD &a, const SCMatD &b)
	{
		double c = 0 ;
		sme_sum_matmult_diag_NC (a, b, c) ;
		return c ;
	}


	void sme_sum_matmult_diag (const SCMatD &a, const SCMatD &b, double &c)
	{
		THROW (a.ncol () == b.nrow ()) ;
		sme_sum_matmult_diag_NC (a, b, c) ;
	}

	void sme_sum_matmult_diag_NC (const SCMatD &a, const SCMatD &b, double &c)
	{
		ASSERT (a.ncol () == b.nrow ()) ;
		const t_size dwMin = sm_min (a.nrow (), b.ncol ()) ; 

//		if (c < 0)
		{
			c = 0 ;

			const double *pdB = b ;
			t_size i, j ;
			double dTemp ;
			for (j = 0; j < dwMin; j++)
			{
				dTemp = 0 ;
				for (i = 0; i < a.ncol (); i++)
					dTemp += a (j, i) * *pdB++ ;
				c += dTemp ;
			}
		}
/*		else
		{
			c = 0 ;
			const double *pdB = b.GetDataEnd () ;
			t_size i, j ;
			double dTemp ;
			for (j = dwMin - 1; j != NAI; j--)
			{
				dTemp = 0 ;
				for (i = a.ncol () - 1; i != NAI; i--)
					dTemp += a (j, i) * *--pdB ;
				c += dTemp ;
			}
		}
*/	}

	double sme_sum_diag_At_matmult_B (const SCMatD &a, const SCMatD &b)
	{
		double dSum ;
		sme_sum_diag_Bt_matmult_C (dSum, a, b) ;
		return dSum ;
	}

	double sme_sum_diag_At_matmult_B_NC (const SCMatD &a, const SCMatD &b)
	{
		double dSum ;
		sme_sum_diag_Bt_matmult_C_NC (dSum, a, b) ;
		return dSum ;
	}

	void sme_sum_diag_Bt_matmult_C (double &a, const SCMatD &b, const SCMatD &c)
	{	//	calculates  sum (diag (t (A) %*% B))
		THROW (b.nrow () == c.nrow ()) ;
		sme_sum_diag_Bt_matmult_C_NC (a, b, c) ;
	}

	void sme_sum_diag_Bt_matmult_C_NC (double &a, const SCMatD &b, const SCMatD &c)
	{	//	calculates  sum (diag (t (A) %*% B))

		ASSERT (b.nrow () == c.nrow ()) ;
		const t_size dwDim = sm_min (b.ncol (), b.ncol ()) ;

		double const * pdB = b, * const pdEndB = pdB + dwDim * b.nrow ();

		a = 0 ;
		EO<SOP::ApaBmC>::SVcVc_raw (a, pdB, pdEndB, c.GetData ()) ;
	}

////////////////////////
//	Calc Covariances  //
////////////////////////

	void cov_centered_R (SVMatD &a, const SCMatD &b, const double &dFact)
	{
		a.Require (b.ncol (), b.ncol ()) ;
		cov_centered_NC (a, b) ;
	}

	void cov_centered (const SVMatD &a, const SCMatD &b, const double &dFact)
	{
		THROW (a.nrow () == a.ncol ()) ;
		THROW (a.nrow () == b.ncol ()) ;
		cov_centered_NC (a, b) ;
	}

	void cov_centered_NC (const SVMatD &a, const SCMatD &b, const double &dFact)
	{		//	2do: think about only calculating half of it!
		ASSERT (a.nrow () == a.ncol ()) ;
		ASSERT (a.nrow () == b.ncol ()) ;

		sme_tmatmult_NC (b, b, a, TRUE, FALSE) ;
		EO<SOP::a_multiply>::VSc (*a, dFact / (b.nrow () - 1.0)) ;
	}

////////////
//	misc  //
////////////

	double median (const SCData<double> &a)
	{
		ASSERT_TEMPRANGE (0, 0) ;
		SVecD temp (tempRef (0), a.size ()) ;	//	2do: should be copied by constructor!
		temp.Copy_NC (a) ;
		return median_V (*temp) ;
	}

	double median_V (const SVData<double> &a)	//	2do: make this a template function!
	{
		int n = a.size () ;

		double *pA = a ;
		if (n < 3)
		{
			if (!n) 
				return meal_NaN () ;
			if (n == 1)
				return pA[0] ;
			return (pA[0] + pA[1]) / 2 ;
		}

		int nHalf = (n + 1) >> 1 ;

			//	odd length
		if (n & 1)
			return psort_V(a, nHalf-1);

			//	even length
		const double dTemp = psort_V(a, nHalf - 1) ;
		return (dTemp + min (pA + nHalf, pA + n)) / 2 ;
	}

	double mad0 (const SVData<double> &a) 
	{
		const double dCenter = median_V (a) ;
		EO<SOP::Aa_abs_AsB>::VSc (a, dCenter) ;

		return median_V (a) ;
	}

	double medianabs_V (const SVData <double> &a)
	{
		EO<SOP::a_abs>::V (a) ;
		return median_V (a) ;
	}

	double mad_V (const SVData<double> &a)
	{
		return mad0 (a) * 1.482602218505602 ;
	}

	double mad (const SCData<double> &a)
	{
		ASSERT_TEMPRANGE (0, 0) ;
		SVecD temp (tempRef (0), a.size ()) ;	//	2do: should be copied by the constructor!
		temp.Copy_NC (a) ;
		return mad_V (*temp) ;
	}

	void sort_R (const SCData<double> &a, SVecD &b)
	{
		b.Require (a.size ()) ;
		sort_NC (a, b) ;
	}

	void sort (const SCData<double> &a, const SVecD &b)
	{
		THROW (a.size () == b.size ()) ;
		sort_NC (a, b) ;
	}

	void sort_NC (const SCData<double> &a, const SVecD &b)
	{
		b.Copy_NC (a) ;
		meal_sort (b, b.size ()) ;
	}

	void sort (const SVData<double> &a)
	{
		meal_sort (a, a.size ()) ;
	}

	void sort_order (const SVData<double> &a, const SVData<int> &b)
	{
		THROW (a.size () == b.size ()) ;
		sort_order_NC (a, b) ;
	}

	void sort_order_NC (const SVData<double> &a, const SVData<int> &b)
	{
		ASSERT (a.size () == b.size ()) ;
		meal_sort_order (a, b, b.size ()) ;
	}

	void norm2 (double &dNorm, const SCData<double> &a)
	{
		dNorm = 0 ;
		EO<SOP::Apa_sqr_B>::SVc (dNorm, a) ;
		dNorm = sqrt (dNorm) ;
	}

	double norm2 (const SCData<double> &a)
	{
		double dNorm ;
		norm2 (dNorm, a) ;
		return dNorm ;
	}

//////////////////////////
//	Printing Functions  //
//////////////////////////

	void Print (const double &v)
	{
		meal_printf ("%f", v) ;
	}

	void Print (const float &v)
	{
		meal_printf ("%f", v) ;
	}

	void Print (const int &v)
	{
		meal_printf ("%d", v) ;
	}

	void Print (const t_size &v)
	{
		meal_printf ("%d", v) ;
	}


/*		//	2do: move to smat.test.cpp - file
	EXPORT void ex_mad (int *pnParIn, double *pdParOut, double *pdData)
	{
		*pdParOut = mad_V (*SVecD (pdData, *pnParIn)) ;
	}
*/
/*		//	2do: move to smat.test.cpp - file
	EXPORT void sme_tmatmult (int *pnParIn, double *pdA, double *pdB, double *pdC)	//	2do: put in different file!
	{		
		const t_size 
			na = pnParIn [0], pa = pnParIn [1],
			nb = pnParIn [2], pb = pnParIn [3],
			nc = pnParIn [4], pc = pnParIn [5] ;
		BOOL bTransA = pnParIn[6], bTransB = pnParIn[7] ;

		SCMatD a (pdA, na, pa) ;
		SCMatD b (pdB, nb, pb) ;
		SMatD c (pdC, nc, pc) ;

		sme_tmatmult_NC (a, b, !c, bTransA, bTransB) ;
	}
*/
/*		//	2do: move to smat.test.cpp - file
	EXPORT void ex_median (int *pnParIn, double *pdParOut, double *pdData)
	{
		*pdParOut = median_V (*SVecD (pdData, *pnParIn)) ;
	}
*/
/*		//	2do: move to smat.test.cpp - file
 	EXPORT void sme_matmult_a_diagb_at (int *pnParIn, double *pdA, double  *pdB, double *pdC)
	{
		int n = pnParIn [0], p = pnParIn [1] ;

		SCMatD a (pdA, n, p) ;
		SCVecD b (pdB, p) ;
		SMatD c (pdC, n, p) ;

		sme_matmult_a_diagb_at_NC (a, b, !c) ;
	}
*/
/*		//	2do: move to smat.test.cpp - file
	EXPORT void sme_eigen_sqr (int *pnParIn, double *pdA, double *pdVal, double *pdVec)	//	2do: put into different File
	{
		int p = pnParIn[0] ;
		BOOL bOrder = pnParIn[1] ;
		sme_eigen_sqr (!SMatD (pdA, p, p), SVecD (pdVal, p), !SMatD (pdVec, p, p), bOrder) ;
	}
*/