'numpy.concatenate((A, B, C)) = A when B and C are None

I have the following piece of code inside the following function:-

import numpy as np

def fun(A, B=None, C=None):
      M = np.concatenate((A, B, C))

Where A, B, and C are matrices with the same number of columns.

When B and C are NOT None, there is no problem. However, when at least one of them is None, we will have an error with the dimensions.

My Question: is there an elegant way to have M = np.concatenate((A, B, C)) = A when both B and C are None?



Solution 1:[1]

Use Variable-length Argument:

>>> def connect(A, *args):
...     return np.concatenate((A,) + args)
...
>>> connect(np.arange(10))
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> connect(np.arange(10), np.arange(5))
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4])
>>> connect(*([np.arange(3)] * 5))
array([0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2])

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mechanic Pig