The module "mloop" of the Mastrave modelling library


Daniele de Rigo


Copyright and license notice of the function mloop



Copyright © 2009,2010,2011,2012,2013,2014 Daniele de Rigo

The file mloop.m is part of Mastrave.

Mastrave is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

Mastrave is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with Mastrave. If not, see

Function declaration



 [transformed_obj, ...] = mloop( until_, func, obj, ... )




Module for supporting tail-recursive application of a generic function handled by func to a object obj (or more objects passed following the first object obj ). The tail-recursion is implemented as a loop which is intended to address dynamic assignments so that the only possible parallelization (planned) could rely on the definition of the func handle, especially when obj is composed by a large amount of elements. The loop ends according to the until_ argument.

Input arguments



 until_             ::scalar_numel|function_handle::
                    Criterion for terminating the loop.  It may be a 
                    scalar nonnegative integer, meaning that the loop
                    is to be reapeted  until_  times.
                    It may also be a function handle.  If so, it is
                    expected to be a function whose number of input 
                    arguments is the same as the number of objects
                    passed to  @mloop .  The function must return as 
                    output a logical scalar.

 func               ::function_handle::
                    Handle of the function to be applied to  obj  and 
                    to the possible subsequently passed objects.  It
                    is expected to be a function whose number of input 
                    and output arguments is the same and is compatible
                    with the number of objects passed to  @mloop .

 obj                ::generic::
                    Object on which to apply the function handled by
                     func  until the  until_  condition terminates the 
                    More than one object can be passed with an 
                    arbitrary number of optional arguments.

Example of usage



   % Basic usage
   c = mloop( 1, @cumsum, [1 0 0 0 0 0 0 0 0] )
   c = mloop( 2, @cumsum, [1 0 0 0 0 0 0 0 0] )
   c = mloop( 3, @cumsum, [1 0 0 0 0 0 0 0 0] )

   % Fibonacci numbers  (sequence A000045 in OEIS:
   mloop( 10, @(x)[ x sum(x(end+[-1:0])) ], [0 1] )
   % Lucas numbers      (sequence A000032 in OEIS:
   mloop( 10, @(x)[ x sum(x(end+[-1:0])) ], [2 1] )
   % Tribonacci numbers (sequence A000073 in OEIS:
   mloop( 10, @(x)[ x sum(x(end+[-2:0])) ], [0 0 1] )

   % Implementing a cellular automata
   colormap( 'default' );
   rule  = @(x)[ x; mod( filter2( [1 1 1], x(end,:) ), 2 ) ];
   C     = mloop( 300, rule, rand(1,300)>0.5  );
   imagesc( C ); pause(3)
   C     = mloop( 300, rule, rand(1,300)>0.05 );
   imagesc( C ); pause(3)
   C     = mloop( 300, rule, rand(1,300)>0.99 );
   imagesc( C )

   % Implementing a Julia set
   [x,y] = meshgrid( linspace(-1,1,400)*pi/2 ); 
   z     = x + 1i*y;
   function [z,z0] = julia(z,z0)
      z  = z.^2 + z0;
      imagesc( 1-atan(abs(z))/pi*2 ); axis equal; pause(0.1);
   mloop( 40, @julia, z, 0.37*(1+1i)  );
   mloop( 40, @julia, z, .65i         );
   mloop( 40, @julia, z, -0.8 -0.175i );
   mloop( 50, @julia, z, -0.4 +0.6i   );
   mloop( 50, @julia, z, 0.285 +0.01i );

   % A more complex example:
   % Spatially correlated noise and downsampling with binary dithering
   [D, d]         = deal( 25, 13 ) % downsampling diameter and radius
   generator_func = @(M)interp2(M,1,'spline')+.5*rand(size(M)*2-1)
   get_corrnoise  = @(n)mloop(8,generator_func,rand(n))
   norm_func      = @(M)(M-min(M(:)))/range(M(:));
   M              = norm_func( get_corrnoise(5) );
   colormap( 1 - colormap( 'gray' ) );
   subplot( 1, 3, 1 ); imagesc( M );
   Mdown          = mblk_fun( M, @(M)isfinite(M)*mean(M(:)), D ); 
   subplot( 1, 3, 2 ); imagesc( Mdown );
   Mdn            = norm_func(  Mdown ); 
   z = zeros(size(M)); z(d:D:end,d:D:end) = 1;
   % see also mfreq_matrix
   Z = double( mloop(                                                 ...
      d, @(Z,v)deal(Z|filter2(ones(3),Mdn.*Z>v),(v^.5+1/d)^2), z, 1/D ...
   subplot( 1, 3, 3 ); imagesc( Z );

See also:
   mstream, mblk_fun, func_let, mfreq_matrix

   tail-recursion, looping, anonymous-function

Version: 0.3.9




The Mastrave modelling library is committed to provide reusable and general - but also robust and scalable - modules for research modellers dealing with computational science.  You can help the Mastrave project by providing feedbacks on unexpected behaviours of this module.  Despite all efforts, all of us - either developers or users - (should) know that errors are unavoidable.  However, the free software paradigm successfully highlights that scientific knowledge freedom also implies an impressive opportunity for collectively evolve the tools and ideas upon which our daily work is based.  Reporting a problem that you found using Mastrave may help the developer team to find a possible bug.  Please, be aware that Mastrave is entirely based on voluntary efforts: in order for your help to be as effective as possible, please read carefully the section on reporting problems.  Thank you for your collaboration.

Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Daniele de Rigo

This page is licensed under a Creative Commons Attribution-NoDerivs 3.0 Italy License.

This document is also part of the book:
de Rigo, D. (2012). Semantic Array Programming with Mastrave - Introduction to Semantic Computational Modelling.

Valid XHTML 1.0 Transitional