-
-
Notifications
You must be signed in to change notification settings - Fork 55
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
Merged
+612
−178
Merged
RealAbs and RealSign #885
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
@@ -1207,6 +1213,44 @@ class Real_(Builtin): | |
name = "Real" | ||
|
||
|
||
class RealAbs(Builtin): | ||
""" | ||
<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,6 +1281,42 @@ def test(self, expr) -> bool: | |
return isinstance(expr, (Integer, Rational, Real)) | ||
|
||
|
||
class RealSign(Builtin): | ||
""" | ||
<url>:WMA link:https://reference.wolfram.com/language/ref/RealAbs.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> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.