第 1 部分
如果我们有这个
[> restart; a[5]:=23: a[7]:=41:
然后
[> about(a); whattype(a);
a:
nothing known about this object
symbol
但是
[> print(a);
table([5 = 23, 7 = 41])
和
[> convert(a,list);
[23, 41]
所以 Maple 确实有足够的关于变量 a 及其索引的信息。我们如何在不打印的情况下检查该信息?
对于小索引来说这不是问题,但是如果 b[123457891234578912345789]:=789;
那么我们如何检查是否存在某个索引 i
其 b[i]
有一个定义的值,索引 i
是什么(不打印,即不手动)?
第 2 部分
我希望能够在函数内使用此类变量并返回此类变量。 例如,假设我想将所有索引增加 1。如果我知道存在哪些索引,那么我就可以做到这一点
[> a[5]:=23: a[7]:=41:
f:=proc(x) local y;
y[6]:=x[5]; y[8]:=x[7]; y;
end proc:
b:=f(a); b[6]; b[8];
b:=y
23
41
但我常常不知道变量 a 有什么索引。
最佳答案
您可以进行多种编程查询,
restart;
a[5]:=23: a[7]:=41:
# test whether a is assigned
assigned('a');
true
# test whether a is assigned a table
type(a, table);
true
# test whether table a has any indexed values
evalb( nops([indices(a, 'nolist')]) > 0 );
true
assigned('a'[7]);
true
如果您愿意,可以在尝试访问和使用索引引用之前进行此类查询(即检查它是否具有指定值)。例如,
if type(a,table) and assigned('a'[7]) then
a[7];
end if;
41
if type(a, table) then
[indices(a, 'nolist')];
end if;
[5, 7]
关于maple - 检查索引变量是否在某处分配了值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68749091/