This file is indexed.

/usr/share/pyshared/brian/tools/autodiff.py is in python-brian 1.3.1-1build1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
'''
Automatic differentiation.
(C) R. Brette 2008
This file is released under the terms of the BSD licence
--------------------------------------------------------
Method: forward accumulation by operator overloading.

Use::
  df=differentiate(f)                  # where f is a single-variable function
  y=differentiate(f,x0)                # differentiate at x=x0
  y=differentiate(f,x0,order=2)        # second-order derivative at x=x0
  y=differentiate(f,(a,b),order=(1,2)) # d2f/dxdy(a,b)
  y=differentiate(f,order=(1,2))       # d2f/dxdy
  
  a0,a1,a2=taylor_series(f,x0,order=2) # 2nd order Taylor series of f at x0
  J=gradient(f,(a,b))                  # gradient of f at (a,b) (returns a list)
  H=hessian(f,(a,b))                   # hessian of f at (a,b) (returns a numpy array)
  
TODO:
* replace x by x0
* merge HigherDifferentiable in Differentiable?
* simplify code
* gradient, taylor_series, hessian: x0 should be optional

* iadd, etc. (more operators)
* NonDifferentiableException and maybe use this on conditions (when cmp=0)
* Differential operators: Laplacian...
* gradient, taylor_series, hessian: x0 should be optional
* test with units, functions Rp->something, alternative differential operators,
  differential (f:Rp->Rn), complex numbers, arithmetic derivative
* remove most dependency with numpy (except Hessian)
* implicitly defined functions

If several partial derivatives of the same function need to be
calculated, it is faster to use the lower level object Differentiable.
Example::
  y=f(Differentiable('x',3),Differentiable('y',2))
will return a Differentiable object with y.val=f(3,2)
and y.diff={'x': df/dx(3,2), 'y': df/dy(3,2)}
'''

from numpy import exp, sin, cos, log, zeros # todo: look in frame instead?
import types

__all__ = ['differentiate', 'Differentiable', 'taylor_series', 'gradient', 'hessian']

def differentiate(f, x=None, order=1):
    '''
    Calculate the derivative of f at point x.
    Higher-order derivatives are possible.
    Ex.: differentiate(lambda x:3*x*x+2,x=2,order=2)
    (Returns: 6.0)
    
    Partial derivatives:
    1) Give a tuple with the order.
    Ex.: differentiate(lambda x,y:x*y+2*y+1,x=(1,2),order=(0,1))
    (Returns: 3.0 (partial derivative d/dy) )
    2) Give a dictionnary with the order:
    Ex.: differentiate(lambda x,y:x*y+2*y+1,x=(1,2),order={'y':1})
    '''
    if x is None:
        return lambda x:differentiate(f, x, order)
    if (type(order) == types.ListType) or (type(order) == types.TupleType): # several variables
        # Build the list of arguments
        n = sum(order) # total order
        args = [HigherDifferentiable(i, x[i], n) for i in range(len(x))]
        y = f(*args)
        for i in xrange(len(x)):
            for _ in xrange(order[i]):
                if isinstance(y, Differentiable) and (i in y.diff):
                    y = y.diff[i]
                else: # constant
                    return 0.
        return y
    elif (type(order) == types.DictType): # named variables
        # Build the list of arguments
        n = sum(order.itervalues())
        vars = list(f.func_code.co_varnames)
        args = []
        args.extend(x)
        for name in order.iterkeys():
            i = vars.index(name)
            args[i] = HigherDifferentiable(name, x[i], n)
        y = f(*args)
        for name, n in order.iteritems():
            for j in range(n):
                if isinstance(y, Differentiable) and (name in y.diff):
                    y = y.diff[name]
                else: # constant
                    return 0.
        return y
    elif order == 0:
        return f(x)
    elif order == 1:
        y = f(Differentiable('x', x))
        if isinstance(y, Differentiable) and ('x' in y.diff):
            return y.diff['x']
        else: # constant
            return 0.
    else:
        return differentiate(lambda y:differentiate(f, y, order=order - 1), x)

def taylor_series(f, x, order=1):
    '''
    Returns the list of coefficients of the Taylor Series of f
    at x (scalar).
    '''
    y = f(HigherDifferentiable('x', x, order))
    series = []
    n = 1.
    for i in range(order + 1):
        # Value
        val = y
        while isinstance(val, Differentiable):
            val = val.val
        series.append(val / n)
        n *= (i + 1)
        # Next order
        if isinstance(y, Differentiable) and ('x' in y.diff):
            y = y.diff['x']
        else:
            y = 0.
    return series

def gradient(f, x):
    '''
    Gradient of f at x (= tuple).
    Result = list.
    '''
    args = [Differentiable(i, val=x[i]) for i in range(len(x))]
    y = f(*args)
    if isinstance(y, Differentiable):
        result = []
        for i in range(len(x)):
            if i in y.diff:
                result.append(y.diff[i])
            else:
                result.append(0.)
        return result
    else: # constant
        return [0.] * len(x)

def hessian(f, x):
    '''
    Hessian of f at x (= tuple).
    Result = array.
    N.B.: not working with vectors.
    '''
    args = [HigherDifferentiable(i, x[i], 2) for i in range(len(x))]
    y = f(*args)
    result = zeros((len(x), len(x)))
    if isinstance(y, Differentiable): # non-zero Hessian
        for i in range(len(x)):
            if i in y.diff and isinstance(y.diff[i], Differentiable):
                for j in range(len(x)):
                    if j in y.diff[i].diff:
                        result[i, j] = y.diff[i].diff[j]
    return result


class Differentiable(object):
    '''
    A differentiable variable.
    Implemented operations: +,-,*,[],len,exp,sin,cos,/,abs,sqrt,comparisons,**
    '''
    def __init__(self, name=None, val=0.):
        '''
        Initializes a variable and its derivative.
        If the name is None, then it is a constant.
        '''
        self.val = val # value
        if name == None: # constant
            self.diff = {} # derivative
        else:
            self.diff = {name:1.} # or jacobian? (eye)

    def sqrt(self):
        return self ** .5

    def __cmp__(self, x):
        if isinstance(x, Differentiable):
            return cmp(self.val, x.val)
        else:
            return cmp(self.val, x)

    def __abs__(self):
        # NOT WORKING WITH VECTORS
        if self.val == 0:
            # TODO: specific exception
            raise ZeroDivisionError, "x:abs(x) is not differentiable at x=0."
        elif self.val > 0:
            return self
        else:
            return - self

    def __add__(self, y):
        # VECTOR-READY
        if isinstance(y, Differentiable):
            zdict = {}
            zdict.update(self.diff)
            zdict.update(y.diff)
            for key in zdict.iterkeys():
                if (key in self.diff) and (key in y.diff):
                    zdict[key] = self.diff[key] + y.diff[key]
            z = Differentiable()
            z.val = self.val + y.val
            z.diff = zdict
            return z
        else: # Adding a constant
            z = Differentiable(val=self.val + y)
            z.diff = self.diff
            return z

    def __radd__(self, x):
        # VECTOR-READY
        if isinstance(x, Differentiable):
            zdict = {}
            zdict.update(self.diff)
            zdict.update(x.diff)
            for key in zdict.iterkeys():
                if (key in self.diff) and (key in x.diff):
                    zdict[key] = x.diff[key] + self.diff[key]
            z = Differentiable()
            z.val = x.val + self.val
            z.diff = zdict
            return z
        else: # Adding a constant
            z = Differentiable(val=x + self.val)
            z.diff = self.diff
            return z

    def __mul__(self, y):
        # !Not working with vectors!
        if isinstance(y, Differentiable):
            #TODO: with vectors
            zdict = {}
            zdict.update(self.diff)
            zdict.update(y.diff)
            for key in zdict.iterkeys():
                if (key in self.diff) and (key in y.diff):
                    zdict[key] = self.diff[key] * y.val + self.val * y.diff[key]
                elif (key in self.diff):
                    zdict[key] = self.diff[key] * y.val
                else:
                    zdict[key] = self.val * y.diff[key]
            z = Differentiable()
            z.val = self.val * y.val
            z.diff = zdict
            return z
        else: # Multiplying by a constant
            z = Differentiable(val=self.val * y)
            for key in self.diff:
                z.diff[key] = self.diff[key] * y
            return z

    def __rmul__(self, x):
        if isinstance(x, Differentiable):
            #TODO: with vectors
            zdict = {}
            zdict.update(self.diff)
            zdict.update(x.diff)
            for key in zdict.iterkeys():
                if (key in self.diff) and (key in x.diff):
                    zdict[key] = x.val * self.diff[key] + x.diff[key] * self.val
                elif (key in self.diff):
                    zdict[key] = x.val * self.diff[key]
                else:
                    zdict[key] = x.diff[key] * self.val
            z = Differentiable()
            z.val = x.val * self.val
            z.diff = zdict
            return z
        else: # Multiplying by a constant
            z = Differentiable(val=x * self.val)
            for key in self.diff:
                z.diff[key] = x * self.diff[key]
            return z

    def __div__(self, x):
        return self * (x ** -1)

    def __rdiv__(self, x):
        return x * (self ** -1)

    def __sub__(self, y):
        # VECTOR-READY
        if isinstance(y, Differentiable):
            zdict = {}
            zdict.update(self.diff)
            zdict.update(y.diff)
            for key in zdict.iterkeys():
                if (key in self.diff) and (key in y.diff):
                    zdict[key] = self.diff[key] - y.diff[key]
            z = Differentiable()
            z.val = self.val - y.val
            z.diff = zdict
            return z
        else: # Subtracting a constant
            z = Differentiable(val=self.val - y)
            z.diff = self.diff
            return z

    def __rsub__(self, x):
        # VECTOR-READY
        if isinstance(x, Differentiable):
            zdict = {}
            zdict.update(self.diff)
            zdict.update(x.diff)
            for key in zdict.iterkeys():
                if (key in self.diff) and (key in x.diff):
                    zdict[key] = x.diff[key] - self.diff[key]
            z = Differentiable()
            z.val = x.val - self.val
            z.diff = zdict
            return z
        else: # Subtracting a constant
            z = Differentiable(val=x - self.val)
            for key in self.diff:
                z.diff[key] = -self.diff[key]
            return z

    def __neg__(self):
        # VECTOR-READY
        z = Differentiable(val= -self.val)
        for key in self.diff:
            z.diff[key] = -self.diff[key]
        return z

    def __pow__(self, x):
        # NOT WORKING WITH VECTORS
        if isinstance(x, Differentiable):
            return exp(x * log(self))
        elif x == 0:
            z = Differentiable(val=self.val ** x)
            for key in self.diff:
                z.diff[key] = 0 * self.diff[key]
            return z
        else:
            z = Differentiable(val=self.val ** x)
            for key in self.diff:
                z.diff[key] = x * self.val ** (x - 1) * self.diff[key]
            return z

    def __rpow__(self, x):
        # NOT WORKING WITH VECTORS
        return exp(self * log(x))

    def __getitem__(self, i):
        z = Differentiable(val=self.val[i])
        z.diff = {}
        for key in self.diff.iterkeys():
            z.diff[key] = self.diff[key][i, :]
        return z

    def __len__(self):
        return len(self.val)

    def __str__(self):
        s = str(self.val)
        for key, value in self.diff.iteritems():
            s += ' ; d/d' + key + '=' + str(value)
        return s

    def exp(self):
        # !! Not working for vectors !!
        zdict = {}
        zdict.update(self.diff)
        for key in zdict.iterkeys():
            zdict[key] = exp(self.val) * self.diff[key]
        z = Differentiable(val=exp(self.val))
        z.diff = zdict
        return z

    def cos(self):
        # !! Not working for vectors !!
        zdict = {}
        zdict.update(self.diff)
        for key in zdict.iterkeys():
            zdict[key] = -sin(self.val) * self.diff[key]
        z = Differentiable(val=cos(self.val))
        z.diff = zdict
        return z

    def sin(self):
        # !! Not working for vectors !!
        zdict = {}
        zdict.update(self.diff)
        for key in zdict.iterkeys():
            zdict[key] = cos(self.val) * self.diff[key]
        z = Differentiable(val=sin(self.val))
        z.diff = zdict
        return z

    def log(self):
        # !! Not working for vectors !!
        zdict = {}
        zdict.update(self.diff)
        for key in zdict.iterkeys():
            zdict[key] = (1. / (self.val)) * self.diff[key]
        z = Differentiable(val=log(self.val))
        z.diff = zdict
        return z


class HigherDifferentiable(Differentiable):
    '''
    A differentiable variable with order n.
    '''
    def __init__(self, name=None, val=0., order=1):
        Differentiable.__init__(self, name, val)
        if order > 1:
            self.val = HigherDifferentiable(name, val, order - 1)

if __name__ == '__main__':
    print "Derivative:"
    print "> differentiate(lambda x:3*x*x+2,x=2,order=2)"
    print differentiate(lambda x:3 * x * x + 2, x=2, order=2)
    print "> differentiate(lambda x:3*x*x+2,order=2)(2)"
    print differentiate(lambda x:3 * x * x + 2, order=2)(2)
    print "Partial derivative:"
    print "> differentiate(lambda x,y:x*y+2*y+1,x=(1,2),order=(0,1))"
    print differentiate(lambda x, y:x * y + 2 * y + 1, x=(1, 2), order=(0, 1))