Normalize & Compliment
-
- Posts: 71
- Joined: Mon Aug 01, 2016 9:45 pm
Normalize & Compliment
Please post an example of the Normalize and Compliment functions for DSL_Dmatrix.
-
- Site Admin
- Posts: 1417
- Joined: Mon Nov 26, 2007 5:51 pm
Re: Normalize & Compliment
Here's the example for the Normalize method. Note that there are also overloads with single integer (representing the linear index into the matrix) and with no parameters (all columns are normalized).
Code: Select all
int testNormalize()
{
DSL_network net;
// hepar.xdsl is distributed with GeNIe. Your path will be different!
int res = net.ReadFile("d:/tmpx/hepar.xdsl");
if (DSL_OKAY != res)
{
return res;
}
int h = net.FindNode("ESR");
if (h < 0)
{
return h;
}
// 'live' access to ESR's definition.
DSL_Dmatrix & mtx = *net.GetNode(h)->Definition()->GetMatrix();
// select arbitrary coordinates for the CPT
// in this example, this is the second-to-last column
DSL_intArray coords;
coords.Add(1); // PBC = absent
coords.Add(2); // ChHepatitis = absent
coords.Add(1); // Steatosis = absent
coords.Add(0); // Hyperbilirubinemia = present
coords.Add(0); // final coordinate required, this is index of ESR's outcome
printf("Original values in the CPT:\n");
int base = mtx.CoordinatesToIndex(coords);
for (int i = 0; i < 3; i++)
{
printf("%d, %Lg\n", i, mtx[base + i]);
}
// write arbitrary values, these will be normalized to ensure total==1
mtx[coords] = 2;
mtx.NextCoordinates(coords);
mtx[coords] = 1;
mtx.NextCoordinates(coords);
mtx[coords] = 5;
// Normalize ignores the last coordinate, will replace it with zero anyway
mtx.Normalize(coords);
// [2,1,5] should get normalized to 2/8 (0.25), 1/8 (0.125), 5/8 (0.625)
// rewind back to 0th outcome in the specific CPT column
printf("Modified values in the CPT:\n");
for (int i = 0; i < 3; i++)
{
printf("%d, %Lg\n", i, mtx[base + i]);
mtx.NextCoordinates(coords);
}
return DSL_OKAY;
}
-
- Site Admin
- Posts: 1417
- Joined: Mon Nov 26, 2007 5:51 pm
Re: Normalize & Compliment
And here's the example for the Complement method.
Code: Select all
int testComplement()
{
DSL_network net;
// hepar.xdsl is distributed with GeNIe. Your path will be different!
int res = net.ReadFile("d:/tmpx/hepar.xdsl");
if (DSL_OKAY != res)
{
return res;
}
int h = net.FindNode("ESR");
if (h < 0)
{
return h;
}
// 'live' access to ESR's definition.
DSL_Dmatrix & mtx = *net.GetNode(h)->Definition()->GetMatrix();
// select arbitrary coordinates for the CPT
// in this example, this is the second-to-last column
DSL_intArray coords;
coords.Add(1); // PBC = absent
coords.Add(2); // ChHepatitis = absent
coords.Add(1); // Steatosis = absent
coords.Add(0); // Hyperbilirubinemia = present
coords.Add(0); // final coordinate required, this is index of ESR's outcome
printf("Original values in the CPT:\n");
int base = mtx.CoordinatesToIndex(coords);
for (int i = 0; i < 3; i++)
{
printf("%d, %Lg\n", i, mtx[base + i]);
}
// write arbitrary values to 0th and 2nd entry.
mtx[base] = 0.21;
mtx[base + 2] = 0.33;
// we'll be complementing the column by changing 1st entry
coords[coords.NumItems() - 1] = 1;
mtx.Complement(coords);
// [0.21, x, 0.33] should change to [0.21, 0.46, 0.33]
printf("Modified values in the CPT:\n");
for (int i = 0; i < 3; i++)
{
printf("%d, %Lg\n", i, mtx[base + i]);
mtx.NextCoordinates(coords);
}
return DSL_OKAY;
}