Basic Examples

Step-by-step examples demonstrating the fundamental operations and use cases of iTensor.

Getting Started with Examples

This section provides hands-on examples to help you understand how to use iTensor in practice. Each example includes code snippets, explanations, and expected outputs to guide you through common operations and workflows.

All examples can be run directly using the iTensor API. You can copy the code snippets and execute them using your preferred HTTP client (curl, Postman, Insomnia, or directly in your application).

Example 1: Matrix Addition

This example demonstrates how to add two numerical matrices using the iTensor API.

Request

// POST /api/tensors/numerical/ { "operation": "add", "operands": [ { "name": "A", "shape": [2, 2], "values": [[1.0, 2.0], [3.0, 4.0]] }, { "name": "B", "shape": [2, 2], "values": [[5.0, 6.0], [7.0, 8.0]] } ] }

Response

{ "operation": "add", "result": { "shape": [2, 2], "values": [[6.0, 8.0], [10.0, 12.0]] } }

This example adds two 2×2 matrices element-wise. Each element in the result is the sum of the corresponding elements in matrices A and B.

Example 2: Matrix Multiplication

This example shows how to perform matrix multiplication between two numerical matrices.

Request

// POST /api/tensors/numerical/ { "operation": "matmul", "operands": [ { "name": "A", "shape": [2, 3], "values": [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]] }, { "name": "B", "shape": [3, 2], "values": [[7.0, 8.0], [9.0, 10.0], [11.0, 12.0]] } ] }

Response

{ "operation": "matmul", "result": { "shape": [2, 2], "values": [[58.0, 64.0], [139.0, 154.0]] } }

This example multiplies a 2×3 matrix with a 3×2 matrix using standard matrix multiplication rules. The resulting matrix has shape [2, 2] where each element is computed as the dot product of a row from matrix A and a column from matrix B.

Example 3: Symbolic Differentiation

This example demonstrates how to compute symbolic derivatives of tensor expressions.

Request

// POST /api/tensors/symbolic/ { "operation": "differentiate", "tensor": { "name": "F", "shape": [2, 2], "values": [["x^2", "sin(x)"], ["cos(y)", "y^3"]] }, "with_respect_to": "x" }

Response

{ "operation": "differentiate", "result": { "shape": [2, 2], "values": [["2*x", "cos(x)"], ["0", "0"]] } }

This example computes the partial derivative of a 2×2 tensor with respect to the variable x. Note that only elements containing x are differentiated, while elements without x have derivatives of zero.

Example 4: Creating Special Tensors

This example shows how to create special tensors like identity matrices and tensors filled with zeros.

Creating an Identity Matrix

// POST /api/tensors/numerical/ { "operation": "identity", "tensor": { "name": "I", "shape": [3, 3], "dtype": "float64" } }

Response

{ "operation": "identity", "result": { "shape": [3, 3], "values": [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]] } }

Creating a Tensor Filled with Zeros

// POST /api/tensors/numerical/ { "operation": "zeros", "tensor": { "name": "Z", "shape": [2, 2], "dtype": "float32" } }

Response

{ "operation": "zeros", "result": { "shape": [2, 2], "values": [[0.0, 0.0], [0.0, 0.0]] } }

Example 5: Converting Between Symbolic and Numerical

This example demonstrates how to define a tensor symbolically and then evaluate it with numerical values.

Step 1: Create a Symbolic Tensor

// POST /api/tensors/symbolic/ { "operation": "create", "tensor": { "name": "R", "shape": [2, 2], "values": [["cos(theta)", "-sin(theta)"], ["sin(theta)", "cos(theta)"]] } }

Step 2: Evaluate with a Specific Angle

// POST /api/tensors/numerical/ { "operation": "evaluate", "symbolic_tensor": "R", "values": { "theta": 0.5 }, "result_tensor": { "name": "R_numeric", "dtype": "float64" } }

Response

{ "operation": "evaluate", "result": { "shape": [2, 2], "values": [ [0.8775825618903728, -0.4794255386042031], [0.4794255386042031, 0.8775825618903728] ] } }

This example demonstrates a common workflow: first defining a tensor symbolically (in this case, a 2D rotation matrix) and then evaluating it with specific numerical values (theta = 0.5 radians).