From 6bbc393aff5f0a9a8fa67b00989d6199d4b3cc71 Mon Sep 17 00:00:00 2001 From: rocky Date: Tue, 13 Sep 2022 14:26:01 -0400 Subject: [PATCH 1/4] Go over docs for Accuracy and Precision * Shy away from using markup that doesn't render properly in Django. In partuclar, math mode * Add Wiki and WMA links in Accuracy and Precision builtin functions, and * more cross refrences * Move unit tests in autodoc to unit test * reversed quote -> RawBackquote which is the WMA term for the character --- mathics/builtin/atomic/numbers.py | 50 +++++++++++++------------ mathics/doc/documentation/1-Manual.mdoc | 42 ++++++++++----------- test/builtin/atomic/test_numbers.py | 16 ++++++++ 3 files changed, 62 insertions(+), 46 deletions(-) diff --git a/mathics/builtin/atomic/numbers.py b/mathics/builtin/atomic/numbers.py index 9ad9e4add..3f86641e1 100644 --- a/mathics/builtin/atomic/numbers.py +++ b/mathics/builtin/atomic/numbers.py @@ -148,12 +148,13 @@ def convert_float(x, base, exponents): class Accuracy(Builtin): """ + :Accuracy: https://en.wikipedia.org/wiki/Accuracy_and_precision (WMA :Accuracy: https://reference.wolfram.com/language/ref/Accuracy.html) +
'Accuracy[$x$]'
examines the number of significant digits of $expr$ after the decimal point in the number x.
- This is rather a proof-of-concept than a full implementation. - + This is rather a proof-of-concept than a full implementation. Accuracy of a real number is estimated from its value and its precision: @@ -176,9 +177,15 @@ class Accuracy(Builtin): Accuracy of expressions is given by the minimum accuracy of its elements: >> Accuracy[F[1, Pi, A]] = Infinity + >> Accuracy[F[1.3, Pi, A]] = 14.8861 + 'Accuracy' for the value 0 is a fixed-precision Real number: + >> 0``2 + = 0.00 + + See also :'Precision': /doc/reference-of-built-in-symbols/atomic-elements-of-expressions/representation-of-numbers/precision/. """ summary_text = "find the accuracy of a number" @@ -954,40 +961,37 @@ def apply(self, expr, evaluation): class Precision(Builtin): """ + :Precision: https://en.wikipedia.org/wiki/Accuracy_and_precision (WMA :Precision: https://reference.wolfram.com/language/ref/Precision.html) +
'Precision[$expr$]'
examines the number of significant digits of $expr$.
- This is rather a proof-of-concept than a full implementation. - Precision of compound expression is not supported yet. + + This is rather a proof-of-concept than a full implementation. + + Precision of compound expression is not supported yet. + + The precision of an exact number, e.g. an Integer, is 'Infinity': + >> Precision[1] = Infinity + + A fraction is an exact number too, so its Precision is 'Infinity': + >> Precision[1/2] = Infinity - >> Precision[0.5] - = MachinePrecision - #> Precision[0.0] - = MachinePrecision - #> Precision[0.000000000000000000000000000000000000] - = 0. - #> Precision[-0.0] - = MachinePrecision - #> Precision[-0.000000000000000000000000000000000000] - = 0. + Numbers entered in the form $digits$`$p$ are taken to have precision $p$: - #> 1.0000000000000000 // Precision - = MachinePrecision - #> 1.00000000000000000 // Precision - = 17. + >> Precision[1.23`10] + = 10. - #> 0.4 + 2.4 I // Precision + Precision of a machineā€precision number is 'MachinePrecision': + >> Precision[0.5] = MachinePrecision - #> Precision[2 + 3 I] - = Infinity - #> Precision["abc"] - = Infinity + See also :'Accuracy': /doc/reference-of-built-in-symbols/atomic-elements-of-expressions/representation-of-numbers/accuracy/. """ rules = { diff --git a/mathics/doc/documentation/1-Manual.mdoc b/mathics/doc/documentation/1-Manual.mdoc index 269cb5c96..5f7ab6a67 100644 --- a/mathics/doc/documentation/1-Manual.mdoc +++ b/mathics/doc/documentation/1-Manual.mdoc @@ -196,50 +196,46 @@ The result of the previous query to \Mathics can be accessed by '%':
-\Mathics handles relative ('Precision') and absolute ('Accuracy') uncertanties in numerical quantities. 'Precision' is set by adding a single reversed quote $`$ -and the numerical value of the precision right after the last digit of the mantissa. For example, +\Mathics handles relative and absolute uncertanties in numerical quantities. The precision or relative accuracy, is set by adding a RawBackquote character ('`') and the number of digits of precision in the mantissa. For example: - >> a = 3.1416`3 + >> 3.1416`3 = 3.14 -set $a$ with a number having a relative uncertainty of $10^{-3}$, in a way that this number is numerically equivalent to $3.1413`4$: +Above, two decimal places are shown in output after the decimal point, but three places of precision are stored. - >> a == 3.1413`4 +The relative uncertainty of '3.1416`3' is 10^-3. It is numerically equivalent, in three places after the decimal point, to 3.1413`4: + + >> 3.1416`3 == 3.1413`4 = True -We can recover the precision of the number by using 'Precision' +We can get the precision of the number by using the \Mathics :'Precision': /doc/reference-of-built-in-symbols/atomic-elements-of-expressions/representation-of-numbers/precision/ function: - >> Precision[a] - = 3. + >> Precision[3.1413`4] + = 4. -Up to the precision, $a$ is equivalent to $\pi$, so +While 3.1419 not the closest approximation to Pi in 4 digits after the decimal point (or with precision 4), for 3 digits of precision it is: - >> Pi - a - = 0. - >> Pi == a + >> Pi == 3.141987654321`3 = True -In a similar way, 'Accuracy' is set by adding a double reversed quote $``$ -and the numerical value of the accuracy right after the last digit of the mantissa. For example, +The absolute accuracy of a number, is set by adding a two RawBackquotes '``' and the number digits. + +For example: - >> a = 13.1416``4 + >> 13.1416``4 = 13.142 -set $a$ with a number having a absolute uncertainty of $10^{-4}$, in a way that this number is numerically equivalent to $13.1413``4$: +is a number having a absolute uncertainty of 10^-4. This number is numerically equivalent to '13.1413``4': - >> a == 13.1413``4 + >> 13.1416``4 == 13.1413``4 = True -For $0$, 'Precision' is ignored, since corresponds to a zero uncertainty: - - >> 0`4 - = 0 - -while 'Accuracy' is taken into account by storing the value as a fixed precision Real number: +The absolute accuracy for the value 0 is a fixed-precision Real number: >> 0``4 = 0.0000 +See also :Accuracy and precision: https://en.wikipedia.org/wiki/Accuracy_and_precision.
diff --git a/test/builtin/atomic/test_numbers.py b/test/builtin/atomic/test_numbers.py index 124988659..03af3a9d9 100644 --- a/test/builtin/atomic/test_numbers.py +++ b/test/builtin/atomic/test_numbers.py @@ -175,3 +175,19 @@ def test_n(): ("N[1.01234567890123456789`, 2] // Precision", "MachinePrecision"), ): check_evaluation(str_expr, str_expected) + + +def test_precision(): + for str_expr, str_expected in ( + ("0`4", "0"), + ("Precision[0.0]", "MachinePrecision"), + ("Precision[0.000000000000000000000000000000000000]", "0."), + ("Precision[-0.0]", "MachinePrecision"), + ("Precision[-0.000000000000000000000000000000000000]", "0."), + ("1.0000000000000000 // Precision", "MachinePrecision"), + ("1.00000000000000000 // Precision", "17."), + (" 0.4 + 2.4 I // Precision", "MachinePrecision"), + ("Precision[2 + 3 I]", "Infinity"), + ('Precision["abc"]', "Infinity"), + ): + check_evaluation(str_expr, str_expected) From 151bfc3d248c544116b48901f193bfdb6980d272 Mon Sep 17 00:00:00 2001 From: mmatera Date: Tue, 13 Sep 2022 16:00:02 -0300 Subject: [PATCH 2/4] adding tests --- mathics/builtin/atomic/numbers.py | 15 ++++++++++++--- test/builtin/atomic/test_numbers.py | 26 ++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/mathics/builtin/atomic/numbers.py b/mathics/builtin/atomic/numbers.py index 3f86641e1..adf583e94 100644 --- a/mathics/builtin/atomic/numbers.py +++ b/mathics/builtin/atomic/numbers.py @@ -185,6 +185,11 @@ class Accuracy(Builtin): >> 0``2 = 0.00 + In compound expressions, the 'Accuracy' is fixed by the number with + the lowest 'Accuracy': + >> Accuracy[{{1, 1.`},{1.``5, 1.``10}}] + = 5. + See also :'Precision': /doc/reference-of-built-in-symbols/atomic-elements-of-expressions/representation-of-numbers/precision/. """ @@ -968,9 +973,7 @@ class Precision(Builtin):
examines the number of significant digits of $expr$. - This is rather a proof-of-concept than a full implementation. - - Precision of compound expression is not supported yet. + This is rather a proof-of-concept than a full implementation. The precision of an exact number, e.g. an Integer, is 'Infinity': @@ -991,6 +994,12 @@ class Precision(Builtin): >> Precision[0.5] = MachinePrecision + In compound expressions, the 'Precision' is fixed by the number with + the lowest 'Precision': + >> Precision[{{1, 1.`},{1.`5, 1.`10}}] + = 5. + + See also :'Accuracy': /doc/reference-of-built-in-symbols/atomic-elements-of-expressions/representation-of-numbers/accuracy/. """ diff --git a/test/builtin/atomic/test_numbers.py b/test/builtin/atomic/test_numbers.py index 03af3a9d9..415db64c0 100644 --- a/test/builtin/atomic/test_numbers.py +++ b/test/builtin/atomic/test_numbers.py @@ -177,6 +177,30 @@ def test_n(): check_evaluation(str_expr, str_expected) +def test_accuracy(): + for str_expr, str_expected in ( + ("0`4", "0"), + ("Accuracy[0.0]", "15."), + ("Accuracy[0.000000000000000000000000000000000000]", "36."), + ("Accuracy[-0.0]", "15."), + # In WMA, this gives 36. Seems to be a rounding issue + # ("Accuracy[-0.000000000000000000000000000000000000]", "36."), + ("1.0000000000000000 // Accuracy", "15."), + ("1.00000000000000000 // Accuracy", "17."), + # Returns the accuracy of ```2.4``` + (" 0.4 + 2.4 I // Accuracy", "14.6198"), + ("Accuracy[2 + 3 I]", "Infinity"), + ('Accuracy["abc"]', "Infinity"), + # Returns the accuracy of ``` 3.2`3 ``` + ('Accuracy[F["a", 2, 3.2`3]]', "2.49482"), + ('Accuracy[{{a, 2, 3.2`},{2.1`5, 3.2`3, "a"}}]', "2.49482"), + # Anoter case of issues with rounding. In Mathics, this returns + # 2.67776 + # ('Accuracy[{{a, 2, 3.2`},{2.1``3, 3.2``5, "a"}}]', '3.'), + ): + check_evaluation(str_expr, str_expected) + + def test_precision(): for str_expr, str_expected in ( ("0`4", "0"), @@ -189,5 +213,7 @@ def test_precision(): (" 0.4 + 2.4 I // Precision", "MachinePrecision"), ("Precision[2 + 3 I]", "Infinity"), ('Precision["abc"]', "Infinity"), + ('Precision[F["a", 2, 3.2`3]]', "3."), + ('Precision[{{a,2,3.2`},{2.1`5, 2.`3, "a"}}]', "3."), ): check_evaluation(str_expr, str_expected) From 585d856cde6f83cf5fc94ffae4aa0071ae8e8c46 Mon Sep 17 00:00:00 2001 From: Juan Mauricio Matera Date: Tue, 13 Sep 2022 16:51:00 -0300 Subject: [PATCH 3/4] fix typo --- test/builtin/atomic/test_numbers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/builtin/atomic/test_numbers.py b/test/builtin/atomic/test_numbers.py index 415db64c0..9039abc63 100644 --- a/test/builtin/atomic/test_numbers.py +++ b/test/builtin/atomic/test_numbers.py @@ -194,7 +194,7 @@ def test_accuracy(): # Returns the accuracy of ``` 3.2`3 ``` ('Accuracy[F["a", 2, 3.2`3]]', "2.49482"), ('Accuracy[{{a, 2, 3.2`},{2.1`5, 3.2`3, "a"}}]', "2.49482"), - # Anoter case of issues with rounding. In Mathics, this returns + # Another case of issues with rounding. In Mathics, this returns # 2.67776 # ('Accuracy[{{a, 2, 3.2`},{2.1``3, 3.2``5, "a"}}]', '3.'), ): From 9be4fc551105824fed694595b29cb02cdc5b570f Mon Sep 17 00:00:00 2001 From: rocky Date: Thu, 15 Sep 2022 07:37:54 -0400 Subject: [PATCH 4/4] Remove a hard line break in Accuracy doc --- mathics/builtin/atomic/numbers.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/mathics/builtin/atomic/numbers.py b/mathics/builtin/atomic/numbers.py index adf583e94..0808fabbb 100644 --- a/mathics/builtin/atomic/numbers.py +++ b/mathics/builtin/atomic/numbers.py @@ -161,8 +161,7 @@ class Accuracy(Builtin): >> Accuracy[3.1416`2] = 1.50298 - Notice that the value is not exactly equal to the obtained in WMA: This is due to the different way in which - Precision is handled in SymPy. + Notice that the value is not exactly equal to the obtained in WMA: This is due to the different way in which 'Precision' is handled in SymPy. Accuracy for exact atoms is $Infinity$: >> Accuracy[1]