<!--
This file is generated by a tool. Do not edit directly.
For open-source contributions the docs will be updated automatically.
-->

*Last updated: 2023-03-16.*

<div itemscope itemtype="http://developers.google.com/ReferenceObject">
<meta itemprop="name" content="tf_quant_finance.utils.broadcast_common_batch_shape" />
<meta itemprop="path" content="Stable" />
</div>

# tf_quant_finance.utils.broadcast_common_batch_shape

<!-- Insert buttons and diff -->

<table class="tfo-notebook-buttons tfo-api" align="left">
</table>

<a target="_blank" href="https://github.com/paolodelia99/tf-quant-finance/blob/main/tf_quant_finance/utils/shape_utils.py">View source</a>



Broadcasts argument batch shapes to the common shape.

```python
tf_quant_finance.utils.broadcast_common_batch_shape(
    *, event_ranks=None, name=None, *args
)
```



<!-- Placeholder for "Used in" -->

Each input `Tensor` is assumed to be of shape `batch_shape_i + event_shape_i`.
The function finds a common `batch_shape` and broadcasts each `Tensor` to
`batch_shape + event_shape_i`. The common batch shape is the minimal shape
such that all `batch_shape_i` can broadcast to it.

#### Example 1. Batch shape is all dimensions but the last one
```python
import tensorflow as tf
import tf_quant_finance as tff

# Two Tensors of shapes [2, 3] and [2]. The batch shape of the 1st Tensor is
# [2] and for the second is []. The common batch shape is [2]
args = [tf.ones([2, 3], dtype=tf.float64), tf.constant([True, False])]
tff.utils.broadcast_common_batch_shape(*args)
# Expected: (array([[1., 1., 1.], [1., 1., 1.]]),
#            array([[True, True], [False, False]])
```

#### Example 2. Specify ranks of event shapes
```python
import tensorflow as tf
import tf_quant_finance as tff

args = [tf.ones([2, 3], dtype=tf.float64), tf.constant([True, False])]
tff.utils.broadcast_common_batch_shape(*args,
                                       event_ranks)
# Expected: (array([[1., 1., 1.], [1., 1., 1.]]),
#            array([[True, True], [False, False]])
```

#### Args:


* <b>`*args`</b>: A sequence of `Tensor`s of compatible shapes and any `dtype`s.
* <b>`event_ranks`</b>: A sequence of integers of the same length as `args` specifying
  ranks of `event_shape` for each input `Tensor`.
  Default value: `None` which means that all dimensions but the last one
  are treated as batch dimension.
* <b>`name`</b>: Python string. The name to give to the ops created by this function.
  Default value: `None` which maps to the default name
  `broadcast_tensor_shapes`.


#### Returns:

A tuple of broadcasted `Tensor`s. Each `Tensor` has the same `dtype` as the
corresponding input `Tensor`.



#### Raises:


* <b>`ValueError`</b>:   (a) If `event_ranks` is supplied and is of different from `args` length.
  (b) If inputs are of incompatible shapes.