Python sympy.ones() 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.ones() Method:

We can generate a matrix with dimension nxm filled with ones(1) by using the ones() method of the sympy module. Here n and m are passed as arguments.

where n = no of rows

m = no of columns

Syntax:

 sympy.ones()

Return Value:

A ones matrix is returned by the ones() function.

sympy.ones() Method in Python

Method #1: Using ones() Function (Static Input)

Approach:

  • Import all the functions from sympy module using the import keyword
  • Pass the number of rows and columns as arguments to the ones() function to generate a one’s matrix with the given number of rows and columns.
  • Here it generates a ones matrix with 3 rows and 3 columns.
  • Print the one’s matrix with the given number of rows and columns.
  • The Exit of the Program.

Below is the implementation:

# Import all the functions from sympy module using the import keyword
from sympy import *

# Pass the number of rows and columns as arguments to ones() function to 
# generate a one's matrix with the given number of rows and columns.
# Here it generates a ones matrix with 3 rows and 3 columns.
ones_matx = ones(3, 3)

# Print the ones matrix with the given number of rows and columns
print("The ones matrix with 3 rows and 3 columns:")
print(ones_matx)

Output:

The ones matrix with 3 rows and 3 columns:
Matrix([[1, 1, 1], [1, 1, 1], [1, 1, 1]])

Method #2: Using ones() Function (User Input)

Approach:

  • Import all the functions from sympy module using the import keyword
  • Give the number of rows as user input using the int(input()) function and store it in a variable.
  • Give the number of columns as user input using the int(input()) function
    and store it in another variable.
  • Pass the above-given number of rows and columns as arguments to the ones() function to generate a one’s matrix with the given number of rows and columns.
  • Print the one’s matrix with the given number of rows and columns.
  • The Exit of the Program.

Below is the implementation:

# Import all the functions from sympy module using the import keyword
from sympy import *
# Give the number of rows as user input using the int(input()) function 
# and store it in a variable.
gvn_rows = int(input("Enter some random no of rows = "))

# Give the number of columns as user input using the int(input()) function 
# and store it in another variable.
gvn_columns = int(input("Enter some random no of columns = "))

# Pass the above given number of rows and columns as arguments to ones() function 
# to generate a ones matrix with the given number of rows and columns.
ones_matx = ones(gvn_rows, gvn_columns)

# Print the ones matrix with the given number of rows and columns.
print("The ones matrix with {", gvn_rows, "} rows and{", gvn_columns,"}columns:")
print(ones_matx)

Output:

Enter some random no of rows = 2
Enter some random no of columns = 4
The ones matrix with { 2 } rows and{ 4 }columns:
Matrix([[1, 1, 1, 1], [1, 1, 1, 1]])