# More fun with Derivatives of Matrices

Robin Becker robin at jessikat.fsnet.co.uk
Fri Jun 8 18:43:32 CEST 2001

```In article <mailman.992008393.2590.python-list at python.org>,
PoulsenL at capecon.com writes
>
>    I have a very complex function for which I would like to take the
>    first derivative.  The problem is that involves the inverse of a
>    Matrix.
>
>    DerivVar chokes on Numpy's inverse because it is not implemented in
>    matfunc.py module with high hopes.  Unfortunately, these hopes were
>    soon dashed due to the absence of the __float__ attribute in the
>    DerivVar object.
>
>
>
>    Any advice would be greatly appreciated.
>
>
>
>    C = matfunc.makeMat([[DerivVar(1.0,0),2],[3,5]])
>
>    >>> C.mmul(C.inverse())
>
>    Traceback (most recent call last):
>
>      File "<interactive input>", line 1, in ?
>
>      File "C:\Documents and Settings\PoulsenL\Desktop\PyMCMC\matfunc.py
>    ", line 183, in inverse
>
>        return self.solve( eye(self.rows) )
>
>      File "C:\Documents and Settings\PoulsenL\Desktop\PyMCMC\matfunc.py
>    ", line 143, in solve
>
>        if isinstance(b,Mat): return makeMat( map(self.solve, b.tr()) ).
>    tr()
>
>      File "C:\Documents and Settings\PoulsenL\Desktop\PyMCMC\matfunc.py
>    ", line 145, in solve
>
>        x = self._solve( b )
>
>      File "C:\Documents and Settings\PoulsenL\Desktop\PyMCMC\matfunc.py
>    ", line 139, in _solve
>
>        Q, R = self.qr()
>
>      File "C:\Documents and Settings\PoulsenL\Desktop\PyMCMC\matfunc.py
>    ", line 127, in qr
>
>        v, beta = R.tr()[i].house(i)
>
>      File "C:\Documents and Settings\PoulsenL\Desktop\PyMCMC\matfunc.py
>    ", line 81, in house
>
>        v = Vec( Elementwise([0]*index).concat(self[index:])
>    ).normalize()
>
>      File "C:\Documents and Settings\PoulsenL\Desktop\PyMCMC\matfunc.py
>    ", line 72, in normalize
>
>        def normalize( self ):  return self / self.norm()
>
>      File "C:\Documents and Settings\PoulsenL\Desktop\PyMCMC\matfunc.py
>    ", line 71, in norm
>
>        def norm( self ):  return math.sqrt(abs( self.dot(self.conjugate
>    ()) ))
>
>    AttributeError: DerivVar instance has no attribute '__float__'
>
>    >>>
>
>
>
>    Loren Poulsen
if your matrix is really just

[ x 2 ]
[ 3 5 ]

then the differential is

- [5   -2]  [ 1  0] [5   -2]
[-3   x]  [ 0  0] [-3   x]
---------
(5x-6)**2

but I already said that you need the numerical -inv(A)*dA/dx*inv(A), but
if you want to do the numeric differentiation  like ADIFOR