monteCarlo java
This commit is contained in:
9
.idea/MonteCarlo_Java.iml
generated
Normal file
9
.idea/MonteCarlo_Java.iml
generated
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="JAVA_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||||
|
<exclude-output />
|
||||||
|
<content url="file://$MODULE_DIR$" />
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
||||||
6
.idea/misc.xml
generated
Normal file
6
.idea/misc.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="21" project-jdk-type="JavaSDK">
|
||||||
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/MonteCarlo_Java.iml" filepath="$PROJECT_DIR$/.idea/MonteCarlo_Java.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
50
.idea/workspace.xml
generated
Normal file
50
.idea/workspace.xml
generated
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ChangeListManager">
|
||||||
|
<list default="true" id="14270ddd-3489-41a4-a32d-5acbddefb094" name="Changes" comment="" />
|
||||||
|
<option name="SHOW_DIALOG" value="false" />
|
||||||
|
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||||
|
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
|
||||||
|
<option name="LAST_RESOLUTION" value="IGNORE" />
|
||||||
|
</component>
|
||||||
|
<component name="ProjectColorInfo"><![CDATA[{
|
||||||
|
"customColor": "",
|
||||||
|
"associatedIndex": 3
|
||||||
|
}]]></component>
|
||||||
|
<component name="ProjectId" id="2f8GfgPc4SNisU2YtrGzKkx27b1" />
|
||||||
|
<component name="ProjectViewState">
|
||||||
|
<option name="hideEmptyMiddlePackages" value="true" />
|
||||||
|
<option name="showLibraryContents" value="true" />
|
||||||
|
</component>
|
||||||
|
<component name="PropertiesComponent"><![CDATA[{
|
||||||
|
"keyToString": {
|
||||||
|
"RunOnceActivity.OpenProjectViewOnStart": "true",
|
||||||
|
"RunOnceActivity.ShowReadmeOnStart": "true",
|
||||||
|
"kotlin-language-version-configured": "true",
|
||||||
|
"nodejs_package_manager_path": "npm",
|
||||||
|
"vue.rearranger.settings.migration": "true"
|
||||||
|
}
|
||||||
|
}]]></component>
|
||||||
|
<component name="SharedIndexes">
|
||||||
|
<attachedChunks>
|
||||||
|
<set>
|
||||||
|
<option value="jdk-21.0.1-liberica-21.0.1-f644763e9732-4f02bf01" />
|
||||||
|
</set>
|
||||||
|
</attachedChunks>
|
||||||
|
</component>
|
||||||
|
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
|
||||||
|
<component name="TaskManager">
|
||||||
|
<task active="true" id="Default" summary="Default task">
|
||||||
|
<changelist id="14270ddd-3489-41a4-a32d-5acbddefb094" name="Changes" comment="" />
|
||||||
|
<created>1713175398866</created>
|
||||||
|
<option name="number" value="Default" />
|
||||||
|
<option name="presentableId" value="Default" />
|
||||||
|
<updated>1713175398866</updated>
|
||||||
|
<workItem from="1713175399721" duration="45000" />
|
||||||
|
</task>
|
||||||
|
<servers />
|
||||||
|
</component>
|
||||||
|
<component name="TypeScriptGeneratedFilesManager">
|
||||||
|
<option name="version" value="3" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
29
MonteCarlo_Java/.gitignore
vendored
Normal file
29
MonteCarlo_Java/.gitignore
vendored
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
### IntelliJ IDEA ###
|
||||||
|
out/
|
||||||
|
!**/src/main/**/out/
|
||||||
|
!**/src/test/**/out/
|
||||||
|
|
||||||
|
### Eclipse ###
|
||||||
|
.apt_generated
|
||||||
|
.classpath
|
||||||
|
.factorypath
|
||||||
|
.project
|
||||||
|
.settings
|
||||||
|
.springBeans
|
||||||
|
.sts4-cache
|
||||||
|
bin/
|
||||||
|
!**/src/main/**/bin/
|
||||||
|
!**/src/test/**/bin/
|
||||||
|
|
||||||
|
### NetBeans ###
|
||||||
|
/nbproject/private/
|
||||||
|
/nbbuild/
|
||||||
|
/dist/
|
||||||
|
/nbdist/
|
||||||
|
/.nb-gradle/
|
||||||
|
|
||||||
|
### VS Code ###
|
||||||
|
.vscode/
|
||||||
|
|
||||||
|
### Mac OS ###
|
||||||
|
.DS_Store
|
||||||
8
MonteCarlo_Java/.idea/.gitignore
generated
vendored
Normal file
8
MonteCarlo_Java/.idea/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
||||||
6
MonteCarlo_Java/.idea/misc.xml
generated
Normal file
6
MonteCarlo_Java/.idea/misc.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="21" project-jdk-type="JavaSDK">
|
||||||
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
8
MonteCarlo_Java/.idea/modules.xml
generated
Normal file
8
MonteCarlo_Java/.idea/modules.xml
generated
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/MonteCarlo_Java.iml" filepath="$PROJECT_DIR$/MonteCarlo_Java.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
11
MonteCarlo_Java/MonteCarlo_Java.iml
Normal file
11
MonteCarlo_Java/MonteCarlo_Java.iml
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="JAVA_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||||
|
<exclude-output />
|
||||||
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||||
|
</content>
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
||||||
134
MonteCarlo_Java/src/Main.java
Normal file
134
MonteCarlo_Java/src/Main.java
Normal file
@@ -0,0 +1,134 @@
|
|||||||
|
public class Main {
|
||||||
|
|
||||||
|
static Bounds bounds = new Bounds(new LinearBounds(0, 20), new LinearBounds(-100, 150000));
|
||||||
|
static double samples = 1_000_000;
|
||||||
|
static int threadCount = 16;
|
||||||
|
|
||||||
|
static class Point {
|
||||||
|
private final double x;
|
||||||
|
private final double y;
|
||||||
|
|
||||||
|
public Point(double _x, double _y) {
|
||||||
|
this.x = _x;
|
||||||
|
this.y = _y;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getX() {
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getY() {
|
||||||
|
return y;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static class LinearBounds {
|
||||||
|
private final float lower;
|
||||||
|
private final float higher;
|
||||||
|
|
||||||
|
public LinearBounds(float _lower, float _higher) {
|
||||||
|
this.lower = _lower;
|
||||||
|
this.higher = _higher;
|
||||||
|
}
|
||||||
|
public double length() {
|
||||||
|
return Math.abs(higher-lower);
|
||||||
|
}
|
||||||
|
public double getRandomValue() {
|
||||||
|
return lower + (randomFloat() * this.length());
|
||||||
|
}
|
||||||
|
public float getLower() {
|
||||||
|
return lower;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getHigher() {
|
||||||
|
return higher;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static class Bounds {
|
||||||
|
private final LinearBounds x;
|
||||||
|
private final LinearBounds y;
|
||||||
|
|
||||||
|
public Bounds(LinearBounds _x, LinearBounds _y) {
|
||||||
|
this.x = _x;
|
||||||
|
this.y = _y;
|
||||||
|
}
|
||||||
|
public double area() {
|
||||||
|
return x.length() * y.length();
|
||||||
|
}
|
||||||
|
public Point getRandomPoint() {
|
||||||
|
return new Point(x.getRandomValue(), y.getRandomValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
public LinearBounds getX() {
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static double function(double x) {
|
||||||
|
return Math.pow(x,4)-4*Math.pow(x,3)+18*Math.pow(x,2)-12*x-69;
|
||||||
|
}
|
||||||
|
static double indefiniteIntegral(double x) {
|
||||||
|
return (1.0/5) * Math.pow(x, 5) - Math.pow(x, 4) + 6*Math.pow(x, 3) - 6 * Math.pow(x, 2) -69*x;
|
||||||
|
}
|
||||||
|
static boolean getIsInside(Point p) {
|
||||||
|
double y_0 = function(p.getX());
|
||||||
|
return Math.abs(y_0) > Math.abs(p.getY()) && y_0 * p.getY() >= 0;
|
||||||
|
}
|
||||||
|
static double getRealIntegral(LinearBounds bounds) {
|
||||||
|
return indefiniteIntegral(bounds.getHigher()) - indefiniteIntegral(bounds.getLower());
|
||||||
|
}
|
||||||
|
static double randomFloat() {
|
||||||
|
return Math.random();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static void main(String[] args) throws InterruptedException {
|
||||||
|
|
||||||
|
double pointsInside = 0;
|
||||||
|
long startTime = System.nanoTime();
|
||||||
|
double real_value = getRealIntegral(bounds.getX());
|
||||||
|
|
||||||
|
|
||||||
|
for (int i = 0; i< threadCount; i++) {
|
||||||
|
|
||||||
|
Threading t = new Threading();
|
||||||
|
t.start();t.join();
|
||||||
|
pointsInside += t.getPoints();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
double integral = (pointsInside/(samples*threadCount)) * bounds.area();
|
||||||
|
double error = Math.abs(real_value-integral);
|
||||||
|
|
||||||
|
System.out.println("The approximated Integral of the function is: " + String.format("%.2f", integral));
|
||||||
|
System.out.println("The real Integral of the function is: " + String.format("%.2f",real_value));
|
||||||
|
System.out.println("That's an error of " + String.format("%.8f", error) +" or " + String.format("%.8f",(error/real_value)*100) + "%");
|
||||||
|
System.out.println("And the whole thing took " + String.format("%.6f", (System.nanoTime()-startTime)/1000000000.0) + " Seconds for " + String.format("%,.0f", samples*threadCount) + " samples");
|
||||||
|
|
||||||
|
}
|
||||||
|
static class Threading extends Thread {
|
||||||
|
private double pointsInsideThread = 0;
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
|
||||||
|
//System.out.println("Thread " + Thread.currentThread());
|
||||||
|
Point toTestPoint;
|
||||||
|
for (int i = 0; i < samples; i++) {
|
||||||
|
toTestPoint = bounds.getRandomPoint();
|
||||||
|
if (getIsInside(toTestPoint)) {
|
||||||
|
pointsInsideThread++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getPoints() {
|
||||||
|
return pointsInsideThread;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user