【问题标题】:Iterating over distinct permutations of a vector in Pari/GP迭代 Pari/GP 中向量的不同排列
【发布时间】:2019-01-07 04:06:18
【问题描述】:

我想遍历向量的所有不同排列。我已经尝试通过使用vecextract()numtoperm() 来创建一个排列向量,并使用vecsort(,,,8) 来删除等效排列。

不幸的是,这不能很好地扩展:在我当前的 4GB 堆栈大小中,向量的最大大小小于 12!,而我的机器只有 16GB。

有没有办法在不耗尽内存的情况下做到这一点,也许是直接生成第 k 个不同的排列?

【问题讨论】:

    标签: combinatorics pari-gp


    【解决方案1】:

    PARI 没有为此内置任何内容。我建议阅读How to generate all the permutations of a multiset?

    【讨论】:

    • 我怀疑是这种情况,并将推出我自己的解决方案。否定的答案比根本没有答案有用得多!
    【解决方案2】:

    使用forperm

    forperm([1,1,2,3], v, print(v))
    

    生产

    Vecsmall([1, 1, 2, 3])
    Vecsmall([1, 1, 3, 2])
    Vecsmall([1, 2, 1, 3])
    Vecsmall([1, 2, 3, 1])
    Vecsmall([1, 3, 1, 2])
    Vecsmall([1, 3, 2, 1])
    Vecsmall([2, 1, 1, 3])
    Vecsmall([2, 1, 3, 1])
    Vecsmall([2, 3, 1, 1])
    Vecsmall([3, 1, 1, 2])
    Vecsmall([3, 1, 2, 1])
    Vecsmall([3, 2, 1, 1])
    

    请注意,forperm 的输入向量应进行排序以获得正确的结果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-20
      • 2015-03-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-24
      • 2020-07-06
      • 1970-01-01
      • 2014-03-10
      相关资源
      最近更新 更多