Haskell 使用索引创建包含子向量的向量
Haskell create vector with subvectors using indexes
我正在尝试创建一个向量,其中的子向量由使用子向量索引的向量从另一个向量中取出的元素组成。
b
中的每个元素对应于 a
中的元素在放入 c
.
时应具有的子向量索引
import Data.Vector
let a = fromList [9,2,3,7,4,1,8,5]
let b = fromList [3,3,2,0,1,1,2,2]
let c = fromList [ a ! k | k <- b ]
Expected c = [[7],[4,1],[3,8,5],[9,2]]
我有点卡住了,收到错误
"Could not match expected type [Int] with actual type Vector Integer
in stmt list comprehension
k <- b"
这不起作用,因为 b
是向量,而不是列表:
k <- b
但是,这可以工作:
[ ... | k <- toList b ]
接下来,a
和b
的类型是Vector Integer
,!
运算符取一个Int
。所以你需要使用 fromInteger
:
转换索引
let c = fromList [ a ! fromInteger k | k <- toList b]
更新
这是一种无需重复遍历数组即可执行转换的方法:
import Data.List
fst3 (b,_,_) = b
third (_,_,a) = a
doit :: Vector Int -> Vector Int -> [[Int]]
doit av bv = [ map third g | g <- groups ]
where
triples = zip3 (V.toList bv) [1..] (V.toList av)
groups = groupBy (\s t -> fst3 s == fst3 t) $ sort triples
这基本上是一个 Schwartzian Transform,在排序步骤之后添加了一个 groupBy。三元组的排序以规范方式完成 - 第一个坐标的 lex 排序,然后是第二个坐标,然后是第三个坐标。
groups
的表达式还有其他写法:
import Data.Funcition (on)
import GHC.Exts (groupWith)
...
groups = groupBy (on (==) fst3) $ sort triples
groups = groupWith fst3 triples
请注意,groupBy
要求对三元组进行排序,而 groupWith
则不需要。
在 ErikR 的帮助下,我想到了这个:
let c = fromList [fromList [a ! i | i <- [0..Data.Vector.length b-1], (b ! i)==j] | j <- [0..Data.Vector.maximum(b)]]
可以用,但不是很漂亮,有更好的吗?
看起来你想要的可能是
accumulate (flip (:)) (replicate n []) (zip b a)
...尽管您将不得不显式计算 n
,也许是 maximum b + 1
。
对于列表,这似乎是逻辑
> map (map snd) $ groupBy ((==) `on` fst) $ sortBy (comparing fst) $ zip b a
[[7],[4,1],[3,8,5],[9,2]]
我正在尝试创建一个向量,其中的子向量由使用子向量索引的向量从另一个向量中取出的元素组成。
b
中的每个元素对应于 a
中的元素在放入 c
.
import Data.Vector
let a = fromList [9,2,3,7,4,1,8,5]
let b = fromList [3,3,2,0,1,1,2,2]
let c = fromList [ a ! k | k <- b ]
Expected c = [[7],[4,1],[3,8,5],[9,2]]
我有点卡住了,收到错误
"Could not match expected type [Int] with actual type Vector Integer in stmt list comprehension k <- b"
这不起作用,因为 b
是向量,而不是列表:
k <- b
但是,这可以工作:
[ ... | k <- toList b ]
接下来,a
和b
的类型是Vector Integer
,!
运算符取一个Int
。所以你需要使用 fromInteger
:
let c = fromList [ a ! fromInteger k | k <- toList b]
更新
这是一种无需重复遍历数组即可执行转换的方法:
import Data.List
fst3 (b,_,_) = b
third (_,_,a) = a
doit :: Vector Int -> Vector Int -> [[Int]]
doit av bv = [ map third g | g <- groups ]
where
triples = zip3 (V.toList bv) [1..] (V.toList av)
groups = groupBy (\s t -> fst3 s == fst3 t) $ sort triples
这基本上是一个 Schwartzian Transform,在排序步骤之后添加了一个 groupBy。三元组的排序以规范方式完成 - 第一个坐标的 lex 排序,然后是第二个坐标,然后是第三个坐标。
groups
的表达式还有其他写法:
import Data.Funcition (on)
import GHC.Exts (groupWith)
...
groups = groupBy (on (==) fst3) $ sort triples
groups = groupWith fst3 triples
请注意,groupBy
要求对三元组进行排序,而 groupWith
则不需要。
在 ErikR 的帮助下,我想到了这个:
let c = fromList [fromList [a ! i | i <- [0..Data.Vector.length b-1], (b ! i)==j] | j <- [0..Data.Vector.maximum(b)]]
可以用,但不是很漂亮,有更好的吗?
看起来你想要的可能是
accumulate (flip (:)) (replicate n []) (zip b a)
...尽管您将不得不显式计算 n
,也许是 maximum b + 1
。
对于列表,这似乎是逻辑
> map (map snd) $ groupBy ((==) `on` fst) $ sortBy (comparing fst) $ zip b a
[[7],[4,1],[3,8,5],[9,2]]