# 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:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![DK87](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/d/7c8e57/32.png) [@DK87](https://verificationacademy.com/forums/u/DK87)\
**Post date:** [May 18, 2020, 5:46pm UTC](https://verificationacademy.com/forums/t/constraint-to-make-sure-a-matrix-inverse-exists/37598/1 "2020-05-18T17:46:37Z")

</div>

Hi,

I was wondering if there is a easier way to randomize a 2-D array (matrix) so that the inverse exists?  
For a matrix to have a valid inverse:

1. It should be a square matrix.
2. A x A^-1 = I where A is our matrix, I is a identity matrix (1 along the diagonal, every other element is 0).

Any help is appreciated!

---

<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:** [May 18, 2020, 8:01pm UTC](https://verificationacademy.com/forums/t/constraint-to-make-sure-a-matrix-inverse-exists/37598/2 "2020-05-18T20:01:48Z")

</div>

_In reply to [DK87](https://verificationacademy.com/forums/t/constraint-to-make-sure-a-matrix-inverse-exists/37598):_

You can try this (and please check my dot-product math)

```verilog
module top;
  class matrix;
    rand bit signed [3:0] A[][], AI[][];
    
    constraint c_size { A.size inside {[2:4]}; A.size == AI.size; }
    constraint c_square { foreach (A[D1]) {A.size == A[D1].size;
                                           A.size == AI[D1].size; } 
                        }
    constraint c_inverse { foreach ( A[D1,D2] )
      A[D1].sum() with (int'(A[item.index][D1] * AI[D2][item.index])) == (D1 == D2); }
  endclass
      
  matrix m = new;
  initial begin
    assert(m.randomize());
    foreach (m.A[i,j]) begin
      if (j==0) $display;
      $write(m.A[i][j],,);
    end
    $write("\n-------");
    foreach (m.AI[i,j]) begin
      if (j==0) $display;
       $write(m.AI[i][j],,);
    end
    $display;
  end
      
endmodule : top

```

Do realize these constraints explode as you increase the number of bits and the size of the array. A 4X4X4 array is randomizing a 128 bit variable with constraints between every combination of bits.

---

<div class="post-metadata">

**Author:** ![DK87](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/d/7c8e57/32.png) [@DK87](https://verificationacademy.com/forums/u/DK87)\
**Post date:** [May 19, 2020, 8:34pm UTC](https://verificationacademy.com/forums/t/constraint-to-make-sure-a-matrix-inverse-exists/37598/3 "2020-05-19T20:34:39Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/constraint-to-make-sure-a-matrix-inverse-exists/37598/2):_

Thank you Dave. I might have to end up doing matrices upto 12x12 which can cause quite a overhead. I’m thinking of just randomizing it (without a constraint) and doing a check in post randomize to make sure if it’s inverse exists (redo it in case inverse doesn’t exist) in case the constraint solver takes too long.

If you have any suggestions on a implementation, what would be great!

Thanks again!

---

<div class="post-metadata">

**Author:** ![juhi\_p](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/j/e0b2c6/32.png) [@juhi\_p](https://verificationacademy.com/forums/u/juhi_p)\
**Post date:** [May 21, 2020, 5:50pm UTC](https://verificationacademy.com/forums/t/constraint-to-make-sure-a-matrix-inverse-exists/37598/4 "2020-05-21T17:50:35Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/constraint-to-make-sure-a-matrix-inverse-exists/37598/3):_  
can you please explain c\_square and c\_inverse constraints?

---

<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:** [May 22, 2020, 12:12am UTC](https://verificationacademy.com/forums/t/constraint-to-make-sure-a-matrix-inverse-exists/37598/5 "2020-05-22T00:12:27Z")

</div>

_In reply to [DK87](https://verificationacademy.com/forums/t/constraint-to-make-sure-a-matrix-inverse-exists/37598/4):_

The approach of trying completely random values and then seeing if they match the constraints might take just as long or longer. You may want to try looking for a program designed specifically for creating inverse matrices and import the results.

_In reply to [juhi\_p](https://verificationacademy.com/forums/t/constraint-to-make-sure-a-matrix-inverse-exists/37598/4):_

SystemVerilog has arrays of arrays, not really 2-dimensional arrays. The means that each element of the first dimension could have a different second dimension size. The C\_square constraint makes sure that each element of the first dimension has an array whose dimension is the same size as the first dimension (i.e number of rows and columns are equal) to make it a square array

The C\_inverse constraint is computing the dot product for each row(i)/column(j) and comparing to 0 or 1. The identity matrix is where the only row/column combination elements are true is when row(i) == column(j).

---

<div class="post-metadata">

**Author:** ![nnd](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/n/b2d939/32.png) [@nnd](https://verificationacademy.com/forums/u/nnd)\
**Post date:** [November 25, 2020, 6:35am UTC](https://verificationacademy.com/forums/t/constraint-to-make-sure-a-matrix-inverse-exists/37598/6 "2020-11-25T06:35:14Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/constraint-to-make-sure-a-matrix-inverse-exists/37598/5):_

Hi Dave,

```verilog

lets say for the following 2 matrices shown below:

A = |1 2| AI = |5 6|
    |3 4| |7 8|

Now the resultant dot product matrix would be:
Result = |(1*5+2*7) (1*6+2*8)|
         |(3*5+4*7) (3*6+4*8)|

You've shown in your code the following constraint: 
constraint c_inverse { foreach ( A[D1,D2] )
      A[D1].sum() with (int'(A[item.index][D1] * AI[D2][item.index])) == (D1 == D2); }

Can you explain how is this [item.index][D1]*[D2][item.index] mapping to the above product dot sum?

```

Thanks

---

<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.

---

<div class="post-metadata">

**Author:** ![nnd](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/n/b2d939/32.png) [@nnd](https://verificationacademy.com/forums/u/nnd)\
**Post date:** [November 25, 2020, 8:07am UTC](https://verificationacademy.com/forums/t/constraint-to-make-sure-a-matrix-inverse-exists/37598/8 "2020-11-25T08:07:32Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/constraint-to-make-sure-a-matrix-inverse-exists/37598/7):_

Thanks!!
