By the way, if you don't actually need any specific attributes of
matrices other than non-commutativity (and you're assuming everything
is square n x n), you can already do this in SymPy:
In [10]: a, b = symbols('a b', commutative=False)
In [15]: diff(f(a)*g(a), a)
Out[15]:
d d
f(a)⋅──(g(a)) + ──(f(a))⋅g(a)
da da
In [16]: diff(g(a)*f(a), a)
Out[16]:
d d
g(a)⋅──(f(a)) + ──(g(a))⋅f(a)
da da
The inverse rule you mentioned works too, but unfortunately there's a
bug in the printer, so the output looks strage (but the actual object
is correct):
In [21]: print diff(f(a)**-1, a).args
(-1, 1/f(a), Derivative(f(a), a), 1/f(a))
In [18]: diff(f(a)**-1, a)
Out[18]:
d
-──(f(a))
da
─────────
f(a)⋅f(a)
In [19]: print diff(f(a)**-1, a)
-Derivative(f(a), a)/(f(a)*f(a))
I created http://code.google.com/p/sympy/issues/detail?id=2532 for this.
Regarding the symbolic matrix class that Matthew is working on, I
suppose it should allow to define a set of symbols that it depends on,
so that differentiation works. Or maybe it should just allow abstract
differentiation, using .diff() (with no arguments).
Aaron Meurer
On Thu, Jun 30, 2011 at 1:40 AM, Amit <[email protected]> wrote:
> Hello Aaron,
>
> Thanks for pointing out this thread (I did search the group but missed
> it):
> http://groups.google.com/group/sympy/browse_thread/thread/7a4ab2af17b3c10d/90ba1a6bebb59eb9
>
> I am talking about a similar idea but I am looking also for calculus
> operations like differentiation.
>
> Thanks,
> Amit
>
> On Jun 30, 9:37 am, Aaron Meurer <[email protected]> wrote:
>> I think he wants to do things with symbolic matrices, like what
>> Matthew proposed in another thread. Differentiating matrices
>> symbolically is indeed tricky because you have to be careful about
>> commutativity.
>>
>> Aaron Meurer
>>
>>
>>
>>
>>
>>
>>
>> On Thu, Jun 30, 2011 at 12:21 AM, SherjilOzair <[email protected]>
>> wrote:
>> > Hello Amit,
>> > The line of code you have given is not clear. Could you give a more
>> > concrete example of a functionality that you're expecting ?
>>
>> > If you want to calculate the hessian of a function, Sympy has a
>> > hessian function which takes in a function and a list of variables of
>> > the function, and returns the hessian matrix of size m * m, where m is
>> > number of variables.
>>
>> > In [4]: f = x**2*y
>>
>> > In [5]: f
>> > Out[5]:
>> > 2
>> > x ⋅y
>>
>> > In [6]: syms = [x,y]
>>
>> > In [7]: hessian(f, syms)
>> > Out[7]:
>> > ⎡2⋅y 2⋅x⎤
>> > ⎢ ⎥
>> > ⎣2⋅x 0 ⎦
>>
>> > As to about other matrix calculus functions, it would help if you
>> > could show what exactly do you want along with the corresponding
>> > output you expect.
>>
>> > -Sherjil Ozair
>>
>> > On Jun 30, 7:10 am, Amit <[email protected]> wrote:
>> >> Hi,
>>
>> >> I am studying machine learning and lately I had to do some matrix
>> >> calculus. I am using the 'The Matrix cookbook' to calculate things
>> >> like the gradient and hessian of expressions like:
>>
>> >> {Z \alpha}^T (Z^T diag(\alpha) Z - ...)^{-1} Z \alpha
>>
>> >> where Z is a matrix and alpha a vector. This can get pretty ugly and
>> >> tedious when done by hand.
>> >> I looked for some automatic way to do this i.e. a CAS, but didn't find
>> >> any (I am looking for something that can do this symbolically and not
>> >> numerically).
>> >> I was wandering why is it so. Is it too complicated? or maybe its not
>> >> well defined?
>>
>> >> Thanks,
>> >> Amit
>>
>> > --
>> > You received this message because you are subscribed to the Google Groups
>> > "sympy" group.
>> > To post to this group, send email to [email protected].
>> > To unsubscribe from this group, send email to
>> > [email protected].
>> > For more options, visit this group
>> > athttp://groups.google.com/group/sympy?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/sympy?hl=en.
>
>
--
You received this message because you are subscribed to the Google Groups
"sympy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sympy?hl=en.