The purpose of this assignment is to give you practice writing programs with Java functions (static methods). The first exercise involves real-valued functions; the second exercise focuses on integer-valued functions; the third exercise considers functions on arrays.



  1. Activation functions. Write a program ActivationFunction.java to compute various activation functions that arise in neural networks. An activation function is a function that maps real numbers into a desired range, such as between 0 and 1 or between –1 and +1.

    All activation functions should return NaN (not a number) if the argument is NaN.

    To do so, organize your program according to the following public API:

    public class ActivationFunction {
    
        // Returns the Heaviside function of x.
        public static double heaviside(double x)
    
        // Returns the sigmoid function of x.
        public static double sigmoid(double x)
    
        // Returns the hyperbolic tangent of x.
        public static double tanh(double x)
    
        // Returns the softsign function of x.
        public static double softsign(double x)
    
        // Returns the square nonlinearity function of x.
        public static double sqnl(double x)
    
        // Takes a double command-line argument x and prints each activation
        // function, evaluated, in the format (and order) given below.
        public static void main(String[] args)
    }
    

    Here are some sample executions:

    ~/Desktop/functions> java-introcs ActivationFunction 0.0
    heaviside(0.0) = 0.5
      sigmoid(0.0) = 0.5
         tanh(0.0) = 0.0
     softsign(0.0) = 0.0
         sqnl(0.0) = 0.0
    
    ~/Desktop/functions> java-introcs ActivationFunction 1.0
    heaviside(1.0) = 1.0
      sigmoid(1.0) = 0.7310585786300049
         tanh(1.0) = 0.7615941559557649
     softsign(1.0) = 0.5
         sqnl(1.0) = 0.75
    
    ~/Desktop/functions> java-introcs ActivationFunction -0.5
    heaviside(-0.5) = 0.0
      sigmoid(-0.5) = 0.3775406687981454
         tanh(-0.5) = -0.4621171572600098
     softsign(-0.5) = -0.3333333333333333
         sqnl(-0.5) = -0.4375
    


  2. Greatest common divisors. Write a program Divisors.java to compute the greatest common divisor and related functions on integers:

    To do so, organize your program according to the following public API:

    public class Divisors {
    
        // Returns the greatest common divisor of a and b.
        public static int gcd(int a, int b)
    
        // Returns the least common multiple of a and b.
        public static int lcm(int a, int b)
    
        // Returns true if a and b are relatively prime; false otherwise.
        public static boolean areRelativelyPrime(int a, int b)
    
        // Returns the number of integers between 1 and n that are
        // relatively prime with n.
        public static int totient(int n)
    
        // Takes two integer command-line arguments a and b and prints
        // each function, evaluated in the format (and order) given below.
        public static void main(String[] args)
    }
    

    Here are some sample executions:

    ~/Desktop/functions> java-introcs Divisors 1440 408
    gcd(1440, 408) = 24
    lcm(1440, 408) = 24480
    areRelativelyPrime(1440, 408) = false
    totient(1440) = 384
    totient(408) = 128
    
    ~/Desktop/functions> java-introcs Divisors 987 610
    gcd(987, 610) = 1
    lcm(987, 610) = 602070
    areRelativelyPrime(987, 610) = true
    totient(987) = 552
    totient(610) = 240
    

    Use the following algorithms to implement the corresponding functions.

    The greatest common divisor and least common multiple functions arise in a variety of applications, including reducing fractions, modular arithmetic, and cryptography. Euler’s totient function plays an important role in number theory, including Euler’s theorem and cyclotomic polynomials.


  3. Audio collage. Create a library to manipulate digital audio and use that library to create an audio collage. As in lecture, we will represent sound as an array of real numbers between –1 and +1, with 44,100 samples per second. You will write a library of functions to produce audio effects by manipulating such arrays.

    To do so, organize your program according to the following public API:

    public class AudioCollage {
    
        // Returns a new array that rescales a[] by a multiplicative factor of alpha.
        public static double[] amplify(double[] a, double alpha)
    
        // Returns a new array that is the reverse of a[].
        public static double[] reverse(double[] a)
    
        // Returns a new array that is the concatenation of a[] and b[].
        public static double[] merge(double[] a, double[] b)
    
        // Returns a new array that is the sum of a[] and b[],
        // padding the shorter arrays with trailing 0s if necessary.
        public static double[] mix(double[] a, double[] b)
    
        // Returns a new array that changes the speed by the given factor.
        public static double[] changeSpeed(double[] a, double alpha)
    
        // Creates an audio collage and plays it on standard audio.
        // See below for the requirements.
        public static void main(String[] args)
    }
    
    Here is some more information about the required behavior:

    This problem was inspired by an audio effects programming assignment by Keith Vertanen.


Submission. Submit a .zip file containing ActivationFunction.java, Divisors.java, and AudioCollage.java. You may also submit supplementary .wav files—put them in the same directory as your .java files. (There is no need to submit the provided .wav files.) You may not call library functions except those in java.lang. Use only Java features that have already been introduced in the course (e.g., loops, arrays, and functions but not objects).


This assignment was developed by Kevin Wayne.
Copyright © 2019.