Montecarlo Kotlin

This commit is contained in:
Parric007
2024-05-13 12:08:29 +02:00
commit b5ed399b0c
11 changed files with 251 additions and 0 deletions

29
.gitignore vendored Normal file
View 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
.idea/.gitignore generated vendored Normal file
View 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

10
.idea/codeStyles/Project.xml generated Normal file
View File

@@ -0,0 +1,10 @@
<component name="ProjectCodeStyleConfiguration">
<code_scheme name="Project" version="173">
<JetCodeStyleSettings>
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
</JetCodeStyleSettings>
<codeStyleSettings language="kotlin">
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
</codeStyleSettings>
</code_scheme>
</component>

5
.idea/codeStyles/codeStyleConfig.xml generated Normal file
View File

@@ -0,0 +1,5 @@
<component name="ProjectCodeStyleConfiguration">
<state>
<option name="USE_PER_PROJECT_SETTINGS" value="true" />
</state>
</component>

View File

@@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="ReplaceUntilWithRangeUntil" enabled="true" level="WEAK WARNING" enabled_by_default="true" />
</profile>
</component>

10
.idea/kotlinc.xml generated Normal file
View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Kotlin2JvmCompilerArguments">
<option name="jvmTarget" value="1.8" />
</component>
<component name="KotlinCommonCompilerArguments">
<option name="apiVersion" value="1.9" />
<option name="languageVersion" value="1.9" />
</component>
</project>

23
.idea/libraries/KotlinJavaRuntime.xml generated Normal file
View File

@@ -0,0 +1,23 @@
<component name="libraryTable">
<library name="KotlinJavaRuntime" type="repository">
<properties maven-id="org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.9.23" />
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk8/1.9.23/kotlin-stdlib-jdk8-1.9.23.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib/1.9.23/kotlin-stdlib-1.9.23.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/annotations/13.0/annotations-13.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk7/1.9.23/kotlin-stdlib-jdk7-1.9.23.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk8/1.9.23/kotlin-stdlib-jdk8-1.9.23-javadoc.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib/1.9.23/kotlin-stdlib-1.9.23-javadoc.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/annotations/13.0/annotations-13.0-javadoc.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk7/1.9.23/kotlin-stdlib-jdk7-1.9.23-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk8/1.9.23/kotlin-stdlib-jdk8-1.9.23-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib/1.9.23/kotlin-stdlib-1.9.23-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/annotations/13.0/annotations-13.0-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk7/1.9.23/kotlin-stdlib-jdk7-1.9.23-sources.jar!/" />
</SOURCES>
</library>
</component>

6
.idea/misc.xml generated Normal file
View 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
View 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_Kotlin.iml" filepath="$PROJECT_DIR$/MonteCarlo_Kotlin.iml" />
</modules>
</component>
</project>

15
MonteCarlo_Kotlin.iml Normal file
View File

@@ -0,0 +1,15 @@
<?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$/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/testResources" type="java-test-resource" />
</content>
<orderEntry type="jdk" jdkName="21" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="KotlinJavaRuntime" level="project" />
</component>
</module>

131
src/Main.kt Normal file
View File

@@ -0,0 +1,131 @@
import java.text.DecimalFormat
import kotlin.math.abs
import kotlin.math.pow
import kotlin.random.Random.Default.nextDouble
import kotlin.system.measureTimeMillis
var lowerX = 0.0
var upperX = 20.0
fun randomFloat(): Double {
return nextDouble()
}
class Point(private var x: Double, private var y: Double) {
fun getX() : Double {
return x
}
fun getY() : Double {
return y
}
}
class LinearBounds(private var lower: Float, private var higher: Float) {
fun length(): Double {
return abs(higher-lower).toDouble()
}
fun getRandomValue() : Double{
return lower + (randomFloat()*length())
}
fun getLower() : Float {
return lower
}
fun getHigher() : Float {
return higher
}
}
class Bounds(private var x : LinearBounds, private var y : LinearBounds) {
fun area() : Double {
return x.length() * y.length()
}
fun getRandomPoint() : Point {
return Point(x.getRandomValue(), y.getRandomValue())
}
fun getX() : LinearBounds {
return x
}
}
fun function(x : Double) : Double {
return x.pow(4.0) -4* x.pow(3.0) +18* x.pow(2.0) -12*x-69
}
fun indefiniteIntegral(x : Double) : Double {
return (1.0/5) * x.pow(5) - x.pow(4) + 6*x.pow(3) - 6 * x.pow(2) -69*x
}
fun getIsInside(p : Point) : Boolean {
var y0 = function(p.getX())
return abs(y0) > abs(p.getY()) && y0*p.getY() >= 0
}
fun getRealIntegral(bounds : LinearBounds) : Double{
return indefiniteIntegral(bounds.getHigher()-indefiniteIntegral(bounds.getLower().toDouble()))
}
fun getUpperLimit() : Float{
var upperLimit = 0.0f
var i = lowerX
while(i < upperX) {
i += 0.025
if(function(i) > upperLimit) {
upperLimit = (function(i) * 1.025).toFloat()
}
}
return upperLimit
}
fun getLowerLimit() : Float {
var lowerLimit = 0.0f
var i = lowerX
while(i < upperX) {
i += 0.025
if(function(i) < lowerLimit) {
lowerLimit = (function(i) * 1.025).toFloat()
}
}
return lowerLimit
}
fun main() {
val format = DecimalFormat("#,###.##")
var pointsInside = 0.0
val samples = 20_000_000
val bounds = Bounds(LinearBounds(lowerX.toFloat(), upperX.toFloat()), LinearBounds(getLowerLimit(), getUpperLimit()))
var integral = 0.0
var error = 0.0
var result = 0.0
val realValue = getRealIntegral(bounds.getX())
var toTestPoint : Point
val elapsed = measureTimeMillis {
for(i in 0..50){
for(ii in 0..samples) {
toTestPoint = bounds.getRandomPoint()
if(getIsInside(toTestPoint)){
pointsInside++
}
}
integral = (pointsInside/samples) * bounds.area()
result += integral
integral = 0.0
pointsInside = 0.0
}
result /= 50.0
error = abs(realValue-result)
}
println("The approximated Integral of the function is: " + format.format(result))
println("The real Integral of the function is: " + format.format(realValue))
println("That's an error of " + format.format(error) + " or " + format.format((error/realValue)*100) + "%")
println("And the whole thing took " + format.format(elapsed/1000.0) + " Seconds for 50 times" + format.format(samples) + " Samples")
}