code: def test_sparse_matrix(self):
      # Test sparse matrix creation.
      sm1 = SparseMatrix(
          3, 4, [0, 2, 2, 4, 4], [1, 2, 1, 2], [1.0, 2.0, 4.0, 5.0]
      )
      self.assertEqual(sm1.numRows, 3)
      self.assertEqual(sm1.numCols, 4)
      self.assertEqual(sm1.colPtrs.tolist(), [0, 2, 2, 4, 4])
      self.assertEqual(sm1.rowIndices.tolist(), [1, 2, 1, 2])
      self.assertEqual(sm1.values.tolist(), [1.0, 2.0, 4.0, 5.0])
      self.assertTrue(
          repr(sm1),
          "SparseMatrix(3, 4, [0, 2, 2, 4, 4], [1, 2, 1, 2], [1.0, 2.0, 4.0, 5.0], False)",
      )
  
      # Test indexing
      expected = [[0, 0, 0, 0], [1, 0, 4, 0], [2, 0, 5, 0]]
  
      for i in range(3):
          for j in range(4):
              self.assertEqual(expected[i][j], sm1[i, j])
      self.assertTrue(array_equal(sm1.toArray(), expected))
  
      for i, j in [(-1, 1), (4, 3), (3, 5)]:
          self.assertRaises(IndexError, sm1.__getitem__, (i, j))
  
      # Test conversion to dense and sparse.
      smnew = sm1.toDense().toSparse()
      self.assertEqual(sm1.numRows, smnew.numRows)
      self.assertEqual(sm1.numCols, smnew.numCols)
      self.assertTrue(array_equal(sm1.colPtrs, smnew.colPtrs))
      self.assertTrue(array_equal(sm1.rowIndices, smnew.rowIndices))
      self.assertTrue(array_equal(sm1.values, smnew.values))
  
      sm1t = SparseMatrix(
          3,
          4,
          [0, 2, 3, 5],
          [0, 1, 2, 0, 2],
          [3.0, 2.0, 4.0, 9.0, 8.0],
          isTransposed=True,
      )
      self.assertEqual(sm1t.numRows, 3)
      self.assertEqual(sm1t.numCols, 4)
      self.assertEqual(sm1t.colPtrs.tolist(), [0, 2, 3, 5])
      self.assertEqual(sm1t.rowIndices.tolist(), [0, 1, 2, 0, 2])
      self.assertEqual(sm1t.values.tolist(), [3.0, 2.0, 4.0, 9.0, 8.0])
  
      expected = [[3, 2, 0, 0], [0, 0, 4, 0], [9, 0, 8, 0]]
  
      for i in range(3):
          for j in range(4):
              self.assertEqual(expected[i][j], sm1t[i, j])
      self.assertTrue(array_equal(sm1t.toArray(), expected))
  
imports: import array
  import unittest
  from numpy import array, array_equal, zeros, arange, tile, ones, inf
  import pyspark.ml.linalg
  from pyspark.serializers import CPickleSerializer
  from pyspark.mllib.linalg import Vector, SparseVector, DenseVector, VectorUDT, _convert_to_vector, DenseMatrix, SparseMatrix, Vectors, Matrices, MatrixUDT
  from pyspark.mllib.linalg.distributed import RowMatrix, IndexedRowMatrix, IndexedRow
  from pyspark.mllib.regression import LabeledPoint
  from pyspark.sql import Row
  from pyspark.testing.mllibutils import MLlibTestCase
  from pyspark.testing.utils import have_scipy
label_desc: Use image node_id 14 for calling the VectorTests obj's underlying member method code with example usage: obj.test_sparse_matrix() without return types