Author name: Vikram Chiluka

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

The mathematical expressions can be evaluated using the sympy.evalf() method.

Syntax:

 sympy.evalf()

Return Value:

The evaluated mathematical expression is returned by the evalf() function.

sympy.evalf() Method Python

Example1

Approach:

  • Import all the functions from sympy using the import keyword
  • Give the mathematical expression as static input and store it in a variable.
  • Apply evalf() function on the above given mathematical expression to evaluate the given mathematical expression.
  • Store it in another variable.
  • Print the result of the evaluated mathematical expression.
  • The Exit of the Program.

Below is the implementation:

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

# Give the mathematical expression as static input and store it in a variable.
gvn_expression = sqrt(16)

# Apply evalf() function on the above given mathematical expression to 
# evaluate the given mathematical expression. 
# Store it in another variable.
rslt = gvn_expression.evalf()
# Print the result of the evaluated mathematical expression. 
print("The result of the evaluated mathematical expression:") 
print(rslt)

Output:

The result of the evaluated mathematical expression:
4.00000000000000

Example2

Approach:

  • Import all the functions from sympy using the import keyword
  • Give the mathematical expression as static input and store it in a variable.
  • Apply evalf() function on the above given mathematical expression to evaluate the given mathematical expression.
  •  Here we multiply the values of sin(45) and cos(45).
  • Store it in another variable.
  • Print the result of the evaluated mathematical expression.
  • The Exit of the Program.

Below is the implementation:

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

# Give the mathematical expression as static input and store it in a variable.
gvn_expression = sin(45)*cos(45)

# Apply evalf() function on the above given mathematical expression to 
# evaluate the given mathematical expression. 
# Here we multiply the values of sin(45) and cos(45).
# Store it in another variable.
rslt = gvn_expression.evalf()
# Print the result of the evaluated mathematical expression. 
print("The result of the evaluated mathematical expression:") 
print(rslt)

Output:

The result of the evaluated mathematical expression:
0.446998331800279

Python sympy.evalf() Method Read More »

Python Numpy matrix.ravel() Function

NumPy Library 

NumPy is a library in python that is created to work efficiently with arrays in python. It is fast, easy to learn, and provides efficient storage. It also provides a better way of handling data for the process. We can create an n-dimensional array in NumPy. To use NumPy simply have to import it in our program and then we can easily use the functionality of NumPy in our program.

NumPy is a Python library that is frequently used for scientific and statistical analysis. NumPy arrays are grids of the same datatype’s values.

Numpy matrix.ravel() Function:

We can obtain the flattened matrix from a given matrix using the matrix.ravel() function of the NumPy module.

Syntax:

 matrix.ravel()

Return Value:

The flattened matrix from a given matrix is returned by the ravel() function.

What is a Flattened matrix?

Flattening a matrix means flattening the given n-dimensional matrix to a one-Dimensional(1D) matrix.

Example:

Let matrix be:

1     2     3

4     5      6

7     8      9

Matrix after Flattening:

[1 2 3 4 5 6 7 8 9]

Numpy matrix.ravel() Function in Python

For 2-Dimensional (2D) Matrix

Approach:

  • Import numpy module using the import keyword
  • Create a matrix(2-Dimensional) using the matrix() function of numpy module by passing some random 2D matrix as an argument to it and store it in a variable
  • Apply the ravel() function on the given matrix to get the flattened matrix from a given matrix.
  • Here, it flattens to a 1-Dimensional matrix.
  • Store it in another variable
  • Print the flattened matrix from a given matrix.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
            
# Create a matrix(2-Dimensional) using the matrix() function of numpy module by passing 
# some random 2D matrix as an argument to it and store it in a variable
gvn_matrx = np.matrix('[1, 2; 4, 5]')
            
# Apply the ravel() function on the given matrix to get the flattened matrix from a given matrix.
# Here, it flattens to a 1-Dimensional matrix.
# Store it in another variable
rslt = gvn_matrx.ravel()
# Print the flattened matrix from a given matrix
print("The flattened matrix from a given matrix:")
print(rslt)

Output:

The flattened matrix from a given matrix:
[[1 2 4 5]]

For 3-Dimensional (3D) Matrix

Approach:

  • Import numpy module using the import keyword
  • Create a matrix(3-Dimensional) using the matrix() function of numpy module by passing some random 3D matrix as an argument to it and store it in a variable
  • Apply the ravel() function on the given matrix to get the flattened matrix from a given matrix.
  • Here, it flattens to a 1-Dimensional matrix.
  • Store it in another variable
  • Print the flattened matrix from a given matrix.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
            
# Create a matrix(3-Dimensional) using the matrix() function of numpy module by passing 
# some random 3D matrix as an argument to it and store it in a variable
gvn_matrx = np.matrix('[2, 4, 1; 8, 7, 3; 10, 9, 5]')
            
# Apply ravel() function on the given matrix to get the flattened matrix from a given matrix.
# Here, it flattens to 1-dimensional matrix
# Store it in another variable
rslt = gvn_matrx.ravel()
# Print the flattened matrix from a given matrix
print("The flattened matrix from a given matrix:")
print(rslt)

Output:

The flattened matrix from a given matrix:
[[ 2 4 1 8 7 3 10 9 5]]

 

Python Numpy matrix.ravel() Function Read More »

Python Numpy matrix.diagonal() Function

NumPy Library 

NumPy is a library in python that is created to work efficiently with arrays in python. It is fast, easy to learn, and provides efficient storage. It also provides a better way of handling data for the process. We can create an n-dimensional array in NumPy. To use NumPy simply have to import it into our program and then we can easily use the functionality of NumPy in our program.

NumPy is a Python library that is frequently used for scientific and statistical analysis. NumPy arrays are grids of the same datatype’s values.

Numpy matrix.diagonal() Function:

We can find a diagonal element from a given matrix using the Numpy matrix.diagonal() method, which returns a one-dimensional matrix as output.

Syntax:

 matrix.diagonal()

Return Value:

The diagonal element of a given matrix is returned by the diagonal() function.

Numpy matrix.diagonal() Function in Python

For 2-Dimensional (2D) Matrix

Approach:

  • Import numpy module using the import keyword
  • Create a matrix(2-Dimensional) using the matrix() function of numpy module by passing some random 2D matrix as an argument to it and store it in a variable.
  • Apply diagonal() function on the given matrix to get all the diagonal elements of a given matrix.
  • Store it in another variable
  • Print all the diagonal elements of a given matrix.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
            
# Create a matrix(2-Dimensional) using the matrix() function of numpy module by passing 
# some random 2D matrix as an argument to it and store it in a variable
gvn_matrx = np.matrix('[2, 1; 6, 3]')
            
# Apply diagonal() function on the given matrix to get all the diagonal elements of a given matrix.
# Store it in another variable
rslt = gvn_matrx.diagonal()
# Print all the diagonal elements of a given matrix.
print("The diagonal elements of a given matrix:")
print(rslt)

Output:

The diagonal elements of a given matrix:
[[2 3]]

For 3-Dimensional (3D) Matrix

Approach:

  • Import numpy module using the import keyword
  • Create a matrix(3-Dimensional) using the matrix() function of numpy module by passing some random 3D matrix as an argument to it and store it in a variable.
  • Apply diagonal() function on the given matrix to get all the diagonal elements of a given matrix.
  • Store it in another variable
  • Print all the diagonal elements of a given matrix.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
            
# Create a matrix(3-Dimensional) using the matrix() function of numpy module by passing 
# some random 3D matrix as an argument to it and store it in a variable
gvn_matrx = np.matrix('[2, 4, 1; 8, 7, 3; 10, 9, 5]')
            
# Apply diagonal() function on the given matrix to get all the diagonal elements of a given matrix.
# Store it in another variable
rslt = gvn_matrx.diagonal()
# Print all the diagonal elements of a given matrix.
print("The diagonal elements of a given matrix:")
print(rslt)

Output:

The diagonal elements of a given matrix:
[[2 7 5]]

Python Numpy matrix.diagonal() Function Read More »

Python Numpy matrix.astype() Function

NumPy Library 

NumPy is a library in python that is created to work efficiently with arrays in python. It is fast, easy to learn, and provides efficient storage. It also provides a better way of handling data for the process. We can create an n-dimensional array in NumPy. To use NumPy simply have to import it into our program and then we can easily use the functionality of NumPy in our program.

NumPy is a Python library that is frequently used for scientific and statistical analysis. NumPy arrays are grids of the same datatype’s values.

Numpy matrix.astype() Function:

We can convert the type of matrix using the matrix.astype() method of the NumPy module, but there is a problem with data loss. For example, if we wish to convert a float to an int, some of the data will be lost. This approach helps in matrix-type conversion.

Syntax:

 matrix.astype()

Return Value:

The matrix after type conversion is returned by the astype() function.

Numpy matrix.astype() Function in Python

For 2-Dimensional (2D) Matrix

Approach:

  • Import numpy module using the import keyword
  • Create a matrix(2-Dimensional) using the matrix() function of numpy module by passing some random 2D matrix as an argument to it and store it in a variable
  • Apply astype() function on the given matrix by passing the datatype as an argument to it to convert all the elements of a given matrix to the specified datatype.
  • Store it in another variable.
  • Print the given matrix after type conversion.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
            
# Create a matrix(2-Dimensional) using the matrix() function of numpy module by passing 
# some random 2D matrix as an argument to it and store it in a variable
gvn_matrx = np.matrix('[2.3, 1.5; 6.8, 3]')
            
# Apply astype() function on the given matrix by passing the datatype as an argument to it 
# to convert all the elements of a given matrix to the specified datatype.
# Store it in another variable
rslt = gvn_matrx.astype(int)
# Print the given matrix after type conversion
print("The given matrix after type conversion:")
print(rslt)

Output:

The given matrix after type conversion:
[[2 1]
 [6 3]]

Explanation:

Here it converts the datatype of all the elements of the given matrix 
to an integer

For 3-Dimensional (3D) Matrix

Approach:

  • Import numpy module using the import keyword
  • Create a matrix(3-Dimensional) using the matrix() function of numpy module by passing some random 3D matrix as an argument to it and store it in a variable
  • Apply astype() function on the given matrix by passing the datatype as an argument to it to convert all the elements of a given matrix to the specified datatype.
  • Store it in another variable.
  • Print the given matrix after type conversion.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
            
# Create a matrix(3-Dimensional) using the matrix() function of numpy module by passing 
# some random 3D matrix as an argument to it and store it in a variable
gvn_matrx = np.matrix('[2.2, 4, 10.3; 8.8, 7, 3.3; 10.1, 9.9, 5.]')
            
# Apply astype() function on the given matrix by passing the datatype as an argument to it 
# to convert all the elements of a given matrix to the specified datatype.
# Store it in another variable
rslt = gvn_matrx.astype(int)
# Print the given matrix after type conversion
print("The given matrix after type conversion:")
print(rslt)

Output:

The given matrix after type conversion:
[[ 2 4 10]
 [ 8 7 3]
 [10 9 5]]

Python Numpy matrix.astype() Function Read More »

Python Numpy matrix.conj() Function

NumPy Library 

NumPy is a library in python that is created to work efficiently with arrays in python. It is fast, easy to learn, and provides efficient storage. It also provides a better way of handling data for the process. We can create an n-dimensional array in NumPy. To use NumPy simply have to import it into our program and then we can easily use the functionality of NumPy in our program.

NumPy is a Python library that is frequently used for scientific and statistical analysis. NumPy arrays are grids of the same datatype’s values.

Numpy matrix.conj() Function:

We can obtain the conjugate of a given matrix with one or more dimensions using the matrix.conj() method of the Numpy module.

Syntax:

 matrix.conj()

Return Value:

The conjugate of a given matrix is returned by the matrix.conj() function.

Conjugate of a Matrix:

A conjugate matrix is a complex matrix in which all of its elements have been replaced by their complex conjugates, i.e. the sign of all the imaginary part of the complex numbers has been changed.

Example:

Let matrix = [1+3j,  2-4j]
conjugate of a matrix = [1-3j,  2+4j]

Numpy matrix.conj() Function in Python

For 1-Dimensional (1D) Matrix

Approach:

  • Import numpy module using the import keyword.
  • Create a complex matrix(1-Dimensional) using the matrix() function of numpy module by passing some random 1D matrix as an argument to it and store it in a variable.
  • Apply conj() function on the given matrix to get the conjugate of the given complex matrix
  • Store it in another variable.
  • Print the conjugate of the given complex matrix.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
            
# Create a complex matrix(1-Dimensional) using the matrix() function of numpy module by passing 
# some random 1D matrix as an argument to it and store it in a variable
gvn_matrx = np.matrix([1+3j, 2-4j])
            
# Apply conj() function on the given matrix to get the conjugate of the given complex matrix
# Store it in another variable
rslt = gvn_matrx.conj()
# Print the conjugate of the given complex matrix
print("The conjugate of the given complex matrix:")
print(rslt)

Output:

The conjugate of the given complex matrix:
[[1.-3.j 2.+4.j]]

For 2-Dimensional (2D) Matrix

Approach:

  • Import numpy module using the import keyword.
  • Create a complex matrix(1-Dimensional) using the matrix() function of numpy module by passing some random 1D matrix as an argument to it and store it in a variable.
  • Print the given complex matrix.
  • Apply conj() function on the given matrix to get the conjugate of the given complex matrix
  • Store it in another variable.
  • Print the conjugate of the given complex matrix.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
            
# Create a complex matrix(2-Dimensional) using the matrix() function of numpy module by passing 
# some random 2D matrix as an argument to it and store it in a variable
gvn_matrx = np.matrix([[2-3j, 3 + 1j], [-5 + 2j, 6-4j]])
# Print the given complex matrix. 
print("The given complex matrix:")   
print(gvn_matrx)       
            
# Apply conj() function on the given matrix to get the conjugate of the given complex matrix
# Store it in another variable
rslt = gvn_matrx.conj()
# Print the conjugate of the given complex matrix
print("The conjugate of the given complex matrix:")
print(rslt)

Output:

The given complex matrix:
[[ 2.-3.j 3.+1.j]
 [-5.+2.j 6.-4.j]]
The conjugate of the given complex matrix:
[[ 2.+3.j 3.-1.j]
 [-5.-2.j 6.+4.j]]

Python Numpy matrix.conj() Function Read More »

Python Numpy matrix.byteswap() Function

NumPy Library 

NumPy is a library in python that is created to work efficiently with arrays in python. It is fast, easy to learn, and provides efficient storage. It also provides a better way of handling data for the process. We can create an n-dimensional array in NumPy. To use NumPy simply have to import it into our program and then we can easily use the functionality of NumPy in our program.

NumPy is a Python library that is frequently used for scientific and statistical analysis. NumPy arrays are grids of the same datatype’s values.

Numpy matrix.byteswap() Function:

Using the matrix.byteswap() method, we can swap the bytes position of an element in a specified matrix with one or more dimensions. It would not work on a string or character matrix.

Syntax:

 matrix.byteswap()

Return Value:

The byte swapped matrix is returned by the byteswap() function.

Numpy matrix.byteswap() Function in Python

For 1-Dimensional (1D) Matrix

Approach:

  • Import numpy module using the import keyword
  • Create a matrix(1-Dimensional) using the matrix() function of numpy module by passing some random 1D matrix as an argument to it and store it in a variable
  • Apply byteswap() function on the given matrix to swap the place/position of bytes of an element in a given matrix.
  • Store it in another variable
  • Print the given matrix after byte swapping.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
            
# Create a matrix(1-Dimensional) using the matrix() function of numpy module by passing 
# some random 1D matrix as an argument to it and store it in a variable
gvn_matrx = np.matrix('[2, 1, 6, 3]')
            
# Apply byteswap() function on the given matrix to swap the place/position of bytes of an 
# element in a given matrix.
# Store it in another variable
rslt = gvn_matrx.byteswap()
# Print the given matrix after byte swapping.
print("The given matrix after byte swapping:")
print(rslt)

Output:

The given matrix after byte swapping:
[[144115188075855872 72057594037927936 432345564227567616
216172782113783808]]

For 2-Dimensional (2D) Matrix

Approach:

  • Import numpy module using the import keyword
  • Create a matrix(2-Dimensional) using the matrix() function of numpy module by passing some random 2D matrix as an argument to it and store it in a variable
  • Apply byteswap() function on the given matrix to swap the place/position of bytes of an element in a given matrix.
  • Store it in another variable
  • Print the given matrix after byte swapping.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
            
# Create a matrix(2-Dimensional) using the matrix() function of numpy module by passing 
# some random 2D matrix as an argument to it and store it in a variable
gvn_matrx = np.matrix('[5, 4; 1, 3]')
            
# Apply byteswap() function on the given matrix to swap the place/position of bytes of an 
# element in a given matrix.
# Store it in another variable
rslt = gvn_matrx.byteswap()
# Print the given matrix after byte swapping.
print("The given matrix after byte swapping:")
print(rslt)

Output:

The given matrix after byte swapping:
[[360287970189639680 288230376151711744]
 [ 72057594037927936 216172782113783808]]

Python Numpy matrix.byteswap() Function Read More »

Python Numpy matrix.H() Function

NumPy Library 

NumPy is a library in python that is created to work efficiently with arrays in python. It is fast, easy to learn, and provides efficient storage. It also provides a better way of handling data for the process. We can create an n-dimensional array in NumPy. To use NumPy simply have to import it into our program and then we can easily use the functionality of NumPy in our program.

NumPy is a Python library that is frequently used for scientific and statistical analysis. NumPy arrays are grids of the same datatype’s values.

Numpy matrix.H() Function:

We can build a conjugate Transpose of any complex matrix with dimensions one or more than one using the numpy.matrix.H() method of the Numpy module.

Syntax:

 numpy.matrix.H()

Return Value:

The conjugate transpose of every complex matrix is returned by the matrix.H() function.

Transpose of a Matrix

The transpose of a matrix is one of the most often used methods in matrix transformation in linear algebra.

The transpose of a matrix for a given matrix is obtained by swapping/interchanging rows into columns or columns to rows.

For Example:

Let the matrix be: 
[1  2]
[3  4]
Transpose of a matrix is:
[1  3]
[2  4]

Conjugate of a Matrix:

A conjugate matrix is a complex matrix in which all of its elements have been replaced by their complex conjugates, i.e. the sign of all the imaginary part of the complex numbers has been changed.

Example:

Let matrix = [1+3j,  2-4j]
conjugate of a matrix = [1-3j,  2+4j]

Numpy matrix.H() Function in Python

For 1-Dimensional (1D) Matrix

Here the getH() function performs conjugate and the transpose operations simultaneously on a given complex matrix.

Approach:

  • Import numpy module using the import keyword
  • Create a complex matrix(1-Dimensional) using the matrix() function of numpy module by passing some random 1D matrix as an argument to it and store it in a variable
  • Apply getH() function on the given matrix to get the conjugate Transpose of the given complex matrix
  • Store it in another variable
  • Print the conjugate Transpose of the given complex matrix
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
            
# Create a complex matrix(1-Dimensional) using the matrix() function of numpy module by passing 
# some random 1D matrix as an argument to it and store it in a variable
gvn_matrx = np.matrix([1+3j, 2-4j])
            
# Apply getH() function on the given matrix to get the conjugate Transpose of the given complex matrix
# Store it in another variable
rslt = gvn_matrx.getH()
# Print the conjugate Transpose of the given complex matrix
print("The conjugate Transpose of the given complex matrix:")
print(rslt)

Output:

The conjugate Transpose of the given complex matrix:
[[1.-3.j]
 [2.+4.j]]

For 2-Dimensional (2D) Matrix

Approach:

  • Import numpy module using the import keyword
  • Create a complex matrix(2-Dimensional) using the matrix() function of numpy module by passing some random 2D matrix as an argument to it and store it in a variable
  • Print the given complex matrix.
  • Apply getH() function on the given matrix to get the conjugate Transpose of the given complex matrix
  • Store it in another variable
  • Print the conjugate Transpose of the given complex matrix
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
            
# Create a complex matrix(2-Dimensional) using the matrix() function of numpy module by passing 
# some random 2D matrix as an argument to it and store it in a variable
gvn_matrx = np.matrix([[2-3j, 3 + 1j], [-5 + 2j, 6-4j]])
# Print the given complex matrix. 
print("The given complex matrix:")   
print(gvn_matrx)       
# Apply getH() function on the given matrix to get the conjugate Transpose of the given complex matrix
# Store it in another variable
rslt = gvn_matrx.getH()
# Print the conjugate Transpose of the given complex matrix
print("The conjugate Transpose of the given complex matrix:")
print(rslt)

Output:

The given complex matrix:
[[ 2.-3.j 3.+1.j]
 [-5.+2.j 6.-4.j]]
The conjugate Transpose of the given complex matrix:
[[ 2.+3.j -5.-2.j]
 [ 3.-1.j 6.+4.j]]

 

Python Numpy matrix.H() Function Read More »

Python Numpy matrix.T() Function

NumPy Library 

NumPy is a library in python that is created to work efficiently with arrays in python. It is fast, easy to learn, and provides efficient storage. It also provides a better way of handling data for the process. We can create an n-dimensional array in NumPy. To use NumPy simply have to import it into our program and then we can easily use the functionality of NumPy in our program.

NumPy is a Python library that is frequently used for scientific and statistical analysis. NumPy arrays are grids of the same datatype’s values.

Numpy matrix.T() Function:

We can build a Transpose of any matrix of dimension one or more than one by using the numpy.matrix.T() method of the Numpy module.

Syntax:

 numpy.matrix.T()

Return Value:

The transpose of every matrix is returned by the matrix.T() function.

Transpose of a Matrix

The transpose of a matrix is one of the most often used methods in matrix transformation in linear algebra.

The transpose of a matrix for a given matrix is obtained by swapping/interchanging rows into columns or columns to rows.

For Example:

Let the matrix be: 
[1  2]
[3  4]
Transpose of a matrix is:
[1  3]
[2  4]

Numpy matrix.T() Function in Python

For 1-Dimensional (1D) Matrix

Approach:

  • Import numpy module using the import keyword
  • Create a matrix(1-Dimensional) using the matrix() function of numpy module by passing some random 1D matrix as an argument to it and store it in a variable
  • Apply getT() function on the given matrix to get the transpose of a given matrix.
  • Store it in another variable
  • Print the transpose of a given matrix.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
            
# Create a matrix(1-Dimensional) using the matrix() function of numpy module by passing 
# some random 1D matrix as an argument to it and store it in a variable
gvn_matrx = np.matrix('[2, 1, 6, 3]')
            
# Apply getT() function on the given matrix to get the transpose of a given matrix.
# Store it in another variable
rslt = gvn_matrx.getT()
# Print the transpose of a given matrix.
print("The transpose of a given matrix:")
print(rslt)

Output:

The transpose of a given matrix:
[[2]
 [1]
 [6]
 [3]]

For 3-Dimensional (3D) Matrix

Approach:

  • Import numpy module using the import keyword
  • Create a matrix(3-Dimensional) using the matrix() function of numpy module by passing some random 3D matrix as an argument to it and store it in a variable
  • Apply getT() function on the given matrix to get the transpose of a given matrix.
  • Store it in another variable
  • Print the transpose of a given matrix.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
            
# Create a matrix(3-Dimensional) using the matrix() function of numpy module by passing 
# some random 3D matrix as an argument to it and store it in a variable
gvn_matrx = np.matrix('[2, 4, 1; 8, 7, 3; 10, 9, 5]')
            
# Apply getT() function on the given matrix to get the transpose of a given matrix.
# Store it in another variable
rslt = gvn_matrx.getT()
# Print the transpose of a given matrix.
print("The transpose of a given matrix:")
print(rslt)

Output:

The transpose of a given matrix:
[[ 2 8 10]
 [ 4 7 9]
 [ 1 3 5]]

 

Python Numpy matrix.T() Function Read More »

Python Numpy matrix.A() Function

NumPy Library 

NumPy is a library in python that is created to work efficiently with arrays in python. It is fast, easy to learn, and provides efficient storage. It also provides a better way of handling data for the process. We can create an n-dimensional array in NumPy. To use NumPy simply have to import it into our program and then we can easily use the functionality of NumPy in our program.

NumPy is a Python library that is frequently used for scientific and statistical analysis. NumPy arrays are grids of the same datatype’s values.

Numpy matrix.A() Function:

Using the numpy.matrix.A() function of the Numpy module, we can obtain the same matrix as self. This means that we may obtain the identical matrix using this method.

Syntax:

 numpy.matrix.A()

Return Value:

The same(self) matrix is returned by the matrix.A() function.

Numpy matrix.A() Function in Python

For 1-Dimensional (1D) Matrix

Approach:

  • Import numpy module using the import keyword
  • Create a matrix(1-Dimensional) using the matrix() function of numpy module by passing some random 1D matrix as an argument to it and store it in a variable
  • Apply getA() function on the given matrix to get a self matrix of a given matrix.
  • Store it in another variable
  • Print the self matrix of a given matrix.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
            
# Create a matrix(1-Dimensional) using the matrix() function of numpy module by passing 
# some random 1D matrix as an argument to it and store it in a variable
gvn_matrx = np.matrix('[2, 1, 6, 3]')
            
# Apply getA() function on the given matrix to get a self matrix of a given matrix.
# Store it in another variable
rslt = gvn_matrx.getA()
# Print the self matrix of a given matrix.
print("The self matrix of a given matrix:")
print(rslt)

Output:

The self matrix of a given matrix:
[[2 1 6 3]]

For 3-Dimensional (3D) Matrix

Approach:

  • Import numpy module using the import keyword
  • Create a matrix(3-Dimensional) using the matrix() function of numpy module by passing some random 3D matrix as an argument to it and store it in a variable
  • Apply getA() function on the given matrix to get a self matrix of a given matrix.
  • Store it in another variable
  • Print the self matrix of a given matrix.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
            
# Create a matrix(3-Dimensional) using the matrix() function of numpy module by passing 
# some random 3D matrix as an argument to it and store it in a variable
gvn_matrx = np.matrix('[2, 4, 1; 8, 7, 3; 10, 9, 5]')
            
# Apply getA() function on the given matrix to get a self matrix of a given matrix.
# Store it in another variable
rslt = gvn_matrx.getA()
# Print the self matrix of a given matrix.
print("The self matrix of a given matrix:")
print(rslt)

Output:

The self matrix of a given matrix:
[[ 2 4 1]
 [ 8 7 3]
 [10 9 5]]

 

Python Numpy matrix.A() Function Read More »

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

We can determine the value of cot theta with the sympy.cot() method.

Syntax:

  sympy.cot()

Return Value:

The value of cot theta is returned by the cot() function.

sympy.cot() Method in Python

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

Approach:

  • Import all the functions from sympy module using the import keyword
  • Pass some random theta value to the cot() function to get the cot value of the given angle theta.
  • Store it in a variable.
  • Print the cot value of the given angle theta.
  • The Exit of the Program.

Below is the implementation:

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

# Pass some random theta value to the cot() function to get the 
# cot value of the given angle theta.
# Store it in a variable.
rslt = cot(pi / 4)
    
# Print the cot value of the given angle theta
print("The cot value of the given angle theta = ", rslt)

Output:

The cot value of the given angle theta = 1

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

Approach:

  • Import all the functions from sympy module using the import keyword
  • Give the theta value(angle) as user input using the input() function and store it in a variable.
  • Pass the above-given theta value as an argument to the cot() function to get the cot value of the given angle theta.
  • Store it in a variable.
  • Print the cot value of the given angle theta.
  • 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 theta value(angle) as user input using input() function 
# and store it in a variable.
gvn_theta = input("Enter some random theta value = ")

# Pass the above given theta value as an argument to the cot() function to get the 
# cot value of the given angle theta.
# Store it in a variable.
rslt = cot(gvn_theta)
    
# Print the cot value of the given angle theta
print("The cot value of the given angle theta = ", rslt)

Output:

Enter some random theta value = pi/3
The cot value of the given angle theta = sqrt(3)/3

Python sympy.cot() Method Read More »