AppDividend
Latest Code Tutorials

Python Numpy bmat() Function Example

0

Python numpy bmat() is an inbuilt NumPy function that is declared under the library numpy. It returns a 2-D matrix from nested objects consisting of string, array, or nested sequence. It forms a small matrix from the input and combines them in a bigger 2-D matrix.

Python Numpy.bmat()

Python numpy bmat() function builds a matrix object from a string, nested sequence, or array. The numpy.bmat() function is used to build a matrix object from a string, nested sequence, or array.

Syntax

numpy.bmat(obj, ldict=None, gdict=None)

Parameters

obj: (string or array_like)

Input data to be built in the matrix. If a string, variables may be referenced by name in the current scope.

ldict:(dict,optional)

The dictionary that replaces the local operands in the current frame. Ignore if a string is passed or gdict is set to NONE.

gdict:(dict, optional)

A dictionary that replaces the global operands in the current frame. Ignore if a string is passed.

Return Value

It gives a matrix object of the input data in the form of a specialized 2-D array. Each array and strings are converted to a small matrix, and a whole matrix is made by combing them to form a bigger matrix.

Examples

# app.py

import numpy as np

a=np.mat('1 1; 1 1')
b=np.mat('2 2; 2 2')
c=np.mat('3 4; 5 6')
d=np.mat('7 8; 9 0')
np.bmat([[a, b],[c, d]])
w=np.mat('11 11; 11 11')
x=np.mat('12 12; 12 12')
y=np.mat('13 14; 15 16')
z=np.mat('17 18; 19 20')
print(np.bmat('w x;y z'))

Output

python3 app.py
[[11 11 12 12]
 [11 11 12 12]
 [13 14 17 18]
 [15 16 19 20]]

Programs on NumPy asmatrix() function in Python

Write a program to show the function of bmat and print output via array-like and string-like input.

# app.py

import numpy as np

data = []

ir = input("Enter the rows:")
ic = input("Enter the cols:")

row = int(ir)
col = int(ic)

print("Enter the data rowise:")
for i in range(row):
    for j in range(col):
        data.append(int(input()))

a = np.array(data).reshape(row, col)
print("as array: \n", a)

c = np.asmatrix(a)

print("as matrix: \n", c)

mr = input("Enter the row of modified value:")
mc = input("Enter the col of modified value:")
mb = input("Enter the value to be modified in the matrix:")

n = int(mr)
m = int(mc)
b = int(mb)
a[n - 1, m - 1] = b

print("as matrix after modifying value: \n", c)

Output

python3 app.py
Enter the rows:3
Enter the cols:3
Enter the data rowise:
12
13
14
15
16
17
18
19
20
as array:
 [[12 13 14]
 [15 16 17]
 [18 19 20]]
as matrix:
 [[12 13 14]
 [15 16 17]
 [18 19 20]]
Enter the row of modified value:2
Enter the col of modified value:2
Enter the value to be modified in the matrix:19
as matrix after modifying value:
 [[12 13 14]
 [15 19 17]
 [18 19 20]]

First, we have defined the 3*3 matrix with input data and then determined which values should be modified, which is at 2*2 with 19, and the final output has replaced value with 19. So 16 value is replaced by 19.

Let’s see different variations of the above program in the output.

Value of parameter:   a=np.mat('4 1; 22 1')
	              b=np.mat('5 2; 5 2')
		      c=np.mat('8 4; 6 6')
		      d=np.mat('7 5; 2 3')
					
For output: via bmat array like input:  matrix([[ 4,  1,  5,  2],
       						[22,  1,  5,  2],
       						[ 8,  4,  7,  5],
      						[ 6,  6,  2,  3]])
via bmat string like input:  matrix([[ 4,  1,  5,  2],
        			     [22,  1,  5,  2],
       				     [ 8,  4,  7,  5],
       				     [ 6,  6,  2,  3]])

Value of parameter:   a=np.mat('14 11; 2 1')
		      b=np.mat('15 12; 15 12')
		      c=np.mat('28 24; 26 26')
		      d=np.mat('37 35; 32 33')
					
For output: via bmat array like input:  matrix([[ 14,  11,  15,  12],
        				        [2,  1,  15,  12],
        					[ 28,  24,  37,  35],
        					[ 26,  26,  32,  33]])
via bmat string like input:  matrix([[ 14,  11,  5,  2],
        			     [2,  1,  5,  2],
       				     [ 28,  24,  37,  35],
       				     [ 26,  26,  32,  33]])

Value of parameter:   a=np.mat('44 14; 52 51')
		      b=np.mat('65 62; 65 62')
		      c=np.mat('78 74; 76 76')
		      d=np.mat('87 85; 82 83')
					
For output: via bmat array like input:  matrix([[ 44,  14,  65,  62],
        					[52,  51,  65,  62],
        					[ 78,  74,  87,  85],
        					[ 76,  76,  82,  83]])
via bmat string like input:  matrix([[ 44,  14,  65,  62],
        			     [52,  51,  65,  62],
       				     [ 78,  74,  87,  85],
       				     [ 76,  76,  82,  83]])

Here a 2-D matrix is formed via the inputted array and strings. The input is formed into a small matrix first, and later they are converted into a bigger matrix. Each input is formed into a small matrix inside the bigger matrix. The input can be in the format of strings or array; this function converts them into a matrix and returns the matrix.

See also

Python NumPy insert()

Python NumPy append()

Python NumPy delete()

Python NumPy arange()

Python NumPy diagflat()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.