Square matrix of size odd number and no duplicate max element in rows

Hi,
I tried below code for below spec and seems matrix[i].max() is not supported. May i know how to implement this in any alternate way.

//square matrix with odd number size
//each row should have only one max element and rest less than max and can be repetitive
//Max elements of each row should be unique, meaning no two rows should have same max element


class matrix_sq;
  rand bit[3:0] md_array[][];
  rand bit [2:0] odd_num;
  constraint c_md_array { 
       odd_num[0] == 1;
       // First assign the size of the first dimension of md_array
       md_array.size() == odd_num; 
  
       // Then for each sub-array in the first dimension do the following:
       foreach (md_array[i]) {
  
          // Randomize size of the sub-array(second dimension of matrix) to a value same as first dimension(odd_num)
          md_array[i].size() == odd_num;
  
          // Iterate over the first dimension and check for max element not to be repeated
          foreach (md_array[i]) {
            foreach(md_array[j]) {
              (i!=j)->(md_array[i].max()!=md_array[j]);
            }
             
           }
        }
   }

endclass
      
module tb;

  initial begin
    matrix_sq sq = new;
    sq.randomize();
    $display ("md_array = %p", sq.md_array);
  end
endmodule  

In reply to sra1dreddy:

A problem with array locater methods like max() is they return a queue, not a single value. This is in case the array is empty to begin with. That means you cannot not use locator methods in constraints. In this example, it is fairly easy to work around by adding a helper array: max_value.

class matrix_sq;
  rand bit [3:0] md_array[][];
  rand bit [3:0] odd_num;
  rand bit [3:0] max_value[];
  constraint c_md_array { 
    odd_num % 2 == 1; odd_num > 1;
    
    // First assign the size of the first dimension of md_array
    md_array.size() == odd_num;
    max_value.size() == odd_num;
 
    foreach (md_array[r]) {
      // make the array square
      md_array[r].size() == odd_num;
      // one and only one max value in a row
      md_array[r].sum() with (int'(max_value[r] == item)) == 1;
     }
     // compute max_values and make unique
     // this works because the previous constraint makes sure at least one element
     // is equal to the max value
    foreach (md_array[r,c]) {
      max_value[r] >= md_array[r][c];
    }
    unique { max_value };
  }
endclass
 
module tb;
 
  initial repeat (5) begin
   static matrix_sq sq = new;
    assert(sq.randomize());
    $display ("%p", sq);
  end
endmodule

In reply to dave_59:

“each row should have only one max element and rest less than max and can be repetitive”.
Does it mean - in each row only one element should have max value and all other elements must be less than that. ‘max’ is non-repetitive and others can be repetitive?

I made a minor change to make sure max is not repetitive.


     // compute max_values and make unique
     // one element is equal to the max value
     // and check for max element not to be repeated
     foreach (md_array[r]) {
       md_array[r].sum() with (int'(item < max_value[r])) == odd_num-1;;
     }

In reply to Alokpati:

Yes, that solution also works.