Re: Re: Package and options, subroutines Mathematica programming 3GL function procedure
- To: mathgroup at smc.vnet.net
- Subject: [mg50692] Re: [mg50668] Re: Package and options, subroutines Mathematica programming 3GL function procedure
- From: "David Park" <djmp at earthlink.net>
- Date: Fri, 17 Sep 2004 01:16:06 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
Bob, This may be slightly tangential to your question but I hope it will be useful. First, learn how to use Sections, Subsections etc., in Mathematica notebooks. 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 earthlink.net http://home.earthlink.net/~djmp/ From: Bob Marlow [mailto:bobmarlow at postmaster.co.uk] To: mathgroup at smc.vnet.net 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 - y1)/n; Rvalue = 0; Do[Rvalue = Rvalue + f[ x1 + i*dx, y1 + j*dy], {i, 0, n - 1}, {j, 0, n - 1}]; N[dx*dy*Rvalue]}[[1]] Example call:= T1[x_,y_]:=x+y z=Rintegrate2D[T1,-3,3,-3,3,100] "David Park" <djmp at earthlink.net> wrote in message news:<ch97qo$ff1$1 at smc.vnet.net>... > Guillermo, > > If it was part of a notebook, I would write your package something like the > 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 used > Global`A and Global`B to set the default values of a and b. Also you want to > start the usage message with the function name and arguments to get > automatic command completion when desired. > > David Park > djmp at earthlink.net > http://home.earthlink.net/~djmp/ > > > From: Guillermo Sanchez [mailto:guillerm at aida.usal.es] To: mathgroup at smc.vnet.net > > > 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*)