How to write constraints on 2D arrays or multi dimentional arrays?

I am dealing with a situation to randomize a large matrix with specific conditions. But I am seeing results are not coming as expected. Can anyone point me what should be the good method here.

module top();
class transaction;
rand bit[8:0]  A[5][5] ;
shortint i,j;
constraint c_1 {foreach (A[i][j]) A[i][j] == i+j; }
endclass

transaction tr;
initial begin
tr = new();
tr.randomize();
$display ("Array is %p",tr.A);

end
endmodule

I am getting results as:

Array is ‘{’{'h0, 'h1, 'h2, 'h3, 'h4}, '{'h1b9, 'h6c, 'ha7, 'ha3, 'h19c}, '{'h34, 'h39, 'h83, 'h114, 'hf6}, '{'h52, 'hc9, 'hdb, 'h36, 'h69}, '{'h8a, 'h99, 'h17, 'h118, 'h1d1}}

Here I see for only first row of the matrix is valid and others failed. Why it so?

In reply to ktheja:

This is same mentioned by Dave in this.
https://verificationacademy.com/forums/systemverilog/getting-array-values-inside-certain-range

Your A[0] is only constraint in this case. You won’t require explicit i, j.


// Code your testbench here
// or browse Examples
module top();
class transaction;
rand bit[8:0]  A[5][5] ;
// Not need
//shortint i,j;
constraint c_1 {
  foreach (A[i]) {
    foreach (A[i][j]) {
      A[i][j] == i+j; 
    }
  }
}
endclass
 
transaction tr;
initial begin
tr = new();
tr.randomize();
$display ("Array is %p",tr.A);
 
end
endmodule

//Results
# vsim -voptargs=+acc=npr
# run -all
# Array is '{'{0, 1, 2, 3, 4}, '{1, 2, 3, 4, 5}, '{2, 3, 4, 5, 6}, '{3, 4, 5, 6, 7}, '{4, 5, 6, 7, 8}}
# exit
# End time: 11:05:40 on Sep 17,2021, Elapsed time: 0:00:01
# Errors: 0, Warnings: 2
Done


Thanks!

In reply to harsh pandya:

The constraints are illegal syntax the way they are written. You are only allowed one set of brackets 's at the end of the expression used in a foreach loop iterator. Since you already declared i, it is treating that as a non-random state variable using its initial value 0, not as a loop iterator. I would report that to your tool vendor as a bug.

You should write this with one using commas to separate the iterator variables.

class transaction;
rand bit[8:0]  A[5][5] ;
  constraint c_1 {foreach (A[i,j]) A[i][j] == i+j; }
endclass


In reply to dave_59:

I am surprised almost all EDA tool not shouting an error for this.
Thanks Dave for updating my knowledge.