Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #48681

Re: Mandelbrot Set

From "WJ" <w_a_x_man@yahoo.com>
Newsgroups comp.lang.forth
Subject Re: Mandelbrot Set
Date 2016-07-02 06:07 +0000
Organization A noiseless patient Spider
Message-ID <nl7lm4$grf$1@dont-email.me> (permalink)
References <4fcaecbc-53bc-4c5b-8d12-f24b0755a557@googlegroups.com>

Show all headers | View raw


hughaguilar wrote:

> \ Mandelbrot Set --- ANS-Forth --- written by Hugh Aguilar
> \ from Paul Rubin's C code in this thread: 
> \ https://groups.google.com/forum/#!topic/comp.lang.lisp/8XigMh1ujjM
> 
> \ ANS-Forth fails to support locals, so I had to use global variables.
> \ ANS-Forth also fails to provide F>R FR@ FR> which could have also helped.
> 
> marker Mandelbrot.4th
> 
> : f+! ( adr -- )                            \ float: n --
>     dup f@ f+  f! ;
>     
> : csqr ( -- )                               \ float: a ai -- product producti
>     fover fdup f*  fover fdup f*  f-        \ float: -- a ai a*a-ai*ai
>     frot frot                               \ float: -- a*a-ai*ai a ai
>     f*  fdup f+ ;                           \ float: -- a*a-ai*ai a*ai+a*ai
>     
> : cabs ( -- )                               \ float: a ai -- distance
>     fdup f*  fswap 
>     fdup f*  f+
>     fsqrt ;
>     
> : s>f ( n -- )                              \ float: -- n
>     s>d  d>f ;
>     
> fvariable cc
> fvariable cci
> : <mandelbrot-iteration> ( -- count )       \ float: c cci -- z zi
>     fover cc f!  fdup cci f!                \ float: -- z zi    \ Z starts as C
>                                                                   not as origin
>     1 begin
>         csqr 
>         fswap cc f@ f+  fswap cci f@ f+
>         1+          
>         dup 1000 >= if                      exit then
>         fover fover cabs  100.0e f> if      exit then
>         again ;
>         
> : mandelbrot-iteration ( -- count )
>     <mandelbrot-iteration>
>     fdrop fdrop ;                           \ discard z zi
> 
> fvariable dx    
> fvariable dy
> fvariable x
> fvariable y
> fvariable x1
> fvariable y1
> variable nx
> variable ny 
> variable sum   
> : mandelbrot-box ( nx ny -- sum )           \ float: x1 x2 y1 y2 -- 
>     ny !  nx !  0 sum !
>     fover f-  ny @ s>f f/  dy f!            \ float: -- x1 x2 y1 
>     y1 f!                                   \ float: -- x1 x2
>     fover f-  nx @ s>f f/  dx f!            \ float: -- x1
>     x1 f!                                   \ float: --
>     x1 f@ x f!  0 begin  y1 f@ y f!  0 begin
>         x f@ y f@  mandelbrot-iteration sum +!
>         dup ny @ < while  1+  dy f@ y f+!  repeat drop 
>         dup nx @ < while  1+  dx f@ x f+!  repeat drop
>     sum @ ;
>     
> : test ( -- )    
>     256 256  0.0e 1.0e 0.0e 1.0e  mandelbrot-box
>     \ 2 2  -2.0e 1.0e -1.0e 1.0e  mandelbrot-box
>     cr ." sum = "  u. ;
> 
> \ The TIME-TEST function is VFX only ---
>  comment this out if you are using any other ANS-Forth compiler.
> : time-test ( -- )  ticks  test  ticks swap -  cr ." milliseconds: " . ;


Paul Rubin's function seems to have off-by-one errors:

> const int mandelbrot_iteration(const double complex c)
> {
>   int k = 1;
>   double complex z = c;
>   do {
>     z = z*z + c;
>   } while (k++ < 1000 && cabs(z) <= 100.0) ;
>   return k;
> }

The minimal number that can be returned is 2;
the maximal is 1001.  I believe those numbers
should be 1 and 1000.

Also, the correct way is to compare the magnitude of
the complex number to 2.0, not 100.0.

Also, z should not be initialized to c; it should start
at the origin.

The following code doesn't use a complex-number type;
just floats.

OCaml:

let max_iteration = 1000;;

(* Based on the wikipedia pseudocode *)
let count_iterations x0 y0 =
  let rec loop iteration x y =
    let xx = x *. x  and  yy = y *. y in
      (* Exit when magnitude of complex number >= 2.0. *)
      if (xx +. yy) < 4.0  &&  iteration < max_iteration  then
        loop (iteration+1) (xx -. yy +. x0) (2.0 *. x *. y +. y0)
      else
        iteration
  in loop 0 0.0 0.0 ;;

let mandelbrot_box x1 x2 y1 y2 nx ny =
  let dx = (x2 -. x1) /. float nx  and
      dy = (y2 -. y1) /. float ny  and
      sum = ref 0  in
    for kx = 0 to nx do
      let x = dx *. float kx +. x1 in
        for ky = 0 to ny do
          let y = dy *. float ky +. y1 in
            sum := !sum + count_iterations x y
        done
    done ;
    !sum ;;

Printf.printf "Minimal count_iterations: %d\n" (count_iterations 988.0 988.0);;
Printf.printf "Maximal count_iterations: %d\n" (count_iterations 0.0 0.0) ;;

for _ = 1 to 9 do
  let t = Sys.time () in
    print_int (mandelbrot_box 0.0 1.0 0.0 1.0 256 256) ;
    Printf.printf " %.3f\n" ((Sys.time ()) -. t)
done



On an old WinXP laptop:

Minimal count_iterations: 1
Maximal count_iterations: 1000
13808106 0.313
13808106 0.312
13808106 0.328
13808106 0.313
13808106 0.312
13808106 0.313
13808106 0.312
13808106 0.313
13808106 0.312

Back to comp.lang.forth | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Mandelbrot Set hughaguilar96@gmail.com - 2016-07-01 11:52 -0700
  Re: Mandelbrot Set hughaguilar96@gmail.com - 2016-07-01 12:35 -0700
    Re: Mandelbrot Set Pablo Hugo Reda <pabloreda@gmail.com> - 2016-07-01 12:40 -0700
      Re: Mandelbrot Set hughaguilar96@gmail.com - 2016-07-01 12:57 -0700
        Re: Mandelbrot Set Pablo Hugo Reda <pabloreda@gmail.com> - 2016-07-01 13:22 -0700
      Re: Mandelbrot Set josv@planet.nl - 2016-07-01 23:58 -0700
    Re: Mandelbrot Set hughaguilar96@gmail.com - 2016-07-03 10:17 -0700
      bug in VFX disassembler? hughaguilar96@gmail.com - 2016-07-04 17:51 -0700
        Re: bug in VFX disassembler? Cecil Bayona <cbayona@cbayona.com> - 2016-07-04 23:25 -0500
          Re: bug in VFX disassembler? hughaguilar96@gmail.com - 2016-07-04 22:48 -0700
            Re: bug in VFX disassembler? rickman <gnuarm@gmail.com> - 2016-07-05 02:02 -0400
              Re: bug in VFX disassembler? lawrencedo99@gmail.com - 2016-07-06 00:46 -0700
                Re: bug in VFX disassembler? Ron Aaron <rambamist@gmail.com> - 2016-07-06 10:48 +0300
                Re: bug in VFX disassembler? lawrencedo99@gmail.com - 2016-07-06 19:56 -0700
                Re: bug in VFX disassembler? rickman <gnuarm@gmail.com> - 2016-07-06 11:33 -0400
                Re: bug in VFX disassembler? lawrencedo99@gmail.com - 2016-07-06 19:57 -0700
                Re: bug in VFX disassembler? rickman <gnuarm@gmail.com> - 2016-07-07 00:45 -0400
                Re: bug in VFX disassembler? lawrencedo99@gmail.com - 2016-07-06 23:42 -0700
                Re: bug in VFX disassembler? rickman <gnuarm@gmail.com> - 2016-07-07 03:14 -0400
                Re: bug in VFX disassembler? Cecil Bayona <cbayona@cbayona.com> - 2016-07-06 22:32 -0500
                Re: bug in VFX disassembler? lawrencedo99@gmail.com - 2016-07-06 21:08 -0700
                Re: bug in VFX disassembler? hughaguilar96@gmail.com - 2016-07-07 00:22 -0700
                Re: bug in VFX disassembler? Cecil Bayona <cbayona@cbayona.com> - 2016-07-07 02:34 -0500
                Re: bug in VFX disassembler? hughaguilar96@gmail.com - 2016-07-07 00:40 -0700
                Re: bug in VFX disassembler? Julian Fondren <julian.fondren@gmail.com> - 2016-07-07 01:23 -0700
                Re: bug in VFX disassembler? rickman <gnuarm@gmail.com> - 2016-07-07 03:39 -0400
                Re: bug in VFX disassembler? hughaguilar96@gmail.com - 2016-07-07 00:50 -0700
                Re: bug in VFX disassembler? rickman <gnuarm@gmail.com> - 2016-07-07 10:27 -0400
          Re: bug in VFX disassembler? "HAA" <someone@microsoft.com> - 2016-07-07 14:38 +1000
        Re: bug in VFX disassembler? "HAA" <someone@microsoft.com> - 2016-07-07 17:01 +1000
          Re: bug in VFX disassembler? hughaguilar96@gmail.com - 2016-07-07 00:04 -0700
            Re: bug in VFX disassembler? "HAA" <someone@microsoft.com> - 2016-07-07 17:39 +1000
  Re: Mandelbrot Set "WJ" <w_a_x_man@yahoo.com> - 2016-07-02 06:07 +0000
    Re: Mandelbrot Set mhx@iae.nl - 2016-07-02 00:12 -0700
    Re: Mandelbrot Set Paul Rubin <no.email@nospam.invalid> - 2016-07-02 00:58 -0700
  Re: Mandelbrot Set humptydumpty <ouatubi@gmail.com> - 2016-07-20 03:46 -0700
    Re: Mandelbrot Set hughaguilar96@gmail.com - 2016-07-20 07:22 -0700
      Re: Mandelbrot Set Coos Haak <chforth@hccnet.nl> - 2016-07-20 17:02 +0200
        Re: Mandelbrot Set hughaguilar96@gmail.com - 2016-07-20 08:20 -0700
          Re: Mandelbrot Set humptydumpty <ouatubi@gmail.com> - 2016-07-20 08:58 -0700
            Re: Mandelbrot Set humptydumpty <ouatubi@gmail.com> - 2016-07-20 09:02 -0700
            Re: Mandelbrot Set hughaguilar96@gmail.com - 2016-07-20 09:35 -0700
            Re: Mandelbrot Set anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-07-21 13:18 +0000
              Re: Mandelbrot Set humptydumpty <ouatubi@gmail.com> - 2016-07-21 09:04 -0700

csiph-web