55
$\begingroup$

I'm trying to expedite some quantum mechanical calculations (expectation values etc.) by running them through Mathematica. When I say, for example,

u[x_] := Sqrt[2/L] * Sin[Pi * n * x / L]

and then take the complex conjugate, I get

Sqrt[2] Conjugate[Sqrt[1/L]] Sin[(π Conjugate[n x])/Conjugate[L]]

But I want to tell Mathematica that some of the parameters are real (i.e. $L$) and some are integer valued and real (i.e. $n$). Is there a way to do that? I've tried adapting some syntax that I've seen in other context (but do not strictly know what it means or does) but it hasn't worked. For example,

Conjugate[u[x], Im[n] = 0]
Sin[n*Pi] /. n = Integer

Don't work the way I want them to. Chugging through this, however, when it comes time to compute values (like, in this example, $\langle p^2\rangle$, I get the following:

Integrate[u[x]*(-h^2)*u''[x], {x, 0, L}]
(* => (h^2 n π (n π - 1/2 Sin[2 n π]))/L^2 *)

where the second term there is clearly zero for all integer values of $n$ (but Mathematica doesn't know that).

$\endgroup$
2
  • 3
    $\begingroup$ But I want to tell Mathematica that some of the parameters are real (ie L) and some are integer valued you can us e ComplexExpand it says expands expr assuming that all variables are real, for integers, you can use Assuming[Element[x,Integers],Simplify[....]] $\endgroup$
    – Nasser
    Commented Nov 21, 2014 at 22:54
  • 5
    $\begingroup$ There's no general way to declare a variable as real, integer etc. Instead, some functions take an Assumptions option which affects that function. Take a look at reference.wolfram.com/language/tutorial/UsingAssumptions.html and also ComplexExpand. There can be a default for the Assumptions option through $Assumptions but it won't affect everything, only functions that know about assumptions. $\endgroup$
    – Szabolcs
    Commented Nov 21, 2014 at 22:56

3 Answers 3

40
$\begingroup$

You can also use Refine with Element :

Refine[Sqrt[2] Conjugate[Sqrt[1/L]] Sin[(Pi* Conjugate[n x])/Conjugate[L]],
{Element[L, Reals], Element[n, Integers]}]

gives

enter image description here

and if you add that L>0:

Refine[Sqrt[2] Conjugate[Sqrt[1/L]] Sin[(Pi* Conjugate[n x])/Conjugate[L]],
{Element[L, Reals], Element[n, Integers], L > 0}]

enter image description here

Other simple examples :

1. Example with reals

Re[x + I*y]

returns

-Im[y] + Re[x]

but if x is assumed to be real :

Refine[Re[x + I*y], Element[x, Reals]]

gives

x - Im[y]

and if x and y are assumed to be reals :

Refine[Re[x + I*y], {Element[x, Reals], Element[y, Reals]}]

gives

x

2. Example with integers

Sin[n*Pi]

gives the same

Sin[n*Pi]

but if n is assumed to be an integer :

Refine[Sin[n*Pi], Element[n, Integers]]

returns

0
$\endgroup$
7
  • $\begingroup$ Fantastic! This is exactly what I wanted. Can I tell mathematica that a variable is "globally" real valued, etc? Or do I have to add that Element[] function to every new input line? $\endgroup$ Commented Nov 22, 2014 at 19:38
  • $\begingroup$ After playing around with Refine[] for a bit, it looks like I can just do all of my calculations within the Refine function, add it my constraints at the end, and get a nice looking answer. Am I correct in that assessment? $\endgroup$ Commented Nov 22, 2014 at 21:38
  • $\begingroup$ @MatthewBrunetti Theoretically yes ... It is maybe safer to use assumptions as soon as possible in your calculations. As was shown in the other answer, for example Integrate can use Assumptions. It depends on the problem. Anyway see the docs in particular the tutorials under "Learning Ressources" ;) $\endgroup$
    – SquareOne
    Commented Nov 22, 2014 at 22:30
  • $\begingroup$ @MatthewBrunetti Maybe the function Assuming is what you are looking for because it "spreads" the assumptions to all the functions in an expression at the same time. $\endgroup$
    – SquareOne
    Commented Nov 22, 2014 at 23:13
  • 3
    $\begingroup$ Note that, in place of the assumption Element[t, Reals] you can use the assumption _Symbol ∈ Reals to assume that all explicit variables are real. This allows you to encapsulate this in a function without having to manually pass a list of arguments. For an exhaustive discussion, see here. $\endgroup$ Commented Sep 27, 2017 at 18:56
19
$\begingroup$
$Assumptions = n > 0 && n ∈ Integers && L ∈ Reals && L > 0

You can type symbol in the form n ∈ Integers using ESC+el+ESC, or by typing \[Element], or you can use the the alternative Element[n, Integers] form.

To delete all global assumptions: $Assumptions = True

$\endgroup$
1
  • 1
    $\begingroup$ According to the documentation, use [Esc (Escape) key] el [Esc key]. $\endgroup$
    – bbgodfrey
    Commented Jun 13, 2017 at 17:38
7
$\begingroup$

Conjugate by default assumes that all symbolic quantities are potentially complex. This may seem annoying at first, but there is a very good reason for it, and one way to see why is to define your own version of Conjugate, and see it fail. For educational purposes, I do that below.

Define $Conjugate as follows:

$Conjugate[x_] := x /. Complex[a_, b_] :> a - I b;

This simply replaces any occurrence of I with -I. For example:

$Conjugate[u[x]]

produces

Sqrt[2] Sqrt[1/L] Sin[(n π x)/L]

which is presumably what you expected.

Beware that this simple definition of $Conjugate is only guaranteed to work on numbers and functions which satisfy $f(\overline{x})=\overline{f(x)}$, which oddly enough is true for the majority of analytic elementary functions, such as Zeta, Gamma, Sin, Log, and many others.

One possible counterexample is SphericalHarmonicY:

$Conjugate[ SphericalHarmonicY[3, θ, ϕ, a + b I]] /. 
                                                    {θ -> 2.3, ϕ -> 1.0, a -> 1.2, b -> 2.5}
$Conjugate[ SphericalHarmonicY[3, θ, ϕ,  a + b I] /. 
                                                    {θ -> 2.3, ϕ -> 1.0, a -> 1.2, b -> 2.5}]  

which produces two different outputs:

-104.193 + 41.8086 I
-0.00105549 - 0.000423525 I

In contrast, replacing $Conjugate with the built-in Conjugate gives correct results. So while Conjugate may give silly-looking outputs, it is guaranteed to be correct; thus a simple substitution $a+bi\rightarrow a-bi$ does not necessarily give correct results!

Meanwhile, to make the assumption that n is an integer in your integral, do this:

Simplify[Integrate[u[x]*(-h^2)*u''[x], {x, 0, L}], Assumptions -> n ∈ Integers]

which returns

(h^2 n^2 π^2)/L^2
$\endgroup$
2
  • 2
    $\begingroup$ "which oddly enough is true for the majority of analytic elementary functions" -- This is true for an analytic function iff it has real coefficients in its Taylor series (as can easily be seen by expanding it in said Taylor series). Thus in particular if the function is real on [a segment of] the real axis, and analytic it will work. Be careful with functions with singularities, however. Your statement about logarithms is not in general true. Notice that on Mathematica's branch $\log(-1) = i \pi$ but $\log(\overline{-1}) = i \pi \neq \overline{i\pi}$. You need to cross branches. $\endgroup$ Commented May 19, 2016 at 21:05
  • $\begingroup$ "$Conjugate[x_] := x /. Complex[a_, b_] :> a - I b;" is Great for simple cases. $\endgroup$
    – hlren
    Commented Jun 19, 2021 at 14:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.