Ask Your Question
0

round function for matrices ?

asked 2024-02-14 04:30:56 +0100

updated 2024-02-15 08:42:54 +0100

FrédéricC gravatar image

Given a matrix with real entries, how can I display that matrix with all entries rounded to a particular number of decimal places? The "round" function doesn't seem to work when you call a matrix instead of a scalar value.

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
3

answered 2024-02-14 11:25:12 +0100

rburing gravatar image

updated 2024-02-15 11:20:58 +0100

Setup:

sage: A = MatrixSpace(RR, 4, 4).random_element(); A
[ 0.920573676643417 -0.560186873335771 -0.402412102767735  0.914068238090819]
[ 0.764033488007043  0.362387514136435 -0.978584162444727 -0.146282232026529]
[ 0.361536832568428 -0.568836944889535  0.482853931905572 -0.356197531222867]
[-0.209529579979057 -0.253957680611044 -0.335959826512397 -0.289775531888556]

Rounding:

sage: A.numerical_approx(digits=5)
[ 0.92057 -0.56019 -0.40241  0.91407]
[ 0.76403  0.36239 -0.97858 -0.14628]
[ 0.36154 -0.56884  0.48285 -0.35620]
[-0.20953 -0.25396 -0.33596 -0.28978]

More generalizable, applying a map to to all entries:

sage: A.apply_map(lambda z: z.numerical_approx(digits=5))
[ 0.92057 -0.56019 -0.40241  0.91407]
[ 0.76403  0.36239 -0.97858 -0.14628]
[ 0.36154 -0.56884  0.48285 -0.35620]
[-0.20953 -0.25396 -0.33596 -0.28978]

Alternative, specifically for methods:

sage: A.apply_map(attrcall('numerical_approx', digits=5))
[ 0.92057 -0.56019 -0.40241  0.91407]
[ 0.76403  0.36239 -0.97858 -0.14628]
[ 0.36154 -0.56884  0.48285 -0.35620]
[-0.20953 -0.25396 -0.33596 -0.28978]
edit flag offensive delete link more
1

answered 2024-02-16 02:24:03 +0100

dan_fulea gravatar image

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...

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2024-02-14 04:30:56 +0100

Seen: 158 times

Last updated: Feb 16