Mathematica Notes
Mathematica Notes
Mathematicians
MATHEMATICA
By:
Uzair Salman
Computer Science Department
Superior Collage Jauharabad
Table of Contents
1. INTRODUCTION TO MATHEMATICA. .......................................................................................... 3
1.1. Notebook....................................................................................................................................... 3
1.2. Mathematica Syntax ...................................................................................................................... 4
1.3. Variables ....................................................................................................................................... 4
6. GRAPHICS ......................................................................................................................................... 18
7. MATHEMATICA PACKAGES......................................................................................................... 22
7.1. Introduction ................................................................................................................................. 22
7.2. Writing your own package ........................................................................................................... 23
REFERENCE: ............................................................................................................................................. 24
1
[Comp-102] Programming Language for Mathematicians (2+1 Cr. Hrs.)
BS(Math) Term-II
Blank Page
2
[Comp-102] Programming Language for Mathematicians (2+1 Cr. Hrs.)
BS(Math) Term-II
1. INTRODUCTION TO MATHEMATICA.
Mathematica is a very powerful program with many features. Mathematica is a computer
algebra system designed to do mathematics. Symbolic, numerical and graphical computations
can all be done with Mathematica. Unlike conventional computer languages Mathematica can
do symbolic manipulation, has a huge number of built-in numerical functions, can do numerical
calculations to arbitrary precision, and has powerful plotting routines which interface directly
with the results of calculations. In short we can say Mathematica is a powerful calculator.
There are two interfaces to Mathematica: (i) the command line interface, and (ii) the notebook
interface. The same commands are given in both, but the notebook interface has some
additional features. We will discuss notebook interface in this course.
1.1. Notebook
When you start up Mathematica, the first thing you see is a window displaying the contents is
a “notebook” Notebook in Mathematica is like a document in Microsoft Word. Whatever you
type (Input) is entered into a notebook file, which is designated with the extension “.nb”. Just
as Microsoft Word starts with an open document called Document1, Mathematica starts with
an open notebook called Untitled-1.nb.
Cell
Insertion Point
3
[Comp-102] Programming Language for Mathematicians (2+1 Cr. Hrs.)
BS(Math) Term-II
To enter a command into a notebook, simply begin typing. The default cell type is input. When
you’re done typing, to evaluate an existing input cell, simply click anywhere inside the cell and
press Shift + Enter keys. Pressing Enter without the Shift key starts a new line in the
current cell.
Note: Mathematica is case sensitive you must type first letter of each function and symbol as capitalized.
e.g. to indicate 𝜋, 𝑒, 𝑖 you must type Pi , E and I
1.3. Variables
in order to feed data to a computer program one needs to define variables to be able to assign
data to them. As long as you use common sense, any names you choose for variables are valid
in Mathematica. Names like x, y, x1, myfunc, yValue ... are all fine. Do not use underscore to
define a variable because The underscore is reserved and will be used in the definition of
functions. Have a look at this example:
2. NUMERIC/ALGEBRAIC CALCULATION
4
[Comp-102] Programming Language for Mathematicians (2+1 Cr. Hrs.)
BS(Math) Term-II
Addition + Plus[x, y]
Subtraction - Subtract[x, y]
Multiplication * Times[x, y]
Division / Divide[x, y]
Exponential(power) ˆ Power[x, n]
Factorial ! Factorial[x]
Note: Precedence is the same as normal mathematics, so for example multiplication takes
precedence over addition
Example:
By default, the output from Mathematica is given in its most exact form. (as in above example
output of 2/3 is shown in exact form) The result may contain fractions or symbols such as 𝑷𝒊
or 𝝐. To get a numerical result, you use the function N[…].
5
[Comp-102] Programming Language for Mathematicians (2+1 Cr. Hrs.)
BS(Math) Term-II
Equality ==
Inequality !=
6
[Comp-102] Programming Language for Mathematicians (2+1 Cr. Hrs.)
BS(Math) Term-II
Examples:
In[1]:= Abs[-2.5] In[7]:= Sin[60 Degree] In[11]:= PrimeQ[13]
Out[1]:= 2.5 Out[7]:= Out[11]:= True
In[2]:= Sqrt[3.]
Out[2]:= 1.73205 In[8]:= Cos[30 Degree]
In[3]:= Exp[3.4] Out[8]:=
Out[3]:= 29.9641
In[4]:= Log[1000.] In[9]:= Tan[30 Degree]
Out[4]:= 6.90776 Out[9]:=
In[5]:= Log[10, 1000]
Out[5]:= 3 In[10]:= Prime[100]
In[6]:= RandomInteger[{1,10}] Out[10]:= 541
Out[6]:= 6
7
[Comp-102] Programming Language for Mathematicians (2+1 Cr. Hrs.)
BS(Math) Term-II
• Together[exp] puts terms in a sum over a common denominator, and cancels factors
in the result. Together makes a sum of terms into a single rational function.
In[3] ≔ 𝑇𝑜𝑔𝑒𝑡ℎ𝑒𝑟[𝑎/𝑏 + 𝑐/𝑑]
𝑏𝑐 + 𝑎𝑑
Out[3]: =
𝑏𝑑
• Simplify[expr] tries to convert the expression into a simpler form:
𝑥 S +6𝑥 A + 11𝑥 + 6
In[4]: = 𝑆𝑖𝑚𝑝𝑙𝑖𝑓𝑦[ ]
𝑥 A + 4𝑥 + 3
Out[4]: = 2 + 𝑥
• Apart[expr] rewrites a rational expression as a sum of terms with minimal
denominators.
6 + 16𝑥 + 8𝑥 A
In[5]: = 𝐴𝑝𝑎𝑟𝑡[ ]
1 + 2𝑥
Out[5]: = 6 + 4 𝑥
• Cancel[expr] Cancel common factors in Numerator and Denominator.
𝑥A − 1
In[6]: = 𝐶𝑎𝑛𝑐𝑒𝑙[ ]
𝑥−1
Out[6]: = 1 + 𝑥
• Exponent[expr,x] find largest exponent of x in exp.
In[7]: = 𝐸𝑥𝑝𝑜𝑛𝑒𝑛𝑡[𝑥 S +6𝑥 A + 11𝑥 + 6 , 𝑥]
Out[7]: = 3
• Solve[expr,x] Find the solutions to a cubic equation.
In[7]: = 𝑆𝑜𝑙𝑣𝑒[𝑥 S − 6𝑥 A + 11𝑥 − 6 , 𝑥]
Out[7]: = {{𝑥 → 1}, {𝑥 → 2}, {𝑥 → 3}}
Note: In order to display the polynomial in decreasing powers of x we may invoke the
TraditionalForm[exp] instruction:
In[8] ≔ 𝑇𝑟𝑎𝑑𝑖𝑡𝑖𝑜𝑛𝑎𝑙𝐹𝑜𝑟𝑚[6 + 16 𝑥 + 8 𝑥 A ]
Out[8] ≔ 8 𝑥 A + 16 𝑥 + 6
8
[Comp-102] Programming Language for Mathematicians (2+1 Cr. Hrs.)
BS(Math) Term-II
entity for example, do arithmetic on the whole list at once, or assign the whole list to be the
value of a variable.
Some Basic operations on lists:
In[1]: = 𝐴 = {1 , 2 , 3 , 4}; 𝐵 = { 5 , 6 , 7 , 8 };
In[2]: = A+B
Out[2]: = {6 , 8 , 10 , 12}
In[3]: = 𝐴^2
Out[3]: = {1 , 4 , 9 , 16}
In[4]: = A∗ B
Out[4] ≔ {5 , 12 , 21 , 32}
In[5]: = A/B
Out[5] ≔ 1 1 3 1
d + + + e
5 3 7 2
We can also carry out vector operations.
In[6]: = 𝐴. 𝐵 Scaler or Dot product
Out[6] ≔ 70
In[7]: = 𝐴 ∗ 𝐵 Vector or cross product
Out[7] ≔ {5 , 12 , 21 , 32}
Note: We can also apply any of mathematical function to whole lists. We can refer to an
element of a list by giving its index. Elements are numbered in order starting from 1. For
example:
In[1] ≔ 𝐴 = {2 , 4 , 6 , 8};
In[2] ≔ 𝐴[[2]]
Out[2] ≔ 4
9
[Comp-102] Programming Language for Mathematicians (2+1 Cr. Hrs.)
BS(Math) Term-II
• Table[…] provides an easy way to generate many kind of lists such as matrices ,
vectors.
In[1] ≔ 𝑇𝑎𝑏𝑙𝑒[𝑥^2 , 5] It will generate list of 10 copies of exp x2
Out[1] ≔ {𝒙𝟐 , 𝒙𝟐 , 𝒙𝟐 , 𝒙𝟐 , 𝒙𝟐 }
In[2] ≔ 𝑇𝑎𝑏𝑙𝑒[𝑛, {𝑛, 10}] It will generate list for exp n for 1 ≤ 𝑛 ≤ 10
Out[2] ≔ {1,2,3,4,5,6,7,8,9,10}
In[3] ≔ 𝑇𝑎𝑏𝑙𝑒[𝑛 ^2 , {𝑛, 5}] Generate list for exp n2 for 1 ≤ 𝑛 ≤ 5
Out[3] ≔ {1 , 4 , 9 , 16 , 25}
In[4] ≔ 𝑇𝑎𝑏𝑙𝑒[𝑥 + 1 , {𝑥, 1 , 10 , 2}] Generate list for exp 𝒙 + 𝟏 for 1 ≤ 𝑥 ≤ 10
Out[4] ≔ {2 , 4 , 6 , 8 , 10} with interval of 2
In[5] ≔ 𝑇𝑎𝑏𝑙𝑒[𝑃𝑟𝑖𝑚𝑒[𝑖] , {𝑖, 5}] Generate list first 5 prime numbers.
Out[5] ≔ {2 , 3 , 5 , 7 , 11}
• NestList[f, exp , n] generate a list of the results of applying f to exp through n times.
In[1] ≔ 𝑁𝑒𝑠𝑡𝐿𝑖𝑠𝑡[𝑓 , 𝑥 , 4]
Out[1] ≔ p 𝒙 , 𝒇[𝒙] , 𝒇r𝒇[𝒙]s , 𝒇 t𝒇r𝒇[𝒙]su , 𝒇 v𝒇 t𝒇r𝒇[𝒙]suw x
In[1] ≔ 𝑓 [𝑥_] ≔ 𝑥 ^2 User define function
In[1] ≔ 𝑁𝑒𝑠𝑡𝐿𝑖𝑠𝑡[𝑓 ,2 , 4] Generate list on given function
Out[1] ≔ { 2, 4, 16, 256, 65536 }
10
[Comp-102] Programming Language for Mathematicians (2+1 Cr. Hrs.)
BS(Math) Term-II
In[1] ≔ 𝑠 = 𝑇𝑎𝑏𝑙𝑒[𝑖 + 𝑗 , {𝑖, 3}, {𝑗, 3}] This build a 3x3 matrix S with
elements sij = i+j
Out[1] ≔ }{2,3,4}, {3,4,5}, {4,5,6}~
In[2] ≔ 𝑀𝑎𝑡𝑟𝑖𝑥𝐹𝑜𝑟𝑚[𝑠] This display s in standard two
2 3 4 dimensional format
Out[2] ≔ ‡3 4 5ˆ
4 5 6
In[3] ≔ 𝐴𝑟𝑟𝑎𝑦[𝑎, 3] Create an array of three elements
Out[3] ≔ {𝑎[1], 𝑎[2], 𝑎[3] }
In[4] ≔ 𝑚 = }{𝑎, 𝑏}, {𝑐, 𝑑 }~; 𝐷𝑒𝑡[𝑚] Generate a list m and find its
Out[4] ≔ −𝑏𝑐 + 𝑎𝑑 determinants
In[5] ≔ 𝑇𝑟𝑎𝑛𝑠𝑝𝑜𝑠𝑒[𝑚] Find transpose of m
Out[5] ≔ }{𝑎, 𝑐 }, {𝑏, 𝑑 }~
In[5] ≔ 𝑖𝑛𝑣𝑒𝑟𝑠𝑒[𝑚] Find inverse of matrix m
𝑑 𝑏 𝑐 𝑎
Out[5] ≔ ‰d ,
−𝑏𝑐 + 𝑎𝑑 −𝑏𝑐 + 𝑎𝑑
e , p− ,
−𝑏𝑐 + 𝑎𝑑 −𝑏𝑐 + 𝑎𝑑
xŠ
11
[Comp-102] Programming Language for Mathematicians (2+1 Cr. Hrs.)
BS(Math) Term-II
3.3. Sets
Mathematica usually keeps the elements of a list in exactly the order you originally entered
them. If you want to treat a list like a mathematical set, however, you may want to ignore the
order of elements in the list. Sets are represented by sorted lists.
3.3.1. Functions for Sets.
Function Name
f[x_]:= body; Function Definition
Function Arguments
(Pattern)
Definition symbol 12
[Comp-102] Programming Language for Mathematicians (2+1 Cr. Hrs.)
BS(Math) Term-II
Basic Example:
F[x_] := x2
When using a function, the argument is replaced by an expression. In other words, you fill in
the blanks. So
f[2] 4
f[a] a2
f[a+b] (a+b)2
Some more examples:
assigns the value vali to the function f at x in case the condition condi is TRUE. It assigns the
value val to x whenever none of the conditions condi for i = 1, 2, ..., n are TRUE.
To illustrate of doing this, consider the following example. Suppose that
𝑥A 𝑖𝑓 𝑥 ≤ −1
𝑓 (𝑥 ) = ”−2 + 𝑥 𝑖𝑓 − 1𝑥 < 1
𝑥S 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒
To define such function, we use such command:
In[1] ≔ 𝑓 [𝑥 ] ≔ 𝑃𝑖𝑒𝑐𝑒𝑤𝑖𝑠𝑒[{𝑥ˆ2 , 𝑥 < = −1}, {𝑥 − 2, −1 < 𝑥 < 1}, 𝑥 ^ 3]
In[2] ≔ 𝑓 [𝑥 ] Calling user defined function
Out[2] ≔ 𝑥A 𝑖𝑓 𝑥 ≤ −1
”−2 + 𝑥 𝑖𝑓 − 1𝑥 < 1
𝑥S 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒
13
[Comp-102] Programming Language for Mathematicians (2+1 Cr. Hrs.)
BS(Math) Term-II
4.2. Recursion
The process in which a function calls itself directly or indirectly is called recursion
and the corresponding function is called as recursive function.
A recursive process is one in which objects are defined in terms of other objects of the
same type. Using some sort of recurrence relation, the entire class of objects can then be built
up from a few initial values and a small number of rules. The Fibonacci numbers are most
commonly defined recursively.
Problem: Write a recursive solution to find factorial of a given number n
Solution:
In[1] ≔ 𝑚𝑦𝐹𝑎𝑐𝑡𝑜𝑟𝑖𝑎𝑙[0] = 1;
𝑚𝑦𝐹𝑎𝑐𝑡𝑜𝑟𝑖𝑎𝑙[𝑛_] ≔ 𝑛 ∗ 𝑚𝑦𝐹𝑎𝑐𝑡𝑜𝑟𝑖𝑎𝑙[𝑛 − 1];
𝑚𝑦𝐹𝑎𝑐𝑡𝑜𝑟𝑖𝑎𝑙[5]
Out[1] ≔ 120
5. PROCEDURAL PROGRAMMING
Conventional programming languages express a style of programming that write code in a step-
by-step manner. These procedures typically involved certain basic elements:
• looping over an array
• conditional statements that controlled the flow of execution
• logical constructs to build up tests
• functions to jump around from one place in a program to another.
Although newer languages have introduced many new programming paradigms, procedural
programming continues to be used and remains an appropriate style for certain kinds of
problems. Mathematica supports all standard procedural programming constructs listed below.
5.1. Assignments
Mathematica supports a highly generalized notion of assignment such as:
𝑥=𝑦 Assign a value {𝑥, 𝑦} = {𝑢, 𝑣} Assign multiple value {𝑥, 𝑦} = {𝑦, 𝑥} Swap values
𝑥++ Increment 𝑥−− Decrement ++𝑥 PreIncrement
−−𝑥 PreDecrement 𝑥−= 𝑦 SubtractFrom 𝑥+= 𝑦 AddTo
𝑥+= 𝑦 TimesBy 𝑥/ = 𝑦 DivideBy
14
[Comp-102] Programming Language for Mathematicians (2+1 Cr. Hrs.)
BS(Math) Term-II
While[condition, body] (*evaluates condition, then body, repetitively, until condition remain true*)
Example:
In[1]:= n=1; While[ n<5 ,Print[n]; n++]
Out[1]:= 1
2
3
4 Condition Body of loop
15
[Comp-102] Programming Language for Mathematicians (2+1 Cr. Hrs.)
BS(Math) Term-II
Solution:
In[1] ≔ 𝑖 = 1;
𝑛 = 𝐼𝑛𝑝𝑢𝑡[“𝑒𝑛𝑡𝑒𝑟 𝑎 𝑛𝑢𝑚𝑏𝑒𝑟”];
𝑝𝑠𝑒𝑡 = { };
𝑊ℎ𝑖𝑙𝑒[ 𝑃𝑟𝑖𝑚𝑒[𝑖 ] ≤ 𝑛,
𝑝𝑠𝑒𝑡 = 𝐴𝑝𝑝𝑒𝑛𝑑[𝑝𝑠𝑒𝑡 , 𝑃𝑟𝑖𝑚𝑒[𝑖] ];
𝑖 + +];
𝑝𝑠𝑒𝑡
Out[1] ≔ {2, 3, 5, 7, 11, 13, 17, 19} Suppose n=21 then.
Basic Example:
€ A S š
Problem: find the sum of the sequence €žA + AžS + SžŸ + ⋯ + šž(šž€) where n is given.
Solution:
16
[Comp-102] Programming Language for Mathematicians (2+1 Cr. Hrs.)
BS(Math) Term-II
Out[-]:= f[0,0]
f[0,1]
f[0,2]
f[1,0]
f[1,1]
f[1,2]
f[2,0]
f[2,1]
f[2,2]
5.3. Flow Control
The flow of execution might change based on the result evaluated by some condition. This
process is referred to as decision making. The decision making statements are also called as
control statement.
5.3.1. If Statement
Like other languages, Mathematica use If statement to see if a condition is true or false. The
basic syntax of if statement is:
In[1] ≔ 𝑔[𝑥_ ] ≔ 𝐼𝑓[𝑥 > 0, 𝑆𝑞𝑟𝑡[𝑥 ], 0]; defined a function g[x] having
𝑔[4] condition is if x > 0. If this condition is
true, then the If statement will return
the square root of x, if the condition is
false the function will return zero.
Out[1] ≔ 2
17
[Comp-102] Programming Language for Mathematicians (2+1 Cr. Hrs.)
BS(Math) Term-II
Example:
In[1] ≔ 𝑚𝑦𝐶𝑎𝑙𝑐[𝑜𝑝_, 𝑛𝑢𝑚𝑠_]: =
𝑆𝑤𝑖𝑡𝑐ℎ[𝑜𝑝 ,
" + ", 𝐴𝑝𝑝𝑙𝑦[𝑃𝑙𝑢𝑠, 𝑛𝑢𝑚𝑠],
" − ", 𝐴𝑝𝑝𝑙𝑦[𝑆𝑢𝑏𝑡𝑟𝑎𝑐𝑡, 𝑛𝑢𝑚𝑠],
" ∗ ", 𝐴𝑝𝑝𝑙𝑦[𝑇𝑖𝑚𝑒𝑠, 𝑛𝑢𝑚𝑠],
"/ ", 𝐴𝑝𝑝𝑙𝑦[𝐷𝑖𝑣𝑖𝑑𝑒, 𝑛𝑢𝑚𝑠],
_ , 𝑃𝑟𝑖𝑛𝑡["𝑖𝑛𝑣𝑎𝑙𝑖𝑑 𝑜𝑝𝑒𝑟𝑎𝑡𝑜𝑟"]
]
𝐼𝑛[2] ≔ 𝑚𝑦𝐶𝑎𝑙𝑐[ "+", {2,3}]
Out[2] ≔ 5
𝐼𝑛[3] ≔ 𝑚𝑦𝐶𝑎𝑙𝑐[ "*", {2, 5} ]
Out[3] ≔ 10
6. GRAPHICS
Mathematica is extremely good at creating graphics to help us analyze problems. it is very
often helpful to involve graphics in a calculation. There are several methods in Mathematica
to generate graphics, some of 2D and 3D graphics generation methods will be discussed here.
6.1. Plot
Plot is used to display the plot for a given function. This needs to have the range of values
over which the plot is to be made specified.
The Plot Function takes two arguments. the first is the function, and the second is a list of
intervals. Basic syntax is:
Plot[f, {x, xmin, xmax}]
(*generates a plot of f as a Function of x from xmin to xmax *)
Example:
18
[Comp-102] Programming Language for Mathematicians (2+1 Cr. Hrs.)
BS(Math) Term-II
There are numerous ways that we could have affected the appearance of the plot by specifying
options. Among the few options for Plot are:
• AxisStyle®{ {xColor, Style} , {yColor, Style} }
• AxisLabel®{x,y} • • AspectRatio®Automatic
• Filling®{Axis/Top/Bottom} • • FillingStyle®{Color}
• PlotLabel®"Sample" • • PlotLabels®{"A","B"}
• PlotLegends®{"one”,”two”} • • PlotRange®Automatic/Full/value
• PlotStyle®{Color,Thick/Thin/Dotted/Dashed}
Note that the arrow character is typed as ESC -> ESC . or just typing ->
Example:
19
[Comp-102] Programming Language for Mathematicians (2+1 Cr. Hrs.)
BS(Math) Term-II
Problem: use PolerPlot to graph the polar equation 𝑟 = 1 + tan (𝜃) 𝑓𝑟𝑜𝑚 𝜃 = 0 𝑡𝑜 2𝜋 . (2019)
Solution:
In[1] ≔ 𝑃𝑜𝑙𝑎𝑟𝑃𝑙𝑜𝑡[{1 + 𝑇𝑎𝑛[𝑥]} , {𝑥, 0, 2𝜋}, 𝐴𝑠𝑝𝑒𝑐𝑡𝑅𝑎𝑡𝑖𝑜 → 0.5, 𝑃𝑙𝑜𝑡𝑆𝑡𝑦𝑙𝑒 → 𝑂𝑟𝑎𝑛𝑔𝑒]
Out[1] ≔
6.2. Graphics
Graphics[..] represents a two-dimensional graphic image whereas Graphics3D[..] can
generate 3D graphic image. Graphics is displayed in StandardForm as a graphical image. In
InputForm, it is displayed as an explicit list of primitives.
The following graphics primitives can be used:
𝐼𝑛[1]: = 𝐺𝑟𝑎𝑝ℎ𝑖𝑐𝑠[𝐴𝑟𝑟𝑜𝑤[{{1,0}, {2,1}, {3,0}, {4,1}}]]
• 𝑨𝒓𝒓𝒐𝒘[ {𝒙𝟏, 𝒙𝟐}, … ] is a graphics
primitive that represents an arrow
from x1 to x2.
•𝑨𝒓𝒓𝒐𝒘[ {𝒙𝟏, 𝒙𝟐}, 𝒔] represents an
arrow with its ends set back from x1 𝐼𝑛[2]: = 𝐺𝑟𝑎𝑝ℎ𝑖𝑐𝑠3𝐷[𝐴𝑟𝑟𝑜𝑤[{{1,0,1}, {2,1,2}, {3,0,1}, {4,2,0}}]]
and x2 by a distance s.
• 𝑳𝒊𝒏𝒆[ {𝒙𝟏, 𝒙𝟐, … }] represents the 𝐼𝑛[1]: = 𝐺𝑟𝑎𝑝ℎ𝑖𝑐𝑠 t𝐿𝑖𝑛𝑒r } {1,0}, {2,1}, {3,0}, {4,1}~su
line segments joining a sequence of
points xi.
•𝑳𝒊𝒏𝒆[ {𝒙𝟏𝟏 , 𝒙𝟏𝟐 , … } , {𝒙𝟐𝟏 , … } ]
20
[Comp-102] Programming Language for Mathematicians (2+1 Cr. Hrs.)
BS(Math) Term-II
• 𝑪𝒊𝒓𝒍𝒄𝒆[ {𝒙, 𝒚, }, 𝒓 ] represents a 𝐼𝑛[1]: = 𝐺𝑟𝑎𝑝ℎ𝑖𝑐𝑠[ { 𝑅𝑒𝑑, 𝑇ℎ𝑖𝑐𝑘, 𝐷𝑎𝑠ℎ𝑒𝑑, 𝐶𝑖𝑟𝑐𝑙𝑒[ {1,2}, 2 ] }]
circle of radius r with center point at
x,y
𝐼𝑛[1]: 𝐼𝑛[1]:
• 𝑫𝒊𝒔𝒌[ {𝒙, 𝒚, }, 𝒓 ] represents a disk = 𝐺𝑟𝑎𝑝ℎ𝑖𝑐𝑠[ { 𝑂𝑟𝑎𝑛𝑔𝑒, 𝐷𝑖𝑠𝑘[ {1,2}, 1 ] }] = 𝐺𝑟𝑎𝑝ℎ𝑖𝑐𝑠[ { 𝑂𝑟𝑎𝑛𝑔𝑒, 𝐷𝑖𝑠𝑘[ {1,2}, 1, {0°, 90°} ] }]
of radius r with center point at x,y.
• 𝑫𝒊𝒔𝒌[ {𝒙, 𝒚, }, {𝜽𝟏 , 𝜽𝟐 } ] give a
sector of a disk from angle 𝜃€ 𝑡𝑜 𝜃A
• 𝑹𝒆𝒄𝒕𝒂𝒏𝒈𝒍𝒆r }{𝒙𝒎𝒊𝒏, 𝒚𝒎𝒊𝒏 }, {𝒙𝒎𝒂𝒙 , 𝒚𝒎𝒂𝒙 }~s 𝐼𝑛[1]: = 𝐺𝑟𝑎𝑝ℎ𝑖𝑐𝑠 v 𝑅𝑒𝑐𝑡𝑎𝑛𝑔𝑙𝑒 t p𝑂𝑟𝑎𝑛𝑔𝑒, }{0,2}, {2,1}~x uw
Represent an axis aligned filled
rectangle from 𝑥–™š , 𝑦–™š to
𝑥–—˜ , 𝑦–—˜
• It can be used as a geometric region
and a graphics primitive.
21
[Comp-102] Programming Language for Mathematicians (2+1 Cr. Hrs.)
BS(Math) Term-II
7. MATHEMATICA PACKAGES
7.1. Introduction
When you begin a Mathematica session, the built-in functions are immediately available for
you to use. There are, however, many more functions that you can access that reside in files
supplied with Mathematica. In principle, the only difference between those files and the ones
you create is that those were written by professional programmers. There is another difference:
the definitions in those files are placed in special structures called packages. Indeed, these files
themselves are often called “packages” instead of “files”.
A Mathematica package is used to store Mathematica code so that it can be loaded into
a Mathematica session. They are designed to make it easy to distribute your programs to others.
Typically, a package is placed in a file that has the extension ".m".
A Mathematica package provides one or more functions, which are placed into a context or
group of Mathematica symbols. The code that gives the functionality is hidden in an
implementation section of the package.
Their purpose is to allow the programmer to define a collection of functions for export. These
exported functions are for the users of the package to work with and are often referred to as
public functions.
22
[Comp-102] Programming Language for Mathematicians (2+1 Cr. Hrs.)
BS(Math) Term-II
BeginPackage["SimpleArithmetic`"]
Begin["`Private`"]
AddTwo[a_, b_] := plus[a, b];
AddThree[a_, b_, c_] := plus[a, b, c];
TimesTwo[a_, b_] := times[a, b];
TimesThree[a_, b_, c_] := times[a, b, c];
End[]
EndPackage[]
In[1]:= << SimpleArithmetic.m This reads in the sample package given above
23
[Comp-102] Programming Language for Mathematicians (2+1 Cr. Hrs.)
BS(Math) Term-II
REFERENCE:
1. (n.d.). Retrieved May 08, 2020, from https://reference.wolfram.com/language/
2. Wellin, P. R., Gaylord, R. J., & Kamin, S. N. (2013). An introduction to programming
with mathematica. Cambridge: Cambridge University Press.
3. Eric Peasley (2013). An introduction to Mathematica. Department of Engineering
Science, University of Oxford.
4. Richard Gaylord. Mathematica Programming Fundamentals.
5. G. P´ olya. (2007). Programming In Mathematica, A Problem-Centred Approach
6. Thomas Steger (2007). An Introduction to Mathematica. University of Leipzig
7. David Maslanka (2010). Introduction to Mathematica.
8. Phil Ramsden and Phillip Kent (1999). An Introduction to Mathematica. Mathematics
Department, Imperial College.
9. Peter Young (2013). Introduction to Mathematica.
24
Best PDF Encryption Reviews