# How to round whole matrix like numpy.round()

**URL:** <https://discourse.julialang.org/t/how-to-round-whole-matrix-like-numpy-round/84897>\
**Category:** General Usage\
**Tags:** question, symbolic, sympy, rounding\
**Created:** [July 27, 2022, 11:11pm UTC](https://discourse.julialang.org/t/how-to-round-whole-matrix-like-numpy-round/84897 "2022-07-27T23:11:12Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Starpower\_Power](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/starpower_power/32/38282_2.png) [@Starpower\_Power](https://discourse.julialang.org/u/Starpower_Power)\
**Post date:** [July 27, 2022, 11:11pm UTC](https://discourse.julialang.org/t/how-to-round-whole-matrix-like-numpy-round/84897/1 "2022-07-27T23:11:12Z")

</div>

1. how to round whole matrix like numpy.round()
2. how to round SymPy Matrix in julia SymPy pakage

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [July 27, 2022, 11:16pm UTC](https://discourse.julialang.org/t/how-to-round-whole-matrix-like-numpy-round/84897/2 "2022-07-27T23:16:49Z")

</div>

> [@Starpower\_Power](#):
>
> how to round whole matrix

```julia
round.(your_matrix)

```

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [July 28, 2022, 12:58am UTC](https://discourse.julialang.org/t/how-to-round-whole-matrix-like-numpy-round/84897/3 "2022-07-28T00:58:52Z")

</div>

You can also explictly request rounding to a 64-bit integer.

```julia
julia> round.([3.4 5.6; 7.8 10.9])
2×2 Matrix{Float64}:
 3.0 6.0
 8.0 11.0

julia> round.(Int, [3.4 5.6; 7.8 10.9])
2×2 Matrix{Int64}:
 3 6
 8 11

```

The important character here is the `.` which “broadcasts” your function to each element of the matrix.

---

<div class="post-metadata">

**Author:** ![Starpower\_Power](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/starpower_power/32/38282_2.png) [@Starpower\_Power](https://discourse.julialang.org/u/Starpower_Power)\
**Post date:** [July 28, 2022, 12:33pm UTC](https://discourse.julialang.org/t/how-to-round-whole-matrix-like-numpy-round/84897/4 "2022-07-28T12:33:53Z")

</div>

1. how to round SymPy Matrix in julia SymPy pakage  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/c/f/cf6cac3091f786a6afa0d7c531640eb9f13b0c23.png)

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [July 28, 2022, 12:46pm UTC](https://discourse.julialang.org/t/how-to-round-whole-matrix-like-numpy-round/84897/5 "2022-07-28T12:46:41Z")

</div>

Did you try `round.(dh)`?

---

<div class="post-metadata">

**Author:** ![Starpower\_Power](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/starpower_power/32/38282_2.png) [@Starpower\_Power](https://discourse.julialang.org/u/Starpower_Power)\
**Post date:** [July 28, 2022, 9:10pm UTC](https://discourse.julialang.org/t/how-to-round-whole-matrix-like-numpy-round/84897/6 "2022-07-28T21:10:52Z")

</div>

yes. I tried. and the error is can’t round symbolic expression

---

<div class="post-metadata">

**Author:** ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)\
**Post date:** [July 29, 2022, 12:37am UTC](https://discourse.julialang.org/t/how-to-round-whole-matrix-like-numpy-round/84897/7 "2022-07-29T00:37:05Z")

</div>

Yes, what should that be? I mean, what would be expected? My guess is defining that to be a noop would be okay, right? Anyways, open an issue at SymPy.jl

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [July 29, 2022, 1:11am UTC](https://discourse.julialang.org/t/how-to-round-whole-matrix-like-numpy-round/84897/8 "2022-07-29T01:11:23Z")

</div>

Wait a minute, it’s not actually possible to `numpy.round` `sympy` expressions like `yb`, right? I never used `sympy` before, but I know `numpy` functions only work on some numerical types. Looking up [your error and `sympy` rounding](https://stackoverflow.com/questions/70474622/typeerror-cant-round-symbolic-expression), it looks like it’s only possible after you give symbols values _and_ convert the expressions to a numerical type with instance methods like `yb.evalf` or `yb.round`.

Assuming you can’t give any symbols values, you could skip those elements and round the rest:

```julia
julia> using SymPy
julia> @syms x, y
(x, y)
julia> m = [x pi/2
            Sym("pi")/3 y ]
2×2 Matrix{Sym}:
    x 1.57079632679490
 pi/3 y

julia> function roundtry(el, n)
         # I would prefer if-else, but neither sympy
         # nor Julia can simply test if .round works
         try
           el.round(n) # do not use Julia's round, it converts
         catch 
           el
         end
       end
roundtry (generic function with 1 method)

julia> roundtry.(m, 3)
2×2 Matrix{Sym}:
    x 1.571
 pi/3 y

```

Note that Julia’s `round` does not work like the SymPy `.round(n)`. I’m not sure if this is worth a PR, it is likely an intentional difference because Python-like dot-access syntax is a documented feature of SymPy.jl. It just means you shouldn’t use the same code for `Matrix{Sym}` as other `Matrix`es.

```julia
julia> round(m[3]; digits=3) |> typeof
Float64

julia> m[3].round(3) |> typeof
Sym

```

---

<div class="post-metadata">

**Author:** ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)\
**Post date:** [July 29, 2022, 12:13pm UTC](https://discourse.julialang.org/t/how-to-round-whole-matrix-like-numpy-round/84897/9 "2022-07-29T12:13:42Z")

</div>

Yes, currently `round` in SymPy punts (with an error) on symbols and converts to a Julia number, then rounds non-symbols. SymPy’s `round` also doesn’t handle symbolic values. Perhaps, the following behaviour is more in line with the the OP wants: `(x -> x.evalf(10)).(M)`. Either way, the `round` function in `SymPy.jl` could be improved.
