0

Possible Duplicate:
Alternatives to procedural loops and iterating over lists in Mathematica

I am new to Mathematica and not familiar with functional programming. In particular, I have no idea on how to replace the usual for loops in C++. Here is an example of the way I am writing code now:

InitCoords[] := {
  VDiv[gap, region, initCell];
  n = 0;
  For[nz = 0, nz < initCell[3], nz++,
   For [ny = 0, ny < initCell[2], ny++,
     For[nx = 0, nx < initCell[1], nx++,
       VSet[c, nx + 0.25, ny + 0.25, nz + 0.25];
       VMul[c, c, gap];
       VSAdd[c, c, -0.5, region];
       For[j = 0, j < 4, j++, 
        Do[r[n][i] = c[i], {i, 1, NDIM}];
        If[j != 3,
         If[j != 0, r[n][1] += 0.5*gap[1];];
         If[j != 1, r[n][2] += 0.5*gap[2];];
         If[j != 2, r[n][3] += 0.5*gap[3];];
         ];
       ++ n;
        ];
       ];
     ];
   ];
  }

I know that this is a very bad way to code in Mathematica. What are the alternatives then?

Another question. Is there is a Mathematica convention for defining a subroutine with no input? I want to partition my big program into smaller parts. Currently, I am doing it by writing a function with no input.

Gosere
  • 357
  • 3
  • 8
  • What do VDiv, VSet, VMul, and VSAdd do? – rcollyer Dec 16 '12 at 06:44
  • My own defined functions: VDiv[v1_, v2_, v3_] := { v1[1] = v2[1]/v3[1]; v1[2] = v2[2]/v3[2]; v1[3] = v2[3]/v3[3]; } Others are just similar like addition and multiplication. Now I just learn to use the list function to replace all those functions. – Gosere Dec 16 '12 at 07:14
  • 1
    If you give the definitions of all functions used we can try to write a simpler version for you to illustrate. – Mr.Wizard Dec 16 '12 at 08:13
  • VDiv[v1_, v2_, v3_] := { v1[1] = v2[1]/v3[1]; v1[2] = v2[2]/v3[2]; v1[3] = v2[3]/v3[3]; }

    VSet[v_, sx_, sy_, sz_] := { v[1] = sx; v[2] = sy; v[3] = sz; }

    VMul[v1_, v2_, v3_] := { v1[1] = v2[1]v3[1]; v1[2] = v2[2]v3[2]; v1[3] = v2[3]*v3[3]; }

    VSAdd[v1_, v2_, s3_, v3_] := { v1[1] = v2[1] + s3v3[1]; v1[2] = v2[2] + s3v3[2]; v1[3] = v2[3] + s3*v3[3]; }

    (sorry it seems that I don't know the comment formatting)

    – Gosere Dec 16 '12 at 09:32
  • Like C++, in Mathematica there is really no distinction between functions and subroutines, so using functions with not arguments to organize your program is fine. – m_goldberg Dec 16 '12 at 11:30
  • You should invest some time reading the documentation about lists. Be aware that the notation is rather different from C. It looks like you are using DownValues (like c[i]) and SubValues (like r[n][1]) to mimic the array notation in C, and I'm pretty sure that's not what you intended. – Simon Woods Dec 16 '12 at 13:50

1 Answers1

1

Of course you can make no argument functions in mathematica, for example:

f[] := Plot[x^2 - 1, {x, -2, 2}]

This function will always make the same plot, whenever

f[]

is called.

To make a real C/C++ function (in the meaning of logically encapsulated piece of code) jus use Module[] function. For example, the function that takes no arguments, makes a plot, and prints a few lines can be written as follows:


f[] := Module[
               {i}, (* variables to be treated as f[]'s local variables *)

Do[Print["Line" + Text[i]], {i, 1, 5}] (loop for(int i=1;i<=5;i++) std::cout<<"Line "<<IntToStr(i)<<std::endl; )

       Plot[x^2 - 1, {x, -2, 2}] (* make also a plot *)

    ]  (* bracket ending the Module[] *)

Hope that helped a little :]

PS.: Don't ever give up C/C++ programming. It will become useful in future to combine faster functions with mathematica.

PS.: Also take a look at Compile[]

Misery
  • 2,640
  • 1
  • 30
  • 39