-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathxll_pwflat.cpp
103 lines (87 loc) · 2.81 KB
/
xll_pwflat.cpp
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
// xll_pwflat.cpp - piecewise flat curves
#include "fms_pwflat.h"
#include "G5260.h"
using namespace fms;
using namespace xll;
static AddIn xai_pwflat_curve(
Function(XLL_HANDLE, L"?xll_pwflat_curve", L"PWFLAT.CURVE")
.Arg(XLL_FP, L"times", L"is an array of times.")
.Arg(XLL_FP, L"rates", L"is an array of rates.")
.Uncalced() // <--- must have for handle classes
.Category(CATEGORY)
.FunctionHelp(L"Return a handle to a piecewise flat curve.")
);
HANDLEX WINAPI xll_pwflat_curve(const _FP12* pt, const _FP12* pr)
{
#pragma XLLEXPORT
handlex h;
try {
ensure (size(*pt) == size(*pr));
ensure (pwflat::monotonic(begin(*pt), end(*pt)));
xll::handle<pwflat::curve<>> h_(new pwflat::curve<>(size(*pt), pt->array, pr->array));
h = h_.get();
}
catch (const std::exception& ex) {
XLL_ERROR(ex.what());
}
return h;
}
static AddIn xai_pwflat_curve_extend(
Function(XLL_HANDLE, L"?xll_pwflat_curve_extend", L"PWFLAT.CURVE.EXTEND")
.Arg(XLL_HANDLE, L"handle", L"is a handle to a piecewise flat curve.")
.Arg(XLL_FP, L"times", L"is an array of times.")
.Arg(XLL_FP, L"rates", L"is an array of rates.")
.Category(CATEGORY)
.FunctionHelp(L"Return a handle to a piecewise flat curve.")
);
HANDLEX WINAPI xll_pwflat_curve_extend(HANDLEX h0, const _FP12* pt, const _FP12* pr)
{
#pragma XLLEXPORT
handlex h;
try {
ensure (size(*pt) == size(*pr));
ensure (pwflat::monotonic(begin(*pt), end(*pt)));
xll::handle<pwflat::curve<>> h_(h0);
ensure (h_);
for (int i = 0; i < size(*pt); ++i) {
h_->push_back(pt->array[i], pr->array[i]);
}
h = h_.get();
}
catch (const std::exception& ex) {
XLL_ERROR(ex.what());
}
return h;
}
static AddIn xai_pwflat_value(
Function(XLL_FP, L"?xll_pwflat_value", L"PWFLAT.VALUE")
.Arg(XLL_HANDLE, L"handle", L"is a handle to a piecewise flat curve.")
.Arg(XLL_FP, L"times", L"is an array of times at which to evaluate the curve.")
.Arg(XLL_DOUBLE, L"extrapolate", L"is an optional rate to extrapolate the curve.")
.Category(CATEGORY)
.FunctionHelp(L"Return the value of a piecewise flat curve at times.")
);
_FP12* WINAPI xll_pwflat_value(HANDLEX h, const _FP12* pt, double _f)
{
#pragma XLLEXPORT
static xll::FP12 f;
try {
xll::handle<pwflat::curve<>> c_(h);
ensure (c_);
auto n = size(*pt);
if (pt->rows == 1)
f.resize(1, n);
else
f.resize(n, 1);
if (_f == 0) {
_f = std::numeric_limits<double>::quiet_NaN();
}
for (int i = 0; i < n; ++i) {
f[i] = (*c_)(pt->array[i], _f);
}
}
catch (const std::exception& ex) {
XLL_ERROR(ex.what());
}
return f.get();
}