From 5ebfb519bad8fdd8fe94f1c58c3f5718f97e2a6e Mon Sep 17 00:00:00 2001 From: algobytewise Date: Thu, 1 Apr 2021 11:50:02 +0530 Subject: [PATCH 1/4] readded EulerMethod.java after sync --- Maths/EulerMethod.java | 103 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 Maths/EulerMethod.java diff --git a/Maths/EulerMethod.java b/Maths/EulerMethod.java new file mode 100644 index 000000000000..a1dcc98bdf71 --- /dev/null +++ b/Maths/EulerMethod.java @@ -0,0 +1,103 @@ +import java.util.ArrayList; +import java.util.function.BiFunction; + +/** + * In mathematics and computational science, the Euler method (also called forward Euler method) is + * a first-order numerical procedure for solving ordinary differential equations (ODEs) with a given + * initial value. It is the most basic explicit method for numerical integration of ordinary + * differential equations. The method proceeds in a series of steps. At each step the y-value is + * calculated by evaluating the differential equation at the previous step, multiplying the result + * with the step-size and adding it to the last y-value: y_n+1 = y_n + stepSize * f(x_n, y_n). + * (description adapted from https://en.wikipedia.org/wiki/Euler_method ) (see also: + * https://www.geeksforgeeks.org/euler-method-solving-differential-equation/ ) + */ +public class EulerMethod { + + /** Illustrates how the algorithm is used in 3 examples and prints the results to the console. */ + public static void main(String[] args) { + System.out.println("example 1:"); + BiFunction exampleEquation1 = (x, y) -> x; + ArrayList points1 = eulerFull(0, 4, 0.1, 0, exampleEquation1); + assert points1.get(points1.size() - 1)[1] == 7.800000000000003; + points1.forEach( + point -> System.out.println(String.format("x: %1$f; y: %2$f", point[0], point[1]))); + + // example from https://en.wikipedia.org/wiki/Euler_method + System.out.println("\n\nexample 2:"); + BiFunction exampleEquation2 = (x, y) -> y; + ArrayList points2 = eulerFull(0, 4, 0.1, 1, exampleEquation2); + assert points2.get(points2.size() - 1)[1] == 45.25925556817596; + points2.forEach( + point -> System.out.println(String.format("x: %1$f; y: %2$f", point[0], point[1]))); + + // example from https://www.geeksforgeeks.org/euler-method-solving-differential-equation/ + System.out.println("\n\nexample 3:"); + BiFunction exampleEquation3 = (x, y) -> x + y + x * y; + ArrayList points3 = eulerFull(0, 0.1, 0.025, 1, exampleEquation3); + assert points3.get(points3.size() - 1)[1] == 1.1116729841674804; + points3.forEach( + point -> System.out.println(String.format("x: %1$f; y: %2$f", point[0], point[1]))); + } + + /** + * calculates the next y-value based on the current value of x, y and the stepSize the console. + * + * @param xCurrent Current x-value. + * @param stepSize Step-size on the x-axis. + * @param yCurrent Current y-value. + * @param differentialEquation The differential equation to be solved. + * @return The next y-value. + */ + public static double eulerStep( + double xCurrent, + double stepSize, + double yCurrent, + BiFunction differentialEquation) { + if (stepSize <= 0) { + throw new IllegalArgumentException("stepSize should be greater than zero"); + } + double yNext = yCurrent + stepSize * differentialEquation.apply(xCurrent, yCurrent); + return yNext; + } + + /** + * Loops through all the steps until xEnd is reached, adds a point for each step and then returns + * all the points + * + * @param xStart First x-value. + * @param xEnd Last x-value. + * @param stepSize Step-size on the x-axis. + * @param yStart First y-value. + * @param differentialEquation The differential equation to be solved. + * @return The points constituting the solution of the differential equation. + */ + public static ArrayList eulerFull( + double xStart, + double xEnd, + double stepSize, + double yStart, + BiFunction differentialEquation) { + if (xStart >= xEnd) { + throw new IllegalArgumentException("xEnd should be greater than xStart"); + } + if (stepSize <= 0) { + throw new IllegalArgumentException("stepSize should be greater than zero"); + } + + ArrayList points = new ArrayList(); + double[] firstPoint = {xStart, yStart}; + points.add(firstPoint); + double yCurrent = yStart; + double xCurrent = xStart; + + while (xCurrent < xEnd) { + // Euler method for next step + yCurrent = eulerStep(xCurrent, stepSize, yCurrent, differentialEquation); + xCurrent += stepSize; + double[] point = {xCurrent, yCurrent}; + points.add(point); + } + + return points; + } +} From 598810e595b2789282db28357748e174b8d67710 Mon Sep 17 00:00:00 2001 From: algobytewise Date: Mon, 12 Apr 2021 15:43:07 +0530 Subject: [PATCH 2/4] add RgbHsvConversion.java --- Conversions/RgbHsvConversion.java | 163 ++++++++++++++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 Conversions/RgbHsvConversion.java diff --git a/Conversions/RgbHsvConversion.java b/Conversions/RgbHsvConversion.java new file mode 100644 index 000000000000..bcfb6190a07b --- /dev/null +++ b/Conversions/RgbHsvConversion.java @@ -0,0 +1,163 @@ +import java.util.Arrays; + +/** + * The RGB color model is an additive color model in which red, green, and blue light are added + * together in various ways to reproduce a broad array of colors. The name of the model comes from + * the initials of the three additive primary colors, red, green, and blue. Meanwhile, the HSV + * representation models how colors appear under light. In it, colors are represented using three + * components: hue, saturation and (brightness-)value. This class provides methods for converting + * colors from one representation to the other. (description adapted from + * https://en.wikipedia.org/wiki/RGB_color_model and https://en.wikipedia.org/wiki/HSL_and_HSV). + */ +public class RgbHsvConversion { + + public static void main(String[] args) { + // Expected RGB-values taken from https://www.rapidtables.com/convert/color/hsv-to-rgb.html + + // Test hsvToRgb-method + assert Arrays.equals(hsvToRgb(0, 0, 0), new int[] {0, 0, 0}); + assert Arrays.equals(hsvToRgb(0, 0, 1), new int[] {255, 255, 255}); + assert Arrays.equals(hsvToRgb(0, 1, 1), new int[] {255, 0, 0}); + assert Arrays.equals(hsvToRgb(60, 1, 1), new int[] {255, 255, 0}); + assert Arrays.equals(hsvToRgb(120, 1, 1), new int[] {0, 255, 0}); + assert Arrays.equals(hsvToRgb(240, 1, 1), new int[] {0, 0, 255}); + assert Arrays.equals(hsvToRgb(300, 1, 1), new int[] {255, 0, 255}); + assert Arrays.equals(hsvToRgb(180, 0.5, 0.5), new int[] {64, 128, 128}); + assert Arrays.equals(hsvToRgb(234, 0.14, 0.88), new int[] {193, 196, 224}); + assert Arrays.equals(hsvToRgb(330, 0.75, 0.5), new int[] {128, 32, 80}); + + // Test rgbToHsv-method + // approximate-assertions needed because of small deviations due to converting between + // int-values and double-values. + assert approximatelyEqualHsv(rgbToHsv(0, 0, 0), new double[] {0, 0, 0}); + assert approximatelyEqualHsv(rgbToHsv(255, 255, 255), new double[] {0, 0, 1}); + assert approximatelyEqualHsv(rgbToHsv(255, 0, 0), new double[] {0, 1, 1}); + assert approximatelyEqualHsv(rgbToHsv(255, 255, 0), new double[] {60, 1, 1}); + assert approximatelyEqualHsv(rgbToHsv(0, 255, 0), new double[] {120, 1, 1}); + assert approximatelyEqualHsv(rgbToHsv(0, 0, 255), new double[] {240, 1, 1}); + assert approximatelyEqualHsv(rgbToHsv(255, 0, 255), new double[] {300, 1, 1}); + assert approximatelyEqualHsv(rgbToHsv(64, 128, 128), new double[] {180, 0.5, 0.5}); + assert approximatelyEqualHsv(rgbToHsv(193, 196, 224), new double[] {234, 0.14, 0.88}); + assert approximatelyEqualHsv(rgbToHsv(128, 32, 80), new double[] {330, 0.75, 0.5}); + } + + /** + * Conversion from the HSV-representation to the RGB-representation. + * + * @param hue Hue of the color. + * @param saturation Saturation of the color. + * @param value Brightness-value of the color. + * @return The tuple of RGB-components. + */ + public static int[] hsvToRgb(double hue, double saturation, double value) { + if (hue < 0 || hue > 360) { + throw new IllegalArgumentException("hue should be between 0 and 360"); + } + + if (saturation < 0 || saturation > 1) { + throw new IllegalArgumentException("saturation should be between 0 and 1"); + } + + if (value < 0 || value > 1) { + throw new IllegalArgumentException("value should be between 0 and 1"); + } + + double chroma = value * saturation; + double hueSection = hue / 60; + double secondLargestComponent = chroma * (1 - Math.abs(hueSection % 2 - 1)); + double matchValue = value - chroma; + + return getRgbBySection(hueSection, chroma, matchValue, secondLargestComponent); + } + + /** + * Conversion from the RGB-representation to the HSV-representation. + * + * @param red Red-component of the color. + * @param green Green-component of the color. + * @param blue Blue-component of the color. + * @return The tuple of HSV-components. + */ + public static double[] rgbToHsv(int red, int green, int blue) { + if (red < 0 || red > 255) { + throw new IllegalArgumentException("red should be between 0 and 255"); + } + + if (green < 0 || green > 255) { + throw new IllegalArgumentException("green should be between 0 and 255"); + } + + if (blue < 0 || blue > 255) { + throw new IllegalArgumentException("blue should be between 0 and 255"); + } + + double dRed = (double) red / 255; + double dGreen = (double) green / 255; + double dBlue = (double) blue / 255; + double value = Math.max(Math.max(dRed, dGreen), dBlue); + double chroma = value - Math.min(Math.min(dRed, dGreen), dBlue); + double saturation = value == 0 ? 0 : chroma / value; + double hue; + + if (chroma == 0) { + hue = 0; + } else if (value == dRed) { + hue = 60 * (0 + (dGreen - dBlue) / chroma); + } else if (value == dGreen) { + hue = 60 * (2 + (dBlue - dRed) / chroma); + } else { + hue = 60 * (4 + (dRed - dGreen) / chroma); + } + + hue = (hue + 360) % 360; + + return new double[] {hue, saturation, value}; + } + + private static boolean approximatelyEqualHsv(double[] hsv1, double[] hsv2) { + boolean bHue = Math.abs(hsv1[0] - hsv2[0]) < 0.2; + boolean bSaturation = Math.abs(hsv1[1] - hsv2[1]) < 0.002; + boolean bValue = Math.abs(hsv1[2] - hsv2[2]) < 0.002; + + return bHue && bSaturation && bValue; + } + + private static int[] getRgbBySection( + double hueSection, double chroma, double matchValue, double secondLargestComponent) { + int red; + int green; + int blue; + + if (hueSection >= 0 && hueSection <= 1) { + red = convertToInt(chroma + matchValue); + green = convertToInt(secondLargestComponent + matchValue); + blue = convertToInt(matchValue); + } else if (hueSection > 1 && hueSection <= 2) { + red = convertToInt(secondLargestComponent + matchValue); + green = convertToInt(chroma + matchValue); + blue = convertToInt(matchValue); + } else if (hueSection > 2 && hueSection <= 3) { + red = convertToInt(matchValue); + green = convertToInt(chroma + matchValue); + blue = convertToInt(secondLargestComponent + matchValue); + } else if (hueSection > 3 && hueSection <= 4) { + red = convertToInt(matchValue); + green = convertToInt(secondLargestComponent + matchValue); + blue = convertToInt(chroma + matchValue); + } else if (hueSection > 4 && hueSection <= 5) { + red = convertToInt(secondLargestComponent + matchValue); + green = convertToInt(matchValue); + blue = convertToInt(chroma + matchValue); + } else { + red = convertToInt(chroma + matchValue); + green = convertToInt(matchValue); + blue = convertToInt(secondLargestComponent + matchValue); + } + + return new int[] {red, green, blue}; + } + + private static int convertToInt(double input) { + return (int) Math.round(255 * input); + } +} From ccc7d19259f3bc33de150340fca7f2bbef66aa62 Mon Sep 17 00:00:00 2001 From: algobytewise Date: Mon, 12 Apr 2021 15:45:41 +0530 Subject: [PATCH 3/4] Delete EulerMethod.java --- Maths/EulerMethod.java | 103 ----------------------------------------- 1 file changed, 103 deletions(-) delete mode 100644 Maths/EulerMethod.java diff --git a/Maths/EulerMethod.java b/Maths/EulerMethod.java deleted file mode 100644 index a1dcc98bdf71..000000000000 --- a/Maths/EulerMethod.java +++ /dev/null @@ -1,103 +0,0 @@ -import java.util.ArrayList; -import java.util.function.BiFunction; - -/** - * In mathematics and computational science, the Euler method (also called forward Euler method) is - * a first-order numerical procedure for solving ordinary differential equations (ODEs) with a given - * initial value. It is the most basic explicit method for numerical integration of ordinary - * differential equations. The method proceeds in a series of steps. At each step the y-value is - * calculated by evaluating the differential equation at the previous step, multiplying the result - * with the step-size and adding it to the last y-value: y_n+1 = y_n + stepSize * f(x_n, y_n). - * (description adapted from https://en.wikipedia.org/wiki/Euler_method ) (see also: - * https://www.geeksforgeeks.org/euler-method-solving-differential-equation/ ) - */ -public class EulerMethod { - - /** Illustrates how the algorithm is used in 3 examples and prints the results to the console. */ - public static void main(String[] args) { - System.out.println("example 1:"); - BiFunction exampleEquation1 = (x, y) -> x; - ArrayList points1 = eulerFull(0, 4, 0.1, 0, exampleEquation1); - assert points1.get(points1.size() - 1)[1] == 7.800000000000003; - points1.forEach( - point -> System.out.println(String.format("x: %1$f; y: %2$f", point[0], point[1]))); - - // example from https://en.wikipedia.org/wiki/Euler_method - System.out.println("\n\nexample 2:"); - BiFunction exampleEquation2 = (x, y) -> y; - ArrayList points2 = eulerFull(0, 4, 0.1, 1, exampleEquation2); - assert points2.get(points2.size() - 1)[1] == 45.25925556817596; - points2.forEach( - point -> System.out.println(String.format("x: %1$f; y: %2$f", point[0], point[1]))); - - // example from https://www.geeksforgeeks.org/euler-method-solving-differential-equation/ - System.out.println("\n\nexample 3:"); - BiFunction exampleEquation3 = (x, y) -> x + y + x * y; - ArrayList points3 = eulerFull(0, 0.1, 0.025, 1, exampleEquation3); - assert points3.get(points3.size() - 1)[1] == 1.1116729841674804; - points3.forEach( - point -> System.out.println(String.format("x: %1$f; y: %2$f", point[0], point[1]))); - } - - /** - * calculates the next y-value based on the current value of x, y and the stepSize the console. - * - * @param xCurrent Current x-value. - * @param stepSize Step-size on the x-axis. - * @param yCurrent Current y-value. - * @param differentialEquation The differential equation to be solved. - * @return The next y-value. - */ - public static double eulerStep( - double xCurrent, - double stepSize, - double yCurrent, - BiFunction differentialEquation) { - if (stepSize <= 0) { - throw new IllegalArgumentException("stepSize should be greater than zero"); - } - double yNext = yCurrent + stepSize * differentialEquation.apply(xCurrent, yCurrent); - return yNext; - } - - /** - * Loops through all the steps until xEnd is reached, adds a point for each step and then returns - * all the points - * - * @param xStart First x-value. - * @param xEnd Last x-value. - * @param stepSize Step-size on the x-axis. - * @param yStart First y-value. - * @param differentialEquation The differential equation to be solved. - * @return The points constituting the solution of the differential equation. - */ - public static ArrayList eulerFull( - double xStart, - double xEnd, - double stepSize, - double yStart, - BiFunction differentialEquation) { - if (xStart >= xEnd) { - throw new IllegalArgumentException("xEnd should be greater than xStart"); - } - if (stepSize <= 0) { - throw new IllegalArgumentException("stepSize should be greater than zero"); - } - - ArrayList points = new ArrayList(); - double[] firstPoint = {xStart, yStart}; - points.add(firstPoint); - double yCurrent = yStart; - double xCurrent = xStart; - - while (xCurrent < xEnd) { - // Euler method for next step - yCurrent = eulerStep(xCurrent, stepSize, yCurrent, differentialEquation); - xCurrent += stepSize; - double[] point = {xCurrent, yCurrent}; - points.add(point); - } - - return points; - } -} From bf59dadb9a3c81d588dccb8206f528dd639bfd2d Mon Sep 17 00:00:00 2001 From: algobytewise Date: Thu, 15 Apr 2021 19:37:39 +0530 Subject: [PATCH 4/4] add package --- Conversions/RgbHsvConversion.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Conversions/RgbHsvConversion.java b/Conversions/RgbHsvConversion.java index bcfb6190a07b..60fffe7a5f72 100644 --- a/Conversions/RgbHsvConversion.java +++ b/Conversions/RgbHsvConversion.java @@ -1,3 +1,5 @@ +package Conversions; + import java.util.Arrays; /**