Services & Resources / Wolfram Forums
MathGroup Archive
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2004

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Re: Package and options, subroutines Mathematica programming 3GL function procedure

  • To: mathgroup at
  • Subject: [mg50692] Re: [mg50668] Re: Package and options, subroutines Mathematica programming 3GL function procedure
  • From: "David Park" <djmp at>
  • Date: Fri, 17 Sep 2004 01:16:06 -0400 (EDT)
  • Sender: owner-wri-mathgroup at


This may be slightly tangential to your question but I hope it will be

First, learn how to use Sections, Subsections etc., in Mathematica

Then, when working on some project or studying some text, make an
Initialization Section in your notebooks where you load all the packages you
need and do other standard initializations, and make a Routines section
where you put routines that you have developed as you work along. Whenever
you come across something that will be generally useful in your work,
develop it and then move it to the Routines Section.

Make the code in the Initialization and Routines Section initialization
cells. When you save the notebook DON'T create an AutoSave Notebook. Then
when you reload the notebook you can go right to your work and all the
necesssary initializations will automatically be performed when you do your
first evaluation.

Write usage messsages for each of your routines. Always begin the usage
message with a template of the function call. Ctrl-K will give you automatic
completion of the function name and Shift-Ctrl-K will give you the template.
Writing usage messages might seem like a nuisance but they help you to
clarify what the code should do, they help you remember in the future what
the function does, and if you are going to make a package you will
absolutely have to write usage messages to export the function anyway.

If it is difficult to explain in a usage message what the function does it
is a sure sign that the function is poorly designed.

If I have multiple notebooks I usually copy the Initialization and Routines
Sections over from one notebook to another. But eventually you will probably
have a nice set of routines that have been refined and tested. Then it
should be quite easy to turn them into a package.

David Park
djmp at

From: Bob Marlow [mailto:bobmarlow at]
To: mathgroup at

I have written a 'subroutine' in Mathematica, shown below, which
seems to work fine, but when would it be better, or when would
I need to make it a package?

Rintegrate2D[f_, x1_, x2_, y1_, y2_, n_] := {dx = (x2 - x1)/n; dy = (y2 -
      Rvalue = 0; Do[Rvalue = Rvalue + f[
      x1 + i*dx, y1 + j*dy], {i, 0, n - 1}, {j, 0, n - 1}];

Example call:=


"David Park" <djmp at> wrote in message
news:<ch97qo$ff1$1 at>...
> Guillermo,
> If it was part of a notebook, I would write your package something like
> following.
> BeginPackage["Test1`"]
> fexample1::"usage" =
>     "fexample[x, opts] calculates (a + b) x where a and b are set by \
> options.";
> a::usage = "a is an option for fexample that sets its value.";
> b::usage = "b is an option for fexample that sets its value.";
> Begin["`Private`"]
> Options[fexample1] = {a -> Global` A, b -> Global` B};
> fexample1[x_, opts___Rule] :=
>   (a + b) x /. {opts} /. Options[fexample1]
> End[]
> EndPackage[]
> Then
> ?Test1`*
> gives usage messges for a, b and fexample1 and
> fexample1[x]
> (A + B) x
> Notice that I moved the Options statement to the Private section and I
> Global`A and Global`B to set the default values of a and b. Also you want
> start the usage message with the function name and arguments to get
> automatic command completion when desired.
> David Park
> djmp at
> From: Guillermo Sanchez [mailto:guillerm at]
To: mathgroup at
> Dear friends
> Here is a example package:
> (*Title : Package for testing options*)
> BeginPackage["Test`Test1`"]
> fexample1::"usage" = "Help example";
> Options[fexample1] = {a -> A, b -> B}
> Begin["`Private`"]
> fexample1[x_, opts___Rule] :=
>   (a + b) x /. {opts} /. Options[fexample1]
> End[]
> EndPackage[]
> (*Now I request the package Help*)
> ?"Test`Test1`*"
> (*They are shown not only the fexample1 help but the symbols used in
> Options are also shown. How can I prevend that these symbols (a, A, b,
> B} be shown.
> Other general question:
> When Options should be place inside of the private context?
> Thanks.
> Guillermo*)

  • Prev by Date: Re: Primed Variables in Mathematica
  • Next by Date: GUIKit
  • Previous by thread: Re: Package and options, subroutines Mathematica programming 3GL function procedure
  • Next by thread: How to simplify to a result that is real