# Convert arrays to single precision

**URL:** https://discourse.julialang.org/t/convert-arrays-to-single-precision/10903
**Category:** General Usage
**Tags:** question
**Created:** [May 15, 2018, 4:18am UTC](https://discourse.julialang.org/t/convert-arrays-to-single-precision/10903 "2018-05-15T04:18:15Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)
#### Post date: [May 15, 2018, 4:18am UTC](https://discourse.julialang.org/t/convert-arrays-to-single-precision/10903/1 "2018-05-15T04:18:15Z")

</div>

Hello, I was wondering if there exists a function to convert objects, e.g. `Array`, `SparseMatrix`, special matrix types like `Diagonal`, etc…) to single precision.

It should also handle converting objects of complex numbers (e.g. `Matrix{Complex{Float64}}` → `Matrix{Complex{Float32}}`).

Thanks,  
Jeremy

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [May 15, 2018, 4:29am UTC](https://discourse.julialang.org/t/convert-arrays-to-single-precision/10903/2 "2018-05-15T04:29:05Z")

</div>

```julia
julia> A = rand(Complex128, 2, 2)
2×2 Array{Complex{Float64},2}:
 0.675516+0.723924im 0.599831+0.998357im
 0.526924+0.509325im 0.223859+0.164175im

julia> convert(Matrix{Complex64}, A)
2×2 Array{Complex{Float32},2}:
 0.675516+0.723924im 0.599831+0.998357im
 0.526924+0.509325im 0.223859+0.164175im

```

---

<div class="post-metadata">

### Author: ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)
#### Post date: [May 15, 2018, 4:33am UTC](https://discourse.julialang.org/t/convert-arrays-to-single-precision/10903/3 "2018-05-15T04:33:20Z")

</div>

I know about `convert`, but I am looking for something that will preserve the container type…

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [May 15, 2018, 4:33am UTC](https://discourse.julialang.org/t/convert-arrays-to-single-precision/10903/4 "2018-05-15T04:33:32Z")

</div>

i don’t believe such a function exists, but Julia provides all the tools to construct one.

```julia
julia> singlify(x::Float64) = Float32(x)
singlify (generic function with 1 method)

julia> singlify(x::Complex{Float64}) = Complex{Float32}(x)
singlify (generic function with 2 methods)

```

Broadcasting works quite nicely for applying the function elementwise to various different kinds of arrays:

```julia
julia> x = rand(2, 2)
2×2 Array{Float64,2}:
 0.689747 0.539186
 0.607853 0.858326

julia> singlify.(x)
2×2 Array{Float32,2}:
 0.689747 0.539186
 0.607853 0.858326

julia> x = sprand(2, 2, 0.5)
2×2 SparseMatrixCSC{Float64,Int64} with 4 stored entries:
  [1, 1] = 0.0518641
  [2, 1] = 0.896834
  [1, 2] = 0.0067949
  [2, 2] = 0.57429

julia> singlify.(x)
2×2 SparseMatrixCSC{Float32,Int64} with 4 stored entries:
  [1, 1] = 0.0518641
  [2, 1] = 0.896834
  [1, 2] = 0.0067949
  [2, 2] = 0.57429

```

Unfortunately, Diagonals aren’t handled perfectly:

```julia
julia> x = Diagonal([1., 2])
2×2 Diagonal{Float64}:
 1.0 ⋅ 
  ⋅ 2.0

julia> singlify.(x)
2×2 SparseMatrixCSC{Float32,Int64} with 2 stored entries:
  [1, 1] = 1.0
  [2, 2] = 2.0

```

The fact that broadcasting over a `Diagonal` returns a plain sparse matrix is, fortunately, resolved in the latest nightly builds of Julia, so in the future, you should get a `Diagonal` out.

---

<div class="post-metadata">

### Author: ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)
#### Post date: [May 15, 2018, 4:37am UTC](https://discourse.julialang.org/t/convert-arrays-to-single-precision/10903/5 "2018-05-15T04:37:05Z")

</div>

I see, thanks!

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [May 15, 2018, 5:57am UTC](https://discourse.julialang.org/t/convert-arrays-to-single-precision/10903/6 "2018-05-15T05:57:47Z")

</div>

Note that the broadcasting solution above makes a copy even in cases where the data is already of single precision.  
Another solution is to convert to an `AbstractArray`

```julia
julia> x = sprand(2, 2, 0.5)
2×2 SparseMatrixCSC{Float64,Int64} with 1 stored entry:
  [1, 2] = 0.897311

julia> convert(AbstractArray{Float32}, x)
2×2 SparseMatrixCSC{Float32,Int64} with 1 stored entry:
  [1, 2] = 0.897311

julia> x = Diagonal([1., 2])
2×2 Diagonal{Float64}:
 1.0 ⋅ 
  ⋅ 2.0

julia> convert(AbstractArray{Float32}, x)
2×2 Diagonal{Float32}:
 1.0 ⋅ 
  ⋅ 2.0

```

---

<div class="post-metadata">

### Author: ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)
#### Post date: [May 15, 2018, 12:48pm UTC](https://discourse.julialang.org/t/convert-arrays-to-single-precision/10903/7 "2018-05-15T12:48:02Z")

</div>

Even better!
