Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· openaiHugging Face Suzhen/CodeChat

Hello, I have already implemented a matlab program for the question of lu decomposition,…

  1. 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

    b - Description of variable b, including expected data type and/or values

    Outputs:

    c - Description of variable c, including expected data type and/or values

    d - Description of variable d, including expected data type and/or values

    Code_line_1 for i in range(b): Code_line_2 c = a + b d = a - b 2 return c, d After declaration of the function include a short one or two line summary of the function followed by a blank line. Then describe all inputs and output in a similar manner to the MATLAB comments above. As Python is whitespace sensitive all code with already be properly formatted.---------------------------------Follow the above instructions and formatt the below code accordingly --------------------------------------------------------------------------function [L,U,P] = hbommaka_aboda_pp12(A)

    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);

  2. 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_pp12
    

    Please make sure to replace hbommaka_aboda with the appropriate ubitnames according to the style guide instructions.

  3. 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.

  4. 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
    

    Please make sure to replace ubitname with your ubitname according to the style guide instructions. Test the function with appropriate test cases and functions g(x) to ensure it works correctly.

  5. 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")

  6. 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.

  7. User

    For this homework you will get a bit of experience writing and running Spark code, and performing some follow-up analysis. In general, you will install and set up Spark, write a basic word count program, and then answer some questions about data flow in Spark applications. General Homework Requirements

    1. Work Environment: This homework must be written in Python.

    2. Programming: Relevant tutorials and resources are linked below to aid in the programming portion of this homework.

    3. Academic Integrity: You will get an automatic F for the course if you violate the academic integrity policy.

    4. Teams: This homework is an individual assignment. You are not permitted to work with anyone else on this assignment. All work submitted must be yours and yours alone. Submission Format Your submission should be a single zip file that contains a src/ directory for your code, and a single PDF report. Submissions must be made on UBLearns by 5/1/23 @ 11:59PM. ● Your src/ directory should contain your code files and your input dataset (10 txt files from project gutenberg), and your list of stopwords. ● Your report should contain the answers to each of the questions below, including any required screenshots of your application output. --------------------------------------------------------Once you have Spark setup, you must also get the data you will be using for the homework. Go to Project Gutenberg (https://www.gutenberg.org/ebooks/) and download 10 different books as plain text files. Each of the files you download must be at least 100 kB in size, but the larger the book the more interesting your results may be. You may use the same books you used from HW #1.

                Part 1 - Implement and Analyze Word Count [14/30 points]
      

    Basic Word Count For the first part of this homework, you must write a basic word count program that reads in a single text file, counts the number of occurrences of each word in that text file, and outputs the result back to a text file as a list of key-value pairs, where the key is the word, and the value is the number of times the word occurred in the text file. In addition to basic word counting your code must also do the following:

    1. It must be case insensitive (see lower() in Python)
    2. It must ignore all punctuation (see, for example, translate() in Python)
    3. It must ignore stop words (see filter() in Spark)
    4. The output must be sorted by count in descending order (see sortBy() in Spark) To accomplish this you will use a combination of basic Python, and RDD operations in PySpark. Any examples from class and any official Spark documentation can be used as reference material. Cite any code you use directly as necessary. The following programming guide goes over basics of getting started with Spark and should contain everything you need to complete this part of the homework: https://spark.apache.org/docs/latest/rdd-programming-guide.html Note: To make later parts of this HW easier, you should write your word count code in a function that takes the Spark context as an argument so you can call your function from the interactive PySpark REPL. See the in-class demo code as an example. CSE 4/587 Spring 2023 Extending Your Application Once you have your basic word count working for a single text file as described above, you will now extend it to work for a list of text files, combining the results from each file into a single RDD of key-value pairs, where the key is the word and the value is the number of times that word appeared across all passed in text files. The result should only be sorted and written to file once after all text files have been processed. Note: This is different from the in class example where we combined results using join(). See instead union() in Spark. Analysis Answer the following questions based on your WordCount application.
    5. In the PySpark REPL, run your basic word count program on a single text file. a. What are the 25 most common words? Include a screenshot of program output to back-up your claim. b. How many stages is execution broken up into? Explain why. Include a screenshot of the DAG visualization from Spark's WebUI to back-up your claim.
    6. In the PySpark REPL, run your extended word count program on all 10 text files. a. What are the 25 most common words? Include a screenshot of program output to back-up your claim. b. How many stages is execution broken up into? Explain why. Include a screenshot of the DAG visualization from Spark's WebUI to back-up your claim.
    7. Your WordCount application should compute the same results as your WordCount application from Homework #1. Answer the following based on your knowledge of both MapReduce and Spark: a. If you were running your WordCount programs in a large cluster or cloud environment, and one of the nodes you were running on died mid computation, how would your MapReduce and Spark programs handle this? b. Explain one concrete benefit you experienced when writing the Spark version of WordCount compared to the MapReduce version.
  8. 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)
    

    Following the instructions, download 10 different books from Project Gutenberg and save them as book1.txt, book2.txt, etc. Also, create a stopwords.txt file containing the stop words list you want the program to ignore.

    To analyze the results, you need to run this script on the PySpark REPL as described in the assignment. To find the 25 most common words, you can add this snippet before counts.saveAsTextFile(output_file):

    top_words = counts.take(25)
    print(top_words)
    

    You can access the DAG visualization from Spark's WebUI following this tutorial: Monitoring your application. Then, analyze the results and DAG visualization to answer the questions as mentioned in the assignment.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.