From 8d60f7158cad813b38353d73eb1a84ad9ab7cec7 Mon Sep 17 00:00:00 2001 From: Nischay-loq Date: Fri, 24 Oct 2025 16:59:03 +0530 Subject: [PATCH 1/4] Added documentation entry for NumPy ndarray trace() method --- .../concepts/ndarray/terms/trace/trace.md | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 content/numpy/concepts/ndarray/terms/trace/trace.md diff --git a/content/numpy/concepts/ndarray/terms/trace/trace.md b/content/numpy/concepts/ndarray/terms/trace/trace.md new file mode 100644 index 00000000000..16bf9e95d33 --- /dev/null +++ b/content/numpy/concepts/ndarray/terms/trace/trace.md @@ -0,0 +1,90 @@ +--- +Title: '.trace()' +Description: 'Returns the sum of the elements along the diagonal of an array.' +Subjects: + - 'Computer Science' + - 'Data Science' +Tags: + - 'Arrays' + - 'Linear Algebra' + - 'Matrices' + - 'NumPy' + - 'Python' +CatalogContent: + - 'learn-python-3' + - 'paths/data-science' +--- + +The **`.trace()`** method returns the sum of the elements along the diagonal of a NumPy array. The diagonal is defined as the set of elements where the row index equals the column index. This method is commonly used in linear algebra operations, such as calculating the trace of a matrix, which can provide insights into matrix properties like the sum of eigenvalues. + +The `.trace()` method supports various parameters to customize which diagonal to sum, making it versatile for different array shapes and orientations. It can handle multi-dimensional arrays by specifying the axes to consider as the matrix dimensions. + +## Syntax + +```pseudo +ndarray.trace(offset=0, axis1=0, axis2=1, dtype=None, out=None) +``` + +**Parameters:** + +- `offset` (Optional): The offset of the diagonal from the main diagonal. A positive value selects a diagonal above the main diagonal, while a negative value selects one below. Default is `0` (main diagonal). +- `axis1` (Optional): The axis to be used as the first axis of the 2D sub-arrays from which the diagonals should be taken. Default is `0`. +- `axis2` (Optional): The axis to be used as the second axis of the 2D sub-arrays from which the diagonals should be taken. Default is `1`. +- `dtype` (Optional): The data type of the returned array. If not specified, it is determined from the input array. +- `out` (Optional): An alternative output array in which to place the result. It must have the same shape as the expected output. + +**Return value:** + +Returns the sum of the diagonal elements as a scalar or array, depending on the input array's dimensions and the specified parameters. + +## Example + +This example demonstrates how to use `.trace()` to calculate the sum of the main diagonal elements of a 2D array: + +```py +import numpy as np + +# Create a 2D array +array_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) +print("Original array:") +print(array_2d) + +# Calculate the trace (sum of main diagonal) +trace_value = array_2d.trace() +print("\nTrace (sum of diagonal elements):", trace_value) +``` + +The output produced by this code is: + +```shell +Original array: +[[1 2 3] + [4 5 6] + [7 8 9]] + +Trace (sum of diagonal elements): 15 +``` + +This code creates a 3×3 array and calculates the trace by summing the diagonal elements (1 + 5 + 9 = 15). + +## Codebyte Example + +```codebyte/python +import numpy as np + +# Create a 3x3 matrix +matrix = np.array([[2, 0, 1], [1, 1, 0], [0, 1, 2]]) +print("Matrix:") +print(matrix) + +# Calculate the trace +trace_result = matrix.trace() +print("\nTrace of the matrix:", trace_result) + +# Calculate trace with offset +trace_offset_1 = matrix.trace(offset=1) +trace_offset_minus_1 = matrix.trace(offset=-1) +print("Trace with offset 1 (above diagonal):", trace_offset_1) +print("Trace with offset -1 (below diagonal):", trace_offset_minus_1) +``` +n:\Open_Source\docs\content\numpy\concepts\ndarray\terms\trace\trace.md From 86e263668ef58fb5aa884bd671446ccbc896d1ec Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Sat, 25 Oct 2025 13:47:31 +0530 Subject: [PATCH 2/4] Enhance .trace() method documentation for clarity Clarified explanation of the .trace() method for NumPy arrays, including details on handling multi-dimensional arrays and optional parameters. --- content/numpy/concepts/ndarray/terms/trace/trace.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/content/numpy/concepts/ndarray/terms/trace/trace.md b/content/numpy/concepts/ndarray/terms/trace/trace.md index 16bf9e95d33..f5774859f42 100644 --- a/content/numpy/concepts/ndarray/terms/trace/trace.md +++ b/content/numpy/concepts/ndarray/terms/trace/trace.md @@ -15,18 +15,19 @@ CatalogContent: - 'paths/data-science' --- -The **`.trace()`** method returns the sum of the elements along the diagonal of a NumPy array. The diagonal is defined as the set of elements where the row index equals the column index. This method is commonly used in linear algebra operations, such as calculating the trace of a matrix, which can provide insights into matrix properties like the sum of eigenvalues. +The **`.trace()`** method returns the sum of the elements along the diagonal of a NumPy array. For 2D arrays, the diagonal consists of elements where the row index equals the column index. For multi-dimensional arrays, the axes specified by axis1 and axis2 define the matrix dimensions for diagonal summation. -The `.trace()` method supports various parameters to customize which diagonal to sum, making it versatile for different array shapes and orientations. It can handle multi-dimensional arrays by specifying the axes to consider as the matrix dimensions. +The `.trace()` method supports optional parameters to select a diagonal offset or specify axes, making it versatile for arrays of different shapes and orientations. ## Syntax ```pseudo -ndarray.trace(offset=0, axis1=0, axis2=1, dtype=None, out=None) +numpy.trace(a, offset=0, axis1=0, axis2=1, dtype=None, out=None) ``` **Parameters:** +- `a`: Input array. - `offset` (Optional): The offset of the diagonal from the main diagonal. A positive value selects a diagonal above the main diagonal, while a negative value selects one below. Default is `0` (main diagonal). - `axis1` (Optional): The axis to be used as the first axis of the 2D sub-arrays from which the diagonals should be taken. Default is `0`. - `axis2` (Optional): The axis to be used as the second axis of the 2D sub-arrays from which the diagonals should be taken. Default is `1`. @@ -86,5 +87,4 @@ trace_offset_1 = matrix.trace(offset=1) trace_offset_minus_1 = matrix.trace(offset=-1) print("Trace with offset 1 (above diagonal):", trace_offset_1) print("Trace with offset -1 (below diagonal):", trace_offset_minus_1) -``` -n:\Open_Source\docs\content\numpy\concepts\ndarray\terms\trace\trace.md +``` From dbdd9784540b1b1a3f1b7ee4106466de122a92ea Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Sat, 25 Oct 2025 13:59:07 +0530 Subject: [PATCH 3/4] Correct syntax for numpy.trace to ndarray.trace Updated the syntax for the numpy trace function to reflect the correct usage with ndarray. --- content/numpy/concepts/ndarray/terms/trace/trace.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/content/numpy/concepts/ndarray/terms/trace/trace.md b/content/numpy/concepts/ndarray/terms/trace/trace.md index f5774859f42..d8050388106 100644 --- a/content/numpy/concepts/ndarray/terms/trace/trace.md +++ b/content/numpy/concepts/ndarray/terms/trace/trace.md @@ -22,12 +22,11 @@ The `.trace()` method supports optional parameters to select a diagonal offset o ## Syntax ```pseudo -numpy.trace(a, offset=0, axis1=0, axis2=1, dtype=None, out=None) +ndarray.trace(offset=0, axis1=0, axis2=1, dtype=None, out=None) ``` **Parameters:** -- `a`: Input array. - `offset` (Optional): The offset of the diagonal from the main diagonal. A positive value selects a diagonal above the main diagonal, while a negative value selects one below. Default is `0` (main diagonal). - `axis1` (Optional): The axis to be used as the first axis of the 2D sub-arrays from which the diagonals should be taken. Default is `0`. - `axis2` (Optional): The axis to be used as the second axis of the 2D sub-arrays from which the diagonals should be taken. Default is `1`. From 0b8cda333bab71b64ec71ed55e9fe8a0ac457c4e Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Tue, 28 Oct 2025 14:35:07 +0530 Subject: [PATCH 4/4] Update trace.md --- content/numpy/concepts/ndarray/terms/trace/trace.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/content/numpy/concepts/ndarray/terms/trace/trace.md b/content/numpy/concepts/ndarray/terms/trace/trace.md index d8050388106..247c68a54d3 100644 --- a/content/numpy/concepts/ndarray/terms/trace/trace.md +++ b/content/numpy/concepts/ndarray/terms/trace/trace.md @@ -15,7 +15,7 @@ CatalogContent: - 'paths/data-science' --- -The **`.trace()`** method returns the sum of the elements along the diagonal of a NumPy array. For 2D arrays, the diagonal consists of elements where the row index equals the column index. For multi-dimensional arrays, the axes specified by axis1 and axis2 define the matrix dimensions for diagonal summation. +The **`.trace()`** method returns the sum of the elements along the diagonal of a [NumPy](https://www.codecademy.com/resources/docs/numpy) array. For 2D arrays, the diagonal consists of elements where the row index equals the column index. For multi-dimensional arrays, the axes specified by axis1 and axis2 define the matrix dimensions for diagonal summation. The `.trace()` method supports optional parameters to select a diagonal offset or specify axes, making it versatile for arrays of different shapes and orientations. @@ -27,11 +27,11 @@ ndarray.trace(offset=0, axis1=0, axis2=1, dtype=None, out=None) **Parameters:** -- `offset` (Optional): The offset of the diagonal from the main diagonal. A positive value selects a diagonal above the main diagonal, while a negative value selects one below. Default is `0` (main diagonal). +- `offset` (Optional): The diagonal offset from the main diagonal. A positive value selects a diagonal above the main diagonal, while a negative value selects one below. Default is `0` (main diagonal). - `axis1` (Optional): The axis to be used as the first axis of the 2D sub-arrays from which the diagonals should be taken. Default is `0`. - `axis2` (Optional): The axis to be used as the second axis of the 2D sub-arrays from which the diagonals should be taken. Default is `1`. - `dtype` (Optional): The data type of the returned array. If not specified, it is determined from the input array. -- `out` (Optional): An alternative output array in which to place the result. It must have the same shape as the expected output. +- `out` (Optional): An alternative output array to place the result. It must have the same shape as the expected output. **Return value:**