Skip to content

Commit 3691e3d

Browse files
authored
Merge pull request #1334 from mathics/Arg0
Handle Arg[0]
2 parents 91db1fb + 2d37720 commit 3691e3d

1 file changed

Lines changed: 47 additions & 8 deletions

File tree

mathics/builtin/arithmetic.py

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,12 +1298,26 @@ class Abs(_MPMathFunction):
12981298

12991299
class Arg(_MPMathFunction):
13001300
"""
1301-
<dl>
1302-
<dt>'Arg[$z$]'
1303-
<dd>returns the argument of a complex value $z$.
1304-
</dl>
1305-
>> Arg[-3]
1306-
= Pi
1301+
<dl>
1302+
<dt>'Arg'[$z$, $method_option$]</dt>
1303+
<dd>returns the argument of a complex value $z$.</dd>
1304+
1305+
<ul>
1306+
<li>'Arg'[$z$] is left unevaluated if $z$ is not a numeric quantity.
1307+
<li>'Arg'[$z$] gives the phase angle of $z$ in radians.
1308+
<li>The result from 'Arg'[$z$] is always between -Pi and +Pi.
1309+
<li>'Arg'[$z$] has a branch cut discontinuity in the complex $z$ plane running from -Infinity to 0.
1310+
<li>'Arg'[0] is 0.
1311+
</ul>
1312+
</dl>
1313+
1314+
>> Arg[-3]
1315+
= Pi
1316+
1317+
Same as above using sympy's method:
1318+
>> Arg[-3, Method->"sympy"]
1319+
= Pi
1320+
13071321
>> Arg[1-I]
13081322
= -Pi / 4
13091323
@@ -1313,15 +1327,40 @@ class Arg(_MPMathFunction):
13131327
= Pi / 4
13141328
>> Arg[DirectedInfinity[]]
13151329
= 1
1330+
Arg for 0 is assumed to be 0:
1331+
>> Arg[0]
1332+
= 0
13161333
"""
1317-
rules = {"Arg[DirectedInfinity[]]": "1",
1318-
"Arg[DirectedInfinity[a_]]": "Arg[a]",
1334+
rules = {
1335+
"Arg[0]": "0",
1336+
"Arg[DirectedInfinity[]]": "1",
1337+
"Arg[DirectedInfinity[a_]]": "Arg[a]",
13191338
}
1339+
13201340
attributes = ("Listable", "NumericFunction")
1341+
options = {"Method": "Automatic"}
13211342

1343+
numpy_name = "angle" # for later
13221344
mpmath_name = "arg"
13231345
sympy_name = "arg"
13241346

1347+
def apply(self, z, evaluation, options={}):
1348+
"%(name)s[z_, OptionsPattern[%(name)s]]"
1349+
if Expression("PossibleZeroQ", z).evaluate(evaluation) == SymbolTrue:
1350+
return Integer0
1351+
preference = self.get_option(options, "Method", evaluation).get_string_value()
1352+
if preference is None or preference == "Automatic":
1353+
return super(Arg, self).apply(z, evaluation)
1354+
elif preference == "mpmath":
1355+
return _MPMathFunction.apply(self, z, evaluation)
1356+
elif preference == "sympy":
1357+
return SympyFunction.apply(self, z)
1358+
# TODO: add NumpyFunction
1359+
evaluation.message(
1360+
"meth", f'Arg Method {preference} not in ("sympy", "mpmath")'
1361+
)
1362+
return
1363+
13251364

13261365
class Sign(SympyFunction):
13271366
"""

0 commit comments

Comments
 (0)