Skip to content

Commit 2ee2905

Browse files
author
zihluwang
committed
feat: added mathematic calculators
1 parent 846c25a commit 2ee2905

File tree

5 files changed

+147
-2
lines changed

5 files changed

+147
-2
lines changed

num4j/build.gradle.kts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
plugins {
2+
id("java")
3+
}
4+
5+
group = "com.onixbyte"
6+
version = "unspecified"
7+
8+
repositories {
9+
mavenCentral()
10+
}
11+
12+
dependencies {
13+
testImplementation(platform("org.junit:junit-bom:5.10.0"))
14+
testImplementation("org.junit.jupiter:junit-jupiter")
15+
}
16+
17+
tasks.test {
18+
useJUnitPlatform()
19+
}

devkit-utils/src/main/java/com/onixbyte/devkit/utils/ChainedCalcUtil.java renamed to num4j/src/main/java/com/onixbyte/nums/ChainedCalcUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
package com.onixbyte.devkit.utils;
18+
package com.onixbyte.nums;
1919

2020
import lombok.Getter;
2121

@@ -87,7 +87,7 @@
8787
*
8888
* @author sunzsh
8989
* @version 1.1.0
90-
* @see java.math.BigDecimal
90+
* @see BigDecimal
9191
* @since 1.0.0
9292
*/
9393
@Getter
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright (C) 2024 OnixByte.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
*
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.onixbyte.nums;
18+
19+
import com.onixbyte.nums.model.QuartileBounds;
20+
21+
import java.util.List;
22+
23+
/**
24+
* Percentile calculator.
25+
*
26+
* @author zihluwang
27+
* @version 1.7.0
28+
* @since 1.7.0
29+
*/
30+
public final class PercentileCalculator {
31+
32+
public static Double calculatePercentile(List<Double> values, Double percentile) {
33+
var sorted = values.stream().sorted().toList();
34+
if (sorted.isEmpty()) {
35+
throw new IllegalArgumentException("Unable to sort an empty list.");
36+
}
37+
38+
var rank = percentile / 100. * (sorted.size() - 1);
39+
var lowerIndex = ((int) Math.floor(rank));
40+
var upperIndex = ((int) Math.ceil(rank));
41+
var weight = rank - lowerIndex;
42+
43+
return sorted.get(lowerIndex) * (1 - weight) + sorted.get(upperIndex) * weight;
44+
}
45+
46+
public static QuartileBounds calculatePercentileBounds(List<Double> data) {
47+
var sorted = data.stream().sorted().toList();
48+
var Q1 = calculatePercentile(sorted, 25.);
49+
var Q3 = calculatePercentile(sorted, 75.);
50+
51+
var IQR = Q3 - Q1;
52+
53+
var lowerBound = Q1 - 1.5 * IQR;
54+
var upperBound = Q3 + 1.5 * IQR;
55+
56+
return QuartileBounds.builder()
57+
.upperBound(upperBound)
58+
.lowerBound(lowerBound)
59+
.build();
60+
}
61+
62+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright (C) 2024-2024 OnixByte.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
*
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package com.onixbyte.nums.model;
19+
20+
/**
21+
* QuartileBound.
22+
*
23+
* @param upperBound
24+
* @param lowerBound
25+
* @author zihluwang
26+
*/
27+
public record QuartileBounds(
28+
Double upperBound,
29+
Double lowerBound
30+
) {
31+
32+
/**
33+
* Get a builder for {@link QuartileBounds}.
34+
*
35+
* @return a builder
36+
*/
37+
public static Builder builder() {
38+
return new Builder();
39+
}
40+
41+
public static class Builder {
42+
private Double upperBound;
43+
private Double lowerBound;
44+
45+
private Builder() {
46+
}
47+
48+
public Builder upperBound(Double upperBound) {
49+
this.upperBound = upperBound;
50+
return this;
51+
}
52+
53+
public Builder lowerBound(Double lowerBound) {
54+
this.lowerBound = lowerBound;
55+
return this;
56+
}
57+
58+
public QuartileBounds build() {
59+
return new QuartileBounds(upperBound, lowerBound);
60+
}
61+
}
62+
63+
}

settings.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@ include(
2828
"simple-jwt-spring-boot-starter",
2929
"property-guard-spring-boot-starter"
3030
)
31+
include("num4j")

0 commit comments

Comments
 (0)