* @brief Computes the inverse of a 4x4 matrix with the layout * [ a 0 0 b ] * [ 0 c 0 0 ] * [ 0 0 d 0 ] * [ b 0 0 e ] * * @param metric af::array with the shape af::dims4(4, 4, M, N) * * @return af::array with the shape af::dims4(4, 4, M, N) */
| 428 | * @return af::array with the shape af::dims4(4, 4, M, N) |
| 429 | */ |
| 430 | af::array inv_metric(const af::array& metric) { |
| 431 | af::array a = metric(0, 0, af::span); |
| 432 | af::array b = metric(3, 0, af::span); |
| 433 | af::array c = metric(1, 1, af::span); |
| 434 | af::array d = metric(2, 2, af::span); |
| 435 | af::array e = metric(3, 3, af::span); |
| 436 | |
| 437 | af::array det = b * b - a * e; |
| 438 | |
| 439 | auto res = af::constant(0, 4, 4, metric.dims()[2], metric.dims()[3], f64); |
| 440 | |
| 441 | res(0, 0, af::span) = -e / det; |
| 442 | res(0, 3, af::span) = b / det; |
| 443 | res(3, 0, af::span) = b / det; |
| 444 | res(1, 1, af::span) = 1.0 / c; |
| 445 | res(2, 2, af::span) = 1.0 / d; |
| 446 | res(3, 3, af::span) = -a / det; |
| 447 | |
| 448 | return res; |
| 449 | } |
| 450 | |
| 451 | /** |
| 452 | * @brief Computes the 4x4 metric matrix for the given 4-vector positions |