# [GiNaC-list] Differentiate matrix wrt vector

Orxan Shibliyev orxan.shibli at gmail.com
Wed Mar 3 13:42:30 CET 2021

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

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

int main()
{
const int DIM = 5;

symbol g("g");

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

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

auto qL = matrix(DIM, 1);

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

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

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

auto qR = matrix(DIM, 1);

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

q.mul_scalar(0.5);

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

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

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

A.diff(uL);

return 0;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.ginac.de/pipermail/ginac-list/attachments/20210303/f1c93bca/attachment.htm>
```