5.1 How to write a package?
5.2 How to use context and packages?
5.3 How to find what packages are loaded?
5.4 Finding packages in specific context
5.5 How to find what contexts are loaded?
5.6 How to load an m file or a package?
5.7 Finding names of functions in a package
5.8 Finding a package that has specific function
5.9 Finding all contexts that belong to a package?
5.10 How to remove packages?
5.11 How to list files in a directory?
5.12 Where is init.m and how to use it?
5.13 Reading Mathematica example data and location
5.14 Finding which folders are on trusted path
5.15 save matrix to file, and read it again
5.16 run Mathematica m file as script

5.1 How to write a package?

Small note here

5.2 How to use context and packages?

?$Packages

"gives a list of the contexts corresponding to all packages which have been loaded in your current Mathematica session."

{"JLink`", "GetFEKernelInit`", "ResourceLocator`", 
"PacletManager`", "QuantityUnits`", "WebServices`", 
 "System`", "Global`"}
 

5.3 How to find what packages are loaded?

$ContextPath

{"PacletManager`", "QuantityUnits`", "WebServices`", 
"System`", "Global`"}
 

5.4 Finding packages in specific context

Names["System`ComplexExpand`*"]

 
{"System`ComplexExpand`AbsExpr", 
"System`ComplexExpand`ArgExpr", 
"System`ComplexExpand`ConjugateExpr", 
"System`ComplexExpand`ReImExpr", 
"System`ComplexExpand`ReImFail", 
"System`ComplexExpand`SignExpr"}
 

5.5 How to find what contexts are loaded?

?Contexts

Contexts[] gives a list of all contexts. 
Contexts["string"] gives a list of the contexts 
                   which match the string.
 

5.6 How to load an m file or a package?

Append to the Path the folder name where the package is located in. In this example, assuming there is a package control.m located in folder C:\data then type the following to load the package

AppendTo[$Path, "C:\\data"] 
<< control.m
 

5.7 Finding names of functions in a package

$Packages

{"JLink`", "GetFEKernelInit`", "ResourceLocator`", "PacletManager`", 
"QuantityUnits`", "WebServices`", "System`", "Global`"}
 

Names["JLink`*"]

{"JLink`AddPeriodical", "JLink`AddToClassPath",...
 

5.8 Finding a package that has specific function

use Context.

Context[Integrate] 
 
     "System`"
 

5.9 Finding all contexts that belong to a package?

Contexts["packageName*"]
 

5.10 How to remove packages?

to do

5.11 How to list files in a directory?

SetDirectory[$BaseDirectory]; 
FileNames["*"]
 

5.12 Where is init.m and how to use it?

Possible locations for init.m files include the following:

  1. $BaseDirectory/Kernel kernel initialization code for all users
  2. $UserBaseDirectory/Kernel kernel initialization code for the currently logged-in user
  3. $BaseDirectory/FrontEnd front end initialization code for all users
  4. $UserBaseDirectory/FrontEnd front end initialization code for the currently logged-in user
  5. I have my init.m in the following folder

    C:\Documents and Settings\All Users\Application Data\Mathematica\Kernel\init.m

5.13 Reading Mathematica example data and location

on windows, V 8, example data is located in

C:\Program Files\Wolfram Research\Mathematica\8.0\Documentation\English\System\ExampleData
 

and it can be read like this

str = OpenRead["ExampleData/USConstitution.txt"] 
 
Out[147]= InputStream[ExampleData/USConstitution.txt, 127]
 

5.14 Finding which folders are on trusted path

Thanks to Mike for these commands, see http://stackoverflow.com/questions/8583521/wh y-do-i-get-security-warning-message-this-file-contains-potentially-unsafe-dyn

CurrentValue[$FrontEnd, {"NotebookSecurityOptions", "TrustedPath"}] 
 
Out[212]= {FrontEnd`FileName[{$InstallationDirectory}], 
          FrontEnd`FileName[{$BaseDirectory}], 
          FrontEnd`FileName[{$UserBaseDirectory}]} 
 
CurrentValue[$FrontEnd, {"NotebookSecurityOptions", "UntrustedPath"}] 
 
Out[213]= {FrontEnd`FileName[{FrontEnd`$DesktopDirectory}], 
           FrontEnd`FileName[{FrontEnd`$DownloadsDirectory}], 
  FrontEnd`FileName[{FrontEnd`$LocalApplicationDataDirectory}], 
  FrontEnd`FileName[{FrontEnd`$RemoteApplicationDataDirectory}], 
  FrontEnd`FileName[{FrontEnd`$ProgramFilesDirectory}], 
  FrontEnd`FileName[{FrontEnd`$ProgramFilesX86Directory}], 
  FrontEnd`FileName[{$TemporaryPrefix}]}
 

Now to find if your current notebook is on the trusted path type NotebookDirectory[] and see if the output shows up in the trusted path of not. To add a folder to trusted path go to "Preferences > Advanced > Open Options Inspector". Then under Global Preferences search for trusted

5.15 save matrix to file, and read it again

SetDirectory[NotebookDirectory[]]; 
list = {{3, 4, 5}, {4, 5, 6}}; 
Export["data.txt", list]
 

To read it later, say after closing and restarting Mathematica again to continue working on the data

SetDirectory[NotebookDirectory[]]; 
list = ToExpression@Import["data.txt", "List"] 
 
    {{3, 4, 5}, {4, 5, 6}}
 

5.16 run Mathematica m file as script

Thanks to Rolf Mertig reference for help on this.

Make a file foo.m such as

AppendTo[$Echo, "stdout"] 
SetOptions[ $Output, FormatType -> OutputForm ]; 
Integrate[Sin[x],x]
 

Now type, from DOS window

"C:\Program Files\Wolfram Research\Mathematica\10.1\math.exe" < foo.m
                                                                                    
                                                                                    
 

This will send input and output to screen. To send output to file do

"C:\Program Files\Wolfram Research\Mathematica\10.1\math.exe" < foo.m > log.txt
 

You can modify the PATH on windows to add the above to environment variable so not to have to type the long command each time