https://airwiki.elet.polimi.it/api.php?action=feedcontributions&user=MicheleMazzucchi&feedformat=atomAIRWiki - User contributions [en]2022-01-28T02:32:01ZUser contributionsMediaWiki 1.25.6https://airwiki.elet.polimi.it/index.php?title=Mathematica&diff=1404Mathematica2007-11-07T19:37:31Z<p>MicheleMazzucchi: first version</p>
<hr />
<div>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:<br />
* it is fully symbolic<br />
* once learned the language basics, it is usable efficiently<br />
* it has a cleaner and more consistent namespace for functions<br />
* it has more powerful simplification capabilities<br />
<br />
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.<br />
<br />
=A tutorial=<br />
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.<br />
<br />
==Basics==<br />
When started, Mathematica prompts a blank console. This is actually a Mathematica "document" (or NoteBook) that can include mixes of formulas and text describing them.<br />
<br />
Once a formula has been typed in, press Shift-Enter to let Mathematica resolve it (on the Mac the "enter" key also runs the computation). A trivial formula is a symbol or a symbol assignment:<br />
<pre><nowiki>x</nowiki></pre><br />
or<br />
<pre><nowiki>x = 4</nowiki></pre><br />
<br />
Once a symbol (like "x") 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.<br />
<br />
==Vectors and Matrices (Lists)==<br />
Matrices are defined with a weird syntax that uses a core concept in Mathematica: lists. (Column) Vectors are:<br />
<pre><nowiki>vec = {1,2,3}</nowiki></pre><br />
and matrices are simply vectors of vectors (lists of lists):<br />
<pre><nowiki>matr = {{11, 12}, {21, 22}}</nowiki></pre><br />
This input is sometimes confusing, and the MatrixForm function can help:<br />
<pre><nowiki>MatrixForm[matr]</nowiki></pre><br />
this presents a list symbol in matrix format.<br />
<br />
==Functions==<br />
<br />
Functions have two distinctive features:<br />
* they always wrap their possible arguments in square brackets<br />
* their names' words are always capitalized (in Java style)<br />
<br />
Everything may be expressed with functions in Mathematica. These are sum and multiplication:<br />
<pre><nowiki><br />
Plus[1,2]<br />
Times[5,4]<br />
</nowiki></pre><br />
and they return 3 and 20, respectively. This is called "full form", that is expressing operation with their full function name. Fortunately, the most common operations have a compact form as well:<br />
<pre><nowiki><br />
1+2<br />
5*4<br />
</nowiki></pre><br />
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:<br />
<pre><nowiki>5 4</nowiki></pre><br />
which returns again 20.<br />
<br />
==Symbolic and numerical evaluation==<br />
Mathematica propagates symbolic evaluation until symbols are not associated to a specific value. For example, the cosine function<br />
<br />
<pre><nowiki>Cos[x]</nowiki></pre><br />
returns <nowiki>Cos[x]</nowiki> unless x is defined.<br />
<pre><nowiki><br />
x = 1<br />
Cos[x]<br />
</nowiki></pre><br />
does not return 0.540302 but <nowiki>Cos[4]</nowiki>.<br />
<br />
The evaluation keeps symbolic not to fall into numerical imprecision propagation, but the actual numerical evaluation can be requested at any time with the <nowiki>N[]</nowiki> function:<br />
<pre><nowiki>N[Cos[x]]</nowiki></pre><br />
<br />
==Simplification==<br />
<br />
Let's introduce a more complicated function:<br />
<pre><nowiki>2 Sin[y] Cos[y]</nowiki></pre><br />
which is the product (mind the space) between sin(y) and cos(y), multiplied (mind again the space) by 2.<br />
We can use Mathematica's smart simplification engine to reduce this expression with the <nowiki>Simplify[]</nowiki>:<br />
<pre><nowiki>Simplify[2 Sin[y] Cos[y]]</nowiki></pre><br />
that returns <nowiki>Sin[2 y]</nowiki> using prosthaphaeresis formulas.<br />
<br />
It is frequently useful to inject some further assumptions to blow the simplification to a further degree. This is pretty easy:<br />
<pre><nowiki>Simplify[Sqrt[a b^2], b > 0]</nowiki></pre><br />
yields <nowiki>Sqrt[a] b</nowiki>.<br />
Assumptions can be composed to any complexity using the && operator.<br />
<br />
==Defining functions==<br />
<br />
Custom functions can be defined. Let's say we want to define a custom implementation of the [[Quaternion]] algebra that uses lists.<br />
<br />
The quaternion conjugate is probably the easiest function: it has arity 1 and inverts the sign of the quaternion's pure part:<br />
<pre><nowiki>QuaternionConjugate = Function[quat, {quat[[1]],-quat[[2]],-quat[[3]],-quat[[4]]}]</nowiki></pre><br />
This introduces something. The <nowiki>Function[]</nowiki> 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 <nowiki>Function[argument or list of multiple arguments, result]</nowiki>. 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.<br />
<br />
A conceptually better representation of the same function:<br />
<pre><nowiki>QuatConjugate = Function[quat, Flatten[{quat[[1]], -quat[[2 ;; 4]]}]]</nowiki></pre><br />
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 <nowiki>Flatten[]</nowiki> is used to avoid a nested list to be returned.<br />
<br />
Of course, functions with higher rank can be defined. This is quaternion product:<br />
<pre><nowiki><br />
QuatMultiply = Function[{p, q}, <br />
Flatten[{p[[1]]*q[[1]] - p[[2 ;; 4]].q[[2 ;; 4]], <br />
p[[1]]*q[[2 ;; 4]] + p[[2 ;; 4]]*q[[1]] + <br />
Cross[p[[2 ;; 4]], q[[2 ;; 4]]]}]]<br />
</nowiki></pre><br />
where the argument is now defined as a list of (two) arguments, and some new operations are introduced: the vector dot product "." and the vector cross product <nowiki>Cross[]</nowiki>.<br />
<br />
Functions can be simplified as well, with the <nowiki>Simplify[]</nowiki> operator. This is one such example:<br />
<pre><nowiki><br />
QuatMultiply = <br />
Simplify[Function[{p, q}, <br />
Flatten[{p[[1]]*q[[1]] - p[[2 ;; 4]].q[[2 ;; 4]], <br />
p[[1]]*q[[2 ;; 4]] + p[[2 ;; 4]]*q[[1]] + <br />
Cross[p[[2 ;; 4]], q[[2 ;; 4]]]}]], <br />
p \[Element] Reals && q \[Element] Reals && Simplify[Norm[p]] == 1 && <br />
Simplify[Norm[q]] == 1]<br />
</nowiki></pre><br />
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 <nowiki>\[Element]</nowiki> is the <math>\in</math> symbol.<br />
<br />
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 "el" Esc ("el" means the actual characters e and l), the <math>\alpha</math> can be replaced by Esc "alpha" Esc, et cetera.<br />
<br />
=Tips and Tricks=<br />
<br />
==Notebooks and commented notebooks==<br />
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 -> Style menu). Text-formatted blocks can contain any [http://reference.wolfram.com/mathematica/guide/NotebookFormattingAndStyling.html kind of formatting]: colors, italic etc.<br />
<br />
Do comment your notebooks if you have a fair complexity in them.<br />
<br />
<br />
==TeX output==<br />
It is very often useful to export Mathematica output to TeX. This is simple with Mathematica:<br />
<br />
<pre><nowiki><br />
TeXForm[formula]<br />
</nowiki></pre></div>MicheleMazzucchihttps://airwiki.elet.polimi.it/index.php?title=Rotations&diff=1380Rotations2007-10-26T13:04:29Z<p>MicheleMazzucchi: /* Rotations */</p>
<hr />
<div>You have several choices when choosing how to represent a rotation in 3D space. Representations mostly differ in<br />
* number of parameters (space) required<br />
* globality (capacity to express *every* rotation) and singularities<br />
* readability to humans<br />
but more minor characteristics exist like complexity, multiplicity (more parametrizations for the same physical rotation) and numerical behaviour.<br />
<br />
The most useful rotation representations to know of for practical use are the [[Direction Cosine Matrix]] and the [[Quaternion]]. Wikipedia has a rough introduction on [http://en.wikipedia.org/wiki/Rotation_representation_%28mathematics%29 more of the other existing representations].</div>MicheleMazzucchihttps://airwiki.elet.polimi.it/index.php?title=Rotations&diff=1379Rotations2007-10-26T13:03:59Z<p>MicheleMazzucchi: basic properties, references to DCM and Quaternions</p>
<hr />
<div>==Rotations==<br />
<br />
You have several choices when choosing how to represent a rotation in 3D space. Representations mostly differ in<br />
* number of parameters (space) required<br />
* globality (capacity to express *every* rotation) and singularities<br />
* readability to humans<br />
but more minor characteristics exist like complexity, multiplicity (more parametrizations for the same physical rotation) and numerical behaviour.<br />
<br />
The most useful rotation representations to know of for practical use are the [[Direction Cosine Matrix]] and the [[Quaternion]]. Wikipedia has a rough introduction on [http://en.wikipedia.org/wiki/Rotation_representation_%28mathematics%29 more of the other existing representations].</div>MicheleMazzucchi