# [GiNaC-list] Differentiate matrix wrt vector

Vladimir V. Kisil V.Kisil at leeds.ac.uk
Thu Mar 4 09:40:30 CET 2021

```	Hello,

I am not sure what do you mean by "differentiate a matrix wrt to
a vector", is it a sort of gradient? In any case, if you could express your
aim through the standard partial derivatives of functions (which
GiNaC can do) you will succeed.

Best wishes,
--
Book:      Geometry of Mobius Maps       https://doi.org/10.1142/p835
Soft:      Geometry of cycles         http://moebinv.sourceforge.net/
Jupyter notebooks:       https://github.com/vvkisil/MoebInv-notebooks
>>>>> On Wed, 3 Mar 2021 14:42:30 +0200, Orxan Shibliyev <orxan.shibli at gmail.com> said:

OSh> Suppose, I have a 5x5 matrix and I want to differentiate the
OSh> matrix wrt to a 5x1 matrix in order to obtain a 3D matrix or
OSh> tensor. In the following code, A.diff(uL); cannot compile. Is
OSh> it possible to differentiate a matrix with symbolic elements
OSh> wrt to a vector which also contains symbols?

OSh> #include <iostream> #include <ginac/ginac.h> using namespace
OSh> std; using namespace GiNaC;

OSh> int main() { const int DIM = 5;

OSh>     symbol g("g");

OSh>     symbol uL1("uL1"); symbol uL2("uL2"); symbol uL3("uL3");
OSh> symbol uL4("uL4"); symbol uL5("uL5");

OSh>     matrix uL = { {uL1}, {uL2}, {uL3}, {uL4}, {uL5} };

OSh>     auto qL = matrix(DIM, 1);

OSh>     qL(0,0) = sqrt(uL1); qL(1,0) = uL2 / qL(0,0); qL(2,0) = uL3
OSh> / qL(0,0); qL(3,0) = uL4 / qL(0,0); qL(4,0) = (uL5 + (g - 1) *
OSh> (uL5 - 0.5 * (pow(uL2,2) + pow(uL3,2) + pow(uL4,2)) / uL1)) /
OSh> uL1;

OSh>     symbol uR1("uR1"); symbol uR2("uR2"); symbol uR3("uR3");
OSh> symbol uR4("uR4"); symbol uR5("uR5");

OSh>     matrix uR = { {uR1}, {uR2}, {uR3}, {uR4}, {uR5} };

OSh>     auto qR = matrix(DIM, 1);

OSh>     qR(0,0) = sqrt(uR1); qR(1,0) = uR2 / qR(0,0); qR(2,0) = uR3
OSh> / qR(0,0); qR(3,0) = uR4 / qR(0,0); qR(4,0) = (uR5 + (g - 1) *
OSh> (uR5 - 0.5 * (pow(uR2,2) + pow(uR3,2) + pow(uR4,2)) / uR1)) /
OSh> uR1;

OSh>     auto q = qL.add(qR); q.mul_scalar(0.5);

OSh>     matrix B = { {2*q(0,0), 0, 0, 0, 0}, {q(1,0), q(0,0), 0, 0,
OSh> 0}, {q(1,0), q(0,0), 0, 0, 0}, {q(2,0), 0, q(0,0), 0, 0},
OSh> {q(3,0), 0, 0, q(0,0), 0}, {q(4,0)/g, (g-1)*q(1,0)/g,
OSh> (g-1)*q(2,0)/g, (g-1)*q(3,0)/g, q(0,0)/g} };

OSh>     matrix C = { {q(1,0), q(0,0), 0, 0, 0}, {(g-1)*q(4,0)/g,
OSh> (g+1)*q(1,0)/g, (1-g)*q(2,0)/g, (1-g)*q(3,0)/g,
OSh> (g-1)*q(0,0)/g}, {0, q(2,0), q(1,0), 0, 0}, {0, q(3,0), 0,
OSh> q(1,0), 0}, {0, q(4,0), 0, 0, q(1,0)} };

OSh>     auto A = B.mul(C.inverse());

OSh>     A.diff(uL);

OSh>     return 0; }

OSh> ----------------------------------------------------
OSh> Alternatives:

OSh> ----------------------------------------------------
OSh> _______________________________________________ GiNaC-list
OSh> mailing list GiNaC-list at ginac.de
OSh> https://www.ginac.de/mailman/listinfo/ginac-list
```