I am using functions that are only well-defined for real values (e.g. HeavisideTheta) within NDSolve.

Internally NDSolve works, of course, with complex numbers and sometimes returns a numerically vansihing complex value (e.g. 0.` i) that breaks these functions.

Is it possible to get rid of the complex part (like applying Chop at each step)?

  • 6
    $\begingroup$ It would be good to provide a simple example demonstrating the problem, for people to test possible answers with. $\endgroup$ Nov 18, 2013 at 21:48

2 Answers 2


Instead of getting rid of the small complex part after each internal step of NDSolve, I would make the projection before feeding those wimpy functions:

unbreakableHeavisideTheta[x_] := HeavisideTheta[Chop[x]]

Go to the Mathematica documentation for the HeavysideTheta function. There is an example for integration:

Integrate[HeavisideTheta'''[x - a] f[x], {x, -Infinity, Infinity}, 
 Assumptions -> a ∈ Reals]

The options Assumptions works with every built-in of Mathematica.

Mathematica allows the $Assumptions shell variable.

Simply set:

$Assumptions = x∈Reals

in the complete notebook or in a Block.

In the notebook set $Assumptions = True if not needed anymore.

Since Mathematica infers the domain from the input, just input Reals only will do usually enough work. Check if the input variables and constant is done with Elements

Following the idea:

unbreakableHeavisideTheta[x_] := HeavisideTheta[Re[x]]

Re makes every complex number real ignoring the complex part of it.

  • 1
    $\begingroup$ (-1) "The options Assumptions works with every built-in of Mathematica. " No, many functions don't have the option Assumptions, and NDSolve is one of them. $Assumptions won't work either, because it only influences functions with Assumptions option. $\endgroup$
    – xzczd
    Jun 8, 2020 at 14:21
  • $\begingroup$ $Assumptions, Mathematica documentation, Quiet[Select[ToExpression /@ Names["System*"], Head[#] === Symbol && MemberQ[Options[#], Assumptions :> $Assumptions] &]]`. reference.wolfram.com/language/guide/Expressions.html "At the core of the Wolfram Language is the foundational idea that everything—data, programs, formulas, graphics, documents—can be represented as symbolic expressions. And it is this unifying concept that underlies the Wolfram Language's symbolic programming paradigm, and makes possible of the unique power of the Wolfram Language and the Wolfram System." $\endgroup$ Jun 10, 2020 at 9:49
  • $\begingroup$ The quoted materials don't support your point. Show me an example that Assumptions / $Assumptions works with NDSolve or at least a built-in that doesn't own Assumptions option, and I'll retract my downvote. $\endgroup$
    – xzczd
    Jun 10, 2020 at 10:46
  • 1
    $\begingroup$ I don't think the comments are not related. First, it's about adding Assumptions to DSolve/DSolveValue, rather than NDSolve. And, though the answer is written before the release of v10.3 (Assumptions becomes an option of DSolve in this version), as mentioned by Michael there, DSolve is influenced by Assuming because functions with Assumptions option such as Integrate, Simplify, etc. have been called internally. $\endgroup$
    – xzczd
    Jun 10, 2020 at 12:45
  • 1
    $\begingroup$ Anyway, this is indeed an example that a built-in doesn't own Assumptions option is influenced by Assumptions, so I fulfill my promise, downvote retracted. But, I'd like to emphasize again the statement The options Assumptions works with every built-in of Mathematica is incorrect and cases like the DSolve example above are rather rare. $\endgroup$
    – xzczd
    Jun 10, 2020 at 12:55

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