Tensor Calculus Basics
Understand the fundamentals of tensor mathematics and learn how to apply them in physics simulations using the iTensor framework.
Tutorial Contents
Introduction
Tensors are mathematical objects that generalize scalars, vectors, and matrices to higher dimensions. They are essential tools in physics, engineering, and computer science for representing multidimensional data and physical laws in a coordinate-independent way. In this tutorial, we'll cover:
- The mathematical definition and intuition behind tensors
- Tensor notation and index manipulation
- Common operations like tensor contraction and transformation
- Applications in physics including stress-strain relationships and electromagnetic fields
- Implementing tensor calculations using the iTensor framework
By the end of this tutorial, you'll have a solid understanding of tensor mathematics and be able to implement tensor-based algorithms for physics simulations using the iTensor framework.
Prerequisites
Before beginning this tutorial, you should have:
- Basic knowledge of linear algebra (vectors, matrices)
- Familiarity with basic calculus concepts (derivatives, gradients)
- iTensor framework installed (v2.0+)
Understanding Tensors
A tensor can be thought of as a multi-dimensional array that transforms in specific ways when we change coordinate systems. Let's start with defining tensors of different ranks:
Rank-0: Scalar
A single number that doesn't change under coordinate transformations.
Rank-1: Vector
A one-dimensional array (components with one index).
Rank-2: Matrix
A two-dimensional array (components with two indices).
Let's define a simple rank-2 tensor (which is essentially a matrix) using the iTensor framework:
// Define a simple rank-2 tensor (matrix)
const tensor = new iTensor.Tensor({
rank: 2,
dimensions: [3, 3],
components: [
[1, 0, 0],
[0, 1, 0],
[0, 0, 1]
]
});
// This creates the identity tensor (identity matrix) in 3D space
console.log(tensor.toString());
The key aspect of tensors is how they transform under coordinate changes. When we change our coordinate system, the components of a tensor transform in a specific way that preserves the physical meaning they represent.
Mathematical Note
A tensor of rank n in m-dimensional space has m^n components. For example, a rank-2 tensor in 3D space has 3² = 9 components, which can be arranged in a 3×3 matrix.
Tensor Operations
Tensor Transformations
One of the most important operations with tensors is transformation between different coordinate systems. Let's implement a simple rotation in 2D space:
// Create a coordinate transformation (rotation)
const rotation = new iTensor.CoordinateTransformation({
fromCoordinates: 'cartesian',
toCoordinates: 'cartesian',
transformationMatrix: [
[Math.cos(Math.PI/4), -Math.sin(Math.PI/4), 0],
[Math.sin(Math.PI/4), Math.cos(Math.PI/4), 0],
[0, 0, 1]
]
});
// Apply transformation to a vector
const vector = new iTensor.Tensor({
rank: 1,
dimensions: [3],
components: [1, 0, 0]
});
const transformedVector = rotation.apply(vector);
console.log(transformedVector.components); // Will output [0.7071..., 0.7071..., 0]
In this example, we define a 45-degree rotation in the xy-plane and apply it to a unit vector pointing along the x-axis. The result is a vector with equal x and y components (pointing at a 45-degree angle), as expected.
Tensor Contractions
Tensor contraction is the operation of summing over matching indices. For rank-2 tensors, this is similar to matrix multiplication:
// Create two tensors
const tensor1 = new iTensor.Tensor({
rank: 2,
dimensions: [3, 3],
components: [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
});
const tensor2 = new iTensor.Tensor({
rank: 2,
dimensions: [3, 3],
components: [
[9, 8, 7],
[6, 5, 4],
[3, 2, 1]
]
});
// Perform contraction (like matrix multiplication)
const contracted = iTensor.contract(tensor1, tensor2, [[1], [0]]);
console.log(contracted.components);
The contract
function takes three arguments:
- The first tensor
- The second tensor
- An array of indices to contract (in this case, the second index of the first tensor with the first index of the second tensor)
The result is a new tensor with the contracted indices removed.
Tensor Calculus
Tensor calculus extends vector calculus to handle derivatives of tensor fields. Let's compute the gradient of a scalar field:
// Define a scalar field in 3D space: f(x,y,z) = x^2 + y^2 + z^2
const scalarField = new iTensor.ScalarField({
coordinates: 'cartesian',
expression: (x, y, z) => x*x + y*y + z*z
});
// Compute the gradient (result is a vector field)
const gradientField = iTensor.gradient(scalarField);
// Evaluate the gradient at point (1,1,1)
const gradientVector = gradientField.evaluate(1, 1, 1);
console.log(gradientVector.components); // Will output [2, 2, 2]
The gradient of a scalar field f(x,y,z) = x² + y² + z² at any point is a vector pointing in the direction of steepest increase. At the point (1,1,1), the gradient is (2,2,2), which means the function increases most rapidly in the direction (1,1,1).
Physics Note
In physics, we often work with covariant and contravariant tensors, distinguished by their transformation properties. In the iTensor framework, these distinctions are handled automatically when you specify the coordinate system.
Applications in Physics
Tensors are used extensively in physics to represent physical quantities and laws in a coordinate-independent way. Here are some important applications:
Stress-Strain Tensor
In solid mechanics, the stress tensor and strain tensor describe how forces and deformations affect materials. The relationship between them characterizes material properties.
Electromagnetic Field Tensor
In electromagnetism, the electromagnetic field tensor combines the electric and magnetic fields into a single rank-2 tensor, elegantly expressing Maxwell's equations.
Riemann Curvature Tensor
In general relativity, the Riemann tensor describes the curvature of spacetime, which is interpreted as the gravitational field.
Moment of Inertia Tensor
In classical mechanics, the moment of inertia tensor describes how mass is distributed relative to the rotation axes in a rigid body.
Conclusion
In this tutorial, we've covered the fundamental concepts of tensor mathematics and how to implement tensor operations using the iTensor framework. We've learned about tensor definitions, transformations, contractions, and basic tensor calculus.
Tensor mathematics is a powerful tool for physics simulations, as it allows us to express physical laws in a coordinate-independent way. With the iTensor framework, you can now build sophisticated simulations for a wide range of physical problems, from classical mechanics to relativity and beyond.