Re: Re: How to declare Integers?

*To*: mathgroup at smc.vnet.net*Subject*: [mg13433] Re: [mg13167] Re: How to declare Integers?*From*: "Fred Simons" <wsgbfs at win.tue.nl>*Date*: Thu, 23 Jul 1998 03:33:34 -0400*Sender*: owner-wri-mathgroup at wolfram.com

With some interest I read the messages on type declaration in Mathematica. I might have missed some, so if someone else already posted a similar solution I apologize. I think that Sean Ross in his last message hits the root of the desire for type declarations: > > Perhaps the term variable "typing" is ill-advised since it sounds > the same as what is done in C++ or Fortran. No, what we are after > is something like this: > > > Declare[symbol,Integer]; > > Sin[symbol Pi x] > > and have it return zero for all x even with no explicit value > assigned to symbol. > > I want to be able to tell mathematica that a certain symbol is Real, > Complex, Imaginary, greater than 2, Integer etc. and have every > single function in the language react appropriately taking that > declaration as an assumption. (...) I have no idea how realistic this last statement is. Can anyone give a complete list of results he wants to have when a variable has to be declared of integer type? Usually it is only a much smaller problem, like Sean Ross's problem with the trigonometric functions, that we are interested in. I have often met that problem when computing Fourier coefficients with Mathematica in a traditional pen and paper course on Fourier series. It can be solved in a straightforward way. Of course I will declare a variable n to be an integer by the statement IntegerQ[n] ^= True. Unfortunately, IntegerQ does not know (why not?) that the sum, the product and positive powers of integers are integers so I extend IntegerQ in the following way: Unprotect[IntegerQ]; IntegerQ[ a_?IntegerQ +b_?IntegerQ] = True; IntegerQ[ a_?IntegerQ * b_?IntegerQ] = True; IntegerQ[a_?IntegerQ ^ b_Integer?Positive] = True; Protect[IntegerQ]; Then the following definitions are entered: Unprotect[ Sin, Cos, Exp]; Sin[ n_?IntegerQ Pi] = 0; Cos[ n_?IntegerQ Pi] := (-1)^n; Exp[ n_?IntegerQ Pi Complex[a_, b_]] := Exp[n a Pi] (-1)^(b n); Protect[ Sin, Cos, Exp ]; Having done this, we adapt the function (-1)^n. The first rule states that for integer a, n and natural p we have (-1)^( a n^p) = (-1)^ (a n); the second rule that for integer a and n we have (-1)^(a n) = 1 when a is even and (-1)^n when a is odd. The last rule reduces for example (-1)^(n+3) to -(-1)^n when n is an integer. Unprotect[Power]; (-1)^((a_Integer:1) b_?IntegerQ^c_Integer?Positive +d_.) := (-1)^(a b + d); (-1)^(a_Integer n_?IntegerQ + b_.) := (-1)^(Mod[a,2]n + b); (-1)^(a_Integer + b_) := (-1)^a (-1)^b; Protect[Power]; For me, this works very satisfactory. You could try the following examples: IntegerQ[n] ^= True (-1)^(4 n^3 + 3n+7 ) Cos[ (m + 3n + 7)Pi ] // ExpandAll // TrigExpand Integrate[x Exp[ I n x ], {x, 0, Pi}] Fred Simons Eindhoven University of Technology