RE: function definition
- To: mathgroup at christensen.cybernetics.net
- Subject: RE: function definition
- From: olness at phyvms.physics.smu.edu (Fredrick Olness (214) 768-2500 or -2495, Fax -4095)
- Date: Sun, 23 Oct 1994 19:02:18 -0500
========================================================
IN RESPONSE TO:
========================================================
From: SMTP%"sample at shire.ac.arknet.edu" 23-OCT-1994 10:22:21.67
To: mathgroup at christensen.cybernetics.net
Subj: RE: function definition
Can anyone explain the principle for function definition that yields the
following:
First define f2[x]:
in: f2[x_] := x + 2;
in: ?f2
out: Global`f2
f2[x_] := x + 2
Now re-define f2[x], and we see that the old definition has been overwritten:
in: f2[x_] := x + 3;
in: ?f2
out: Global`f2
f2[x_] := x + 3
But now define f2[y], and see that the old definition
is the one that is used by Mma!
in: f2[y_] := y + 4;
out: ?f2
Global`f2
f2[x_] := x + 3
f2[y_] := y + 4
Check out values of f2. They're all from the x + 3 definition:
in: f2[x]
out: 3 + x
in: f2[y]
out: 3 + y
in: f2[1]
out: 4
_____________________________/________________________________________________
David Kramer Telephone: (717) 291-4339
Coordinator of Academic Computing e-mail: D_Kramer at Acad.FandM.edu
Franklin & Marshall College FAX: (717) 399-4446
Lancaster, Pennsylvania 17604-3003 USA
_______________________________________________________________________\______
+++++++++++++++++++++++++++++++++++++++++++++++++++
I call that a bug. Clearly Mma uses the first definition it finds it its list,
but I think the second definition should over write the first.
The only thing I could think was that if before f2[x_] was defined, x was
declared an integer (say), and before f2[y_] was defined, y was declared a
vector, then f2 could be used to do different things depending on the data type
it is sent. BUT THIS DOESN'T WORK. So I think its a bug.
Chip
===========================================================================
===========================================================================
From: OLNESS "Fredrick Olness (214) 768-2500 or -2495, Fax -4095" 21-OCT-1994 02:22:48.92
To: SMTP%"D_Kramer at Acad.FandM.edu"
CC: OLNESS
Subj: function definition
David Kramer,
Regarding your question about function definition:
When you define:
f2[x_] := x + 3
f2[y_] := y + 4
Mathematica is unable to tell that the 2nd rule supersedes the 1st
because you have used different letters, x & y.
I agree it probably should, but ...
Not knowing what to do, it keeps both rules in the order you
gave them, and when it evaluates f2[1], it uses the first rule
in the list that matches the pattern; namely, f2[x_] := x + 3
You may want to refer to section 2.4 in the Mathematica manual.
and also experiment with UpValues and DownValues.
Let me know if this helps.
Fredrick I. Olness
SMU Mail: Department of Physics
Fondren Science Bldg.
Southern Methodist University
Dallas, TX 75275
Phone: (214) 768-2500 (SMU Office)
(214) 768-2495 (SMU Secretary)
(214) 768-4095 (SMU FAX)
Internet: Olness at phyvms.physics.smu.edu (129.119.200.74)
Olness at mail.physics.smu.edu