如何将函数计算为 KDB 中的矩阵?

发布于 2025-01-20 12:49:01 字数 157 浏览 0 评论 0原文

假设我有一个根据 i 和 j 坐标定义矩阵的函数:

f: {y+2*x}

我正在尝试创建一个方阵来评估该函数地点。

我知道它需要类似于 f ' (til 5) /:\: til 5,但我很难休息。

Let's say I've got a function that defines a matrix in terms of it's i and j coordinates:

f: {y+2*x}

I'm trying to create a square matrix that evaluates this function at all locations.

I know it needs to be something like f ' (til 5) /:\: til 5, but I'm struggling with rest.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帝王念 2025-01-27 12:49:01

稍微重新提出您的问题,您想创建一个矩阵a = [a ij ],其中a ij = f(i,j),i,j = 0 .. N-1。

换句话说,您需要评估i和j的所有可能组合的f。因此:

q)N:5;
q)i:til[N] cross til N; / all combinations of i and j
q)a:f .' i;             / evaluate f for all pairs (i;j)
q)A:(N;N)#a;            / create a matrix using #: https://code.kx.com/q/ref/take/
0 1 2  3  4
2 3 4  5  6
4 5 6  7  8
6 7 8  9  10
8 9 10 11 12

ps no,(til 5)/:\:til 5不是恰好您需要的东西,而是关闭。您正在生成所有 pairs 的列表 ie,您正在配对或加入til 5的第一个元素,即(另一个)til 5的每个元素一一,然后是第二个等。因此,您需要加入操作员( https:// code.kx.com/q/ref/join/ ):

(til 5),/:\: til 5

Rephrasing your question a bit, you want to create a matrix A = [aij] where aij = f(i, j), i, j = 0..N-1.

In other words you want to evaluate f for all possible combinations of i and j. So:

q)N:5;
q)i:til[N] cross til N; / all combinations of i and j
q)a:f .' i;             / evaluate f for all pairs (i;j)
q)A:(N;N)#a;            / create a matrix using #: https://code.kx.com/q/ref/take/
0 1 2  3  4
2 3 4  5  6
4 5 6  7  8
6 7 8  9  10
8 9 10 11 12

P.S. No, (til 5) /:\: til 5 is not exactly what you'd need but close. You are generating a list of all pairs i.e. you are pairing or joining the first element of til 5 with every element of (another) til 5 one by one, then the second , etc. So you need the join operator (https://code.kx.com/q/ref/join/):

(til 5),/:\: til 5
墨落成白 2025-01-27 12:49:01

你很接近。但不需要生成所有坐标对然后迭代它们。 左右各左 /:\: 为您管理所有这些并返回您想要的矩阵。

q)(til 5)f/:\:til 5
0 1 2  3  4
2 3 4  5  6
4 5 6  7  8
6 7 8  9  10
8 9 10 11 12

You were close. But there is no need to generate all the coordinate pairs and then iterate over them. Each Right Each Left /:\: manages all that for you and returns the matrix you want.

q)(til 5)f/:\:til 5
0 1 2  3  4
2 3 4  5  6
4 5 6  7  8
6 7 8  9  10
8 9 10 11 12
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文