Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How do I tell Mathematica than n is an integer?

567 views
Skip to first unread message

michael a. morrison

unread,
Nov 13, 1997, 3:00:00 AM11/13/97
to

In writing some packages for courses I'm teaching, I've run into the
following recurrent difficulty:

How do you tell Mathematica within a notebook session that a variable n
is a positive integer in such a way that a command defined in a package
such as
waveFunction[n_Integer?Positive] := will execute rather than
returning waveFunction[n] unevaluated. Setting
Integer[n] ^= True;
Postive[n] ^= True;
in the notebook has no effect whatsoever on the failure of Mathematica
to evaluate waveFunction[n].

I would be very grateful for any advice anyone can give me!
Thanks---michael morrison (university of oklahoma)

Michael A. Morrison
Dept. Physics & Astronomy
University of Oklahoma
Norman, OK 73019
Phone: 405 325-3961
FAX: 405 325-7557

j...@max.mpae.gwdg.de

unread,
Nov 13, 1997, 3:00:00 AM11/13/97
to

Hi Michael

> How do you tell Mathematica within a notebook session that a variable n
> is a positive integer in such a way that a command defined in a package
> such as
> waveFunction[n_Integer?Positive] := will execute rather than

This will not work because n_Integer ask Head[n]===Integer. For Your
symbolic n this will never be true. The head of "n" remains a symbol
regardles what You say with Your definitions

> returning waveFunction[n] unevaluated. Setting
> Integer[n] ^= True;
> Postive[n] ^= True;
> in the notebook has no effect whatsoever on the failure of Mathematica
> to evaluate waveFunction[n].

The correct way to define the symbol "n" is a positive integer is

n /: IntegerQ[n]:=True
n /: Positive[n]:=True


Your waveFunction[] must ask IntegerQ[n] and not Head[n]==Integer so the
definition is

waveFunction[n_ /; IntegerQ[n] && Positive[n]]:=Psi[n,x]

Hope that helps
Jens


Paul Abbott

unread,
Nov 14, 1997, 3:00:00 AM11/14/97
to

michael a. morrison wrote:

> How do you tell Mathematica within a notebook session that a variable n
> is a positive integer in such a way that a command defined in a package
> such as
> waveFunction[n_Integer?Positive] := will execute rather than

> returning waveFunction[n] unevaluated. Setting
> Integer[n] ^= True;
> Postive[n] ^= True;
> in the notebook has no effect whatsoever on the failure of Mathematica
> to evaluate waveFunction[n].
>

The assignment

In[1]:= waveFunction[n_Integer?Positive]:=what

checks the Head of n

In[2]:= Head[n]
Out[2]= Symbol

which has to be an Integer, and then applies the Positive test. What you
want to do, I think, is

In[3]:= IntegerQ[n] ^= True;

In[4]:= Positive[n] ^= True;

In[5]:= ?n

"Global`n"
IntegerQ[n] ^= True
Positive[n] ^= True

then define a test for symbols with assigned properties:

In[6]:= test[n_] := IntegerQ[n] && Positive[n]

In[7]:= test[n]
Out[7]= True

Your function then reads

In[8]:= waveFunction[n_?test] := blah

In[9]:= waveFunction[n]
Out[9]= blah

In[10]:= waveFunction[m]
Out[10]= waveFunction[m]

Cheers,
Paul

____________________________________________________________________
Paul Abbott Phone: +61-8-9380-2734
Department of Physics Fax: +61-8-9380-1014
The University of Western Australia Nedlands WA 6907
mailto:pa...@physics.uwa.edu.au AUSTRALIA
http://www.pd.uwa.edu.au/~paul

God IS a weakly left-handed dice player
____________________________________________________________________


sean...@worldnet.att.net

unread,
Nov 15, 1997, 3:00:00 AM11/15/97
to

michael a. morrison wrote:
>
> In writing some packages for courses I'm teaching, I've run into the
> following recurrent difficulty:
>
> How do you tell Mathematica within a notebook session that a variable n
> is a positive integer in such a way that a command defined in a package
> such as
> waveFunction[n_Integer?Positive] := will execute rather than
> returning waveFunction[n] unevaluated. Setting
> Integer[n] ^= True;
> Postive[n] ^= True;
> in the notebook has no effect whatsoever on the failure of Mathematica
> to evaluate waveFunction[n].
>
> I would be very grateful for any advice anyone can give me!
> Thanks---michael morrison (university of oklahoma)
>
> Michael A. Morrison
> Dept. Physics & Astronomy
> University of Oklahoma
> Norman, OK 73019
> Phone: 405 325-3961
> FAX: 405 325-7557

If I understand correctly, you want to execute the function symbolically
without specifying an actual value for n, but just tell Mathematica
that n is a positive integer. The problem is that Mathematica has no
declaration or typing beyond the Head. There is no Head for positive
integer. As far as I know, there is no direct way to do what you want.
Several of us have lamented the absense of true object oriented
variable typing, but the Wolfram gurus have told us, and I believe
them, that it is a devilishly difficult thing to do in general.

If you get rid of the test on your symbol n and execute the function,
doesn't that return the answer you want? Another thing you could do is
to force the value of n to be a positive integer inside your code by
use of Round and Abs.
--
Remove the _nospam_ in the return address to respond.


Leszek Sczaniecki

unread,
Nov 15, 1997, 3:00:00 AM11/15/97
to

foo[n_] := OK /; IntegerQ[n] && Positive[n] IntegerQ[n] ^= True;
Positive[n] ^= True;

-leszek

michael a. morrison wrote:

> In writing some packages for courses I'm teaching, I've run into the
> following recurrent difficulty:
>
> How do you tell Mathematica within a notebook session that a variable n
> is a positive integer in such a way that a command defined in a package
> such as
> waveFunction[n_Integer?Positive] := will execute rather than
> returning waveFunction[n] unevaluated. Setting
> Integer[n] ^= True;
> Postive[n] ^= True;

A !

0 new messages