Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Here is the same answer, some nuances are shown. The function / method numerical_approx(...) has the short hand n(...)and its arguments / options are prec=None, digits=None, algorithm=None. For us, the first two are interesting. Please also compare with:

https://doc.sagemath.org/html/en/reference/misc/sage/misc/functional.html

Consider a number, and let us see what n() does with it.

sage: a = 56789.3456
sage: a.n(digits=2)
57000.
sage: a.n(digits=8)
56789.346
sage: a.n(prec=2)
49000.
sage: a.n(prec=6)
56000.
sage: a.n(prec=8)
57000.

sage: f"{a:e}"
'5.67893456000000e+4'

So digits what we want (prec is working binary, digits thinks in a decimal world). Now an example with matrices:

import random
random.seed('75986')

A = matrix(RR, 5, 5, [random.uniform(0, 10^random.choice([-4..10])) for count in range(5^2)])

And we obtain our test matrix:

sage: A
[     63543.3389328726  0.000736538191035056  0.000895407805608647 0.0000106867455934270    5.15508298915226e6]
[   7.86559376536411e7   0.00655431229353655    6.12649210201700e9    5.74262540915562e7    5.01590930940852e7]
[    0.272774522172060     0.567796814218967      503.414415232817     0.504025659604463      99609.9284950566]
[    0.727489291982834    7.42982613086165e6    8.14891687426721e8    3.29252151209820e6    1.85713734568604e6]
[   2.32144069623205e6    2.71752136036553e7    9.12158172757762e9 0.0000526060634628119      2915.02383079855]

Now we can try:

sage: n(A, digits=5)
[     63543.  0.00073654  0.00089541 0.000010687    5.1551e6]
[   7.8656e7   0.0065543    6.1265e9    5.7426e7    5.0159e7]
[    0.27277     0.56780      503.41     0.50403      99610.]
[    0.72749    7.4298e6    8.1489e8    3.2925e6    1.8571e6]
[   2.3214e6    2.7175e7    9.1216e9 0.000052606      2915.0]

and (depending on what we want) this is almost in all cases good enough. The five "main digits" are shown in each case. But we also have the entry 0.00089541 which is not rounded, instead, five relevant decimals are shown. If we really want a rounding (or truncation), we can define a new matrix (and call it B) with the rounded entries:

sage: matrix(5, 5, [round(entry, 3) for entry in A.list()])
[     63543.339          0.001          0.001            0.0    5155082.989]
[  78655937.654          0.007 6126492102.017   57426254.092   50159093.094]
[         0.273          0.568        503.414          0.504      99609.928]
[         0.727    7429826.131  814891687.427    3292521.512    1857137.346]
[   2321440.696   27175213.604 9121581727.578            0.0       2915.024]


sage: A.apply_map(lambda entry: round(entry, 3))
[     63543.339          0.001          0.001            0.0    5155082.989]
[  78655937.654          0.007 6126492102.017   57426254.092   50159093.094]
[         0.273          0.568        503.414          0.504      99609.928]
[         0.727    7429826.131  814891687.427    3292521.512    1857137.346]
[   2321440.696   27175213.604 9121581727.578            0.0       2915.024]

Sometimes this is what we want...