# Custom error to check stochastic matrix

**URL:** https://discourse.julialang.org/t/custom-error-to-check-stochastic-matrix/89259
**Category:** General Usage
**Created:** [October 25, 2022, 8:24pm UTC](https://discourse.julialang.org/t/custom-error-to-check-stochastic-matrix/89259 "2022-10-25T20:24:17Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![ejlongman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ejlongman/32/36801_2.png) [@ejlongman](https://discourse.julialang.org/u/ejlongman)
#### Post date: [October 25, 2022, 8:24pm UTC](https://discourse.julialang.org/t/custom-error-to-check-stochastic-matrix/89259/1 "2022-10-25T20:24:17Z")

</div>

I would like to write a function that checks whether the rows of a stochastic matrix sum to 1 and throws an error if they do not.

The following code works for a check by hand:

```julia
sum(mat,dims = 2)

```

But this code does not work. It falsely flags all matrixes as not summing to 1, even “good” matrixes.

```julia
stoch_mat_check(mat) = any(x -> x != 1.0,sum(mat,dims = 2)) : error("ERROR: rows dont sum one")

mat = [.5,.5]
stoch_mat_check(mat)

```

Returns

```julia
ERROR: rows dont sum one

Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:33
 [2] stoch_mat_check(mat::Vector{Float64})
   @ Main ./In[231]:18
 [3] top-level scope
   @ In[231]:21
 [4] eval
   @ ./boot.jl:373 [inlined]
 [5] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
   @ Base ./loading.jl:1196

```

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [October 25, 2022, 8:31pm UTC](https://discourse.julialang.org/t/custom-error-to-check-stochastic-matrix/89259/2 "2022-10-25T20:31:36Z")

</div>

Two points.

1. This isn’t doing what you think it’s doing:

```julia
Julia> mat = [0.5, 0.5]
2-element Vector{Float64}:
0.5
0.5

julia> sum(mat, dims = 2)
2-element Vector{Float64}:
0.5
0.5

julia> mat = [0.5 0.5]
1×2 Matrix{Float64}:
 0.5 0.5

julia> sum(mat; dims = 2)
1×1 Matrix{Float64}:
 1.0

```

Note the different definition of `mat`.

1. Never use exact comparisons.

```julia
julia> 3 * 0.1 == 0.3
false

```

Use `isapprox(3 * 0.1, 0.3)` instead: [Getting started with Julia · JuMP](https://jump.dev/JuMP.jl/stable/tutorials/getting_started/getting_started_with_julia/#Floating-point-numbers)

---

<div class="post-metadata">

### Author: ![cchderrick](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@cchderrick](https://discourse.julialang.org/u/cchderrick)
#### Post date: [October 25, 2022, 8:32pm UTC](https://discourse.julialang.org/t/custom-error-to-check-stochastic-matrix/89259/3 "2022-10-25T20:32:37Z")

</div>

“mat” is vector, not matrix, it will acts like a row vector.

```julia
julia> mat
2-element Vector{Float64}:
 0.5
 0.5

julia> sum(mat, dims=2)
2-element Vector{Float64}:
 0.5
 0.5

```

You can initialize a matrix with spaces instead of comma.

```julia
julia> mat = [0.5 0.5]
1×2 Matrix{Float64}:
 0.5 0.5

julia> stoch_mat_check(mat)
false

```

---

<div class="post-metadata">

### Author: ![ejlongman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ejlongman/32/36801_2.png) [@ejlongman](https://discourse.julialang.org/u/ejlongman)
#### Post date: [October 25, 2022, 8:37pm UTC](https://discourse.julialang.org/t/custom-error-to-check-stochastic-matrix/89259/4 "2022-10-25T20:37:14Z")

</div>

Thank you! But this still returns the error:

```julia
#quick check: 
stoch_mat_check(mat) = any(x -> !(isapprox(x,1)),sum(mat,dims = 2)) : error("ERROR: rows dont sum one")

mat = [.5 .5]
stoch_mat_check(mat)

```

---

<div class="post-metadata">

### Author: ![cchderrick](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@cchderrick](https://discourse.julialang.org/u/cchderrick)
#### Post date: [October 25, 2022, 8:45pm UTC](https://discourse.julialang.org/t/custom-error-to-check-stochastic-matrix/89259/5 "2022-10-25T20:45:43Z")

</div>

oh yea, I forgot to mention, I think you meant to use `&&` short-circuit.

```julia
stoch_mat_check(mat) = any(x -> x != 1.0,sum(mat,dims = 2)) && error("ERROR: rows dont sum one")

```

If you want ternary, you are missing the `?` and statement for true

```julia
stoch_mat_check(mat) = any(x -> x != 1.0,sum(mat,dims = 2)) ? error("ERROR: rows dont sum one") : print("all good")

```

---

<div class="post-metadata">

### Author: ![ejlongman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ejlongman/32/36801_2.png) [@ejlongman](https://discourse.julialang.org/u/ejlongman)
#### Post date: [October 25, 2022, 8:46pm UTC](https://discourse.julialang.org/t/custom-error-to-check-stochastic-matrix/89259/6 "2022-10-25T20:46:54Z")

</div>

oh thank you, that’s it!! This is my first time making an error function.
