Re: Formating output of Modules
- Subject: [mg2248] Re: [mg2214] Formating output of Modules
- From: ptk at imada.ou.dk (Peder Thusgaard Ruhoff)
- Date: Wed, 18 Oct 1995 05:55:42 GMT
- Approved: usenet@wri.com
- Distribution: local
- Newsgroups: wri.mathgroup
- Organization: Wolfram Research, Inc.
- Sender: daemon at wri.com ( )
On Mon, 16 Oct 1995, Scott Herod <sherod at boussinesq.Colorado.EDU> wrote:
>
> I've got an annoying little problem that I was hoping someone can
> help me solve. I have a Module, NDelaySolve, which returns a
> numerical approximation of solutions to differential-delay equations.
> In order to make the output look as much like that of NDSolve as
> possible, I have the module return something like
>
> {{x[t] -> g[t], y[t] -> f[t]}}
>
> where g[t] and f[t] are often big Which[] functions. I would like
> to reduce the output by creating a DelaySolveFunction[][t] which
> looks something like an Interpolating function.
>
> Here is an example:
>
> In[4]:=
> Format[spam[x_]] := 1/x;
> In[8]:=
> spam[a_] := Module[{},
> pop = Table[a,{3}]
> ];
> In[9]:=
> spam[8]
> Out[9]=
> {8, 8, 8}
>
>
> This Format isn't doing what I want. I would like spam[8] to return {8,8,8}
> but to print the output as 1/8. If I get this one to work, I could solve
> my real problem.
>
> Scott Herod
> sherod at newton.colorado.edu
>
>
Dear Scott,
The basic problem is that your function spam returns an object with head
List. Thus, you should define a formatting rule for the built-in object
List and not spam. However, in general, it is not a good idea to change
the behavior of built-in functions. Instead, consider the following
version of your program
In[1]:= spam[a_] :=
Module[{},
spamresult[Table[a,{3}]]
]
Now, the output from spam has head spamresult, e.g.
In[2]:= spam[8]
Out[2]= spamresult[{8,8,8}]
You can now define a formatting rule for objects with head spamresult
In[3]:= Format[spamresult[{x_, y___}]] := HoldForm[1/x]
In[4]:= spam[8]
1
Out[4]= -
8
In[5]:= FullForm[%]
Out[5]//FullForm= spamresult[List[8, 8, 8]]
If you need the list, just use First on the result, e.g.
In[6]:= First[%4]
Out[6]= {8, 8, 8}
I hope this will help you.
Good luck,
Peder
------------------------------------------------------------------------------
Peder Thusgaard Ruhoff Phone: (+45) 66 15 86 96, ext. 2411
Dept. of Mathematics and Computer Science Fax: (+45) 65 93 26 91
Odense University, Campusvej 55 Email: ptk at imada.ou.dk
DK-5230 Odense M, DENMARK
"It is important for him who wants to discover not to confine himself
to one chapter of science, but to keep in touch with various others."
- Jacques Hadamard
------------------------------------------------------------------------------