Skip to content

Instantly share code, notes, and snippets.

@superbobry
Created February 13, 2014 12:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save superbobry/8974461 to your computer and use it in GitHub Desktop.
Save superbobry/8974461 to your computer and use it in GitHub Desktop.
Idiomatic C++ code from hsmm R package
void InitParaAndVar(int CensoringPara, int tauPara, int JPara, int MPara, double dPara[],
double pPara[], double piPara[], double pdfPara[])
{
int i, j, u;
Censoring = CensoringPara;
tau = tauPara;
J = JPara;
M = MPara;
StateIn = matrix<double>(J, tau);
if (StateIn == NULL)
{
throw memory_exception();
}
F = matrix<double>(J, tau);
if (F == NULL)
{
throw memory_exception();
}
L = matrix<double>(J, tau);
if (L == NULL)
{
throw memory_exception();
}
G = matrix<double>(J, tau);
if (G == NULL)
{
throw memory_exception();
}
H = cube<double>(J, tau, M + 1);
if (H == NULL)
{
throw memory_exception();
}
L1 = matrix<double>(J, tau);
if (L1 == NULL)
{
throw memory_exception();
}
N = new double[tau];
if (N == NULL)
{
throw memory_exception();
}
Norm = matrix<double>(J, tau);
if (Norm == NULL)
{
throw memory_exception();
}
d = matrix<double>(J, M + 1);
if (d == NULL)
{
throw memory_exception();
}
if (M + 1 > tau)
D = matrix<double>(J, M + 1);
else
D = matrix<double>(J, tau + 1);
if (D == NULL)
{
throw memory_exception();
}
mean_d = new double[J];
if (mean_d == NULL)
{
throw memory_exception();
}
p = matrix<double>(J, J);
if (p == NULL)
{
throw memory_exception();
}
pi = new double[J];
if (pi == NULL)
{
throw memory_exception();
}
eta = matrix<double>(J, M + 1);
if (eta == NULL)
{
throw memory_exception();
}
xi = matrix<double>(J, M + 1);
if (xi == NULL)
{
throw memory_exception();
}
alpha = matrix<double>(J, tau);
if (alpha == NULL)
{
throw memory_exception();
}
maxI = matrix<int>(J, tau);
if (maxI == NULL)
{
throw memory_exception();
}
maxU = matrix<int>(J, tau);
if (maxU == NULL)
{
throw memory_exception();
}
pdf = matrix<double>(J, tau);
if (pdf == NULL)
{
throw memory_exception();
}
hiddenStates = new int[tau];
if (hiddenStates == NULL)
{
throw memory_exception();
}
for (j = 0; j <= J - 1; j++)
{
pi[j] = piPara[j];
}
for (j = 0; j <= J - 1; j++)
for (u = 0; u <= M - 1; u++) {
d[j][u + 1] = dPara[j * M + u];
}
for (i = 0; i <= J - 1; i++)
for (j = 0; j <= J - 1; j++)
{
p[i][j] = pPara[i * J + j];
}
for (j = 0; j <= J - 1; j++)
for (u = 0; u <= tau - 1; u++)
{
pdf[j][u] = pdfPara[j * tau + u];
}
switch (Censoring)
{
case noCensoring:
{
LeftCensoring = false;
RightCensoring = false;
break;
}
case rightCensoring:
{
LeftCensoring = false;
RightCensoring = true;
break;
}
case leftRightCensoring:
{
LeftCensoring = true;
RightCensoring = true;
break;
}
}
}
@olegs
Copy link

olegs commented Feb 13, 2014

What does memory_exception() look like?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment