如何在Haskell中编写Data.Vector.Unboxed实例? [英] How do I write a Data.Vector.Unboxed instance in Haskell?

查看:118
本文介绍了如何在Haskell中编写Data.Vector.Unboxed实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数字应用程序,对于负概率的负值日志做了很多工作,其中(因为概率范围从零到一)取正值加倍值或负值无穷大值(如果基础概率为零) 。



我使用这些新类型 Score ,如下所示:

  newtype得分=得分双
得出(Eq,Ord)
- ^得分是概率$ b的否定对数
$ b negLogZero :: Score - ^代表 - log 0
negLogZero =得分10e1024

negLogOne ::得分 - ^ - log 1
negLogOne = Score 0.0

unScore :: Score - > Double
unScore(Score x)= x

实例显示分数
show(Score x)= show x

现在,在Viterbi算法的实现中,我一直使用 Data.Vector 的确,我有一些 Data.Vector s Score s。在尝试做一些性能调整时,我决定尝试使用 Data.Vector.Unboxed 。但是,我需要为 Unbox 编写一个实例,这个实例不能派生出来,我不能完全弄清楚我需要做什么(特别是, Unbox typeclass是)。由于 Score 实际上是一个 Double ,它带有一些有用的构造函数和语义,我认为这应该是可能的。据我所知,我需要能够告诉 Data.Vector.Unboxed Score s必须是,我猜如何读写它们(但是,他们很像 Double s)。



那么,我该怎么做?感谢!

解决方案 type类没有任何方法 - 它只是 Vector MVector 类型类的简写。导出这些文件,并且 Unbox 类免费提供(通过派生或通过仅写入实例U.Unbox Score )它自己的行在某处)。

  { - #LANGUAGE GeneralizedNewtypeDivingiving# - } 
import Data.Vector.Generic.Base
导入Data.Vector.Generic.Mutable
导入限定的Data.Vector.Unboxed为U
newtype Score =得分双推导(Vector U.Vector,MVector U.MVector,U.Unbox)


I've got a numeric application that does a lot of work with negative logs of probabilities, which (since probabilities range from zero to one) take the values of positive doubles, or negative infinity (if the underlying probability was zero).

I'm using these with a newtype Score as follows:

newtype Score = Score Double
  deriving (Eq, Ord)
 -- ^ A "score" is the negated logarithm of a probability

negLogZero :: Score -- ^ Stands in for - log 0
negLogZero = Score 10e1024

negLogOne :: Score -- ^ - log 1
negLogOne = Score 0.0

unScore :: Score -> Double
unScore (Score x) = x

instance Show Score where
  show (Score x) = show x

Now, in an implementation of the Viterbi algorithm, I've been using Data.Vector a lot, and indeed I have some Data.Vectors of Scores. While trying to do some performance tuning, I decided to try using Data.Vector.Unboxed. However, I need to write an instance for Unbox, which cannot be derived, and I can't quite figure out what I need to do (particularly, what the contract for the Unbox typeclass is). Since Score is really a Double with some useful constructors and semantics, this should be possible, I'd think. As far as I can tell, I need to be able to tell Data.Vector.Unboxed how big each slot in a vector of Scores must be, and I guess how to read and write them (but heck, they're a lot like Doubles).

So, what do I do? Thanks!

解决方案

The Unbox type class doesn't have any methods -- it's just shorthand for the Vector and MVector type classes. Derive those, and the Unbox class comes for free (either via deriving or by just writing instance U.Unbox Score on its own line somewhere).

{-# LANGUAGE GeneralizedNewtypeDeriving #-}
import Data.Vector.Generic.Base
import Data.Vector.Generic.Mutable
import qualified Data.Vector.Unboxed as U
newtype Score = Score Double deriving (Vector U.Vector, MVector U.MVector, U.Unbox)

这篇关于如何在Haskell中编写Data.Vector.Unboxed实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆