Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.26 KB | None | 0 0
  1. #include <math.h>
  2. #include "mex.h"
  3. void mexFunction( int nlhs, mxArray *plhs[],
  4.           int nrhs, const mxArray *prhs[] )
  5.  
  6. /* function [z,c] = mandelbrot_step(z,c,z0,k);
  7.  * Take one step of the Mandelbrot iteration.
  8.  * Complex arithmetic:
  9.  *    z = z.^2 + z0
  10.  *    c(abs(z) < 2) == k
  11.  * Real arithmetic:
  12.  *    x <-> real(z);
  13.  *    y <-> imag(z);
  14.  *    u <-> real(z0);
  15.  *    v <-> imag(z0);
  16.  *    [x,y] = [x.^2-y.^2+u, 2*x.*y+v];
  17.  *    c(x.^2+y.^2 < 4) = k;
  18.  *
  19.  *    prhs[0] = z       The input matrix of complex numbers
  20.  *    prhs[1] = c       The input matrix of iterations / count
  21.  *    prhs[2] = z0      The input complex number
  22.  *    prhs[3] = k       The iteration count
  23.  *
  24.  *    plhs[0] = z       The resultant matrix of complex numbers
  25.  *    plhs[1] = c       The resultant matrix of iterations / count
  26.  */
  27.      
  28. {
  29.     double *x,*y,*u,*v,t;
  30.     unsigned short *c,k;
  31.     int j,n;
  32.    
  33.     x = mxGetPr(prhs[0]);   // Get array of real components of 'z'
  34.     y = mxGetPi(prhs[0]);   // Get array of imaginary components of 'z'
  35.     c = (unsigned short *) mxGetData(prhs[1]); // Get 'c' array from input
  36.     u = mxGetPr(prhs[2]);   // Get array of real components of 'z0'
  37.     v = mxGetPi(prhs[2]);   // Get array of imaginary components of 'z0'
  38.     k = (unsigned short) mxGetScalar(prhs[3]);  // Get 'k' interate
  39.     /* In the following, the output parameter is a pointer to the input
  40.        data which is about to be modified. A clever trick! */
  41.     plhs[0] = prhs[0];      // &(z') = &(z)
  42.     plhs[1] = prhs[1];      // &(c') = &(c) Output points to 'c' which is about to be modified
  43.  
  44.     n = mxGetN(prhs[0]);    // Number of cols. in 'z' (but a vector in C)
  45.     for (j=0; j<n*n; j++) { // For every element in 'z'...
  46.         if (c[j] == k-1) {  // If element made it to k last time around...
  47.             t = x[j];       // Save the real component
  48.             /* Apply real arithmetic to X and Y components of Z[] */
  49.             x[j] = x[j]*x[j] - y[j]*y[j] + u[j];    // x^2 - y^2 + u
  50.             y[j] = 2*t*y[j] + v[j];                 // 2*x(orig)*y + v
  51.             if (x[j]*x[j] + y[j]*y[j] < 4) {    // |If new value|^2 < 4...
  52.                 c[j] = k;                       // the element made it to k
  53.             }
  54.         }
  55.     }
  56.     return;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement