當使用衍生塔的vector-space包(請參閱derivative towers)時,我遇到了區分積分的需求。 從數學是很清楚如何實現這一點:與函數如何區分積分與向量空間庫(haskell)
g : R -> R
例如
f(x) = int g(y) dy from 0 to x
。
關於x的導數是:
f'(x) = g(x)
我試圖通過先定義一個類 「一體化」 得到這個行爲
class Integration a b where
--standard integration function
integrate :: (a -> b) -> a -> a -> b
一個基本的例子是
instance Integration Double Double where
integrate f a b = fst $ integrateQAGS prec 1000 f a b
與integrateQAGS
from hmatrix
問題帶有b值代表衍生物的塔:
instance Integration Double (Double :> (NC.T Double)) where
integrate = integrateD
NC.T
是從Numeric.Complex(數字-前奏)。 功能integrateD
定義如下(但錯誤):
integrateD ::(Integration a b, HasTrie (Basis a), HasBasis a, AdditiveGroup b) => (a -> a :> b) -> a -> a -> (a :> b)
integrateD f l u = D (integrate (powVal . f) l u) (derivative $ f u)
的函數不返回我想要的東西,它的來源積,而不是整體。問題是,我需要一個線性地圖,返回f u
。該a :> b
定義如下:
data a :> b = D { powVal :: b, derivative :: a :-* (a :> b) }
我不知道如何定義derivative
。任何幫助將不勝感激,謝謝
編輯:
我忘了提供Integration Double (NC.T Double)
實例:
instance Integration Double (NC.T Double) where
integrate f a b = bc $ (\g -> integrate g a b) <$> [NC.real . f, NC.imag . f]
where bc (x:y:[]) = x NC.+: y
,我可以給我的意思的例子: 比方說,我有一個函數
f(x) = exp(2*x)*sin(x)
>let f = \x -> (Prelude.exp ((pureD 2.0) AR.* (idD x))) * (sin (idD x)) :: Double :> Double
(AR * *)表示代數的乘法。環(數字-前奏)
我很容易這個功能與上面的功能integrateD
整合:
>integrateD f 0 1 :: Double :> Double
D 1.888605715258933 ...
當我看一看f的衍生物:
f'(x) = 2*exp(2*x)*sin(x)+exp(2*x)*cos(x)
並評估此在0
和pi/2
我得到1
和一些值:
> derivAtBasis (f 0.0)()
D 1.0 ...
> derivAtBasis (f (pi AF./ 2))()
D 46.281385265558534 ...
現在,獲得積分的時候,我得到的功能f
的推導而不是它的上限
> derivAtBasis (integrate f 0 (pi AF./ 2))()
D 46.281385265558534 ...
但我值期待:
> f (pi AF./ 2)
D 23.140692632779267 ...
是的,這是真的。但是,當我在'Double:> Double'值上使用'powVal'函數時,它可以工作。但是,當然,我失去了有關衍生物的信息。我必須明確提供這些信息,這就是我卡住的地方:( – TheMADMAN 2012-07-11 19:01:39