Services & Resources / Wolfram Forums
MathGroup Archive
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1999

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

Search the Archive

RE: This is what UpValues are for

  • To: mathgroup at
  • Subject: [mg19414] RE: [mg19357] This is what UpValues are for
  • From: "Ersek, Ted R" <ErsekTR at>
  • Date: Mon, 23 Aug 1999 13:57:22 -0400
  • Sender: owner-wri-mathgroup at

Earlier I wrote that if you evaluate:

Unprotect[Sinh, Cosh];

The above identity is stored in DownValues[Sinh], DownValues[Cosh].  I was
careless when I wrote that.  The identity is actually stored in
UpValues[Sinh], DownValues[Cosh]. I have  corrected the error below.

Ted Ersek

-----Original Message-----
From: Ersek, Ted R 
To: mathgroup at
Subject: [mg19414] [mg19357] This is what UpValues are for

Last week I said I would provide a discussion of UpValues once I resolved
some subtle details.  Well I still haven't resolved the details, but I will
tell you all that I know on the subject.

Suppose you wanted to make it so the kernel would 
automatically use the simplification:
Below I show different ways of doing this.
First we should Unprotect the symbols we will work with.


The line below will use the identity for Cosh[z]+Sinh[z] and associate the
definition with Plus which is apparent when you evaluate (??Plus ).  When
the identity is associated with Plus the kernel will take more time to
perform addition.  Actually in Versions 3 and 4 Plus (Times as well) apply
built-in rules before user defined rules.  As far as I am aware all other
kernel functions apply user defined rules first.  So to put it more
correctly (I hope); when the identity below is used the kernel will check to
see if this identity can be used whenever an expression has the head Plus
after using all built-in rules for Plus.

For example the kernel will not try the identity below on 
(4+2/3),  but it will try it on (4+x).

(* Plus information includes the identity above. *)

The identity above was stored in DownValues[Plus].  We should clear out the
DownValue  from Plus before moving on.  The output of (??Plus ) proves the
identity was removed.

(* The identity was removed from Plus information. *)

Instead we can associate the identity with (Sinh) using the code in the next
line. The "Sinh/:" part of the code says to only associate the definition
with Sinh.  When done this way the use of this identity will have no impact
on how fast the kernel evaluates expressions with the Head Plus or Cosh.
But this will have an impact on how fast expressions with the Head Sinh
evaluate.  This is likely a better approach since (Sinh) is used much less
often than addition.  However, it isn't clear to me how much of an impact it
would have on the evaluation of Sinh[_].  Anyway that is a very practical
application of UpValues. 

The result of evaluating (??Sinh ), (??Cosh ), (??Plus ) will show the
identity is associated with Sinh but not Cosh or Plus.

(* Sinh information includes the identity above. *)

(* Cosh information doesn't include the identity above. *)

(* Plus information doesn't include the identity above. *)

The identity above was stored in UpValues[Sinh].  We should clear out the
UpValue before moving on.  The output of (??Sinh ) proves that it was

(* Sinh information doesn't include the identity above. *)


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
UpValues[Sinh] and UpValues[Cosh].  When this method is used both (Sinh)
and (Cosh) will run slower.

(* Sinh information does include the identity above. *)

(* Cosh information does include the identity above. *)

(* Plus information doesn't include the identity above. *)

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 Plus a DownValue using:
  Sin[z_]^2 + Cos[z_]^2 :=1
Actually the next line also works just as well since (z) isn't used on the
right side. Let me know if you want to know why it matters if (z) is used on
the right side.
  Sin[z_]^2 + Cos[z_]^2 =1

Instead you could use an UpValue for Power using either of the next four
  Sin[z_]^2 + Cos[z_]^2 ^:=1
  Sin[z_]^2 + Cos[z_]^2 ^=1
  Power/:Sin[z_]^2 + Cos[z_]^2 :=1
  Power/:Sin[z_]^2 + Cos[z_]^2 =1

Let me know if you want me to elaborate on the differences between 

BUT Mathematica will not let you associate the definition above with (Sin)
or (Cos).  You might be tempted to use one of the following, but they will
not work because (Sin) and (Cos) are too deep in the expression.
  Sin/:( Sin[z_]^2 +Cos[z_]^2 )=1
  Cos/:( Sin[z_]^2 +Cos[z_]^2 )=1
  Sin/:( Sin[z_]^2 +Cos[z_]^2 ):=1
  Cos/:( Sin[z_]^2 +Cos[z_]^2 ):=1

When you evaluate something with the form
(f) must be a symbol, and  lhs[[n,0]] must return (f) for some integer (n).
The same thing goes for  f/:lhs^:=rhs

In the case where ( lhs = Sin[z_]^2+Cos[z_]^2 )

You see both Sin and Cos are in so deep that the identity can't be
associated with either Sin or Cos.

When you use 
An UpValue is given to every symbol that can be accessed using 
lhs[[n,0]] for any integer (n).

For example when you use  lhs^:=rhs
with ( lhs=Cosh[z_]+Sinh[z_] )
so an UpValue is given to both Cosh, Sinh.

Before continuing you might want to evaluate the next line to put everything
back to normal.


Ted Ersek

For Mathematica tips, tricks see

  • Prev by Date: Please help me! (Warnings and Messages...)
  • Next by Date: Re: strange problem with tickmarks
  • Previous by thread: Re: This is what UpValues are for
  • Next by thread: Nodal demo and Electronic Engineering with Mathematica