Apply and up/down value questions
- To: mathgroup at smc.vnet.net
- Subject: [mg61533] Apply and up/down value questions
- From: "Matt" <anonmous69 at netscape.net>
- Date: Sat, 22 Oct 2005 00:35:18 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
Hello,
Again, I'm working my way through some of the programming examples
in Michael Trott's Programming Guidebook, and there's behaviour that I
cannot figure out. I've been over and over section A.5.2 and 2.5.10 in
the Mathematica Book, but I still don't understand up values when
applied to functions with more than one argument. Also, I had a
question about level specification, which I couldn't figure out even
after looking at section A.3.6. Any help is appreciated. First the
upvalue questions:
I don't understand the point of using TagSet as opposed to UpSet:
e.g. in Michael Trott's Programming Guidebook on page 318 he states:
"For functions with several arguments, the information can be
associated with a certain prescribed argument rather than with all
arguments at the first level." However, I don't see any difference in
functionality between this:
x /: Plus[x, y_] = y;
Plus[x, 7]
??x
??y
and this (after clearing x and y)
Plus[x, y_] ^:= y
Plus[x, 7]
??x
??y
Also, I'm stumped on this particular wording in the TagSet
documentation:
"If f appears several times in lhs, then f/: lhs = rhs associates the
assignment with each occurrence"
What does this really mean?
As regards Apply:
Is it true that Apply[newHead, expr, 1] and Apply[newHead, expr, {1}]
will always be the same result given that the other two arguments are
identical?
By definition, a level specification of n applies to levels 1 through
n. I have found that if I do something like Apply[newHead, expr, -2],
that what I actually end up with seems to be equivalent to newExpr =
Apply[newHead, expr, {1}] followed by Apply[newHead, newExpr, {-2}].
Here is an example:
Cell[BoxData[{
RowBox[{
RowBox[{"a1", " ", "=", " ",
RowBox[{"Array", "[",
RowBox[{"\[DoubleStruckR]", ",",
RowBox[{"{",
RowBox[{"2", ",", "2"}], "}"}], ",",
RowBox[{"{",
RowBox[{"2", ",", "4"}], "}"}]}], "]"}]}],
";"}], "\[IndentingNewLine]",
RowBox[{"MatrixForm", "[", "a1", "]"}], "\[IndentingNewLine]",
RowBox[{
RowBox[{"a11", " ", "=", " ",
RowBox[{"Apply", "[",
RowBox[{"newHead", ",", "a1", ",",
RowBox[{"{", "1", "}"}]}], "]"}]}],
";"}], "\[IndentingNewLine]",
RowBox[{"MatrixForm", "[", "a11", "]"}], "\[IndentingNewLine]",
RowBox[{
RowBox[{"a12", " ", "=", " ",
RowBox[{"Apply", "[",
RowBox[{"newHead", ",", "a11", ",",
RowBox[{"{",
RowBox[{"-", "2"}], "}"}]}], "]"}]}],
";"}], "\[IndentingNewLine]",
RowBox[{"MatrixForm", "[", "a12", "]"}], "\[IndentingNewLine]",
RowBox[{
RowBox[{"a13", " ", "=", " ",
RowBox[{"Apply", "[",
RowBox[{"newHead", ",", " ", "a1", ",", " ",
RowBox[{"-", "2"}]}], "]"}]}],
";"}], "\[IndentingNewLine]",
RowBox[{"MatrixForm", "[", "a13", "]"}]}], "Input"]
What is the general rule, or am I just misusing the level specification
in Apply?
And finally, a question on modified built in functions:
In Michael Trott's Programming Guidebook, on page 311, he uses the
following construct to remove a downvalue that had been specified for
the Cos and Sin functions:
Unprotect[Cos];
Clear[Cos];
Protect[Cos];
{Cos}
I've noticed that if I omit the last line (i.e. {Cos}), that it still
works. What is the point of the last line?
Thanks very much,
Matt