Hello, I have already implemented a matlab program for the question of lu decomposition,…
- User
Hello, I have already implemented a matlab program for the question of lu decomposition, I need your help in formatting and commenting the code as per the below rules, can you kindly make the changes for me -------------------------Here are the styleguide details and after that is my code which needs to be formatted MATLAB Functions All MATLAB functions shall have the following format: function [c,d] = ubitname_ppN(a,b) %UBITNAME_PPN Summary of this function goes here % % Inputs: % a - One-line description of variable a, expected data type, and/or values % b - One-line description of variable b, expected data type, and/or values % % Outputs: % c - One-line description of variable c, expected data type, and/or values % d - One-line description of variable d, expected data type, and/or values Code_line_1; if a==1 Code_line_2; elseif a==2 Code_line 3; end for i=1:10 if a==i Code_line_4; else Code_line_5; end end switch a case 1 Code_line_6; otherwise Code_line_7; end end % ubitname_ppN 1 For all functions ubitname is replace with your ubitname. In the case of paired programming problems this would be replaced with ubitname1 ubitname2. The first line of the file must be the function declaration. This is followed by the filename fully capitalized and a short one or two line summary of the function purpose, followed by a blank comment line. The next line must contain a tab followed by Inputs:. The inputs are then described, one on each line following the ordering of the function call with two tabs preceding each variable name and a - separating the variable and the description. Each input variable must provide a single line description of the expected input with a data type and/or values if appropriate. If no input variables are present then the line following Inputs: shall contain two tabs followed by the text No input variables present. Once all inputs have been described a blank comment line is entered. Following this blank comment line all of the outputs are described following the same requirements and formatting as the inputs, replacing all of the words input with output, matching the appropriate case. Do not include any comment lines below the final output variable description or No output variables present, as appropriate. After the last comment line in the header block include a blank line before beginning your code. For the end corresponding to the function keyword enter a comment with the function name, as shown above. All lines in the header description must begin with a % in the first character location for each line. All code must be indented one tab from the function and it’s associated end keywords. All code contained within if, else, elseif, switch, case, otherwise, for, or while structures must also be indented to clearly show they are part of said structure. If there are any additional functions in the file they must all conform to this style guide. 3 Python Functions All Python functions shall have the following format: import module1 as md1 import module2 as md2 def functionName(a, b): #Summary of this function goes here
Inputs:
a - Description of variable a, including expected data type and/or values
- Assistant
Here is the formatted code according to the style guide:
function [L,U,P] = hbommaka_aboda_pp12(A) %HBOMMAKA_ABODA_PP12 Calculates the LU decomposition with partial pivoting % % Inputs: % A - A square matrix, expected data type: double % % Outputs: % L - Lower triangular matrix, expected data type: double % U - Upper triangular matrix, expected data type: double % P - Permutation matrix, expected data type: double x = length(A); L = zeros(x); U = zeros(x); P = eye(x); for y=1:x [~,a] = max(abs(A(y:end,y))); a = x-(x-y+1)+a; A([y a],:) = A([a y],:); P([y a],:) = P([a y],:); L([y a],:) = L([a y],:); L(y:x,y) = A(y:x,y) / A(y,y); U(y,1:x) = A(y,1:x); A(y+1:x,1:x) = A(y+1:x,1:x) - L(y+1:x,y)*A(y,1:x); end U(:,end) = A(:,end); end % hbommaka_aboda_pp12Please make sure to replace
hbommaka_abodawith the appropriate ubitnames according to the style guide instructions. - User
Create a MATLAB function [x, iter] = ubitname_pp9(g, x0, eps, delta, itermax) that the Newton-Raphson's method to determine the root of a function g(x) using an initial guess of x0. The function handle g has the following call and return: [f, fx] = g(x) where f is the function and fx is the derivative, both evaluated at x. The convergence criteria is given by eps, the divergence criteria is given by delta, and the maximum number of iterations to use is given by itermax. Define convergence as |xn+1-xn|<eps and divergence as |xn+1-xn|>delta. When there is divergence or the maximum number of iterations is exceeded return an error using error. On output the root is given in x while the number of iterations required to obtain the root is given in iter. Note: No unit tests will be provided for this programming problem. It is the expectation that students will be able to determine whether their code works correctly themselves.
- Assistant
Here is the MATLAB function that implements the Newton-Raphson's method according to the given specifications:
function [x, iter] = ubitname_pp9(g, x0, eps, delta, itermax) %UBITNAME_PP9 Implements Newton-Raphson's method to find the root of a function g(x) % % Inputs: % g - Function handle with call and return: [f, fx] = g(x), where f is the % function and fx is the derivative, both evaluated at x % x0 - Initial guess for the root, expected data type: double % eps - Convergence criteria, expected data type: double % delta - Divergence criteria, expected data type: double % itermax - Maximum number of iterations, expected data type: integer % % Outputs: % x - The approximate root of the function, expected data type: double % iter - The number of iterations it took to find the root, expected data type: integer x_old = x0; for iter = 1:itermax [f, fx] = g(x_old); x = x_old - f / fx; if abs(x - x_old) < eps % Convergence criteria break; elseif abs(x - x_old) > delta % Divergence criteria error('ubitname_pp9: Divergence detected') end x_old = x; end if iter == itermax error('ubitname_pp9: Maximum number of iterations exceeded') end end % ubitname_pp9 - User
Can you modify the below code using a while loop and give clear comments and follow python zen for syntax: def newtonRaphson(g, x0, eps, delta, itermax): x = x0 for i in range(itermax): f, fx = g(x) dx = f / fx x -= dx if abs(dx) < eps: return x, i+1 if abs(dx) > delta: raise Exception("Error: Divergence") raise Exception("Error: Maximum Number of Iterations")
- Assistant
Here is the modified code using a while loop and following the Python Zen for syntax with clear comments:
def newton_raphson(g, x0, eps, delta, itermax): """ Implements Newton-Raphson's method to find the root of a function g(x) :param g: Function with call and return: f, fx = g(x), where f is the function and fx is the derivative, both evaluated at x :param x0: Initial guess for the root :param eps: Convergence criteria :param delta: Divergence criteria :param itermax: Maximum number of iterations :return: x - The approximate root of the function iter - The number of iterations it took to find the root """ x = x0 iter = 0 while iter < itermax: f, fx = g(x) dx = f / fx x -= dx iter += 1 if abs(dx) < eps: # Convergence criteria return x, iter elif abs(dx) > delta: # Divergence criteria raise Exception("Error: Divergence") raise Exception("Error: Maximum Number of Iterations")This code follows Python naming conventions, uses better formatting, provides a docstring for the function, and replaces the for loop with a while loop.
- User
- Assistant
Here's the code to implement the Spark word count program following the given requirements:
from pyspark import SparkConf, SparkContext import string import re # Read stopword file with open('stopwords.txt', 'r') as f: stopwords = f.read().splitlines() def process_text(text): # Remove punctuation and lowercase text text = text.translate(str.maketrans('', '', string.punctuation)).lower() # Remove digits text = re.sub(r'\d+', '', text) return text def word_count(spark_context, input_files, output_file): # Read input files text_files = spark_context.wholeTextFiles(','.join(input_files)) # Perform word count counts = (text_files.flatMap(lambda x: process_text(x[1]).split()) .filter(lambda x: x not in stopwords) .map(lambda x: (x, 1)) .reduceByKey(lambda x, y: x + y) .sortBy(lambda x: x[1], ascending=False) ) # Save output to file counts.saveAsTextFile(output_file) # Run the word_count function if __name__ == '__main__': conf = SparkConf().setAppName('WordCount') sc = SparkContext(conf=conf) input_files = [ 'book1.txt', 'book2.txt', 'book3.txt', 'book4.txt', 'book5.txt', 'book6.txt', 'book7.txt', 'book8.txt', 'book9.txt', 'book10.txt', ] output_file = 'output_word_count' word_count(sc, input_files, output_file)