Skip to content

Commit

Permalink
export sobol points
Browse files Browse the repository at this point in the history
  • Loading branch information
asavine committed Dec 26, 2020
1 parent e32154e commit ce71e4a
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 1 deletion.
9 changes: 9 additions & 0 deletions xlComp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,25 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "xlComp", "xlComp.vcxproj",
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{ACEA4631-013B-4AD4-9780-3DC6039A7A51}.Debug|x64.ActiveCfg = Debug|x64
{ACEA4631-013B-4AD4-9780-3DC6039A7A51}.Debug|x64.Build.0 = Debug|x64
{ACEA4631-013B-4AD4-9780-3DC6039A7A51}.Debug|x86.ActiveCfg = Debug|Win32
{ACEA4631-013B-4AD4-9780-3DC6039A7A51}.Debug|x86.Build.0 = Debug|Win32
{ACEA4631-013B-4AD4-9780-3DC6039A7A51}.Release|x64.ActiveCfg = Release|x64
{ACEA4631-013B-4AD4-9780-3DC6039A7A51}.Release|x64.Build.0 = Release|x64
{ACEA4631-013B-4AD4-9780-3DC6039A7A51}.Release|x86.ActiveCfg = Release|Win32
{ACEA4631-013B-4AD4-9780-3DC6039A7A51}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B8A5CF54-BDB0-4962-8998-C9DEDAB4D496}
EndGlobalSection
EndGlobal
47 changes: 47 additions & 0 deletions xlExport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1208,6 +1208,41 @@ LPXLOPER12 xToyDupireBarrierMcRisks(
return results;
}

extern "C" __declspec(dllexport)
LPXLOPER12 xSobolPoints(
double numPoints,
double dimension,
double anti,
double skip)
{
FreeAllTempMemory();

if (numPoints <= 0.0 || dimension <= 0.0) return TempErr12(xlerrNA);

auto rng = make_unique<Sobol>();
rng->init(int(dimension));
rng->skipTo((unsigned)skip);

vector<double> pt((int) dimension);
matrix<double> pts((int) numPoints, (int) dimension);

int i = 0;
while (i < numPoints)
{
rng->nextU(pt);
copy(pt.begin(), pt.end(), pts[i]);
++i;

if (i < numPoints && anti > 0.5)
{
for (double& xi : pt) xi = 1 - xi;
copy(pt.begin(), pt.end(), pts[i]);
++i;
}
}

return from_matrix(pts);
}

// Registers

Expand Down Expand Up @@ -1541,6 +1576,18 @@ extern "C" __declspec(dllexport) int xlAutoOpen(void)
(LPXLOPER12)TempStr12(L"Toy Dupire Barrier MC AAD risks"),
(LPXLOPER12)TempStr12(L""));

Excel12f(xlfRegister, 0, 11, (LPXLOPER12)&xDLL,
(LPXLOPER12)TempStr12(L"xSobolPoints"),
(LPXLOPER12)TempStr12(L"QBBBB"),
(LPXLOPER12)TempStr12(L"xSobolPoints"),
(LPXLOPER12)TempStr12(L"numPoints, dimension, [antithetic], [skip]"),
(LPXLOPER12)TempStr12(L"1"),
(LPXLOPER12)TempStr12(L"myOwnCppFunctions"),
(LPXLOPER12)TempStr12(L""),
(LPXLOPER12)TempStr12(L""),
(LPXLOPER12)TempStr12(L"Visualization of Sobol points"),
(LPXLOPER12)TempStr12(L""));

/* Free the XLL filename */
Excel12f(xlFree, 0, 1, (LPXLOPER12)&xDLL);

Expand Down
2 changes: 1 addition & 1 deletion xlMemoryPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
// Total amount of memory to allocate for all temporary XLOPERs
//

#define MEMORYSIZE 4194304
#define MEMORYSIZE 16777216

class MemoryPool
{
Expand Down
12 changes: 12 additions & 0 deletions xlOper.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,18 @@ LPXLOPER12 from_labelsAndNumbers(const vector<string>& labels, const vector<doub
return oper;
}

LPXLOPER12 from_matrix(const matrix<double>& mat)
{
const size_t n = mat.rows(), m = mat.cols();
if (n == 0 || m == 0) return TempErr12(xlerrNA);

LPXLOPER12 oper = TempXLOPER12();
resize(oper, n, m);
for (size_t i = 0; i < n; ++i) for (size_t j = 0; j < m; ++j) setNum(oper, mat[i][j], i, j);

return oper;
}

LPXLOPER12 from_labelledMatrix(const vector<string>& rowLabels, const vector<string>& colLabels, const matrix<double>& mat)
{
const size_t n = rowLabels.size(), m = colLabels.size();
Expand Down
Binary file added xlTest.xlsm
Binary file not shown.

0 comments on commit ce71e4a

Please sign in to comment.