# Comparing 2 Array2Ds

**URL:** https://forums.fsharp.org/t/comparing-2-array2ds/2240
**Category:** General
**Created:** [May 8, 2022, 2:07am UTC](https://forums.fsharp.org/t/comparing-2-array2ds/2240 "2022-05-08T02:07:22Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![jonoke](https://avatars.discourse-cdn.com/v4/letter/j/a8b319/32.png) [@jonoke](https://forums.fsharp.org/u/jonoke)
#### Post date: [May 8, 2022, 2:07am UTC](https://forums.fsharp.org/t/comparing-2-array2ds/2240/1 "2022-05-08T02:07:22Z")

</div>

Hi,

the problem is to compare two Array2D arrays element by element stopping at the first element that fails.  
I’ve spend a long time googling … the closest I’ve come to so far is this which won’t compile:

```auto
let isUneq a b =
    abs(a - b) > 0.1

let m = array2D [[1.1;2.0;3.0;4.0];[5.0;6.0;7.0;8.0]]
let n = array2D [[1.1;8.0;7.0;6.0];[5.0;4.0;3.0;2.0]]

let sm = seq { for x in m do yield x }
let sn = seq { for x in n do yield x }
let mm = Seq.forall2 isUneq sm sn
printf "mm is %A\n" mm

```

… which gives this compile error …

```auto
Program.fs(9,32): error FS0001: The type 'obj' does not support the operator '-'
[test1/test1.fsproj]

```

so how do I create a sequence of the appropriate type (seq \<float\>) ??

If there is better solution to the problem - I’d love to know.

Jonathan.

---

<div class="post-metadata">

### Author: ![Prash](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/prash/32/815_2.png) [@Prash](https://forums.fsharp.org/u/Prash)
#### Post date: [May 9, 2022, 11:36am UTC](https://forums.fsharp.org/t/comparing-2-array2ds/2240/2 "2022-05-09T11:36:25Z")

</div>

The simplest way to unblock you with minimal code changes is this:

```nohighlight
let sm = seq { for x in m do unbox<float> x }
let sn = seq { for x in n do unbox<float> x }

```

This uses `unbox` to do an unsafe cast to `float`. If you try to cast to another type and evaluate the sequence there will be a runtime exception.

The problem is that `Array2D` isn’t really designed for use as an enumerable so it yields `obj` by default, which is basically a value with no type that the compiler can’t do anything useful with.

There isn’t a built-in `Array2D` function to make this easier unfortunately. To do it in a more type-safe way requires some boilerplate code to access the Array2D items by index. See here for an example: [F# converting Array2D to array of arrays - Stack Overflow](https://stackoverflow.com/questions/37842353/f-converting-array2d-to-array-of-arrays)

You could add a helper function to convert an Array2D to a flat sequence and you would end up with this:

```nohighlight
module Array2D =
    let toSeq arr = seq {
        for x in Array2D.base1 arr .. Array2D.length1 arr - 1 do
            for y in Array2D.base2 arr .. Array2D.length2 arr - 1 do
                yield arr.[x, y]
    }

let isUneq a b =
    abs(a - b) > 0.1

let m = array2D [[1.1;2.0;3.0;4.0];[5.0;6.0;7.0;8.0]]
let n = array2D [[1.1;8.0;7.0;6.0];[5.0;4.0;3.0;2.0]]

let mm = Seq.forall2 isUneq (Array2D.toSeq m) (Array2D.toSeq n)
printf "mm is %A\n" mm

```
