caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Mandelbrot renderer
@ 2005-11-23  1:50 Jon Harrop
  2005-11-23  8:00 ` [Caml-list] " David Baelde
  0 siblings, 1 reply; 6+ messages in thread
From: Jon Harrop @ 2005-11-23  1:50 UTC (permalink / raw)
  To: caml-list


Following Oliver's objections regarding the lack of serious software written 
in OCaml (e.g. web servers), I have written a very serious Mandelbrot 
renderer. The program is 35 lines of OCaml and renders using OpenGL. This 
page breaks it down and describes how it works:

  http://www.ffconsultancy.com/free/fractal

I've written a simple, recursive C++ version as well. It weighs in at 45 lines 
but only 6% more bytes. If you specialise the complex-number arithmetic in 
the OCaml:

  let rec mandelbrot i cx cy zx zy =
    if i = 63 || zx *. zx +. zy *. zy > 4. then i else
      let zx = zx *. zx -. zy *. zy and zy = 2. *. zx *. zy in
      mandelbrot (i+1) cx cy (zx +. cx) (zy +. cy)

then, with only -O3, the C++ is actually significantly slower. The performance 
of the C++ improves considerably with -ffast-math so that it is slightly 
faster. The performance of the C++ can be further improved by using an 
imperative style. This is on both AMD64 and x86.

-- 
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
Objective CAML for Scientists
http://www.ffconsultancy.com/products/ocaml_for_scientists


^ permalink raw reply	[flat|nested] 6+ messages in thread
* Re: [Caml-list] Mandelbrot renderer
@ 2005-11-24  2:38 Simon
  0 siblings, 0 replies; 6+ messages in thread
From: Simon @ 2005-11-24  2:38 UTC (permalink / raw)
  To: christophe.raffalli; +Cc: caml-list


> And as I said in another post, I really would like an OCaml sudoko GENERATOR
(not a solver) on my portable phone, I almost finished all the grids I have in
the demo of a java commercial program I found on getjar.com, so this now is
urgent :-)


This won't work on your phone, but it will generate a wide range of sudoku
puzzles, all of them will most likely be near to impossible. It works by
shuffling a sudoku puzzle I took from Tuesday's paper and coded in after I
removed a few unneeded numbers, then applying a permutation to the numbers. Not
the most complex, but it should produce a very large range of puzzles, although
probably not every single possible least-complex puzzle. Yes, it's long for its
simplicity, all in imperative style and it won't work on your phone :(.
---

Random.self_init()

let m = [|
    [| 9; 4; 0; 0; 0; 7; 0; 0; 0 |];
    [| 0; 6; 0; 0; 0; 0; 0; 1; 0 |];
    [| 0; 0; 8; 0; 0; 1; 0; 5; 3 |];
    [| 3; 0; 7; 0; 0; 0; 0; 6; 0 |];
    [| 0; 0; 0; 0; 2; 0; 0; 0; 0 |];
    [| 0; 5; 0; 0; 0; 0; 0; 3; 7 |];
    [| 0; 9; 0; 8; 0; 0; 1; 0; 0 |];
    [| 0; 1; 0; 0; 0; 0; 0; 8; 0 |];
    [| 0; 0; 0; 4; 0; 0; 0; 0; 9 |]
|]

let swap_row board rowA rowB =
    let t = board.(rowA) in
    let _ = board.(rowA) <- board.(rowB) in
    board.(rowB) <- t

let swap_col board colA colB =
    for i = 0 to 8 do
        let t = board.(i).(colA) in
        let _ = board.(i).(colA) <- board.(i).(colB) in
        board.(i).(colB) <- t
    done
   
let shuffle_rows board =   
    for i = 0 to 2 do
        for j = 0 to 2 do
            swap_row board (i * 3 + j) (i * 3 + (Random.int 3))
        done
    done
       
let shuffle_cols board =   
    for i = 0 to 2 do
        for j = 0 to 2 do
            swap_col board (i * 3 + j) (i * 3 + (Random.int 3))
        done
    done
   
(* shuffle the 3x3 squares in their rows *)
let shuffle_square_rows board =
    let swap_square_row a b =
        for i = 0 to 2 do
            swap_row board (a + i) (b + i)
        done
    in
   
    for i = 0 to 2 do
        swap_square_row (i * 3) (Random.int 3 * 3)
    done

(* shuffle the 3x3 squares in their columns *)
let shuffle_square_cols board =
    let swap_square_col a b =
        for i = 0 to 2 do
            swap_col board (a + i) (b + i)
        done
    in
   
    for i = 0 to 2 do
        swap_square_col (i * 3) (Random.int 3 * 3)
    done

let shuffle board =
    shuffle_rows board;
    shuffle_cols board;
    shuffle_square_rows board;
    shuffle_square_cols board

(* swap the numbers around *)
let permutation board =
    let arr = Array.init 10 (fun i -> i) in
    (* really crappy way of making a permutation *)
    for i  = 0 to 999 do
        swap_row arr (i mod 9 + 1) (Random.int 9 + 1)
    done;
    for i = 0 to 8 do
        let row = board.(i) in
        for j = 0 to 8 do
            row.(j) <- arr.(row.(j))
        done
    done

let () =
    shuffle m;
    permutation m;
    Array.iter (fun r -> Array.iter (print_int)  r; print_newline()) m
   


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2005-11-24 14:43 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-11-23  1:50 Mandelbrot renderer Jon Harrop
2005-11-23  8:00 ` [Caml-list] " David Baelde
2005-11-23 10:10   ` Yaron Minsky
2005-11-23 14:58     ` Christophe Raffalli
2005-11-24 14:43   ` Chris Campbell
2005-11-24  2:38 Simon

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).