automatic-differentiation

Is there any working implementation of reverse mode automatic differentiation for Haskell?

安稳与你 提交于 2019-12-03 10:48:34
The closest-related implementation in Haskell I have seen is the forward mode at http://hackage.haskell.org/packages/archive/fad/1.0/doc/html/Numeric-FAD.html . The closest related related research appears to be reverse mode for another functional language related to Scheme at http://www.bcl.hamilton.ie/~qobi/stalingrad/ . I see reverse mode in Haskell as kind of a holy grail for a lot of tasks, with the hopes that it could use Haskell's nested data parallelism to gain a nice speedup in heavy numerical optimization. In response to this question, I've uploaded a package named ad to Hackage for

Why don't C++ compilers do better constant folding?

筅森魡賤 提交于 2019-12-03 02:08:16
问题 I'm investigating ways to speed up a large section of C++ code, which has automatic derivatives for computing jacobians. This involves doing some amount of work in the actual residuals, but the majority of the work (based on profiled execution time) is in calculating the jacobians. This surprised me, since most of the jacobians are propagated forward from 0s and 1s, so the amount of work should be 2-4x the function, not 10-12x. In order to model what a large amount of the jacobian work is

Numeric.AD and typing problem

丶灬走出姿态 提交于 2019-12-01 16:31:34
I'm trying to work with Numeric.AD and a custom Expr type. I wish to calculate the symbolic gradient of user inputted expression. The first trial with a constant expression works nicely: calcGrad0 :: [Expr Double] calcGrad0 = grad df vars where df [x,y] = eval (env [x,y]) (EVar "x"*EVar "y") env vs = zip varNames vs varNames = ["x","y"] vars = map EVar varNames This works: >calcGrad0 [Const 0.0 :+ (Const 0.0 :+ (EVar "y" :* Const 1.0)),Const 0.0 :+ (Const 0.0 :+ (EVar "x" :* Const 1.0))] However, if I pull the expression out as a parameter: calcGrad1 :: [Expr Double] calcGrad1 = calcGrad1'

How to do automatic differentiation on hmatrix?

旧巷老猫 提交于 2019-11-30 21:37:51
Sooooo ... as it turns out going from fake matrices to hmatrix datatypes turns out to be nontrivial :) Preamble for reference: {-# LANGUAGE RankNTypes #-} {-# LANGUAGE ParallelListComp #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE FlexibleContexts #-} import Numeric.LinearAlgebra.HMatrix import Numeric.AD reconstruct :: (Container Vector a, Num (Vector a)) => [a] -> [Matrix a] -> Matrix a reconstruct as φs = sum [ a `scale` φ | a <- as | φ <- φs ] preserveInfo :: (Container Vector a, Num (Vector a)) => Matrix a -> [a] -> [Matrix a] -> a preserveInfo img as

Java - Computation of Derivations with Apache Commons Mathematic Library

≡放荡痞女 提交于 2019-11-30 10:33:53
I have a problem in using the apache commons math library. I just want to create functions like f(x) = 4x^2 + 2x and I want to compute the derivative of this function --> f'(x) = 8x + 2 I read the article about Differentiation ( http://commons.apache.org/proper/commons-math/userguide/analysis.html , section 4.7). There is an example which I don't understand: int params = 1; int order = 3; double xRealValue = 2.5; DerivativeStructure x = new DerivativeStructure(params, order, 0, xRealValue); DerivativeStructure y = f(x); //COMPILE ERROR System.out.println("y = " + y.getValue(); System.out

Java - Computation of Derivations with Apache Commons Mathematic Library

做~自己de王妃 提交于 2019-11-29 15:50:06
问题 I have a problem in using the apache commons math library. I just want to create functions like f(x) = 4x^2 + 2x and I want to compute the derivative of this function --> f'(x) = 8x + 2 I read the article about Differentiation (http://commons.apache.org/proper/commons-math/userguide/analysis.html, section 4.7). There is an example which I don't understand: int params = 1; int order = 3; double xRealValue = 2.5; DerivativeStructure x = new DerivativeStructure(params, order, 0, xRealValue);

Differential Operator usable in Matrix form, in Python module Sympy

倖福魔咒の 提交于 2019-11-29 11:07:26
We need two matrices of differential operators [B] and [C] such as: B = sympy.Matrix([[ D(x), D(y) ], [ D(y), D(x) ]]) C = sympy.Matrix([[ D(x), D(y) ]]) ans = B * sympy.Matrix([[x*y**2], [x**2*y]]) print ans [x**2 + y**2] [ 4*x*y] ans2 = ans * C print ans2 [2*x, 2*y] [4*y, 4*x] This could also be applied to calculate the curl of a vector field like: culr = sympy.Matrix([[ D(x), D(y), D(z) ]]) field = sympy.Matrix([[ x**2*y, x*y*z, -x**2*y**2 ]]) To solve this using Sympy the following Python class had to be created: import sympy class D( sympy.Derivative ): def __init__( self, var ): super( D

How to do automatic differentiation on complex datatypes?

老子叫甜甜 提交于 2019-11-28 10:32:24
Given a very simple Matrix definition based on Vector: import Numeric.AD import qualified Data.Vector as V newtype Mat a = Mat { unMat :: V.Vector a } scale' f = Mat . V.map (*f) . unMat add' a b = Mat $ V.zipWith (+) (unMat a) (unMat b) sub' a b = Mat $ V.zipWith (-) (unMat a) (unMat b) mul' a b = Mat $ V.zipWith (*) (unMat a) (unMat b) pow' a e = Mat $ V.map (^e) (unMat a) sumElems' :: Num a => Mat a -> a sumElems' = V.sum . unMat (for demonstration purposes ... I am using hmatrix but thought the problem was there somehow) And an error function ( eq3 ): eq1' :: Num a => [a] -> [Mat a] -> Mat

Avoid sorting args in Python module Sympy

≯℡__Kan透↙ 提交于 2019-11-28 09:15:29
问题 I am currently developing a differential operator for sympy that can be placed in matricial form. In this case the order of the args list when creating a Mul object is very important to guarantee that the differentiation is performed where it is required only. The issue is that, when the following is done: input = (t,z,x) Mul(*input).args It returns (t, x, z) because some rearrangement in args took place. How to avoid args to be sorted? 回答1: Why is the arg ordering important for it to be

Differential Operator usable in Matrix form, in Python module Sympy

六眼飞鱼酱① 提交于 2019-11-28 04:33:47
问题 We need two matrices of differential operators [B] and [C] such as: B = sympy.Matrix([[ D(x), D(y) ], [ D(y), D(x) ]]) C = sympy.Matrix([[ D(x), D(y) ]]) ans = B * sympy.Matrix([[x*y**2], [x**2*y]]) print ans [x**2 + y**2] [ 4*x*y] ans2 = ans * C print ans2 [2*x, 2*y] [4*y, 4*x] This could also be applied to calculate the curl of a vector field like: culr = sympy.Matrix([[ D(x), D(y), D(z) ]]) field = sympy.Matrix([[ x**2*y, x*y*z, -x**2*y**2 ]]) To solve this using Sympy the following Python