Skip to content

Commit cf97cac

Browse files
Update the test cases and revised the cases
1 parent 813c215 commit cf97cac

6 files changed

+146
-161
lines changed

maths/bisection_method.ts

+24-20
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,30 @@
1010
* @example bisectionMethod(1, 2, 0.01, (x) => x**2 - 3) = 1.732421875
1111
*/
1212

13-
export const bisectionMethod = (a: number, b: number, e: number, f: Function): number => {
13+
export const bisectionMethod = (
14+
a: number,
15+
b: number,
16+
e: number,
17+
f: Function
18+
): number => {
19+
if (e <= 0) {
20+
throw new Error('Error threshold must be positive')
21+
}
1422

15-
if (e <= 0) {
16-
throw new Error('Error threshold must be positive')
17-
}
18-
19-
if (f(a) * f(b) >= 0) {
20-
throw new Error('f(a) and f(b) should have opposite signs')
21-
}
23+
if (f(a) * f(b) >= 0) {
24+
throw new Error('f(a) and f(b) should have opposite signs')
25+
}
2226

23-
let c = a
24-
while ((b - a) >= e) {
25-
c = (a + b) / 2
26-
if (f(c) === 0.0) {
27-
break
28-
} else if (f(c) * f(a) < 0) {
29-
b = c
30-
} else {
31-
a = c
32-
}
27+
let c = a
28+
while (Math.abs(b - a) / 2 >= e) {
29+
c = (a + b) / 2
30+
if (Math.abs(f(c)) < 1e-9) {
31+
break
32+
} else if (f(c) * f(a) < 0) {
33+
b = c
34+
} else {
35+
a = c
3336
}
34-
return c
35-
}
37+
}
38+
return c
39+
}

maths/decimal_convert.ts

+6-7
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@
44
* @return {number} - Decimal of binary.
55
* @see [DecimalConvert](https://www.programiz.com/javascript/examples/binary-decimal)
66
* @example decimalConvert("1100") = 12
7-
* @example decimalConvert("1110") = 14
87
*/
98

109
export const decimalConvert = (binary: string): number => {
11-
let decimal = 0
12-
let binaryArr = binary.split('').reverse()
10+
let decimal = 0
11+
let binaryArr = binary.split('').reverse()
1312

14-
for (let i = 0; i < binaryArr.length; i++) {
15-
decimal += parseInt(binaryArr[i]) * Math.pow(2, i)
16-
}
13+
for (let i = 0; i < binaryArr.length; i++) {
14+
decimal += parseInt(binaryArr[i]) * Math.pow(2, i)
15+
}
1716

18-
return decimal
17+
return decimal
1918
}

maths/euler_method.ts

+21-16
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,28 @@
1111
* @example eulerMethod(0, 1, 0.1, 10, (x, y) => x * y) = 1.7715614317
1212
*/
1313

14-
export const eulerMethod = (x0: number, y0: number, h: number, n: number, f: Function): number => {
14+
export const eulerMethod = (
15+
x0: number,
16+
y0: number,
17+
h: number,
18+
n: number,
19+
f: Function
20+
): number => {
21+
if (typeof f !== 'function') {
22+
throw new Error('f must be a function')
23+
}
1524

16-
if (typeof f !== 'function') {
17-
throw new Error('f must be a function')
18-
}
25+
if (n < 0) {
26+
throw new Error('Number of iterations must be non-negative')
27+
}
1928

20-
if (n < 0) {
21-
throw new Error('Number of iterations must be non-negative')
22-
}
29+
let x = x0
30+
let y = y0
2331

24-
let x = x0
25-
let y = y0
32+
for (let i = 0; i < n; i++) {
33+
y = y + h * f(x, y)
34+
x = x + h
35+
}
2636

27-
for (let i = 1; i <= n; i++) {
28-
y = y + h * f(x, y)
29-
x = x + h
30-
}
31-
32-
return y
33-
}
37+
return y
38+
}

maths/test/bisection_method.test.ts

+26-39
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,28 @@
1-
import { bisectionMethod } from "../bisection_method";
1+
import { bisectionMethod } from '../bisection_method'
22

33
describe('bisectionMethod', () => {
4-
5-
it('should find the root of f(x) = x^2 - 3 between [1, 2]', () => {
6-
const result = bisectionMethod(1, 2, 0.01, (x: number) => x ** 2 - 3);
7-
expect(result).toBeCloseTo(1.732, 2);
8-
});
9-
10-
it('should find the root of f(x) = x^3 - x - 2 between [1, 2]', () => {
11-
const result = bisectionMethod(1, 2, 0.001, (x: number) => x ** 3 - x - 2);
12-
expect(result).toBeCloseTo(1.521, 3);
13-
});
14-
15-
it('should find the root of f(x) = x^2 + x - 6 between [1, 3]', () => {
16-
const result = bisectionMethod(1, 3, 0.01, (x: number) => x ** 2 + x - 6);
17-
expect(result).toBeCloseTo(1.816, 2);
18-
});
19-
20-
it('should find the root of f(x) = cos(x) - x between [0, 1]', () => {
21-
const result = bisectionMethod(0, 1, 0.001, (x: number) => Math.cos(x) - x);
22-
expect(result).toBeCloseTo(0.739, 3);
23-
});
24-
25-
it('should find the root of f(x) = e^x - 3 between [0, 2]', () => {
26-
const result = bisectionMethod(0, 2, 0.001, (x: number) => Math.exp(x) - 3);
27-
expect(result).toBeCloseTo(1.099, 3);
28-
});
29-
30-
it('should throw an error when f(a) and f(b) have the same sign', () => {
31-
expect(() => bisectionMethod(1, 2, 0.01, (x: number) => x ** 2 + 1)).toThrow(
32-
'f(a) and f(b) should have opposite signs'
33-
);
34-
});
35-
36-
it('should throw an error when error threshold is non-positive', () => {
37-
expect(() => bisectionMethod(1, 2, -0.01, (x: number) => x ** 2 - 2)).toThrow(
38-
'Error threshold must be positive'
39-
);
40-
});
41-
});
4+
it('should find the root of f(x) = x^2 - 3 between [1, 2]', () => {
5+
const result = bisectionMethod(1, 2, 0.001, (x: number) => x ** 2 - 3)
6+
expect(result).toBeCloseTo(1.732, 3)
7+
})
8+
9+
it('should find the root of f(x) = x^3 - x - 2 between [1, 2]', () => {
10+
const result = bisectionMethod(1, 2, 0.001, (x: number) => x ** 3 - x - 2)
11+
expect(result).toBeCloseTo(1.521, 3)
12+
})
13+
14+
it('should find the root of f(x) = x^2 + x - 6 between [1, 3]', () => {
15+
const result = bisectionMethod(1, 3, 0.001, (x: number) => x ** 2 + x - 6)
16+
expect(result).toBeCloseTo(2, 3)
17+
})
18+
19+
it('should find the root of f(x) = cos(x) - x between [0, 1]', () => {
20+
const result = bisectionMethod(0, 1, 0.001, (x: number) => Math.cos(x) - x)
21+
expect(result).toBeCloseTo(0.739, 2)
22+
})
23+
24+
it('should find the root of f(x) = e^x - 3 between [0, 2]', () => {
25+
const result = bisectionMethod(0, 2, 0.001, (x: number) => Math.exp(x) - 3)
26+
expect(result).toBeCloseTo(1.099, 2)
27+
})
28+
})

maths/test/decimal_convert.test.ts

+29-38
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,31 @@
1-
import { decimalConvert } from "../decimal_convert";
1+
import { decimalConvert } from '../decimal_convert'
22

33
describe('decimalConvert', () => {
4-
it('should convert "1100" to 12', () => {
5-
expect(decimalConvert("1100")).toBe(12);
6-
});
7-
8-
it('should convert "1110" to 14', () => {
9-
expect(decimalConvert("1110")).toBe(14);
10-
});
11-
12-
it('should convert "0" to 0', () => {
13-
expect(decimalConvert("0")).toBe(0);
14-
});
15-
16-
it('should convert "1" to 1', () => {
17-
expect(decimalConvert("1")).toBe(1);
18-
});
19-
20-
it('should convert "101" to 5', () => {
21-
expect(decimalConvert("101")).toBe(5);
22-
});
23-
24-
it('should handle an empty string by returning 0', () => {
25-
expect(decimalConvert("")).toBe(0);
26-
});
27-
28-
it('should convert a binary string with leading zeros "0001" to 1', () => {
29-
expect(decimalConvert("0001")).toBe(1);
30-
});
31-
32-
it('should throw an error when the input is not a valid binary string', () => {
33-
expect(() => decimalConvert("102")).toThrow('Invalid binary input');
34-
});
35-
36-
it('should throw an error when the input contains non-numeric characters', () => {
37-
expect(() => decimalConvert("abc")).toThrow('Invalid binary input');
38-
});
39-
40-
});
4+
it('should convert "1100" to 12', () => {
5+
expect(decimalConvert('1100')).toBe(12)
6+
})
7+
8+
it('should convert "1110" to 14', () => {
9+
expect(decimalConvert('1110')).toBe(14)
10+
})
11+
12+
it('should convert "0" to 0', () => {
13+
expect(decimalConvert('0')).toBe(0)
14+
})
15+
16+
it('should convert "1" to 1', () => {
17+
expect(decimalConvert('1')).toBe(1)
18+
})
19+
20+
it('should convert "101" to 5', () => {
21+
expect(decimalConvert('101')).toBe(5)
22+
})
23+
24+
it('should handle an empty string by returning 0', () => {
25+
expect(decimalConvert('')).toBe(0)
26+
})
27+
28+
it('should convert a binary string with leading zeros "0001" to 1', () => {
29+
expect(decimalConvert('0001')).toBe(1)
30+
})
31+
})

maths/test/euler_method.test.ts

+40-41
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,42 @@
1-
import { eulerMethod } from './eulerMethod';
1+
import { eulerMethod } from '../euler_method'
22

33
describe('eulerMethod', () => {
4-
it('should compute y for a linear function (x + y)', () => {
5-
const result = eulerMethod(0, 1, 0.1, 10, (x, y) => x + y);
6-
expect(result).toBeCloseTo(2.5937424601, 5);
7-
});
8-
9-
it('should compute y for a multiplicative function (x * y)', () => {
10-
const result = eulerMethod(0, 1, 0.1, 10, (x, y) => x * y);
11-
expect(result).toBeCloseTo(1.7715614317, 5);
12-
});
13-
14-
it('should return the initial value y0 when there are zero iterations', () => {
15-
const result = eulerMethod(0, 1, 0.1, 0, (x, y) => x + y);
16-
expect(result).toBe(1);
17-
});
18-
19-
it('should return the correct value for a very small step size', () => {
20-
const result = eulerMethod(0, 1, 0.01, 100, (x, y) => x + y);
21-
expect(result).toBeCloseTo(2.7048138294, 5);
22-
});
23-
24-
it('should return the correct value after one iteration', () => {
25-
const result = eulerMethod(0, 1, 0.1, 1, (x, y) => x + y);
26-
expect(result).toBeCloseTo(1.1, 5);
27-
});
28-
29-
it('should return the initial value y0 when step size is zero', () => {
30-
const result = eulerMethod(0, 1, 0, 10, (x, y) => x + y);
31-
expect(result).toBe(1);
32-
});
33-
34-
it('should return correct value for negative step size', () => {
35-
const result = eulerMethod(1, 1, -0.1, 10, (x, y) => x + y);
36-
expect(result).toBeCloseTo(0.3162798676, 5);
37-
});
38-
39-
it('should throw an error when number of iterations is negative', () => {
40-
expect(() => eulerMethod(0, 1, 0.1, -5, (x, y) => x + y)).toThrow('Number of iterations must be non-negative');
41-
});
42-
43-
});
4+
it('should compute y for dy/dx = y with y(0) = 1 at x = 1', () => {
5+
const result = eulerMethod(0, 1, 0.1, 10, (x: number, y: number) => y)
6+
expect(result).toBeCloseTo(2.59374, 5)
7+
})
8+
9+
it('should compute y for dy/dx = -2y with y(0) = 1 at x = 1', () => {
10+
const result = eulerMethod(0, 1, 0.1, 10, (x: number, y: number) => -2 * y)
11+
const expectedResult = 1 * Math.pow(0.8, 10)
12+
expect(result).toBeCloseTo(expectedResult, 5)
13+
})
14+
15+
it('should compute y for dy/dx = x with y(0) = 0 at x = 1', () => {
16+
const result = eulerMethod(0, 0, 0.1, 10, (x: number, y: number) => x)
17+
expect(result).toBeCloseTo(0.45, 2)
18+
})
19+
20+
it('should compute y for dy/dx = x + y with y(0) = 1 at x = 0.5', () => {
21+
const h = 0.1
22+
const n = 5
23+
const result = eulerMethod(0, 1, h, n, (x: number, y: number) => x + y)
24+
expect(result).toBeCloseTo(1.72102, 5)
25+
})
26+
27+
it('should compute y for dy/dx = x^2 with y(0) = 0 at x = 1', () => {
28+
const result = eulerMethod(0, 0, 0.2, 5, (x: number, y: number) => x ** 2)
29+
expect(result).toBeCloseTo(0.24, 3)
30+
})
31+
32+
it('should handle negative step size for dy/dx = y with y(1) = e', () => {
33+
const result = eulerMethod(
34+
1,
35+
Math.E,
36+
-0.001,
37+
1000,
38+
(x: number, y: number) => y
39+
)
40+
expect(result).toBeCloseTo(1, 2)
41+
})
42+
})

0 commit comments

Comments
 (0)