Skip to content

Commit 1cc1810

Browse files
committed
clang formatted
1 parent 8728470 commit 1cc1810

2 files changed

Lines changed: 38 additions & 26 deletions

File tree

‎src/main/java/com/thealgorithms/maths/SigmoidActivation.java‎

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,25 @@ public class SigmoidActivation {
1717
*/
1818
public static double activate(double x) {
1919
// If the number x is NaN then, returning NaN to saving from unexpected output.
20-
if(Double.isNaN(x)) return Double.NaN;
20+
if (Double.isNaN(x)) {
21+
return Double.NaN;
22+
}
2123
// Saving from unnecessary and heavy calculations.
2224
// lim x->-inf sigmoid(x) will return number very close to 0
23-
if(x < -745) return 0.0;
25+
if (x < -745) return 0.0;
2426
// lim x->inf sigmoid(x) will return number very close to 1
25-
if(x > 745) return 1.0;
27+
if (x > 745) return 1.0;
2628
// sigmoid function's formula
27-
return 1.0 / ( 1 + Math.exp((-1) * x));
29+
return 1.0 / (1 + Math.exp((-1) * x));
2830
}
2931

3032
public static double[][] activate(double[][] x) {
3133
// apply calculation to every value in batch.
3234
double[][] activatedNumbers = new double[x.length][x[0].length];
33-
for(int i = 0; i < x.length; i++) {
34-
for (int j = 0; j < x[0].length; j++) activatedNumbers[i][j] = activate(x[i][j]);
35+
for (int i = 0; i < x.length; i++) {
36+
for (int j = 0; j < x[0].length; j++) {
37+
activatedNumbers[i][j] = activate(x[i][j]);
38+
}
3539
}
3640
return activatedNumbers;
3741
}
@@ -50,10 +54,11 @@ public static double grad(double y) {
5054
public static double[][] grad(double[][] y) {
5155
// apply calculation to every value in batch.
5256
double[][] grads = new double[y.length][y[0].length];
53-
for(int i = 0; i < y.length; i++) {
54-
for (int j = 0; j < y[0].length; j++) grads[i][j] = grad(y[i][j]);
57+
for (int i = 0; i < y.length; i++) {
58+
for (int j = 0; j < y[0].length; j++) {
59+
grads[i][j] = grad(y[i][j]);
60+
}
5561
}
5662
return grads;
5763
}
58-
5964
}

‎src/test/java/com/thealgorithms/maths/SigmoidActivationTest.java‎

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,52 @@
11
package com.thealgorithms.maths;
22

3-
import org.junit.jupiter.api.Test;
4-
5-
import java.util.Arrays;
6-
73
import static org.junit.jupiter.api.Assertions.assertEquals;
84
import static org.junit.jupiter.api.Assertions.assertTrue;
95

6+
import java.util.Arrays;
7+
import org.junit.jupiter.api.Test;
8+
109
public class SigmoidActivationTest {
1110

1211
@Test
1312
public void calculationTest() {
14-
assertEquals(0.5,SigmoidActivation.activate(0), 0.01,"1 case correct" );
15-
assertEquals(0.73,SigmoidActivation.activate(1),0.01,"2 case correct");
16-
assertEquals(0.26,SigmoidActivation.activate(-1),0.01, "3 case correct");
17-
assertEquals(0.88,SigmoidActivation.activate(2),0.01,"4 case correct");
18-
assertEquals(0.11,SigmoidActivation.activate(-2),0.01,"5 case correct");
13+
assertEquals(0.5, SigmoidActivation.activate(0), 0.01, "1 case correct");
14+
assertEquals(0.73, SigmoidActivation.activate(1), 0.01, "2 case correct");
15+
assertEquals(0.26, SigmoidActivation.activate(-1), 0.01, "3 case correct");
16+
assertEquals(0.88, SigmoidActivation.activate(2), 0.01, "4 case correct");
17+
assertEquals(0.11, SigmoidActivation.activate(-2), 0.01, "5 case correct");
1918

2019
double[][] xBatch = new double[4][3];
2120
double[][] expectedX = new double[4][3];
2221

23-
for(int i = 0; i < 4; i++) {
24-
for(int j = 0; j < 3; j++) xBatch[i][j] = 0;
22+
for (int i = 0; i < 4; i++) {
23+
for (int j = 0; j < 3; j++) {
24+
xBatch[i][j] = 0;
25+
}
2526
}
2627

27-
for(int i = 0; i < 4; i++) {
28-
for(int j = 0; j < 3; j++) expectedX[i][j] = 0.5;
28+
for (int i = 0; i < 4; i++) {
29+
for (int j = 0; j < 3; j++) {
30+
expectedX[i][j] = 0.5;
31+
}
2932
}
3033

3134
assertTrue(Arrays.deepEquals(expectedX, SigmoidActivation.activate(xBatch)), "batch case correct");
3235

3336
assertEquals(0.25, SigmoidActivation.grad(0.5), 0.01, "grad calculation correct");
3437

3538
double[][] yBatch = new double[4][3];
36-
for(int i = 0; i < 4; i++) {
37-
for(int j = 0; j < 3; j++) yBatch[i][j] = 0.5;
39+
for (int i = 0; i < 4; i++) {
40+
for (int j = 0; j < 3; j++) {
41+
yBatch[i][j] = 0.5;
42+
}
3843
}
3944

4045
double[][] expectedY = new double[4][3];
41-
for(int i = 0; i < 4; i++) {
42-
for(int j = 0; j < 3; j++) expectedY[i][j] = 0.25;
46+
for (int i = 0; i < 4; i++) {
47+
for (int j = 0; j < 3; j++) {
48+
expectedY[i][j] = 0.25;
49+
}
4350
}
4451
assertTrue(Arrays.deepEquals(expectedY, SigmoidActivation.grad(yBatch)), "grad batch case correct");
4552
}

0 commit comments

Comments
 (0)