|
77 | 77 | SymbolTable,
|
78 | 78 | SymbolUndefined,
|
79 | 79 | )
|
80 |
| -from mathics.eval.arithmetic import eval_Abs, eval_mpmath_function, eval_Sign |
| 80 | +from mathics.eval.arithmetic import ( |
| 81 | + eval_Abs, |
| 82 | + eval_mpmath_function, |
| 83 | + eval_negate_number, |
| 84 | + eval_RealSign, |
| 85 | + eval_Sign, |
| 86 | +) |
81 | 87 | from mathics.eval.nevaluator import eval_N
|
82 | 88 | from mathics.eval.numerify import numerify
|
83 | 89 |
|
@@ -823,7 +829,9 @@ def evaluate(self, evaluation: Evaluation):
|
823 | 829 |
|
824 | 830 | class Im(SympyFunction):
|
825 | 831 | """
|
826 |
| - <url>:WMA link:https://reference.wolfram.com/language/ref/Im.html</url> |
| 832 | + <url> |
| 833 | + :WMA link: |
| 834 | + https://reference.wolfram.com/language/ref/Im.html</url> |
827 | 835 |
|
828 | 836 | <dl>
|
829 | 837 | <dt>'Im[$z$]'
|
@@ -1207,6 +1215,49 @@ class Real_(Builtin):
|
1207 | 1215 | name = "Real"
|
1208 | 1216 |
|
1209 | 1217 |
|
| 1218 | +class RealAbs(Builtin): |
| 1219 | + """ |
| 1220 | + <url> |
| 1221 | + :Abs (Real): |
| 1222 | + https://en.wikipedia.org/wiki/Absolute_value</url> (<url> |
| 1223 | + :WMA link: |
| 1224 | + https://reference.wolfram.com/language/ref/RealAbs.html |
| 1225 | + </url>) |
| 1226 | +
|
| 1227 | + <dl> |
| 1228 | + <dt>'RealAbs[$x$]' |
| 1229 | + <dd>returns the absolute value of a real number $x$. |
| 1230 | + </dl> |
| 1231 | + 'RealAbs' is also known as modulus. It is evaluated if $x$ can be compared \ |
| 1232 | + with $0$. |
| 1233 | +
|
| 1234 | + >> RealAbs[-3.] |
| 1235 | + = 3. |
| 1236 | + 'RealAbs[$z$]' is left unevaluated for complex $z$: |
| 1237 | + >> RealAbs[2. + 3. I] |
| 1238 | + = RealAbs[2. + 3. I] |
| 1239 | + >> D[RealAbs[x ^ 2], x] |
| 1240 | + = 2 x ^ 3 / RealAbs[x ^ 2] |
| 1241 | + """ |
| 1242 | + |
| 1243 | + attributes = A_LISTABLE | A_NUMERIC_FUNCTION | A_PROTECTED |
| 1244 | + rules = { |
| 1245 | + "D[RealAbs[x_],x_]": "x/RealAbs[x]", |
| 1246 | + "Integrate[RealAbs[x_],x_]": "1/2 x RealAbs[x]", |
| 1247 | + "Integrate[RealAbs[u_],{u_,a_,b_}]": "1/2 b RealAbs[b]-1/2 a RealAbs[a]", |
| 1248 | + } |
| 1249 | + summary_text = "real absolute value" |
| 1250 | + |
| 1251 | + def eval(self, x: BaseElement, evaluation: Evaluation): |
| 1252 | + """RealAbs[x_]""" |
| 1253 | + real_sign = eval_RealSign(x) |
| 1254 | + if real_sign is IntegerM1: |
| 1255 | + return eval_negate_number(x) |
| 1256 | + if real_sign is None: |
| 1257 | + return |
| 1258 | + return x |
| 1259 | + |
| 1260 | + |
1210 | 1261 | class RealNumberQ(Test):
|
1211 | 1262 | """
|
1212 | 1263 | ## Not found in WMA
|
@@ -1237,9 +1288,54 @@ def test(self, expr) -> bool:
|
1237 | 1288 | return isinstance(expr, (Integer, Rational, Real))
|
1238 | 1289 |
|
1239 | 1290 |
|
| 1291 | +class RealSign(Builtin): |
| 1292 | + """ |
| 1293 | + <url> |
| 1294 | + :Signum: |
| 1295 | + https://en.wikipedia.org/wiki/Sign_function</url> (<url> |
| 1296 | + :WMA link: |
| 1297 | + https://reference.wolfram.com/language/ref/RealSign.html |
| 1298 | + </url>) |
| 1299 | + <dl> |
| 1300 | + <dt>'RealSign[$x$]' |
| 1301 | + <dd>returns $-1$, $0$ or $1$ depending on whether $x$ is negative, |
| 1302 | + zero or positive. |
| 1303 | + </dl> |
| 1304 | + 'RealSign' is also known as $sgn$ or $signum$ function. |
| 1305 | +
|
| 1306 | + >> RealSign[-3.] |
| 1307 | + = -1 |
| 1308 | + 'RealSign[$z$]' is left unevaluated for complex $z$: |
| 1309 | + >> RealSign[2. + 3. I] |
| 1310 | + = RealSign[2. + 3. I] |
| 1311 | +
|
| 1312 | + >> D[RealSign[x^2],x] |
| 1313 | + = 2 x Piecewise[{{0, x ^ 2 != 0}}, Indeterminate] |
| 1314 | + >> Integrate[RealSign[u],{u,0,x}] |
| 1315 | + = RealAbs[x] |
| 1316 | + """ |
| 1317 | + |
| 1318 | + attributes = A_LISTABLE | A_NUMERIC_FUNCTION | A_PROTECTED |
| 1319 | + rules = { |
| 1320 | + "D[RealSign[x_],x_]": "Piecewise[{{0, x!=0}}, Indeterminate]", |
| 1321 | + "Integrate[RealSign[x_],x_]": "RealAbs[x]", |
| 1322 | + "Integrate[RealSign[u_],{u_, a_, b_}]": "RealAbs[b]-RealSign[a]", |
| 1323 | + } |
| 1324 | + summary_text = "real sign" |
| 1325 | + |
| 1326 | + def eval(self, x: Number, evaluation: Evaluation) -> Optional[Integer]: |
| 1327 | + """RealSign[x_]""" |
| 1328 | + return eval_RealSign(x) |
| 1329 | + |
| 1330 | + |
1240 | 1331 | class Sign(SympyFunction):
|
1241 | 1332 | """
|
1242 |
| - <url>:WMA link:https://reference.wolfram.com/language/ref/Sign.html</url> |
| 1333 | + <url> |
| 1334 | + :Sign: |
| 1335 | + https://en.wikipedia.org/wiki/Sign_function</url> (<url> |
| 1336 | + :WMA link: |
| 1337 | + https://reference.wolfram.com/language/ref/Sign.html |
| 1338 | + </url>) |
1243 | 1339 |
|
1244 | 1340 | <dl>
|
1245 | 1341 | <dt>'Sign[$x$]'
|
|
0 commit comments