0% found this document useful (0 votes)
11 views

Mathematica Notes

The document introduces the Mathematica programming language. It discusses that Mathematica is a powerful computer algebra system that can perform symbolic, numeric, and graphical computations. It can do algebraic manipulation and has many built-in functions. The document outlines the notebook interface for Mathematica, which allows entering inputs and viewing outputs. It also discusses Mathematica syntax including capitalization conventions for functions and the use of brackets, parentheses and braces for arguments, grouping, vectors/matrices and lists. Finally, it notes that variables can be defined in Mathematica to store and work with data.

Uploaded by

abdulrehman g
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Mathematica Notes

The document introduces the Mathematica programming language. It discusses that Mathematica is a powerful computer algebra system that can perform symbolic, numeric, and graphical computations. It can do algebraic manipulation and has many built-in functions. The document outlines the notebook interface for Mathematica, which allows entering inputs and viewing outputs. It also discusses Mathematica syntax including capitalization conventions for functions and the use of brackets, parentheses and braces for arguments, grouping, vectors/matrices and lists. Finally, it notes that variables can be defined in Mathematica to store and work with data.

Uploaded by

abdulrehman g
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Programming Language for

Mathematicians

MATHEMATICA

By:
Uzair Salman
Computer Science Department
Superior Collage Jauharabad

Version 1.2; May, 2020


[Comp-102] Programming Language for Mathematicians (2+1 Cr. Hrs.)
BS(Math) Term-II

Table of Contents
1. INTRODUCTION TO MATHEMATICA. .......................................................................................... 3
1.1. Notebook....................................................................................................................................... 3
1.2. Mathematica Syntax ...................................................................................................................... 4
1.3. Variables ....................................................................................................................................... 4

2. NUMERIC/ALGEBRAIC CALCULATION ....................................................................................... 4


2.1. Arithmetic Operators ..................................................................................................................... 4
2.2. Assignment Operators.................................................................................................................... 6
2.3. Relational Operators ..................................................................................................................... 6
2.4. Standard Functions ........................................................................................................................ 7
2.5. Algebraic Manipulation(Symbolic Calculation) .............................................................................. 7

3. LISTS, SETS, VECTORS AND MATRICES. .................................................................................... 8


3.1. Lists .............................................................................................................................................. 8
3.1.1. Functions producing lists. ......................................................................................................... 9
3.2. Vectors and Matrices ................................................................................................................... 10
3.2.1. Functions for Vectors .............................................................................................................. 11
3.2.2. Functions for Matrices. ........................................................................................................... 11
3.3. Sets ............................................................................................................................................. 12
3.3.1. Functions for Sets.................................................................................................................... 12

4. FUNCTIONS & FUNCTIONAL PROGRAMMING ........................................................................ 12


4.1. Defining functions ....................................................................................................................... 12
4.2. Recursion .................................................................................................................................... 14

5. PROCEDURAL PROGRAMMING .................................................................................................. 14


5.1. Assignments ................................................................................................................................ 14
5.2. Loop Constructs .......................................................................................................................... 15
5.2.1. Do Loop.................................................................................................................................. 15
5.2.2. While Loop.............................................................................................................................. 15
5.2.3. For Loop................................................................................................................................. 16
5.3. Flow Control ............................................................................................................................... 17
5.3.1. If Statement ................................................................................................................................. 17
5.3.2. Switch Statement ......................................................................................................................... 17

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

Figure 1 Wolfram Mathematica Notebook web Interface


A Mathematica notebook is composed of cells. On the right side of the window you see cell
brackets. Each cell in the notebook shown above is either an input cell, an output cell, or a
graphics cell. There are several other kinds of cells. Some of these are text, title, and section.
Also notice the horizontal line near the bottom of the window. This indicates the insertion
point for the next new cell.
info: the Input is designated with In[number]:= and the Output response by Out[number]:= The number
indicates the sequential order of the Evaluations that Mathematica has performed.

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.2. Mathematica Syntax


i. Mathematica is case sensitive. Thus, cos[x] is not the same as Cos[x].
ii. Mathematica has several built-in functions (e.g., Cos[x], Sin[x], Exp[x] etc.). The first
letter of built-in functions always have the first letter capitalized, and if a name
consists of two or more words, the first letter of each word is capitalized and there will
be no space between the words (e.g., LaplaceTransform[x]).
iii. The arguments(input) of functions are given in brackets [...], parenthesis (...) are used
for grouping operations; vectors, matrices, and lists are given in braces {...}; and
double square brackets [[...]] are used for indexing lists and tables.

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:

Here a & b are


variables holding Note: By putting ; at the
some values end of any expression will
evaluate the input but
didn’t show its result.

2. NUMERIC/ALGEBRAIC CALCULATION

2.1. Arithmetic Operators


Mathematica can be used as a calculator with the basic arithmetic operation such as + , - , *,
/

4
[Comp-102] Programming Language for Mathematicians (2+1 Cr. Hrs.)
BS(Math) Term-II

Operation Mathematica Syntax Alternative Functions

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:

Note: To compute the product of 2 times 3,


we may simply enter: 2 3 in an input cell,
i.e., 2[space]3 , and Mathematica will
automatically insert the multiplication
operator.

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[…].

Decimal approximation of 2/3

Numerate to 3 decimal places

5
[Comp-102] Programming Language for Mathematicians (2+1 Cr. Hrs.)
BS(Math) Term-II

2.2. Assignment Operators


These operators assign value or expression into variables. =, := Primarily there are two
assignment operator in Mathematica. There is a fundamental differences between = and :=
Have a look at this example:

In above example it is clear that when


in above example y := x+2, it is clearly
we define y = x+2 then y takes the value
seen that when x changes, the value of y
of x+2 and this will be assigned to y. No
changes too. Namely using := then y is a
matter if x changes its value, the value of
function with variable x.
y remains the same.
“y is dependent on x”
“y is independent of x”

2.3. Relational Operators


Relational operators are used to compare two entities.

Relations Mathematica Syntax

Equality ==

Inequality !=

Less then < Figure 2 Example of relational


Operators
Greater than >

Less than or equal to <=

Greater than or equal >=

6
[Comp-102] Programming Language for Mathematicians (2+1 Cr. Hrs.)
BS(Math) Term-II

2.4. Standard Functions

Functions Mathematica Syntax Functions Mathematica Syntax

Absolute value of x Abs[x] Tangent of z Tan[z]

Square root of x Sqrt[x] Cotangent of z Cot[z]

Base e exponential of x Exp[x] Secant of z Sec[z]

Base e logarithm of x Log[x] Cosecant of z Cse[z]

Base b logarithm of x Log[b,x] Find nth prime num Prime[n]

Sine of z Sin[z] n is prime of not PrimeQ[n]

Cosine of z Cos[z] Generate random RandomInteger[]

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

2.5. Algebraic Manipulation(Symbolic Calculation)


In addition to numerical computations, Mathematica can also does symbolic computation
including the manipulation of algebraic expressions. Mathematica has a number of functions
for this purpose.
• Expand[expr] is used to write the expression expr as a sum of simple terms. For
example
In[1]: = 𝐸𝑥𝑝𝑎𝑛𝑑[(2 𝑥 + 3) (4 𝑥 + 2)]
Out[1]: = 6 + 16 𝑥 + 8 𝑥 A
• Factor[x] factors the polynomial x over the integers. For example
In[2] ≔ 𝐹𝑎𝑐𝑡𝑜𝑟[1 + 2 𝑥 + 𝑥^2]
Out[2]: = (1 + 𝑥)A

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

3. LISTS, SETS, VECTORS AND MATRICES.


3.1. Lists
A list in Mathematica is a collection of objects that can be used to form a vector, a matrix or
a general array. In Mathematica, list is the fundamental data structure used to group objects
together. To enter a list you enclose the list within curly braces and separate each item with a
comma. it is often convenient to collect together several objects, and treat them as a single

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

Some other common functions of list:


In[1]: = 𝑃𝑟𝑒𝑝𝑒𝑛𝑑[𝐴, 10] Prepend can add element to list at front end
In[2] ≔ 𝐴𝑝𝑝𝑒𝑛𝑑[𝐴, 20] Append add element to list at end
In[3]: = 𝐹𝑖𝑟𝑠𝑡[𝐴] Will return first element of list A
In[4] ≔ 𝐿𝑎𝑠𝑡[𝐴] Will return last element of list A
In[5] ≔ Sort[A] Will return sorted list in ascending order
In[6] ≔ Length[A] Will return length of list A

3.1.1. Functions producing lists.


Mathematica provides us with commands of which the output is a list. These commands have
a nature of repetition. These are Range, Table and NestList
• Range[…] function is used to generate list. It can be used with one, two or three
arguments(inputs).
In[1] ≔ 𝑅𝑎𝑛𝑔𝑒[10] It will generate list of first 10 natural
Out[1] ≔ {1,2,3,4,5,6,7,8,9,10} number
In[2] ≔ 𝑅𝑎𝑛𝑔𝑒[10 , 15] Generate list between interval of 10 and 15
Out[2] ≔ {11 , 12 , 13 , 14 , 15}
In[3] ≔ 𝑅𝑎𝑛𝑔𝑒[0 , 3 , 0.5] Generate list between 0 and 3 with interval
Out[3] ≔ {0 , 0.5 , 1 , 1.5 , 2 , 2.5 , 3} of 0.5

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 }

3.2. Vectors and Matrices


Vectors and matrices in the Mathematica are simply represented by lists and by lists of lists,
respectively.
{a, b, c} Vector(a, b, c)
}{𝑎, 𝑏}, {𝑐, 𝑑 }~ 𝑎 𝑏u
𝑚𝑎𝑡𝑟𝑖𝑥 t
𝑐 𝑑
Example:
Creating vector of 3 components

Creating 2x2 matrix

Showing matrix in array form

To represent list in matrix form


MatrixForm[m] method is used.

Showing list in matrix form

10
[Comp-102] Programming Language for Mathematicians (2+1 Cr. Hrs.)
BS(Math) Term-II

3.2.1. Functions for Vectors

𝑇𝑎𝑏𝑙𝑒[𝑓, {𝑖, 𝑛}] build a length-n vector by evaluating f with i=1,2,…,n


𝐴𝑟𝑟𝑎𝑦[𝑎, 𝑛] build a length-n vector of the form {a[1],a[2],…}
𝑅𝑎𝑛𝑔𝑒[𝑛] create the list {1,2,3,…,n}
𝑅𝑎𝑛𝑔𝑒[𝑛€ , 𝑛A ] create the list {n1,n1+1,…,n2}
𝑅𝑎𝑛𝑔𝑒[𝑛€ , 𝑛A , 𝑑𝑛] create the list {n1,n1+dn,…,n2}
𝑁𝑜𝑟𝑚[𝑣] Euclidean norm of a vector

3.2.2. Functions for Matrices.

build an m×n matrix by evaluating f with i ranging


𝑇𝑎𝑏𝑙𝑒[𝑓, {𝑖, 𝑚}, {𝑗, 𝑛}] from 1 to m and j ranging from 1 to n
𝐴𝑟𝑎𝑦[𝑎, {𝑚, 𝑛}] build an m×n matrix with i,j th element a[i,j]
𝐼𝑑𝑒𝑛𝑡𝑖𝑡𝑦𝑀𝑎𝑡𝑟𝑖𝑥[𝑛] generate an n×n identity matrix
𝐷𝑖𝑎𝑔𝑜𝑛𝑎𝑙𝑀𝑎𝑡𝑟𝑖𝑥[𝑙𝑖𝑠𝑡] generate a square matrix with the elements in list on the main diagonal
𝐶𝑜𝑙𝑢𝑚𝑛[𝑙𝑖𝑠𝑡] display the elements of list in a column
𝑀𝑎𝑟𝑖𝑥𝐹𝑜𝑟𝑚[𝑙𝑖𝑠𝑡] display list in matrix form
𝐼𝑛𝑣𝑒𝑟𝑠𝑒[𝑚] Matrix inverse
𝑀𝑎𝑡𝑟𝑖𝑥𝑃𝑜𝑤𝑒𝑟[𝑚, 𝑛] Nth power of matrix m
𝑇𝑟𝑎𝑛𝑠𝑝𝑜𝑠𝑒[𝑚] Find transpose of matrix m
𝐷𝑒𝑡[𝑚] Find determinant of matrix m

Some basic Examples:

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− ,
−𝑏𝑐 + 𝑎𝑑 −𝑏𝑐 + 𝑎𝑑

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.

𝑈𝑛𝑖𝑜𝑛[𝑙€ , 𝑙A , 𝑙S … ] give a list of the distinct elements in the listi


𝐼𝑛𝑡𝑒𝑟𝑠𝑒𝑐𝑡𝑖𝑜𝑛[𝑙€ , 𝑙A , 𝑙S … ] give a list of the elements that are common to all the listi
give a list of the elements that are in universal, but not in any
𝐶𝑜𝑚𝑝𝑙𝑒𝑚𝑒𝑛𝑡[𝑈, 𝑙€ , … ] of the list
i

S𝑢𝑏𝑠𝑒𝑡𝑠[list] give a list of all subsets of the elements in list


𝐷𝑒𝑙𝑒𝑡𝑒𝐷𝑢𝑝𝑙𝑖𝑐𝑎𝑡𝑒[𝑙𝑖𝑠𝑡] delete all duplicates from list
Basic Examples:
In[1] ≔ 𝑈𝑛𝑖𝑜𝑛[{3,4,5}, {1,2,3}] Give a sorted list of distinct values
Out[1] ≔ {1,2,3,4,5}
In[2] ≔ 𝐼𝑛𝑡𝑒𝑟𝑠𝑒𝑐𝑡𝑖𝑜𝑛[{3,4,5}, {1,2,3}] Find elements common to all the lists
Out[2] ≔ {3}
In[3] ≔ 𝐶𝑜𝑚𝑝𝑙𝑒𝑚𝑒𝑛𝑡[{1,2,3,4,5}, {1,2,3}] Find elements that are in first list but not in
Out[3] ≔ {4,5} others.
In[4] ≔ 𝑆𝑢𝑏𝑠𝑒𝑡[{𝑎, 𝑏}] Power set of given list
Out[4] ≔ }{ }, {𝑎}, {𝑏}, {𝑎, 𝑏}~
In[5] ≔ 𝐷𝑒𝑙𝑒𝑡𝑒𝐷𝑢𝑝𝑙𝑖𝑐𝑎𝑡𝑒[{1,1,2,3,3,4,1}] Delete duplicate elements
Out[5] ≔ {1,2,3,4}
4. FUNCTIONS & FUNCTIONAL PROGRAMMING
A function is a block of statements that performs a specific task. Mainly there are two types
of functions i) built in/predefined functions. ii) user defined function.
i. Built-in or Predefined functions:
These are the functions which already have a definition. We just call then
whenever there is a need to use them. There are many functions built into Mathematica
(few of them are already discussed earlier [pg.5]).
ii. Use defined functions:
The functions that we create according to our need. These functions are known
as user defined functions. he we focus on how to create your own defined function.
4.1. Defining functions
Defining functions is one of the strong features of Mathematica. One can define a function in
several different ways in Mathematica. The general form (syntax) of a function definition is:

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

It is recommended that you use function names that start with a


The function name is f lower case letter, so that they don't get confused with names of
built in functions.
The underscore ( _, called blank in Mathematica) means put
x_ Is an argument
anything you like here. The x defines a name for this particular
(Pattern).
anything
:= Set delay assignment The set delay assignment is essential for a function

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:

In[1] ≔ 𝑓 [𝑛‘ ] ≔ 𝑛ˆ2 + 4 Create a user defined function


In[2] ≔ 𝑓 [−2] Calling user defined function
Out[2] ≔ 8
In[3] ≔ 𝑔[𝑥‘ , 𝑦‘ ] ≔ 𝑆𝑞𝑟𝑡[𝑥ˆ2 + 𝑦ˆ2] Another user defined function with
In[4] ≔ 𝑔[3,4] two arguments.
Out[4] ≔ 5

Sometimes, we may need to define a piecewise function in Mathematica. To do this


Piecewise[] command is used. General syntax of defining piecewise functions is:

f[x_]:= Piecewise[{Val1, cond1}, {val2, cond2}...{valn, condn}, val]

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

Problem: Write a recursive solution to find the nth Fibonacci term.


Solution:
In[1] ≔ 𝑚𝑦𝐹𝑖𝑏𝑜𝑛𝑎𝑐𝑐𝑖[1] = 1;
𝑚𝑦𝐹𝑖𝑏𝑜𝑛𝑎𝑐𝑐𝑖[𝑛_] ≔ 𝑚𝑦𝐹𝑖𝑏𝑜𝑛𝑎𝑐𝑐𝑖[𝑛 − 1] + 𝑚𝑦𝐹𝑖𝑏𝑜𝑛𝑎𝑐𝑐𝑖[𝑛 − 2];
𝑚𝑦𝐹𝑖𝑏𝑜𝑛𝑎𝑐𝑐𝑖[5]
Out[1] ≔ 8

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

5.2. Loop Constructs


Looping is a core concept in programming. It is the ability to repeat a certain code
“fast”. Mathematica provides three loops that enable us to repeat part of our codes. These are
quite similar to the loops that exist in any procedural language like C/C++ or JAVA.
5.2.1. Do Loop
The Do function is Mathematica’s equivalent of a for loop. the basic syntax is as:

Do[exp , n] (*Repeat the exp n times*)


Example:
In[1] ≔ 𝐷𝑜[𝑃𝑟𝑖𝑛𝑡[𝑖 ], {𝑖, 1, 5}] Print value of i from 1 to 5
Out[1] ≔ 1
2
3
4
5
In[2] ≔ 𝐷𝑜[𝑃𝑟𝑖𝑛𝑡[𝑛 ^2], {𝑛, 1, 4}] Print n2 for n ={1 to 4}
Out[2] ≔ 1
4
9
16
There are many variations of Do function:

𝐷𝑜[𝑒𝑥𝑝, 𝑛] Evaluate exp n times


𝐷𝑜[𝑒𝑥𝑝, {𝑖, 𝑖–—˜ }] Evaluate exp with the variable i taking on the values 1 through imax
𝐷𝑜r𝑒𝑥𝑝, }𝑖, 𝑖–™š , 𝑖–—˜} ~s Evaluate exp with the variable i start from imin through imax
𝐷𝑜r𝑒𝑥𝑝, }𝑖, 𝑖–™š , 𝑖–—˜} , 𝑠𝑡𝑒𝑝𝑠~s Increment by steps

5.2.2. While Loop


While function is similar to While loop in any programming language. This one operates on a
Boolean (True or False) statement and gives you the ability to repeat a block until the Boolean
statement becomes False.

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

Problem: find all prime numbers less than a given n.

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.

5.2.3. For Loop


For function works just like for loop in any programming language the basic syntax is as
followed:
For[start, condition, incr, body]
(*Executes start , then repeatedly evaluate body and incr until condition remain true*)

Basic Example:

In[1]:= For[ n=1, n<5, n++, Print[n]]


Out[1]:= 1
2 Body of loop
3 increment
4 Start Condition

€ A S š
Problem: find the sum of the sequence €žA + AžS + SžŸ + ⋯ + šž(šž€) where n is given.

Solution:

In[1] ≔ 𝑛 = 𝐼𝑛𝑝𝑢𝑡[“𝑒𝑛𝑡𝑒𝑟 𝑎 𝑛𝑢𝑚𝑏𝑒𝑟”]; Suppose n=10


Sum=0;
𝐹𝑜𝑟[ 𝑖 = 1 , (* Start i=1 *)
𝑖 ≤ 𝑛, (* Condition *)
𝑖++, (* Increment *)
𝑠𝑢𝑚 = 𝑠𝑢𝑚 + 𝑖/(𝑖 + 𝑖 + 1)]; (* Body *)
𝑠𝑢𝑚
Out[1] ≔ 64 157 087
d e
14 549 535

5.2.4. Nested Loops


A loop structure within another loop is called nested loop. In many applications there are
several factors (variables) which change simultaneously, and this calls for what we call a nested
loop. let’s see an example to understand nested loop.

16
[Comp-102] Programming Language for Mathematicians (2+1 Cr. Hrs.)
BS(Math) Term-II

Nested For Loop Nested Do Loop

𝐹𝑜𝑟 [𝑖 = 0, 𝑖 < 3, 𝑖 + +, 𝐷𝑜[


𝐹𝑜𝑟 [𝑗 = 0, 𝑗 < 3, 𝑗 + +, 𝐷𝑜[
𝑃𝑟𝑖𝑛𝑡[ 𝑓[𝑖, 𝑗] ] , {𝑗, 0, 2}
𝑃𝑟𝑖𝑛𝑡r𝑓 [𝑖, 𝑗]s ],
] {𝑖, 0, 2}
] ]

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:

If[condition, True Case, False Case]


(*gives True Case if Condition is true and False Case if Condition evaluate false*)
Example:

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

5.3.2. Switch Statement


In Mathematica the switch statement is a multiway branch statement. It provides an easy way
to dispatch execution to different parts of code based on the value of the expression. it is a
control statement that allows a value to change control of execution. Basic syntax of switch
is:

17
[Comp-102] Programming Language for Mathematicians (2+1 Cr. Hrs.)
BS(Math) Term-II

Switch[expr, Form1, Value1, Form2, Value2, ...]


(*evaluate expr then compare it with each formi in turn and return the valuei corresponding to the match*)
Only the value corresponding to the first form that matches expr is evaluated. If none of the
i i

form match expr, the Switch is returned unevaluated.


i

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:

In[1] ≔ 𝑓 = 𝑥ˆ3 − 8𝑥ˆ2 + 19𝑥 − 8;


𝑃𝑙𝑜𝑡[𝑓, {𝑥, 𝑜, 5}]
Out[1] ≔

18
[Comp-102] Programming Language for Mathematicians (2+1 Cr. Hrs.)
BS(Math) Term-II

In[2] ≔ 𝑃𝑙𝑜𝑡[Sin [𝜋 𝑥 (3-x)] , {x, 0, 3} ]


Out[2] ≔

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:

In[1] ≔ 𝑃𝑙𝑜𝑡[{𝑆𝑖𝑛[𝑥], 𝐶𝑜𝑠[𝑥]} , {𝑥, 0, 10},


𝐴𝑠𝑝𝑒𝑐𝑡𝑅𝑎𝑡𝑖𝑜 → 0.5,
𝑃𝑙𝑜𝑡𝐿𝑒𝑔𝑒𝑛𝑑𝑠 → Expressions,
𝐴𝑥𝑒𝑠𝑆𝑡𝑦𝑙𝑒 → 𝑇ℎ𝑖𝑐𝑘,
𝐹𝑖𝑙𝑙𝑖𝑛𝑔 → 𝐴𝑥𝑖𝑠 ,
𝑃𝑙𝑜𝑡𝐿𝑎𝑏𝑒𝑙𝑠 → {A,B},
𝑃𝑙𝑜𝑡𝑅𝑎𝑛𝑔𝑒 → 𝐴𝑢𝑡𝑜𝑚𝑎𝑡𝑖𝑐,
𝑃𝑙𝑜𝑡𝑆𝑡𝑦𝑙𝑒 → {𝐿𝑖𝑔ℎ𝑡𝐵𝑙𝑢𝑒, 𝐿𝑖𝑔ℎ𝑡𝑂𝑟𝑎𝑛𝑔𝑒}
]
Out[1] ≔

19
[Comp-102] Programming Language for Mathematicians (2+1 Cr. Hrs.)
BS(Math) Term-II

6.1.1. Polar Plot


PolarPlot function generates a polar plot of a curve with radius r as a function of angle θ. Its
basic syntax is:
PolarPlot[r, { θ, θ min, θ max}]

• The angle θ is measured in radians, counterclockwise from the positive x axis.


• The x, y position corresponding to r, θ is r cos(θ), r sin(θ). The value of θ need not be
between 0 and 2Pi.

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 collection of lines.

𝐼𝑛[2]: = 𝐺𝑟𝑎𝑝ℎ𝑖𝑐𝑠3𝐷 t𝐿𝑖𝑛𝑒r } {1,0,1}, {2,1,2}, {3,0,1}, {4,2,0}~su

• 𝑪𝒊𝒓𝒍𝒄𝒆[ {𝒙, 𝒚, }, 𝒓 ] 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

• 𝑪𝒊𝒓𝒄𝒖𝒎𝒔𝒑𝒉𝒆𝒓𝒆[ {𝒑𝟏 , … , 𝒑𝒏ž𝟏 } ] 𝐼𝑛[1]: = 𝐺𝑟𝑎𝑝ℎ𝑖𝑐𝑠 t 𝐶𝑖𝑟𝑐𝑢𝑚𝑠𝑝ℎ𝑒𝑟𝑒r }{0,0}, {1,0}, {0,1}~ su


Give the sphere that circumscribes
the points pi
• It is also known as circumcircle,
circumscribed circle, or
circumscribed disk.
𝐼𝑛[2]: = 𝐺𝑟𝑎𝑝ℎ𝑖𝑐𝑠3𝐷 t 𝐶𝑖𝑟𝑐𝑢𝑚𝑠𝑝ℎ𝑒𝑟𝑒r }{0,0,0}, {1,0,0}, {0,1,0}, {0,1,1}~ su

• 𝑹𝒆𝒄𝒕𝒂𝒏𝒈𝒍𝒆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

• 𝑻𝒓𝒊𝒂𝒏𝒈𝒍𝒆[ {𝒑𝟏 , 𝒑𝟐 , 𝒑𝟑 } ] 𝐼𝑛[1]: = 𝐺𝑟𝑎𝑝ℎ𝑖𝑐𝑠r}𝑂𝑟𝑎𝑛𝑔𝑒, 𝑇𝑟𝑖𝑎𝑛𝑔𝑙𝑒r }{0,0}, {1,0}, {1,1}~s ~s


Represent a filled triangle with
corner points 𝑝€ , 𝑝A 𝑎𝑛𝑑 𝑝S
• It can be used as a geometric region
and a graphics primitive.

𝐼𝑛[2]: = 𝐺𝑟𝑎𝑝ℎ𝑖𝑐𝑠3𝐷 t 𝑇𝑟𝑖𝑎𝑛𝑔𝑙𝑒r }{0,0,0}, {1,0,0}, {0,1,1}~ su

• 𝑷𝒐𝒍𝒚𝒈𝒐𝒏[ {𝒑𝟏 , … , 𝒑𝒏 } ] 𝐼𝑛[1]: = 𝐺𝑟𝑎𝑝ℎ𝑖𝑐𝑠r}𝑂𝑟𝑎𝑛𝑔𝑒, 𝑃𝑜𝑙𝑦𝑔𝑜𝑛r }{0,0}, {2,0}, {2,2}, {0,2}~s ~s


Represent a filled polygon with
points 𝑝™
• 𝑹𝒆𝒈𝒖𝒂𝒍𝒓𝑷𝒐𝒍𝒚𝒈𝒐𝒏[ 𝒏 ]
gives the regular polygon with n
vertices equally spaced around the 𝐼𝑛[2]: = 𝐺𝑟𝑎𝑝ℎ𝑖𝑐𝑠[{ 𝑂𝑟𝑎𝑛𝑔𝑒, 𝑅𝑒𝑔𝑢𝑙𝑎𝑟𝑃𝑜𝑙𝑦𝑔𝑜𝑛[ 8 ]}]
unit circle

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

7.2. Writing your own package


The basic syntax of writing a Mathematica package is:
BeginPackage[“package name`”]
Public-functions-usage-massage
Begin[“`Private`”]
Functions Code here
End[]
EndPackage[]
Example:

BeginPackage["SimpleArithmetic`"]

AddTwo::usage = "AddTwo[a, b] returns a+b";


AddThree::usage = "AddThree[a, b, c] returns a+b+c";
TimesTwo::usage = "TimesTwo[a, b] returns a*b";
TimesThree::usage = "TimesThree[a, b, c] returns a*b*c";

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

You might also like