gsl-ocaml / lib / matrix_complex.ml

 ```Markus Mottl c8ec844 2012-07-03 Markus Mottl 0540f0f 2012-09-16 Markus Mottl a685ddc 2012-07-05 Markus Mottl b6d7484 2012-07-03 ``` ``` 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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210``` ```(* gsl-ocaml - OCaml interface to GSL *) (* Copyright (©) 2002-2012 - Olivier Andrieu *) (* Distributed under the terms of the GPL version 3 *) open Bigarray open Gsl_complex type complex_mat_bigarr = (Complex.t, Bigarray.complex64_elt, Bigarray.c_layout) Bigarray.Array2.t type matrix = complex_mat_bigarr let create ?init dimx dimy = let barr = Array2.create complex64 c_layout dimx dimy in begin match init with | None -> () | Some x -> Array2.fill barr x end ; barr let dims mat = (Array2.dim1 mat, Array2.dim2 mat) let of_array arr dim1 dim2 = let mat = create dim1 dim2 in for i=0 to pred dim1 do for j=0 to pred dim2 do mat.{i,j} <- arr.(dim2*i+j) done done ; mat let of_complex_array arr dim1 dim2 = let mat = create dim1 dim2 in for i=0 to pred dim1 do for j=0 to pred dim2 do let k = 2 * (dim2*i+j) in mat.{i,j} <- complex ~re:arr.(k) ~im:arr.(k+1) done done ; mat let of_arrays arr = Array2.of_array complex64 c_layout arr let to_array (mat : matrix) = let d1 = Array2.dim1 mat in let d2 = Array2.dim2 mat in Array.init (d1*d2) (fun i -> mat.{i/d2, i mod d2}) let to_complex_array (mat : matrix) = let d1 = Array2.dim1 mat in let d2 = Array2.dim2 mat in let arr = Array.create (2*d1*d2) 0. in for i=0 to pred (d1*d2) do let { re = re; im = im } = mat.{i/d2, i mod d2} in arr.(2*i) <- re ; arr.(2*i+1) <- im done ; arr let to_arrays (mat : matrix) = let d1 = Array2.dim1 mat in let d2 = Array2.dim2 mat in let a = Array.init d1 (fun _ -> Array.make d2 Complex.zero) in for i=0 to pred d1 do for j=0 to pred d2 do a.(i).(j) <- mat.{i,j} done done ; a let get (m : matrix) i j = Array2.get m i j let set (m : matrix) i j x = Array2.set m i j x let set_all = Array2.fill let set_zero m = set_all m Complex.zero let set_id m = set_zero m ; for i=0 to pred (min (Array2.dim1 m) (Array2.dim2 m)) do set m i i Complex.one done let memcpy ~src ~dst = Array2.blit src dst let copy m = let m' = create (Array2.dim1 m) (Array2.dim2 m) in Array2.blit m m' ; m' let row = Array2.slice_left external add : matrix -> matrix -> unit = "ml_gsl_matrix_complex_add" external sub : matrix -> matrix -> unit = "ml_gsl_matrix_complex_sub" external mul_elements : matrix -> matrix -> unit = "ml_gsl_matrix_complex_mul" external div_elements : matrix -> matrix -> unit = "ml_gsl_matrix_complex_div" external scale : matrix -> complex -> unit = "ml_gsl_matrix_complex_scale" external add_constant : matrix -> complex -> unit = "ml_gsl_matrix_complex_add_constant" external add_diagonal : matrix -> complex -> unit = "ml_gsl_matrix_complex_add_diagonal" external is_null : matrix -> bool = "ml_gsl_matrix_complex_isnull" external swap_rows : matrix -> int -> int -> unit = "ml_gsl_matrix_complex_swap_rows" external swap_columns : matrix -> int -> int -> unit = "ml_gsl_matrix_complex_swap_columns" external swap_rowcol : matrix -> int -> int -> unit = "ml_gsl_matrix_complex_swap_rowcol" external transpose : matrix -> matrix -> unit = "ml_gsl_matrix_complex_transpose_memcpy" external transpose_in_place : matrix -> unit = "ml_gsl_matrix_complex_transpose" module Single = struct type complex_float_mat_bigarr = (Complex.t, Bigarray.complex32_elt, Bigarray.c_layout) Bigarray.Array2.t type matrix = complex_float_mat_bigarr let create ?init dimx dimy = let barr = Array2.create complex32 c_layout dimx dimy in begin match init with | None -> () | Some x -> Array2.fill barr x end ; barr let dims = dims let of_array arr dim1 dim2 = let mat = create dim1 dim2 in for i=0 to pred dim1 do for j=0 to pred dim2 do mat.{i,j} <- arr.(dim2*i+j) done done ; mat let of_complex_array arr dim1 dim2 = let mat = create dim1 dim2 in for i=0 to pred dim1 do for j=0 to pred dim2 do let k = 2 * (dim2*i+j) in mat.{i,j} <- complex ~re:arr.(k) ~im:arr.(k+1) done done ; mat let of_arrays arr = Array2.of_array complex32 c_layout arr let to_array (mat : matrix) = let d1 = Array2.dim1 mat in let d2 = Array2.dim2 mat in Array.init (d1*d2) (fun i -> mat.{i/d2, i mod d2}) let to_complex_array (mat : matrix) = let d1 = Array2.dim1 mat in let d2 = Array2.dim2 mat in let arr = Array.create (2*d1*d2) 0. in for i=0 to pred (d1*d2) do let { re = re; im = im } = mat.{i/d2, i mod d2} in arr.(2*i) <- re ; arr.(2*i+1) <- im done ; arr let to_arrays (mat : matrix) = let d1 = Array2.dim1 mat in let d2 = Array2.dim2 mat in let a = Array.init d1 (fun _ -> Array.make d2 Complex.zero) in for i=0 to pred d1 do for j=0 to pred d2 do a.(i).(j) <- mat.{i,j} done done ; a let get (m : matrix) i j = Array2.get m i j let set (m : matrix) i j x = Array2.set m i j x let set_all = set_all let set_zero = set_zero let set_id m = set_zero m ; for i=0 to pred (min (Array2.dim1 m) (Array2.dim2 m)) do set m i i Complex.one done let memcpy = memcpy let copy m = let m' = create (Array2.dim1 m) (Array2.dim2 m) in Array2.blit m m' ; m' let row = row external add : matrix -> matrix -> unit = "ml_gsl_matrix_complex_float_add" external sub : matrix -> matrix -> unit = "ml_gsl_matrix_complex_float_sub" external mul_elements : matrix -> matrix -> unit = "ml_gsl_matrix_complex_float_mul" external div_elements : matrix -> matrix -> unit = "ml_gsl_matrix_complex_float_div" external scale : matrix -> complex -> unit = "ml_gsl_matrix_complex_float_scale" external add_constant : matrix -> complex -> unit = "ml_gsl_matrix_complex_float_add_constant" external add_diagonal : matrix -> complex -> unit = "ml_gsl_matrix_complex_float_add_diagonal" external is_null : matrix -> bool = "ml_gsl_matrix_complex_float_isnull" external swap_rows : matrix -> int -> int -> unit = "ml_gsl_matrix_complex_float_swap_rows" external swap_columns : matrix -> int -> int -> unit = "ml_gsl_matrix_complex_float_swap_columns" external swap_rowcol : matrix -> int -> int -> unit = "ml_gsl_matrix_complex_float_swap_rowcol" external transpose : matrix -> matrix -> unit = "ml_gsl_matrix_complex_float_transpose_memcpy" external transpose_in_place : matrix -> unit = "ml_gsl_matrix_complex_float_transpose" end ```
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.