Repository navigation
Expand file tree
/
Copy pathxll_array.cpp
More file actions
477 lines (416 loc) · 10.4 KB
/
Copy pathxll_array.cpp
File metadata and controls
477 lines (416 loc) · 10.4 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
// xll_array.cpp - array functions
#include <numeric>
#include "xll/xll/xll.h"
#ifndef CATEGORY
#define CATEGORY "XLL"
#endif
using namespace xll;
using xcstr = traits<XLOPERX>::xcstr;
AddIn xai_array_(
Function(XLL_HANDLEX, "xll_array_", "\\ARRAY")
.Arguments({
Arg(XLL_FPX, "array", "is the array to set.", "{1,2;3,4}")
})
.Uncalced()
.FunctionHelp("Return a handle to an array.")
.Category(CATEGORY)
.Documentation(R"(
Create a handle to a two dimensional array of floating point numbers.
)")
);
HANDLEX WINAPI xll_array_(_FPX* px)
{
#pragma XLLEXPORT
handle<FPX> h(new FPX(*px));
return h.get();
}
AddIn xai_array_get(
Function(XLL_FPX, "xll_array_get", "ARRAY")
.Arguments({
Arg(XLL_HANDLEX, "handle", "is a handle returned by \\ARRAY().")
})
.FunctionHelp("Return the array held by a handle.")
.Category(CATEGORY)
.Documentation(R"(
Return a two dimensional array of cells.
)")
);
_FPX* WINAPI xll_array_get(HANDLEX h)
{
#pragma XLLEXPORT
handle<FPX> h_(h);
if (!h_) {
XLL_ERROR(__FUNCTION__ ": unknown handle");
return nullptr;
}
return h_->get();
}
AddIn xai_array_size(
Function(XLL_LONG, "xll_array_size", "ARRAY.SIZE")
.Arguments({
Arg(XLL_FPX, "array", "is an array or handle to an array."),
})
.FunctionHelp("Return the size of array.")
.Category(CATEGORY)
.Documentation("Return the size of an array.")
);
LONG WINAPI xll_array_size(_FPX* pa)
{
#pragma XLLEXPORT
LONG n = size(*pa);
if (n == 1) {
handle<FPX> a_(pa->array[0]);
if (a_) {
n = a_->size();
}
}
return n;
}
AddIn xai_array_rows(
Function(XLL_LONG, "xll_array_rows", "ARRAY.ROWS")
.Arguments({
Arg(XLL_FPX, "array", "is an array or handle to an array."),
})
.FunctionHelp("Return the number of rows of an array.")
.Category(CATEGORY)
.Documentation("Return the number of rows of an array.")
);
LONG WINAPI xll_array_rows(_FPX* pa)
{
#pragma XLLEXPORT
LONG n = pa->rows;
if (n == 1) {
handle<FPX> a_(pa->array[0]);
if (a_) {
n = a_->rows();
}
}
return n;
}
AddIn xai_array_columns(
Function(XLL_LONG, "xll_array_columns", "ARRAY.COLUMNS")
.Arguments({
Arg(XLL_FPX, "array", "is an array or handle to an array."),
})
.FunctionHelp("Return the number of columns of an array.")
.Category(CATEGORY)
.Documentation("Return the number of columns of an array.")
);
LONG WINAPI xll_array_columns(_FPX* pa)
{
#pragma XLLEXPORT
LONG n = pa->columns;
if (n == 1) {
handle<FPX> a_(pa->array[0]);
if (a_) {
n = a_->columns();
}
}
return n;
}
AddIn xai_array_resize(
Function(XLL_FPX, "xll_array_resize", "ARRAY.RESIZE")
.Arguments({
Arg(XLL_FPX, "array", "is an array or handle to an array."),
Arg(XLL_LONG, "rows", "is the number of rows."),
Arg(XLL_LONG, "columns", "is the number of columns."),
})
.FunctionHelp("Resize an array.")
.Category(CATEGORY)
.Documentation(R"(
Resize array to <code>rows</code> and <code>columns</code>.
If <code>array</code> is a handle this function resizes the in-memory array and
returns its handle, otherwise the resized array is returned.
)")
);
_FPX* WINAPI xll_array_resize(_FPX* pa, LONG r, LONG c)
{
#pragma XLLEXPORT
static FPX a;
try {
a = *pa;
if (size(*pa) == 1) {
handle<FPX> a_(pa->array[0]);
if (a_) {
a_->resize(r, c);
}
}
else {
a.resize(r, c);
}
}
catch (const std::exception& ex) {
XLL_ERROR(ex.what());
}
catch (...) {
XLL_ERROR(__FUNCTION__ ": unknown exception");
}
return a.get();
}
AddIn xai_array_index(
Function(XLL_FPX, "xll_array_index", "ARRAY.INDEX")
.Arguments({
Arg(XLL_FPX, "array", "is a array or handle to a array."),
Arg(XLL_LPOPER, "rows", "are an array of rows to return."),
Arg(XLL_LPOPER, "columns", "are an array of columns to return."),
})
.FunctionHelp("Return rows and columns of array.")
.Category(CATEGORY)
.Documentation(R"(
This works like <code>INDEX</code> for arrays.
)")
);
_FPX* WINAPI xll_array_index(_FPX* parray, LPOPER prows, LPOPER pcolumns)
{
#pragma XLLEXPORT
static FPX a;
try {
unsigned r = prows->size();
unsigned c = pcolumns->size();
if (size(*parray) == 1) {
handle<FPX> h_(parray->array[0]);
if (h_) {
parray = h_->get();
}
}
if (prows->is_missing()) {
r = parray->rows;
}
if (pcolumns->is_missing()) {
c = parray->columns;
}
a.resize(r, c);
for (unsigned i = 0; i < r; ++i) {
unsigned ri = prows->is_missing() ? i : static_cast<unsigned>((*prows)[i].val.num);
for (unsigned j = 0; j < c; ++j) {
unsigned cj = pcolumns->is_missing() ? j : static_cast<unsigned>((*pcolumns)[j].val.num);
a(i, j) = index(*parray, ri, cj);
}
}
}
catch (const std::exception& ex) {
XLL_ERROR(ex.what());
}
return a.get();
}
AddIn xai_array_take(
Function(XLL_FPX, "xll_array_take", "ARRAY.TAKE")
.Arguments({
Arg(XLL_FPX, "array", "is a array or handle to a array."),
Arg(XLL_LONG, "count", "take items from front (count > 0) or back (count < 0)."),
})
.FunctionHelp("Return taken array items.")
.Category(CATEGORY)
.Documentation(R"(
If array has one row/column then take row/columns elements,
otherwise take array rows.
)")
);
_FPX* WINAPI xll_array_take(_FPX* parray, LONG n)
{
#pragma XLLEXPORT
static FPX a;
try {
a = FPX{};
a[0] = std::numeric_limits<double>::quiet_NaN();
if (size(*parray) == 1) {
handle<FPX> h_(parray->array[0]);
if (h_) {
xll::take(h_->get(), n);
a[0] = parray->array[0];
}
}
else {
a = *take(parray, n);
}
}
catch (const std::exception& ex) {
XLL_ERROR(ex.what());
}
return a.get();
}
AddIn xai_array_sequence(
Function(XLL_FP, "xll_array_sequence", "ARRAY.SEQUENCE")
.Arguments({
Arg(XLL_DOUBLE, "start", "is the first value in the sequence.", "0"),
Arg(XLL_DOUBLE, "stop", "is the last value in the sequence.", "3"),
Arg(XLL_DOUBLE, "_incr", "is an optional value to increment by. Default is 1.")
})
.FunctionHelp("Return a one column array from start to stop with specified optional increment.")
.Category(CATEGORY)
.Documentation(R"(
Return a one columns array <code>{start; start + incr; ...; stop}<code>.
If <code>incr</code> is an integer greater than 1, then generate
an array of <code>incr</code> numbers from <code>start</code> to <code>stop</code>.
)")
);
_FPX* WINAPI xll_array_sequence(double start, double stop, double incr)
{
#pragma XLLEXPORT
static xll::FPX a;
try {
if (incr == 0) {
incr = 1;
if (start > stop) {
incr = -1;
}
}
unsigned n;
if (incr > 1) {
n = static_cast<unsigned>(incr);
incr = (stop - start) / (n - 1);
}
else {
n = 1u + static_cast<unsigned>(fabs((stop - start) / incr));
}
a.resize(n, 1);
for (unsigned i = 0; i < n; ++i) {
a[i] = start + i * incr;
}
}
catch (const std::exception& ex) {
XLL_ERROR(ex.what());
}
catch (...) {
XLL_ERROR("ARRAY.SEQUENCE: unknown exception");
}
return a.get();
}
AddIn xai_array_diff(
Function(XLL_FPX, "xll_array_diff", "ARRAY.DIFF")
.Arguments({
Arg(XLL_FPX, "array", "is an array or handle to an array."),
Arg(XLL_CSTRING, "_op", "is a binary operation.")
})
.FunctionHelp("Return adjacent differences of array.")
.Category(CATEGORY)
.Documentation(R"xyzyx(
Call <code>std::adjacent_difference</code> using <code>op</code>.
If <op> is missing use subtraction ("-").
)xyzyx")
);
_FPX* WINAPI xll_array_diff(_FPX* pa, xcstr op)
{
#pragma XLLEXPORT
unsigned na = size(*pa);
if (na == 1) {
handle<FPX> a_(pa->array[0]);
if (a_) {
pa = a_->get();
}
}
if (*op == 0 or *op == '-') {
std::adjacent_difference(begin(*pa), end(*pa), begin(*pa));
}
if (*op == '/') {
std::adjacent_difference(begin(*pa), end(*pa), begin(*pa), std::divides<double>{});
}
return pa;
}
AddIn xai_array_scan(
Function(XLL_FPX, "xll_array_scan", "ARRAY.SCAN")
.Arguments({
Arg(XLL_FPX, "array", "is an array or handle to an array."),
Arg(XLL_CSTRING, "_op", "is a binary operation")
})
.FunctionHelp("Return right fold of array using op.")
.Category(CATEGORY)
.Documentation(R"xyzyx(
If <op> is missing use addition ("+").
)xyzyx")
);
_FPX* WINAPI xll_array_scan(_FPX* pa, xcstr op)
{
#pragma XLLEXPORT
unsigned na = size(*pa);
if (na == 1) {
handle<FPX> a_(pa->array[0]);
if (a_) {
pa = a_->get();
}
}
if (*op == 0 or *op == '+') {
for (unsigned i = 1; i < size(*pa); ++i) {
pa->array[i] = pa->array[i - 1] + pa->array[i];
}
}
if (*op == '*') {
for (unsigned i = 1; i < size(*pa); ++i) {
pa->array[i] = pa->array[i - 1] * pa->array[i];
}
}
return pa;
}
#ifdef _DEBUG
#endif // _DEBUG
#if 0
AddIn xai_array_drop(
Function(XLL_LPOPER, "xll_array_drop", "array.DROP")
.Arguments({
Arg(XLL_LPOPER, "array", "is a array or handle to a array."),
Arg(XLL_LONG, "count", "drop items from front (count > 0) or back (count < 0)."),
})
.FunctionHelp("Return array with dropped items removed.")
.Category(CATEGORY)
.Documentation(R"(
If array has one row/column then drop row/columns elements,
otherwise drop array rows.
)")
);
LPOPER WINAPI xll_array_drop(LPOPER parray, LONG count)
{
#pragma XLLEXPORT
static OPER o;
try {
o = ErrNA;
if (parray->is_num()) {
handle<OPER> h_(parray->val.num);
if (h_) {
parray = h_.ptr();
}
}
o = parray->drop(count);
}
catch (const std::exception& ex) {
XLL_ERROR(ex.what());
}
return &o;
}
AddIn xai_array_cumprod(
Function(XLL_LPOPER, "xll_array_cumprod", "array.CUMPROD")
.Arguments({
Arg(XLL_LPOPER, "array", "is a array."),
})
.Category(CATEGORY)
.FunctionHelp("Cumulative product of array columns")
.Documentation(R"()")
);
LPOPER WINAPI xll_array_cumprod(LPOPER parray)
{
#pragma XLLEXPORT
static OPER o;
try {
if (parray->is_num()) {
handle<OPER> h_(parray->val.num);
if (h_) {
parray = h_.ptr();
}
}
const OPER& array = *parray;
o.resize(array.rows(), array.columns());
for (unsigned j = 0; j < o.columns(); ++j) {
o(0, j) = array(0, j);
}
for (unsigned i = 1; i < o.rows(); ++i) {
for (unsigned j = 0; j < o.columns(); ++j) {
o(i, j) = o(i - 1, j).as_num() * array(i, j).as_num();
}
}
}
catch (const std::exception& ex) {
XLL_ERROR(ex.what());
o = ErrValue;
}
return &o;
}
#endif