https://airwiki.elet.polimi.it/api.php?action=feedcontributions&user=MicheleMazzucchi&feedformat=atom AIRWiki - User contributions [en] 2022-01-28T02:32:01Z User contributions MediaWiki 1.25.6 https://airwiki.elet.polimi.it/index.php?title=Mathematica&diff=1404 Mathematica 2007-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 &quot;document&quot; (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 &quot;enter&quot; key also runs the computation). A trivial formula is a symbol or a symbol assignment:<br /> &lt;pre&gt;&lt;nowiki&gt;x&lt;/nowiki&gt;&lt;/pre&gt;<br /> or<br /> &lt;pre&gt;&lt;nowiki&gt;x = 4&lt;/nowiki&gt;&lt;/pre&gt;<br /> <br /> Once a symbol (like &quot;x&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.<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 /> &lt;pre&gt;&lt;nowiki&gt;vec = {1,2,3}&lt;/nowiki&gt;&lt;/pre&gt;<br /> and matrices are simply vectors of vectors (lists of lists):<br /> &lt;pre&gt;&lt;nowiki&gt;matr = {{11, 12}, {21, 22}}&lt;/nowiki&gt;&lt;/pre&gt;<br /> This input is sometimes confusing, and the MatrixForm function can help:<br /> &lt;pre&gt;&lt;nowiki&gt;MatrixForm[matr]&lt;/nowiki&gt;&lt;/pre&gt;<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 /> &lt;pre&gt;&lt;nowiki&gt;<br /> Plus[1,2]<br /> Times[5,4]<br /> &lt;/nowiki&gt;&lt;/pre&gt;<br /> and they return 3 and 20, respectively. This is called &quot;full form&quot;, that is expressing operation with their full function name. Fortunately, the most common operations have a compact form as well:<br /> &lt;pre&gt;&lt;nowiki&gt;<br /> 1+2<br /> 5*4<br /> &lt;/nowiki&gt;&lt;/pre&gt;<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 /> &lt;pre&gt;&lt;nowiki&gt;5 4&lt;/nowiki&gt;&lt;/pre&gt;<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 /> &lt;pre&gt;&lt;nowiki&gt;Cos[x]&lt;/nowiki&gt;&lt;/pre&gt;<br /> returns &lt;nowiki&gt;Cos[x]&lt;/nowiki&gt; unless x is defined.<br /> &lt;pre&gt;&lt;nowiki&gt;<br /> x = 1<br /> Cos[x]<br /> &lt;/nowiki&gt;&lt;/pre&gt;<br /> does not return 0.540302 but &lt;nowiki&gt;Cos&lt;/nowiki&gt;.<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 &lt;nowiki&gt;N[]&lt;/nowiki&gt; function:<br /> &lt;pre&gt;&lt;nowiki&gt;N[Cos[x]]&lt;/nowiki&gt;&lt;/pre&gt;<br /> <br /> ==Simplification==<br /> <br /> Let's introduce a more complicated function:<br /> &lt;pre&gt;&lt;nowiki&gt;2 Sin[y] Cos[y]&lt;/nowiki&gt;&lt;/pre&gt;<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 &lt;nowiki&gt;Simplify[]&lt;/nowiki&gt;:<br /> &lt;pre&gt;&lt;nowiki&gt;Simplify[2 Sin[y] Cos[y]]&lt;/nowiki&gt;&lt;/pre&gt;<br /> that returns &lt;nowiki&gt;Sin[2 y]&lt;/nowiki&gt; 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 /> &lt;pre&gt;&lt;nowiki&gt;Simplify[Sqrt[a b^2], b &gt; 0]&lt;/nowiki&gt;&lt;/pre&gt;<br /> yields &lt;nowiki&gt;Sqrt[a] b&lt;/nowiki&gt;.<br /> Assumptions can be composed to any complexity using the &amp;&amp; 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 /> &lt;pre&gt;&lt;nowiki&gt;QuaternionConjugate = Function[quat, {quat[],-quat[],-quat[],-quat[]}]&lt;/nowiki&gt;&lt;/pre&gt;<br /> This introduces something. The &lt;nowiki&gt;Function[]&lt;/nowiki&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 &lt;nowiki&gt;Function[argument or list of multiple arguments, result]&lt;/nowiki&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.<br /> <br /> A conceptually better representation of the same function:<br /> &lt;pre&gt;&lt;nowiki&gt;QuatConjugate = Function[quat, Flatten[{quat[], -quat[[2 ;; 4]]}]]&lt;/nowiki&gt;&lt;/pre&gt;<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 &lt;nowiki&gt;Flatten[]&lt;/nowiki&gt; 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 /> &lt;pre&gt;&lt;nowiki&gt;<br /> QuatMultiply = Function[{p, q}, <br /> Flatten[{p[]*q[] - p[[2 ;; 4]].q[[2 ;; 4]], <br /> p[]*q[[2 ;; 4]] + p[[2 ;; 4]]*q[] + <br /> Cross[p[[2 ;; 4]], q[[2 ;; 4]]]}]]<br /> &lt;/nowiki&gt;&lt;/pre&gt;<br /> where the argument is now defined as a list of (two) arguments, and some new operations are introduced: the vector dot product &quot;.&quot; and the vector cross product &lt;nowiki&gt;Cross[]&lt;/nowiki&gt;.<br /> <br /> Functions can be simplified as well, with the &lt;nowiki&gt;Simplify[]&lt;/nowiki&gt; operator. This is one such example:<br /> &lt;pre&gt;&lt;nowiki&gt;<br /> QuatMultiply = <br /> Simplify[Function[{p, q}, <br /> Flatten[{p[]*q[] - p[[2 ;; 4]].q[[2 ;; 4]], <br /> p[]*q[[2 ;; 4]] + p[[2 ;; 4]]*q[] + <br /> Cross[p[[2 ;; 4]], q[[2 ;; 4]]]}]], <br /> p \[Element] Reals &amp;&amp; q \[Element] Reals &amp;&amp; Simplify[Norm[p]] == 1 &amp;&amp; <br /> Simplify[Norm[q]] == 1]<br /> &lt;/nowiki&gt;&lt;/pre&gt;<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 &lt;nowiki&gt;\[Element]&lt;/nowiki&gt; is the &lt;math&gt;\in&lt;/math&gt; 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 &quot;el&quot; Esc (&quot;el&quot; means the actual characters e and l), the &lt;math&gt;\alpha&lt;/math&gt; can be replaced by Esc &quot;alpha&quot; 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 -&gt; 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 /> &lt;pre&gt;&lt;nowiki&gt;<br /> TeXForm[formula]<br /> &lt;/nowiki&gt;&lt;/pre&gt;</div> MicheleMazzucchi https://airwiki.elet.polimi.it/index.php?title=Rotations&diff=1380 Rotations 2007-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> MicheleMazzucchi https://airwiki.elet.polimi.it/index.php?title=Rotations&diff=1379 Rotations 2007-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