2.2 Attributes and Methods

A matrix has the following attributes.

size
A tuple with the dimensions of the matrix. This is a read-only attribute; operations that change the size of a matrix are not permitted.

typecode
A char, either 'i', 'd', or 'z', for integer, real and complex matrices, respectively. A read-only attribute.

trans( )
Returns the transpose of the matrix as a new matrix. One can also use A.T instead of A.trans().

ctrans( )
Returns the conjugate transpose of the matrix as a new matrix. One can also use A.H instead of A.ctrans().

real( )
For complex matrices, returns the real part as a real matrix. For integer and real matrices, returns a copy of the matrix.

imag( )
For complex matrices, returns the imaginary part as a real matrix. For integer and real matrices, returns an integer or real zero matrix.

__array_struct__
A PyCObject implementing the NumPy array interface (see section 2.8 for details).

tofile( f)
Writes the elements of the matrix in column-major order to a binary file f.

fromfile( f)
Reads the contents of a binary file f into the matrix object.

The last two methods are illustrated in the following example.

>>> from cvxopt.base import matrix
>>> A = matrix([[1.,2.,3.], [4.,5.,6.]])  
>>> print A
  1.0000e+00  4.0000e+00
  2.0000e+00  5.0000e+00
  3.0000e+00  6.0000e+00
>>> f = open('mat.bin','w')
>>> A.tofile(f)
>>> f.close()
>>> B = matrix(0.0, (2,3))
>>> f = open('mat.bin','r')
>>> B.fromfile(f)
>>> f.close()
>>> print B
  1.0000e+00  3.0000e+00  5.0000e+00
  2.0000e+00  4.0000e+00  6.0000e+00

Matrices can also be written to or read from files using the dump() and load() functions in the pickle module.