File: PCAproj.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 (224 lines) | stat: -rw-r--r-- 7,228 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
#include "PCAproj.h"

	void vec_mult_mat_t_partial (double *pA, double const *pB, double const *pC, const int n, const int p, const int nDimN)
	{
		//	calculates
		//
		//	pA <- pB %*% t (pC[1:n, ])	
		//	with	pA: a result vector of length n
		//			pB: a vector of length p
		//			pC: a matrix of dimension nDimN x p
		//			with n <= nDimN

		THROW (n <= nDimN) ;
		const t_size dwJump = nDimN - n ;
		double const *const pEndC = pC + nDimN * p ;

		double * const pStartA = pA, * const pEndA = pA + n ;

		for (; pA < pEndA; ++pA)
			*pA = 0 ;

		while (pC < pEndC)
		{
			pA = pStartA ;
			while (pA < pEndA)
			{
				*pA += *pB * *pC ;
				++pA ;
				++pC ;
			}

			pC += dwJump ;
			++pB ;
		}
	}

	CPCAproj::CPCAproj (int *pnParIn, double *pdParIn, double *pdX, double *pdZ, double *pdL, double *pdSDev)
		: m_dwN (pnParIn[0]), m_dwP (pnParIn[1]), m_dwRealN (pnParIn[2]), m_dwK (pnParIn[3])
		, m_nScal (pnParIn[4]), m_nScores (pnParIn[5])
		, m_dZeroTol (pdParIn [0]), m_dCurLambda (0)
		, m_mX (pdX, m_dwN, m_dwP), m_mL (pdL, m_dwP, m_dwK), m_mA (m_dwN, m_dwP)
		, m_vSDev (pdSDev, m_dwK), m_vCurScore (m_dwN)
		, m_vHelpTF (m_dwN)
	{
		if (m_nScores)
			m_mZ.Set (pdZ, m_dwRealN, m_dwK) ;
	}

	void NULL1 (const SMatD &a)	//	takes a pxp loadings matrix, where the last column is not filled, and computes this last column
	{
		ASSERT_TEMPRANGE (0, 1) ;
		ASSERT (a.nrow () == a.ncol ()) ;

		t_size p = a.nrow () ;

		const SVecD &vLastCol = a.GetColRef (p-1) ;
		const SMatD mLpm1 (a.GetColRef (0, p - 1)) ;

		vLastCol.Reset (1) ;
		EO<UOP::Aa_As_sqrB>::VMc (*vLastCol, mLpm1) ;
		EO<SOP::a_sqrt>::V (*vLastCol) ;

		int n = which_max1 (vLastCol) ;

		SVecD vTemp1 (tempRef (0), p - 1), vTemp2 (tempRef (1), p) ;

		CopyRow (*vTemp1, mLpm1, n) ;				//	2do: implement SnVec -> no copy..
		vTemp2.Reset (0) ;
		EO<SOP::ApaBmC>::VMcVct_NC (*vTemp2, mLpm1, vTemp1) ;

 		vTemp2(n) *= -1.0 ;																	//	for not triggering element n in the next line, which is positive by default...
		EO<UOP::if_B_gr_0_AamA>::VVc (*vLastCol, vTemp2) ;									//	assigns the negative signs of vector vTemp2 to vector vCurEVec (assuming that vCurEVec has only positive elemts)
	}

	void CPCAproj::SetSingular (t_size dwK)
	{
		m_mZ.GetColRef (dwK, m_dwK).Reset (0) ;
		m_vSDev.GetDataRef (dwK, m_dwK).Reset (0) ;
		if (!dwK)
			SetDiag (!m_mL) ;
		else
			m_vSDev.GetDataRef (dwK, m_dwK).Reset (-1) ;	//	sets the sdev to -1, indicating, that the according columns of the Loadings Matrix are invalid!
			
	}

	void CPCAproj::Calc ()
	{
		SVecD vPcol (m_dwN), vVH (m_dwP), vHlp (m_dwN), vHlpS (vHlp) ;
		SVecD vCurA (tempRef (0), m_dwP) ;

		SVecD vCurScoreS (*m_vCurScore, m_dwRealN) ;

		//double *pdCurLambda = m_vSDev ;

		t_size i, j ;
		for (i = 0; i < m_dwK; i++)
		{
			const SVecD &vCurEVec = m_mL.GetColRef (i) ;

			vHlp.Reset (0) ;
			EO<SOP::Apa_sqr_B>::VMc (*vHlp, m_mX) ;												//R	vHlp <- rowSums (mY^2)
			m_dwShortN = 0 ;
			EO<UOP::aB_cA_C_le_D>::SVScVc (m_dwShortN, *m_vHelpTF, m_dZeroTol, vHlp) ;			//R	m_vHelpTF <- (vHlp>dZeroTol); m_dwShortN <- sum ()

			if (!m_dwShortN)	//	all observations seem to be concentrated in one point (in the center) when considering the current subspace
			{
				SetSingular (i) ;
				return ;
			}

			vHlpS.Reshape (m_dwShortN) ;
			m_mA.Reshape (m_dwShortN, m_dwP) ;

			EO<SOP::a_sqrt>::V (*vHlp) ;														//R	vHlp <- sqrt (vHlp)
			EO<SOP::divide>::MsMcVcVbc (!m_mA, m_mX, vHlp, m_vHelpTF) ;							//R	m_mA <- (m_mX / vHlp)[m_vHelpTF,]

			m_vCurScore.Reshape (m_dwRealN) ;

			if (i < m_dwP - 1)
			{
				t_size dwBestj = NAI;

				//for (j = 0; j < m_dwShortN; ++j)
				for (j = m_dwShortN - 1; j != NAI; --j)
				{
					CopyRow (*vCurA, m_mA, j) ;														//R	vCurA <- m_mA[j, ]
					vec_mult_mat_t_partial (m_vCurScore, vCurA, m_mX, m_dwRealN, m_dwP, m_dwN) ;	//R	m_vCurScore <- vCurA %*% m_mX[1:m_dwRealN,]	//RR	Y =A %*% t(y[1:n,]);		
					double dScat = ApplyMethod (m_vCurScore, m_nScal) ;								//R dScat = fscale (m_vCurScore)				//RR	pcol = apply (Y, 1, fs)
					if (dwBestj == NAI || m_dCurLambda < dScat)
					{
						dwBestj = j ;																												//RR	istar <- which.max (pcol)
						m_dCurLambda = dScat ;																										//RR	lambda[i] <- pcol[istar]
					}
				}

				CopyRow (*vCurEVec, m_mA, dwBestj) ;												//R	vCurEVec <- m_mA[dwBestj,]					//RR	vhelp <- A[istar,]
				m_vCurScore.Reshape (m_dwN) ;
				m_vCurScore.Reset (0) ;
				EO<SOP::ApaBmC>::VMcVct (*m_vCurScore, m_mX, vCurEVec) ;							//R m_vCurScore <- m_mX %*% vCurEVec			//RR	scorevec <- y%*%(A[istar,])
				Update (vCurEVec) ;

				if (m_nScores)
					Copy (*m_mZ.GetColRef (i), vCurScoreS) ;

				if (i < m_dwK - 1)
					EO<SOP::AsaBmC>::MVcVct (!m_mX, m_vCurScore, vCurEVec) ;						//R	m_mX <- m_mX - m_vCurScore %*% vCurEVec

				m_vSDev (i) = m_dCurLambda ;
//				*pdCurLambda = m_dCurLambda ;
//				++pdCurLambda ;
			}
			else
			{
				NULL1 (m_mL) ;	//	computes the last eigenvector (loadings vector) in m_mL

				m_vCurScore.Reshape (m_dwN) ;
				m_vCurScore.Reset (0) ;
				EO<SOP::ApaBmC>::VMcVct (*m_vCurScore, m_mX, vCurEVec) ;							//R m_vCurScore <- m_mX %*% vCurEVec			//RR	scorevec <- y%*%(A[istar,])
								//	2do -> pass this to BLAS

				//*pdCurLambda = 
				m_vSDev (i) = ApplyMethod (m_vCurScore, m_nScal) ;									//R	dCurLambda = fscale (m_vCurScore)

				if (m_nScores)
					Copy (*m_mZ.GetColRef (i), vCurScoreS) ;

			}
		}
	}

	CPCAprojU::CPCAprojU (int *pnParIn, double *pdParIn, double *pdX, double *pdZ, double *pdL, double *pdSDev)
		: CPCAproj (pnParIn, pdParIn, pdX, pdZ, pdL, pdSDev)
		, m_dwMaxIt (pnParIn[6]), m_dwMaxHalf (pnParIn[7]) {}

	void CPCAprojU::Update (const SVecD &vCurEVec)
	{
		ASSERT_TEMPRANGE (11, 12) ;

		t_size m, kk ;

		SVecN	vScoreSign (tempRef (0), m_dwShortN) ;
		SVecD	vVH (tempRef (11), m_dwP),
				vTScores (tempRef (12), m_dwN) ;

		for (m = m_dwMaxIt; m ; --m)
		{
			EO<SOP::sign>::VsVcVbc (*vScoreSign, m_vCurScore, m_vHelpTF) ;						//R	vScoreSign <- sign (vScoreSign [m_vHelpTF])

			vVH.Reset (0) ;																		//R	{
			EO<UOP::ApaBm_signC>::VtMcVc (*vVH, m_mA, vScoreSign) ;								//R		vVH <- t (m_mA) %*% vScoreSignS
			double dNewObj, dSqSum = 0 ;														//R		dSqSum <- sum (vVH^2)
			EO<SOP::Apa_sqr_B>::SVc (dSqSum, vVH) ;												//R	}

			//	...
			//		this part of the original R source has been merged with the following for loop
			//	...

			for (kk = 0; kk <= m_dwMaxHalf; ++kk)
			{
				if (kk)
				{
					dSqSum = 0 ;																//R	vVH <- (vVH + vCurEVec) / 2
					EO<UOP::Ba_BpC_d2_Apa_sq_B>::SVVc (dSqSum, *vVH, vCurEVec) ;				//R	dSqSum <- sum (vVH^2)
				}

				EO<SOP::a_divide>::VSc (*vVH, sqrt (dSqSum)) ;									//R	vVH <- vVH / sqrt (dSqSum)

				vTScores.Reset (0) ;															//R
				EO<SOP::ApaBmC>::VMcVct (*vTScores, m_mX, vVH) ;								//R	VTScores <- mX %*% vVH
				dNewObj = ApplyMethod (vTScores, m_nScal) ;										//R	dNewObj <- fscale (vTScores)

				if (dNewObj >= m_dCurLambda)
					break ;
			}

			if (dNewObj < m_dCurLambda)
				break ;

			Copy (*m_vCurScore, vTScores) ;														//R	m_vCurScore <- vTScores
			Copy (*vCurEVec, vVH) ;																//R	vCurEVec <- vVH
			m_dCurLambda = dNewObj;
		}
	}