code: def _basis_matrix(X, hdmr):
      """The basis matrix represents the foundation of the component functions.
      It is constructed using orthonormal polynomials for each input variable,
      ensuring that it captures the data optimally. The component functions are
      formed by linearly combining the columns of this matrix.
  
      Parameters
      ----------
      X : numpy.array
          Model input matrix
      hdmr : namedtuple
          Core parameters of hdmr expansion
  
      Returns
      -------
      b_m : numpy.array
          Basis matrix
      """
      # Compute normalized X-values
      X_n = (X - np.tile(X.min(0), (X.shape[0], 1))) / np.tile(
          (X.max(0)) - X.min(0), (X.shape[0], 1)
      )
  
      # Compute Orthonormal Polynomial Coefficients
      coeff = _orth_poly_coeff(X_n, hdmr)
  
      # Initialize Basis Matrix
      b_m = np.zeros((X.shape[0], hdmr.a_tnt))
  
      # First order columns of basis matrix
      col = 0
      for i, j in product(range(hdmr.d), range(hdmr.p_o)):
          b_m[:, col] = np.polyval(coeff[j, : j + 2, i], X_n[:, i])
          col += 1
  
      # Second order columns of basis matrix
      if hdmr.max_order > 1:
          for i, j in _prod(range(0, hdmr.d - 1), range(1, hdmr.d)):
              if hdmr.ext_base:
                  b_m[:, col : col + hdmr.p_o] = b_m[
                      :, i * hdmr.p_o : (i + 1) * hdmr.p_o
                  ]
                  col += hdmr.p_o
  
                  b_m[:, col : col + hdmr.p_o] = b_m[
                      :, j * hdmr.p_o : (j + 1) * hdmr.p_o
                  ]
                  col += hdmr.p_o
  
              for k1, k2 in _prod(
                  range(i * hdmr.p_o, (i + 1) * hdmr.p_o),
                  range(j * hdmr.p_o, (j + 1) * hdmr.p_o),
              ):
                  b_m[:, col] = np.multiply(b_m[:, k1], b_m[:, k2])
                  col += 1
  
      # Third order columns of basis matrix
      if hdmr.max_order == 3:
          for i, j, k in _prod(
              range(0, hdmr.d - 2),
              range(1, hdmr.d - 1),
              range(2, hdmr.d),
          ):
              if hdmr.ext_base:
                  b_m[:, col : col + hdmr.p_o] = b_m[
                      :, i * hdmr.p_o : (i + 1) * hdmr.p_o
                  ]
                  col += hdmr.p_o
  
                  b_m[:, col : col + hdmr.p_o] = b_m[
                      :, j * hdmr.p_o : (j + 1) * hdmr.p_o
                  ]
                  col += hdmr.p_o
  
                  b_m[:, col : col + hdmr.p_o] = b_m[
                      :, k * hdmr.p_o : (k + 1) * hdmr.p_o
                  ]
                  col += hdmr.p_o
  
                  p_o_2 = hdmr.p_o**2
                  b_m[:, col : col + p_o_2] = b_m[
                      :,
                      hdmr.tnt1
                      + (2 * hdmr.nt1) * (i + 1)
                      + i * p_o_2 : hdmr.tnt1
                      + (i + 1) * (p_o_2 + 2 * hdmr.nt1),
                  ]
                  col += p_o_2
  
                  b_m[:, col : col + p_o_2] = b_m[
                      :,
                      hdmr.tnt1
                      + (2 * hdmr.nt1) * (j + 1)
                      + j * p_o_2 : hdmr.tnt1
                      + (j + 1) * (p_o_2 + 2 * hdmr.nt1),
                  ]
                  col += p_o_2
                  b_m[:, col : col + p_o_2] = b_m[
                      :,
                      hdmr.tnt1
                      + (2 * hdmr.nt1) * (k + 1)
                      + k * p_o_2 : hdmr.tnt1
                      + (k + 1) * (p_o_2 + 2 * hdmr.nt1),
                  ]
                  col += p_o_2
  
              for l1, l2, l3 in _prod(
                  range(i * hdmr.p_o, (i + 1) * hdmr.p_o),
                  range(j * hdmr.p_o, (j + 1) * hdmr.p_o),
                  range(k * hdmr.p_o, (k + 1) * hdmr.p_o),
              ):
                  b_m[:, col] = np.multiply(
                      np.multiply(b_m[:, l1], b_m[:, l2]), b_m[:, l3]
                  )
                  col += 1
  
      return b_m
  
imports: import math
  import warnings
  from typing import Dict, Tuple
  from types import MethodType
  from itertools import combinations, product
  from collections import defaultdict, namedtuple
  import numpy
  from pandas import DataFrame
  from numpy.linalg import det, pinv, matrix_rank
  from scipy.linalg import svd, LinAlgError, solve
  from scipy import stats, special
  from .None import common_args
  from ..util import read_param_file, ResultDict
label_desc: Use image node_id 4 for calling a global function with example usage: _basis_matrix(X, hdmr) and returns: b_m