code: def test_grad_matrix(rng):
      """Test for matricial form of gradient."""
      _, _, w, mask, *_ = _make_data()
  
      G = get_gradient_matrix(w.size, mask)
  
      image_buffer = np.zeros(mask.shape)
      grad_mask = np.array([mask for _ in range(mask.ndim)])
      for _ in range(10):
          v = rng.random(w.size) * rng.integers(1000)
          image_buffer[mask] = v
          assert_almost_equal(
              gradient(image_buffer)[grad_mask], np.dot(G, v)
          )
  
imports: import numpy
  import pytest
  import scipy
  from nibabel import Nifti1Image
  from numpy.testing import assert_almost_equal
  from scipy import linalg
  from nilearn.decoding._objective_functions import divergence, gradient
  from nilearn.decoding.space_net import BaseSpaceNet
  from nilearn.decoding.space_net_solvers import _graph_net_adjoint_data_function, _graph_net_data_function, _logistic_data_loss_and_spatial_grad, _logistic_data_loss_and_spatial_grad_derivative, _logistic_derivative_lipschitz_constant, _squared_loss_and_spatial_grad, _squared_loss_and_spatial_grad_derivative, _squared_loss_derivative_lipschitz_constant, mfista
  from nilearn.decoding.tests._testing import create_graph_net_simulation_data
  from .test_same_api import to_niimgs
label_desc: Use image node_id 3 for calling a global function with example usage: test_grad_matrix(rng) without return types