Code snippets
Index
• 01. Bash• 02. Perl
• 03. C/C++
• 04. Maple/Octave/Matlab
• 05. Latex
• 06. Beamer
• 07. Miscelaneous
• 08. GNUplot
• 09. R
• 10. MS-Windows stuff
01. Bash
01.001. Setting the current DATE and TIME
01.002. Run PEPS2003 (passing args) in a whole directory of .san files, save progress to an output file
01.003. Run a command, inside a loop (use of for and while) iterates in directory
01.004. Using if - testing for initial parameters
01.005. Multiple if
02. Perl
02.001. Standard Perl header
02.002. Open a file for reading, iterate through lines, remove \n
02.003. Trim, changes multiple spaces for single space and functions for trim and strip (removing spaces inside strings)
02.004. Open all *.txt files of a directory, runs a system command to print the last line of the file
02.005. Print blocks of text
02.006. Formatting numbers
02.007. Printing blocks of text to an output file
02.008. Using hashes with arrays and iterating
02.009. Creating a dotty file from a matrix
02.010. Passing two arrays to a subrotine and printing
02.011. Date and time and printf
02.012. Match everything betwen { and }
02.013. Open file, match pattern between ( and ), reverse and print array without ending spaces
02.014. Filling a string with zeroes (before the number)
02.015. Take a file with many runs and execute the confidence intervals
02.016. Call subrotine with array and variables
02.017. Using hashes of arrays
02.018. Convert an array to a matrix
02.019a. Convert photos from high definition to 4x6 inches (or 10x15 cms) format (1024x768) - using convert
02.019b. Convert photos from high definition to another size - using Image::Resize
02.020. Convert movies from MPG ou MOV to AVI format
02.021. Order hashes by keys or values
02.022. Permutation Algorithm
02.023. Open huge files in Perl
02.024. Create a basic gnuplot file in Perl
02.025. Read folder, iterate through subfolders, then its files
02.026. Pattern matching of datetime format (e.g. 2010-10-13 17:55:39,666)
02.027. Passing two hashes to a function
02.028. Checks if number is an integer (greater than zero) or if number is integer or float
02.029. Checks two datetime parameters ('yyyy mm dd hh:mm:ss') are valid
02.030. Move files without system call
02.031. Working with XML file and parameters
02.032. (classic) Permutation algorithm
02.033. Sub-routines in Perl
03. C/C++
03.001. Solve an infinitesimal generator
03.002. Removing equals from a list
03.003. Removing chars from string
03.004. Converting string to upper or lower case
03.005. sampler - ferramenta para geração de samples da distribuição uniforme
04. Maple/Octave/Matlab
04.001. Solve a pepa model in Maple
04.002. Solve a Markov Chains model in Octave
04.003. Print in file in Octave
05. Latex
05.001. Figures in Latex
05.002. Putting vertical labels in tables
05.003. Script for cleaning latex files and compiling latex
05.004. Full latex project for Lecture Notes in Computer Science format
05.005. Highlighting paragraphs
05.006. Adding fancy notes
05.007. Better cell alignment with tabular
06. Beamer
06.001. A presentation in Beamer, with multiple slides
06.002. Beamer complete list of themes
07. Miscelaneous
07.001. A Makefile without having to tell all .c files
07.002. Convert a .wav to .mp3 using lame
07.003. Create a small book from a ps file
07.004. Merging PDFs
07.005. Converting between file formats
07.006. Use regex in Notepad++
08. GNUplot
08.001. General GNUplot scripting
09. R
09.001. Several scripts
10. MS-Windows stuff
10.001. Batch script to test HTTP code of a URL
01. Bash programming
01.001. Setting the current DATE and TIME
#!/bin/bash DATE=`date +%d%m%Y.%H%S` cd `somedir`
01.002. Run PEPS2003 (passing args) in a whole directory of .san files, save progress to an output file
echo "" > file.log dirsan=/tmp for i in $dirsan/*.san; do #for all existing files in 'san' directory ni=${i//.san/} #removes file termination .san (san wont accept it) rm $dirsan/agg/ $dirsan/cnd/ $dirsan/des/ $dirsan/dsc/ $dirsan/jit/ -rf rm $dirsan/*.vct -rf #remove previous peps stuff hora=`date +%H:%M:%S%t%d/%m` #saves current time, to mark progress echo "running $ni at $hora" >> file.log #save to file.log what's running echo 4 2 100000 1 1 $ni 2 1 1 vector 0 2 | $dirsan/peps #the last line does 3 things: #1. calls peps setting iterations to 100000 #2. compiling (1 1) and #3. solving (2 1 1) then exiting (0 2) #we are assuming that the executable is in the dirsan directory done
01.003. Run a command, inside a loop (use of for and while) iterates in directory
t=0 c=0 o=0 cut=10 term=62 dir=$base'models/8slaves/' while [ $t -le $term ]; do path=$dir'term'$t for i in $path/*; do file=$path/'8slaves_'$t'_t'$o'.out' `rm $file -f` let o+=1 while [ $c -le $cut ]; do `$base''split $i $c >> $file` let c+=1 done c=0 done o=0 let t+=1 done
01.004. Using if - testing for initial parameters
#!/bin/bash if [ "$1" = "" ]; then echo "error. missing parameter n" else echo "success" fi
01.005. Multiple if
#!/bin/sh if [ "$1" = "" ]; then echo "invalid command. options: get/getall/put" else if [ "$1" = "getall" ]; then echo "retrieving ALL from foo" else if [ "$1" = "get" ]; then echo "retrieving *.htm from foo" else if [ "$1" = "put" ]; then if [ "$2" = "" ]; then echo "choose a file." else echo "uploading $2 to foo" fi fi fi fi fi
02. Perl programming
02.001. Standard Perl header
#!/usr/bin/perl use strict; use warnings; if (@ARGV != 1) { print "missing FILE parameter. \nusage: perl file.pl FILE\n"; exit; }
02.002. Open a file for reading, iterate through lines, remove \n
open(INFILE, "<$file") or die("cannot open hash file named $file\n"); my(@lines) = <INFILE>; close(INFILE); foreach my $line (@lines) { $line =~ s/\n//g; }
02.003. Trim, changes multiple spaces for single space
$line =~ s/^\s+//; #trim at start $line =~ s/\s+$//; #trim at end $line =~ s/\s+/ /g; #remove multiple spaces for single spaceOr using functions to trim or strip spaces within strings:
# removes spaces from beginning and end of strings sub trim { my $s = shift; $s =~ s/^\s+|\s+$//g; return $s }; # substitutes extra spaces in the middle of strings for single spaces sub strip { my $s = shift; $s =~ s/ +/ /g; return $s };
02.004. Open all *.txt files of a directory, runs a system command to print the last line of the file
#!/usr/bin/perl my $path = $ARGV[0]."output/"; my @contents; opendir MYDIR, $path; @contents = grep !/^\.\.?$/,grep /.txt/, readdir MYDIR; closedir MYDIR; foreach my $file (@contents) { print "file=$file\n"; system("tail -n 1 $path$file"); }
02.005. Print blocks of text
print >>END; \\begin{table*}[!hbt] \\begin{center} END
02.006. Formatting numbers (requires module to be installed)
#put this on the header use Number::Format qw(:subs :vars); $THOUSANDS_SEP = '.'; $DECIMAL_POINT = ','; $INT_CURR_SYMBOL = 'DEM'; #use it like this: $mem = format_number($mem, 2, 1); $complex = format_number($complex);
02.007. Printing blocks of text to an output file
open(OUTFILE, ">$name") or die("cannot open $name\n"); print OUTFILE >>END; #!/bin/sh set term postscript eps enhanced color set xtics 2 plot[0:$termo][0:$lcut] 'bc_$model.txt' title "term x cut" with boxes END close(OUTFILE); #or open(OUTFILE, ">$name") or die("cannot open $name\n"); print OUTFILE "this is some text to be printed\n"; close(OUTFILE);
02.008. Using hashes with arrays and iterating
$strut->{$lid} = { 'gid' => $gid, 'aa' => [@aa], 'ja' => [@ja], 'ia' => [@ia], 'order' => $order, 'nz' => $nz, }; #defining an array of hashes: push @terms, $strut; #iterating foreach my $st (@terms) { foreach my $termo (keys %$strut) { print "gid=".($strut->{$termo}{'gid'})."\n"; print "lid=$termo\n"; print "order=".$strut->{$termo}{'order'}."\n"; print "nz=".$strut->{$termo}{'nz'}."\n"; print "id=".$strut->{$termo}{'id'}."\n"; } } #finally, to print the whole structure, with arrays: sub print_strut { my @lterms = @_; print "call to print...\n"; foreach my $st (@lterms) { foreach my $termo (keys %$st) { print "term $termo [".($st->{$termo}{'gid'})."] nz=".$st->{$termo}{'nz'}."\n"; my @aux=@{$st->{$termo}{'aa'}}; print "aa: "; foreach my $a_ (@aux) { print "".$a_." "; } print "\n"; my @aux=@{$st->{$termo}{'ja'}}; print "ja: "; foreach my $a_ (@aux) { print "".$a_." "; } print "\n"; my @aux=@{$st->{$termo}{'ia'}}; print "ia: "; foreach my $a_ (@aux) { print "".$a_." "; } print "\n"; } print "\n"; } }
02.009. Creating a dotty file from a matrix
print "digraph G {\n"; print "\tnode [shape = circle];\n"; for (my $i = 0; $i < $SIZE; $i++) { for (my $j = 0; $j < $SIZE; $j++) { if ($matrix[$i][$j] ne "") { my $label = "label$i$j"; my $auxi = ($i+1) < 10 ? "0".($i+1) : ($i+1); my $auxj = ($j+1) < 10 ? "0".($j+1) : ($j+1); print "\tS".($auxi)." -> S".($auxj)." [ label = \"$label\" ]\n"; } } } print "}\n";
02.010. Passing two arrays to a subrotine and printing
#call: verify_changes(@arr1, @arr2); #subrotine: sub verify_changes { my @v1 = @{$_[0]}; my @v2 = @{$_[1]}; print "printing...\n"; for (my $i = 0; $i < $size; $i++) { print "$v1[$i] "; } print "\n"; for (my $i = 0; $i < $size; $i++) { print "$v2[$i] "; } print "\n"; }
02.011. Date and time and printf
(my $sec,my $min,my $hour,my $mday,my $mon,my $year,my $wday,my $yday,my $isdst)=localtime(time); printf "%4d-%02d-%02d %02d:%02d:%02d\n",$year+1900,$mon+1,$mday,$hour,$min,$sec; #or my $date = sprintf("%02d/%02d/%4d %02d:%02d:%02d\n",$mday,$mon+1,$year+1900,$hour,$min,$sec); print $date."\n";
02.012. Match everything betwen { and }
if ($line =~ /(\s*)authors=\{(.*)\}/) { print "authors=$2\n"; }
02.013. Open file, match pattern between ( and ), reverse and print array without ending spaces
#!/usr/bin/perl use strict; use warnings; #this script will revert the .mdd final file in the same format peps works if (@ARGV != 1) { print "missing FILE parameter. \nusage: perl revert.pl FILE\nenter a .rep file with termination\n"; exit; } my $rep_file = $ARGV[0]; open(INFILE, "<$rep_file") or die("cannot open $rep_file"); my(@lines) = <INFILE>; close(INFILE); my $name; my $states; my @entries; my $cont = 0; foreach my $line (@lines) { $line =~ s/\n//g; if ($line =~ /\(\s(.*)\s\)/) { #for example ( 2 2 3 4 2 1 ) $states = $1; (@entries) = split(" ", $states); @entries = reverse(@entries); #example of reverse foreach my $entry (@entries) { print "".((++$cont % @entries == 0) ? "$entry" : "$entry "); } #$cont = 0; print "\n"; } }
02.014. Filling a string with zeroes (before the number)
my $i = 10; my $val = sprintf("%05d",$i);
02.015. Take a file with many runs and execute the confidence intervals
Download file here -> Coding language: Perl.
Download samples file here Text file containing some values.
02.016. Call subrotine with array and variables
deal(\@sizes,\@cuts,\@nzs,\@perms,$n_aut,$n_evt,$model); ... sub deal { my ($sz,$ct,$nz,$pe,$auts,$evts,$model_) = @_; my @sizes_ = @$sz; my @cuts_ = @$ct; my @nzs_ = @$nz; my @perms_ = @$pe; }
02.017. Using hashes of arrays
my %rosa = ( 'N66' => [80,0,0,0,1,1,70,100], 'N23' => [1,70,80,100,30,3,2,1], 'EQU' => [1,15,50,80,100,75,45,10], 'S23' => [1,1,2,3,30,100,80,70], 'S66' => [80,100,70,1,1,0,0,0] ); ... foreach my $key (keys %rosa) { my @forces = @{$rosa{$key}}; #this is where you take the array reference print "$key\n"; foreach my $f (@forces) { #now, just use it print "$f "; } print "\n"; }
02.018. Convert an array to a matrix
#!/usr/bin/perl #implementado em 18/01/2010 use strict; use warnings; #converte um vetor em uma matriz #util para percorrer um vetor e imprimir a linha da matriz, acessando os indices corretos #colocar valores multiplos para fechar matrizes quadradas (nao testado) if (@ARGV != 2) { print "falta TAMANHO ou DIMENSAO. \nuso: perl vec2mat.pl TAMANHO DIMENSAO\n\tTAMANHO: tamanho total do vetor\n\tDIMENSAO: dimensao final da matriz\n"; exit; } my $SIZE = $ARGV[0]; #tamanho do vetor my $DIM = $ARGV[1]; #cria uma matriz DIMxDIM a partir do vetor de SIZE posicoes #inicia um vetor qualquer com valores quaisquer my @ARR = (); for (my $i = 0; $i < $SIZE; $i++) { push @ARR, $i; } #calcula o tamanho da linha my $linha = @ARR / $DIM; my $c = 0; my $val = 0; my $aux = 0; for(my $i = 0; $i < @ARR; $i++) { my $o = ($c++ % $DIM); $val = $aux + ($linha * $o); print "$ARR[$val] "; if (($i + 1) % $DIM == 0) { print "\n"; $aux++; } }
02.019a. Convert photos from high definition to 4x6 inches (or 10x15 cms) - 1024x768 - using convert
#!/usr/bin/perl use strict; use warnings; if (@ARGV != 1) { print "missing PATH parameter. \nusage: perl convert.pl PATH\n"; exit; } my $path = $ARGV[0]; my @contents; opendir MYDIR, $path; @contents = grep !/^\.\.?$/,grep /.JPG/i, readdir MYDIR; closedir MYDIR; system("rm new -rf"); system("mkdir new"); foreach my $file (sort @contents) { my $fp = "$path/$file"; my $aux = $file; $aux =~ s/\./;/; my $filename; my $term; ($filename,$term) = split(";",$aux); my $new_name = $filename."\_.".$term; print "[$aux] converting $file... to $new_name\n"; system("convert -resize 1024x768 $file $new_name"); system("mv $new_name ./new"); }
02.019b. Convert photos from high definition to another size - using Image::Resize
#!/usr/bin/perl use strict; use warnings; use Time::Local; use Cwd qw(); use Image::Resize; #install this package: on command.exe type: cpan Image::Resize #use: perl resize-photos.pl FOLDER #this script will convert JPG images and put in a new folder, created according to current time (folder will be called "FOLDER-dd-mm-yyyy_hh-mm-ss") if (@ARGV != 1) { print "missing OUTPUT-DIR parameter. \nusage: perl resize-photos.pl OUTPUT-DIR\n"; exit; } my $path = $ARGV[0]; #current localtime (my $sec,my $min,my $hour,my $mday,my $mon,my $year,my $wday,my $yday,my $isdst)=localtime(time); my $date = sprintf("%02d-%02d-%4d_%02d-%02d-%02d",$mday,$mon+1,$year+1900,$hour,$min,$sec); #create directory with the current date-time my $dirname = $path."-".$date; print "mkdir \"$dirname\"\n"; system("mkdir \"$dirname\""); my @contents; opendir MYDIR, $path; @contents = grep !/^\.\.?$/,grep /.JPG/i, readdir MYDIR; closedir MYDIR; my $cont = 1; my $size = @contents; foreach my $file (@contents) { print "[".$cont."/".$size."] processing file=$file...\n"; $cont++; my $image = Image::Resize->new($path."\\".$file); my $gd = $image->resize(2400, 2400); $file =~ s/.jpg//ig; my $new_file = $dirname."\\".$file."-new.jpg"; open(DISPLAY, ">$new_file"); binmode DISPLAY; print DISPLAY $gd->jpeg(); close(DISPLAY); } print "converted images in '$dirname' folder.\n";
02.020. Convert movies from MPG ou MOV to AVI format
#!/bin/bash for i in *.[Mm][Pp][Gg]; do filename=${i//.MPG/} mencoder $i -ofps 25 -ovc xvid -oac mp3lame -lameopts abr:br=192 -srate 48000 -xvidencopts fixed_quant=4 -o $filename.avi doneou, para converter de MOV para AVI
#!/bin/bash for i in *.[Mm][Oo][Vv]; do filename=${i//.MOV/} mencoder $i -ovc lavc -lavcopts vcodec=mpeg4:vpass=1 -oac mp3lame -lameopts br=128:cbr:vol=0:mode=0 -o $filename.avi mencoder $i -ovc lavc -lavcopts vcodec=mpeg4:vpass=2 -oac mp3lame -lameopts br=128:cbr:vol=0:mode=0 -o $filename.avi done
02.021. Order hashes by keys or values
#order by keys my %FILES; foreach my $key (sort keys %FILES) { print "$key => $FILES{$key}\n"; } #1. order by values my %FILES; my @keys = sort { $FILES{$b} cmp $FILES{$a} } keys %FILES; foreach my $key (@keys) { print "$key => $FILES{$key}\n"; } #2. order when the key is an IP address, e.g. 200.12.34.3 or 15.189.22.33 my %IP; foreach my $key (sort {(pack'C*',split/\./,$a) cmp (pack'C*',split/\./,$b)} keys %IP) { print "IP = $key => $IP{$key}\n"; } #3. numeric hashes foreach my $key (sort { $processes{$b} <=> $processes{$a} } keys %processes) { }02.022. Permutation Algorithm
#!/usr/bin/perl use strict; use warnings; permute(["x", "a", "b", "c"], []); sub permute { my @items = @{ $_[0] }; my @perms = @{ $_[1] }; unless (@items) { verify(@perms); } else { my(@newitems,@newperms,$i); foreach $i (0 .. $#items) { @newitems = @items; @newperms = @perms; unshift(@newperms, splice(@newitems, $i, 1)); permute([@newitems], [@newperms]); } } } sub verify { my @list = @_; print "@list\n"; }
02.023. Open huge files in Perl
#!/usr/bin/perl use strict; use warnings; open(INFILE, "<$file") or die("cannot open file named $file\n"); print "creating dat file with *hot* vector positions named '$hname'...\n"; while () { if ($_ =~ /vec\[(\d*)\] = (\d*)/) { #do something } } close(INFILE);
02.024. Create a basic gnuplot file in Perl
#!/usr/bin/perl use strict; use warnings; if (@ARGV != 1) { print "missing FILE parameter. \nusage: perl create_gnuplot.pl FILE\n"; exit; } my $file = $ARGV[0]; if (!($file =~ /.log$/)) { print "file extension MUST be .log. exiting.\n"; exit; } print "opening file named '$file'...\n"; my $base = $file; $base =~ s/.log$//; #create a gnuplot my $gname = "$base.plot"; print "creating new gnuplot file named '$gname'\n"; open(OUTFILE, ">$gname") or die("cannot open $gname\n"); print OUTFILE "set terminal postscript enhanced color\n"; print OUTFILE "set output \"$base.eps\"\n"; #print OUTFILE "set logscale y\n"; print OUTFILE "set key right top\n"; print OUTFILE "set ylabel \"\# of vector access\"\n"; print OUTFILE "set xlabel \"Index\"\n"; print OUTFILE "set style line 1 lt 5 lc rgb \"#0000FF\" lw 1\n"; print OUTFILE "plot \\\n"; print OUTFILE " \"$base.hot\" using 1:2 ls 1 with impulses title \"Accesses\"\n"; close(OUTFILE); system("gnuplot $base.plot"); system("evince $base.eps");
02.025. Read folder, iterate through subfolders, then its files
#!/usr/bin/perl use strict; use warnings; if (@ARGV != 1) { print "missing parameter DATA_DIR\nusage: perl iterate.pl DATA_DIR\n"; exit; } my $path = "./".$ARGV[0]; print "reading directory $path ...\n"; my @dirs; opendir DIR, $path; @dirs = grep !/^\.\.?$/,grep /test/, readdir DIR; closedir DIR; foreach my $dir (sort @dirs) { print "directory read $dir\n"; my @files; opendir FILE, $path.$dir; @files = grep !/^\.\.?$/,, readdir FILE; closedir FILE; foreach my $file (sort @files) { print "\tfile read $dir/$file\n"; } }
02.026. Pattern matching of datetime format (e.g. 2010-10-13 17:55:39,666)
if ($timestamp =~ /^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2}),(\d{3})$/) { $Y = $1; $M = $2; $D = $3; $H = $4; $MI = $5; $S = $6; $CCC = $7; print "format: d=$D m=$M y=$Y h=$H mi=$MI s=$S ccc=$CCC\n"; }
02.027. Passing two hashes to a function
... compute(\%statespace, \%mc); sub compute { my %statespace = %{ $_[0] }; my %mc = %{ $_[1] }; #... }
02.028. Checks if number is an integer (greater than zero)
# checks if number is an integer greater than zero sub isInteger { my $d = shift; return 1 if ($d =~ /^(\d+)$/); return 0; } # validates integer or float numbers: i.e. 35 or 87.53 sub isIntegerOrFloat { my $p = shift; return 1 if ($p =~ /^\d+(\.\d+)?(\s*)$/gi); return 0; }
02.029. Checks two datetime parameters ('yyyy mm dd hh:mm:ss') are valid
use Time::Local; use Cwd qw(); # checks if an interval of dates are correct, i.e., end is after start # Parameters: first is the start time # second is the stop time sub isValidDateInterval { my $d1_ = shift; my $d2_ = shift; my($yy1,$mm1,$dd1,$h1,$m1,$s1) = (0,0,0,0,0,0); $d1_ =~ m/^(\')(\d{4})\s(\d{2})\s(\d{2})\s(\d{2}):(\d{2}):(\d{2})(\')$/g; $yy1=$2; $mm1=$3; $dd1=$4; $h1=$5; $m1=$6; $s1=$7; #print "date1: $dd1/$mm1/$yy1 $h1:$m1:$s1\n"; my $seconds1 = timelocal($s1,$m1,$h1,$dd1,$mm1-1,$yy1); #print "seconds1=$seconds1\n"; my($yy2,$mm2,$dd2,$h2,$m2,$s2) = (0,0,0,0,0,0); $d2_ =~ m/^(\')(\d{4})\s(\d{2})\s(\d{2})\s(\d{2}):(\d{2}):(\d{2})(\')$/g; $yy2=$2; $mm2=$3; $dd2=$4; $h2=$5; $m2=$6; $s2=$7; #print "date2: $dd2/$mm2/$yy2 $h2:$m2:$s2\n"; my $seconds2 = timelocal($s2,$m2,$h2,$dd2,$mm2-1,$yy2); #print "seconds2=$seconds2\n"; #print "diff=".($seconds2-$seconds1)."\n"; return 1 if ($seconds2-$seconds1>0); return 0; }
02.030. Move files without system call
use File::Copy; move("mc-$target_year.txt", "output/mc-$target_year.txt") or die "Move mc-$target_year.txt -> output/mc-$target_year.txt failed: $!";
02.031. Working with XML file and parameters
#!/usr/bin/perl use strict; use warnings; use XML::LibXML; if (@ARGV != 1) { print "missing FILE parameter. \nusage: perl test.pl FILE\n"; exit; } my $file = $ARGV[0]; my $dom = XML::LibXML->load_xml(location => $file); my $parser = XML::LibXML->new(); my $tree = $parser->parse_file($file); my $root = $tree->getDocumentElement; foreach my $id ($root->findnodes('//AMC/PROBABILITES_SEQUENCES/SEQUENCE')) { my $prob = $id->findvalue('@PROBABILITE'); print "Probability: $prob\n"; foreach my $trans ($id->findnodes('./BRANCHE/TRANSITION')) { my $objet = $trans->findvalue('@OBJET'); my $realization = $trans->findvalue('@TRANS'); print "\tObject: $objet\n"; print "\tRealization: $realization\n"; } } XML test file: <AMC> <PROBABILITES_SEQUENCES> <SEQUENCE TRONQUEE="CIBLE" PROBABILITE="0.22983"> <BRANCHE DATE_FIN_MOY="2595.97313362042"> <TRANSITION OBJET="acquire" REGLE="success_undetected" TRANS="real"/> </BRANCHE> <BRANCHE DATE_FIN_MOY="2595.97313362042"> <TRANSITION OBJET="data" REGLE="real_nd" TRANS="no_realization"/> <TRANSITION OBJET="abs" REGLE="real_nd" TRANS="nd_real"/> <TRANSITION OBJET="muscular" REGLE="real_nd" TRANS="no_realization"/> </BRANCHE> </SEQUENCE> <SEQUENCE TRONQUEE="CIBLE" PROBABILITE="0.10047"> <BRANCHE DATE_FIN_MOY="2588.08094838842"> <TRANSITION OBJET="balance" REGLE="success_undetected" TRANS="real"/> </BRANCHE> <BRANCHE DATE_FIN_MOY="2588.08094838842"> <TRANSITION OBJET="data" REGLE="real_nd" TRANS="nd_real"/> <TRANSITION OBJET="abs" REGLE="real_nd" TRANS="nd_real"/> <TRANSITION OBJET="muscular" REGLE="real_nd" TRANS="no_realization"/> </BRANCHE> </SEQUENCE> </PROBABILITES_SEQUENCES> </AMC>
02.032. (classic) Permutation algorithm
#!/usr/bin/perl use strict; use warnings; #incredible permutation algorithm! See Knuth. my %parameters = ( "123.456" => "1;2;10", "123.457" => "2;8", "123.458" => "3;12", "123.459" => "5;10", ); # start of permutation algorithm for creating all the possible scenarios from the parameters (above) my $tam = keys %parameters; my @scenarios; my $cont = 0; my @keys; foreach my $key (keys %parameters) { my @arr = split(";", $parameters{$key}); $scenarios[$cont] = @arr; #gets the size of the variations $keys[$cont] = $key; $cont++; } my @pgs; #initialisation for (my $i=0;$i<$tam;$i++) { $pgs[$i] = 0; } my @vv; $vv[0] = $scenarios[0]; for (my $i=1;$i<$tam;$i++) { $vv[$i] = $scenarios[$i] - 1; } #algorithm 'per se' $pgs[$tam-1] = -1; my $a = $tam - 1; my $nz; $cont = 0; my @idx_scenarios = (); while ($pgs[0] != $vv[0]) { if ($pgs[$a] < $vv[$a]) { $pgs[$a] = $pgs[$a] + 1; $a = $tam - 1; if ($pgs[0] != $vv[0]) { my $str = ""; for (my $i = 0; $i < $tam; $i++) { $str .= "$pgs[$i]"; $str .= $i==$tam-1?"":";"; } #print "".($cont++).": $str\n"; push @idx_scenarios, $str; } } else { $a--; #restart from a+1 till the end with zeroes for (my $i = $a + 1; $i < $tam; $i++) { $nz = ($pgs[$i] != -1) ? 0 : -1; $pgs[$i] = $nz; } } } foreach (my $i=0; $i < @keys; $i++) { print "Key: $keys[$i] Size: $scenarios[$i]\n"; } print "\nIndices: \n"; print "A total of ".(@idx_scenarios)." scenarios was created:\n"; $cont = 1; foreach my $elem (@idx_scenarios) { print "[".($cont++)."]: $elem\n"; } #accessing values print "\nScenarios: "; $cont = 0; print "\n"; for (my $i=0; $i < @idx_scenarios; $i++) { $cont++; my @aux = split(";", $idx_scenarios[$i]); print "[".$cont."] "; for (my $j=0; $j < @aux; $j++) { my @elem = split(";",$parameters{$keys[$j]}); print "$keys[$j]=$elem[$aux[$j]]"; print ($j==@aux-1?"":", "); } print "\n"; }
02.033. Sub-routines in Perl
On each file, add
require './bdmp-routines.pl';And on this file, add all sub (sub-routines), and end the file with:
1;(otherwise, it won't work)
03. C/C++ programming
03.001. Solve an infinitesimal generator
Download file here -> Coding language: C++.
Obs.: the file must have the following format:
q1 n9 -2 2 0 0 0 0 0 0 0 0 -6 1 5 0 0 0 0 0 0 0 -10 0 10 0 0 0 0 0 0 0 -6 6 0 0 0 0 5 0 0 0 -13 3 5 0 0 0 0 0 0 0 -5 0 5 0 0 0 0 0 0 0 -4 4 0 0 0 0 5 0 0 0 -7 2 0 0 0 0 5 0 0 0 -5
The call for the executable is: ./aps ITERATIONS FILE
Example: ./aps 100 file.txt
The output will be the resulting vector.
03.002. Removing equals from a list
#include <vector> #include <list> #include <iostream> #include <iomanip> #include <fstream> #include <stdlib.h> #include <math.h> #include <stdio.h> #include <time.h> #include <assert.h> using namespace std; //for minimum and maximum float value #include <float.h> std::vector<int> removeequals(int *y, int size) { std::list<int> l; for (int i = 0; i < size; i++) { l.push_back(y[i]); } l.sort(); l.unique(); std::vector<int> v(l.begin(), l.end()); return v; } int main(char argc, char** argv) { if (argc != 1) { printf("Usage: ./list\n\n"); //printf("FILE file name to be open\nmodel - model to be used\nruns - number of executions\n"); exit(0); } int *y = (int*)malloc(sizeof(int)*10); y[0] = 15; y[1] = 11; y[2] = 11; y[3] = 12; y[4] = 11; y[5] = 14; y[6] = 11; y[7] = 14; y[8] = 12; y[9] = 11; std::vector<int> newv = removeequals(y, 10); for (int i = 0; i < newv.size(); i++) { int value = (int) newv[i]; cout << "value["<<i<<"]=" << value << endl; } return 0; }
03.003. Removing chars from string
string StringUtils::removechar(const char r, const string& in) { string out = in; while (out.find(r) != string::npos) { string::size_type pos = out.find(r); out.erase(pos,1); } return out; }
03.004. Converting string to upper or lower case
string lcase(const string& in) { string out = in; std::transform(out.begin(), out.end(), out.begin(), ::tolower); return out; } string ucase(const string& in) { string out = in; std::transform(out.begin(), out.end(), out.begin(), ::toupper); return out; }
03.005. sampler - ferramenta para geração de samples da distribuição uniforme
baixe aqui a ferramenta
4. Maple/Matlab/Octave
04.001. Solve a pepa model in Maple
n:=2; l:=1; r:=3; p:=5; t:=4; with(linalg): Q := array(sparse,1..96,1..96): read `mobile2agents.maple`: b := array(sparse,1..96): for i to 96 do Q[i,96] := 1.0 od: b[96] := 1: QT := transpose(Q): P := linsolve(QT,b);
n:=2; l:=1; r:=3; p:=5; t:=4; with(linalg): Q := array(sparse,1..96,1..96): read `mobile2agents.maple`: b := array(sparse,1..96): for i to 96 do Q[i,96] := 1.0 od: b[96] := 1: QT := transpose(Q): P := linsolve(QT,b);
04.002. Solve a Markov Chains model in Octave
#author: Ricardo M. Czekster #created: 01/27/2010 clear; #infinitesimal generator definition #every line must sums to zero (diagonal has the negative sum of the line) Q = [-109.3154, 100, 0, 0, 0, 9.1854, 0, 0, 0.13, 0, 0; 200, -205.13, 5, 0, 0, 0, 0, 0, 0, 0, 0.13; 0, 0.0004, -135.0004, 8, 0, 0, 127, 0, 0, 0, 0; 0, 0, 41.58, -58.58, 17, 0, 0, 0, 0, 0, 0; 0, 0, 0, 1.69, -3.04, 1.35, 0, 0, 0, 0, 0; 87, 0, 0, 0, 0.18, -87.18, 0, 0, 0, 0, 0; 0, 0, 14, 0, 0, 0, -17.5, 3.5, 0, 0, 0; 0, 0, 0, 0, 0, 0, 4.2336, -104.4436, 100, 0.21, 0; 100, 0, 0, 0, 0, 0, 0, 419.58, -519.58, 0, 0; 0, 0, 0, 0, 0, 0, 0, 2.77, 0, -10.57, 7.8; 0, 0.013, 0, 0, 0, 0, 0, 0, 0, 30.24, -30.253]; #just saves original matrix before manipulation (uncomment here if needed) #O = Q; #last column set to one for i=1:11 Q(i,11) = 1; endfor b = [0; 0; 0; 0; 0; 0; 0; 0; 0; 0; 1]; #transpose matrix Q QT = Q'; #linear solution (results in vector P) P = QT \ b; #probability output (times 100) #P *= 100;
04.003. Print in file using Octave
#create a file with .m extension 1; start = 1; lmax = 100; X_t = zeros(1,lmax); #create a file named out.txt with n and X as values from start to lmax filename = "out.txt"; fid = fopen (filename, "w+"); fprintf(fid,"#n X\n"); i = 1; for n=start:lmax fprintf(fid,"%d %f\n",n,X_t(i)); i = i + 1; endfor fclose(fid);
05. Latex
05.001. Figures in Latex
\begin{figure}[!htb] \centering \includegraphics[scale=1]{image.png} \caption{This is a caption.} \label{fig:label} \end{figure}
05.002. Putting vertical labels in tables
\usepackage{rotating} ... \begin{tabular}{|r|r|}\hline \begin{sideways}Paper\end{sideways} &\begin{sideways}Static\end{sideways} \\ \hline HAR1994j & Journal \\ SWRT1996c & Conference \\ \hline \end{tabular}
05.003. Script for cleaning latex files and compiling latex
Cleaning a latex directory with a bash script -> Script language: bash.
Compiling latex from a bash script -> Script language: bash.
05.004. Full latex project for Lecture Notes in Computer Science format
Template for LNCS project -> Latex files.
05.005. Highlighting paragraphs
05.006. Adding fancy notes
\usepackage{todonotes} \reversemarginpar \newcommand{\observation}[2][Rand]{\todo[author={\tiny\hfill\textbf{#1}},fancyline,size=\small,color=black!20,bordercolor=black!20]{\scriptsize #2}} \newcommand{\obs}[2][Rand]{{\observation[#1]{#2}}} %use: \obs[RPI]{Observe this.} where RPI = Random Person Initials
05.007. Better cell alignment with tabular
Put this inside the cell (between &): (it's noisy but it works!)
06. Beamer presentations
06.001. A presentation in Beamer, with multiple slides
Download presentation+figures
Link to some useful tips
06.002. Beamer complete list of themes
To use it, change the following line:
\usetheme{THEME}
I particularly like the Frankfurt theme. It is the most classic. He is without sections, only balls to mark where you are in the presentation.
AnnArbor Antibes Bergen Berkeley Berlin Boadilla CambridgeUS Copenhagen Darmstadt Dresden Frankfurt Goettingen Hannover Ilmenau JuanLesPins Luebeck Madrid Malmoe Marburg Montpellier PaloAlto Pittsburgh Rochester Singapore Stuttgart Szeged Warsaw
07. Miscelaneous
07.001. 07. A Makefile without having to tell all .c files
# # Makefile of XXXX XXXX # Changed by xxxx@xxx.xxxxx.xx # CPP=g++ INCLUDE_DIR = ./include BIN_DIR = . OBJ_DIR = . LIB_DIR = ./lib # The options of compilation (debug or optimization) CPPFLAGS= -I/usr/local/include -Wno-non-virtual-dtor -Wno-deprecated -O3 -I$(INCLUDE_DIR) # The options of linkage LDFLAGS= -L$(LIB_DIR) -lyourlib SOURCES := $(patsubst %.cpp,%.o,$(wildcard *.cpp)) OBJECTS=$(SOURCES:.cpp=.o) EXECUTABLE=hello compile: $(SOURCES) $(EXECUTABLE) $(EXECUTABLE): $(OBJECTS) $(CPP) $(LDFLAGS) $(OBJECTS) -o $@ .cpp.o: $(CPP) $(CPPFLAGS) -c $< -o $@ all: compile clean: rm -f *.o hello
07.002. Convert a .wav to .mp3 using lame
for i in *.wav; do lame --preset standard $i `basename $i .wav`.mp3; done
07.003. Create a small book from a ps file
psbook IN OUT psnup -pa4 -2 IN OUT
07.004. Merging PDFs
gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=output.pdf file1.pdf file2.pdf
07.005. Converting between file formats
iconv --from-code=ISO-8859-1 --to-code=UTF-8 ./file.txt > out.txt
07.006. Use regex in Notepad++
Example of use of Arena's output (in Portuguese):
Open Search Dialog (Ctrl-F) then change 'Search Mode' to "Regular expression"
Atendimento no Caixa.Queue.NumberInQueue.*\s*\.\d*
08. GNUplot
08.001. 08. General GNUplot scripting
Positioning legends in figure:
set key { left | right | top | bottom | outside | below }
Second axis y:
plot "ad.txt" using 1:2 axes x1y2 ls 1 with boxes title "Experiment 2 - 9"
Columns operations:
plot \ "ad.txt" using 1:($3-$2) with boxes title "Experiment [3 - 2]", \ "" using 1:2 with lines title "Experiment 2" file "ad.txt" #(note that it has 3 columns, ie, $1, $2, $3): 1 10 2 2 15 5 3 21 10 4 15 8 5 11 10 6 9 3 ...
Line styles example:
set term postscript eps color set output "ad.eps" set logscale xy set xrange[9.5:16.5] set key right bottom set grid set style line 1 lt 1 lc rgb "red" lw 3 set style line 2 lt 2 lc rgb "blue" lw 1 set style line 3 lt 3 lc rgb "yellow" lw 3 set style line 4 lt 4 lc rgb "#aabbcc" lw 1 set style line 5 lt 5 lc rgb "grey" lw 1 plot \ "ad.txt" using 1:2 ls 1 with lines title "Experiment 1", \ "" using 1:3 ls 2 with lines title "Experiment 2", \ "" using 1:4 ls 3 with lines title "Experiment 3", \ "" using 1:5 ls 4 with lines title "Experiment 4", \ "" using 1:6 ls 5 with lines title "Experiment 5"
Multiplot example:
set term postscript eps color set output "results.eps" set multiplot layout 1, 2 title "Results for Master-Slave Model" ###################################################################### set title "Time (s) - loglog scale" set logscale xy set xrange[4.5:10.5] set key left top plot \ "slaves.txt" using 1:2 with lines title "Exp. 1", \ "" using 1:3 with lines title "Experiment 2", \ "" using 1:4 with lines title "Experiment 3", \ "" using 1:5 with lines title "Experiment 4", \ "" using 1:6 with lines title "Experiment 5" ###################################################################### set title "Memory (Kb) - loglog scale" set logscale xy set xrange[4.5:10.5] set key left top plot \ "slaves.txt" using 1:11 ls 1 with lines title "Exp. 1", \ "" using 1:12 ls 3 with lines title "Exp. 3", \ "" using 1:13 ls 5 with lines title "Exp. 5", \ "" using 1:14 ls 6 with lines title "Exp. 6", \ "" using 1:15 ls 7 with lines title "Exp. 7", \ "" using 1:16 ls 8 with lines title "Exp. 8"
Animation (.gif) and drawing a matrix:
set terminal gif animate delay 1 set output "floripa_certo.gif" #set view 60,60 set view 60,45 #set view 0,180 #set nosurface #set contour base #set dgrid3d set surface set pm3d set cntrparam levels auto 30 splot "floripa_certo_1.dat" matrix with lines splot "floripa_certo_501.dat" matrix with lines splot "floripa_certo_1001.dat" matrix with lines
Multiplot with single axis X and the rest 'stacked'
set terminal postscript set output "data.ps" unset key NX=1; NY=3 DX=0.01; DY=0.01; SX=0.85; SY=0.25 set bmargin DX; set tmargin DX; set lmargin DY; set rmargin DY ## set the margin of each side of a plot as small as possible ## to connect each plot without space set size SX*NX+DX*1.5,SY*NY+DY*1.8 set multiplot ## First Figure-bottom set size SX,SY set xrange [0:140] set yrange [-0.9:0.9] set ytic -0.6,0.3,0.6 ### from -0.6 to 0.6 with 0.3 interval set origin DX,DY; plot sin(x) ###Second Figure-middle set origin DX,DY+SY; unset xtics plot sin(x**2) ##- Third Figure-top set origin DX,DY+SY*2 plot cos(x) unset multiplot
09. R
09.001. Several scripts
Standard commands:
#used for the transpose function library(data.table) #remove all objects defined earlier rm(list=ls(all=TRUE)) #remove all plots in the environment graphics.off() #set working directory setwd("C:\\Users\\stout\\Desktop\\Rscripts") outputfile = "output-longstudy.txt" #read .csv file data <- read.table(outputfile, header=FALSE, sep=";") #count entries maxf = max(count.fields(outputfile, sep = ';')) #read to variable, adding "V#" into each column data <- read.table(outputfile, header = FALSE, sep = ";", col.names = paste0("V",seq_len(maxf)), fill = TRUE) #save first column with the path names pathnames <- data[,1] #remove first column data$V1 <- NULL #transpose the data, because each column corresponds to a probab. over time t_data <- transpose(data) #--------------------------------------------- #summary of one column (named V4) summary(t_data$V4) #summary of all data summary(data) graphics.off() #selecting only observations in column 'V5' higher than 0.001 high_prob <- data[data$V5 > 0.001, ] #plotting a boxplot on that variable boxplot(high_prob) #plotting two simple lines on column 'V5' plot(t_data$V5,type = "l", lty = 1, col="red") lines(t_data$V2,type = "l", lty = 1, col="blue") ############## graphics.off() #plotting MULTIPLE data series using matplot (use of cbind function) matplot(cbind(t_data1,t_data2), type = c("l"),main="Ensemble of attack paths",cex.main=1.5, pch=1,col = 2:32,xlim=c(1,36), xaxs="i", yaxs="i", ylim=c(0,0.011),ylab="Path probability",xlab="Time (in hours)",axes=F) box() axis(1, seq(0,36,1),las=1, cex.axis=0.75, font=1) #use in conjunction with ylim (above) --> 0.011 axis(2, seq(0,0.011,0.01),cex.axis=0.85, las=2)
10. MS-Windows stuff
10.001. Batch script to test HTTP code of a URL
File: test-url.bat
ECHO OFF ::CMD will no longer show us what command it’s executing(cleaner) ECHO This is a cURL script. :: Print some text FOR /L %%i IN (1,1,200000) DO ( echo | set /p= [%%i] & curl -LI https://www.google.com -o /dev/null -w %%{http_code}\n -s timeout 60 > nul :: set the timeout to wait between requests )