File: hess.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 (73 lines) | stat: -rw-r--r-- 1,751 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
#include "R_package.h"
#include "math.h"

void Hess_Sub (int p, double *pdX_i, double *pdMu, double *pdHess, double *pdTempP)
{
	int l, k ;

	double dNorm = 0 ;
	for (l = p - 1; l != -1; l--)
	{
		double &dCur = pdTempP [l] = pdX_i[l] - pdMu[l] ;
		dNorm += dCur * dCur ;
	}

	dNorm = 1 / sqrt (dNorm) ;
	double dNorm3 = pow (dNorm, 3.0) ;

	for (l = p - 1; l != -1; l--)
	{
		pdHess [l * p + l] += dNorm ;

		//for (k = p - 1; k != -1; k--)
		for (k = l; k != -1; k--)
			pdHess [l * p + k] -= pdTempP [l] * pdTempP[k] * dNorm3 ;
	}

}

void Hess (int p, int n, double *pdX, double *pdMu, double *pdHess, double *pdTempP1, double *pdTempP2)
{
	int i, j ;

	for (i = p - 1; i != -1; i--)
		for (j = p - 1; j != -1; j--)
			pdHess [i + j * p] = 0 ;

	for (i = n - 1; i != -1; i--)
	{
		for (j = p - 1; j != -1; j--)
			pdTempP2 [j] = pdX[i + j * n] ;

		Hess_Sub (p, pdTempP2, pdMu, pdHess, pdTempP1) ;
	}

	for (i = p - 1; i != -1; i--)
		for (j = i - 1; j != -1; j--)
			pdHess [i + j * p] = pdHess [i * p + j] ;
}

void Hess_Sub_R (int *pnPar, double *pdX_i, double *pdMu, double *pdHess)
{
	const int &p = pnPar[0]  ;
	double *pdTempP = new double [p] ;

	Hess_Sub (pnPar [0], pdX_i, pdMu, pdHess, pdTempP) ;
// VT::04.12.2017
// Causes an warning in clang++: "'delete' applied to a pointer that was allocated with 'new[]'; did you mean 'delete[]'? [-Wmismatched-new-delete]"
// 	- replace delete by delete[]
//	delete pdTempP ;
	delete[] pdTempP ;

}

void Hess_R (int *pnPar, double *pdX, double *pdMu, double *pdHess)
{
	double *pdTempP1 = new double [pnPar[0]], *pdTempP2 = new double [pnPar[0]] ;
	Hess (pnPar[0], pnPar[1], pdX, pdMu, pdHess, pdTempP1, pdTempP2) ;

// VT::04.12.2017
// -  as above
	delete[] pdTempP1 ;
	delete[] pdTempP2 ;
}