Files
playbook/docs/tsl/codegen/builtin/base/array.md
T
csh b6160676b4 ♻️ refactor(tsl): rebuild codegen taxonomy
Move generated builtin and dotnet function docs into the upgraded taxonomy, remove legacy route pages, and regenerate function_index.tsv from markdown headings.
2026-07-07 16:36:08 +08:00

2506 lines
61 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Builtin - 基础 / 数组
## `getStrindexnum(value)`
返回字符串下标的个数
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `value` | array | 数组类型 |
返回:integer
### 示例
```tsl
arr := array("a1": 1, "a2": 2, 1: 3);
return getStrindexnum(arr);
// 输出:2
```
## `getstrindexs(value)`
返回数组类型,存储参数Value的所有的字符串下标
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `value` | array | 数组类型 |
返回:array of string
### 示例
```tsl
arr := array("a1": 1, "a2": 2, "a3": 3);
return getstrindexs(arr);
// 输出:array(“a1”,”a2”,”a3”)。
```
## `getintindexs(value)`
返回数组类型,存储数组Value的所有的数字下标
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `value` | array | 数组类型 |
返回:array of integer
### 示例
```tsl
arr := array(1: "p1", 2: "p2", 3: "p3", "a4": "p4");
return getintindexs(arr);
// 输出:array(1,2,3)
```
## `getAllIndexs(value)`
返回数组类型,存储数组Value的所有下标
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `value` | array | 数组类型 |
返回:array
### 示例
```tsl
arr := array("p1": 1, "p2": 2, "p3": 3);
return getAllIndexs(arr); // 输出:array(“p1”,”p2”,”p3”)
```
## `length(value)`
返回字符串、二进制流或者数组或者Matrix的行长度。
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `value` | string\|binary\|array\|matrix | 字符串、二进制流或数组或者Matrix类型 |
返回:integer
### 示例
```tsl
arr := array(("p1": 1, "p2": 2, "p3": 3), ("p1": 4, "p2": 5, "p3": 6));
return length(arr); // 输出:2
```
## `sortTableByField(value, field, sortdir)`
对一个表类型的数组Value用字段名Field排序,排序方向由sortdir决定,如果省略sortdir参数或者设置sortdir为1,正序排序,否则逆序排序。
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `value` | array | 二维数组; |
| `field` | string | 字符串,数组的列下标; |
| `sortdir` | bool | 布尔型,排序方向,1表示正序,0表示逆序。 |
返回:integer
### 示例
```tsl
arr := array(
("p1": 1, "p2": 2, "p3": 3),
("p1": 4, "p2": 5, "p3": 6),
("p1": 66, "p2": 200, "p3": 36),
("p1": 30, "p2": 500, "p3": 40)
);
sortTableByField(arr, "p1", 1); // sortdir=1按"p1"正序,sortdir=0反序
return arr;
// 输出:
// array(
// ("p1":1, "p2":2, "p3":3),
// ("p1":4, "p2":5, "p3":6),
// ("p1":30, "p2":500, "p3":40),
// ("p1":66, "p2":200, "p3":36))
```
## `filterIn(r, v, field, b_return_sub_result=true)`
返回指定列的值在过滤集内的子结果集或者下标列表。
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `r` | array | 待过滤的数据表或数组。 |
| `v` | array | 过滤集。 |
| `field` | string | 指定列名;为 `nil` 时按整行或一维数组元素匹配。 |
| `b_return_sub_result` | bool | 可选参数,默认 `true`。为 `true` 时返回子结果集,为 `false` 时返回行下标列表。 |
返回:array,tablearray
### 示例
范例 01:过滤表中指定列的值。
```tsl
a := array(
("a": 0, "b": 0, "c": 0),
("a": 1, "b": 0, "c": 2),
("a": 2, "b": 1, "c": 4),
("a": 3, "b": 1, "c": 6),
("a": 4, "b": 2, "c": 8),
("a": 5, "b": 2, "c": 10),
("a": 6, "b": 3, "c": 12)
);
return filterIn(a, array(1, 2, 5), "a");
// 输出:array(
// ("a":1, "b":0, "c":2),
// ("a":2, "b":1, "c":4),
// ("a":5, "b":2, "c":10)
// )
```
范例 02:按整行数据筛选子集。
```tsl
a := array();
for i := 0 to 15 do
begin
a[i]["a"] := i;
a[i]["b"] := i div 2;
a[i]["c"] := i * 2;
end;
return filterIn(a, array(("a": 2, "b": 1, "c": 4), ("a": 3, "b": 1, "c": 4)), nil);
// 输出:array(
// ("a":2, "b":1, "c":4)
// )
```
范例 03:按多列组合筛选子集。
```tsl
a := array();
for i := 0 to 15 do
begin
a[i]["a"] := i;
a[i]["b"] := i div 2;
a[i]["c"] := i * 2;
end;
return filterIn(a, array(("a": 2, "b": 1), ("a": 3, "b": 1)), array("a", "b"));
// 输出:array(
// ("a":2, "b":1, "c":4),
// ("a":3, "b":1, "c":6)
// )
```
范例 04:返回符合指定列数据的行下标。
```tsl
a := array();
for i := 0 to 15 do
begin
a[i]["a"] := i;
a[i]["b"] := i div 2;
a[i]["c"] := i * 2;
end;
return filterIn(a, array(("a": 2, "b": 1), ("a": 3, "b": 1)), array("a", "b"), false);
// 输出:array(2, 3)
```
范例 05:过滤一维数组。
```tsl
return filterIn(array(1, -5, 10, 2, 10, 14, -5), array(2, 3, 10), nil);
// 输出:array(10, 2, 10)
```
## `filterNotIn(r, v, field, b_return_sub_result=true)`
返回指定列的值不在过滤集内的子结果集或者下标列表。
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `r` | array | 待过滤的数据表或数组。 |
| `v` | array | 过滤集。 |
| `field` | string | 指定列名;为 `nil` 时按整行或一维数组元素匹配。 |
| `b_return_sub_result` | bool | 可选参数,默认 `true`。为 `true` 时返回子结果集,为 `false` 时返回行下标列表。 |
返回:array,tablearray
### 示例
范例 01:按指定列过滤数据表,并同时查看子结果集和行下标。
```tsl
a := array();
for i := 0 to 5 do
begin
a[i]["a"] := i;
a[i]["b"] := i div 2;
a[i]["c"] := i * 2;
end;
b := array(1, 3, 5);
c1 := filterNotIn(a, b, "a"); // 过滤结果
c2 := filterNotIn(a, b, "a", false); // 过滤仅返回下标
return array(c1, c2);
// 输出:
// array(
// (
// ("a":0, "b":0, "c":0),
// ("a":2, "b":1, "c":4),
// ("a":4, "b":2, "c":8)),
// (0, 2, 4))
```
范例 02:按整行数据过滤二维数组,保留重复行。
```tsl
t := array(
("a": 0, "b": 0, "c": 0),
("a": 1, "b": 0, "c": 2),
("a": 2, "b": 1, "c": 4),
("a": 2, "b": 1, "c": 4),
("a": 4, "b": 2, "c": 8),
("a": 4, "b": 2, "c": 8),
("a": 6, "b": 3, "c": 12)
);
return filterNotIn(t, array(("a": 2, "b": 1, "c": 4)), nil);
// 输出:
// array(
// ("a":0, "b":0, "c":0),
// ("a":1, "b":0, "c":2),
// ("a":4, "b":2, "c":8),
// ("a":4, "b":2, "c":8),
// ("a":6, "b":3, "c":12))
```
范例 03:过滤一维数组。
```tsl
return filterNotIn(array(1, -5, 10, 2, 10, 14, -5), array(2, 3, 10), nil);
// 输出:array(1, -5, 14, -5)
```
## `sortArray(value, sortdir)`
对一个一维数组进行排序,方向为sortdir;如果省略sortdir参数,默认为1,正序排序;设置为0时,逆序排序。
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `value` | array | 一维数组 |
| `sortdir` | bool | 布尔型,排序方向 |
返回:integer
### 示例
```tsl
arr := array(1, 6, 3);
sortArray(arr);
return arr;
// array(1,3,6)
```
## `reIndex(data, index_info1, index_info2, index_info_n)`
修改数组下标
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `data` | array | 需要重构的数组 |
| `index_info1` | nil\|array | 第一维的重构信息,如果为NIL则表示该维度不重构,数组以需要重构的下标为下标新下标为值的模式。 |
| `index_info2` | nil\|array | 第二维的重构信息,可省略 |
| `index_info_n` | nil\|array | 第N维的重构信息,可省略 |
返回:any
### 示例
```tsl
// 修改数字列名数组为字符串列名数组,即将0,1,2(自增数字)列名重构为"a","b","c"列名的数组
a := rand(10, 3);
s := array("a", "b", "c");
reIndex(a, nil, s);
return a;
// 修改数字行标数组为字符串行标数组,即将0,1,2(自增数字)行标重构为"a","b","c"行标的数组
a := rand(3, 3);
s := array("a", "b", "c");
reIndex(a, s);
return a;
// 通过设置行标为nil进行删除该行记录
a := array("A": 0, "B": 1, "D": 2, "E": 3);
reIndex(a, array("A": "A1", "D": nil));
return a;
// 返回:array("A1":0,"B":1,"E":3);
// 如果我们要对多维数组进行处理
a := rand(10, array("A", "B", "D", "E"); // 初始化一个四列的二维矩阵
reIndex(a, nil, array("A": "A1", "D": nil));
return a;
// 说明,第2个参数为nil表示对第一维的下标不进行变动,因此我们修改列"A"为"A1",并删除"D"列。
// 利用Reindex做列的交换
a := array(1, 2, 3, 4, 5);
reIndex(a, array(0: 4, 4: 0));
return a;
// 结果为array(5,2,3,4,1);
```
## `fieldNamesOfStrSubscript(t)`
字符串下标数组的字段名称。
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `t` | array | 整数 |
返回:any
### 示例
```tsl
s := `array('a': (1, 2, 3, 4, 5), 'b': (2, 3, 4, 5, 6), 'c': (3, 4, 5, 6, 7), 'd': (4, 5, 6, 7, 8));
return fieldNamesOfStrSubscript(s);
// 返回:
```
## `fieldCountOfStrSubscript(t)`
字符串下标数组的字段个数。
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `t` | array | 数据表类型 |
返回:integer
### 示例
```tsl
s := `array('a': (1, 2, 3, 4, 5), 'b': (2, 3, 4, 5, 6), 'c': (3, 4, 5, 6, 7), 'd': (4, 5, 6, 7, 8));
return fieldCountOfStrSubscript(s);
// 返回:4
```
## `argmax(x)`
1、返回一维数组中最大值所在下标
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `x` | array,tablearray | 一维数字数组 |
返回:integer
### 示例
```tsl
x := array(9, 8, 10);
return argmax(x); // 2
```
## `argmin(x)`
1、返回一维数组中最小值所在下标
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `x` | array,tablearray | 一维数字数组 |
返回:integer
### 示例
```tsl
x := array(3, 1, 9, 2);
return argmin(x); // 1
```
## `sortTableByFieldExt(t, sort_field)`
将数组t依据字段SortField排序。把大于0的按照从小到大排序,把小于0的按照绝对值从小到大排序,然后,把两个结果合并。
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `t` | array | 数据表类型,数组 |
| `sort_field` | string | 字符串,排序字段 |
返回:any
### 示例
```tsl
s := `array('a': (1, 2, 3, 4, 5), 'b': (-2, -3, -4, 5, 6), 'c': (3, 4, 5, 6, 7), 'd': (4, 5, 6, 7, 8));
return sortTableByFieldExt(s, 'b');
// 返回:
```
## `rankValue(t, fields)`
按照指定字段求Rank,返回的数据,保存在传递过来的数据t中。返回按Fields列排序的结果,
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `t` | array | 数据表类型,数据表 |
| `fields` | string | 字符串,排序的列 |
返回:any
### 示例
```tsl
// 取上证50的成分股,获得涨幅之后,对涨幅进行排名
stock_arr := getBkByDate('SH000016', 20190201T);
s := select ['StockID'] as '代码',
["StockName"] as "名称",
['price'] as '收盘',
stockZf(['date'], ['date']) as '涨幅(%)'
from markettable datekey 20190201T to 20190201T of stockArr end;
rankValue(s, '涨幅(%)');
return s;
// 输出:
// Execute script error at Line:2
// function:__main__:line 2:instruction:usercall: function:getBkByDate compile error or not found
// function:__main__:line 2:instruction:usercall: function execute error
```
## `argsort(value, sortdir)`
返回可以用于排序的一维数组下标
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `value` | array | 一维数字数组,数组 |
| `sortdir` | integer | 整数,排序方向(1:正序【默认】;0:逆序) |
返回:array
### 示例
```tsl
value := array(4, 8, 2, 3, 9);
return argsort(value); // 返回:array(2,3,0,1,4)
```
## `rankIt(t, f_n, sort_order)`
按指定字段的值排名;SortOrder0 降序排名;SortOrder1升序排名
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `t` | array | 数据表 |
| `f_n` | string | 字段名 |
| `sort_order` | integer | 排名方向,0 降序排名, 1升序排名 |
返回:array
### 示例
```tsl
t2 := array(('a': 2, 'c': 13), ('a': 3, 'c': 33));
rankIt(t2, 'c', 1);
return t2;
// 输出:
// Execute script error at Line:2
// function:__main__:line 2:instruction:usercall: function:rankIt compile error or not found
// function:__main__:line 2:instruction:usercall: function execute error
```
## `rankIt2(t, f_n, of_n, sort_order)`
按指定字段的值排名,SortOrder:0 降序排名;SortOrder:1 升序排名,排名列的字段为:OfN
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `t` | array | 数据表 |
| `f_n` | string | 指定字段,字符串 |
| `of_n` | string | 排名列字段名,字符串 |
| `sort_order` | integer | 排名方向:0 降序排名,1 升序排名 |
返回:array
### 示例
```tsl
t2 := array(('a': 2, 'c': 13), ('a': 3, 'c': 33));
rankIt2(t2, 'c', '名次', 1);
return t2;
// 输出:
// 示例包含说明文字,未作为可执行片段运行。
```
## `rpsIt(t, f_n, sort_order)`
按指定字段的值排名,获得其排名的百分位,排序百分位放在列 FN+"\_RPS"中;
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `t` | array | 数组,数据表 |
| `f_n` | string | 字符串/数字,字段名 |
| `sort_order` | integer | 排名方向,0 降序排名, 1升序排名 |
返回:array
### 示例
```tsl
// 获得沪深300的成分股,并计算EndT的涨幅,对涨幅排序后返回其排序百分比。
endt := intToDate(20140401);
setSysParam(PN_Date(), end_t);
stock_arr := getBkByDate('SH000300', end_t);
r := array();
n := 0;
for n_i := 0 to length(stock_arr)-1 do
begin
stock_id := stock_arr[n_i];
setSysParam(pn_stock(), stock_id);
if not isTradeDay(end_t) then continue;
r[n]['StockID'] := stock_id;
r[n]['StockName'] := stockName(stock_id);
r[n]['StockZF'] := stockZf3();
n++;
end;
return rpsIt(r, 'StockZF', 1);
// 部分结果截图:
```
## `deleteNilValueByField(t, fields)`
删除指定字段fields数据为NIL的记录。
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `t` | array | 数据表类型,数据 |
| `fields` | string | 字符串,字段名称,多个字段用分号(;)连接,例:"Field1;Field2 |
返回:array
### 示例
```tsl
tdata := array(array('time': 1, 'price': 7.78), array('time': 2));
deleteNilValueByField(tdata, 'price');
return tdata;
// 返回:
```
## `inserttablebyrno(t, index, rdata)`
向表格指定位置插入行,天软表格行下标从0开始
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `t` | array | Array,数组,支持一维,二维数组,只支持下标为自增长的序列数组 |
| `index` | integer | Integer,插入行的起始下标,下标位置从0开始 |
| `rdata` | any | Any,插入的数据,支持一个值、一维表及二维表 |
返回:array
### 示例
范例1
```tsl
// 向一维数组指定位置插入一个字符串’aaa’
t := 43456->43460;
index := 2;
r_data := 'aaa';
return inserttablebyrno(t, index, r_data);
// 结果:array(43456,43457,"aaa",43458,43459,43460)
```
范例2
```tsl
// 向一维数组第3行插入多行的数据
t := 44000->44004;
index := 3;
r_data := array('a', 'b', 'c');
return inserttablebyrno(t, index, r_data);
// 结果:
array(44000, 44001, 44002, "a", "b", "c", 44003, 44004)
```
范例3
```tsl
// 向二维数组第3行插入多行的数据
t := array(('a': 10, 'b': 'i', 'c': dateToStr(20200319T)),
('a': 50, 'b': 'i', 'c': dateToStr(20200318T)),
('a': 60, 'b': 'i', 'c': dateToStr(20200317T)),
('a': 40, 'b': 'o', 'c': dateToStr(20200316T)),
('a': 20, 'b': 'o', 'c': dateToStr(20200313T)));
index := 3;
r_data := array(('c': 0, 'b': 44000, 'a': '2020-03-20'));
return inserttablebyrno(t, index, r_data);
// 输出:
// Execute script error at Line:9
// function:__main__:line 9:instruction:usercall: function:inserttablebyrno compile error or not found
// function:__main__:line 9:instruction:usercall: function execute error
```
范例4
```tsl
// 向二维数组第1行插入多行的数据
t := array(('a': 10, 'b': 'i', 'c': dateToStr(20200319T)),
('a': 50, 'b': 'i', 'c': dateToStr(20200318T)),
('a': 60, 'b': 'i', 'c': dateToStr(20200317T)),
('a': 40, 'b': 'o', 'c': dateToStr(20200316T)),
('a': 20, 'b': 'o', 'c': dateToStr(20200313T)));
index := 1;
r_data := array(('c': '2020', 'b': 'AAA', 'a': 44456),
('c': '2020', 'b': 'BBB', 'a': 44566));
return inserttablebyrno(t, index, r_data);
// 输出:
// Execute script error at Line:10
// function:__main__:line 10:instruction:usercall: function:inserttablebyrno compile error or not found
// function:__main__:line 10:instruction:usercall: function execute error
```
## `arrayextend(a, ind, v)`
数组-指定位置插入子集
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `a` | array of number | Array of number,一维数字数组 |
| `ind` | integer | Integer,插入的起始位置 |
| `v` | array of number | Array of number,插入的子数组 |
返回:array
### 示例
```tsl
a := letters(1->6);
return arrayextend(a, 2, array('d', 'e'));
// 返回:array("A", "B", "d", "e", "C", "D", "E", "F")
```
## `arrayinsert(a, ind, v)`
数组-指定位置插入值
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `a` | array of number | Array of number,一维数字数组 |
| `ind` | integer | Integer,插入位置 |
| `v` | any | Any,元素值 |
返回:array
### 示例
```tsl
a := letters(1->6);
return arrayinsert(a, 2, 3);
// 返回:array("A", "B", 3, "C", "D", "E", "F")
```
## `frameinsert(data, loc, fname, v)`
数据表-指定位置插入列
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `data` | table | Table,二维数据表,源数据 |
| `loc` | integer | Integer,插入位置 |
| `fname` | string | String,插入索引命名 |
| `v` | integer | Integer,插入值 |
返回:array
### 示例
```tsl
data := load_dataset_iris();
return frameinsert(data, 1, '新列', 0);
```
## `searchsorted(v, x)`
有序列表插入指定值的位置,原数组必须为有序表且元素应可比较。
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `v` | array | 一维数组 有序数组 |
| `x` | string \| real | 插入的值 |
返回:integer
### 示例
```tsl
// 数字插入
v := array(1, 2, 3, 4, 5, 6);
return searchsorted(v, 3.5); // 返回:3
// 字符串插入
v := array("SH600001", "SH600004", "SH600050", "SH600305");
return searchsorted(v, "SH600049"); // 返回:2
```
## `argconfirst(a, v, _type)`
第一次满足条件的位置, 原数组必须为有序表且元素应可比较。
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `a` | array | 有序数组 |
| `v` | string\|real\|array | 对比值 |
| `_type` | integer | 对比条件 |
返回:integer
### 示例
```tsl
// 实数对比
a := 0->9;
return argconfirst(a, 3.5, 1); // 返回:4
// 数组对比
a := array(("a", "A"), ("b", "B"), ("c", "C"), ("d", "D"));
v := array("b", "B");
return argconfirst(a, v, 6); // 返回:1
```
## `stockAmount(t, date, yes_value)`
从数据表t中获取符合ChangeField大于或等于Date的数据。
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `t` | array | 数据表类型,需要比较获取的数据表 |
| `date` | integer | 实数,或者日期 |
| `yes_value` | integer | 布尔值,决定返回Date当天的数据还是之前的数据; |
返回:any
### 示例
```tsl
// 平安银行2011年6月30日流通A股发生变动
setSysParam(PN_Stock(), 'SZ000001');
t := infoArray(16); // 股票.股本结构表
r1 := stockAmount(t, 20110630, 0, '变动日', '流通A股'); // Date当天
r2 := stockAmount(t, 20110630, 1, '变动日', '流通A股'); // Date之前
return array(r1, r2);
// 结果:array(3105358672.0,3105358511.0)
```
## `seperateTable(t, sep_field, sep_count)`
把表格t分割为SepCount个表格。
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `t` | array | 数据表类型,原始数组 |
| `sep_field` | string | 字符串,排序字段 |
| `sep_count` | integer | 整数,分割数 |
返回:array
### 示例
```tsl
t := `array('a': (1, 2, 3, 4), 'b': (2, 3, 4, 5), 'c': (3, 4, 5, 6), 'd': (4, 5, 6, 7), 'e': (5, 6, 7, 8), 'f': (7, 8, 9, 10));
return seperateTable(t, 'a', 2);
// 返回:
```
## `unitary(t, key_fields, _type, f_value)`
归一处理。对数组t指定列KeyFields进行归一处理,归一方式由Type决定。
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `t` | array,tablearray | |
| `key_fields` | string | |
| `_type` | integer | |
| `f_value` | integer | |
返回:array,tablearray
### 示例
```tsl
t := `array('a': (1, 2, 3, 4), 'b': (2, 3, 4, 5), 'c': (3, 4, 5, 6), 'd': (4, 5, 6, 7), 'e': (5, 6, 7, 8), 'f': (7, 8, 9, 10));
unitary(t, 'b', 0, 10);
return t;
// 返回:
```
## `tableVectorizeArray(t, s, opt, is_func)`
数据表与一维数据表的矢量化运算。
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `t` | array | 数据表 |
| `s` | array\|real | 一维数组 或实数, 运算标量 |
| `opt` | string | 字符串,操作运算符,默认"-",表逐行减法 |
| `is_func` | bool | 布尔,opt是否是函数,默认假 |
返回:array
### 示例
```tsl
t := ones(10, 3);
s := array(1, 2, 3);
return tableVectorizeArray(t, s, '-');
// 输出:
// Execute script error at Line:3
// function:__main__:line 3:instruction:usercall: function:tableVectorizeArray compile error or not found
// function:__main__:line 3:instruction:usercall: function execute error
```
## `reverse(arr)`
数组倒置反向。
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `arr` | array | 数组 |
返回:array
### 示例
```tsl
// 倒置数组
setSysParam(pn_stock(), "SZ000002");
setSysParam(pn_date(), 20210601t);
arr := nDay(10, "date", dateToStr(sp_time()), "close", close());
return reverse(arr);
// 输出:
// Execute script error at Line:5
// function:__main__:line 5:instruction:usercall: function:reverse compile error or not found
// function:__main__:line 5:instruction:usercall: function execute error
```
## `descartesJoin(v1, v2, v_n)`
数组笛卡尔积连接。
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `v1` | | 任意类型,支持不定个数参数,数值1 |
| `v2` | | 任意类型,数值2 |
| `v_n` | | 任意类型,数值3 |
返回:array,tablearray
### 示例
```tsl
return descartesJoin(1->3, 21->22, 3);
// 输出:
// Execute script error at Line:1
// function:__main__:line 1:instruction:->: function:descartesJoin compile error or not found
// function:__main__:line 1:instruction:->: function execute error
```
## `abnormalData(y, flag, ystore)`
数组中出现奇异值的处理。其中y为数据表,Flag为处理方法,Ystore为选择中位数和3倍标准差法返回被处理数据的数据。
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `y` | array | 一维数字数组 |
| `flag` | string | 用户自定义,奇异值处理方法 |
| `ystore` | array | 数据表类型,存放修改的数据,包括修改的行、原数值、处理后的值 |
返回:any
### 示例
范例01:返回奇异值的位置及处理后的结果。
```tsl
t := array(1, 100, 101, 102, 103, 1000);
abnormalData(t, "median", r);
return r;
// 返回:
// 其中,0和5为修改的行,1和1000是对应的原值,93.7和109.3是修改后的值
```
范例02:对源数据列增加一列奇异值处理后的结果
```tsl
col := 'SH000001';
setSysParam(pn_stock(), col);
setSysParam(pn_date(), 20240930T);
setSysParam(pn_cycle(), cy_day());
days := 100;
// SH000001在20240930最近100天的行情数据中设置三个奇异值
t := nDay(days, '日期', dateToStr(sp_time()), col, close());
t[29, col] := 1500;
t[59, col] := 4500;
t[79, col] := 6000;
// 找到数组指定列奇异值,以中位数法为例
tyc := abnormalData(t[: , col], "median", abn);
t[: , col$"_奇异值处理"] := tyc;
return t;
// 输出:增加列"SH000001_奇异值处理"
```
## `zValue(t, column_name)`
按照指定字段求Z值。
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `t` | array | 数据表类型,数据表 |
| `column_name` | string | 字符串,要计算Rank的字段 |
返回:any
### 示例
```tsl
stock_arr := getBkByDate('SH000016', 20190201T);
s := select ['StockID'] as '代码',
["StockName"] as "名称",
['price'] as '收盘',
stockZf(['date'], ['date']) as '涨幅(%)'
from markettable datekey 20190201T to 20190201T of stockArr end;
zValue(s, '涨幅(%)');
return s
// 返回:
```
## `rpsValue(t, column_name)`
按照指定字段求RPS。
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `t` | array | 数据表类型,数据表 |
| `column_name` | string | 字符串,要计算RPS的字段 |
返回:any
### 示例
```tsl
stock_arr := getBkByDate('SH000016', 20190201T);
s := select ['StockID'] as '代码',
["StockName"] as "名称",
['price'] as '收盘',
stockZf(['date'], ['date']) as '涨幅(%)'
from markettable datekey 20190201T to 20190201T of stockArr end;
rpsValue(s, '涨幅(%)');
return s;
// 返回:
```
## `webFormatToRung(a)`
将数组Nan/Nil/±Inf替换为"-"
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `a` | array | 数组,待替换数组,支持多维数组,高维嵌套数组 |
返回:any
### 示例
```tsl
a := array('a': ('b': ('c': (nil, nan, 3), 'd': 2), 'e': nan));
return webFormatToRung(a); // array("a":("b":("c":("-","-",3),"d":2),"e":"-"))
```
## `factorize(value, ifsort, nan_fill_value, uniques)`
分类变量数值映射,将一个一维分类变量数组映射为给分类变量递增编号后的数值数组
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `value` | array | 数组,一维数组 |
| `ifsort` | bool | 布尔型,是否排序,默认不排序 |
| `nan_fill_value` | any | 数字/字符串,填充nan的值,默认为-1 |
| `uniques` | array | 数组,输出,各值对应的原值 |
返回:array
### 示例
```tsl
value := Array('a', 'd', 'c', 'a', 'b');
labels := factorize(value, true, -1, uniques);
echo uniques; // array("a","b","c","d")
return labels; // array(0,3,2,0,1);
```
## `sparseMatFill(a, fillv)`
稀疏数据表填充,直接在A矩阵上更改
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `a` | array,tablearray | 数据表类型,稀疏矩阵 |
| `fillv` | real | 实数,给定的填充值 |
返回:array,tablearray
### 示例
```tsl
a := array((3: 4), ('A': 2));
sparseMatFill(a, -1);
return a; // array((3:4,"A":-1),(3:-1,"A":2))
```
## `elementWhere(con, x, y)`
根据布尔数组con,为True时从X中,为False时从Y中,取对应位置元素的值
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `con` | array,tablearray | |
| `x` | array,tablearray | |
| `y` | array,tablearray | |
返回:array,tablearray
### 示例
```tsl
con := array(1, 0, 1, 1, 1, 0);
x := array(1, 2, 3, 4, 5, 6);
y := array(11, 12, 13, 14, 15, 16);
return elementWhere(con, x, y); // array(1,12,3,4,5,16)
```
## `arrayAddGap(t, key, v, m)`
数组添加分割线,只支持字符串下标
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `t` | array | 数组,待添加数组,直接在原数组更改 |
| `key` | string | 字符串,间隔名,默认无 |
| `v` | string | 字符串,间隔串,默认为 DupeString('-',m\*2) |
| `m` | integer | 整型,间隔串的长度设置,默认 5 |
返回:array
### 示例
```tsl
a := array('alpha(%)': 3);
arrayAddGap(a, '风险');
a['beta'] := 3;
return a; // array("alpha(%)":3,"-----风险-----":"----------","beta":3)
```
## `groupPercentrankIt(t, g_name, v_name)`
计算分组百分位,对数据表t按[GName]分组后,对每组的[Vname_]计算百分位
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `t` | array | 数组,待计算的数组 |
| `g_name` | string | 字符串,分组字段名 |
| `v_name` | string | 字符串,转换字段名 |
返回:array
### 示例
```tsl
t := array(("G": 1, "V1": 1, "V2": 11, "V3": 21),
("G": 0, "V1": 2, "V2": 12, "V3": 22),
("G": 1, "V1": 3, "V2": 13, "V3": 23),
("G": 0, "V1": 4, "V2": 14, "V3": 24),
("G": 1, "V1": 5, "V2": 15, "V3": 25),
("G": 0, "V1": 6, "V2": 16, "V3": 26));
return groupPercentrankIt(t, 'G', 'V1'); // array(0.0,0.0,0.5,0.5,1.0,1.0)
```
## `groupRankIt(t, g_name, v_name, sort_order)`
计算分组排名,对数据表t按[GName]分组后,对每组的[Vname_]计算排名
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `t` | array | 数组,待计算的数组 |
| `g_name` | string | 字符串,分组字段名 |
| `v_name` | string | 字符串,转换字段名 |
| `sort_order` | bool | 布尔型,排名方向 |
返回:array
### 示例
```tsl
t := array(
("G": 1, "V1": 1, "V2": 11, "V3": 21),
("G": 0, "V1": 2, "V2": 12, "V3": 22),
("G": 1, "V1": 3, "V2": 13, "V3": 23),
("G": 0, "V1": 4, "V2": 14, "V3": 24),
("G": 1, "V1": 5, "V2": 15, "V3": 25),
("G": 0, "V1": 6, "V2": 16, "V3": 26));
return groupRankIt(t, 'G', 'V1'); // array(1,1,2,2,3,3)
```
## `groupTransformByFunc(t, g_name, v_name, fp)`
计算分组排名,对数据表t按[GName]分组后,对每组的[Vname_]根据给定函数进行分组转换操作
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `t` | array | 数组,待计算的数组 |
| `g_name` | string | 字符串,分组字段名 |
| `v_name` | string | 字符串或数组,转换字段名 |
| `fp` | string | 字符串(函数名)或函数指针,转换函数必须两个参数,分别表示当前的聚合字段与排名字段,以分组排名为例: |
返回:array
### 示例
```tsl
t := array(("G": 1, "V1": 1, "V2": 11, "V3": 21),
("G": 0, "V1": 2, "V2": 12, "V3": 22),
("G": 1, "V1": 3, "V2": 13, "V3": 23),
("G": 0, "V1": 4, "V2": 14, "V3": 24),
("G": 1, "V1": 5, "V2": 15, "V3": 25),
("G": 0, "V1": 6, "V2": 16, "V3": 26));
fp := function (v, g);
begin
return rankof([v], [v]);
end
return groupTransformByFunc(t, 'G', 'V1', fp); // array(3,3,2,2,1,1)
```
## `frameGetSubByReg(data, reg)`
通过正则表达式匹配获取数据框子集
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `data` | array | 数组,待匹配的数组 |
| `reg` | string | 字符串,间隔名,正则匹配串 |
返回:array
### 示例
```tsl
t := array((13: 1, "V1": 1, "V2": 11, 12: 21, 'R1w': 1),
(13: 0, "V1": 2, "V2": 12, 12: 22, 'R1w': 1),
(13: 1, "V1": 3, "V2": 13, 12: 23, 'R1w': 1),
(13: 0, "V1": 4, "V2": 14, 12: 24, 'R1w': 1),
(13: 1, "V1": 5, "V2": 15, 12: 25, 'R1w': 1),
(13: 0, "V1": 6, "V2": 16, 12: 26, 'R1w': 1));
return frameGetSubByReg(t, '1');
// 结果:
array(
(21, 1, "V1": 1, "R1w": 1),
(22, 0, "V1": 2, "R1w": 1),
(23, 1, "V1": 3, "R1w": 1),
(24, 0, "V1": 4, "R1w": 1),
(25, 1, "V1": 5, "R1w": 1),
(26, 0, "V1": 6, "R1w": 1))
```
## `histogram(t, bins, format)`
统计data的频数分布表
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `t` | array,tablearray | 数字数组,待计算的数组 |
| `bins` | array,tablearray | 数组,表分组间隔 |
| `format` | string | 字符串,展示区间的格式串,见FormatFloat |
返回:array,tablearray
### 示例
范例一:
```tsl
return histogram(Randn(0, 1, 1000), array(-3, 0.5)->3, '0.00');
// 输出:
// Execute script error at Line:1
// function:__main__:line 1:instruction:usercall: function:Randn compile error or not found
// function:__main__:line 1:instruction:usercall: function execute error
```
范例二:
```tsl
data := randn(0, 1, 1000, array('a', 'b'));
return histogram(data, array(-3, 0.5)->3, '0.00');
// 输出:
// Execute script error at Line:1
// function:__main__:line 1:instruction:array: function:randn compile error or not found
// function:__main__:line 1:instruction:array: function execute error
```
## `framemerge(left, right, ons, how)`
数据表-联结
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `left` | table | Table,左表 |
| `right` | table | Table,右表 |
| `ons` | string | String,联结字段,可以为数组 |
| `how` | string | String,: 联结类型 |
返回:array
### 示例
范例1
```tsl
left := `array('a': 3->5, 'b': rand(3));
right := `array('a': 1->4, 'b': rand(4), 'c': rand(4) * 10);
return framemerge(left, right, 'a');
```
范例2
```tsl
// 多字段merge
left := `array('a': 3->5, 'b': rand(3));
left[: , 'c'] := left[: , 'a'];
right := `array('a': 1->4, 'b': rand(4), 'c': rand(4) * 10);
right[: , 'c'] := right[: , 'a'];
names := array('a', 'c');
return framemerge(left, right, name, 'lfet', nil, true);
```
范例3
```tsl
// 只更名right
left := `array('a': 3->5, 'b': rand(3));
right := `array('a': 1->4, 'b': rand(4), 'c': rand(4) * 10);
return framemerge(left, right, 'a', 'full', array(nil, '_x'));
```
## `frameqcut(data, q)`
数据表-按频率分组(等频分组)
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `data` | table | Table,数据表或一维数字数组,数据表时,表示对 |
| `q` | integer | Integer,分组数,等分q组,正整数,默认q=5 |
返回:array
## `frameupdate(left, right, ons, _type, _type)`
数据表-更新
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `left` | table | Table,待更新数据,直接更改 |
| `right` | table | Table,右表 |
| `ons` | string | String,键字段,默认为nil,表以下标进行联结。 |
| `_type` | integer | Integer,缺失类型,详见MF_isMissValue |
| `_type` | integer | 为以下数值的组合(以下数值的\_XOR或者相加) |
返回:array
### 示例
```tsl
left := `array('a': 3->5, 'b': rand(3));
right := `array('a': 1->4, 'b': rand(4), 'c': rand(4) * 10);
return frameupdate(left, right, 'a');
```
## `groupZscoreIt(t, g_name, v_name)`
1.计算分组标准化,对数据表t按[GName]分组后,对每组的[Vname_]进行标准化(原值-分组平均)/分组标准差
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `t` | array | 数组,待计算的数组 |
| `g_name` | string | 字符串,分组字段名 |
| `v_name` | string | 字符串,转换字段名。如果Vname*为nil则表示除GName之外的所有字段,如果Vname*为一个数组,则表示需要计算的字段组返回:二维数组, |
返回:array
### 示例
```tsl
t := array(
("G": 1, "V1": 1, "V2": 11, "V3": 21),
("G": 0, "V1": 2, "V2": 12, "V3": 22),
("G": 1, "V1": 3, "V2": 13, "V3": 23),
("G": 0, "V1": 4, "V2": 14, "V3": 24),
("G": 1, "V1": 5, "V2": 15, "V3": 25),
("G": 0, "V1": 6, "V2": 16, "V3": 26));
return groupZscoreIt(t, 'G', 'V1');? // array(-1.0,-1.0,0.0,0.0,1.0,1.0)
```
## `framecut(a, bins, option, o_lables)`
从小到大分段分组,最小的为第0组
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `a` | array | 数据表或一维数字数组,数据表时,表示对每列分别操作 |
| `bins` | array | 一维数字数组,分段节点,例如array(0,1)将数组a分为[-inf,0)[0,1)[1,inf)三段, |
| `option` | integer | 整型,区间开/闭,默认为0 |
| `o_lables` | integer | 数组,输出分组区间说明 |
返回:array
### 示例
范例1
```tsl
a := array(0, 59, 60, 70, 80, 99, 100);
bins := array(60, 80);
return framecut(a, bins, 0);
// array(0,0,1,1,2,2,2)
```
范例2
```tsl
a := array(0, 59, 60, 70, 80, 99, 100);
bins := 5;
return framecut(a, bins, 0);
// array(0,2,3,3,4,4,4)
```
## `frameMelt(t, id_names, value_names, variable_sname, value_sname)`
二维表——>立体表
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `t` | array | 二维字符串数组,数据表 |
| `id_names` | array | 一维字符串数组,键字段 |
| `value_names` | array | 一维字符串数组,值字段。为nil或者空时,表每行除开键字段外都是值字段 |
| `variable_sname` | string | 字符串,变量展示名 |
| `value_sname` | string | 字符串,值展示名 |
返回:array
### 示例
```tsl
t := array(
('截止日': 20210101, 'SH000300': 1, 'SH000301': 3),
('截止日': 20210102, 'SH000300': 9, 'SH000301': 2),
);
id_names := array('截止日');
return frameMelt(t, '截止日', nil, '代码', '得分');
// 输出:
// 示例包含说明文字,未作为可执行片段运行。
```
## `groupDemeanIt(t, g_name, v_name)`
1、分组去均值,对数据表t按[GName]分组后,对每组的[VName]去均值
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `t` | dbdata | 数据表,待分组去均值数据 |
| `g_name` | string | 字符串,t的分组字段名 |
| `v_name` | string | 字符串,去均值字段名 |
返回:array
### 示例
范例1
```tsl
t := array(
("G": 1, "V1": 1, "V2": 11, "V3": 21),
("G": 0, "V1": 2, "V2": 12, "V3": 22),
("G": 1, "V1": 3, "V2": 13, "V3": 23),
("G": 0, "V1": 4, "V2": 14, "V3": 24),
("G": 1, "V1": 5, "V2": 15, "V3": 25),
("G": 0, "V1": 6, "V2": 16, "V3": 26));
return groupDemeanIt(t, 'G', 'V1'); // array(-2.0,-2.0,0.0,0.0,2.0,2.0)
```
范例2
```tsl
t := array(
("G": 1, "V1": 1, "V2": 11, "V3": 21),
("G": 0, "V1": 2, "V2": 12, "V3": 22),
("G": 1, "V1": 3, "V2": 13, "V3": 23),
("G": 0, "V1": 4, "V2": 14, "V3": 24),
("G": 1, "V1": 5, "V2": 15, "V3": 25),
("G": 0, "V1": 6, "V2": 16, "V3": 26));
return groupDemeanIt(t, 'G', array('V1', 'V2'));
// 输出:
// Execute script error at Line:8
// function:__main__:line 8:instruction:array: function:groupDemeanIt compile error or not found
// function:__main__:line 8:instruction:array: function execute error
```
## `arrDropDuplicate(data)`
数组去重(包含NAN)。
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `data` | array | 数据表类型。数据表。 |
返回:array
### 示例
```tsl
data := array(("A": 1, "B": nan), ("A": 1, "B": 2), ("A": 1, "B": nan), ("A": 1, "B": 2));
return arrDropDuplicate(data);
// 结果:array(("A":1,"B":NAN),("A":1,"B":2))
```
## `getSubTableByField(t)`
获取表t中的Fields 列的数据。该函数功能可以用子矩阵的方式实现:t[:,array(fn1,fn2...)]
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `t` | array | 数据表类型,数据表 |
返回:any
### 示例
```tsl
setSysParam(pn_stock(), 'SZ000001');
r := nDay(10, 'time', dateToStr(sp_time()), 'close', close(), 'open', open(), 'high', high());
return getSubTableByField(r, 'time;close');
// 功能相当于使用子矩阵取数
return r[: , array('time', 'close')];
// 返回:
```
## `arraygetdefault(r, k, v)`
带默认值的数组取值,取指定下标的值,若存在则返回,否则返回设定的默认值
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `r` | array of string | Array of string, |
| `k` | string | String,下标 |
| `v` | string | String,默认值 |
返回:any
## `arraysetdefault(r, k, v)`
带默认值的数组赋值(字段未使用则赋值)
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `r` | array of string | Array of string, |
| `k` | string | String,下标 |
| `v` | string | String,默认值 |
返回:bool
## `arraysetdefault2(r, k, v)`
带默认值的数组赋值(值为nil则赋值)
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `r` | array of string | Array of string, |
| `k` | string | String,下标 |
| `v` | string | String,默认值 |
返回:bool
## `randomchoice(avalues, size, replace)`
随机抽样
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `avalues` | array of string | Array of string,一维字符串数组,或正整数 |
| `size` | integer | Integer, |
| `replace` | bool | Bool,是否可放回,默认为真可放回 |
返回:any
## `filterIntersectionPart(t1, t2, f_name, method)`
求两个数组在指定列名上的差集、交集、并集
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `t1` | array | 数据表 |
| `t2` | array | 数据表 |
| `f_name` | string | 指定列名字符串 |
| `method` | string | 方法,’IN’交集,’NOT IN’差集,’ALL’并集,字符串 |
返回:array
### 示例
```tsl
t1 := array(('a': 1, 'b': 10, 'c': 13), ('a': 2, 'b': 17, 'c': 13));
t2 := array(('a': 2, 'b': 22, 'c': 23), ('a': 1, 'b': 14, 'c': 13), ('a': 3, 'b': 12, 'c': 13));
return filterIntersectionPart(t1, t2, 'a', 'IN'); // t1 与t2的交集
// 输出:
// 示例包含说明文字,未作为可执行片段运行。
```
## `filterIntersectionPart2(t1, t2, method)`
求两个数组的差集、交集、并集。只适用一维数组。
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `t1` | array | 一维数据表 |
| `t2` | array | 一维数据表 |
| `method` | string | 方法,’IN’交集,’NOT IN’差集,’ALL’并集,字符串 |
返回:array
### 示例
```tsl
t1 := array(1, 2, 3, 4);
t2 := array(3, 4, 5, 6);
return filterIntersectionPart2(t1, t2, 'IN'); // t1 与t2的交集
// 输出:
// 示例包含说明文字,未作为可执行片段运行。
```
## `averageByColumn(t, f_name)`
求指定列的平均值
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `t` | array | 数据表 |
| `f_name` | string | 指定列名 |
返回:real
### 示例
```tsl
t1 := array(('a': 1, 'b': 4, 'c': 9), ('a': 7, 'b': 8, 'c': 5));
return averageByColumn(t1, 'b');
// 结果:6
```
## `harMeanByColumn(t, f_name)`
指定列的调和平均值
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `t` | array | 数据表 |
| `f_name` | string | 指定列名 |
返回:real
### 示例
```tsl
t1 := array(('a': 1, 'b': 4, 'c': 9), ('a': 7, 'b': 5, 'c': 5));
return harMeanByColumn(t1, 'b');
// 结果:4.44
```
## `geoMeanByColumn(t, f_name)`
指定列的几何平均值
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `t` | array | 数据表 |
| `f_name` | string | 指定列名 |
返回:real
### 示例
```tsl
t1 := array(('a': 1, 'b': 4, 'c': 9), ('a': 7, 'b': 5, 'c': 5));
return geoMeanByColumn(t1, 'b');
// 结果:4.47
```
## `devsqByColumn(t, f_name)`
指定列平均值偏差的平方和
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `t` | array | 数据表 |
| `f_name` | string | 指定列名 |
返回:real
### 示例
```tsl
t1 := array(('a': 1, 'b': 4, 'c': 9), ('a': 7, 'b': 5, 'c': 5));
return devsqByColumn(t1, 'b');
// 结果:0.5
```
## `kurtByColumn(t, f_name)`
指定列的峰度
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `t` | array | 数据表 |
| `f_name` | string | 指定列名 |
返回:real
### 示例
```tsl
t2 := array(('a': 2, 'c': 13), ('a': 3, 'c': 33), ('a': 3, 'c': 43), ('a': 5, 'c': 53));
return kurtByColumn(t2, 'c');
// 结果:0.34
```
## `medianByColumn(t, f_name)`
指定列的中位数
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `t` | array | 数据表 |
| `f_name` | string | 指定列名,字符串 |
返回:real
### 示例
```tsl
t2 := array(('a': 2, 'c': 13), ('a': 3, 'c': 33), ('a': 3, 'c': 43), ('a': 5, 'c': 53));
return medianByColumn(t2, 'c');
// 结果:38
```
## `skewByColumn(t, f_name)`
指定列的偏度
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `t` | array | 数据表 |
| `f_name` | string | 指定列名,字符串 |
返回:real
### 示例
```tsl
t2 := array(('a': 2, 'c': 13), ('a': 3, 'c': 33), ('a': 3, 'c': 43), ('a': 5, 'c': 53));
return skewByColumn(t2, 'c');
// 结果:-0.75
```
## `stdevByColumn(t, f_name)`
指定列的标准差
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `t` | array | 数据表 |
| `f_name` | string | 指定列名,字符串 |
返回:real
### 示例
```tsl
t2 := array(('a': 2, 'c': 13), ('a': 3, 'c': 33), ('a': 3, 'c': 43), ('a': 5, 'c': 53));
return stdevByColumn(t2, 'c');
// 结果:17.08
```
## `varByColumn(t, f_name)`
指定列的方差
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `t` | array | 数据表 |
| `f_name` | string | 指定列名,字符串 |
返回:real
### 示例
```tsl
t2 := array(('a': 2, 'c': 13), ('a': 3, 'c': 33), ('a': 3, 'c': 43), ('a': 5, 'c': 53));
return varByColumn(t2, 'c');
// 结果:291.67
```
## `aveDevByColumn(t, f_name)`
指定列与平均值的绝对偏差的平均值
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `t` | array | 数据表 |
| `f_name` | string | 指定列名,字符串 |
返回:real
### 示例
```tsl
t2 := array(('a': 2, 'c': 13), ('a': 3, 'c': 33), ('a': 3, 'c': 43), ('a': 5, 'c': 53));
return aveDevByColumn(t2, 'c');
// 结果:12.5
```
## `percentValueByField(t, key_field, quart)`
数据集在指定百分位数上对应的数值
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `t` | array | 数据表 |
| `key_field` | string | 指定列名,字符串 |
| `quart` | real | 指定分位数,实数 |
返回:real
### 示例
```tsl
t2 := array(('a': 2, 'c': 13), ('a': 3, 'c': 33), ('a': 3, 'c': 43), ('a': 5, 'c': 53));
return percentValueByField(t2, 'c', 0.1);
// 结果:19
```
## `statByColumn(t, fname, _type)`
依据Type传递的字符串,返回字段Fname序列的type值。
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `t` | array | 数据表类型,数据 |
| `fname` | string | 字符串,字段名称 |
| `_type` | string | 字符串,返回类型 |
返回:any
### 示例
```tsl
s := `array('a': (1, 2, 3, 4, 5), 'b': (-2, -3, -4, 5, 6), 'c': (3, 4, 5, 6, 7), 'd': (4, 5, 6, 7, 8));
return statByColumn(s, 'a', 'MAX');
// 返回a列最大值:5
```
## `describe(data, percentiles)`
简单描述统计
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `data` | array of number | 一维数字数组,或 二维矩阵。二维矩阵时,对每列进行操作 |
| `percentiles` | array of number | 统计的分位点,默认取array(0.25,0.5, 0.75) |
返回:array
### 示例
范例01:
```tsl
load_dataset_iris(data);
return describe(data);
```
范例02:
```tsl
return describe(randn(0, 1, 1000), array(0.05, 0.1)->0.95);
```
## `framecorrel(data)`
数据表-相关系数矩阵
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `data` | table | Table,二维数据表,列名可为字符串,不支持一维数组 |
返回:array
### 示例
```tsl
data := TSUT_Data_ZSZF()[: , 1:];
return framecorrel(data);
```
## `framecov(data)`
数据表-协方差
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `data` | table | Table,二维数据表,列名可为字符串。不支持一维数组 |
返回:array
### 示例
```tsl
data := TSUT_Data_ZSZF()[: , 1:];
return framecov(data);
```
## `framehhi(data)`
数据表-HHI指数、赫芬达尔—赫希曼指数
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `data` | table | Table,数组 |
返回:array
## `framevaluecounts(data, countfieldname, iforder)`
数据表-计数
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `data` | table | Table,可以为二维数据表或者为一维数组 |
| `countfieldname` | string | String,计数字段名,默认为""个数"" |
| `iforder` | bool | Bool,是否排序,默认为真 |
返回:array
## `simpledescribe(data)`
简单描述统计
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `data` | array of number | 一维数字数组,或 二维矩阵,二维矩阵时,对每列进行操作 |
返回:array
### 示例
范例01
```tsl
load_dataset_iris(data);
return simpledescribe(data);
```
范例02
```tsl
return simpledescribe(randn(0, 1, 1000), array(0.05, 0.1)->0.95);
```
## `renameFieldByFormat(t, ifcopy)`
通过格式字符串修改列名。
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `t` | array | 数组类型,数据 |
| `ifcopy` | bool | 布尔类型,是否在拷贝后的数组改列名,默认True |
返回:array
### 示例
```tsl
// 给随机数组批量加后缀
t := rand(10, 10);
suffix := '%s'$"@A";
ifcopy := true;
return renameFieldByFormat(t, suffix, ifcopy);
// 输出:
// Execute script error at Line:5
// function:__main__:line 5:instruction:usercall: function:renameFieldByFormat compile error or not found
// function:__main__:line 5:instruction:usercall: function execute error
```
## `sparse1(s, a, m, n)`
挤出任何非零元素,将满矩阵转化为稀疏格式,与Sparsefull函数对应。
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `s` | array of real | 待转化的m\*n满矩阵 |
| `a` | array of real | 变参返回矩阵S的稀疏格式,二维数组 |
| `m` | integer | 变参返回矩阵S的行数,整数 |
| `n` | integer | 变参返回矩阵S的列数,整数 |
返回:array
### 示例
```tsl
// 将矩阵S由满矩阵格式转化为稀疏格式
s := array((0, 0, 4, 0, 0),
(0, 1, 0, 0, 0),
(0, 0, 0, 0, 0),
(3, 0, 0, 0, 0),
(0, 0, 0, 0, 5));
return sparse1(s);
// 输出:
// Execute script error at Line:7
// function:__main__:line 7:instruction:usercall: function:sparse1 compile error or not found
// function:__main__:line 7:instruction:usercall: function execute error
```
## `sparse2(i, j, v, m, n)`
根据i、j和v三元组生成m\*n稀疏格式矩阵S
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `i` | array of real | 存放矩阵行标的一元数组 |
| `j` | array of real | 存放矩阵列标的一元数组 |
| `v` | array of real | 存放矩阵元素值的一元数组 |
| `m` | integer | 矩阵的行数,整数 |
| `n` | integer | 矩阵的列数,整数 |
返回:array
### 示例
```tsl
// 根据i,j,v三元组生成稀疏格式矩阵
i := array(0, 1, 2, 3);
j := array(3, 1, 4, 2);
v := array(5, 3, 2, 4);
return sparse2(i, j, v, 5, 5);
// 输出:
// Execute script error at Line:5
// function:__main__:line 5:instruction:usercall: function:sparse2 compile error or not found
// function:__main__:line 5:instruction:usercall: function execute error
```
## `speye(m)`
创建m\*m稀疏单位矩阵,指定为方阵
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `m` | integer | 稀疏单位矩阵的尺寸,整数 |
返回:array
### 示例
```tsl
// 创建m*m稀疏单位矩阵
a := speye(4);
return a;
// 输出:
// Execute script error at Line:2
// function:__main__:line 2:instruction:usercall: function:speye compile error or not found
// function:__main__:line 2:instruction:usercall: function execute error
```
## `sprand1(s)`
创建与矩阵S结构相同的稀疏格式矩阵,并将其中的非零元素替换为均匀分布随机数
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `s` | array of real | 稀疏矩阵 |
返回:array
### 示例
```tsl
// 创建与矩阵S相同结构的稀疏均匀随机矩阵
s := array((0, 0, 3, 0, 0),
(0, 0, 0, 0, 0),
(0, 0, 0, 5, 0),
(0, 2, 0, 0, 0),
(0, 0, 0, 0, 4));
return sprand1(s);
// 输出:
// Execute script error at Line:7
// function:__main__:line 7:instruction:usercall: function:sprand1 compile error or not found
// function:__main__:line 7:instruction:usercall: function execute error
```
## `sprand2(m, n, pct)`
创建m\*n稀疏均匀随机矩阵,其中非零元素所占比例为pct
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `m` | integer | 矩阵的行数,整数 |
| `n` | integer | 矩阵的列数,整数 |
| `pct` | real | 矩阵中非零元素所占比例,实数 |
返回:array
### 示例
```tsl
// 创建10*10稀疏均匀随机矩阵
a := sprand2(10, 10, 0.3);
return a;
// 输出:
// Execute script error at Line:2
// function:__main__:line 2:instruction:usercall: function:sprand2 compile error or not found
// function:__main__:line 2:instruction:usercall: function execute error
```
## `sprandn1(s)`
创建与矩阵S结构相同的稀疏格式矩阵,并将其中的非零元素替换为正态分布随机数
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `s` | array of real | 稀疏矩阵 |
返回:array
### 示例
```tsl
// 创建与矩阵S相同结构的稀疏正态随机矩阵
s := array((0, 0, 0, 0, 0),
(0, 0, 0, 5, 0),
(3, 0, 0, 0, 0),
(0, 0, 2, 0, 0),
(0, 0, 0, 0, 4));
return sprandn1(s);
// 输出:
// Execute script error at Line:7
// function:__main__:line 7:instruction:usercall: function:sprandn1 compile error or not found
// function:__main__:line 7:instruction:usercall: function execute error
```
## `sprandn2(m, n, pct)`
创建m\*n稀疏正态随机矩阵,其中非零元素所占比例为pct
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `m` | integer | 矩阵的行数,整数 |
| `n` | integer | 矩阵的列数,整数 |
| `pct` | real | 矩阵中非零元素所占比例,实数 |
返回:array
### 示例
```tsl
// 创建10*10稀疏正态随机矩阵
a := sprandn2(10, 10, 0.2);
return a;
// 输出:
// Execute script error at Line:2
// function:__main__:line 2:instruction:usercall: function:sprandn2 compile error or not found
// function:__main__:line 2:instruction:usercall: function execute error
```
## `sprandsym1(s)`
创建与矩阵S结构相同的稀疏格式矩阵,以下三角矩阵为基准的对称正态分布随机矩阵
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `s` | array of real | 稀疏矩阵 |
返回:array
### 示例
```tsl
// 创建与矩阵S相同结构的对称稀疏正态随机矩阵
s := array((6, 1, 0, 14, 0),
(0, 7, 0, 0, 15),
(1, 0, 8, 0, 0),
(0, 2, 0, 9, 0),
(0, 0, 3, 0, 10));
return sprandsym1(s);
// 输出:
// Execute script error at Line:7
// function:__main__:line 7:instruction:usercall: function:sprandsym1 compile error or not found
// function:__main__:line 7:instruction:usercall: function execute error
```
## `sprandsym2(n, pct)`
创建n\*n稀疏正态随机矩阵,以下三角矩阵为基准对称正态分布随机矩阵,其中非零元素所占比例大约为pct
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `n` | integer | 方阵的尺寸,整数 |
| `pct` | real | 方阵中非零元素所占比例,实数 |
返回:array
### 示例
```tsl
// 创建10*10对称稀疏正态随机矩阵
a := sprandnsym2(10, 0.2);
return a;
// 输出:
// Execute script error at Line:2
// function:__main__:line 2:instruction:usercall: function:sprandnsym2 compile error or not found
// function:__main__:line 2:instruction:usercall: function execute error
```
## `sparsefull(s, m, n)`
将稀疏矩阵格式转化为满矩阵,与Sparse1函数相对应。
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `s` | array of real | 稀疏格式矩阵 |
| `m` | integer | 转化为满矩阵的行数,整数 |
| `n` | integer | 转化为满矩阵的列数,整数 |
返回:array
### 示例
```tsl
// 将S由稀疏格式矩阵转化为5*5满矩阵格式
s := array(0: (0: 1), 2: (2: 3), 3: (3: 4));
return sparsefull(s, 5, 5);
// 输出:
// Execute script error at Line:3
// function:__main__:line 3:instruction:usercall: function:sparsefull compile error or not found
// function:__main__:line 3:instruction:usercall: function execute error
```
## `spones(s, n)`
将稀疏矩阵S中的非零元素替换为n
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `s` | array of real | 稀疏矩阵 |
| `n` | real | 非零元素被替换的值,实数 |
返回:array
### 示例
```tsl
// 将S中非零元素替换为1
s := array(0: (0: 1), 2: (2: 3), 3: (3: 4));
return spones(s, 1);
// 输出:
// Execute script error at Line:3
// function:__main__:line 3:instruction:usercall: function:spones compile error or not found
// function:__main__:line 3:instruction:usercall: function execute error
```
## `spnnz(s)`
稀疏矩阵S中的非零元素数目
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `s` | array of real | 稀疏矩阵 |
返回:integer
### 示例
```tsl
// 稀疏正态随机矩阵A中非零元素数目
a := sprandn2(5, 5, 0.2);
return spnnz(a); // 返回4(随机)
```
## `spnonzeros(s)`
提取稀疏矩阵S中的非零元素
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `s` | array of real | 稀疏矩阵 |
返回:array
### 示例
```tsl
// 提取稀疏正态随机矩阵A中非零元素
a := sprandn2(5, 5, 0.2);
return spnonzeros(a);
// 输出:
// Execute script error at Line:2
// function:__main__:line 2:instruction:usercall: function:sprandn2 compile error or not found
// function:__main__:line 2:instruction:usercall: function execute error
```
## `ifsparse(s)`
判断是否为稀疏矩阵。检查条件:只检查下标,下标全为大于等于0的整数。
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `s` | array of real | 待判断矩阵 |
返回:integer
### 示例
```tsl
// 判断矩阵S是否为稀疏矩阵
s := Array(3: 2, "B": 3);
return ifsparse(s); // 返回0
```
## `spdiags1(a)`
提取矩阵非零对角线
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `a` | array | Array,二维数组,原(稀疏)矩阵 |
返回:array
### 示例
```tsl
// 提取矩阵A中非零对角线
a := array((6, 0, 13, 0, 0),
(0, 7, 0, 14, 0),
(1, 0, 8, 0, 15),
(0, 2, 0, 9, 0),
(0, 0, 3, 0, 10));
return spdiags1(a);
```
## `spdiags2(a, d)`
提取矩阵指定对角线
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `a` | array | Array,二维数组,原(稀疏)矩阵 |
| `d` | array | Array,一维数组,对角线序列;也可以为数字; |
返回:array
### 示例
```tsl
// 提取矩阵A中第-2,0,2条对角线,按列排列
a := array((6, 0, 13, 0, 0),
(0, 7, 0, 14, 0),
(1, 0, 8, 0, 15),
(0, 2, 0, 9, 0),
(0, 0, 3, 0, 10));
d := array(-2, 0, 2);
return spdiags2(a, d);
```
## `spdiags3(b, d, a)`
根据指定列替换稀疏矩阵对角线
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `b` | array | Array,二维数组,对角矩阵 |
| `d` | array | Array,一维数组,对角线序列 |
| `a` | array | Array,二维数组,原(稀疏)矩阵 |
返回:array
### 示例
```tsl
// 将矩阵A中d指定的对角线替换为B中的列
b := array((5, 10, 15),
(4, 9, 14),
(3, 8, 13),
(2, 7, 12),
(1, 6, 11));
d := array(-2, 0, 2);
a := array((6, 0, 13, 0, 0),
(0, 7, 0, 14, 0),
(1, 0, 8, 0, 15),
(0, 2, 0, 9, 0),
(0, 0, 3, 0, 10));
return spdiags3(b, d, a);
```
## `spdiags4(b, d, m, n)`
根据指定列创建稀疏矩阵
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `b` | array | Array,二维数组,对角矩阵 |
| `d` | array | Array,一维数组,对角线序列 |
| `m` | integer | Integer,正整数,(稀疏)矩阵的行 |
| `n` | integer | Integer,正整数,(稀疏)矩阵的列 |
返回:array
### 示例
```tsl
// 根据对角矩阵B创建稀疏带状矩阵
b := array((5, 10, 15),
(4, 9, 14),
(3, 8, 13),
(2, 7, 12),
(1, 6, 11));
d := array(-2, 0, 2);
m := 5;
n := 5;
return spdiags4(b, d, m, n);
```
## `compareDataOfDifferentDataProvider(t_src, t_dest, src_provider, dest_provider, key_arr, value_arr, code_field)`
比较两个数据源的差异
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `t_src` | array | Array 源数据 |
| `t_dest` | array | Array 目标数据 |
| `src_provider` | string | String 源数据提供商 |
| `dest_provider` | string | String 目标数据提供商 |
| `key_arr` | array | Array 要比较表的Key字段(也就是数据表中的 唯一性字段) |
| `value_arr` | array | Array 要比较的Value字段 |
| `code_field` | string | String 代码字段(任何数据,都必须有编码字段) |
返回:array
## `pivot(t, index, vals)`
透视,数据透视表
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `t` | array | 二维数组,待透视表,t[:,array(index,columns)] 不能包含重复的行,即index,column 必须可以作为t的唯一主键 |
| `index` | string \| array | 字符串或一维字符串数组,行分类字段,t的列名,多个字段可以用数组,为nil表示取t的行下标 |
| `vals` | string \| array | 字符串或一维字符串数组,值字段,t的列名,多个字段可以用数组, |
返回:array
### 示例
```tsl
// 指定条件,行标为日期,列标为名称,值为收盘价、涨幅、振幅的数据透视表
stocks := array("SZ000001", "SZ000002", "SH600050", "SZ300775");
begt := 20210515T;
endt := 20210525T;
t := select ['StockID'] as "代码",
["StockName"] as "名称",
dateToStr(['date']) as "日期",
["close"] as "收盘价",
spec(specDate(stockZf3(), ['date']), ['StockID']) as "涨幅",
spec(specDate(stockZdf3(), ['date']), ['StockID']) as "振幅",
spec(base(10029), ['StockID']) as "申万行业"
from markettable datekey begt to endt of stocks end;
index := "日期";
colunms := "名称";
vals := array("收盘价", "涨幅", "振幅");
return pivot(t, index, colunms, vals);
// 输出:
// Execute script error at Line:16
// function:__main__:line 16:instruction:usercall: function:pivot compile error or not found
// function:__main__:line 16:instruction:usercall: function execute error
```
## `nonIntersectionPart(t1, t2, key_field)`
求差集(返回在t1中存在,而在t2中不存在的记录)
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `t1` | array | 数据表 |
| `t2` | array | 数据表 |
| `key_field` | string | 指定某一字段 |
返回:array
### 示例
```tsl
t1 := array(('a': 1, 'b': 4), ('a': 7, 'b': 8), ('a': 2, 'b': 3));
t2 := array(('a': 1, 'b': 1), ('a': 7, 'b': 8), ('a': 5, 'b': 5));
return nonIntersectionPart(t1, t2, 'b');
// 输出:
// Execute script error at Line:3
// function:__main__:line 3:instruction:usercall: function:nonIntersectionPart compile error or not found
// function:__main__:line 3:instruction:usercall: function execute error
```
## `getSubTableByKey(t, key_field, return_fields)`
将字符串KeyField以分号分隔,在数组t中从列ReturnFields匹配被分割的值,若找到则返回该行信息,否则返回空数组。
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `t` | array | 数据表类型,数据表 |
| `key_field` | string | 字符串,关键字字段 |
| `return_fields` | string | 字符串,要返回的字段名称 |
返回:array
### 示例
```tsl
s := array(array('A': 'a', 'B': 'b', 'C': 'c', 'D': 'd', 'e': 'e'),
array('A': 'c', 'B': 3, 'C': 4, 'D': 5, 'e': 6),
array('A': 3, 'B': 4, 'C': 5, 'D': 6, 'e': 7));
return getSubTableByKey(s, 'A', 'a;c');
// 返回:
```
## `expandTable(t, key_field)`
按指定字段展开表格,根据Keyfield展开指定列。展开的列值必须二维数据表,否则保留原值。
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `t` | array | 数组类型,数据 |
| `key_field` | string | 字符串类型,关键字,需展开的列名 |
返回:array
### 示例
```tsl
// 按照分红送股列展开数据
data := query("", "SH600000;SZ000002", true, "", "代码", defaultStockId(), "名称",
currentStockName(), "分红送股", infoArray(18));
return expandTable(data, "分红送股");
// 输出:
// Execute script error at Line:4
// function:__main__:line 4:instruction:usercall: function:expandTable compile error or not found
// function:__main__:line 4:instruction:usercall: function execute error
```
## `arrayToFm(value, sample_value)`
将数组转换为 FMArray,并按样例值确定单元格类型。
| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `value` | array | 输入数组 |
| `sample_value` | float | 单元格类型样例;如 `0` 生成整型单元格,`0.0` 生成浮点单元格 |
返回:fmarray