From 8de112ba7ea263bfe120416df1a94d3d21aa2327 Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Tue, 19 Nov 2024 10:24:37 +0900 Subject: [PATCH] Math Matrix multiplication fix for unbalanced array rows Test for unbalanced arrays to matrix multiplication and fix unbalanced a array --- .../tests/Convert/CoreLibsConvertMathTest.php | 30 +++++++++++++++++++ www/lib/CoreLibs/Convert/Math.php | 4 ++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/4dev/tests/Convert/CoreLibsConvertMathTest.php b/4dev/tests/Convert/CoreLibsConvertMathTest.php index 6441ca79..5df06aa0 100644 --- a/4dev/tests/Convert/CoreLibsConvertMathTest.php +++ b/4dev/tests/Convert/CoreLibsConvertMathTest.php @@ -319,6 +319,36 @@ final class CoreLibsConvertMathTest extends TestCase [6, 12, 18], ] ], + 'inblanaced [2x2,3] x [3x2]' => [ + 'a' => [ + [1, 2, 3], + [4, 5] + ], + 'b' => [ + [6, 7], + [8, 9], + [10, 11] + ], + 'result' => [ + [52, 58], + [64, 73], + ] + ], + 'inblanaced [2x3] x [3x1,2]' => [ + 'a' => [ + [1, 2, 3], + [4, 5, 7] + ], + 'b' => [ + [7, 8], + [9, 10], + [11] + ], + 'result' => [ + [58, 28], + [150, 82], + ] + ], ]; } diff --git a/www/lib/CoreLibs/Convert/Math.php b/www/lib/CoreLibs/Convert/Math.php index 41eb7463..daf4bf07 100644 --- a/www/lib/CoreLibs/Convert/Math.php +++ b/www/lib/CoreLibs/Convert/Math.php @@ -158,6 +158,8 @@ class Math * [0, 0, 0] <- automatically added * ] * + * The same is done for unbalanced entries, they are filled with 0 + * * @param array> $a m x n matrice * @param array> $b n x p matrice * @@ -186,7 +188,7 @@ class Math // so that we can multiply row by row $bCols = array_map( callback: fn ($k) => array_map( - (fn ($i) => is_array($i) ? $i[$k] : 0), + (fn ($i) => is_array($i) ? $i[$k] ?? 0 : 0), $b, ), array: array_keys($b[0]),