code: def test_ndarray_qr():
      def assert_raw_equivalence(hl_ndarray, np_ndarray):
          ndarray_h, ndarray_tau = hl.eval(
              hl.nd.qr(hl_ndarray, mode="raw")
          )
          np_ndarray_h, np_ndarray_tau = np.linalg.qr(
              np_ndarray, mode="raw"
          )
  
          # Can't ask for the rank of something that has a 0 in its shape.
          if 0 in np_ndarray.shape:
              assert ndarray_h.shape == np_ndarray_h.shape
              assert ndarray_tau.shape == np_ndarray_tau.shape
          else:
              rank = np.linalg.matrix_rank(np_ndarray)
  
              assert np.allclose(
                  ndarray_h[:, :rank], np_ndarray_h[:, :rank]
              )
              assert np.allclose(
                  ndarray_tau[:rank], np_ndarray_tau[:rank]
              )
  
      def assert_r_equivalence(hl_ndarray, np_ndarray):
          assert np.allclose(
              hl.eval(hl.nd.qr(hl_ndarray, mode="r")),
              np.linalg.qr(np_ndarray, mode="r"),
          )
  
      def assert_reduced_equivalence(hl_ndarray, np_ndarray):
          q, r = hl.eval(hl.nd.qr(hl_ndarray, mode="reduced"))
          nq, nr = np.linalg.qr(np_ndarray, mode="reduced")
  
          # Can't ask for the rank of something that has a 0 in its shape.
          if 0 in np_ndarray.shape:
              assert q.shape == nq.shape
              assert r.shape == nr.shape
          else:
              rank = np.linalg.matrix_rank(np_ndarray)
  
              assert np.allclose(q[:, :rank], nq[:, :rank])
              assert np.allclose(r, nr)
              assert np.allclose(q @ r, np_ndarray)
  
      def assert_complete_equivalence(hl_ndarray, np_ndarray):
          q, r = hl.eval(hl.nd.qr(hl_ndarray, mode="complete"))
          nq, nr = np.linalg.qr(np_ndarray, mode="complete")
  
          # Can't ask for the rank of something that has a 0 in its shape.
          if 0 in np_ndarray.shape:
              assert q.shape == nq.shape
              assert r.shape == nr.shape
          else:
              rank = np.linalg.matrix_rank(np_ndarray)
  
              assert np.allclose(q[:, :rank], nq[:, :rank])
              assert np.allclose(r, nr)
              assert np.allclose(q @ r, np_ndarray)
  
      def assert_same_qr(hl_ndarray, np_ndarray):
          assert_raw_equivalence(hl_ndarray, np_ndarray)
          assert_r_equivalence(hl_ndarray, np_ndarray)
          assert_reduced_equivalence(hl_ndarray, np_ndarray)
          assert_complete_equivalence(hl_ndarray, np_ndarray)
  
      np_identity4 = np.identity(4)
      identity4 = hl.nd.array(np_identity4)
  
      assert_same_qr(identity4, np_identity4)
  
      np_size_zero_n = np.zeros((10, 0))
      size_zero_n = hl.nd.zeros((10, 0))
  
      assert_same_qr(size_zero_n, np_size_zero_n)
  
      np_size_zero_m = np.zeros((0, 10))
      size_zero_m = hl.nd.zeros((0, 10))
  
      assert_same_qr(size_zero_m, np_size_zero_m)
  
      np_all3 = np.full((3, 3), 3)
      all3 = hl.nd.full((3, 3), 3)
  
      assert_same_qr(all3, np_all3)
  
      np_nine_square = np.arange(9).reshape((3, 3))
      nine_square = hl.nd.arange(9).reshape((3, 3))
  
      assert_same_qr(nine_square, np_nine_square)
  
      np_wiki_example = np.array(
          [[12, -51, 4], [6, 167, -68], [-4, 24, -41]]
      )
      wiki_example = hl.nd.array(np_wiki_example)
  
      assert_same_qr(wiki_example, np_wiki_example)
  
      np_wide_rect = np.arange(12).reshape((3, 4))
      wide_rect = hl.nd.arange(12).reshape((3, 4))
  
      assert_same_qr(wide_rect, np_wide_rect)
  
      np_tall_rect = np.arange(12).reshape((4, 3))
      tall_rect = hl.nd.arange(12).reshape((4, 3))
  
      assert_same_qr(tall_rect, np_tall_rect)
  
      np_single_element = np.array([1]).reshape((1, 1))
      single_element = hl.nd.array([1]).reshape((1, 1))
  
      assert_same_qr(single_element, np_single_element)
  
      np_no_elements = np.array([]).reshape((0, 10))
      no_elements = hl.nd.array(np_no_elements)
  
      assert_same_qr(no_elements, np_no_elements)
  
      with pytest.raises(ValueError) as exc:
          hl.nd.qr(wiki_example, mode="invalid")
      assert "Unrecognized mode" in str(exc.value)
  
      with pytest.raises(AssertionError) as exc:
          hl.nd.qr(hl.nd.arange(6))
      assert "requires 2 dimensional" in str(exc.value)
  
imports: import math
  import numpy
  import re
  from ..helpers import *
  import pytest
  from hail.utils.java import FatalError, HailUserError
label_desc: Use image node_id 25 for calling a global function with example usage: test_ndarray_qr() without return types