衍生塔以及如何使用矢量空间包(haskell) [英] Derivative Towers and how to use the vector-space package (haskell)

查看:110
本文介绍了衍生塔以及如何使用矢量空间包(haskell)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在和Haskell工作了很长一段时间,但我还远没有成为专家。但我发现编程的功能方法最适合我。



到目前为止,我正在研究一个计算一些严重问题的项目,比如从一个给出结构。



我跟着博客写的 Conal Elliott (这里有更多线性地图),它是很不错,根本。



不幸的是,我缺乏一个简单的例子:)

更准确地说,我有一条曲线

  f:[0,1] in R  - > R³
t - > a * e_y + 2 * t * e_z

这是(0,a, 2 * T)。
当我想计算f的导数时,例如对于曲线的长度,我知道数学结果,这很简单(0,0,2),但是我怎么在Haskell中完成这项工作,特别是使用 vector-space 包?



我真的想用这个库,因为它的功能正是我所要采取的方法(但我在Haskell的道路上并不遥远)



到目前为止,我所得到的是:

  { - #LANGUAGE Rank2Types,TypeOperators,FlexibleContexts,TypeFamilies# - } 
{ - #OPTIONS_GHC -Wall# - }
导入Numeric.GSL.Integration
导入Data.VectorSpace
导入Data.Basis
导入Data.Cross
导入Data.Derivative
导入数据。 LinearMap

类型Vec3 s =三个s

prec ::双
prec = 1E-9

f1 ::(浮动s ,VectorSpace s,Scalar s〜s)=> s - > s
f1 = id

c1 :: Double - > Vec3 Double
c1 = \ t - > linearCombo [((v 0 0 1),f1 t),((v 0 1 0),2)]

derivC :: Double - > Vec3(Double:> Double)
derivC t = c1(pureD t)

是pureD函数的实际实现,到目前为止我没有尝试使用这个代码片段进行编译。我得到以下错误:

  tests.hs:26:12:
无法匹配预期的类型`Double产品:> Double'
,实际类型为'Double'
预期类型:Vec3(Double:> Double)
实际类型:Vec3 Double
在`c1调用的返回类型'
在表达式中:c1(pureD t)
失败,模块加载:无。

还有一个使用向量空间的图形库,甚至在圆环上也有一个例子, pureD被使用。我试图推断出这个例子,但我没有看到如何将它映射到我的问题。



任何帮助将不胜感激。



预先致谢

PS:我无法发布所有我想要的链接,但愿意提供

解决方案

这是一个有趣的图书馆。感谢分享。
虽然我不明白库的概念,但是
这段代码如何:

  { - #LANGUAGE Rank2Types,TypeOperators,FlexibleContexts,TypeFamilies# - } 
模块Main其中

导入Data.LinearMap
导入Data.Maclaurin

diff ::(Double:〜>(Double,Double,Double)) - > (Double:〜>(Double,Double,Double))
diff f = \ x - > (atBasis(derivative(f x))())

eval ::(Double:〜>(Double,Double,Double)) - >双 - > (Double,Double,Double)
eval f x = powVal(f x)

f :: Double:〜> (double,double,double)
f x = tripleD(pureD 0,pureD 1,(2 * idD)x)


* Main> map(eval f)[0,0.2..1]
[(0.0,1.0,0.0),(0.0,1.0,0.4),(0.0,1.0,0.8),(0.0,1.0,1.2000000000000002),
(0.0,1.0,1.6000000000000003),(0.0,1.0,2.0000000000000004)]

* Main> map(eval(diff f))[0,0.2..1]
[(0.0,0.0,2.0),(0.0,0.0,2.0),(0.0,0.0,2.0),(0.0,0.0, 2.0),(0.0,0.0,2.0),
(0.0,0.0,2.0)]

* Main> map(eval(diff $ diff f))[0,0.2..1]
[(0.0,0.0,0.0),(0.0,0.0,0.0),(0.0,0.0,0.0),(0.0, 0.0,0.0),(0.0,0.0,0.0),(0.0,0.0,0.0)]

试试还有gx = tripleD(pureD 0,idD x,(idD * idD)x)(它似乎代表曲线(0,x,x ^ 2))。

I am working with Haskell for quite a while now, but I am far from being an expert. But I see that the functional approach to programming suits me the best.

So far I am working on a project to calculate some serious stuff, like currents and potentials radiated from a given structure.

I followed the blog written by Conal Elliott (here is some more Linear Maps) which is very nice and fundamental.

Unfortunately, I am lacking a simple example :)

To be more precise, I have a curve

f:[0,1] in R -> R³
t -> a*e_y + 2*t*e_z

which is a simple straight line at (0,a,2*t). When I want to calculate the derivative of f, e.g. for the length of the curve, I know the mathematical result, which is quite simple (0,0,2), but how do I accomplish this in Haskell, especially with the vector-space package?

I really want to use this library because of its functionality, it is exactly the approach I would have take too (but I am not that far ahead on the Haskell road)

What I have so far is this:

{-# LANGUAGE Rank2Types, TypeOperators, FlexibleContexts, TypeFamilies #-}
{-# OPTIONS_GHC -Wall #-}
import Numeric.GSL.Integration
import Data.VectorSpace
import Data.Basis
import Data.Cross
import Data.Derivative
import Data.LinearMap

type Vec3 s = Three s

prec :: Double
prec = 1E-9

f1 :: (Floating s, VectorSpace s, Scalar s ~ s) => s -> s
f1 = id

c1 :: Double -> Vec3 Double
c1  = \t -> linearCombo [((v 0 0 1),f1 t),(( v 0 1 0),2)]

derivC :: Double -> Vec3 (Double :> Double)
derivC t = c1 (pureD t)

It is the the actual implementation of the pureD function, so far nothing that I have tried works to get this snippet to compile. I get the following error:

tests.hs:26:12:
   Couldn't match expected type `Double :> Double'
               with actual type `Double'
   Expected type: Vec3 (Double :> Double)
     Actual type: Vec3 Double
   In the return type of a call of `c1'
   In the expression: c1 (pureD t)
Failed, modules loaded: none.

There is also a graphics library which uses vector-space and there is even an example on a torus, where pureD is used. I tried to deduce the example but I don't see how I can map it to my problem.

Any help would be greatly appreciated.

Thanks in advance

PS: I cannot post all the links I'd like to, but am willing to provide

解决方案

That's an interesting library.. Thanks for sharing. Although I don't understand the concept of the library yet, how about this code:

{-# LANGUAGE Rank2Types, TypeOperators, FlexibleContexts, TypeFamilies #-}
module Main where

import Data.LinearMap
import Data.Maclaurin

diff :: (Double :~> (Double,Double,Double) ) -> (Double :~> (Double,Double,Double))
diff f = \x ->  (atBasis (derivative  (f x)) ())

eval :: (Double :~> (Double,Double,Double)) -> Double -> (Double,Double,Double)
eval f x = powVal (f x)        

f :: Double :~> (Double,Double,Double)
f x = tripleD (pureD 0,pureD 1,(2*idD) x)


*Main> map (eval f) [0,0.2 .. 1]
[(0.0,1.0,0.0),(0.0,1.0,0.4),(0.0,1.0,0.8),(0.0,1.0,1.2000000000000002),
 (0.0,1.0,1.6000000000000003),(0.0,1.0,2.0000000000000004)]

*Main> map (eval (diff f)) [0,0.2 .. 1]
[(0.0,0.0,2.0),(0.0,0.0,2.0),(0.0,0.0,2.0),(0.0,0.0,2.0),(0.0,0.0,2.0), 
 (0.0,0.0,2.0)]

*Main> map (eval (diff $ diff f)) [0,0.2 .. 1]
 [(0.0,0.0,0.0),(0.0,0.0,0.0),(0.0,0.0,0.0),(0.0,0.0,0.0),(0.0,0.0,0.0),(0.0,0.0,0.0)]

Try also g x = tripleD (pureD 0,idD x,(idD*idD) x) (which seem to represent the curve (0,x,x^2)).

这篇关于衍生塔以及如何使用矢量空间包(haskell)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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