<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>https://airwiki.deib.polimi.it/index.php?action=history&amp;feed=atom&amp;title=Mathematica</id>
		<title>Mathematica - Revision history</title>
		<link rel="self" type="application/atom+xml" href="https://airwiki.deib.polimi.it/index.php?action=history&amp;feed=atom&amp;title=Mathematica"/>
		<link rel="alternate" type="text/html" href="https://airwiki.deib.polimi.it/index.php?title=Mathematica&amp;action=history"/>
		<updated>2026-05-05T19:11:02Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.25.6</generator>

	<entry>
		<id>https://airwiki.deib.polimi.it/index.php?title=Mathematica&amp;diff=1404&amp;oldid=prev</id>
		<title>MicheleMazzucchi: first version</title>
		<link rel="alternate" type="text/html" href="https://airwiki.deib.polimi.it/index.php?title=Mathematica&amp;diff=1404&amp;oldid=prev"/>
				<updated>2007-11-07T19:37:31Z</updated>
		
		<summary type="html">&lt;p&gt;first version&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Mathematica is a [http://www.wolfram.com/products/mathematica/platforms/ multiplatform] product for letting your computer do mathematics for you. It has some benefits with respect to the [[Matlab]] numerical environment:&lt;br /&gt;
* it is fully symbolic&lt;br /&gt;
* once learned the language basics, it is usable efficiently&lt;br /&gt;
* it has a cleaner and more consistent namespace for functions&lt;br /&gt;
* it has more powerful simplification capabilities&lt;br /&gt;
&lt;br /&gt;
Unfortunately, its interface is less intuitive and it is less popular at Politecnico wrt Matlab, mostly because Matlab is free to use for DEI (see [http://www.cia.polimi.it/servizi/software.html CIA]) and Mathematica is not.&lt;br /&gt;
&lt;br /&gt;
=A tutorial=&lt;br /&gt;
This is a brief tutorial over the most useful operations with Mathematica. It is probably pretty useless trying its content live by issuing its commands on a Mathematica console, step by step. Interestingly, the full, official [http://reference.wolfram.com/mathematica/guide/Mathematica.html Mathematica documentation] comprises sheets about mathematical concepts that can be cited in scientifical papers.&lt;br /&gt;
&lt;br /&gt;
==Basics==&lt;br /&gt;
When started, Mathematica prompts a blank console. This is actually a Mathematica &amp;quot;document&amp;quot; (or NoteBook) that can include mixes of formulas and text describing them.&lt;br /&gt;
&lt;br /&gt;
Once a formula has been typed in, press Shift-Enter to let Mathematica resolve it (on the Mac the &amp;quot;enter&amp;quot; key also runs the computation). A trivial formula is a symbol or a symbol assignment:&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;nowiki&amp;gt;x&amp;lt;/nowiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
or&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;nowiki&amp;gt;x = 4&amp;lt;/nowiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Once a symbol (like &amp;quot;x&amp;quot;) has been defined, it is recognized by Mathematica and shown black when typing the next (temporally, not spatially) formulae, not blue. This holds for all recognized symbols: literals, constants and function names.&lt;br /&gt;
&lt;br /&gt;
==Vectors and Matrices (Lists)==&lt;br /&gt;
Matrices are defined with a weird syntax that uses a core concept in Mathematica: lists. (Column) Vectors are:&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;nowiki&amp;gt;vec = {1,2,3}&amp;lt;/nowiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
and matrices are simply vectors of vectors (lists of lists):&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;nowiki&amp;gt;matr = {{11, 12}, {21, 22}}&amp;lt;/nowiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
This input is sometimes confusing, and the MatrixForm function can help:&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;nowiki&amp;gt;MatrixForm[matr]&amp;lt;/nowiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
this presents a list symbol in matrix format.&lt;br /&gt;
&lt;br /&gt;
==Functions==&lt;br /&gt;
&lt;br /&gt;
Functions have two distinctive features:&lt;br /&gt;
* they always wrap their possible arguments in square brackets&lt;br /&gt;
* their names' words are always capitalized (in Java style)&lt;br /&gt;
&lt;br /&gt;
Everything may be expressed with functions in Mathematica. These are sum and multiplication:&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;nowiki&amp;gt;&lt;br /&gt;
Plus[1,2]&lt;br /&gt;
Times[5,4]&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
and they return 3 and 20, respectively. This is called &amp;quot;full form&amp;quot;, that is expressing operation with their full function name. Fortunately, the most common operations have a compact form as well:&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;nowiki&amp;gt;&lt;br /&gt;
1+2&lt;br /&gt;
5*4&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
they evaluate to the same, respectively. When operating with symbols, not numbers, the product is often more handy with the alternate form using just a space between the operands:&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;nowiki&amp;gt;5 4&amp;lt;/nowiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
which returns again 20.&lt;br /&gt;
&lt;br /&gt;
==Symbolic and numerical evaluation==&lt;br /&gt;
Mathematica propagates symbolic evaluation until symbols are not associated to a specific value. For example, the cosine function&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;nowiki&amp;gt;Cos[x]&amp;lt;/nowiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
returns &amp;lt;nowiki&amp;gt;Cos[x]&amp;lt;/nowiki&amp;gt; unless x is defined.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;nowiki&amp;gt;&lt;br /&gt;
x = 1&lt;br /&gt;
Cos[x]&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
does not return 0.540302 but &amp;lt;nowiki&amp;gt;Cos[4]&amp;lt;/nowiki&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The evaluation keeps symbolic not to fall into numerical imprecision propagation, but the actual numerical evaluation can be requested at any time with the &amp;lt;nowiki&amp;gt;N[]&amp;lt;/nowiki&amp;gt; function:&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;nowiki&amp;gt;N[Cos[x]]&amp;lt;/nowiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Simplification==&lt;br /&gt;
&lt;br /&gt;
Let's introduce a more complicated function:&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;nowiki&amp;gt;2 Sin[y] Cos[y]&amp;lt;/nowiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
which is the product (mind the space) between sin(y) and cos(y), multiplied (mind again the space) by 2.&lt;br /&gt;
We can use Mathematica's smart simplification engine to reduce this expression with the &amp;lt;nowiki&amp;gt;Simplify[]&amp;lt;/nowiki&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;nowiki&amp;gt;Simplify[2 Sin[y] Cos[y]]&amp;lt;/nowiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
that returns &amp;lt;nowiki&amp;gt;Sin[2 y]&amp;lt;/nowiki&amp;gt; using prosthaphaeresis formulas.&lt;br /&gt;
&lt;br /&gt;
It is frequently useful to inject some further assumptions to blow the simplification to a further degree. This is pretty easy:&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;nowiki&amp;gt;Simplify[Sqrt[a b^2], b &amp;gt; 0]&amp;lt;/nowiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
yields &amp;lt;nowiki&amp;gt;Sqrt[a] b&amp;lt;/nowiki&amp;gt;.&lt;br /&gt;
Assumptions can be composed to any complexity using the &amp;amp;&amp;amp; operator.&lt;br /&gt;
&lt;br /&gt;
==Defining functions==&lt;br /&gt;
&lt;br /&gt;
Custom functions can be defined. Let's say we want to define a custom implementation of the [[Quaternion]] algebra that uses lists.&lt;br /&gt;
&lt;br /&gt;
The quaternion conjugate is probably the easiest function: it has arity 1 and inverts the sign of the quaternion's pure part:&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;nowiki&amp;gt;QuaternionConjugate = Function[quat, {quat[[1]],-quat[[2]],-quat[[3]],-quat[[4]]}]&amp;lt;/nowiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
This introduces something. The &amp;lt;nowiki&amp;gt;Function[]&amp;lt;/nowiki&amp;gt; function is used to define functions. Its result can be assigned to any name, which becomes the function name with which the operation will be called. The syntax is &amp;lt;nowiki&amp;gt;Function[argument or list of multiple arguments, result]&amp;lt;/nowiki&amp;gt;. Further, the example shows how to access list elements, with the double square brackets notation. It is assumed that the argument is a valid quaternions as a list of (at least, here) four elements. Otherwise mathematica will generate a type error when the function is executed.&lt;br /&gt;
&lt;br /&gt;
A conceptually better representation of the same function:&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;nowiki&amp;gt;QuatConjugate = Function[quat, Flatten[{quat[[1]], -quat[[2 ;; 4]]}]]&amp;lt;/nowiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
this inverts the sign of the whole quaternion's pure part (elements 2 to 4 of q) at once. Since this is a sublist, the &amp;lt;nowiki&amp;gt;Flatten[]&amp;lt;/nowiki&amp;gt; is used to avoid a nested list to be returned.&lt;br /&gt;
&lt;br /&gt;
Of course, functions with higher rank can be defined. This is quaternion product:&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;nowiki&amp;gt;&lt;br /&gt;
QuatMultiply = Function[{p, q}, &lt;br /&gt;
 Flatten[{p[[1]]*q[[1]] - p[[2 ;; 4]].q[[2 ;; 4]], &lt;br /&gt;
   p[[1]]*q[[2 ;; 4]] + p[[2 ;; 4]]*q[[1]] + &lt;br /&gt;
    Cross[p[[2 ;; 4]], q[[2 ;; 4]]]}]]&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
where the argument is now defined as a list of (two) arguments, and some new operations are introduced: the vector dot product &amp;quot;.&amp;quot; and the vector cross product &amp;lt;nowiki&amp;gt;Cross[]&amp;lt;/nowiki&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Functions can be simplified as well, with the &amp;lt;nowiki&amp;gt;Simplify[]&amp;lt;/nowiki&amp;gt; operator. This is one such example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;nowiki&amp;gt;&lt;br /&gt;
QuatMultiply = &lt;br /&gt;
 Simplify[Function[{p, q}, &lt;br /&gt;
   Flatten[{p[[1]]*q[[1]] - p[[2 ;; 4]].q[[2 ;; 4]], &lt;br /&gt;
     p[[1]]*q[[2 ;; 4]] + p[[2 ;; 4]]*q[[1]] + &lt;br /&gt;
      Cross[p[[2 ;; 4]], q[[2 ;; 4]]]}]], &lt;br /&gt;
  p \[Element] Reals &amp;amp;&amp;amp; q \[Element] Reals &amp;amp;&amp;amp; Simplify[Norm[p]] == 1 &amp;amp;&amp;amp; &lt;br /&gt;
   Simplify[Norm[q]] == 1]&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
In this simplification, it is said that both p's and q's components are elements in Real numbers, and that both quaternions have unit norm (see [[Quaternion]]). This is necessary because, in the sake of maximum generality, Mathematica always assumes symbols to be Complex. The &amp;lt;nowiki&amp;gt;\[Element]&amp;lt;/nowiki&amp;gt; is the &amp;lt;math&amp;gt;\in&amp;lt;/math&amp;gt; symbol.&lt;br /&gt;
&lt;br /&gt;
Because Mathematica is fully Unicode, many mathematical symbols can be input along with common ASCII text. For example, the \[Element] symbol can be replaced by the sequence Esc &amp;quot;el&amp;quot; Esc (&amp;quot;el&amp;quot; means the actual characters e and l), the &amp;lt;math&amp;gt;\alpha&amp;lt;/math&amp;gt; can be replaced by Esc &amp;quot;alpha&amp;quot; Esc, et cetera.&lt;br /&gt;
&lt;br /&gt;
=Tips and Tricks=&lt;br /&gt;
&lt;br /&gt;
==Notebooks and commented notebooks==&lt;br /&gt;
It is possible to save a sequence of formulae and their evaluation for latter use. It is thus useful to comment formulae with plain text. Mathematica wraps formulae -- their definition and their computed result -- in blocks. Further blocks can be interleaved between formulae with plain text. Just position between to blocks (where you want to insert text), get the horizontal cursor, then press Control-7 (or use the Format -&amp;gt; Style menu). Text-formatted blocks can contain any [http://reference.wolfram.com/mathematica/guide/NotebookFormattingAndStyling.html kind of formatting]: colors, italic etc.&lt;br /&gt;
&lt;br /&gt;
Do comment your notebooks if you have a fair complexity in them.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==TeX output==&lt;br /&gt;
It is very often useful to export Mathematica output to TeX. This is simple with Mathematica:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;nowiki&amp;gt;&lt;br /&gt;
TeXForm[formula]&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>MicheleMazzucchi</name></author>	</entry>

	</feed>