Constraint #41

Constraint #41

In today's constraint, we aim to create a 2-dimensional array representing a lower triangle matrix with incremental values. Unlike our previous example where the lower triangle consisted of 5s, this time we're dealing with different numbers in each index of the lower triangle.

When solving such constraints, it's crucial to identify the sequence pattern. In our case, while there isn't a specific sequence, we observe that the numbers in the lower triangle are in incremental order.

Referring to the image above:

  • In the first row, if i=0 and j=0, the value is 7; otherwise, it's 0.
  • In the second row, if i=1 and j=0, the value is 11 (greater than value[0][0]); if i=1 and j=1, the value is 27 (greater than value[1][0]).
  • In the third row, if i=2 and j=0, the value is 57 (greater than value of [1][1]); if i=2 and j=1, the value is 63 (greater than value of [2][0]); if i=2 and j=2, the value is 92 (greater than value of [2][1]).

We already know how to compute a lower triangle matrix using a 2D array from Constraint #38. Here, we need to generate values from 7 to 700. When j=0, we need to generate a value greater than the previous row's last value. For example, for the second row (i=1 and j=0), the value should be greater than the first row's value at the same index (i=0 and j=0). Similarly, for the third row (i=2 and j=0), the value should be greater than the second row's value at index 1 (i=1 and j=1).

In summary, for the constraint:

if (j <= i) { array[i][j] inside {[7:200]}; if (j == 0 && i != 0) array[i][j] > array[i-1][i-1]; if (j != 0 && i != 0) array[i][j] > array[i][j-1]; } else a[i][j] == 0;

This constraint ensures that each value in the lower triangle of the array is greater than the preceding value, maintaining the incremental pattern.

Don't forget to Check out the Executable Code Below:

class sample;
  rand bit[7:0] array[0:3][0:3];
  
  constraint array_c {
    foreach(array[i,j]){
      if(j<=i) {
        array[i][j] inside {[7:700]};
        if(j==0 && i!=0) array[i][j] > array[i-1][i-1];
        if(j!=0 && i!=0) array[i][j] > array[i][j-1];
      }
      else array[i][j] == 0;
    }
  }
endclass
                             
module top;
  sample s=new();
  
  initial begin
    $display("######## OUTPUT ########");
      assert(s.randomize());
      foreach(s.array[i,j]) begin
          $write("%0d \t",s.array[i][j]);
        if(j==3)
          $display("");
      end
    $display("######## END ########");
  end
endmodule        
https://www.edaplayground.com/x/a_EF

Click here for EDA link

Approach for Displaying in Matrix Format

To format the 2D array into a matrix-like display, we iterated through the array values using a foreach loop. We opted for $write instead of $display to prevent the printing of the next element of the same row on a new line. When the value of 'j' reached 3, indicating the end of the row, we used an empty $display statement to move to the next line. This approach allowed us to achieve the desired matrix format for the 2D array output.

I appreciate your understanding of the methodology we've utilized to handle constraints and their real-world applications. Your support, through likes and reposts, will indeed encourage further exploration. I'm eagerly looking forward to engaging in meaningful discussions with you.

Thank you! ????

Here is the Breakdown of above Code:

In Class

  • Class Declaration: Declares a class named "sample" containing a random 2D array "array" of 8-bit elements.
  • Random 2D Array Declaration: Declares a random 2D array named "array" with dimensions [0:3][0:3] within the "sample" class. The array is declared as a random variable (rand), which means it will be randomized during object creation.
  • Constraint: Defines a constraint named "array_c" which imposes the following conditions on the "array" variable:For each element in the 2D array "array[i][j]":If the column index "j" is less than or equal to the row index "i", the element must be in the range [7, 700].If "j" is 0 and "i" is not 0, the element must be greater than the diagonal element at index [i-1][i-1].If "j" is not 0 and "i" is not 0, the element must be greater than the element to its left at index [i][j-1].Otherwise, the element must be 0.

In Module Top

  • Instantiation of Class: Instantiates an object of the "sample" class named "s".
  • Initial Block: Executes the following actions:Displays a header indicating the beginning of the output.Randomizes the data members of the object "s" according to the constraints specified in the class definition.Displays the values of the randomized 2D array "array" in a tabular format using a nested loop.Displays a footer indicating the end of the output.

This program generates random 2D arrays with specific patterns of elements based on the constraints specified. The output displays the generated 2D array in a tabular format for better understanding.

Rajesh Deshpande

Senior Principal Engineer (Pre-Silicon Verification)

7 个月

if(j==0 && i!=0) array[i][j] > array[i-1][i-1]; isn't it should be if(j==0 && i!=0) array[i][j] > array[i-1][j-1];

mohamed irsath I

Design Verification Engineer

7 个月

Afzal Mansuri Thanks for resposting

回复

要查看或添加评论,请登录

社区洞察

其他会员也浏览了