Introduction to Mathematica
Solve an equation
1
| Solve[x^2 - 3 x + 2 == 0, x]
|
Output
Solve an equation within integers
1
| Solve[x^2 - 3 x + 2 == 0 && 0 <= x <= 2, x, Integers]
|
Output
Solve simultaneous equations
1
| Solve[{x + y == 3, x - y == 1}, {x, y}]
|
Output
Solve an inequality
1
| Reduce[x^2 - 3 x + 2 > 0, x]
|
Output
Differentiate
Output
Integrate
Output
Find a limit
1
| Limit[1/x, x -> 0, Direction -> "FromAbove"]
|
Output
The limit as x approaches 0 from the right is Infinity. The two-sided real limit does not exist. Use Direction to choose a one-sided approach.
Find a series
1
| Sum[1/n^2, {n, 1, Infinity}]
|
Output
Create a matrix
Find the product of matrices
Output
Find the inverse matrix
Output
Find eigenvalues and eigenvectors
Output
1
| {{(5 + Sqrt[33])/2, (5 - Sqrt[33])/2}, {{2, (3 + Sqrt[33])/2}, {2, (3 - Sqrt[33])/2}}}
|
The eigenvalues of this matrix are (5 ± √33)/2. Eigenvectors may be returned in a different order or scaled; each value-vector pair satisfies m.v = λv.
Find the inner product of vectors
Output
Find the cross product of vectors
1
| Cross[{1, 2, 3}, {4, 5, 6}]
|
Output
Find the magnitude of a vector
Output
Find the angle between vectors
1
| ArcCos[{1, 2} . {3, 4}/(Norm[{1, 2}] Norm[{3, 4}])]
|
Output
Find the projection of a vector
1
| Projection[{1, 2}, {3, 4}]
|
Output
Projecting {1,2} onto {3,4} uses the dot-product ratio 11/25, giving {33/25,44/25}.
Find the rotation of a vector
1
| RotationMatrix[π/2].{1, 0}
|
Output
Find the translation of a vector
1
| TranslationTransform[{1, 2}][{3, 4}]
|
Output
Find the scaling of a vector
1
| ScalingTransform[{2, 3}][{1, 1}]
|
Output
Find the reflection of a vector
1
| ReflectionTransform[{0, 1}][{1, 1}]
|
Output
ReflectionTransform takes the mirror’s normal as its first argument. The normal {0,1} defines the x-axis, which maps {1,1} to {1,-1}.
The random outputs below are illustrative and change on each evaluation. RandomInteger[] returns 0 or 1. SeedRandom can reproduce a sequence with the same generator and version.
Generate random numbers
Output
Generate random numbers (integers)
Output
Generate random numbers (range specified)
Output
Generate random numbers (integers, range specified)
Output
Generate random numbers (distribution specified)
1
| RandomVariate[NormalDistribution[0, 1]]
|
Output
Generate random numbers (distribution specified, number specified)
1
| RandomVariate[NormalDistribution[0, 1], 10]
|
Output
1
| {0.123456, 0.234567, 0.345678, 0.456789, 0.567890, 0.678901, 0.789012, 0.890123, 0.901234, 0.012345}
|
Generate random numbers (distribution specified, number specified, seed specified)
1
2
| SeedRandom[12345]
RandomVariate[NormalDistribution[0, 1], 10]
|
Output
1
| (* Output intentionally omitted: exact sequence depends on the Wolfram Language version. *)
|
Apply a function to array elements
1
2
3
| Map[Sqrt, {1, 4, 9}]
Sqrt /@ {1, 4, 9}
Map[#^(1/2)&, {1, 4, 9}]
|
Output
Define a function using lambda expressions
1
2
| f = Function[x, x^2]
f[3]
|
Output
Compose functions
1
2
3
4
| f = Function[x, x^2]
g = Function[x, x + 1]
h = Function[x, f[g[x]]]
h[3]
|
Output
Reference the previous calculation result
Output
Pure functions
Output
1
2
| Select[{1, 2, 3, 4, 5}, EvenQ]
Select[{1, 2, 3, 4, 5}, Mod[#,2]==0&]
|
Output
Official function documentation
Solve · Reduce · D · Integrate · Limit · Sum · Inverse · Eigensystem · Projection · RotationMatrix · ReflectionTransform · RandomReal · RandomInteger · RandomVariate