MathGroup Archive 2004

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

Search the Archive

Re: How to get non-printing heads?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg46968] Re: How to get non-printing heads?
  • From: bobhanlon at aol.com (Bob Hanlon)
  • Date: Thu, 18 Mar 2004 01:24:53 -0500 (EST)
  • References: <c388k0$bnb$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Clear[a,b,f, g, y, isConstant];

Constant is an attribute not a head

SetAttributes[a, Constant]

Dt[#,t]& /@ {a, b, y, E, 3}

{0, Dt[b, t], Dt[y, t], 0, 0}

isConstant[x_] := NumericQ[x] || !FreeQ[Attributes[x], Constant];

The test for NumericQ is needed since Attributes does not know how to handle
numbers that are not numeric symbols (such as Pi and E) 

isConstant /@ {a, b, y, E, 3}

{True, False, False, True, True}

b /: Head[b] = newHead;

Head[#]===newHead& /@ {a, b, y, E, 3}

{False, True, False, False, False}

f[x_?(Head[#]===newHead&)] = 0;

f /@ {a, b, y, E, 3}

{f[a], 0, f[y], f[E], f[3]}

g[x_?(Head[#]===newHead || isConstant[#]&)] = 0;

g /@ {a, b, y, E, 3}

{0, 0, g[y], 0, 0}


Bob Hanlon

In article <c388k0$bnb$1 at smc.vnet.net>, kj <socyl at 987jk.com> wrote:

<< How can I give a Mathematica object a different head?

I tried

  In[1]:= Head[a] ^= Constant;

But, as shown below, this doesn't work as desired:

  In[2]:= MatchQ[a, _Constant]

  Out[2]= False

  In[3]:= MatchQ[a, _Symbol]

  Out[3]= True

I even tried

  In[4]:= a/: MatchQ[a, _Constant] = True;

  In[5]:= ??a
  Global`a

  Head[a] ^= Constant
   
  a /: MatchQ[a, _Constant] = True

...but

  In[6]:= MatchQ[a, _Constant]

  Out[6]= False

Is there any way to give "a" a user-defined head?

My immediate goal is to be able to define functions that perform
algebraic manipulations on Mathematica expressions according to
the types of the objects they contain.  I want to leave these
objects pretty much undefined, other than specifying their types.
E.g.:

  myDerivative[x_Constant] := 0;

  myDerivative[a]

  ==> 0

  myDerivative[b]

  ==> myDerivative[b]    (* Derivative doesn't know how to handle Symbol *)
 >><BR><BR>


  • Prev by Date: Re: doing things on a procedural way and doing them on a functional way
  • Next by Date: Re: Physical Constants package
  • Previous by thread: Re: How to get non-printing heads?
  • Next by thread: Re: How to get non-printing heads?