# Constraint to make sure a matrix inverse exists

**URL:** <https://verificationacademy.com/forums/t/constraint-to-make-sure-a-matrix-inverse-exists/37598>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, constraints, matrix\
**Created:** [May 18, 2020, 5:46pm UTC](https://verificationacademy.com/forums/t/constraint-to-make-sure-a-matrix-inverse-exists/37598 "2020-05-18T17:46:37Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [November 25, 2020, 7:30am UTC](https://verificationacademy.com/forums/t/constraint-to-make-sure-a-matrix-inverse-exists/37598/7 "2020-11-25T07:30:06Z")

</div>

_In reply to [nnd](https://verificationacademy.com/forums/t/constraint-to-make-sure-a-matrix-inverse-exists/37598/6):_  
First unroll the foreach

```verilog
A[0].sum() with (int'(A[item.index][0] * AI[0][item.index])) == (0 == 0)
A[0].sum() with (int'(A[item.index][0] * AI[1][item.index])) == (0 == 1)
A[1].sum() with (int'(A[item.index][1] * AI[0][item.index])) == (1 == 0)
A[1].sum() with (int'(A[item.index][1] * AI[1][item.index])) == (1 == 1)

```

Then expand the sum

```verilog
( A[0][0] * AI[0][0] + A[1][0] * AI[0][1] ) == 1 // 1 * 5 + 2 * 7
( A[0][0] * AI[1][0] + A[1][0] * AI[1][1] ) == 0 // 1 * 6 + 2 * 8
( A[0][1] * AI[0][0] + A[1][1] * AI[0][1] ) == 0 // 3 * 5 + 4 * 7
( A[0][1] * AI[1][0] + A[1][1] * AI[1][1] ) == 1 // 3 * 6 + 4 * 8

```

This assumes D1 is the column and D2 is the row of your matrices.

---

_[View the full topic](https://verificationacademy.com/forums/t/constraint-to-make-sure-a-matrix-inverse-exists/37598)._
