From ed6e024eb986aa4767791d0c1afdebb5cbf4138f Mon Sep 17 00:00:00 2001 From: LittlePyx <627875273@qq.com> Date: Tue, 1 Dec 2026 17:53:06 +0800 Subject: [PATCH] Fix matrix_power for even exponents --- include/xtensor-blas/xlinalg.hpp | 2 +- test/test_linalg.cpp | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/include/xtensor-blas/xlinalg.hpp b/include/xtensor-blas/xlinalg.hpp index 283a9a3..665c5b2 100644 --- a/include/xtensor-blas/xlinalg.hpp +++ b/include/xtensor-blas/xlinalg.hpp @@ -1487,7 +1487,7 @@ namespace xt while (~n & (1 << i)) { blas::gemm(mat, mat, temp); - temp = res; + mat = temp; ++i; } ++i; diff --git a/test/test_linalg.cpp b/test/test_linalg.cpp index 7e53270..d0e6a10 100644 --- a/test/test_linalg.cpp +++ b/test/test_linalg.cpp @@ -66,6 +66,19 @@ namespace xt CHECK(allclose(t5res, t5expected)); } + TEST_CASE("matrix_power_even_exponents") + { + xarray a = {{1, 1, 0}, {1, 0, 1}, {0, 0, 0}}; + xarray fourth = {{5, 3, 2}, {3, 2, 1}, {0, 0, 0}}; + xarray sixth = {{13, 8, 5}, {8, 5, 3}, {0, 0, 0}}; + CHECK_EQ(linalg::matrix_power(a, 4), fourth); + CHECK_EQ(linalg::matrix_power(a, 6), sixth); + + xarray b = {{1, 1}, {0, 1}}; + xarray inverse_fourth = {{1, -4}, {0, 1}}; + CHECK_EQ(linalg::matrix_power(b, -4), inverse_fourth); + } + TEST_CASE("det") { xarray a = {{1, 2}, {3, 4}};