publicclassActivationFunction{// Returns the Heaviside function of x.publicstaticdoubleheaviside(double x){if(Double.isNaN(x)){returnDouble.NaN;}if(x <0){return0;}elseif(x >0){return1;}elsereturn0.5;}// Returns the sigmoid function of x.publicstaticdoublesigmoid(double x){if(Double.isNaN(x)){returnDouble.NaN;}return1/(1+Math.exp(-x));// return 1 / (1 + Math.pow(Math.E, -x));}// Returns the hyperbolic tangent of x.publicstaticdoubletanh(double x){if(x ==Double.MAX_VALUE){return1.0;}elseif(x ==-Double.MAX_VALUE){return-1.0;}elseif(x ==Double.POSITIVE_INFINITY){return1.0;}elseif(x ==Double.NEGATIVE_INFINITY){return-1.0;}elseif(Double.isNaN(x)){returnDouble.NaN;}return(Math.exp(x)-Math.exp(-x))/(Math.exp(x)+Math.exp(-x));}// Returns the softsign function of x.publicstaticdoublesoftsign(double x){if(x ==Double.POSITIVE_INFINITY){return1;}elseif(x ==Double.NEGATIVE_INFINITY){return-1;}elseif(Double.isNaN(x)){returnDouble.NaN;}return x /(1+Math.abs(x));}// Returns the square nonlinearity function of x.publicstaticdoublesqnl(double x){if(Double.isNaN(x)){returnDouble.NaN;}if(x <=-2){return-1;}elseif(x <0){return x + x*x /4;}elseif(x <2){return x - x*x /4;}else{return1;}}// Takes a double command-line argument x and prints each activation// function, evaluated, in the format (and order) given below.publicstaticvoidmain(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 (最大公约数、最小公倍数、欧拉定理)
publicclassDivisors{// Returns the greatest common divisor of a and b.publicstaticintgcd(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.publicstaticintlcm(int a,int b){if(a ==0|| b ==0){return0;}returnMath.abs(a)/gcd(a, b)*Math.abs(b);}// Returns true if a and b are relatively prime; false otherwise.publicstaticbooleanareRelativelyPrime(int a,int b){if(gcd(a, b)==1){returntrue;}returnfalse;}// Returns the number of integers between 1 and n that are// relatively prime with n.publicstaticinttotient(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.publicstaticvoidmain(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 (音频处理)
publicclassAudioCollage{// Returns a new array that rescales a[] by a multiplicative factor of alpha.publicstaticdouble[]amplify(double[] a,double alpha){double[] sound =newdouble[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[].publicstaticdouble[]reverse(double[] a){double[] sound =newdouble[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[].publicstaticdouble[]merge(double[] a,double[] b){double[] sound =newdouble[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.publicstaticdouble[]mix(double[] a,double[] b){int length =Math.max(a.length, b.length);double[] sound =newdouble[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.publicstaticdouble[]changeSpeed(double[] a,double alpha){int n = a.length;double[] sound =newdouble[(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.publicstaticvoidmain(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 =newdouble[]{0.0};
result =merge(result, a2);
result =merge(result, a3);
result =merge(result, a4);
result =merge(result, a5);StdAudio.play(result);}}
Here is the original linkActivationFunction.javapublic class ActivationFunction { // Returns the Heaviside function of x. public static double heaviside(double x) { if (Double.isNaN(x)) { return Double.NaN;