2-Dimenson Dynamic array Constraints

I am trying to write some constraints for a 2-D dynamic array. The requirement being addition of the values for each row and column needs to be 30.

class hello_world;
  rand bit [7:0] a [5][4];
  

  constraint a_row {
    foreach (a[i]) a[i].sum() with (int'(item)) == 30;
  }
  constraint a_col {
    foreach (a[i]) a.sum() with (int'(a[item.index][i])) == 30;
  }
 
endclass
 
module test;
  hello_world m1;
  int temp;
  initial begin
    m1=new();
    assert(m1.randomize());
  end
endmodule

This gives me a constraint solving error :
“xmsim: *W,RNDREJ: This constraint was not processed due to: array index out of bounds
foreach (a[i]) a.sum() with (int’(a[item.index][i])) == 30; (./testbench.sv,9)”

Not quiet able to understand how is this constraint to access at the array index out of bounds.

In reply to fenil_shah:

You need to iterate over the second dimension. I would also use j as the iterator variable name so you don’t confuse it with i [,j].

 constraint a_col {
    foreach (a[,j]) a.sum() with (int'(a[item.index][j])) == 30;
  }

In reply to dave_59:

I tried that too, but now it complaints that
"xmsim: *W,RNDOCS: These constraints contribute to the set of conflicting constraints:

foreach (a[,j]) a.sum() with (int'(a[item.index][j])) == 30; (./testbench.sv,9)
foreach (a[i]) a[i].sum() with (int'(item)) == 30; (./testbench.sv,6)"

In reply to fenil_shah:

That’s a different problem. Your puzzle only has a solution if NRow*SumRow == NCol*SumCol. Try

class hello_world;
  rand bit [7:0] a [5][4];
   constraint a_row {
    foreach (a[i]) a[i].sum() with (int'(item)) == 40;
  }
  constraint a_col{
    foreach (a[,j]) a.sum() with (int'(a[item.index][j])) == 50;
  }
endclass

Or even more compactly

  constraint a_r_c {
    foreach (a[i,j]) {
      a[i].sum() with (int'(item)) == 40;
      a.sum() with (int'(a[item.index][j])) == 50;
    }

In reply to dave_59:

I did not quiet understand NRowSumRow == NColSumCol and why did changing the sum of the columns value to 50 work?

In reply to fenil_shah:
Try on paper with a smaller example with a[2][3] and find a solution where the sums of the rows and columns are both 3.

In reply to dave_59:

According to what I understand it works as below. Please do correct me where am I going wrong.

  rand bit [7:0] a [2][3];
    constraint a_r_c {
      foreach (a[i,j]) {
        a[i].sum() with (int'(item)) == 3;              // a[0][0] + a[0][1] + a[0][2] == 3
                                                        // a[1][0] + a[1][1] + a[1][2] == 3

        a.sum() with (int'(a[item.index][j])) == 3;     // a[0][0] + a[1][0] == 3
                                                        // a[0][1] + a[1][1] == 3
                                                        // a[0][2] + a[1][2] == 3
    }

In reply to fenil_shah:

Can you find one valid solution with values for the array that satisfy the constraints?

In reply to dave_59:

I understand now. Thank you!