<!--
{
  "availability" : [
    "*: -",
    "iOS: 15.0.0 -",
    "iPadOS: 15.0.0 -",
    "macCatalyst: -",
    "macOS: 12.0.0 -",
    "tvOS: 15.0.0 -",
    "visionOS: -",
    "watchOS: 8.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Accelerate",
  "identifier" : "/documentation/Accelerate/BNNS/computeNorm(input:output:axes:)",
  "metadataVersion" : "0.1.0",
  "role" : "Type Method",
  "symbol" : {
    "kind" : "Type Method",
    "modules" : [
      "Accelerate"
    ],
    "preciseIdentifier" : "s:10Accelerate4BNNSO11computeNorm5input6output4axesySo21BNNSNDArrayDescriptora_AISaySiGSgtKFZ"
  },
  "title" : "computeNorm(input:output:axes:)"
}
-->

# computeNorm(input:output:axes:)

Computes the Euclidean norm and writes the result to the output tensor.

```
static func computeNorm(input: BNNSNDArrayDescriptor, output: BNNSNDArrayDescriptor, axes: [Int]? = nil) throws
```

## Parameters

`input`

The descriptor of the input.

`output`

The descriptor of the output.

`axes`

The indices of the axes over which the function computes the norm. Set to `nil` to specify that the function computes the norm over the entire tensor.

## Discussion

Use this function to compute the norm of either an entire tensor or an axis or axes of a tensor.

For example, the following code defines a 3D tensor:

```swift
let inputValues: [Float] = [1, 2, 3,
                            4, 5, 6,
                            
                            10, 20, 30,
                            40, 50, 60,
                            
                            100, 200, 300,
                            400, 500, 600,
                            
                            1000, 2000, 3000,
                            4000, 5000, 6000]

let input = BNNSNDArrayDescriptor.allocate(
    initializingFrom: inputValues,
    shape: .imageCHW(3, 2, 4))
```

Define the `axes` parameter as either `[0, 1, 2]` or `nil` to specify that the operation computes the norm of the entire tensor. In this case, the norm is a scalar value, and the destination’s data layout must be a [`BNNS.DataLayout.vector`](/documentation/Accelerate/BNNS/DataLayout/vector) with a size of `1`.

```swift
let output = BNNSNDArrayDescriptor.allocateUninitialized(
    scalarType: Float.self,
    shape: .vector(1))

try? BNNS.computeNorm(input: input,
                     output: output,
                     axes: [0, 1, 2])

// Prints `[9587.45]`.
print(output.makeArray(of: Float.self)!)
```

On return, the output descriptor contains a single value that is the square root of the sum of squares of each element in the tensor:

![A diagram that depicts a 3D tensor as four 2D planes along the channel axis. Each plane is two elements high and three elements wide. The compute norm operation calculates a single value for the norm of the entire tensor.](images/com.apple.accelerate/media-3786765@2x.png)

Specify an `axes` of `[2]` to compute the norms along the second axis. In this case, the destination must be a matrix with a size that matches the zeroth and first dimensions of the source tensor:

```swift
let output = BNNSNDArrayDescriptor.allocateUninitialized(
    scalarType: Float.self,
    shape: .matrixColumnMajor(3, 2))

try? BNNS.computeNorm(input: input,
                     output: output,
                     axes: [2])

// Prints
//      [1005.0378, 2010.075, 3015.113,
//       4020.1511, 5025.189, 6030.227]
print(output.makeArray(of: Float.self)!)
```

On return, the output descriptor contains six values that are the norms of the slices along the second axis of the input tensor:

![A diagram that depicts a 3D tensor as four 2D planes along the channel axis. Each plane is two elements high and three elements wide. The compute norm operation calculates separate norms for the six 4-element vectors along the channel axis.](images/com.apple.accelerate/media-3786768@2x.png)

To compute the norm along more that one dimension, define the destination tensor with a size of the dimensions you’re not calculating over. For example, the following code defines an `axes` with a value of `[0, 2]` to compute the norm of dimensions zero and two:

```swift
let output = BNNSNDArrayDescriptor.allocateUninitialized(
    scalarType: Float.self,
    shape: .vector(2))

try? BNNS.computeNorm(input: input,
                     output: output,
                     axes: [0, 2])

// Prints `[3760.507, 8819.171]`
print(output.makeArray(of: Float.self)!)
```

On return, the output descriptor contains two values that are the norms of the top and bottom slices of the input tensor:

![A diagram that depicts a 3D tensor as four 2D planes along the channel axis. Each plane is two elements high and three elements wide. The compute norm operation calculates separate norms for the pair of three-by-four matrices defined by the channel axis and the width axis.](images/com.apple.accelerate/media-3786762@2x.png)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)