Added some test files.
This commit is contained in:
96
app/src/test/java/com/example/hpos/CubicEquationSolver.kt
Normal file
96
app/src/test/java/com/example/hpos/CubicEquationSolver.kt
Normal file
@@ -0,0 +1,96 @@
|
||||
package com.example.hpos
|
||||
|
||||
import org.junit.Test
|
||||
|
||||
class CubicEquationSolver {
|
||||
|
||||
@Test
|
||||
fun test() {
|
||||
val a: Double = 12.toDouble()
|
||||
val b: Double = (15).toDouble()
|
||||
val c: Double = (9).toDouble()
|
||||
val d: Double = (-2456).toDouble()
|
||||
|
||||
solve(a, b, c, d)
|
||||
}
|
||||
|
||||
fun solve(a: Double, b: Double, c: Double, d: Double) {
|
||||
val f: Double = getF(a, b, c)
|
||||
val g: Double = getG(a, b, c, d)
|
||||
val h: Double = getH(f, g)
|
||||
println("f = $f & g = $g & h = $h")
|
||||
|
||||
if (f == 0.0 && g == 0.0 && h == 0.0)
|
||||
solveOneRoot(a, d)
|
||||
else if (h <= 0)
|
||||
solveRealRoots(a, b, g, h)
|
||||
else
|
||||
solveNotRealRoots(a, b, g, h)
|
||||
}
|
||||
|
||||
private fun getF(a: Double, b: Double, c: Double): Double {
|
||||
return (3 * c / a - b * b / (a * a)) / 3
|
||||
}
|
||||
|
||||
private fun getG(a: Double, b: Double, c: Double, d: Double): Double {
|
||||
var g: Double = 2 * b * b * b / (a * a * a)
|
||||
g += -9 * b * c / (a * a)
|
||||
g += 27 * d / a
|
||||
g /= 27.0
|
||||
return g
|
||||
}
|
||||
|
||||
private fun getH(f: Double, g: Double): Double {
|
||||
return g * g / 4 + f * f * f / 27
|
||||
}
|
||||
|
||||
private fun solveOneRoot(a: Double, d: Double) {
|
||||
val x: Double = -Math.pow(d / a, 1.0 / 3)
|
||||
println("The solution is: $x")
|
||||
|
||||
println("rounded off solutions are:")
|
||||
println(String.format("%.3g%n", x))
|
||||
}
|
||||
|
||||
private fun solveRealRoots(a: Double, b: Double, g: Double, h: Double) {
|
||||
val i: Double = Math.sqrt(g * g / 4 - h)
|
||||
val j: Double = Math.pow(i, 1.0 / 3)
|
||||
val k: Double = Math.acos(-g / (2 * i))
|
||||
|
||||
val l: Double = -j
|
||||
val m: Double = Math.cos(k / 3)
|
||||
val n: Double = Math.sqrt(3.0) * Math.sin(k / 3)
|
||||
val p: Double = -b / (3 * a)
|
||||
|
||||
val x1: Double = 2 * j * Math.cos(k / 3) - b / (3 * a)
|
||||
val x2: Double = l * (m + n) + p
|
||||
val x3: Double = l * (m - n) + p
|
||||
|
||||
println("The solutions are:")
|
||||
println(x1)
|
||||
println(x2)
|
||||
println(x3)
|
||||
|
||||
println("rounded off solutions are:")
|
||||
println(String.format("%.3g", x1))
|
||||
println(String.format("%.3g", x2))
|
||||
println(String.format("%.3g", x3))
|
||||
}
|
||||
|
||||
private fun solveNotRealRoots(a: Double, b: Double, g: Double, h: Double) {
|
||||
|
||||
val r: Double = -(g / 2) + Math.sqrt(h)
|
||||
val s: Double = if (r >= 0) Math.pow(r, 1.0 / 3) else -Math.pow(-r, 1.0 / 3)
|
||||
val t: Double = -(g / 2) - Math.sqrt(h)
|
||||
val u: Double = if (t >= 0) Math.pow(t, 1.0 / 3) else -Math.pow(-t, 1.0 / 3)
|
||||
|
||||
val x1: Double = s + u - b / (3 * a)
|
||||
val x2: Double = -((s + u) / 2) - b / (3 * a)
|
||||
val immaginary: Double = (s - u) * Math.sqrt(3.0) / 2
|
||||
|
||||
println("The solutions are:")
|
||||
println(x1)
|
||||
println(x2.toString() + " + " + immaginary + "i")
|
||||
println(x2.toString() + " - " + immaginary + "i")
|
||||
}
|
||||
}
|
||||
15
app/src/test/java/com/example/hpos/OtherTests.kt
Normal file
15
app/src/test/java/com/example/hpos/OtherTests.kt
Normal file
@@ -0,0 +1,15 @@
|
||||
package com.example.hpos
|
||||
|
||||
import org.junit.Test
|
||||
import java.util.*
|
||||
import java.util.concurrent.atomic.AtomicLong
|
||||
|
||||
class OtherTests {
|
||||
|
||||
@Test
|
||||
fun uniqueId() {
|
||||
val date = Date()
|
||||
date.day
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user