[Date Index] [Thread Index] [Author Index]
Re: writing programs
On 23 Jul 1998, John M. Dlugosz wrote: > How do you write programs in Mathematica? I'm asking about the > mechanics of preparing the files, not the language reference. As you may have guessed, there is no one way to prepare input for the Mathematica kernel. Any editor capable of producing ASCII text (Notepad, Wordpad, vi, emacs, Programmer's File Editor, etc.) can be used. > I looked at a simple .m file, and see that it's plain text as I would > input it. I wonder though why you don't need a ';' on the end of every > line to separate statements. If it assumes one statement per line, how > do you split a statement accross multiple lines? Here is an example: > > === begin example === > Arg[z_] := -Pi/2 /; Re[z] == 0 && NonNegativeQ[-Im[z]] > > Arg[z_] := ArcTan[Im[z]/Re[z]] /; Re[z] =!= 0 && Im[z] =!= 0 > > Protect[Release[protected]]; > > End; > === end example === > > My guess is that a blank line is used as a statement separator, and the > terminating semi's that are present are in fact redundant. Yes. > But where is this in the documentation? (assuming my guess is even > correct) See the Appendix A.2.10 of the Mathematica Book. FrontEndExecute[FrontEnd`HelpBrowserLookup["MainBook", "A.2.10"]] > Second, typing input text is fine for C++ or Perl, but won't let me get > all the fancy stuff in Mathematica. I'd rather use the notebook > interface. > > This package I'm looking at says I can rename the .nb file to .m and it > will work, too. But I wonder how? If all the cells are executed in > order when I load the "package", what happens to all the examples and > text? Creating packages from the notebook front end can be done via two routes. 1) The approach is to use the AutoGeneratedPackage option setting on the front end to recreate a companion package file every time you save the notebook. This saves out only the contents of your notebook which will be evaluated by the kernel. The contents of cells not flagged for package export are not included. To use this feature, open the notebook that you will use to enter your Mathematica code. Invoke the front end menu command Edit -> Preferences... to summon the Option Inspector dialog. Set the inspector scope to "notebook" if it's showing a different value (like "global" or "selection"). Then enter the text "AutoG" in the Lookup field of the dialog box. You should see an option named AutoGeneratedPackage. Set the option value to Automatic and close the dialog. For each cell that you wish to flag as exportable for kernel input, select the cell's bracket and click on the following menu command sequence: Cell -> Cell Properties -> Initialization Cell This will add a small "I" notch up at the top of the Cell bracket. 2) The other method is to use front end menu command: File -> Save As Special -> Package Format This will save all contents of the notebook. Any cell not flagged for export will be saved as a commented text. As with the AutoGeneratedPackage example, the flagging is done by setting the exported cell's properties to include InitializationCell. If you try to evaluate any cell in notebooks with initialization cells, you may notice a dialog box that asks you if you want to first evaluate the initialization cells. If you find this dialog inconvenient, you can adjust the following options at the global or notebook level with the Option Inspector dialog. InitializationCellLoading -> False InitializationCellWarning -> False You can locate these options with the Lookup field on the Option Inspector dialog. Another customization you may desire is control over indentation. The default input format type for the front end is StandardForm, which does automatic indentation. If you want to avoid this, you should probably set the default input format type to InputForm, which can be done from the front end menu command: Cell -> Default Input Format Type -> InputForm One drawback is that you can use only linear syntax (no two-dimensional typesetting) in this environment. -- P.J. Hinton Mathematica Programming Group paulh at wolfram.com Wolfram Research, Inc. http://www.wolfram.com/~paulh/ Disclaimer: Opinions expressed herein are those of the author alone.