标签: haskell-linear

如何在Edward Kmett的"线性"库中使用可变大小的向量?

我正在尝试使用ekmett的线性库,我在Linear.V中遇到了可变长度向量的问题.如何使用该dim函数来获取向量的大小?如何trace在由嵌套Vs组成的大方阵上使用?我在这两种情况下都遇到了错误.

最小代码:

import qualified Data.Vector as Vector
import Linear.V (V(V), dim)
import Linear.Vector (outer)
import Linear.Matrix (trace)

v, w :: V n Double -- What do I do here?
v = V $ Vector.fromList [1..5]
w = V $ Vector.fromList [2, 3, 5, 7, 11]

d = dim v
m = outer v w
t = trace m
Run Code Online (Sandbox Code Playgroud)

它给出了我不理解的这些错误:

• Ambiguous type variable ‘n0’ arising from a use of ‘dim’
  prevents the constraint ‘(Linear.V.Dim …
Run Code Online (Sandbox Code Playgroud)

haskell vector linear-algebra ghc haskell-linear

6
推荐指数
1
解决办法
197
查看次数

如何使用Haskell库,Linear来缩放矢量?

这是一个关于风格的简单问题.我一直在用:

import Linear
point  = V3 1 2 3
scaled = fmap (* 2) point
Run Code Online (Sandbox Code Playgroud)

要么...

scaled = (* 2) <$> point
Run Code Online (Sandbox Code Playgroud)

这是预期的方式,还是由标量运算符进行适当的乘法?

haskell haskell-linear

5
推荐指数
1
解决办法
516
查看次数

如何在不定义Applicative实例的情况下在Haskell上一般派生Additive?

给定一个类型,只有一种显而易见的方法来实现一个Additive实例,从Linear库到它.方便的是,Additive有一个通用的实现,所以我们可以使用deriving它.不幸的是,它取决于Applicative实例的存在,这是不可导出的,所以你仍然需要声明它:

{-# LANGUAGE DeriveGeneric, DeriveFunctor #-}

import Linear
import GHC.Generics
import Control.Applicative

data Foo a = Foo a a a deriving (Show, Functor, Generic1)

instance Additive Foo

instance Applicative Foo where
    pure x = Foo x x x
    Foo f g h <*> Foo x y z = Foo (f x) (g y) (h z)

main = print $ Foo 1 2 3 ^+^ Foo 4 5 6
Run Code Online (Sandbox Code Playgroud)

有没有办法自动派生Additive,而不必声明Applicative实例?

haskell generic-programming haskell-linear

5
推荐指数
1
解决办法
133
查看次数

Haskell 代码不会使用某些变量名称进行编译

我从 GHCi 收到一个我无法解释的错误。我正在使用以下代码(其中绝大多数似乎与问题无关,但我无法用更少的代码复制问题;注释掉的行是我想添加以替换虚拟的行)in 0行)

import Linear
apply x f = f x
pos xs = -- smallest i where xs!!i > 0, else length xs
    let aux xs n = case xs of
                   x:t -> if x > 0 then n
                          else aux t (n+1)
                   [] -> n
    in aux xs 0
optimize d opt d_opt funs d_funs x0 p0 eps =
    let n = length funs in
    let aux x p f_best = let feas = map …
Run Code Online (Sandbox Code Playgroud)

haskell haskell-linear

0
推荐指数
1
解决办法
79
查看次数