1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#light

// some possibilities
// (1) use Array.init

let project1 j a = Array.init (Array2.length1 a) (fun i -> a.[i,j])
let project2 i a = Array.init (Array2.length2 a) (fun j -> a.[i,j])

// (2) use a sequence expression / list comprehension
let project1' j a = [|for i in 0 .. Array2.length1 a-1 -> a.[i,j]|]
let project2' i a = [|for j in 0 .. Array2.length2 a-1 -> a.[i,j]|]

// note that it doesn't really seem worth factoring out the common pattern but it's possible

let project funLen funVarFix fixIndex a = 
    Array.init (funLen a) (fun varIndex -> let i,j = funVarFix varIndex fixIndex in Array2.get a i j)

// val project : ('a [,] -> int) -> (int -> 'b -> int * int) -> 'b -> 'a [,] -> 'a array

let project1'' j a = project Array2.length1 (fun i j -> i,j) j a
let project2'' i a = project Array2.length2 (fun j i -> i,j) i a
let a = Array2.init 3 5 (fun i j -> i*i+j)

// val it : int [,] = [|[|0; 1; 2; 3; 4|]; [|1; 2; 3; 4; 5|]; [|4; 5; 6; 7; 8|]|]

project1 2 a
project1' 2 a
project1'' 2 a

// val it : int array = [|2; 3; 6|]

project2 2 a
project2' 2 a
project2'' 2 a

// val it : int array = [|4; 5; 6; 7; 8|]
By on 4/11/2008 9:24 PM ()

Thanks for the responses!

The implementation is not really a problem (maybe I should not have posted to the how-to forum :-) but I was looking for input on the interface and naming.

The getRow/getColumn (like Matrix) would be the most perspicuous names, but are inconsistent with length1 and length2 in Array2 (which could have been named rows and columns). Hence why I thought project1 and project2 would be better.

Another question is whether to return an array or a sequence. Ordinarily a sequence will be fine for our usage, but everything in Array2 returns an array.

Any further thoughts?

Thanks
Howard

By on 4/14/2008 7:54 AM ()

It's true that the names project1 and project2 are more consistent with the Array class. However, there's something slightly confusing here: whether the 1 in project1 refers to the fixed or variable dimension. I guess we would expect the 1 to mean we are projecting onto the first dimension, meaning that the first dimension is the variable one, but there's room for confusion.

It seems easy enough to return an array, so why not?

By on 4/14/2008 2:16 PM ()

It's true that the names project1 and project2 are more consistent with the Array class. However, there's something slightly confusing here: whether the 1 in project1 refers to the fixed or variable dimension. I guess we would expect the 1 to mean we are projecting onto the first dimension, meaning that the first dimension is the variable one, but there's room for confusion.

I agree. I think we'll go with the getColumn/getRow names.

It seems easy enough to return an array, so why not?

Performance, mainly. It is not clear to me that an array will definitely be slower, but it will potentially be allocating a big chunk of memory that may be unnecessary (if, for example, I just want to traverse the sequence and output to the screen, or something).

By on 4/17/2008 9:21 AM ()

You can take a look at the Math.Matrix.getRow and Math.Matrix.getColumn functions that do similar operations on matrices.

By on 4/11/2008 4:38 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper