17
Introducing Mathematica Functional Programming & Data Structures

SSG314 Lecture 01 Mathematica Intro

Embed Size (px)

DESCRIPTION

First Lecture on Algorithms and Data Structures SSG 314 University of Lagos. Continue from OOP treatment of SSG 313

Citation preview

Page 1: SSG314 Lecture 01 Mathematica Intro

IntroducingMathematica

Functional Programming & Data Structures

Page 2: SSG314 Lecture 01 Mathematica Intro

Next few weeks, we will use Mathematica as an example of a functional programming language.

Along the way, we shall be introduced to exact computations and Symbolic Algebra. We will also be able to use the numerical and graphical capabilities to do engineering computations. Its rich compendium of mathematical functions and algorithms will be demonstrated.

Required Tools

Page 3: SSG314 Lecture 01 Mathematica Intro

The following will be needed A fully licensed copy of Mathematica 7 or 8. A pdf copy of the Mathematica Book, version

5. A copy of the Mathematica CookbookWith these, it should be a straightforward matter to practice the required examples before coming to class. That way, you will be able to ask more useful questions and gain more for the time we spend together in class. Acquaint yourself with the Mathematica

Documentation system installed with your copy of the software. In most cases, you can find illustrated answers to your questions from this system.

Page 4: SSG314 Lecture 01 Mathematica Intro

Mathematica built-in functions always begin with capital letters. The function arguments are in (square) brackets.o Examples: respectively mean: ,

o Notice that in , the space implies The space is not always necessary as will be correctly interpreted but will be taken as a new identifier rather than the product If the latter is your intention, then put a space, a dot or an asterisk to indicate so.

Notational Conventions

Page 5: SSG314 Lecture 01 Mathematica Intro

Lists are created using braces and nested braces. They can be indexed into via double brackets. For example:We create a double list here and call it M. We then askMathematica to display it in MatrixForm. We can pickthe second element – in thiscase also a list. We look at the first element of this sub-list and we obtain 3. Finally, we ask for the determinant of the matrix.

List Processing

Page 6: SSG314 Lecture 01 Mathematica Intro

You can use the equals sign for arithmetic assignment as you have been doing in your C Sharp programming. It works exactly the same way and it will assign the result of the computation on the right to the identifier on the left.

A new twist is that there can be undefined identifiers on the RHS. Mathematica will process that symbolically.

A colon with an assignment requests a delay in the processing. Recall the previous matrix above.

A double equals (==) is used for an equation. The following example shows a Mathematica set of equations:

Assignments & Equations

Page 7: SSG314 Lecture 01 Mathematica Intro

As you can see in the above equations, they may be linear or nonlinear, Mathematica will solve them if their solutions can be found. Note the (==)

Simultaneous Equations

Page 8: SSG314 Lecture 01 Mathematica Intro

The list of built-in functions is quite impressive. They contain ALL elementary functions and most special functions. It is difficult to imagine an important mathematical function that is left out. Even if this happens, it is possible to connect Mathematica to your C-Sharp code and define new functions.

More about that later.

Built-In functions

Page 9: SSG314 Lecture 01 Mathematica Intro

The following numerical types are supported in Mathematica: Integers, Real, Rational and Complex.

The Mathematica function Head[] can tell you the underlying structure of the result of any expression as you can see here.

Exact results are displayed in their entirety when possible or symbolically when full display would be impossible due to the infinity of the exact representation.

Numerical Types

Page 10: SSG314 Lecture 01 Mathematica Intro

In the example shown here, we can ask Mathematica to add two numbers using our usual addition syntax. We could even add undefined elements, in this case Mathematica assumes a symbolic manipulation and provides whatever answers are reasonable. The full form of these actions are actually functions as the last example shows. Try and interprete:

Times[9,5,3], Divide[Sin[x],x].

Full Forms of Expressions

Page 11: SSG314 Lecture 01 Mathematica Intro

Limits Example

You always knew this didn’t you? That both sine as well as the tangent of x tend to x as x approaches zero. You can use a simple Mathematica plot to settle this matter for any doubter as shown!

Page 12: SSG314 Lecture 01 Mathematica Intro

You may do exact computations with Mathematica. At other times you may want to perform numerical approximations for several reasons. Accuracy and precision will become important issues when you do this.

For most purposes, treat precision as the total number of digits in the decimal representation of a number and accuracy as the total number of digits after the decimal

Precision & Accuracy

Page 13: SSG314 Lecture 01 Mathematica Intro

There can be a variety in the ways you can supply a computation request to Mathematica. Some of these are shown with simple examples. The infix is the way of most operators but not limited to them.o Functional notation: Divide [Sin[x],x], N[Sqrt[2]]o Infix notation: Sin[x]/xo Postfix notation: Sqrt[2]//No Prefix notation: N@1/2

Notational Variety

Page 14: SSG314 Lecture 01 Mathematica Intro

We introduce Interval Arithmetic here by way of a simple example:

Interval Arithmetic

Page 15: SSG314 Lecture 01 Mathematica Intro

The equivalency of the two types of interval calculation above can be further explained as follows:

Apply replaces the headof a list with a new head –in this case, the Subtract. In the above form, the Applycommand was used in itsprefix format.

Infix Apply

Page 16: SSG314 Lecture 01 Mathematica Intro

I can seek assistance by using the double question mark: ??Integrate prompts the system to give assistance on the Integrate[] function.

I can also ask Mathematica to show me how to use the Sin[] function in the Prefix format by

Which immediately shows the required format.

Asking for help

Page 17: SSG314 Lecture 01 Mathematica Intro

Conclude this introductory class with a simple graphical project by a student three years ago.

Let this stimulate your mind to what you can do in your course project.

Mathematica can be used to create useful products by students who are smart enough to go beyond the requirements of the simple examples and class practice.

Graphics Project