<!--
{
  "availability" : [
    "iOS: 16.0.0 - 18.0.0",
    "iPadOS: 16.0.0 - 18.0.0",
    "macCatalyst: 16.0.0 - 18.0.0",
    "macOS: 13.0.0 - 15.0.0",
    "tvOS: 16.0.0 - 18.0.0",
    "visionOS: 1.0.0 - 2.0.0",
    "watchOS: 9.0.0 - 11.0.0"
  ],
  "documentType" : "symbol",
  "framework" : "Accelerate",
  "identifier" : "/documentation/Accelerate/BNNSMatMul(_:_:_:_:_:_:_:_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Function",
  "symbol" : {
    "kind" : "Function",
    "modules" : [
      "Accelerate"
    ],
    "preciseIdentifier" : "c:@F@BNNSMatMul"
  },
  "title" : "BNNSMatMul(_:_:_:_:_:_:_:_:)"
}
-->

# BNNSMatMul(_:_:_:_:_:_:_:_:)

Applies a matrix multiplication operation directly to two input matrices.

```
func BNNSMatMul(_ transA: Bool, _ transB: Bool, _ alpha: Float, _ inputA: UnsafePointer<BNNSNDArrayDescriptor>, _ inputB: UnsafePointer<BNNSNDArrayDescriptor>, _ output: UnsafePointer<BNNSNDArrayDescriptor>, _ workspace: UnsafeMutableRawPointer?, _ filter_params: UnsafePointer<BNNSFilterParameters>?) -> Int32
```

## Parameters

`transA`

A Boolean value that specifies whether the operation should treat `inputA` as transposed.

`transB`

A Boolean value that specifies whether the operation should treat `inputB` as transposed.

`alpha`

A value that the operation uses to scale the result.

`inputA`

A pointer to the `inputA` matrix descriptor.

`inputB`

A pointer to the `inputB` matrix descriptor.

`output`

A pointer to the output matrix descriptor.

`workspace`

An optional pointer to the workspace memory. Use [`BNNSMatMulWorkspaceSize(_:_:_:_:_:_:_:)`](/documentation/Accelerate/BNNSMatMulWorkspaceSize(_:_:_:_:_:_:_:)) to calculate the workspace size that operation requires. [`BNNSMatMul(_:_:_:_:_:_:_:_:)`](/documentation/Accelerate/BNNSMatMul(_:_:_:_:_:_:_:_:)) doesn’t require any particular alignment for the workspace memory.

`filter_params`

The filter runtime parameters.

## Discussion

Use this function to perform the operation `C = alpha * op(A) * op(B)` where `op` transposes the corresponding matrix if the appropriate transpose parameter is `true`. The function broadcasts dimensions that are absent on either input matrix. The matrix multiplication is always on the final two indices of each operand.

For example, the following arrays of values and descriptors for the matrix multiply inputs and outputs define a matrix multiplication operation without broadcasting. Note that the operation repeats the values in `inputBValues` along the third dimension.

```swift
let inputAValues: [Float] = [
    [ 24 values ]
]

let inputBValues: [Float] = [
    1, 2,
    3, 4,
    
    1, 2,
    3, 4,
    
    1, 2,
    3, 4
]

var inputADescriptor = BNNSNDArrayDescriptor.allocate(
    initializingFrom: inputAValues,
    shape: .imageCHW(3, 4, 2))

var inputBDescriptor = BNNSNDArrayDescriptor.allocate(
    initializingFrom: inputBValues,
    shape: .tensor3DFirstMajor(3, 2, 2))

var outputDescriptor = BNNSNDArrayDescriptor.allocateUninitialized(
    scalarType: Float.self,
    shape: .imageCHW(inputADescriptor.shape.size.0,
                     inputADescriptor.shape.size.1,
                     inputBDescriptor.shape.size.1))
```

The [`BNNSMatMul(_:_:_:_:_:_:_:_:)`](/documentation/Accelerate/BNNSMatMul(_:_:_:_:_:_:_:_:)) function calculates the same result using a 2 x 2 matrix.

```swift
let inputBValues: [Float] = [
    1, 2,
    3, 4
]

var inputBDescriptor = BNNSNDArrayDescriptor.allocate(
    initializingFrom: inputBValues,
    shape: .matrixFirstMajor(2, 2))
```

In both cases, the call to the matrix multiply function is the same.

```swift
BNNSMatMul(false, false,
           1,
           &inputADescriptor, &inputBDescriptor,
           &outputDescriptor,
           nil, nil)
```

You may optionally pass a workspace to [`BNNSMatMul(_:_:_:_:_:_:_:_:)`](/documentation/Accelerate/BNNSMatMul(_:_:_:_:_:_:_:_:)). Call [`BNNSMatMulWorkspaceSize(_:_:_:_:_:_:_:)`](/documentation/Accelerate/BNNSMatMulWorkspaceSize(_:_:_:_:_:_:_:)) to calculate the required workspace size for a set of parameters. If you pass `nil` to the [`BNNSMatMul(_:_:_:_:_:_:_:_:)`](/documentation/Accelerate/BNNSMatMul(_:_:_:_:_:_:_:_:)) workspace parameter, BNNS allocates and dellocates the workspace.

---

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)