From 2ee29059b53bcf86b7875c65518b92253c006cde Mon Sep 17 00:00:00 2001 From: zihluwang Date: Sat, 14 Sep 2024 02:02:47 +0800 Subject: [PATCH] feat: added mathematic calculators --- num4j/build.gradle.kts | 19 ++++++ .../com/onixbyte/nums}/ChainedCalcUtil.java | 4 +- .../onixbyte/nums/PercentileCalculator.java | 62 ++++++++++++++++++ .../onixbyte/nums/model/QuartileBounds.java | 63 +++++++++++++++++++ settings.gradle.kts | 1 + 5 files changed, 147 insertions(+), 2 deletions(-) create mode 100644 num4j/build.gradle.kts rename {devkit-utils/src/main/java/com/onixbyte/devkit/utils => num4j/src/main/java/com/onixbyte/nums}/ChainedCalcUtil.java (99%) create mode 100644 num4j/src/main/java/com/onixbyte/nums/PercentileCalculator.java create mode 100644 num4j/src/main/java/com/onixbyte/nums/model/QuartileBounds.java diff --git a/num4j/build.gradle.kts b/num4j/build.gradle.kts new file mode 100644 index 0000000..d95c6ac --- /dev/null +++ b/num4j/build.gradle.kts @@ -0,0 +1,19 @@ +plugins { + id("java") +} + +group = "com.onixbyte" +version = "unspecified" + +repositories { + mavenCentral() +} + +dependencies { + testImplementation(platform("org.junit:junit-bom:5.10.0")) + testImplementation("org.junit.jupiter:junit-jupiter") +} + +tasks.test { + useJUnitPlatform() +} \ No newline at end of file diff --git a/devkit-utils/src/main/java/com/onixbyte/devkit/utils/ChainedCalcUtil.java b/num4j/src/main/java/com/onixbyte/nums/ChainedCalcUtil.java similarity index 99% rename from devkit-utils/src/main/java/com/onixbyte/devkit/utils/ChainedCalcUtil.java rename to num4j/src/main/java/com/onixbyte/nums/ChainedCalcUtil.java index 11bcc47..27242c9 100644 --- a/devkit-utils/src/main/java/com/onixbyte/devkit/utils/ChainedCalcUtil.java +++ b/num4j/src/main/java/com/onixbyte/nums/ChainedCalcUtil.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package com.onixbyte.devkit.utils; +package com.onixbyte.nums; import lombok.Getter; @@ -87,7 +87,7 @@ import java.util.function.Function; * * @author sunzsh * @version 1.1.0 - * @see java.math.BigDecimal + * @see BigDecimal * @since 1.0.0 */ @Getter diff --git a/num4j/src/main/java/com/onixbyte/nums/PercentileCalculator.java b/num4j/src/main/java/com/onixbyte/nums/PercentileCalculator.java new file mode 100644 index 0000000..692eaf0 --- /dev/null +++ b/num4j/src/main/java/com/onixbyte/nums/PercentileCalculator.java @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2024 OnixByte. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.onixbyte.nums; + +import com.onixbyte.nums.model.QuartileBounds; + +import java.util.List; + +/** + * Percentile calculator. + * + * @author zihluwang + * @version 1.7.0 + * @since 1.7.0 + */ +public final class PercentileCalculator { + + public static Double calculatePercentile(List values, Double percentile) { + var sorted = values.stream().sorted().toList(); + if (sorted.isEmpty()) { + throw new IllegalArgumentException("Unable to sort an empty list."); + } + + var rank = percentile / 100. * (sorted.size() - 1); + var lowerIndex = ((int) Math.floor(rank)); + var upperIndex = ((int) Math.ceil(rank)); + var weight = rank - lowerIndex; + + return sorted.get(lowerIndex) * (1 - weight) + sorted.get(upperIndex) * weight; + } + + public static QuartileBounds calculatePercentileBounds(List data) { + var sorted = data.stream().sorted().toList(); + var Q1 = calculatePercentile(sorted, 25.); + var Q3 = calculatePercentile(sorted, 75.); + + var IQR = Q3 - Q1; + + var lowerBound = Q1 - 1.5 * IQR; + var upperBound = Q3 + 1.5 * IQR; + + return QuartileBounds.builder() + .upperBound(upperBound) + .lowerBound(lowerBound) + .build(); + } + +} diff --git a/num4j/src/main/java/com/onixbyte/nums/model/QuartileBounds.java b/num4j/src/main/java/com/onixbyte/nums/model/QuartileBounds.java new file mode 100644 index 0000000..45c6d24 --- /dev/null +++ b/num4j/src/main/java/com/onixbyte/nums/model/QuartileBounds.java @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2024-2024 OnixByte. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.onixbyte.nums.model; + +/** + * QuartileBound. + * + * @param upperBound + * @param lowerBound + * @author zihluwang + */ +public record QuartileBounds( + Double upperBound, + Double lowerBound +) { + + /** + * Get a builder for {@link QuartileBounds}. + * + * @return a builder + */ + public static Builder builder() { + return new Builder(); + } + + public static class Builder { + private Double upperBound; + private Double lowerBound; + + private Builder() { + } + + public Builder upperBound(Double upperBound) { + this.upperBound = upperBound; + return this; + } + + public Builder lowerBound(Double lowerBound) { + this.lowerBound = lowerBound; + return this; + } + + public QuartileBounds build() { + return new QuartileBounds(upperBound, lowerBound); + } + } + +} diff --git a/settings.gradle.kts b/settings.gradle.kts index d39293c..b840c05 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -28,3 +28,4 @@ include( "simple-jwt-spring-boot-starter", "property-guard-spring-boot-starter" ) +include("num4j")