<jupyter_start><jupyter_text>Creating Generalized Ufuncs with NumbaNumba offers the `guvectorize` to generate **generalized ufuncs** which work of input arrays with different dimensions.<jupyter_code>import numpy as np
import numba<jupyter_output><empty_output><jupyter_text>Adding a constant to a vector<jupyter_code>@numba.guvectorize(['(f8[:], f8[:], f8[:])'], '(m),()->(m)')
def vec_add_const(x, y, z):
    for i in range(x.shape[0]):
        z[i] = x[i] + y[0]
x = np.arange(10.0)
z = vec_add_const(x, 2.0)
print(x, z, sep='\n')
x = np.arange(10.0).reshape(2, 5)
z = vec_add_const(x, 2.0)
print(x, z, sep='\n')
x = np.arange(10.0).reshape(2, 5)
y = np.array([1., 2.])
z = vec_add_const(x, y)
print(x, z, sep='\n')<jupyter_output><empty_output><jupyter_text>Matrix Vector Multiplication<jupyter_code>@numba.guvectorize(['(f8[:, :], f8[:], f8[:])'], '(m,n),(n)->(m)')
def mat_vec_mult(x, y, z):
    for i in range(x.shape[0]):
        d = 0.0
        for j in range(x.shape[1]):
            d += x[i, j] * y[j]
        z[i] = d
A = np.arange(9.0).reshape(3, 3)
x = np.array([1., 2., 3.])
z = mat_vec_mult(A, x)
print(A, z, sep='\n\n')
A = np.arange(27.0).reshape(3, 3, 3)
x = np.array([1., 2., 3])
z = mat_vec_mult(A, x)
print(A, z, sep='\n\n')<jupyter_output><empty_output><jupyter_text>Matrix-Matrix Multiplication<jupyter_code>@numba.guvectorize(['(f8[:, :], f8[:, :], f8[:, :])'], '(m,n),(n,k)->(m, k)')
def mat_mul(x, y, z):
    for i in range(x.shape[0]):
        for j in range(y.shape[1]):
            d = 0.0
            for k in range(x.shape[1]):
                d += x[i, k] * y[k, j]
            z[i, j] = d
A = np.arange(9.0).reshape(3, 3)
B = np.arange(9.0, 24.0).reshape(3, 5)
C = mat_mul(A, B)
C_numpy = A @ B
print(C, C_numpy, sep='\n\n')
numba.guvectorize?<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.22842920353982302, 'fraction_numerical': 0.03650442477876106, 'mean_word_length': 3.7605263157894737, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '245162', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 861, 'n_tokens_neox': 838, 'n_words': 173, 'path': 'numba/numba-cpu/Numba_guvec.ipynb', 'repo_name': 'edoardob90/PythonHPC', 'size': '1808'}