Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2000
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2000

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

Search the Archive

RE: The Csc problem

  • To: mathgroup at smc.vnet.net
  • Subject: [mg24344] RE: The Csc problem
  • From: Wolf Hartmut <hwolf at debis.com>
  • Date: Sun, 9 Jul 2000 04:52:49 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com



> -----Original Message-----
> From:	David Bailey [SMTP:david.bailey at salford-software.com]
To: mathgroup at smc.vnet.net
> Sent:	Friday, July 07, 2000 1:56 PM
> To:	Wolf Hartmut
> Cc:	Andrzej Kozlowski
> Subject:	The Csc problem
> 
> Dear Wolf,
>  
> The attached notebook contains a few comments on the Csc/Sec problem.
>  
> David Bailey
> 
> 
	I copy your notebook here


	Although it is a matter of taste I suppose, I am not too keen on
using Format in the way that you suggest. If you are in the habit of typing
into an output cell (thereby creating a new input cell containing the same
information), or indeed just cutting and pasting, HoldForm gets in the way.

	Unprotect[Csc, Sec];
	Format[Csc[x_]] := HoldForm[1/Sin[x]];
	Format[Sec[x_]] := HoldForm[1/Cos[x]];
	Protect[Csc, Sec]; 

	Here we see a trivial calculation carried forward to the next cell
with predictable results.

	Csc[x]

	  1
	------
	Sin[x]


	  1
	------  /.  x -> 0.2
	Sin[x]


	   1
	--------
	Sin[0.2]


	Furthermore, the output that you get using Format in this way can be
a bit wierd!
	1 + Csc[x]^2

	       1    2
	1 + (------)
	     Sin[x]

	I like using private names for functions that I do not want
Mathematica to 'simplify' in unfortunate ways. It gives you such control.
For example, you can manipulate hypergeometric functions that Mathematica
might simplify to more elemetary functions. It is easy to use a set
transformation rules stored in a variable to get exactly the effects that
you need:

	Kill the kernel here!

	mydisplay = {Csc[x_] -> 1/sin[x], Sec[x_] -> 1/cos[x]}
	{Csc[x_] -> 1/sin[x], Sec[x_] -> 1/cos[x]}

	1 + Csc[x]^2 /. mydisplay
	\!\(1 + 1\/sin[x]\^2\)
>   
>  
[Hartmut Wolf]  

Dear David,

thank you very much for your labour. I fully understand your reasoning.
There seems to be no generally satisfying solution for problems of this
kind. As you said, taste (and the demands of the very application) come in,
you don't like this, I don't like that. The problem is that we don't like
some of the built-in rules, however certainly need others (viz. most of
them) for calculus. 

I know of no way to eliminate part of the built-in rules, esp. for instance
if one wants to replace Csc by 1/Sin *sometimes*.


An idea I had used prior on a similar theme was this: use new names for the
functions, enter an environment (e.g. Block) where you have your special
rules defined, expose the expression to the local environment, leave the
environment and then convert back to form you like. See this example for
treating the spherical Bessel functions

 
  sbjEvaluator[expr_] :=
     Expand[PowerExpand[
	  Block[{sbj},
          sbj[n_, z_] := Sqrt[Pi/(2*z)]*BesselJ[n + 1/2, z];
          expr
        ] /. {BesselJ[n_, z_] :> Sqrt[2*z/Pi]*sbj[n, z]}
	]]
    

  sbjEvaluator[D[sbj[j, r], r]]

  1/2*sbj[-(1/2) + j, r] - sbj[1/2 + j, r]/(2*r) - 1/2*sbj[3/2 + j, r]



We can use this kludge also for the trigonometrical functions

 
In[1]:= 
trigEvaluator[expr_] := 
  Block[{sin = Sin, cos = Cos, sec = Sec, csc = Csc, tan = Tan, cot = Cot},

      expr] /. {Cos -> cos, Sin -> sin, Sec[x_] :> 1/cos[x], 
      Csc[x_] :> 1/sin[x], Tan -> tan, Cot -> cot}

In[2]:= 1 + csc[x]^2 // trigEvaluator
Out[2]=
\!\(1 + 1\/sin[x]\^2\)

In[3]:= D[tan[x], x] // trigEvaluator
Out[3]=
\!\(1\/cos[x]\^2\)


In[4]:= D[cot[x], {x, 2}] // trigEvaluator
Out[4]=
\!\(\(2\ cot[x]\)\/sin[x]\^2\)

In[5]:= sin[x]/cos[x] // trigEvaluator
Out[5]= tan[x]

Perhaps this also could serve your purposes. Of course we have

In[7]:= 1/sin[x] /. x -> 0.2  
Out[7]=
\!\(1\/sin[0.2`]\)

but

In[8]:= 1/sin[x] /. x -> 0.2  // trigEvaluator
Out[8]= 5.03349



However with regard to your special example I can improve the Format method

 
  Unprotect[Csc, Sec, Power];

  Format[Csc[x_]] := HoldForm[1/Sin[x]];
  Format[Power[Csc[x_], n_]] := HoldForm[1/Sin[x]^n];

  Format[Sec[x_]] := HoldForm[1/Cos[x]];
  Format[Power[Sec[x_], n_]] := HoldForm[1/Cos[x]^n];

  Protect[Csc, Sec, Power];



so you'll have what you want

  1 + Csc[x]^2 // OutputForm

         1
  1 + -------
            2
      Sin[x]


(This has no serious repercussions on Power, since Format definition are
effectively UpValues)



Finally I would like to show your my reply to Andrzej Kozlowski's objection

-----Original Message-----
From:	Andrzej Kozlowski [SMTP:andrzej at tuins.ac.jp]
To: mathgroup at smc.vnet.net
Sent:	Thursday, July 06, 2000 11:25 AM
To:	Wolf Hartmut; mathgroup at smc.vnet.net; 'David Bailey';
'zhl67 at my-deja.com'
Subject:	Re: [mg24261] Re: [mg24248] 1/Trig function - help

Dear Hartmut

I noticed this too, after posting my first message. I considered sending a
second one just to point this out but than I noticed that using HoldForm in
this way may be a problem if one wanted to hold something else and then
apply ReleaseHold globally. All your Held expressions would then evaluate.
But of course this is a pretty unlikely situation, so this may well be the
best general solution.

Best regards

Andrzej

[Hartmut Wolf]  

Dear Andrzej,

I don't think this will be a problem at all, because when after ReleaseHold
expressions Sec, Csc show up they will be immediately transformed back again
to HoldForm[1/Sin[..]]. See e.g.
 
In[43]:= a := (Print["a evaluated"]; 1)
In[44]:= D[Tan[x], x] Hold[a Sin[x]] // OutputForm
Out[44]//OutputForm=
                  1    2
Hold[a Sin[x]] (------)
                Cos[x]

In[45]:= % // ReleaseHold // OutputForm
>From In[45]:= "a evaluated"
Out[45]//OutputForm=
  1
------ Tan[x]
Cos[x]

In[46]:= 1/Sin[y] // OutputForm
Out[46]//OutputForm=
  1
------
Sin[y]

In[47]:= ReleaseHold[%] // OutputForm
Out[47]//OutputForm=
  1
------
Sin[y]




Kind regards to everyone,
Hartmut Wolf


  • Prev by Date: Re: Divisors
  • Next by Date: get points from the polyhedron
  • Previous by thread: Re: A strange bug in Solve
  • Next by thread: get points from the polyhedron