* [Caml-list] laconical input from a file for arrays and/or matrices
@ 2004-02-02 10:42 Khamenia, Valery
2004-02-02 10:57 ` samsaga2
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Khamenia, Valery @ 2004-02-02 10:42 UTC (permalink / raw)
To: 'caml-list@inria.fr'
Hi OCaMLers,
here goes my usual way to input ASCII matrices from file in C++:
// ------- start of fragment -----------
#include <iostream>
#include <vector>
using namespace std;
void input_matrix(vector<vector<double > >&vec) {
int m, n;
cin >> m;
cin >> n;
vec.resize (m);
for( int i = 0; i<m; i++)
vec[i].resize(n);
for( int i = 0; i<m; i++)
for( int j = 0; j<n; j++)
cin >> vec[i][j];
}
// -------- end of fragment -------------
then for matrix :
3
3
1 2 3
4 5 6
7 8 9
stored in ASCII text file mymatrix.dat
i just run my program:
% myprogram < mymatrix.dat
and it is done.
Thus, the few lines of code and i can apply all math I need.
Is there any similar ascetic way in OCaML for
doing the same?
Thanks a priori,
kind regards,
Valery A.Khamenya
---------------------------------------------------------------------------
Bioinformatics Department
BioVisioN AG, Hannover
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] laconical input from a file for arrays and/or matrices
2004-02-02 10:42 [Caml-list] laconical input from a file for arrays and/or matrices Khamenia, Valery
@ 2004-02-02 10:57 ` samsaga2
2004-02-02 14:20 ` Virgile Prevosto
2004-02-02 14:36 ` Richard Jones
2004-02-02 15:54 ` Issac Trotts
2 siblings, 1 reply; 5+ messages in thread
From: samsaga2 @ 2004-02-02 10:57 UTC (permalink / raw)
To: caml-list
let output_matirx m = output_value stdout m
let input_matrix () = input_value stdin
But no compatible with c++ files :-P
Khamenia, Valery wrote:
>Hi OCaMLers,
>
> here goes my usual way to input ASCII matrices from file in C++:
>
>// ------- start of fragment -----------
>#include <iostream>
>#include <vector>
>using namespace std;
>
>void input_matrix(vector<vector<double > >&vec) {
> int m, n;
> cin >> m;
> cin >> n;
> vec.resize (m);
> for( int i = 0; i<m; i++)
> vec[i].resize(n);
> for( int i = 0; i<m; i++)
> for( int j = 0; j<n; j++)
> cin >> vec[i][j];
>}
>// -------- end of fragment -------------
>
>then for matrix :
>
> 3
> 3
> 1 2 3
> 4 5 6
> 7 8 9
>
>stored in ASCII text file mymatrix.dat
>
>i just run my program:
>
> % myprogram < mymatrix.dat
>
>and it is done.
>
>Thus, the few lines of code and i can apply all math I need.
>
>Is there any similar ascetic way in OCaML for
>doing the same?
>
>Thanks a priori,
>kind regards,
>Valery A.Khamenya
>
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] laconical input from a file for arrays and/or matrices
2004-02-02 10:57 ` samsaga2
@ 2004-02-02 14:20 ` Virgile Prevosto
0 siblings, 0 replies; 5+ messages in thread
From: Virgile Prevosto @ 2004-02-02 14:20 UTC (permalink / raw)
To: caml-list
:
Le lundi 02 février, à 11h57 +0100,
samsaga2 a écrit:
> let output_matirx m = output_value stdout m
> let input_matrix () = input_value stdin
>
> But no compatible with c++ files :-P
>
> Khamenia, Valery wrote:
> > here goes my usual way to input ASCII matrices from file in C++:
# let x = Array.make_matrix 1 1 3 in output_value stdout x;;
¦¾C
- : unit = ()
I doubt that we can speak of ASCII matrices in that case ;-)
Anyway, it's not so difficult to obtain a code fragment close to the
C++ one, thanks to the Array and Scanf modules:
(* CAML version. *)
let input_matrix () =
let m = read_int () in
let n = read_int () in
let matrix = Array.make_matrix m n 0. in
for i = 0 to (m - 1) do
for j = 0 to (n -1) do
matrix.(i).(j) <- Scanf.scanf "%f " (fun x -> x)
done
done;
matrix;;
val input_matrix : unit -> float array array = <fun>
Of course, you'll have to adapt the formatting instruction of scanf to
your datas (the main problem here is that scanf expects to fins a blank
after each entry, which means that your file must end with a newline or
a space).
--
E tutto per oggi, a la prossima volta
Virgile
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] laconical input from a file for arrays and/or matrices
2004-02-02 10:42 [Caml-list] laconical input from a file for arrays and/or matrices Khamenia, Valery
2004-02-02 10:57 ` samsaga2
@ 2004-02-02 14:36 ` Richard Jones
2004-02-02 15:54 ` Issac Trotts
2 siblings, 0 replies; 5+ messages in thread
From: Richard Jones @ 2004-02-02 14:36 UTC (permalink / raw)
To: Khamenia, Valery; +Cc: 'caml-list@inria.fr'
Probably not quite what you want, but I have a library for reading and
writing comma-separated values (CSV) files here:
http://www.merjis.com/developers/csv/
http://www.merjis.com/developers/csv/ocaml-csv-1.0.1.tar.gz
Rich.
--
Richard Jones. http://www.annexia.org/ http://www.j-london.com/
Merjis Ltd. http://www.merjis.com/ - improving website return on investment
'There is a joke about American engineers and French engineers. The
American team brings a prototype to the French team. The French team's
response is: "Well, it works fine in practice; but how will it hold up
in theory?"'
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] laconical input from a file for arrays and/or matrices
2004-02-02 10:42 [Caml-list] laconical input from a file for arrays and/or matrices Khamenia, Valery
2004-02-02 10:57 ` samsaga2
2004-02-02 14:36 ` Richard Jones
@ 2004-02-02 15:54 ` Issac Trotts
2 siblings, 0 replies; 5+ messages in thread
From: Issac Trotts @ 2004-02-02 15:54 UTC (permalink / raw)
To: caml-list
On Mon, Feb 02, 2004 at 11:42:18AM +0100, Khamenia, Valery wrote:
> Hi OCaMLers,
>
> here goes my usual way to input ASCII matrices from file in C++:
>
> // ------- start of fragment -----------
> #include <iostream>
> #include <vector>
> using namespace std;
>
> void input_matrix(vector<vector<double > >&vec) {
> int m, n;
> cin >> m;
> cin >> n;
> vec.resize (m);
> for( int i = 0; i<m; i++)
> vec[i].resize(n);
> for( int i = 0; i<m; i++)
> for( int j = 0; j<n; j++)
> cin >> vec[i][j];
> }
> // -------- end of fragment -------------
>
> then for matrix :
>
> 3
> 3
> 1 2 3
> 4 5 6
> 7 8 9
>
> stored in ASCII text file mymatrix.dat
>
> i just run my program:
>
> % myprogram < mymatrix.dat
>
> and it is done.
>
> Thus, the few lines of code and i can apply all math I need.
Here's a way to do it with camlp4. You get OCaml-style comments
for free from Genlex this way:
#load "camlp4o.cma";;
open Genlex;;
let input_matrix channel =
let read_number = parser
[< 'Int i >] -> float i
| [< 'Float f >] -> f in
let rec read_nums n =
if n = 0
then parser [< >] -> []
else parser [< head=read_number; tail=read_nums (n-1) >] -> head::tail in
let rec read_matrix_data m n =
if m = 0
then parser [< >] -> []
else parser
[< row = read_nums n; rest = read_matrix_data (m-1) n >] ->
(Array.of_list row) :: rest in
let read_matrix = parser
[< 'Int m; 'Int n; rest >] ->
let rows = read_matrix_data m n rest in
Array.of_list rows in
let charstream = Stream.of_channel channel in
let lexer = Genlex.make_lexer [] charstream in
read_matrix lexer;;
input_matrix stdin;;
--
Issac Trotts
http://redwood.ucdavis.edu/~issac
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2004-02-02 15:54 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-02-02 10:42 [Caml-list] laconical input from a file for arrays and/or matrices Khamenia, Valery
2004-02-02 10:57 ` samsaga2
2004-02-02 14:20 ` Virgile Prevosto
2004-02-02 14:36 ` Richard Jones
2004-02-02 15:54 ` Issac Trotts
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox