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 in python that takes in an integer n and computes the left hand...
Write a function in python that takes in an integer n and computes the left hand Riemann sum of the function f(x) = x^2 on the interval [0,1]. Hint: compute the error from the true answer
Write a C function boolean isPrime (int n), that would take a positive integer n as...
Write a C function boolean isPrime (int n), that would take a positive integer n as a parameter and return true or false whether the number is a prime number. You should check the range of n and print proper message (For example, if n is a genitive number, you should print out an error message).
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 LISP function POWER that takes a list containing exactly two numeric atoms and computes...
Write a LISP function POWER that takes a list containing exactly two numeric atoms and computes the first atom raised to the power of the second atom. For example: (POWER ‘(2 4) Returns the value 16
Given an n × n matrix A, write a function that carries out LU factoriza- tion...
Given an n × n matrix A, write a function that carries out LU factoriza- tion without row pivoting. Note that this will be different from the built-in Matlab function lu, which does do row pivoting. (Using MATLAB)
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT