##USAGE	y = zigf( x {,m=0 {,p=2*pi}} )
##
##	zigzag-function     -- /\/\/\ --
##
##INPUT
##m     mode of phase   x: 0 p/4 p/2 3/4p
##      0 : cosine         1  0  -1   0
##      1 :   sine         0  1   0  -1
##p	(scalar) period
##
##NOTES	* works elementwise  on matrices x
##	* very FAST algorithm (no if-loops)
##	* zigf(x,1)./zigf(x) follows tan(x)
##
##ASSOC	recf
##AUTHOR (C) 2000 Rolf Fabian <fabian@tu-cottbus.de> 0519

#EXA	N=501; t=linspace(-5,5,N)'; 
#	plot(t,[zigf(t),cos(t),zigf(t,1),sin(t)])
#	plot(t,[zigf(t,1)./zigf(t),tan(t)])


#990404 * argument number reduced
#	* intro fast calculation using 'abs' and 'floor'
#000519   minor changes

function y=zigf(x,m,p)

	if	(nargin>3||nargin<1) fprintf(stderr,\
		"USAGE y=zigf(x {,m=0 ,p=2*pi}} ) .. quit");
		y=[]; return;
	endif
	if	(nargin<3) p=2*pi; endif
	if	(nargin<2) m   =0; endif

	if	(m) x=x-p/4;	   endif	# define sine-phase

	y = 4*(abs(x/p-floor(x/p)-1/2)-1/4);

endfunction
