Re: declaring variables
- To: mathgroup at smc.vnet.net
- Subject: [mg4800] Re: declaring variables
- From: danl (Daniel Lichtblau)
- Date: Mon, 16 Sep 1996 23:51:24 -0400
- Organization: Wolfram Research, Inc.
- Sender: owner-wri-mathgroup at wolfram.com
> Burkhardt Goedde wrote: > > > > Dear math users, > > just a simple problem, but I got no answer from thew book. > > > > Re[x + I y] gives > > > > Re[x + I y] > > > > whereas > > > > Re[2 + 3 I] > > > > gives 2 > > > > How could I tell Mathematica that x and y are real numbers? > > I think this is in some FAQ file. Where it lives I do not know. Anyway, I've seen a response posted that uses ComplexExpand In[1]:= ComplexExpand[Re[x + I y]] Out[1]= x and also a method that uses the standard package Algebra`ReIm`. I think the former method is to be preferred. let me explain why. First, we really do not maintain Algebra`ReIm` any more. If it works, fine. If not, we may look into bug reports but we will not look very hard. Second, it tends to be much slower on problems of any difficulty. In[4]:= Timing[ComplexExpand[Re[(x-I*y)^3/(Exp[x]*(x^3-x*y+y^2)^2)]]] // InputForm Out[4]//InputForm= {0.15626*Second, x^3/(E^x*(x^3 - x*y + y^2)^2) - (3*x*y^2)/(E^x*(x^3 - x*y + y^2)^2)} vs. (different session) In[2]:= Needs["Algebra`ReIm`"] In[3]:= Im[x]^=0; In[4]:= Im[y]^=0; In[5]:= Re[x + I y] Out[5]= x In[10]:= Timing[Re[(x-I*y)^3/(Exp[x]*(x^3-x*y+y^2)^2)]] // InputForm Out[10]//InputForm= {12.41520599999999*Second, (-2*x*y^2 + x*(x^2 - y^2))/ (E^x*(x^3 - x*y + y^2)^2)} Third, Algebra`ReIm` is not as powerful as ComplexExpand. For example, In[6]:= ComplexExpand[Log[Exp[x]]] Out[6]= x In[7]:= ComplexExpand[Sign[x^2+1]] Out[7]= 1 vs (in session with Algebra`ReIm` loaded and Im[x]^=0) In[11]:= Log[Exp[x]] x Out[11]= Log[E ] In[13]:= Sign[x^2+1] 2 Out[13]= Sign[1 + x ] Fourth, I've seen at least one bug report to the effect that Algebra`ReIm` does not get along with ComplexExpand. The gist was that preloading Algebra`ReIm` caused a seemingly benign ComplexExpand invocation to go into infinite recursion. Finally, my opinion (not necessarily shared by others) is that global assumptions are dangerous. Once we make declarations about variables e.g. x^=0, these may be used elsewhere, or not, depending on what code is invoked. Worse still, we may forget we made the assumptions and have later code perform "simplifications" we do not want. ComplexExpand localizes assumptions, regarding which variables are real, to its arguments. Once it returns the assumptions are no longer valid. Daniel Lichtblau Wolfram Research, Inc. danl at wolfram.com ==== [MESSAGE SEPARATOR] ====