-
-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RealAbs and RealSign #885
RealAbs and RealSign #885
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,7 +77,13 @@ | |
SymbolTable, | ||
SymbolUndefined, | ||
) | ||
from mathics.eval.arithmetic import eval_Abs, eval_mpmath_function, eval_Sign | ||
from mathics.eval.arithmetic import ( | ||
eval_Abs, | ||
eval_mpmath_function, | ||
eval_negate_number, | ||
eval_RealSign, | ||
eval_Sign, | ||
) | ||
from mathics.eval.nevaluator import eval_N | ||
from mathics.eval.numerify import numerify | ||
|
||
|
@@ -823,7 +829,9 @@ def evaluate(self, evaluation: Evaluation): | |
|
||
class Im(SympyFunction): | ||
""" | ||
<url>:WMA link:https://reference.wolfram.com/language/ref/Im.html</url> | ||
<url> | ||
:WMA link: | ||
https://reference.wolfram.com/language/ref/Im.html</url> | ||
|
||
<dl> | ||
<dt>'Im[$z$]' | ||
|
@@ -1207,6 +1215,49 @@ class Real_(Builtin): | |
name = "Real" | ||
|
||
|
||
class RealAbs(Builtin): | ||
""" | ||
<url> | ||
:Abs (Real): | ||
https://en.wikipedia.org/wiki/Absolute_value</url> (<url> | ||
:WMA link: | ||
https://reference.wolfram.com/language/ref/RealAbs.html | ||
</url>) | ||
|
||
<dl> | ||
<dt>'RealAbs[$x$]' | ||
<dd>returns the absolute value of a real number $x$. | ||
</dl> | ||
'RealAbs' is also known as modulus. It is evaluated if $x$ can be compared \ | ||
with $0$. | ||
|
||
>> RealAbs[-3.] | ||
= 3. | ||
'RealAbs[$z$]' is left unevaluated for complex $z$: | ||
>> RealAbs[2. + 3. I] | ||
= RealAbs[2. + 3. I] | ||
>> D[RealAbs[x ^ 2], x] | ||
= 2 x ^ 3 / RealAbs[x ^ 2] | ||
""" | ||
|
||
attributes = A_LISTABLE | A_NUMERIC_FUNCTION | A_PROTECTED | ||
rules = { | ||
"D[RealAbs[x_],x_]": "x/RealAbs[x]", | ||
"Integrate[RealAbs[x_],x_]": "1/2 x RealAbs[x]", | ||
"Integrate[RealAbs[u_],{u_,a_,b_}]": "1/2 b RealAbs[b]-1/2 a RealAbs[a]", | ||
} | ||
summary_text = "real absolute value" | ||
|
||
def eval(self, x: BaseElement, evaluation: Evaluation): | ||
"""RealAbs[x_]""" | ||
real_sign = eval_RealSign(x) | ||
if real_sign is IntegerM1: | ||
return eval_negate_number(x) | ||
if real_sign is None: | ||
return | ||
return x | ||
|
||
|
||
class RealNumberQ(Test): | ||
""" | ||
## Not found in WMA | ||
|
@@ -1237,9 +1288,54 @@ def test(self, expr) -> bool: | |
return isinstance(expr, (Integer, Rational, Real)) | ||
|
||
|
||
class RealSign(Builtin): | ||
""" | ||
<url> | ||
:Signum: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The link name should be what Wikipedia calls it, so this should be "Sign function". Wikipedia mentions that this is also called "Signum" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I can change this in another iteration. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have included that change in #886, which is the next round of patches. |
||
https://en.wikipedia.org/wiki/Sign_function</url> (<url> | ||
:WMA link: | ||
https://reference.wolfram.com/language/ref/RealSign.html | ||
</url>) | ||
<dl> | ||
<dt>'RealSign[$x$]' | ||
<dd>returns $-1$, $0$ or $1$ depending on whether $x$ is negative, | ||
zero or positive. | ||
</dl> | ||
'RealSign' is also known as $sgn$ or $signum$ function. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If it is signum, we should add that before the WMA link a wikipedia link: https://en.wikipedia.org/wiki/Sign_function |
||
|
||
>> RealSign[-3.] | ||
= -1 | ||
'RealSign[$z$]' is left unevaluated for complex $z$: | ||
>> RealSign[2. + 3. I] | ||
= RealSign[2. + 3. I] | ||
|
||
>> D[RealSign[x^2],x] | ||
= 2 x Piecewise[{{0, x ^ 2 != 0}}, Indeterminate] | ||
>> Integrate[RealSign[u],{u,0,x}] | ||
= RealAbs[x] | ||
""" | ||
|
||
attributes = A_LISTABLE | A_NUMERIC_FUNCTION | A_PROTECTED | ||
rules = { | ||
"D[RealSign[x_],x_]": "Piecewise[{{0, x!=0}}, Indeterminate]", | ||
"Integrate[RealSign[x_],x_]": "RealAbs[x]", | ||
"Integrate[RealSign[u_],{u_, a_, b_}]": "RealAbs[b]-RealSign[a]", | ||
} | ||
summary_text = "real sign" | ||
|
||
def eval(self, x: Number, evaluation: Evaluation) -> Optional[Integer]: | ||
"""RealSign[x_]""" | ||
return eval_RealSign(x) | ||
|
||
|
||
class Sign(SympyFunction): | ||
""" | ||
<url>:WMA link:https://reference.wolfram.com/language/ref/Sign.html</url> | ||
<url> | ||
:Sign: | ||
https://en.wikipedia.org/wiki/Sign_function</url> (<url> | ||
:WMA link: | ||
https://reference.wolfram.com/language/ref/Sign.html | ||
</url>) | ||
|
||
<dl> | ||
<dt>'Sign[$x$]' | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment about Wiki reference.