MINVERSE of a 2x2 matrix returns a matrix of zeros
Author: PhilflowIOCreated Sep 20, 2026Updated Sep 20, 2026
Description
det (calc.go:4983) handles len(sqMtx) == 2 and the Laplace expansion for anything larger. There is no case for a 1x1 matrix. The cofactors of a 2x2 matrix are 1x1 determinants, so they fall into the expansion, which asks minor for the minor of a 1x1 matrix, gets an empty matrix back and sums over nothing. Every cofactor comes out as 0, so MINVERSE of a 2x2 matrix is all zeros.
MDETERM is not affected, because det has its own branch for a 2x2 matrix and never descends.
There is no error. The formula returns zeros.
The existing expectation "MINVERSE(A1:B2)": "-0" in calc_test.go pins this, so it has to change with the fix.
Steps to reproduce the issue
package main
import (
"fmt"
"github.com/xuri/excelize/v2"
)
func main() {
f := excelize.NewFile()
// A1:B2 is {{1,4},{2,5}}, determinant -3
f.SetCellValue("Sheet1", "A1", 1)
f.SetCellValue("Sheet1", "B1", 4)
f.SetCellValue("Sheet1", "A2", 2)
f.SetCellValue("Sheet1", "B2", 5)
for _, formula := range []string{
`=INDEX(MINVERSE(A1:B2),1,1)`,
`=INDEX(MINVERSE(A1:B2),1,2)`,
`=INDEX(MINVERSE(A1:B2),2,1)`,
`=INDEX(MINVERSE(A1:B2),2,2)`,
`=MDETERM(A1:B2)`,
} {
f.SetCellFormula("Sheet1", "D1", formula)
result, err := f.CalcCellValue("Sheet1", "D1")
fmt.Printf("%-30s %-20q %v\n", formula, result, err)
}
}Describe the results you received
=INDEX(MINVERSE(A1:B2),1,1) "-0" <nil>
=INDEX(MINVERSE(A1:B2),1,2) "0" <nil>
=INDEX(MINVERSE(A1:B2),2,1) "0" <nil>
=INDEX(MINVERSE(A1:B2),2,2) "-0" <nil>
=MDETERM(A1:B2) "-3" <nil>Describe the results you expected
The inverse is 1/det times {{5,-4},{-2,1}} with det = -3:
=INDEX(MINVERSE(A1:B2),1,1) "-1.66666666666667" <nil>
=INDEX(MINVERSE(A1:B2),1,2) "1.33333333333333" <nil>
=INDEX(MINVERSE(A1:B2),2,1) "0.666666666666667" <nil>
=INDEX(MINVERSE(A1:B2),2,2) "-0.333333333333333" <nil>
=MDETERM(A1:B2) "-3" <nil>Go version
go version go1.26.8 linux/amd64
Excelize version or commit ID
0434413
Environment
GOOS='linux'
GOARCH='amd64'
CGO_ENABLED='1'
GOVERSION='go1.26.8'Source: qax-os/excelize