DIR = src/kernel/tab
BIN = tabmath tabplot tabhist tabspline tablsqfit tabnllsqfit tabdate \
      tabfilter tabtrend gauss1d gauss2d meanmed tabstat

NEED = $(BIN) nemoinp

help:
	@echo $(DIR)

need:
	@echo $(NEED)

clean:
	@echo Cleaning $(DIR)
	@rm -f tab.in tab.out gauss1d.tab gauss2d.tab fit/myline.so

all:	tab.in $(BIN) fitmyline

tab.in:
	@echo Creating $@
	$(EXEC) nemoinp 1:100 > tab.in

tabmath: tab.in
	@echo Running $@
	$(EXEC) tabmath tab.in tab.out 'sqrt(%1)'; nemo.coverage tabmath.c

tabplot:
	@echo Running $@
	$(EXEC) tabmath tab.in - 'sqrt(%1)' | $(EXEC) tabplot - ; nemo.coverage tabplot.c

NMAX = 100000

tabhist:
	@echo Running $*
	$(EXEC) nemoinp 1:1000 | $(EXEC) tabmath - - 'rang(0,1)' all seed=123 | $(EXEC) tabhist - ; nemo.coverage tabhist.c
	$(EXEC) nemoinp 1:$(NMAX) nmax=$(NMAX) | $(EXEC) tabmath - - 'rang(0,1)' all seed=123 | $(EXEC) tabhist - nmax=-$(NMAX); nemo.coverage tabhist.c

tabstat:
	@echo Running $*
	$(EXEC) nemoinp 1:1000 | $(EXEC) tabmath - - 'rang(0,1)' all seed=123 | $(EXEC) tabstat - ; nemo.coverage tabstat.c
	$(EXEC) nemoinp 1:$(NMAX) nmax=$(NMAX) | $(EXEC) tabmath - - 'rang(0,1)' all seed=123 | $(EXEC) tabstat - nmax=-$(NMAX); nemo.coverage tabstat.c
	


meanmed:
	@echo Running $*
	$(EXEC) nemoinp 1:1000 | $(EXEC) tabmath - - 'rang(0,1)' all seed=123 | $(EXEC) meanmed - ; nemo.coverage meanmed.c


tabspline:
	@echo Running $*
	$(EXEC) nemoinp 1:2:0.001 | $(EXEC) tabmath - - '%1*%1' | $(EXEC) tabspline - y=2;  nemo.coverage tabspline.c

tablsqfit:
	@echo Running $*
	$(EXEC) nemoinp 1:2:0.001 | $(EXEC) tabmath - - '%1+rang(0,0.1)' seed=123 | $(EXEC) tablsqfit - ; nemo.coverage tablsqfit.c

tablsqfit_gsl:
	@echo Running $*
	$(EXEC) nemoinp 1:2:0.001 | $(EXEC) tabmath - - '%1+rang(0,0.1)' seed=123 | $(EXEC) tablsqfit_gsl - ; nemo.coverage tablsqfit.c

tabnllsqfit:
	@echo Running $*
	$(EXEC) nemoinp 1:100 | $(EXEC) tabmath - - '4+exp(-(%1-50)**2/(200))+rang(0,0.1)' seed=123 |\
		$(EXEC) tabnllsqfit - fit=gauss1d par=4,1,50,10; nemo.coverage tabnllsqfit.c

tabdate:
	@echo Running $*
	@echo 1000000000 | $(EXEC) tabdate - - %s %c

tabfilter:
	@echo Running $*
	$(EXEC) tabfilter g tbb=1000

tabtrend:
	@echo Running $*
	@nemoinp 0:4 | $(EXEC) tabmath - - '%1*%1' all | $(EXEC) tabtrend -

#   testing object loading load= in tabnlsqfit from the fit/ directory
fitmyline_local:
	(cd fit; make -f $(NEMOLIB)/Makefile.lib myline.so)
	tabnllsqfit tab.out
	tabnllsqfit tab.out load=fit/myline.so par=1,1

#   testsuite 
fitmyline:
	tabnllsqfit tab.out
	tabnllsqfit tab.out load=$(NEMOOBJ)/fit/myline.so par=1,1

SIZE=4
NOISE=0.01

gauss1d:
	@echo "Running $* with gauss1d(B=2 A=3 x0=1 b=1)"
	nemoinp 0:$(SIZE) | tabmath - - '0+3*exp(-((%1-1)**2)/2)+rang(0,$(NOISE))' seed=123 > gauss1d.tab
	tablsqfit   gauss1d.tab fit=gauss1d
	tabnllsqfit gauss1d.tab fit=gauss1d par=0,3,2,1 free=0,1,1,1
	tabnllsqfit gauss1d.tab fit=gauss1d par=0,3,2,1 free=1,1,1,1

gauss2d:
	@echo "Running $* with gauss2d(B=0 A=3 x0=1 y0=1 b=1)"
	ccdmath "" - "0+3*exp(-((%x-1)**2+(%y-1)**2)/2)+rang(0,$(NOISE))" seed=123 size=$(SIZE),$(SIZE) |\
	    ccdprint - x= y= label=x,y newline=t > gauss2d.tab
	tablsqfit   gauss2d.tab 1,2 3 fit=gauss2d
	tabnllsqfit gauss2d.tab 1,2 3 fit=gauss2d par=0,3,1,1,1 free=0,1,1,1,1
	tabnllsqfit gauss2d.tab 1,2 3 fit=gauss2d par=0,3,1,1,1 free=1,1,1,1,1
