Even and odd functions



I have sent a previous response as a new thread. That was very clumsy of 
me and I'm sorry.

To close this one, let me remark that Prof. Willis is right in his 
comments, but calling the function as paridad(h) -without further 
modifications- neither works, but here is a possible solution.

The following function (now called "parity") admits a piecewise-defined
function on an interval [-l,l] and returns 0 if it is even, 1 if it is 
odd and -1 in another case. It does not matter in what order the user 
defines the subintervals of [-l,l]:

(%i1) parity(f):=block(
          local(z,L),
	 L:makelist(part(f(x),i),i,makelist(2*k-1,k,1,(length(f(x))-2)/2)),
	 for j:1 thru length(L) step 1 do
		(
		 M[j]:makelist(part(L[j],k),k,1,length(L[j])),
		 for m:1 thru 2 do N(j,m):=makelist(part(M[j],m,n),n,1,length(M[j])),
		 P[j]:append(N(j,1),N(j,2)),Q[j]:delete(x,P[j])
		 ),
	 l:lmax(unique(flatten(makelist(Q[q],q,1,length(L))))),
          assume(0<z and z<l),
          if is(equal(f(-z),f(z))) then 0 elseif is(equal(f(-z),-f(z))) 
then 1 else -1
)$
(%i2) g(x):=if (x>=-%pi and x<0) then 0 elseif (x>=0 and x<%pi) then 1$
(%i3) h(x):=if (x>=-%pi and x<0) then x+%pi elseif (x>=0 and x<%pi) then 
%pi-x$
(%i4) k(x):=if (0>x and x>=-%pi) then x+%pi elseif (0<=x and %pi>x) then 
%pi-x$
(%i5) parity(g);
(%o5) -1
(%i6) parity(h);
(%o6) 0
(%i7) parity(k);
(%o7) 0

By the way, I need this to work with Fourier series of piecewise-defined 
functions. That was the motivation for considering this problem.

Thank you very much, Barton!

J. A.

 > The function paridad expects a function, but paridad(h(x)) provides
 > paridad with the formula for a function. Instead, try paridad(h).
 > This will work as you expect it once, but not twice. The reason for
 > the failure on the second attempt is that the assumption
 > assume(0<z > and z<l) is made globally, not locally to paridad. After 
 > paridad is executed once, h(z) simplifies to %pi-z.
 >
 > The Maxima logical operators are not commutative (I think), so your
 > part statement works. But it's risky to rely on a user expressing the 
 > predicate as (x>=0 and x<%pi) instead of the logically equivalent
 > (x<%pi and x >>=0).