Question

In: Computer Science

Write a function matrix power(A, n) that computes the power An using Boolean arithmetic and returns...

Write a function matrix power(A, n) that computes the power An using Boolean arithmetic and returns the result. You may assume that A is a 2D list containing only 0s and 1s, A is square (same number of rows and columns), and n is an integer ≥ 1. You should call your previously written matrix multiply boolean function.

Example: Let R = [ [0, 0, 0, 1], [0, 1, 1, 0], [0, 0, 0, 1], [0, 0, 1, 0] ]

Then calling matrix power(R, 2) should return [ [0, 0, 1, 0], [0, 1, 1, 1], [0, 0, 1, 0], [0, 0, 0, 1] ]

Solutions

Expert Solution

We initialized a boolean matrix and call the function matric_power(A,n) for calculating A^n.

Program:

Output of the program:

Source Code :

import numpy as np

# Function that using boolean arithmetic to calculate A^n
def matrix_power(A, n):
R = A
for i in range(n-1): #Loop calculates R = A^n
R = R*A
return R #It Returns the result


#Initializing 2D list A
A = np.matrix([[0,0,0,1], [0,1,1,0], [0,0,0,1], [0,0,1,0]], dtype=bool)

R = matrix_power(A,2) #Calling the function
print("Result is")
print(R.astype(int)) #Printing the result


Related Solutions

C++ Write a recursive function that computes and returns the product of the first n >=1...
C++ Write a recursive function that computes and returns the product of the first n >=1 real numbers in an array.
Write a boolean function that is given a binary tree and returns true if and only...
Write a boolean function that is given a binary tree and returns true if and only if the tree has an odd number of nodes. An empty tree is considered to have an even number of nodes. Notes: The function should have just one argument, a pointer to the root. No global variables may be used. No additional functions may be defined. You may not count the number of nodes.
PYTHON: Write a function named is_palindrome() that returns boolean True if a word or phrase is...
PYTHON: Write a function named is_palindrome() that returns boolean True if a word or phrase is a palindrome, and boolean False if it is not. The palindromes for this problem are contained in the list below. You must use this list in your solution. Use a for loop to retrieve each palindrome and test it. Your script should test each string in the list of palindromes below. Be aware there's a second list of palindromes embedded in the palindromes list....
Write function boolean isSorted(int a[], int size). The function returns true if array a is sorted...
Write function boolean isSorted(int a[], int size). The function returns true if array a is sorted in either ascend order or descend order; false otherwise. c++
Write a function such that given a number N, display the N*N multiplication matrix from 1...
Write a function such that given a number N, display the N*N multiplication matrix from 1 to N. Then, write a C++ program such that, it prints the multiplication matrices of numbers 1,4,7, and 10 using a loop concept. Check the sample output below, to see how the program should work. Make sure to have your output exactly the same as the below output.
Write a Java program to 1. read in the size of a square boolean matrix A...
Write a Java program to 1. read in the size of a square boolean matrix A 2. read in the 0-1 matrix elements 3. read in a positive integer n 4. display A^n
Write a Java program to 1. read in the size of a square boolean matrix A...
Write a Java program to 1. read in the size of a square boolean matrix A 2. read in the 0-1 matrix elements 3. read in a positive integer n 4. display A^n Multiplying that matrix to the nth power. Like A^2 = matrix A * matrix A. Elements ONLY can be 0 or 1.
In R: write a function that inputs a vector x and a number n and returns...
In R: write a function that inputs a vector x and a number n and returns the first n elements of x. When n is greater than length(x), your function should just return x. We are not allowed to use any control flow statements
Using the following returns, calculate the arithmetic average returns, the variances, and the standard deviations for...
Using the following returns, calculate the arithmetic average returns, the variances, and the standard deviations for X and Y. Returns Year X Y 1 11 % 23 % 2 29 44 3 18 -11 4 -19 -25 5 20 52 Using the following returns, calculate the arithmetic average returns, the variances, and the standard deviations for X and Y.    Returns Year X Y 1 11 %     23 %     2 29         44         3 18         -11...
Using the following returns, calculate the arithmetic average returns, the variances, and the standard deviations for...
Using the following returns, calculate the arithmetic average returns, the variances, and the standard deviations for X and Y. Returns Year X Y 1 11 % 23 % 2 29 44 3 18 -11 4 -19 -25 5 20 52 Using the following returns, calculate the arithmetic average returns, the variances, and the standard deviations for X and Y.    Returns Year X Y 1 11 %     23 %     2 29         44         3 18         -11...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT