Wolfram mathematica 定义一个对象,该对象在某些变量中是函数,在某些其他指标中是张量

Wolfram mathematica 定义一个对象,该对象在某些变量中是函数,在某些其他指标中是张量,wolfram-mathematica,Wolfram Mathematica,我试图定义一个对象,它在某些变量中起作用,在其他指数中起作用。 我的尝试是: Clear[mat, k]; mat[k_] := {{0,0},{0,0}}; mat[k_][[1, 1]] := k + 1 mat[k_][[1, 2]] := k + 2 mat[k_][[2, 1]] := k + 3 mat[k_][[2, 2]] := k + 4 mat[1] 它给出的输出是: During evaluation of In[268]:= SetDelayed::setps: mat

我试图定义一个对象,它在某些变量中起作用,在其他指数中起作用。 我的尝试是:

Clear[mat, k];
mat[k_] := {{0,0},{0,0}};
mat[k_][[1, 1]] := k + 1
mat[k_][[1, 2]] := k + 2
mat[k_][[2, 1]] := k + 3
mat[k_][[2, 2]] := k + 4
mat[1]
它给出的输出是:

During evaluation of In[268]:= SetDelayed::setps: mat[k_] in the part    assignment is not a symbol. >>

Out[270]= $Failed

During evaluation of In[268]:= SetDelayed::setps: mat[k_] in the part assignment is not a symbol. >>

Out[271]= $Failed

During evaluation of In[268]:= SetDelayed::setps: mat[k_] in the part assignment is not a symbol. >>

Out[272]= $Failed

During evaluation of In[268]:= SetDelayed::setps: mat[k_] in the part assignment is not a symbol. >>
Out[273]= $Failed

Out[274]= {{0, 0}, {0, 0}}

有人能告诉我这里出了什么问题,有什么办法可以得到我想要的吗

mat[k.]
是一种模式而不是符号
mat[k_3;]:={{0,0},{0,0}
定义一个返回2x2数组的单变量函数
mat[k_]
具有
Head
mat
Part
1
k

我相信您希望定义mat在其自己的单元格中具有以下内容

mat[k_] := {{k + 1, k + 2}, {k + 3, k + 4}}
然后在另一个牢房里

mat[1]
(* {{2, 3}, {4, 5}} *)

希望这能有所帮助。

嗨,欢迎使用Stackoverflow。请注意,您也可以在许多专家也会帮助您的地方发布。感谢您的回复。我不想用你建议的方式来定义,因为在我的原始代码中,我需要定义一个矩阵(不仅仅是2*2),它的块作为某个变量的函数,在这里是k。看看