Re: question on changing 'type' of numbers
- To: mathgroup at smc.vnet.net
- Subject: [mg69485] Re: question on changing 'type' of numbers
- From: dimmechan at yahoo.com
- Date: Thu, 14 Sep 2006 06:54:57 -0400 (EDT)
- References: <ee8h72$j38$1@smc.vnet.net>
***Maybe I am wrong but I think this has to do with atomic expressions. ***You can apply a function to an atomic expression without getting an error message; however, in that case you simply get the atom back. E.g. {h@@2,h@@f,Integer@@z} {2, f, z} ***You can check if an expression is atomic with AtomQ Information[AtomQ] AtomQ[expr] yields True if expr is an expression which cannot be divided into subexpressions, and yields False otherwise. InputForm[Attributes[AtomQ] = {Protected}] AtomQ/@{3,2.1+0.2\[ImaginaryI],"{1,2}",Ï?,{1,2},Sqrt[3]} {True, True, True, True, False,False} ***However notice the following Trace[Apply[Real,x],TraceOriginal->True] {HoldForm[Real @@ x], {HoldForm[Apply]}, {HoldForm[Real]}, {HoldForm[x]}, HoldForm[Real @@ x], HoldForm[x]} ***There is the command HoldPattern (or Literal for older versions of Mma) Information[HoldPattern] HoldPattern[expr] is equivalent to expr for pattern matching, but maintains expr in an unevaluated form. InputForm[Attributes[HoldPattern] = {HoldAll, Protected}] ***Then Trace[Apply[Real,HoldPattern[x]],TraceOriginal->True] {HoldForm[Real @@ HoldPattern[x]], {HoldForm[Apply]}, {HoldForm[Real]}, {HoldForm[HoldPattern[x]], {HoldForm[HoldPattern]}, HoldForm[HoldPattern[x]]}, HoldForm[Real @@ HoldPattern[x]], HoldForm[Real[x]], {HoldForm[Real]}, {HoldForm[x]}, HoldForm[Real[x]]} ***So I guess you can get sth relevant to what you want working as follows Clear["Global`*"] Head[x] Symbol x/:Head[x]=Integer (*Tag Head is protected so you cannot use Head[x]=Integer*) Integer Head[x] Integer y=Apply[Real,HoldPattern[x]] Real[x] Head[y] Real ***Cheers Dimitris Î?/Î? Nasser Abbasi ÎγÏ?αÏ?ε: > I think of a the Head of expression as its 'type'. > > One is supposed to be able to use Apply[f,expr] to change head of expression > to f. > > But Why can't I use this to change the head of a Real number to Integer? > > In[122]:= > Remove["Global`*"] > x = 0.1 > Head[x] > -------------->Real > Apply[Integer,x] > Head[x] > ------------------->Real. I was expecting Intger > x = Integer[x] > Head[x] > ----------------> Now it says Integer > > any idea? > > ofcourse the question as to what should happen to the value of the variable > if for example I change the head of 0.5 from Real to Integer is another > matter. It seems in this case x remained 0.1 but only the head said Integer > > x > ----->Integer[0.1] > > Now sure what the above really means :) > > Nasser