我想对存储数字的 json 数组执行向量数学运算。简而言之,我们如何通过使用jq实现一对一的算术运算?
我尝试了使用"地图"过滤器进行了一些操作,但无法达到我的预期。
jq 'map(one-to-one)' <<< "{"a":[1,2], "b":[3,4]}"
或
jq 'map(one-to-one)' <<< "[[1,2],[3,4]]"
应该产生
[3,8]
对于这种类型的问题,定义一个泛型函数是有意义的:
# Input is assumed to be an array of two numeric arrays of the same length
def pairwise: transpose | map(.[0] * .[1]);
我们现在可以通过多种方式轻松使用它:
[[1,2],[3,4]] | pairwise
{"a":[1,2], "b":[3,4]} | [.a,.b] | pairwise
{"a":[1,2], "b":[3,4]} | [.[]] | pairwise
这些情况下的结果当然是[3,8]
的。
效率
对于非常大的输入,可能值得避免transpose
:
def pairwise:
.[0] as $x | .[1] as $y
| reduce range(0; $x|length) as $i ([]; . + [$x[$i] * $y[$i]]);
通常,对于向量,人们对内积感兴趣,为了效率,最好直接定义内积,例如:
def inner($a; $b):
reduce range(0;$a|length) as $i (0; . + $a[$i]*$b[$i]);
jq '. | transpose | map(reduce .[] as $item (1; . * $item))' <<< "[[1,2],[3,4]]"
转置为我们提供了需要相乘的元素:[[1, 3], [2, 4]]
.然后我们可以使用 reduce 将每个子数组映射到其乘积。
对象版本略有不同,因为我们需要从属性中获取这些值:
jq '[.[]] | transpose | map(reduce .[] as $item (1; . * $item))' <<< "{"a":[1,2], "b":[3,4]}"