MathGroup Archive 1999

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: This is what UpValues are for

Hallo Ted,

we all thank you very much for your elucidations on UpValues, I'd just
like to add two little comments:

this is from your posting:
> We could also use the next line to associate the identity with both (Sinh)
> and (Cosh), but not (Plus).  This is clear from the result of evaluating
> (??Sinh ), (??Cosh ), (??Plus ).  In this case the identity is stored in
> DownValues[Sinh] and DownValues[Cosh].  When this method is used both (Sinh)
> and (Cosh) will run slower.
> In[11]:=
> Cosh[z_]+Sinh[z_]^:=Exp[z];
> ??Sinh
> (* Sinh information does include the identity above. *)
> In[12]:=
> ??Cosh
> (* Cosh information does include the identity above. *)
> In[13]:=
> ??Plus
> (* Plus information doesn't include the identity above. *)

You said "...both (Sinh) and (Cosh) will run slower"

This could be misconstrued. As far as I understand Mathematica -- I
confess I did not prove this by testing, so I'm feeling a little bit
uncomfortable -- the execution of Sinh and Cosh, whenever you call them,
will _not_ be less efficient than before. However, when evaluating any
expression that contains Sinh or Cosh at level 1, when within the
evaluation sequence for _that_ expression it comes at looking for
UpValues >>some will be found for Sinh or Cosh and then have to be
checked whether they apply<< That's the >>extra effort<< , but that's
the minimal prize you have to pay as long as you want to install that
relation as a general rule -- and Mathematica will pay you back.

> --------------------
> Now suppose you wanted the kernel to automatically use the identity
>   Sin[z]^2 + Cos[z]^2 -->1
> Notice both (Sin[z]^2) and (Cos[z]^2) have the Head Power.
> You could give ...
> ...

> BUT Mathematica will not let you associate the definition above with (Sin)
> or (Cos) ...
> ...

I think we should mention Roman E. Maeders "Programming in Mathematica",
Chapter 6 "Building Rule Sets" where he told how to effectively deal
with those cases. So if you do:

In[1]:= << "ProgrammingInMathematica\\TrigSimplification.m"

In[2]:= Sin[x]^2 + Cos[x]^2 // TrigLinear
Out[2]= 1

You get what you want, without any performance penalties elsewhere.

With kind regards, 
your hartmutw

> For Mathematica tips, tricks see

  • Prev by Date: Re: Magnitude of Vector
  • Next by Date: Re: Standard Evaluation with UpValues
  • Previous by thread: This is what UpValues are for
  • Next by thread: RE: This is what UpValues are for