Programming Assignment 5: Functions

Here is the original link

  • ActivationFunction.java (激活函数)
public class ActivationFunction {

    // Returns the Heaviside function of x.
    public static double heaviside(double x)
    {
        if (Double.isNaN(x))
        {
            return Double.NaN;
        }
        if (x < 0) {
            return 0;
        }
        else if (x > 0) {
            return 1;
        }
        else 
            return 0.5;
    }

    // Returns the sigmoid function of x.
    public static double sigmoid(double x)
    {
        if (Double.isNaN(x))
        {
            return Double.NaN;
        }
        return 1 / (1 + Math.exp(-x));
        // return 1 / (1 + Math.pow(Math.E, -x));
    }

    // Returns the hyperbolic tangent of x.
    public static double tanh(double x)
    {
        if (x == Double.MAX_VALUE)
        {
            return 1.0;
        }
        else if (x == -Double.MAX_VALUE)
        {
            return -1.0;
        }
        else if (x == Double.POSITIVE_INFINITY)
        {
            return 1.0;
        }
        else if (x == Double.NEGATIVE_INFINITY)
        {
            return -1.0;
        }
        else if (Double.isNaN(x))
        {
            return Double.NaN;
        }
        return (Math.exp(x) - Math.exp(-x)) / (Math.exp(x) + Math.exp(-x));
    }

    // Returns the softsign function of x.
    public static double softsign(double x)
    {
        if (x == Double.POSITIVE_INFINITY)
        {
            return 1;
        }
        else if (x == Double.NEGATIVE_INFINITY)
        {
            return -1;
        }
        else if (Double.isNaN(x))
        {
            return Double.NaN;
        }
        return x / (1 + Math.abs(x));
    }

    // Returns the square nonlinearity function of x.
    public static double sqnl(double x)
    {
        if (Double.isNaN(x))
        {
            return Double.NaN;
        }
        if (x <= -2)
        {
            return -1;
        }
        else if (x < 0)
        {
            return x + x*x / 4;
        }
        else if (x < 2)
        {
            return x - x*x / 4;
        }
        else
        {
            return 1;
        }
    }

    // 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)
    {
        double x = Double.parseDouble(args[0]);
        StdOut.println("heaviside" + "(" + x + ") = " + heaviside(x));
        StdOut.println("sigmoid" + "(" + x + ") = " + sigmoid(x));
        StdOut.println("tanh" + "(" + x + ") = " + tanh(x));
        StdOut.println("softsign" + "(" + x + ") = " + softsign(x));
        StdOut.println("sqnl" + "(" + x + ") = " + sqnl(x));
    }
}

  • Divisors .java (最大公约数、最小公倍数、欧拉定理)
public class Divisors {

    // Returns the greatest common divisor of a and b.
    public static int gcd(int a, int b)
    {
        int r = 0;
        a = Math.abs(a);
        b = Math.abs(b);
        while (b != 0)
        {
            r = a % b;
            a = b;
            b = r;
        }
        return a;
    }

    // Returns the least common multiple of a and b.
    public static int lcm(int a, int b)
    {
        if (a == 0 || b == 0)
        {
            return 0;
        }
        return Math.abs(a) / gcd(a, b) * Math.abs(b);
    }

    // Returns true if a and b are relatively prime; false otherwise.
    public static boolean areRelativelyPrime(int a, int b)
    {
        if (gcd(a, b) == 1)
        {
            return true;
        }
        return false;
    }

    // Returns the number of integers between 1 and n that are
    // relatively prime with n.
    public static int totient(int n)
    {
        int count = 0;
        for (int i = 1; i <= n; i++)
        {
            if (areRelativelyPrime(n, i))
            {
                count++;
            }
        }
        return count;
    }

    // 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)
    {
        int a = Integer.parseInt(args[0]);
        int b = Integer.parseInt(args[1]);
        StdOut.println("gcd" + "(" + a + "," + b + ")" + " = " + gcd(a, b));
        StdOut.println("lcm" + "(" + a + "," + b + ")" + " = " + lcm(a, b));
        StdOut.println("areRelativelyPrime" + "(" + a + "," + b + ")" + " = " + areRelativelyPrime(a, b));
        StdOut.println("totient" + "(" + b + ")" + " = " + totient(a));
        StdOut.println("totient" + "(" + a + ")" + " = " + totient(b));
    }
}
  • AudioCollage.java (音频处理)
public class AudioCollage {

    // Returns a new array that rescales a[] by a multiplicative factor of alpha.
    public static double[] amplify(double[] a, double alpha)
    {
        double[] sound = new double[a.length];
        for (int i = 0; i < a.length; i++)
        {
            sound[i] = a[i] * alpha;
        }
        return sound;
    }

    // Returns a new array that is the reverse of a[].
    public static double[] reverse(double[] a)
    {
        double[] sound = new double[a.length];
        for (int i = 0; i < a.length; i++)
        {
            sound[i] = a[a.length - 1 - i];
        }
        return sound;
    }

    // Returns a new array that is the concatenation of a[] and b[].
    public static double[] merge(double[] a, double[] b)
    {
        double[] sound = new double[a.length + b.length];
        for (int i = 0; i < a.length; i++)
        {
            sound[i] = a[i];
        }
        for (int i = 0; i < b.length; i++)
        {
            sound[i + a.length] = b[i];
        }
        return sound;
    }

    // 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)
    {
        int length = Math.max(a.length, b.length);
        double[] sound = new double[length];
        for (int i = 0; i < length; i++)
        {
            if (i < a.length) sound[i] += a[i];
            if (i < b.length) sound[i] += b[i];
        }
        return sound;
    }

    // Returns a new array that changes the speed by the given factor.
    public static double[] changeSpeed(double[] a, double alpha)
    {
        int n = a.length;
        double[] sound = new double[(int)(n / alpha)];
        for (int i = 0; i < sound.length; i++)
        {
            sound[i] = a[(int)(i * alpha)];
        }
        return sound;
    }

    // Creates an audio collage and plays it on standard audio.
    // See below for the requirements.
    public static void main(String[] args)
    {
    	double[] a1 = StdAudio.read("beatbox.wav");
        double[] a2 = StdAudio.read("buzzer.wav");
        double[] a3 = StdAudio.read("cow.wav");
        double[] a4 = StdAudio.read("chimes.wav");
        double[] a5 = StdAudio.read("harp.wav");
        double[] result = new double[]{0.0};
        result = merge(result, a2);
        result = merge(result, a3);
        result = merge(result, a4);
        result = merge(result, a5);
        StdAudio.play(result);
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值