please ans my how to write a constraint n * n matrix where diagonal values are greater in order. Diagonal’s right side should be greater.
In reply to Neel Kansara:
Can you give an example? Say the matrix is 4x4; How do you want it to look like?
Or see what I’m assuming is correct or not:
1234
4567
2378
5679
4*4 metric greater in order
and Diagonal’s right side should be greater.
In reply to Neel Kansara:
I think another user on this forum has already asked a similar question. But I’m not sure if that’s the same question as yours as you still didn’t provide any example.
Please go through the code and see if it meets your requirement:
class packet;
rand int array [] [];
rand int size_i,size_j;
int i,j;
constraint unique_elements_in_each_row {
foreach(array[size_i,size_j]) {
unique {array[size_j]};
}
}
constraint size_fix {
array.size() == size_i;
foreach(array[size_i]) {
array[size_i].size() == size_j;
}
}
constraint size_i_and_j_fix {size_i == 5; size_j == 5;}
constraint elements_range_fix {
foreach(array[size_i,size_j]) {
array[size_i][size_j] inside {[1:5]};
}
}
constraint element_pattern_fix {
foreach(array[size_i,size_j]) {
if(size_i == 0) {
foreach(array[size_j]) {
if(size_j == 0 || size_j < 4) {
array[size_i][size_j] < array[size_i][size_j + 1];
}
}
}
if(size_i == 1) {
foreach(array[size_j]) {
if(size_j == 0 || size_j <= 2 ) {
array[size_i][size_j] < array[size_i][size_j + 1];
}
else if(size_j == 3 || size_j < 4) {
array[size_i][size_j] > array[size_i][size_j + 1];
}
}
}
if(size_i == 2) {
foreach(array[size_j]) {
if(size_j == 0 || size_j <= 1 ) {
array[size_i][size_j] < array[size_i][size_j + 1];
}
else if(size_j == 2 || size_j < 4) {
array[size_i][size_j] > array[size_i][size_j + 1];
}
}
}
if(size_i == 3) {
foreach(array[size_j]) {
if(size_j == 0 || size_j < 1 ) {
array[size_i][size_j] < array[size_i][size_j + 1];
}
else if(size_j == 1 || size_j < 4) {
array[size_i][size_j] > array[size_i][size_j + 1];
}
}
}
if(size_i == 4) {
foreach(array[size_j]) {
if(size_j == 0 || size_j < 4) {
array[size_i][size_j] > array[size_i][size_j + 1];
}
}
}
}
}
endclass
module diagonal_array_2;
initial
begin
packet pkt = new();
pkt.randomize();
$display("\ni = %0d \t j = %0d",pkt.size_i,pkt.size_j);
$display("\narray : %0p",pkt.array);
end
endmodule
This is a sample 5 * 5 matrix. All you need to do for n * n is parameterize it. Hope it helps!
In reply to Shashank Gurijala:
If this isn’t what you wanted, please provide an example.
randomly output is
5 9 11
4 7 10
8 10 15
Diagonal’s right side should be greater.
In reply to Neel Kansara:
Ok got it! Will try and post the solution.
In reply to Neel Kansara:
Try this:
class sample;
rand int array [][];
rand int size_i,size_j;
constraint c1 {array.size == size_i;
foreach(array[size_i]){
array[size_i].size == size_j;
}
}
constraint c2 {foreach(array[size_i,size_j]){
unique{array[size_j]};
}
}
constraint c3 {size_i == size_j;}
constraint c4 {size_i == 5;}
constraint c5 {
foreach(array[size_i,size_j]){
if(size_j < 4){
array[size_i][size_j] < array[size_i][size_j + 1];
}
}
}
constraint c6 {
foreach(array[size_i,size_j]){
array[size_i][size_j] inside {[100:999]};
}
}
function void display();
begin
$display("\narray = %0p",array);
end
endfunction
endclass
module diagonal_array_3;
initial
begin
sample s = new();
s.randomize();
s.display();
end
endmodule
In reply to Neel Kansara:
What is diagonal about this? Even if you don’t know how to write constraints, you should be able to express your requirements in a set of equations.
For the example you show, either
arrayi,j < arrayi+1,j or
arrayi,j < arrayi+1,j+1 would be satisfied. But I’m going to assume the latter is what you meant.
class example #(int N);
rand int array[][];
constraint size {
array.size == N;
foreach (array[i]) array[i].size == N;
}
constraint diagonal {
foreach(array[i,j])
i > 0 && j > 0 -> array[i-1][j-1] < array[i][j];
}
endclass