Re: Special Input Forms
- To: mathgroup at yoda.physics.unc.edu
- Subject: Re: Special Input Forms
- From: withoff (David Withoff)
- Date: Fri, 14 Jan 1994 13:15:18 -0600
> Does anyone know how to make shorthand input forms in Mathematica?
> Specifically, I would like an expression like the
> 2 +/- 3
> to evaluate to
> You're talking about adding new shorthands to the MMA parser.
> To the best of my knowledge, this is not possible at the input
> prompt level. The best you might do is to play games with
> object rules, for instance, by defining +/-3 as the 'object'
> UncertainInteger, and then making a rule so that
> 2 + UncertainInteger
> reduces to
> (similarly, 2 - UncertainInteger would give the same answer).
> If you *really* want to be able to use the syntax 2 +/- 3, you're
> going to have to write your own parser (either external to MMA,
> or through an MMA function like MyParser[s_String]) that would
> catch the '+/-' cases and convert them to Uncertainty function
> calls. Given the complexity of the precedence rules for MMA's
> language syntax, I think this will be a waste of effort.
> \\|// "An intellectual carrot...
> - - The mind boggles!!"
> o o -- The Thing (1951)
> J roberto sierra
> O tempered microdesigns NOTICE:
> \_/ san francisco, ca The ideas and opinions expressed
> bert at netcom.com herein are not those of the author.
It is possible to handle this particular example using $PreRead,
and although the given specification of the problem is incomplete,
it may be possible to do the entire problem this way.
In:= $PreRead = (StringReplace[#, "+/-" -> "~Uncertainty~"] &) ;
In:= 2 +/- 3
Out= Uncertainty[2, 3]
As can be inferred from the appendix of the Mathematica Book (under Special
Input Forms, appendix A.2.3, page 716), this will effectively introduce
an infix operator +/- which is left-associative
In:= x +/- y +/- z
Out= Uncertainty[Uncertainty[x, y], z]
and which has a precedence between that of ++ and +
In:= a ++ +/- b + c
Out= c + Uncertainty[a++, b]
There are lots of other things to try between this and writing your
own parser, and even the latter it isn't an insurmountable task. The
aforementioned appendix gives nearly all of the Mathematica-specific
information (tokens, precedence, and associativity) needed to build
Parsing and lexical analysis is being restructured a bit for
Version 2.3 in a way that will make this sort of thing even easier.
Research and Development
Prev by Date:
Symbolic Linear Algebra Package
Next by Date:
Previous by thread:
Re: Special Input Forms
Next by thread:
Adaptive Simulated Annealing (ASA)