Question

In: Computer Science

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

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] ]

**This is to be written in a python code.

Solutions

Expert Solution

ANSWER:--

GIVEN THAT:--

We initialize a boolean matrix and call the function matrix_power(A, n) to calculate A^n

Output:--


Editable Code:

import numpy as np

#Function to calculate A^n using boolean arithmetic
def matrix_power(A, n):
    R = A
    for i in range(n-1): #Loop calculates R = A^n
        R = R*A
    return R #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

The the function call we can change the second argument to calculate different powers of A


Related Solutions

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] ]...
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 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
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT