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

Mathematica As A Calculator

Mathematica can be used as a powerful calculator. It can handle computations on numbers of any size, perform algebraic manipulations, solve equations symbolically, compute integrals and derivatives, and plot functions in 2D and 3D. Mathematica accesses sophisticated algorithms to handle a wide range of numerical and symbolic computations efficiently.

Uploaded by

Zafar Iqbal
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
304 views

Mathematica As A Calculator

Mathematica can be used as a powerful calculator. It can handle computations on numbers of any size, perform algebraic manipulations, solve equations symbolically, compute integrals and derivatives, and plot functions in 2D and 3D. Mathematica accesses sophisticated algorithms to handle a wide range of numerical and symbolic computations efficiently.

Uploaded by

Zafar Iqbal
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 73

Mathematica as a Calculator

You can use Mathematica just like a calculator: you type in questions, and Mathematica prints back answers.
Here is a simple computation. Press  to tell Mathematica to evaluate the input you have given.

6^20
3656158440062976

Mathematica automatically handles numbers of any size.

6^200
42682522381202740079697489151877373234298874535448942949547907893511292954961973901907213934075709729681281546667612983095446524051759524238401
5591919845376

You can enter computations in standard mathematical notation, using palettes or from the keyboard. How this works is discussed below.

6200
42682522381202740079697489151877373234298874535448942949547907893511292954961973901907213934075709729681281546667612983095446524051759524238401
5591919845376


This tells Mathematica to work out the eigenvalues of a matrix.

 1 2 3



Eigenvalues 4 5 6
7 8 9
3 3
0, 5 33 , 5 33
2 2
  
An important feature of Mathematica is its ability to handle formulas as well as numbers.


This asks Mathematica to solve an equation. The answer is a formula that depends on a parameter.

 
Solve

x 
 
1
 x a 2

1 4 a 
x, x

1 8 a , x
1
1 4 a  1 8 a


8 8


This asks Mathematica to evaluate an integral.

  
  
 
x  a x 
x

a x x3 2 1 2
ax   a Log x a x
4 2 4

You can use Mathematica to make 2D and 3D graphics.


 
Here is a 2D plot of a simple function.

Plot Sin x Sin 1.6


x , x, 0, 40

 Graphics 

 
Here is a 3D plot. The space between the x and y indicates multiplication. PlotPoints30 specifies the mesh to use.

Plot3D Sin x y , x, 0, 4 , y, 0, 4 , PlotPoints  30

 SurfaceGraphics 

You can access many of the calculator features of Mathematica just by pushing buttons in standard palettes. Click this hyperlink to check out the Basic Calculations palette.

Power Computing with Mathematica


Even though you can use it as easily as a calculator, Mathematica gives you access to immense computational power.





This creates a 100×100 matrix of random numbers. The semicolon at the end tells Mathematica not to print the matrix.

m  Table Random , 100 , 100 ;


 

On most computers it takes Mathematica under a second to compute all the eigenvalues of the matrix and plot them.

ListPlot Abs Eigenvalues m

 Graphics 
Mathematica can handle numbers of any size. On most computers Mathematica takes under a second to compute the exact factorial of
1000.

1000
40238726007709377354370243392300398571937486421071463254379991042993851239862902059204420848696940480047998861019719605863166687299480855890132
3829669944590997424504087073759918823627727188732519779505950995276120874975462497043601418278094646496291056393887437886487337119181045825783
6478499770124766328898359557354325131853239584630755574091142624174743493475534286465766116677973966688202912073791438537195882498081268678383
7455973174613608537953452422158659320192809087829730843139284440328123155861103697680135730421616874760967587134831202547858932076716913244842
6236131412508780208000261683151027341827977704784635868170164365024153691398281264810213092761244896359928705114964975419909342221566832572080
8213331861168115536158365469840467089756029009505376164758477284218896796462449451607653534081989013854424879849599533191017233555566021394503
9973628075013783761530712776192684903435262520001588853514733161170210396817592151090778801939317811419454525722386554146106289218796022383897
1476088506276862967146674697562911234082439208160153780889893964518263243671616762179168909779911903754031274622289988005195444414282012187361
7459926429565817466283029555702990243241531816172104658320367869061172601587835207515162842255402651704833042261439742869330616908979684825901
2545832716822645806652676995865268227280707578139185817888965220816434834482599326604336766017699961283186078838615027946595513115655203609398
8180612138558600301435694527224206344631797460594682573103790084024432438465657245014402821885252470935190620929023136493273497565513958720559
6542287497740114133469627154228458623773875382304838656889764619273838149001407673104466402598994902222217659043399018860185665264850617997023
5619389701786004081188972991831102117122984590164192106888438712185564612496079872290851929681937238864261483965738229112312502418664935314397
0137428531926649875337218940694281434118520158014123344828015051399694290153483077644569099073152433278288269864602789864321139083506217095002
5973898635542771967428222487575867657523442202075736305694988250879689281627538488633969099598262809561214509948717012445164612603790293091208
8908694202851064018215439945715680594187274899809425474217358240106367740459574178516082923013535808184009699637252423056085590370062427124341
6909004153690105933983835777939410970027753472000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000

Mathematica can do numerical computations to any precision you specify. Here is pi to 500 digits of precision.

N , 500
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725
3594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133
9360726024914127372458700660631558817488152092096282925409171536436789259036001133053054882046652138414695194151160943305727036575959195309218
61173819326117931051185480744623799627495673518857527248912279381830119491




Mathematica routinely handles algebraic calculations that would have taken years by hand. Here is a polynomial factoring problem.

  
 
 
Factor x99 y99


x  y x2  x y  y2 x6  x3 y3  y6 x10  x9 y  x8 y2  x7 y3  x6 y4  x5 y5 x4 y6  x3 y7  x2 y8  x y9  y10
x20  x19 y  x17 y3 x16 y4  x14 y6  x13 y7  x11 y9  x10 y10  x9 y11  x7 y13  x6 y14  x4 y16  x3 y17  x y19  y20
x60  x57 y3  x51 y9  x48 y12  x42 y18  x39 y21  x33 y27  x30 y30  x27 y33  x21 y39  x18 y42  x12 y48  x9 y51  x3 y57  y60



Mathematica calls on sophisticated algorithms to simplify formulas. The % symbol stands for the previous result.

Simplify %
x99  y99

Mathematica has achieved world records—for both size and speed—in many kinds of computations.


Here is a computation in number theory; larger versions of this computation, which took Mathematica only a few minutes, had never been done before.

PartitionsP 8000
78360264351568349490593145013364599719010769352985864331118600209417827764524450990388402844164
Accessing Algorithms in Mathematica
Whenever you use Mathematica, you are accessing the world's largest collection of computational algorithms.



 

Mathematica automatically chooses appropriate algorithms for each computation.

FindRoot Cos x  x  Log x , x, 1


x  0.840619

NIntegrate Log x Sin x , x, 0, 2

 
0.555889

    
NSolve x^5 6
x^3 8x 1  0, x
x  2.05411 , x  1.2915 , x  0.126515 , x  1.55053 , x  1.9216

 
 
Mathematica uses state-of-the-art algorithms for factoring integers. The result is given as a list of factors and exponents.




FactorInteger 2^105 1
7, 2 , 31, 1 , 71, 1 , 127, 1 , 151, 1 , 337, 1 , 29191, 1 , 106681, 1 , 122921, 1 , 152041, 1

 

This generates a 2D table corresponding to a matrix.

 
   
m  Table 2^i  x^j, i, 3 , j, 4
2  x, 2  x2, 2  x3, 2  x4 , 4  x, 4  x2, 4  x3, 4 x4 , 8  x, 8  x2, 8  x3, 8  x4
 
This displays the table in matrix form.


MatrixForm m

 
2  x 2  x2 2  x3 2  x4
4  x 4  x2 4  x3 4  x4
8  x 8  x2 8  x3 8  x4


 
This computes the null space of the matrix.

 
NullSpace m
x  x2, 1  x  x2, 0, 1 , x, 1  x, 1, 0

Mathematica can solve differential equations both symbolically and numerically.




 



Here Mathematica solves a nonlinear differential equation numerically. The answer is an interpolating function that implicitly represents the whole solution.

 
 

NDSolve x'' t x t ^3  Sin t , x 0  x' 0 0 ,
x, t, 0, 50
x  InterpolatingFunction 0., 50. , 





Here is a parametric plot of the solution. The /. tells Mathematica to substitute the solution for x.

ParametricPlot Evaluate x t , x' t . % , t, 0, 50

 Graphics 
Mathematical Knowledge in Mathematica
Mathematica incorporates the knowledge from the world's mathematical handbooks — and uses its own revolutionary algorithms to go much further.


Mathematica knows about all the hundreds of special functions in pure and applied mathematics.

 

LegendreQ 3, x
2 5 x2 3 5 x2 1 x
  x 1 Log
3 2 4 3 1 x

  
Mathematica can evaluate special functions with any parameters to any precision.

N MathieuC 1  I, 2 I, 3 , 40
3.925131137412519864349764616815837920363  1.898823911543347241105274797143911577679 I


 
Mathematica is now able to do vastly more integrals than were ever before possible for either humans or computers.

 

 
4

 

 
 
x ArcTan x 

x 
 
x

1   2 2 x 1 2 2 x 2 32 Log 1  2 x x Log 1  2 x x

 
  2 ArcTan  2 ArcTan  x ArcTan x  
3 3 2 3 2 3 3 2 3 2


 


Log x Exp x3 
x
0
1 1


 Gamma 6 EulerGamma  3   9 Log 3
54 3

 
 



Sin x2 Exp x 
x
0
1 1 3 5 1 1
   Cos  2 HypergeometricPFQ 1 , , ,   Sin
2 2 4 4 4 64 4

Mathematica can also evaluate finite and infinite sums and products.
n 1


k1 k
6

6 1
 PolyGamma 5, 1  n
945 120


 
 

Look at the Integrals demo to see more examples.
Mathematica can solve a wide range of ordinary and partial differential equations.




 



y x  Ex



DSolve y'' x y' x x y x  0, y x , x
2
AiryBi 1
13

1
4
x C 1  AiryAi 1
1 3

1
4
x C 2



Mathematica's algorithms can generate a huge range of mathematical results.
5 2n
FullSimplify Gamma

 
n1 5
2
12 



25 5

Log 2  Zeta 3  2



True



TrigReduce Cos x
1
4

3  4 Cos 2 x  Cos 4 x
8


This finds the billionth prime number, using a mixture of algorithms and built-in tables.

Prime 109
22801763489
Building Up Computations
Being able to work with formulas lets you easily integrate all the parts of a computation.


 


1


Here are the eigenvalues of a matrix of numbers.


Eigenvalues

9  17 ,
3, 1 , 2, 6
1
9  17
2 2


 

Mathematica can still compute the eigenvalues even when symbolic parameters are introduced. The following expression is a compact representation of the eigenvalues for any
value of b.


 
1
2 
 


v  Eigenvalues

3 b 
3, 1 , 2, b

17  6 b  b2 ,
1
2
3 b  17  6 b  b2

Mathematica's functions are carefully designed so that output from one can easily be used as input to others.

  
This takes the formula for the eigenvalues and immediately plots it.

Plot Evaluate v , b, 10, 10

 Graphics 
 
You can solve for the value of b at which the first eigenvalue is zero...


Solve First v  0, b

b
2
3

 

or find the integral from 0 to c.

  
   
c


int  First v 
b

   
0

3 17 3 1 2 3  c
  2 ArcSinh  c 3 17  6 c  c2  c 6  17  6 c  c2  8 ArcSinh
4 2 2 4 2 2



 
This finds the series expansion of the result.




   
Series %, c, 0, 5
1 1 3 2 c3 3 c4 14 c5
6 2 17 c  1 c2    O c
6
4 4 17 51 17 578 17 24565 17

 
This searches numerically for a root.

 
FindRoot int  1 c, c, 1
c  0.554408

Being able to work with formulas is also important in summarizing data.


 

This generates a table of the first 40 primes.

 
Table Prime i , i, 40
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71,
73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173

 
Fit produces an approximate formula.


Fit %, Log x , x, x2 , x
3.91288 x  0.0244903 x2  5.98809 Log x

 

This computes the sum of the first 40 primes using the approximate formula.

Sum %, x, 1, 40
3090.16



Here is the exact result.

Sum Prime i , i, 1, 40
3087


 





The plot puts everything together and makes a plot of the difference between exact and approximate results for sums of up to 50 primes.
imax
ListPlot Table Sum Evaluate Fit Table Prime i , i, imax , Log x , x, x2 , x , x, imax  Prime i , imax, 2, 50 ,
i1
PlotJoined  True

 Graphics 
Handling Data
Mathematica lets you import data in any format, then manipulate it using powerful and flexible functions.

 
Click here to change to the directory that contains the data files used in this section.
This reads image data from the file image.dat. The semicolon tells Mathematica not to print the result.

data  ReadList "image.dat", Number, RecordLists  True ;


This visualizes the data as a density plot.

ListDensityPlot data, Mesh  False, FrameTicks  None 

DensityGraphics


You can apply any Mathematica function to the data.

ListDensityPlot Exp Sqrt data , Mesh  False, FrameTicks  None, ColorFunction  Hue

DensityGraphics

 
Here the data is successively shifted to the right.


ListDensityPlot MapIndexed RotateRight, data , Mesh  False, FrameTicks  None, ColorFunction  Hue

DensityGraphics


Here is a contour plot of the data.

ListContourPlot data, ContourShading  False, Contours  6, FrameTicks  None 

ContourGraphics

 
  



This plots the data keeping only some Fourier components.

ListDensityPlot Re InverseFourier
MapIndexed If Max #2  60, #, 0 &, Fourier data , 2 
, Mesh  False, FrameTicks  None

DensityGraphics



This is the distribution of gray levels in the data.

ListPlot Sort Flatten data


200

150

100

50

2000 4000 6000 8000

Graphics


Here is a 3D plot based on the data.

ListPlot3D data, ColorFunction  Hue,


Mesh  False, ViewPoint  0.2, 2, 5 

80

60

40

20

200
150
100
50
0
20 40 60 80 100

SurfaceGraphics

Mathematica can work with data of any kind — not just numbers.

 
Click here to change to the directory that contains the data files used in this section.
This reads in all the words in a dictionary.

data  ReadList "dictionary.dat", String ;

 
Here are the first 40 words in the dictionary.


Take data, 40


a, AAA, AAAS, Aarhus, Aaron, ABA, Ababa, aback, abacus, abalone, abandon, abase, abash, abate,
abbas, abbe, abbey, abbot, Abbott, abbreviate, abc, abdicate, abdomen, abdominal, abduct, Abe, abed, Abel,
Abelian, Abelson, Aberdeen, Abernathy, aberrant, aberrate, abet, abetted, abetting, abeyance, abeyant, abhorred

 
 

This selects words that are palindromes with length more than 2.

 
Select data, #  StringReverse # && StringLength # 2 &
AAA, ABA, ala, AMA, ana, bib, bob, bub, CDC, civic, dad, deed, did, DOD, dud, eke, ere, eve, ewe, eye, gag, gig, gog, huh, iii, level, madam, minim, mum,
non, noon, nun, pap, PDP, peep, pep, pip, poop, pop, pup, radar, refer, rever, rotor, sis, s's, tat, teet, tenet, tit, TNT, toot, tot, wow

 
This finds the lengths of all the words in the dictionary.

wordLengths  Map StringLength, data ;

 
  


This counts the total number of words of each length.

Table Count wordLengths, i , i, Max wordLengths



26, 131, 775, 2152, 3093, 3793, 3929, 3484, 2969, 1883, 1052, 542, 260, 102, 39, 15, 6, 4, 0, 1, 2, 1

Here is a plot of the length distribution.

ListPlot %, PlotStyle  PointSize 0.02


4000

3000

2000

1000

5 10 15 20

Graphics

Visualization with Mathematica
Mathematica makes it easy to create stunning visual images.








  

This creates a 3D parametric plot with automatic choices for most options.

ParametricPlot3D u Cos u 4 Cos v u , u Sin u 4 Cos v u , u Sin v u , u, 0, 4  , v, 0, 2  , PlotPoints  60, 12

 Graphics3D 

 

Here is the same plot with a variety of specific choices for options.

  
 
Show %, PlotRange  10, 0 , FaceGrids  All, BoxRatios  1, 1, .5 , FaceGrids 
Ticks  None, AxesLabel  1, 1, 1
0, 0, 1 , 0, 1, 0 , 1, 0, 0 , ViewPoint  1, 3, 2 ,

 Graphics3D 
Mathematica includes primitives from which you can build up 2D and 3D graphics of any complexity.


 


This generates a long list of point primitives.

gr  Flatten Table Point


p
q
, Denominator
p
q
, q, 100 , p, q 1 ;



Here are the first five elements in the list.





Take %, 5

Point 

1



,2 , Point
1
,3 , Point
2
,3 , Point
1
,4 , Point
1
,2
2 3 3 4 2

 
This shows the graphics corresponding to the list of primitives.

Show Graphics gr, Frame  True

 Graphics 









This generates a list of 3D cuboid primitives.

gr  Flatten Table If Mod Multinomial x, y, z , 2 1,


Cuboid 1.2x, y, z , , x, 0, 15 , y, 0, 15 , z, 0, 15 ;

Here are the first five elements in the list.




Take %, 5




Cuboid 0, 0, 0 , Cuboid 0, 0, 1.2 , Cuboid 0, 0, 2.4 , Cuboid 0, 0, 3.6 , Cuboid 0, 0, 4.8

 
This shows the graphics corresponding to the list of primitives.

Show Graphics3D gr

 Graphics3D 
Mathematica lets you produce animated movies as well as static graphics.



   


Double click the graphic to start the animation.

Do Plot3D Sin 2 x Sin 2 y Cos t , x, 0,  , y, 0,  , PlotRange  1, 1 , BoxRatios  1, 1, 1 , Ticks  None , t, 0, ,  6 ;


 








 


 
Here is another animation.

Do ParametricPlot3D Cos i

PlotPoints  20,

50
Cos  Sin i

PlotRange 

50
Sin  , Sin  , Cos  , , 0, 2  , , 0, 2  , Axes  None, Boxed  False,

1.5, 1.5 , 1.1, 1.1 , 1.1, 1.1 , i, 0, 25 ;


Mathematica also lets you generate sound.



Play Sin

 Sound 
t
2
24
0
If

12
t 

12

1 1  36
,
4 24

1  
32 24t 2

 
This plays a sound with the specified waveform. Assuming that your computer supports sound output, you can hear the sound immediately by double clicking the graphic.

Sin 1625 t 13000 t , 0 , t, 0, 2 , PlayRange  All

For more examples of sounds, look at the Sound Gallery.

Mathematica has made possible many new kinds of scientific, technical and artistic images.
Follow these links to see more examples:
• 2D Graphics
• 3D Graphics
• Art Images
• Animations
• Diagrams and Objects
Mathematica Notebooks
Every Mathematica notebook is a complete interactive document combining text, tables, graphics, calculations, and other elements.
This document is a notebook.
Your notebooks are automatically organized in a hierarchy of cells.
You can close groups of cells so you see only their headings.
You can use hyperlinks to jump within a notebook or between notebooks.
You can assign each cell a style from a style sheet.
Your Mathematica notebooks are automatically retargeted for screen or printout—optimizing fonts and layout for each medium.
Mathematica provides hundreds of options that allow you to give notebooks any look you want and to generate full publication-quality documents.
Everything in the Mathematica Help Browser is a notebook — including the complete online Mathematica Book.

Here is some ordinary text. It can be in any font, face, size, color, and so on. You can have special characters such as  as well as formulas such as



embedded in text.


Mathematica makes it easy to set up tables and arrays.
2      2  3
3   3  2   2    2

Mathematica lets you set up spacing and justification for text.


Like other objects in Mathematica, the cells in a notebook, and in fact the whole notebook itself, are all ultimately represented as Mathematica expressions. With the
standard notebook front end, you can use the command Show Expression to see the text of the Mathematica expression that corresponds to any particular cell.
Like other objects in Mathematica, the cells in a notebook, and in fact the whole notebook itself, are all ultimately represented as Mathematica expressions. With the
standard notebook front end, you can use the command Show Expression to see the text of the Mathematica expression that corresponds to any particular cell.
The Mathematica language can be used to specify all aspects of notebooks.
Here is a typical cell in a notebook.
This is a typical cell.
This is how Mathematica represents the cell.
Cell["This is a typical cell.", "Text",
CellFrame->True,
FontWeight->"Bold",
FontSlant->"Italic",
Background->RGBColor[0, 1, 1],
CellTags->"T.8"]

Mathematica notebooks can be built up using explicit commands as well as interactively.

 
 

This tells Mathematica to print three cells in subsubsection style.

Do StylePrint StringJoin "Heading ", ToString i , "Subsubsection" , i, 3

Heading 1
Heading 2
Heading 3
Palettes and Buttons
Palettes and buttons provide a simple but fully customizable point-and-click interface to Mathematica.
Mathematica comes with a collection of ready-to-use standard palettes.



Here is part of the Basic Calculations palette.


.




Cross , 


Outer , , 


ListConvolve , 



ListCorrelate , 



Tr 


Det 


Inverse 


Transpose 



Eigenvalues 
Eigenvectors 
LinearSolve , 
RowReduce 

Here is the International Characters palette for European characters.

à á â ã ä å æ ç
è é ê ë ì í î ï
ð ñ ò ó ô õ ö
ø ù ú û ü ý ÿ þ
ß À Á Â Ã Ä Å Æ
Ç È É Ê Ë Ì Í Î
Ï Ð Ñ Ò Ó Ô Õ
Ö Ø Ù Ú Û Ü Ý Þ
€ £ ¥ « » ¿ ¡
Palettes work like extensions to your keyboard.
In a palette like this, clicking the  button inserts an  into the notebook.






21 
21 

Here is a working version of the palette.

 
 


In a palette like this, the  indicates where the current selection should be inserted.

 Log 
2 Exp 



 

Clicking the button takes the highlighted selection and wraps a square root around it.


1 Sin x Cos x

1  Sin x Cos x




Here is a working version of the palette.

2
 Log 
Exp 
It is easy to create your own custom palettes.



 
You can create a blank palette using the Create Table/Matrix/Palette item in the Input menu.

Darken 
EdgeSelect 

Lighten 





You can create custom palettes to do any function or manipulate any expression.


Expand 
Factor 
Simplify 


Clicking the button immediately factors in place the expression you have selected.

1  a^2 2 a b  b^2  p q ^2
1  a  b 2  p q ^2





Here is a working version of the palette:


Expand 
Factor 
Simplify 

Follow this link to see other examples of palettes.


Mathematical Notation
Mathematica notebooks fully support standard mathematical notation—for both output and input.
Mathematica combines the compactness of mathematical notation with the precision of a computer language.

4

  



Here is an integral input using only ordinary keyboard characters.

Integrate Log 1 x
x  4 ArcTan x 2
Sqrt x , x
x Log 1  x



Here is the same integral entered in 2D form with special characters. You can enter this form using a palette or directly from the keyboard.

  
Log 1 



4   4 ArcTan  2  Log 1  


This shows the keys you need to type to get the input above. The symbol  stands for the  key.

intLog 1 x 
   2
x ddx


 
 

Mathematica always lets you edit output—and use it again as input.

4   4 ArcTan  2  Log 1  

4   4 ArcTan  2 2  Log 1 


Mathematica can generate output in traditional textbook form. Note that Mathematica's StandardForm is precise and unambiguous whereas TraditionalForm requires heuristics for


interpretation. This asks Mathematica to compute the integral and display the result in TraditionalForm.



Log 1 

 TraditionalForm

4tan1  2 log 1 4 





Mathematica produces top-quality output for formulas of any size or complexity.


Cos 

        
4

      

 
 

TraditionalForm
22  2 

     
0


 

 

  

  


4 4 4 4

 
1 
F2 ; 1, 1  ; 1  1
F2  ; 1,  1; 1  1 
F2 ; 1, 1  ; 1  1
F2  ; 1, 1; 1  4       
F2 ; 1, 1  ;  1 3 4  1 
1  F2 ; 1,  1;  1 3 4  1 
F2 ; 1, 1  ;  1 3 4  1 
F2 ; 1, 1;  1 3 4 
4      

Look at the Formula Gallery for other examples of mathematical formulas generated by Mathematica.

 
Mathematica makes it easy to work with abstract notation.

 
6i
Table i i ,
i
i, 6
5 4 3 2 1 0
1  1 ,
1
2 2  ,
2
3 3  ,
3
4 4  ,
4
 5 5  ,
5
6 6  
6

Mathematica supports over 700 special characters with new fonts optimized for both screen and printer. You can find all these characters in the Complete Characters palette. All of
them have consistent full names; some also have aliases, as well as TeX and SGML names.
Mathematica and Your Computing Environment
Mathematica runs compatibly across all major computer systems, and lets you exchange data in many standard formats.
The standard Mathematica system consists of two parts:

 The kernel—which actually does computations.


 The front end—which handles user interaction and notebooks.
Mathematica notebooks are completely compatible across computer systems.

From within one notebook you can run several Mathematica kernels—on local or remote computers.
Mathematica notebooks allow importing and exporting of many formats.
You can export graphics and formulas to other programs in EPS, GIF, and so on, and then manipulate them.

Complete Mathematica notebooks can be exported in formats such as HTML, TeX, and RTF.
Notebook files contain only plain text and are completely portable.


Choose Show Expression from the Format menu to see the expression form of cells in this notebook.


Here is a typical cell in a Mathematica notebook.
Log 1 




This is what you get when you copy the integral into an external text application such as email.
\!\(\[Integral]\(Log[1 +
\[Xi]]\/\@\[Xi]\)\[DifferentialD]\[Xi]\)

Mathematica uses the Unicode standard to ensure portability of international character sets.

 
Mathematica provides system-independent functions for file manipulation.
This finds a list of all notebook files in your home directory.
files  FileNames ".nb", $HomeDirectory
The Unifying Idea of Mathematica
Mathematica is built on the powerful unifying idea that everything can be represented as a symbolic expression.


All symbolic expressions are built up from combinations of the basic form:
A list of elements


a, b, c


List a, b, c
head arg1, arg2, …
An algebraic expression


x2 

 x

Plus Power x, 2 , Sqrt x



An equation


x  Sin x

Equal x, Sin x

A logic expression


p&&  q

And p, Not q



A command


m 1  a

AddTo Part m, 1 , a
Graphics








Graphics Circle 1, 0 , 2 ,
Circle 1, 0 , 2

Abstract mathematical notation

   
a  b  c

Tilde CirclePlus a, b , Subscript c, Infinity

A button

 
Press here

ButtonBox "Press here"

A cell in a notebook

 
A cell containing text

Cell "A cell containing text", "Text"

The uniformity of symbolic expressions makes it easy to add to Mathematica any construct you want.
A chemical compound
HNO3




Chemical Hydrogen, 1 , Nitrogen, 1 , Oxygen, 3
An electric circuit


 


Circuit Resistor "R" ,
Capacitor "C"

All operations in Mathematica are ultimately transformations of symbolic expressions. Mathematica has a uniquely powerful pattern
matcher for applying transformation rules.

 

The /. tells Mathematica to apply the simple transformation rule b1+x.

 
a, b, c, d
a, 1  x, c, d
. b 1 x


 

x_ and y_ each stand for any expression, so the pattern x_+y_ stands for a sum of terms.


a  b, c d, a c . x_ y_ x2 y2


 

a2  b2, c2  d2, a2  c2

a  b, c d, a c
b3, c  d, c3
. a x_ x3

Mathematica uses patterns to generalize the notion of functions.


This is an ordinary function definition to be used for any x.

f x_ :
2
x


Here is a special case that overrides the general definition.

f 0 : e



Here is an example of the use of f.

f 6 f a  b f 0
1

2
e
3 a b



This clears the definitions given for f.

Clear f

An important feature of using patterns is that they allow "functions" to take arguments in any structure.



This defines a value for g with an argument that is a list of two elements.



g x_, y_ : x y

g 4, a b



4a b

Clear g


This specifies the value for the "function" area when given a Circle object as an argument.


area Circle _, _ , r_ :  r2

area Circle 2, 3 , u
2

 
u

This implements a logic reduction rule.


reduce p_ && q_ p_ : p
Mathematica as a Programming Language
Mathematica is an unprecedentedly flexible and intuitive programming language.
Mathematica includes advanced programming methods from modern computer science —and adds a host of new ideas of its own.

Mathematica incorporates a range of programming paradigms—so you can write every program in its most natural way.

 

Procedural Programming


z  a;



Do Print z  z i , i, 3




 


a 1a
a 1a 2  a 1 a


a 1a

Clear z
2  a 1 a

List-based Programming
3 a 1 a 2a 1 a


Many operations are automatically threaded over lists.


1  a, b, c 2

 


1  a2, 1  b2, 1  c2

  
Table ij, i, 4 , j, i
1 , 2, 4 , 3, 9, 27 , 4, 16, 64, 256

 

This flattens out sublists.


Flatten %
1, 2, 4, 3, 9, 27, 4, 16, 64, 256

This partitions into sublists of length 2.


 

 
Partition %, 2
1, 2 , 4, 3 , 9, 27 , 4, 16 , 64, 256

 
Functional Programming








NestList f, x, 4
x, f x , f f x , f f f x ,f f f f x


The






NestList 1 #
is a "pure function". The argument is inserted into the # slot.
2

x, 1  x 2, 1  1  x
&, x, 3
2 2
, 1  1  1 x 2 2 2

 
Rule-Based Programming

 
p x_ y_ : p x  p y



p a  b c
p a p b p c

  
The _ stands for a single expression; __ stands for any sequence of expressions.

 
s x__, a_, y__ , a_ : a, x, x, y, y

 
s 1, 2, 3, 4, 5, 6 , 4
4, 1, 2, 3, 1, 2, 3, 5, 6, 5, 6

Clear p, s

Object-Oriented Programming
Here are three definitions to be associated with the object h.








h : h x_ h y_ :hplus x, y



h : p h x_ , x_ : hp x
h : f_ h x_ : fh f, x








 


This uses the definitions made for h.


h a h b f h r h h x


fh f, r  fh h, x  hplus a, b

Clear h




 
String-Based Programming
StringReplace "aababbaabaabababa", "aa"  "", "ba"  " "
b a a



Mixed Programming Paradigms


  
Many of Mathematica's most powerful functions mix different programming paradigms.



Position 1, 2, 3, 4, 5 2, _Integer




 
2 , 4

 

MapIndexed Power, a, b, c, d

   
a , b2 , c3 , d4

 
#1
FixedPointList If EvenQ #1 , , #1 &, 105
2

   


100000, 50000, 25000, 12500, 6250, 3125, 3125

ReplaceList a, b, c, d, e , x__, y__  x, y




 

 


a , b, c, d, e , a, b , c, d, e , a, b, c , d, e , a, b, c, d , e

Mathematica gives you the flexibility to write programs in many different styles.
Here are a dozen definitions of the factorial function.


f  Factorial

  
f n_ : n



 


f n_ : Gamma n 1



f n_ : n f n 1 ; f 1  1

 
  

f n_ : Product i, i, n

 
   

f n_ : Module t  1 , Do t  t i, i, n ; t



 
 

f n_ : Module t  1, i , For i  1, i n, i , t  i ; t


f n_ : Apply Times, Range n

 
f n_ : Fold Times, 1, Range n

 
f n_ : If n  1, 1, n f n 1

   
f  If #1  1, 1, #1 #0 #1  1 &
f n_ : Fold #2 #1 &, 1, Array Function t, #
t &, n



After you have finished with definitions for f, you must clear them.
Clear f

Writing Programs in Mathematica


Mathematica's high-level programming constructs let you build sophisticated programs more quickly than ever before.
Single-line Mathematica programs can perform complex operations.

 
 

This program produces a one-dimensional random walk.

RandomWalk n_ : NestList #  1 ^ Random Integer &, 0, n

  
Here is a plot of a 200-step random walk.

ListPlot RandomWalk 200 , PlotJoined  True

7.5

2.5

50 100 150 200


-2.5

-5

Graphics

 


 


The directness of Mathematica programs makes them easy to generalize. This program produces a random walk in d dimensions.

RandomWalk n_, d_ : NestList #  1 ^Table Random Integer , d &, Table 0, d , n

   
Here is a plot of a 3D random walk.

Show Graphics3D Line RandomWalk 1000, 3


Graphics3D


The richness of Mathematica's programming language makes it easy to implement sophisticated algorithms.



 
 
Here is a direct program for a single step in the Life cellular automaton.

    


LifeStep a_List :
MapThread If #1  1 && #2  4 #2  3, 1, 0 &,
a, Sum RotateLeft a, i, j , i, 1, 1 , j, 1, 1 , 2



  


Here is an alternative highly optimized program, which operates on lists of live cells.

 
LifeStep list_ :

 
  

With u  Split Sort Flatten Outer Plus, list, N9, 1 , 1 ,


Union Cases u, x_, _, _ x ,
Intersection Cases u, x_, _, _, _ x , list
N9  Flatten Array List, 3, 3 , 1 , 1 ;

   
  

Mathematica makes it easy to build up programs from components. This sets up components for a cellular automaton simulation system.

CenterList n_Integer : ReplacePart Table 0, n , 1, Ceiling n 2


  
  



ElementaryRule num_Integer : IntegerDigits num, 2, 8
CAStep rule_List, a_List : rule 8  RotateLeft a  2 a  2 RotateRight a

   
CAEvolveList rule_List, init_List, t_Integer : NestList CAStep rule, # &, init, t
CAGraphics history_List : Graphics Raster 1  Reverse history ,
AspectRatio  Automatic

    
This runs an example.

Show CAGraphics CAEvolveList ElementaryRule 30 , CenterList 101 , 50


   
CAStep  Compile  



Mathematica has a compiler for optimizing programs that work with lists and numbers. This sets up a compiled definition for CAStep.

rule, _Integer, 1 , a, _Integer, 1 , rule 8  RotateLeft a  2 a  2 RotateRight a


 


Mathematica programs are often a direct translation of material in textbooks. Here are definitions for impedance in a circuit.


Impedance Resistor r_ , _ : r

 

1
Impedance Capacitor c_ , _ :

    
 c
Impedance Inductor l_ , _ :  l

  
Impedance SeriesElement e_ , _ : Apply Plus, Map Impedance #,  &, e
Impedance ParallelElement e_ , _ : 1 Apply Plus, 1 Map Impedance #,  &, e



  
 




This uses the definitions that have been given.

 
Impedance SeriesElement Table ParallelElement Table SeriesElement Resistor Rn , n ,
n, 1, 4 ,  Simplify
1
R1  6 R2  4 R3  3 R4
12

Here is a picture of the circuit, generated from its symbolic specification.

Mathematica programs provide unprecedentedly clear ways to express algorithms.

   
Both of these programs approximate the Golden Ratio to k digits.

 
  
1
1 k_ :1  FixedPoint N , k &, 1
1 #1


 

2 k_ : FixedPoint N 1 #1 , k &, 1

1 20 , 2 20 , N GoldenRatio, 20

1.61803398874989484821, 1.6180339887498948482, 1.6180339887498948482 


Mathematica programs allow a unique combination of mathematical and computational notation.

  

These definitions correspond to a recently-discovered approximation to the number of primes.


 n_ : Apply Plus, Map Last, FactorInteger n





 n_ : MoebiusMu n

 x_ :
 Log 2,x

k1
k
x1 k

n2
n n
x1 k
n
; x 0


 

This compares the approximation with the built-in PrimePi function.

 1000 , PrimePi 1000


1229, 1229

Mathematica programs can mix numerical, symbolic, and graphics operations. This short program solves a sophisticated quantum model.

 


These definitions set up a Kohmoto model for the energy spectrum of a quantum particle in a one-dimensional quasiperiodic potential.

 
FareySequence q_ : Apply Union, Array Range # 1 # &, q


 
 


 



TransferMatrix _, _, p_ :

 
 If 1  Mod p , 1 1, 1, 0 , 1 , 1, 0

TransferMatrixList _, _ : Table TransferMatrix , , p , p, 0, Denominator  1



  
 


 

TransferMatrixProduct _, _ : Fold Expand Dot ## &, First # , Rest #
EnergyPolynomial _, _ : Plus Transpose TransferMatrixProduct ,  , 1, 1
& TransferMatrixList , 

   

Spectrum _, _ : . NSolve #  2 #  2,  & EnergyPolynomial , 
SpectrumData _ : Map Line, Partition Thread , Sort Spectrum ,  ,2

   
This runs the model, generating symbolic eigenvalue equations from transfer matrices and then solving them numerically.

Show Graphics
SpectrumData  FareySequence 20
 Graphics 

Look at the Programming Sampler demo to see more examples of Mathematica programs.

Building Systems with Mathematica


Mathematica has everything you need to create complete systems for technical and non-technical applications.
Combinatorica is a system for doing discrete mathematics that comes as a standard add-on package with Mathematica.
This loads the Combinatorica system.

 DiscreteMath`Combinatorica`

    


This uses functions set up by the package.

ShowGraph LineGraph LineGraph CirculantGraph 5, Range 1, 3

Graphics

WorldPlot is another standard add-on package that comes with Mathematica.


 
  

 Miscellaneous`WorldPlot`

Degree
Abs Sin # 1
60
WorldPlot World, WorldProjection  N #2 , # & , WorldBackground  Hue .5
2

WorldGraphics

Optica is a large Mathematica package for doing optical engineering.



Optica`


DrawSystem




  

ConeOfRays 10, NumberOfRays  10 ,

 
 

Move PlanoConvexLens 100, 50, 10 , 100, 0, 0 ,

   


Move PlanoConvexCylindricalLens 100, 50, 50 , 10 , 130, 0, 0 ,
Move BeamSplitter 50, 50 , 50, 50 , 10 , 180, 0, 45 ,
Boundary 100, 100, 100 , 250, 100, 200 ;
Mathematica has made possible a new generation of notebook-based educational courseware.
Follow this link to see a simple example.

Technical Trader is a Mathematica system for financial analysis.


Technical Trader uses palettes and buttons to build a custom user interface.

ACAD Autodesk, Inc.


BORL Borland International
INGR Intergraph Corp.
INTU Intuit
MSFT Microsoft
NOVL Novell, Inc.
ORCL Oracle System Corp.
SYBS Sybase

Clicking on a button in Technical Trader can generate a notebook of data.


Historical Daily Data for Microsoft (MSFT)
High-Low-Open-Close

95
 

11 15 95 11 28 12 8
Microsoft
12 20 1 3 96 1 15 1 25

90

85

80

75

 

11 15 95 11 28 12 8 12 20 1 3 96 1 15 1 25

Summary Statistics
Max Min Average Volatility
Close 94.5 80.1875 88.4865 2.83299
Volume 14215. 711.1 5784.41 2879.6

Any system you build in Mathematica will run unchanged across all computer platforms.

Mathematica as a Software Component



Mathematica has a modular architecture that makes it easy to use as a highly powerful software component.

 
Here is some input and output in the standard notebook front end to Mathematica.


 
Log x 
x

1
  Erfi Log x x Log x
2

You can also access the Mathematica kernel directly from a raw terminal.





1
2

Integrate[Sqrt[Log[x]], x]

 Erfi Log x x Log x

MathLink provides a general program-level interface between Mathematica and external programs.
Here is C code for sending an expression from an external program to Mathematica.

/* Integrate[Sqrt[Log[x]], x] */

MLPutFunction( stdlink, "EvaluatePacket ", 1);


MLPutFunction( stdlink, "Integrate", 2);
MLPutFunction( stdlink, "Sqrt", 1);
MLPutFunction( stdlink, "Log", 1);
MLPutSymbol( stdlink, "x", 1);
MLPutSymbol( stdlink, "x");
MLEndPacket( stdlink);
Click here to load the kernel and set the working directory to one that contains prebuilt examples for your computer system.
This installs a compiled external C program that does bitwise operations on integers.

link  Install "bitops" ;

 
This executes the external code for the BitAnd function.

BitAnd 22222, 33333


516


This uninstalls the external program.

Uninstall link ;

You can use MathLink to access the Mathematica kernel from many kinds of programs.
Here is the Microsoft Word front end to Mathematica.

Here is a web site that calls Mathematica.


Here, Microsoft Excel is linked to Mathematica.
Under Microsoft Windows, you can click this button to start a simple example of a Visual Basic front end to Mathematica.

MathLink can also be used to access other programs from within the Mathematica kernel.
MathLink allows you to set up templates to specify how external programs should be called. This defines a link to a C subroutine library.

:Begin:
:Function: anneal
:Pattern: TSPTour[r:{{_, _}..}]
:Arguments: {First[Transpose[r]], Last[Transpose[r]],
Length[r], Range[Length[r]]}
:ArgumentTypes: {RealList, RealList, Integer, IntegerList}
:ReturnType: Manual
:End:

 
Here is a 3D graphic generated within Mathematica.

 
 


 
ParametricPlot3D

 


u u

 
2  Cos Sin v  Sin Sin 2v Cos u ,
2 2


 

u u
2  Cos Sin v  Sin Sin 2v Sin u ,
2 2

   
u u
Sin Sin v  Cos Sin 2v ,
2 2
v, 0, 2Pi , u, 0, 2Pi , PlotPoints  30, Boxed  False, Axes  None
Graphics3D


This image was generated by sending a description of the graphic from Mathematica, via a MathLink connection, to an external photorealistic renderer.

 
You can use MathLink to control the Mathematica front end from within the kernel.
This tells the front end to bring up the color selector dialog box.

FrontEndTokenExecute "ColorSelectorDialog"

You can use MathLink to communicate between Mathematica kernels — on one computer or several.
On most computer systems (typically excluding Macintosh) this launches a subsidiary Mathematica kernel on your computer.
 
link  LinkLaunch "MathKernel mathlink" ;


This reads data from the subsidiary Mathematica kernel.


LinkRead link

InputNamePacket In 1 :

 
This writes a command to the subsidiary kernel.

LinkWrite link, Unevaluated $SessionID


 
This reads back the $SessionID from the subsidiary kernel.

LinkRead link
ReturnPacket 20002811790628968292

The $SessionID in your main kernel will be different.

$SessionID


20000841219624707995

This closes down the subsidiary kernel.


LinkClose link ;

The World of Mathematica


With over a million users worldwide, a vast array of Mathematica products and services now exists.
There are now hundreds of books in over a dozen languages about Mathematica.
Several print and electronic periodicals are devoted to Mathematica.
There is a growing library of professional applications based on Mathematica.

Hundreds of courses have been developed in Mathematica.


Wolfram Research's MathSource is a vast repository of Mathematica material.

A wide range of products use Mathematica in their development or implementation.


The Wolfram Research Mathematica web site contains thousands of pages of material, and is constantly being updated.

You might also like