array definition and initiation in promela - promela

1 #define NUM_PHIL 4
2
3 byte chopstick[4];
4 chopstick[0] = 1;
5 chopstick[1] = 1;
6 chopstick[2] = 1;
7 chopstick[3] = 1;
8
9 proctype phil(int id) {
10 do
11 ::printf("Philosopher %d is thinking\n",id);
12 /* ... */
13 printf("Philosopher %d is eating with forks %d and %d\n",id,id,(id+1)%4);
14 /* ... */
15 od
16 }
16a
17 init {
18 int i = 0;
19 do
20 :: i >= NUM_PHIL -> break
21 :: else -> run phil(i);
22 i++
23 od
24 }
the codes above send an error "syntax error saw 'an identifier' near 'chopstick'"
how can i define and initialize array as a global variable outside the prototype P()
thanks for helping

You would initialize the chopstick array in an init body.
#define NUM_PHIL 4
byte chopstick[4]; /* NUM_PHIL */
proctype phil (int n) { /* ... */ }
init {
chopstick[0] = 1;
/* ... */
run phil (0);
/* ... */
}

Related

Issue trying to sub sample an image

I am trying to resize a given image by sub-sampling it. I am using grayscae iamges.
The way I understand sub-sampling is basically this:
Let's say we have an 5x5 image and we want to resize it by a factor of 2, so the output image will be 3x3.
So if the 5x5 is: (1 2 3 4 5; 6 7 8 9 10; 11 12 13 14 15; 16 17 18 19 20; 21 22 23 24 25)
Then the 3x3 will be: (1 3 5; 11 13 15; 21 23 25)
So if the factor is 2, for the first row we take the first sample and then every second sample and so on. The same for columns.
The code that I wrote for this is:
i = 0;
j = 0;
for row=1:old_rows
if mod((row-1), a) == 0
i = i + 1;
end
for col=1:old_cols
if mod((row-1), a) == 0 && mod((col-1), a) == 0
j = j + 1;
sub_sampled_I(i, j) = I(row, col); % I is the input image
end
end
j = 0;
end
sub_sampled_I = im2uint8(sub_sampled_I);
end
The issue is that the final image has nothing to do with the original. It is just a white image with some black points here and there. What I understand wrong about sub-sampling?

Print the missing number in a unique sequential list with an arbitrary starting range or starting from 1

This question is similar to How can I find the missing integers in a unique and sequential list (one per line) in a unix terminal?.
The difference being is that I want to know if it is possible to specify a starting range to the list
I have noted the following provided solutions:
awk '{for(i=p+1; i<$1; i++) print i} {p=$1}' file1
and
perl -nE 'say for $a+1 .. $_-1; $a=$_'
file1 is as below:
5
6
7
8
15
16
17
20
Running both solutions, it gives the following output:
1
2
3
4
9
10
11
12
13
14
18
19
Note that the output start printing from 1.
Question is how to pass an arbitrary starting/minimum to start with and if nothing is provided, assume the number 1 as the starting/minimum number?
9
10
11
12
13
14
18
19
Yes, sometimes you will want the starting number to be 1 but sometimes you will want the starting number as the least number from the list.
You can use your awk script, slightly modified, and pass it an initial p value with the -v option:
$ awk 'BEGIN{p=p<1?1:p} {for(i=p; i<$1; i++) print i} {p=p<=$1?$1+1:p}' file1
1
2
3
4
9
10
11
12
13
14
18
19
$ awk -v p=10 'BEGIN{p=p<1?1:p} {for(i=p; i<$1; i++) print i} {p=p<=$1?$1+1:p}' file1
10
11
12
13
14
18
19
The BEGIN block initializes p to 1 if it is not specified or set to 0 or a negative value. The loop starts at p instead of p+1, and the last block assigns $1+1 to p (instead of $1), if and only if p is less or equal $1.
This assumes that the default (1) is the minimum starting number you would want. If you would like to start from 0 or even from a negative number just replace BEGIN{p=p<1?1:p} by BEGIN{p=(p==""?1:p)}:
$ awk -v p=-2 'BEGIN{p=(p==""?1:p)} {for(i=p; i<$1; i++) print i} {p=p<=$1?$1+1:p}' file1
-2
-1
0
1
...
Slight variations of those one-liners to include a start point:
awk
# Optionally include start=NN before the first filename
$ awk 'BEGIN { start= 1 }
$1 < start { next }
$1 == start { p = start }
{ for (i = p + 1; i < $1; i++) print i; p = $1}' start=5 file1
9
10
11
12
13
14
18
19
$ awk 'BEGIN { start= 1 }
$1 < start { next }
$1 == start { p = start }
{ for (i = p + 1; i < $1; i++) print i; p = $1}' file1
1
2
3
4
9
10
11
12
13
14
18
19
perl
# Optionally include -start=NN before the first file and after the --
$ perl -snE 'BEGIN { $start //= 1 }
if ($_ < $start) { next }
if ($_ == $start) { $a = $start }
say for $a+1 .. $_-1; $a=$_' -- -start=5 file1
9
10
11
12
13
14
18
19
$ perl -snE 'BEGIN { $start //= 1 }
if ($_ < $start) { next }
if ($_ == $start) { $a = $start }
say for $a+1 .. $_-1; $a=$_' -- file1
1
2
3
4
9
10
11
12
13
14
18
19
Using Raku (formerly known as Perl_6)
raku -e 'my #a=lines.map: *.Int; .put for (#a.Set (^) #a.minmax.Set).sort.map: *.key;'
Sample Input:
5
6
7
8
15
16
17
20
Sample Output:
9
10
11
12
13
14
18
19
Here's an answer coded in Raku, a member of the Perl-family of programming languages. No, it doesn't address the OP's request for a user-definable starting point. Instead the code above is a general solution that computes the input's minimum Int and counts up from there, returning any missing Ints found up--to the input's maximum Int.
Really need a user-defined lower limit? Try the following code, which allows you to set a $init variable:
~$ raku -e 'my #a=lines.map: *.Int; my $init = 1; .put for (#a.Set (^) ($init..#a.max).Set).sort.map: *.key;'
1
2
3
4
9
10
11
12
13
14
18
19
For explanation and shorter code (including single-line return and/or return without sort), see the link below.
https://stackoverflow.com/a/72221301/7270649
https://raku.org
not as elegant as i hoped :
< file | mawk '
BEGIN { _= int(_)^(\
( ORS = "")<_)
} { ___[ __= $0 ] }
END {
do {
print _ in ___ \
? "" : _ "\n"
} while(++_ < __) }' \_=10
10
11
12
13
14
18
19

Octave: How can I index single element of an array?

I have the following code written in Octave:
1 %-----------------------------------------------------------------------------%
2 % INPUT SIGNAL %
3 %-----------------------------------------------------------------------------%
4
5 sampling_frequency = 8000;
6 sampling_period = 1/sampling_frequency;
7 samples_count = 8;
8 time_samples = sampling_period * (0 : samples_count-1);
9
10
11 amplitude_1 = 1;
12 frequency_1 = 1000;
13 phase_shift_1 = 0;
14 signal_samples_1 = amplitude_1*sin(2*pi * frequency_1 * ...
15 time_samples + phase_shift_1);
16
17
18 amplitude_2 = 0.5;
19 frequency_2 = 2000;
20 phase_shift_2 = 3*pi/4;
21 signal_samples_2 = amplitude_2*sin(2*pi * frequency_2 * ...
22 time_samples + phase_shift_2);
23
24 signal_samples = signal_samples_1 + signal_samples_2;
25 for samples_index = 0 : samples_count-1
26 fprintf(stdout, "Sample %d: ", samples_index)
27 endfor
28
As you may see, at line 25, I would like to print value of each sample at separate line, i.e.
Sample <sample_number>: <sample_value>
My question is: how can I index separate member of an array (e.g. input_signal array)?
I have found the solution by myself, so I would like to share it with you.
signal_samples = signal_samples_1 + signal_samples_2;
for samples_index = 1 : samples_count
fprintf(stdout, "Sample %d: %8.5f\n", samples_index-1, ...
signal_samples(samples_index:samples_index))
endfor
Output:
Sample 0: 0.35355
Sample 1: 0.35355
Sample 2: 0.64645
Sample 3: 1.06066
Sample 4: 0.35355
Sample 5: -1.06066
Sample 6: -1.35355
Sample 7: -0.35355

while loop in my program does not fill the second row

I have 2 inner while loops. Code writes the results to first row on the matrix but when it gets to second row, it just passes the other rows and doesn't fill the columns of the rows. How to solve it? The code and output are like below:
while i <= m-1
i
while a <= m-1
a
den1 = sqrt(((xy{i,j}-xy{a+1,b})^2+(xy{i,j+1}-xy{a+1,b+1})^2 ));
dMat(i,a) = den1;
a = a+1;
end
i = i+1;
end
i = 1
a = 1
a = 2
a = 3
a = 4
a = 5
a = 6
a = 7
a = 8
a = 9
a = 10
a = 11
a = 12
a = 13
a = 14
a = 15
a = 16
a = 17
a = 18
a = 19
a = 20
a = 21
a = 22
a = 23
a = 24
i = 2
i = 3
i = 4
i = 5
i = 6
i = 7
i = 8
i = 9
i = 10
i = 11
i = 12
i = 13
i = 14
i = 15
i = 16
i = 17
i = 18
i = 19
i = 20
i = 21
i = 22
i = 23
i = 24
You have to restart a to a=1 for each iteration. Put it above the while a <= m-1

How do I get microsoft word or pycharm to read dot files as a graphic instead of code

When I open my .dot file with microsoft word or pycharm it shows me the code and that the code is error free but doesnt display the intended graphic.
I have extensively searched on google for a solution but google insists that microsoft word comes with the ability to read .dot files (which it seems to be able to read the code but not display the graphic). I downloaded the pycharm .dot reading plug-in and now pycharm can read the code and it says that the code is error free, but again it does not display the graphic.
I am unsure what code is necessary so I am just giving the whole thing.
digraph Tree {
node [shape=box, style="filled, rounded", color="black", fontname=helvetica] ;
edge [fontname=helvetica] ;
0 [label="failures <= 0.5\nsamples = 100.0%\nvalue = [0.502, 0.498]\nclass = fail", fillcolor="#e5813902"] ;
1 [label="higher_no <= 0.5\nsamples = 84.8%\nvalue = [0.422, 0.578]\nclass = pass", fillcolor="#399de545"] ;
0 -> 1 [labeldistance=2.5, labelangle=45, headlabel="True"] ;
2 [label="school_GP <= 0.5\nsamples = 79.2%\nvalue = [0.386, 0.614]\nclass = pass", fillcolor="#399de55e"] ;
1 -> 2 ;
3 [label="absences <= 1.5\nsamples = 23.0%\nvalue = [0.574, 0.426]\nclass = fail", fillcolor="#e5813942"] ;
2 -> 3 ;
4 [label="freetime <= 4.5\nsamples = 12.4%\nvalue = [0.419, 0.581]\nclass = pass", fillcolor="#399de547"] ;
3 -> 4 ;
5 [label="samples = 11.6%\nvalue = [0.379, 0.621]\nclass = pass", fillcolor="#399de563"] ;
4 -> 5 ;
6 [label="samples = 0.8%\nvalue = [1.0, 0.0]\nclass = fail", fillcolor="#e58139ff"] ;
4 -> 6 ;
7 [label="health <= 1.5\nsamples = 10.6%\nvalue = [0.755, 0.245]\nclass = fail", fillcolor="#e58139ac"] ;
3 -> 7 ;
8 [label="samples = 2.2%\nvalue = [0.455, 0.545]\nclass = pass", fillcolor="#399de52a"] ;
7 -> 8 ;
9 [label="samples = 8.4%\nvalue = [0.833, 0.167]\nclass = fail", fillcolor="#e58139cc"] ;
7 -> 9 ;
10 [label="Walc <= 3.5\nsamples = 56.2%\nvalue = [0.31, 0.69]\nclass = pass", fillcolor="#399de58d"] ;
2 -> 10 ;
11 [label="Medu <= 3.5\nsamples = 46.4%\nvalue = [0.263, 0.737]\nclass = pass", fillcolor="#399de5a4"] ;
10 -> 11 ;
12 [label="samples = 29.4%\nvalue = [0.333, 0.667]\nclass = pass", fillcolor="#399de57f"] ;
11 -> 12 ;
13 [label="samples = 17.0%\nvalue = [0.141, 0.859]\nclass = pass", fillcolor="#399de5d5"] ;
11 -> 13 ;
14 [label="sex_M <= 0.5\nsamples = 9.8%\nvalue = [0.531, 0.469]\nclass = fail", fillcolor="#e581391d"] ;
10 -> 14 ;
15 [label="samples = 3.0%\nvalue = [0.267, 0.733]\nclass = pass", fillcolor="#399de5a2"] ;
14 -> 15 ;
16 [label="samples = 6.8%\nvalue = [0.647, 0.353]\nclass = fail", fillcolor="#e5813974"] ;
14 -> 16 ;
17 [label="reason_course <= 0.5\nsamples = 5.6%\nvalue = [0.929, 0.071]\nclass = fail", fillcolor="#e58139eb"] ;
1 -> 17 ;
18 [label="health <= 3.5\nsamples = 2.6%\nvalue = [0.846, 0.154]\nclass = fail", fillcolor="#e58139d1"] ;
17 -> 18 ;
19 [label="samples = 1.4%\nvalue = [1.0, 0.0]\nclass = fail", fillcolor="#e58139ff"] ;
18 -> 19 ;
20 [label="reason_home <= 0.5\nsamples = 1.2%\nvalue = [0.667, 0.333]\nclass = fail", fillcolor="#e581397f"] ;
18 -> 20 ;
21 [label="samples = 0.6%\nvalue = [0.333, 0.667]\nclass = pass", fillcolor="#399de57f"] ;
20 -> 21 ;
22 [label="samples = 0.6%\nvalue = [1.0, 0.0]\nclass = fail", fillcolor="#e58139ff"] ;
20 -> 22 ;
23 [label="samples = 3.0%\nvalue = [1.0, 0.0]\nclass = fail", fillcolor="#e58139ff"] ;
17 -> 23 ;
24 [label="Fjob_teacher <= 0.5\nsamples = 15.2%\nvalue = [0.947, 0.053]\nclass = fail", fillcolor="#e58139f1"] ;
0 -> 24 [labeldistance=2.5, labelangle=-45, headlabel="False"] ;
25 [label="Fjob_health <= 0.5\nsamples = 15.0%\nvalue = [0.96, 0.04]\nclass = fail", fillcolor="#e58139f4"] ;
24 -> 25 ;
26 [label="freetime <= 1.5\nsamples = 14.8%\nvalue = [0.973, 0.027]\nclass = fail", fillcolor="#e58139f8"] ;
25 -> 26 ;
27 [label="Mjob_at_home <= 0.5\nsamples = 0.6%\nvalue = [0.667, 0.333]\nclass = fail", fillcolor="#e581397f"] ;
26 -> 27 ;
28 [label="samples = 0.2%\nvalue = [0.0, 1.0]\nclass = pass", fillcolor="#399de5ff"] ;
27 -> 28 ;
29 [label="samples = 0.4%\nvalue = [1.0, 0.0]\nclass = fail", fillcolor="#e58139ff"] ;
27 -> 29 ;
30 [label="age <= 16.5\nsamples = 14.2%\nvalue = [0.986, 0.014]\nclass = fail", fillcolor="#e58139fb"] ;
26 -> 30 ;
31 [label="samples = 2.4%\nvalue = [0.917, 0.083]\nclass = fail", fillcolor="#e58139e8"] ;
30 -> 31 ;
32 [label="samples = 11.8%\nvalue = [1.0, 0.0]\nclass = fail", fillcolor="#e58139ff"] ;
30 -> 32 ;
33 [label="samples = 0.2%\nvalue = [0.0, 1.0]\nclass = pass", fillcolor="#399de5ff"] ;
25 -> 33 ;
34 [label="samples = 0.2%\nvalue = [0.0, 1.0]\nclass = pass", fillcolor="#399de5ff"] ;
24 -> 34 ;
}
I expect the above code to display a color-coded decision tree. Instead microsoft word, pycharm, and jupyter notebook are all returning the code.
A GraphViz .dot file is not an image file. It rather contains a logical description of some content (nodes and edges), along with some graphical clues how to display them (e.g. color, font sizes). The exact positions of each element are not defined in the .dot file at all. Calculating these positions, and rendering actual image files, is left to a layout engine, such as dot, or derived Javascript libraries (e.g. viz.js).
Almost no application can display GraphViz .dot files directly.
The confusion probably comes from a conflicting naming convention:
GraphViz .dot file (what we are talking about here)
Microsoft .dot file (what Word is expecting and Google might refer to)
A Microsoft .dot file is a template for a Microsoft Word (.doc) file.
In order to present your desired graphic, you need to run your .dot file through a layout engine to produce e.g. an .svg or .png file.
You can do this manually on your local PC or online (e.g. https://dreampuf.github.io/GraphvizOnline/ ), then save the resulting image.
You can present both .svg and .png images in Word (Insert / Image).
I have been able to get the .dot file to open properly using GVEdit (graphviz text editor). Here is a link to a post that answers my question. Graphviz: How to go from .dot to a graph?
Don't try to open dot file directly from word as it will give an error. Open Ms-word first then locate the dot file which you want to open.