# Constraint 2d array using SUM operator

**URL:** <https://verificationacademy.com/forums/t/constraint-2d-array-using-sum-operator/51672>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, constraint\
**Created:** [June 15, 2026, 6:49am UTC](https://verificationacademy.com/forums/t/constraint-2d-array-using-sum-operator/51672 "2026-06-15T06:49:43Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![verifcurious](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/v/f1d935/32.png) [@verifcurious](https://verificationacademy.com/forums/u/verifcurious)\
**Post date:** [June 15, 2026, 6:49am UTC](https://verificationacademy.com/forums/t/constraint-2d-array-using-sum-operator/51672/1 "2026-06-15T06:49:43Z")

</div>

Hi, I am trying to solve this question with sum() operator. Tried below, but did not work.

Write a constraint to generate mxn(m,n\<-9) matrix such that all rows and collumns have a unique numbers between 1-9 USINg SUM operator

I was able to do it in other ways, but curious if we can achieve it with sum()

```auto
module top;

class A;
rand bit [7:0] a

;
rand int rows,cols;
constraint arr_c {

rows<=9;
cols<=9;
a.size() <= rows;
a[i].size() == cols;

foreach(a[i,j]) {
  a[i].sum() with ((item==a[i])?1:0) == 1;////each item repeats once
 // a.sum with (item.index(2)==j) ? (item==a[i,j]?1:0) :0) ==1;//THis doesnt work

}
}
endclass
A a_h;
initial begin
repeat (10) begin
a_h = new();
assert(a_h.randomize() );
$display(“ *****************************list = %0p**************************************** ”, a_h.a);
end
end
endmodule

```
