Python sympy.Matrix.col() Method

Python SymPy Module:

SymPy is a Python symbolic mathematics library. It aims to be a full-featured computer algebra system (CAS) while keeping the code as basic(simple) as possible in order to be understandable and easily expandable. SymPy is entirely written in Python. SymPy is simple to use because it only depends on mpmath, a pure Python library for arbitrary floating-point arithmetic.

Rational and Integer are the numerical types defined by SymPy. A rational number is represented by the Rational class as a pair of two Integers, numerator and denominator, therefore Rational(1, 2) is 1/2, Rational(3, 2) is 3/2, and so on. Integer numbers are represented by the Integer class.

SymPy uses mpmath in the background, allowing it to execute arbitrary-precision arithmetic computations. Some special constants, such as exp, pi, and oo (Infinity), are thus considered as symbols and can be evaluated with arbitrary precision.

Installation:

pip install sympy

Python sympy.Matrix.col() Method:

The columns of the matrix can be extracted using the sympy.Matrix().col() method.

Syntax:

 sympy.Matrix().col()

Return Value:

The column values of a given matrix are returned by the sympy.Matrix().col() function.

sympy.Matrix.col() Method in Python

For 2-Dimensional (2D) Matrix

Approach:

  • Import all the methods from sympy using the import keyword
  • Create a 2-Dimensional (2D) matrix using the Matrix() function and apply col() function on it by passing the column number as an argument to it to get the values of the specified column.
  • Here it gives the values of the 0th column.
  • Store it in a variable
  • Print the values of the specified column.
  • The Exit of the Program.

Below is the implementation:

# Import all the methods from sympy using the import keyword
from sympy import *

# Create a 2-Dimensional(2D)matrix using the Matrix() function and apply col() function on it by 
# passing the column number as an argument to it to get the values of the specified column.
# Here it gives the values of 0th column.
# Store it in a variable
col_values = Matrix([[3, 2], [8, 7]]).col(0)

# Print the values of the specified column
print("The values of the 0th column:")
print(col_values)

Output:

The values of the 0th column:
Matrix([[3], [8]])

For 3-Dimensional (3D) Matrix

Approach:

  • Import all the methods from sympy using the import keyword
  • Create a 3-Dimensional (3D) matrix using the Matrix() function and apply col() function on it by passing the column number as an argument to it to get the values of the specified column.
  • Here it gives the values of the 1st column.
  • Store it in a variable
  • Print the values of the specified column.
  • The Exit of the Program.

Below is the implementation:

# Import all the methods from sympy using the import keyword
from sympy import *

# Create a 3-Dimensional(3D) matrix using the Matrix() function and apply col() function on it by 
# passing the column number as an argument to it to get the values of the specified column.
# Here it gives the values of 1st column.
# Store it in a variable
col_values = Matrix([[1, 6, 10], [8, 7, 5], [3, 2, 9]]).col(1)

# Print the values of the specified column
print("The values of the 1st column:")
print(col_values)

Output:

The values of the 1st column:
Matrix([[6], [7], [2]])