gpt4 book ai didi

vector - Clojure:将嵌套向量格式应用于展平向量

转载 作者:行者123 更新时间:2023-12-03 01:38:59 25 4
gpt4 key购买 nike

例如,我有以下嵌套向量:

[[[0.582198689235419 -0.34713183143727 0.4685311493624731]
[-0.38928013774079284 -0.5901700383677557 -0.37573234072157]
[0.6716356761877877 -0.19645167952721243 -0.5700686091940252]]
[[0.0027162308840597005 -0.4483592764429284 -0.4766278022217257 -0.2724018313051576]
[-0.2765881229144672 -0.8030656496255356 -0.16159395457031567 -0.27432324260043084]
[-0.6154630466545907 -0.60573539482247 0.4417814561800192 -0.5559788990464504]
[0.6194560094536031 -0.3663074359460578 -0.5704311251195602 0.7194827876969362]]]

我有以下扁平向量:

(0.5366343712173423
-0.816449781850872
-0.16066485785704843
0.9816561233924161
-0.09646744313584676
-0.2619662625757997
-0.9946004265996822
-0.14096299956754854
0.579260850612288
-0.827601452607939
-0.24934665032374648
-0.42272393175707873
0.11239245249400165
-0.29878238708035043
-0.61522274672097
0.8298721730401472
0.5016214138116059
0.11633537727916243
-0.0631891708267196
-0.26569217599364303
0.20900664784109668
0.2005869506108401
-0.2658279978034501
0.3383997403318165
-0.09353513546647907)

我希望将展平向量转换为嵌套向量,该向量遵循与上面所示的嵌套向量相同的结构。 Clojure 或库中是否有核心函数可以执行此操作?我对如何解决这个问题有一些想法,但所有这些想法似乎都非常非常低效,并且此操作将与大向量一起使用。

提前非常感谢您。

最佳答案

这是一个有趣的问题,因为这是我认为使用 zipper 使事情变得更容易而不是更困难的极少数情况之一。这个想法是只对嵌套向量进行向量压缩,以表示所需的结构,并对其重复调用 zip/next ;每当我们到达一个叶子节点时,我们都会用输入序列中的下一个节点替换它的值。

请注意,这假设嵌套结构中的项目数与展平列表中的项目数完全相同;如果情况并非如此,您可能会收到某种错误,谁知道呢。

(require '[clojure.zip :as z])

(defn replace-values [structure values]
(loop [z (z/vector-zip structure)
values (seq values)]
(cond (not values) (z/root z)
(z/branch? z) (recur (z/next z) values)
:else (recur (-> z
(z/replace (first values))
(z/next))
(next values)))))

user> (replace-values '[[[0.582198689235419 -0.34713183143727 0.4685311493624731]
[-0.38928013774079284 -0.5901700383677557 -0.37573234072157]
[0.6716356761877877 -0.19645167952721243 -0.5700686091940252]]
[[0.0027162308840597005 -0.4483592764429284 -0.4766278022217257 -0.2724018313051576]
[-0.2765881229144672 -0.8030656496255356 -0.16159395457031567 -0.27432324260043084]
[-0.6154630466545907 -0.60573539482247 0.4417814561800192 -0.5559788990464504]
[0.6194560094536031 -0.3663074359460578 -0.5704311251195602 0.7194827876969362]]]
'(0.5366343712173423
-0.816449781850872
-0.16066485785704843
0.9816561233924161
-0.09646744313584676
-0.2619662625757997
-0.9946004265996822
-0.14096299956754854
0.579260850612288
-0.827601452607939
-0.24934665032374648
-0.42272393175707873
0.11239245249400165
-0.29878238708035043
-0.61522274672097
0.8298721730401472
0.5016214138116059
0.11633537727916243
-0.0631891708267196
-0.26569217599364303
0.20900664784109668
0.2005869506108401
-0.2658279978034501
0.3383997403318165
-0.09353513546647907))

[[[0.5366343712173423 -0.816449781850872 -0.16066485785704843]
[0.9816561233924161 -0.09646744313584676 -0.2619662625757997]
[-0.9946004265996822 -0.14096299956754854 0.579260850612288]]
[[-0.827601452607939 -0.24934665032374648 -0.42272393175707873 0.11239245249400165]
[-0.29878238708035043 -0.61522274672097 0.8298721730401472 0.5016214138116059]
[0.11633537727916243 -0.0631891708267196 -0.26569217599364303 0.20900664784109668]
[0.2005869506108401 -0.2658279978034501 0.3383997403318165 -0.09353513546647907]]]

关于vector - Clojure:将嵌套向量格式应用于展平向量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37172235/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com