Skip to content

Commit

Permalink
Misc tweaks...
Browse files Browse the repository at this point in the history
* Correct behavior when only one complex variable has been given
* Add "ComplexExpand" to CHANGES.rst
* More ComplexExpand doctests
* Correct url links
  • Loading branch information
rocky committed Jul 6, 2023
1 parent 3921db6 commit 91683f0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ New Builtins
#. ``$PythonImplementation``
#. ``Accuracy``
#. ``ClebschGordan``
#. ``ComplexExpand`` (@yzrun)
#. ``Curl`` (2-D and 3-D vector forms only)
#. ``DiscretePlot``
#. ``Kurtosis``
Expand Down
51 changes: 37 additions & 14 deletions mathics/builtin/numbers/hyperbolic.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,24 +251,47 @@ class ArcTanh(_MPMathFunction):

class ComplexExpand(SympyFunction):
"""
<url>
:SymPy:
https://docs.sympy.org/latest/modules/core.html#sympy.core.expr.Expr.expand
:WMA:
https://reference.wolfram.com/language/ref/ExpandComplex.html</url>)
(<url>
:SymPy:
https://docs.sympy.org/latest/
modules/core.html#sympy.core.expr.Expr.expand</url>, <url>:WMA:
https://reference.wolfram.com/language/ref/ComplexExpand.html
</url>)
<dl>
<dt>'ComplexExpand[$expr$]'
<dd>expands $expr$ assuming that all variables are real.
</dl>
<dl>
<dt>'ComplexExpand[$expr$]'
<dd>expands $expr$ assuming that all variables are real.
</dl>
Note: we get equivalent, but different results from WMA:
>> ComplexExpand[3^(I x)]
= 3 ^ (-Im[x]) Re[3 ^ (I Re[x])] + I Im[3 ^ (I Re[x])] 3 ^ (-Im[x])
Assume that both and are real:
>> ComplexExpand[Sin[x + I y]]
= Cosh[y] Sin[x] + I Cos[x] Sinh[y]
Take $x$ to be complex:
>> ComplexExpand[Sin[x], x]
= Cosh[Im[x]] Sin[Re[x]] + I Cos[Re[x]] Sinh[Im[x]]
Polynomials:
>> ComplexExpand[Re[z^5 - 2 z^3 - z + 1], z]
= 1 + Re[z] ^ 5 - 2 Re[z] ^ 3 - Re[z] - 10 Im[z] ^ 2 Re[z] ^ 3 + 5 Im[z] ^ 4 Re[z] + 6 Im[z] ^ 2 Re[z]
Note: we get equivalent, but different results from WMA:
Trigonometric and hyperbolic functions
>> ComplexExpand[Cos[x + I y] + Tanh[z], {z}]
= Cos[x] Cosh[y] - I Sin[x] Sinh[y] + Cosh[Re[z]] Sinh[Re[z]] / (Cos[Im[z]] ^ 2 + Sinh[Re[z]] ^ 2) + I Cos[Im[z]] Sin[Im[z]] / (Cos[Im[z]] ^ 2 + Sinh[Re[z]] ^ 2)
>> ComplexExpand[3^(I x)]
= 3 ^ (-Im[x]) Re[3 ^ (I Re[x])] + I Im[3 ^ (I Re[x])] 3 ^ (-Im[x])
Exponential and logarithmic functions:
>> ComplexExpand[Abs[2^z Log[2 z]], z]
= Abs[I Arg[Re[z] + I Im[z]] + Log[4 Im[z] ^ 2 + 4 Re[z] ^ 2] / 2] 2 ^ Re[z]
>> ComplexExpand[Sin[x + I y]]
= Cosh[y] Sin[x] + I Cos[x] Sinh[y]
Specify that a variable is take to be complex:
>> ComplexExpand[Re[2 z^3 - z + 1], z]
= 1 - Re[z] + 2 Re[z] ^ 3 - 6 Im[z] ^ 2 Re[z]
"""

summary_text = "expand a complex expression of real variables"
Expand Down
5 changes: 4 additions & 1 deletion mathics/eval/hyperbolic.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@

def eval_ComplexExpand(expr, vars):
sympy_expr = expr.to_sympy()
sympy_vars = {v.to_sympy() for v in vars.elements}
if hasattr(vars, "elements"):
sympy_vars = {v.to_sympy() for v in vars.elements}
else:
sympy_vars = {vars.to_sympy()}
# All vars are assumed to be real
replaces = [
(fs, SympySymbol(fs.name, real=True))
Expand Down

0 comments on commit 91683f0

Please sign in to comment.