Re: Summary: Which[] as Textbook Input, Plot[] Questions
- To: mathgroup at smc.vnet.net
- Subject: [mg54048] Re: Summary: Which[] as Textbook Input, Plot[] Questions
- From: "Carl K. Woll" <carlw at u.washington.edu>
- Date: Wed, 9 Feb 2005 09:27:20 -0500 (EST)
- Organization: University of Washington
- References: <cua5gn$hhl$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Matt,
Here is one idea.
We want an extensible prefix operator which has no built in Mathematica
definition. The best choice would be a brace, {, but that character is
already reserved, and is a matchfix operator requiring both a left brace
and a right brace for input. It's possible to cook up something that works,
but it's rather complicated and ugly. So, I looked at other extensible
characters. The one I ended up with is \[VerticalLine]. Now, \[VerticalLine]
is a letter-like form, so we need to turn it into a prefix operator. The way
to do this is to use \[InvisibleApplication], which has the same
functionality as @, only it's invisible. Finally, we need to create the
GridBox structure (matrix) which has the values and the domains. It's a bit
difficult to type this in, so it would be best to create a palette or an
alias to enter the structure. I will describe how to use an alias, but I
will leave the details of a palette implementation to you or anybody else
that is interested.
To create an alias, you open the Option Inspector, either from the menu
system or with the keyboard shortcut (Windows) Ctrl+Shift+O. Select notebook
for Show option values at the top. Type in alias in the Lookup box and go to
InputAliases. Add the following alias:
{ other aliases,
"which"->RowBox[ {"\[VerticalLine]", "\[InvisibleApplication]",
GridBox[ {{"\[Placeholder]", "\[Placeholder]"}, {"\[Placeholder]",
"\[Placeholder]"}}] }]
}
You can either type in the above alias directly into the option value, or
you can click on the box to the right of the option, click Add, and then put
in which (no quotes) on the left side (x) of the dialog box and the right
hand side above on the right side (y) of the dialog box. Cutting and pasting
is okay in the dialog box, but seems to run into a character limit when you
put the rule directly into the option. Now, when you type esc which esc you
will get a vertical line followed by a 2x2 matrix of placeholders. Put in
the value in the the first placeholder of each row, and the domain in the
second placeholder. You can add more rows by using Ctrl+Enter. You can add
options to the GridBox if you want to control the alignment (for example
ColumnAlignments->Left).
That takes care of the basics of the input process. Next, we want to have
the above structure interpreted as a Which statement. The following
definition will do this.
\[VerticalLine][a_]:=Which@@Flatten@a[[All,{2,1}]]
That's it. It would actually be better to add a definition to MakeExpression
to handle this process, but I'll let somebody else worry about that.
To see the above in action, we can take your example. Type the following:
f[x_] = esc which esc
Then add a row and fill in your information. Hit enter and you will find
that f[x] has the Which definition you desire.
Good luck, and feel free to ask questions if I've been unclear.
Carl Woll
"Matt" <anonmous69 at netscape.net> wrote in message
news:cua5gn$hhl$1 at smc.vnet.net...
> Hello,
> I apologize if the answer to this is somewhere glaringly obvious in
> the documentation, however, after at least 4 hours pawing through both
> the hardcover Mathematica 4.0 book by Wolfram and the in-program
> Mathematica 4.1 documentation, I cannot find how I would annotate a
> function that takes on different values based upon different domains.
> To wit, something like:
>
> Clear[f];
> f[x_] := Which[x < 0, Sin[x]/x, x == 0, 1, x > 0, Sin[x]/x];
> Plot[f[x], {x, -pi, pi}, AxesLabel -> {"x", "f[x]"};
>
> The 'Which' function is great for actually evaluating something, but I
> was looking for something along the lines of traditional mathematical
> notation (such as one would write on a chalkboard or on a sheet of
> paper), where a large left-bracket would be used and the various
> definitions of the function for the various ranges would be
> 'constrained' by the bracket.
>
> I'll try to illustrate what I mean, where the '|'s that I will use
> should be interpreted as a single, large left-bracket:
>
> | Sin(x)/x, x < 0
> f(x) = | 1, x = 0
> | Sin(x)/x, x > 0
>
> Is there a way to do what I'm asking in Mathematica 4.1 (or even
> above)?
>
>
> As regards the Plot[] function, I'm puzzled as to why the following
> doesn't give me an error when evaluated:
>
> Clear[g];
> g[x_] := 1/x;
> Plot[g[x], {x, -5, 5}];
>
> It seems as though it should, considering that x at zero is undefined.
> However, Mathematica draws the graph as though the function were just
> fine.
>
> Thanks for any light you can shed on this,
>
> Matt
>