DLLS = $(addprefix -reference:,$(filter %.dll,$^))
CODE = $(filter %.cs,$^)
MKEXE = mcs -target:exe -out:$@ $(DLLS) $(CODE)
MKDLL = mcs -target:library -out:$@ $(DLLS) $(CODE)

all: CircleErrorPlainMC.png CircleIntegralPlainMC.png \
	CosinesErrorPlainMC.png CosinesIntegralPlainMC.png \
	CosinesErrorQuasi.png \
	Points.png

CircleErrorPlainMC.png: out.circle.txt Makefile
	echo '\
	set term png size 600 notransparent notruecolor enhanced;\
	set out "$@";\
	set title "Plainmc error, integral of 1/4th unit circle";\
	set xlabel "number of sampling points";\
	set ylabel "error";\
	set logscale xy;\
	set mxtics 10 ;\
	set format x "%.tx10^{%T}" ;\
	aa=0.1; qa=-0.5; fa(n) = aa*n**qa;\
	fit fa(x) "$<" using 1:4 via aa;\
	ae=1; qe=-0.5; fe(n) = ae*n**qe;\
	fit fe(x) "$<" using 1:3 via ae,qe;\
	plot [1000:]\
	 "$<" u 1:3 with lines title "estimated error"\
	,"$<" u 1:4 with linespoints title "actual error"\
	,fa(x) with lines title "   fit to actual error: n^{".sprintf("%.2f",qa)."}"\
	,fe(x) with lines title "fit to estimated error, n^{".sprintf("%.2f",qe)."}"\
	'| gnuplot

CosinesErrorPlainMC.png: out.cosines.txt Makefile
	echo '\
	set term png size 600 notransparent notruecolor enhanced;\
	set out "$@";\
	set title "Plainmc error, cosines integral from GSL manual";\
	set xlabel "number of sampling points";\
	set ylabel "error";\
	set logscale xy;\
	aa=1; qa=-0.5; fa(n) = aa*n**qa;\
	fit fa(x) "$<" using 1:4 via aa,qa;\
	ae=1; qe=-0.5; fe(n) = ae*n**qe;\
	fit fe(x) "$<" using 1:3 via ae,qe;\
	plot [1000:]\
	 "$<" u 1:3 with lines title "statistical error"\
	,"$<" u 1:4 with linespoints title "actual error"\
	,fa(x) with lines title "   fit to actual error: n^".sprintf("%.2f",qa)\
	,fe(x) with lines title "fit to estimated error, n^".sprintf("%.2f",qe)\
	'| gnuplot

CosinesErrorQuasi.png: out.cosines.quasi Makefile
	echo '\
	set term png size 600 notransparent notruecolor noenhanced;\
	set out "$@";\
	set title "Lattice error, cosines integral from GSL manual";\
	set xlabel "number of sampling points";\
	set ylabel "error";\
	set logscale y;\
	aa=1; qa=-0.5; fa(n) = aa*n**qa;\
	fit fa(x) "$<" using 1:4 via aa,qa;\
	ae=1; qe=-0.5; fe(n) = ae*n**qe;\
	fit fe(x) "$<" using 1:3 via ae,qe;\
	plot [2000:][:]\
	 "$<" u 1:3 with lines title "estimated error"\
	,"$<" u 1:4 with linespoints title "actual error"\
	,fa(x) with lines title "   fit to actual error: n^".sprintf("%.2f",qa)\
	,fe(x) with lines title "fit to estimated error, n^".sprintf("%.2f",qe)\
	'| gnuplot

CircleIntegralPlainMC.png: out.circle.txt Makefile
	echo '\
	set term png size 600 notransparent notruecolor enhanced;\
	set out "$@";\
	set xlabel "number of points";\
	set ylabel "value of the integral";\
	set logscale x ;\
	plot \
	 "$<" with linespoints title "Plainmc integration of 1/4th unit circle"\
	,pi/4 with lines title "exact"\
	'| gnuplot

CosinesIntegralPlainMC.png: out.cosines.txt Makefile
	echo '\
	set term png size 600 notransparent notruecolor enhanced;\
	set out "$@";\
	set xlabel "number of points";\
	set ylabel "value of the integral";\
	plot [1000:]\
	 "$<" with linespoints title "Plainmc cosines integral from GSL manual"\
	,1.3932039296856768591842462603255 with lines title "exact"\
	'| gnuplot

SEQ0 := $$(seq 1250     250    9500)
SEQ0 += $$(seq 10000    2500   95000)
SEQ0 += $$(seq 100000   25000  550000)
out.circle.txt: main.exe
	>$@
	for N in $(SEQ0) ;\
	do echo $$N; mono $< -N $$N -j 4 -f 0 >> $@;\
	done

SEQ2 := $$(seq 1250     250    9500)
SEQ2 += $$(seq 10000    2500   95000)
SEQ2 += $$(seq 100000   25000  550000)
out.cosines.txt: main.exe
	>$@
	for N in $(SEQ2) ;\
	do echo $$N; mono $< -N $$N -j 1 -f 1 >> $@;\
	done

SEQ := $$(seq 1000 250 10000)
out.cosines.quasi: qmain.exe
	>$@
	for N in $(SEQ) ;\
	do echo $$N; mono $< $$N >> $@;\
	done

main.exe: main.cs plainmc.cs matrix.dll ; $(MKEXE)
qmain.exe: qmain.cs halton.cs lattice.cs matrix.dll ; $(MKEXE)
matrix.dll: ../matrix/vector.cs ../matrix/matrix.cs ; $(MKDLL)

Points.png: out.quasi.txt out.pseudo.txt Makefile
	echo '\
	set term png size 600 notransparent notruecolor enhanced;\
	set out "$@";\
	set tics out;\
	set xtics 0.2;\
	set ytics 0.2;\
	set multiplot;\
	set size 0.5,0.5;\
	set origin 0.0,0.5;\
	plot "out.pseudo.txt" with points pointtype 5 pointsize 0.2 notitle;\
	set size 0.5,0.5;\
	set origin 0.5,0.5;\
	plot "out.quasi.txt" with points pointtype 5 pointsize 0.2 notitle;\
	'|gnuplot

N = 1000
out.quasi.txt out.pseudo.txt: points.exe Makefile
	mono $< $(N) 1> out.quasi.txt 2>out.pseudo.txt

points.exe: points.cs halton.cs matrix.dll ; $(MKEXE)

clean:
	$(RM) *.exe *.dll *.png [Oo]ut* *.log [Ll]og*

test:
	echo $(SEQ)
