code: def _write(
      self,
      stream,
      a,
      comment="",
      field=None,
      precision=None,
      symmetry=None,
  ):
      if (
          isinstance(a, list)
          or isinstance(a, ndarray)
          or isinstance(a, tuple)
          or hasattr(a, "__array__")
      ):
          rep = self.FORMAT_ARRAY
          a = asarray(a)
          if len(a.shape) != 2:
              raise ValueError("Expected 2 dimensional array")
          rows, cols = a.shape
  
          if field is not None:
              if field == self.FIELD_INTEGER:
                  if not can_cast(a.dtype, "intp"):
                      raise OverflowError(
                          "mmwrite does not support integer "
                          "dtypes larger than native 'intp'."
                      )
                  a = a.astype("intp")
              elif field == self.FIELD_REAL:
                  if a.dtype.char not in "fd":
                      a = a.astype("d")
              elif field == self.FIELD_COMPLEX:
                  if a.dtype.char not in "FD":
                      a = a.astype("D")
  
      else:
          if not isspmatrix(a):
              raise ValueError("unknown matrix type: %s" % type(a))
  
          rep = "coordinate"
          rows, cols = a.shape
  
      typecode = a.dtype.char
  
      if precision is None:
          if typecode in "fF":
              precision = 8
          else:
              precision = 16
      if field is None:
          kind = a.dtype.kind
          if kind == "i":
              if not can_cast(a.dtype, "intp"):
                  raise OverflowError(
                      "mmwrite does not support integer "
                      "dtypes larger than native 'intp'."
                  )
              field = "integer"
          elif kind == "f":
              field = "real"
          elif kind == "c":
              field = "complex"
          elif kind == "u":
              field = "unsigned-integer"
          else:
              raise TypeError("unexpected dtype kind " + kind)
  
      if symmetry is None:
          symmetry = self._get_symmetry(a)
  
      # validate rep, field, and symmetry
      self.__class__._validate_format(rep)
      self.__class__._validate_field(field)
      self.__class__._validate_symmetry(symmetry)
  
      # write initial header line
      data = "%%MatrixMarket matrix {0} {1} {2}\n".format(
          rep, field, symmetry
      )
      stream.write(data.encode("latin1"))
  
      # write comments
      for line in comment.split("\n"):
          data = "%%%s\n" % (line)
          stream.write(data.encode("latin1"))
  
      template = self._field_template(field, precision)
      # write dense format
      if rep == self.FORMAT_ARRAY:
          # write shape spec
          data = "%i %i\n" % (rows, cols)
          stream.write(data.encode("latin1"))
  
          if field in (
              self.FIELD_INTEGER,
              self.FIELD_REAL,
              self.FIELD_UNSIGNED,
          ):
              if symmetry == self.SYMMETRY_GENERAL:
                  for j in range(cols):
                      for i in range(rows):
                          data = template % a[i, j]
                          stream.write(data.encode("latin1"))
  
              elif symmetry == self.SYMMETRY_SKEW_SYMMETRIC:
                  for j in range(cols):
                      for i in range(j + 1, rows):
                          data = template % a[i, j]
                          stream.write(data.encode("latin1"))
  
              else:
                  for j in range(cols):
                      for i in range(j, rows):
                          data = template % a[i, j]
                          stream.write(data.encode("latin1"))
  
          elif field == self.FIELD_COMPLEX:
              if symmetry == self.SYMMETRY_GENERAL:
                  for j in range(cols):
                      for i in range(rows):
                          aij = a[i, j]
                          data = template % (real(aij), imag(aij))
                          stream.write(data.encode("latin1"))
              else:
                  for j in range(cols):
                      for i in range(j, rows):
                          aij = a[i, j]
                          data = template % (real(aij), imag(aij))
                          stream.write(data.encode("latin1"))
  
          elif field == self.FIELD_PATTERN:
              raise ValueError(
                  "pattern type inconsisted with dense format"
              )
  
          else:
              raise TypeError("Unknown field type %s" % field)
  
      # write sparse format
      else:
          coo = a.tocoo()  # convert to COOrdinate format
  
          # if symmetry format used, remove values above main diagonal
          if symmetry != self.SYMMETRY_GENERAL:
              lower_triangle_mask = coo.row >= coo.col
              coo = coo_matrix(
                  (
                      coo.data[lower_triangle_mask],
                      (
                          coo.row[lower_triangle_mask],
                          coo.col[lower_triangle_mask],
                      ),
                  ),
                  shape=coo.shape,
              )
  
          # write shape spec
          data = "%i %i %i\n" % (rows, cols, coo.nnz)
          stream.write(data.encode("latin1"))
  
          template = self._field_template(field, precision - 1)
  
          if field == self.FIELD_PATTERN:
              for r, c in zip(coo.row + 1, coo.col + 1):
                  data = "%i %i\n" % (r, c)
                  stream.write(data.encode("latin1"))
          elif field in (
              self.FIELD_INTEGER,
              self.FIELD_REAL,
              self.FIELD_UNSIGNED,
          ):
              for r, c, d in zip(coo.row + 1, coo.col + 1, coo.data):
                  data = ("%i %i " % (r, c)) + (template % d)
                  stream.write(data.encode("latin1"))
          elif field == self.FIELD_COMPLEX:
              for r, c, d in zip(coo.row + 1, coo.col + 1, coo.data):
                  data = ("%i %i " % (r, c)) + (
                      template % (d.real, d.imag)
                  )
                  stream.write(data.encode("latin1"))
          else:
              raise TypeError("Unknown field type %s" % field)
  
imports: import os
  import sys
  from numpy import asarray, real, imag, conj, zeros, ndarray, concatenate, ones, can_cast
  from scipy.sparse import coo_matrix, isspmatrix
label_desc: Use image node_id 23 for calling the MMFile obj's underlying member method code with example usage: obj._write(stream, a, comment, field, precision, symmetry) without return types