Ask Your Question

Revision history [back]

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

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]

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: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]