Try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 

#light

#r "FSharp.PowerPack.dll"

let x = vector [ 1.0; 2.0; 3.0]
let y = matrix [x; x; x] //a 3x3 matrix with _rows_ x 

let colmatrix cols = 
    let cols =  cols |> Array.of_seq |> Array.map Array.of_seq
    if cols.Length = 0 then invalid_arg "empty matrix"
    let nrows = cols.[0].Length 
    if cols |> Array.exists (fun c -> c.Length <> nrows) then invalid_arg "colmatrix: irregular column sizes"
    matrix  [| for i in 0 .. cols.[0].Length - 1 do
                  yield [| for j in 0 .. cols.Length - 1 do
                              yield cols.[j].[i] |] |]

               
let z = colmatrix [x; x; x] //a 3x3 matrix with columns x 

By on 9/11/2008 3:05 AM ()

Wy not use Math.Matrix.transpose y ?

> y;;
val it : Math.matrix = matrix [[1.0; 2.0; 3.0];
[1.0; 2.0; 3.0];
[1.0; 2.0; 3.0];]
> Math.Matrix.transpose y;;
val it : Math.matrix = matrix [[1.0; 1.0; 1.0];
[2.0; 2.0; 2.0];
[3.0; 3.0; 3.0];]

Myselft I study F# for move completely to matlab,
but (for me) not easy - I am an old «imperative» programmer: Fortran, VB, Matlab etc.

By on 9/11/2008 3:45 AM ()

Hah! Good point. Silly me :-)

By on 9/11/2008 10:08 AM ()

vector and matrix operate with float value :-(

not possible wrtie: let x = vector [ 1; 2; 3] ;;

In Fortran array of integer (int32), or real (float32) or double (float) is possible
Idem with Matlab, but the default is double (float),

With F#,
let A = Array.create 3 1 ;;
let B = Array.create 3 2 ;;

but
let C = A + B ;;
generate error
stdin(56,13): error FS0001: The type 'int []' does not support any operators named '+'

Nothing is pefect :-)

By on 9/12/2008 3:01 AM ()

With F#,
let A = Array.create 3 1 ;;
let B = Array.create 3 2 ;;

but
let C = A + B ;;
generate error
stdin(56,13): error FS0001: The type 'int []' does not support any operators named '+'

Nothing is pefect :-)

Well post is old, but I could not hold myself from posting :-) ->
add custom operator with

let (+.) A B = [for (a,b) in Seq.zip A B -> a+b];;

Now you can do-

let C = A +. B;;

Cheers

By on 12/7/2010 1:49 PM ()

In fact,

type matrix = Matrix<float>
type vector = Vector<float>
type rowvec = RowVector<float>

(réf.: matrix.fs, directory: C:\Program Files\FSharp-1.9.6.0\lib\FSharp.PowerPack\math)

and vector is a Vector of float ...

By on 9/12/2008 4:51 PM ()

ok, are there inbuilt functions to take kronecker product of the matrices and concatenate the matrices????

I have defined kronecker product in the following way, which seems to be working fine...

let kron (A :Math.matrix, B :Math.matrix) =
let (arw,acl) = A.Dimensions
let (brw,bcl) = B.Dimensions
let mutable AB = Math.Matrix.create (arw*brw) (acl*bcl) 1.0
for i = 0 to arw-1 do
for j = 0 to acl-1 do
for k = 0 to brw-1 do
for l = 0 to bcl-1 do
AB.[(i*brw)+k,(j*bcl)+l] <- A.[i,j]*B.[k,l]
AB;;

By on 12/28/2008 9:25 AM ()
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