Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Scalaで四元数を扱うためのクラス及びテストのための簡易テスト用のコード。
package info.pandanote.test
import Quaternion._
object QuaternionTest {
def main(args: Array[String]): Unit = {
val a = Quaternion(-2,4,-3,3)
val b = Quaternion(-3,3,5,0)
val c = Quaternion(5,0,9,-4)
val d = Quaternion(9,-4,4,-1)
val e = Quaternion(4,-1,-2,4)
val f = -3.000428692900978
val g = 10
val aa = Quaternion(2,1,3,-3)
val ab = Quaternion(3,-3,4,5)
val ac = Quaternion(4,5,2,1)
val ad = Quaternion.one
// toString()
println("--- toString() ---")
println("a="+a.toString)
println("b="+b.toString)
println("c="+c.toString)
println("d="+d.toString)
println("e="+e.toString)
println("aa="+aa.toString)
println("ab="+ab.toString)
println("ac="+ac.toString)
println("ad="+ad.toString)
println(Quaternion.one)
println(Quaternion.zero)
// re()
println("--- re() ---")
println("Re(a) = "+a.re.toString)
println("Re(b) = "+b.re.toString)
println("Re(c) = "+c.re.toString)
println("Re(d) = "+d.re.toString)
println("Re(e) = "+e.re.toString)
println("Re(aa) = "+aa.re.toString)
println("Re(ab) = "+ab.re.toString)
println("Re(ac) = "+ac.re.toString)
println("Re(ad) = "+ad.re.toString)
println("Re(Quaternion.one) = "+Quaternion.one.re.toString)
// i()
println("--- i() ---")
println("a.i = "+a.i.toString)
println("b.i = "+b.i.toString)
println("c.i = "+c.i.toString)
println("d.i = "+d.i.toString)
println("e.i = "+e.i.toString)
println("aa.i = "+aa.i.toString)
println("ab.i = "+ab.i.toString)
println("ac.i = "+ac.i.toString)
println("ad.i = "+ad.i.toString)
// j()
println("--- j() ---")
println("a.j = "+a.j.toString)
println("b.j = "+b.j.toString)
println("c.j = "+c.j.toString)
println("d.j = "+d.j.toString)
println("e.j = "+e.j.toString)
println("aa.j = "+aa.j.toString)
println("ab.j = "+ab.j.toString)
println("ac.j = "+ac.j.toString)
println("ad.j = "+ad.j.toString)
// k()
println("--- k() ---")
println("a.k = "+a.k.toString)
println("b.k = "+b.k.toString)
println("c.k = "+c.k.toString)
println("d.k = "+d.k.toString)
println("e.k = "+e.k.toString)
println("aa.k = "+aa.k.toString)
println("ab.k = "+ab.k.toString)
println("ac.k = "+ac.k.toString)
println("ad.k = "+ad.k.toString)
// +(that: Quaternion)
println("--- +(that: Quaternion) ---")
println("a + b = "+(a + b).toString)
println("b + c = "+(b + c).toString)
println("c + d = "+(c + d).toString)
println("d + e = "+(d + e).toString)
println("aa + 1 = "+(aa+Quaternion.one).toString)
// +(that: Int)
println("--- +(that: Int) ---")
println("a + 2 = "+(a + 2).toString)
println("b + 3 = "+(b + 3).toString)
println("c + 4 = "+(c + 4).toString)
println("d + 5 = "+(d + 5).toString)
println("e + 6 = "+(d + 6).toString)
// +(that: Long)
println("--- +(that: Long) ---")
println("a + 2L = "+(a + 2L).toString)
println("b + 4L = "+(b + 4L).toString)
println("c + 8L = "+(c + 8L).toString)
println("d + 16L = "+(d + 16L).toString)
println("e + 32L = "+(e + 32L).toString)
// +(that: Float)
println("--- +(that: Float) ---")
println("a + 0.1f = "+(a + 0.1f).toString)
println("b + 0.2f = "+(b + 0.2f).toString)
println("c + 0.3f = "+(c + 0.3f).toString)
println("d + 0.4f = "+(d + 0.4f).toString)
println("e + 0.5f = "+(e + 0.5f).toString)
// +(that: Double)
println("--- +(that: Double) ---")
println("a + 0.3 = "+(a + 0.3).toString)
println("b + 0.6 = "+(b + 0.6).toString)
println("c + 0.9 = "+(c + 0.9).toString)
println("d + 1.2 = "+(d + 1.2).toString)
println("e + 1.5 = "+(e + 1.5).toString)
// -(that: Quaternion)
println("--- -(that: Quaternion) ---")
println("a - b = "+(a - b).toString)
println("b - c = "+(b - c).toString)
println("c - d = "+(c - d).toString)
println("d - e = "+(d - e).toString)
println("aa - ab = "+(aa - ab).toString)
// -(that: Int)
println("--- -(that: Int) ---")
println("a - 2 = "+(a - 2).toString)
println("b - 3 = "+(b - 3).toString)
println("c - 4 = "+(c - 4).toString)
println("d - 5 = "+(d - 5).toString)
println("e - 6 = "+(e - 6).toString)
// -(that: Long)
println("--- -(that: Long) ---")
println("a - 1L = "+(a - 1L).toString)
println("b - 2L = "+(b - 2L).toString)
println("c - 4L = "+(c - 4L).toString)
println("d - 8L = "+(d - 8L).toString)
println("e - 16L = "+(e - 16L).toString)
// -(that: Float)
println("--- -(that: Float) ---")
println("a - 0.1f = "+(a - 0.1f).toString)
println("b - 0.2f = "+(b - 0.2f).toString)
println("c - 0.3f = "+(c - 0.3f).toString)
println("d - 0.4f = "+(d - 0.4f).toString)
println("e - 0.5f = "+(e - 0.5f).toString)
// -(that: Double)
println("--- -(that: Double) ---")
println("a - 0.3 = "+(a - 0.3).toString)
println("b - 0.6 = "+(b - 0.6).toString)
println("c - 0.9 = "+(c - 0.9).toString)
println("d - 1.2 = "+(d - 1.2).toString)
println("e - 1.5 = "+(e - 1.5).toString)
// *(that: Quaternion)
println("--- *(that: Quaternion) ---")
println("a * b = "+(a * b).toString)
println("b * c = "+(b * c).toString)
println("c * d = "+(c * d).toString)
println("d * e = "+(d * e).toString)
println("aa * ac = "+(aa*ac).toString)
// *(that: Int)
println("--- *(that: Int) ---")
println("a * 2 = "+(a * 2).toString)
println("b * 3 = "+(b * 3).toString)
println("c * 4 = "+(c * 4).toString)
println("d * 5 = "+(d * 5).toString)
println("e * 6 = "+(e * 6).toString)
// *(that: Long)
println("--- *(that: Long) ---")
println("a * 7L = "+(a * 7L).toString)
println("b * 8L = "+(b * 8L).toString)
println("c * 9L = "+(c * 9L).toString)
println("d * 10L = "+(d * 10L).toString)
println("e * 11L = "+(e * 11L).toString)
// *(that: Float)
println("--- *(that: Float) ---")
println("a * 0.2f = "+(a * 0.2f).toString)
println("b * 0.3f = "+(b * 0.3f).toString)
println("c * 0.4f = "+(c * 0.4f).toString)
println("d * 0.5f = "+(d * 0.5f).toString)
println("e * 0.6f = "+(e * 0.6f).toString)
// *(that: Double)
println("--- *(that: Double) ---")
println("a * 0.2 = "+(a * 0.2).toString)
println("b * 0.3 = "+(b * 0.3).toString)
println("c * 0.4 = "+(c * 0.4).toString)
println("d * 0.5 = "+(d * 0.5).toString)
println("e * 0.6 = "+(e * 0.6).toString)
// /(that: Quaternion)
println("--- /(that: Quaternion) ---")
println("a / b = "+(a / b).toString)
println("b / c = "+(b / c).toString)
println("c / d = "+(c / d).toString)
println("d / e = "+(d / e).toString)
println("aa / ab = "+(aa/ab).toString)
// /(that: Int)
println("--- /(that: Int) ---")
println("a / 2 = "+(a / 2).toString)
println("b / 3 = "+(b / 3).toString)
println("c / 4 = "+(c / 4).toString)
println("d / 5 = "+(d / 5).toString)
println("e / 6 = "+(e / 6).toString)
println("ab / 4 = "+(ab / 4).toString)
// /(that: Long)
println("--- /(that: Long) ---")
println("a / 6L = "+(a / 6L).toString)
println("b / 5L = "+(b / 5L).toString)
println("c / 4L = "+(c / 4L).toString)
println("d / 3L = "+(d / 3L).toString)
println("e / 2L = "+(e / 2L).toString)
// /(that: Float)
println("--- /(that: Float) ---")
println("a / 0.5f = "+(a / 0.5f).toString)
println("b / 0.6f = "+(b / 0.6f).toString)
println("c / 0.7f = "+(c / 0.7f).toString)
println("d / 0.8f = "+(d / 0.8f).toString)
println("e / 0.9f = "+(e / 0.9f).toString)
// /(that: Double)
println("--- /(that: Double) ---")
println("a / 1.1 = "+(a / 1.1).toString)
println("b / 1.2 = "+(b / 1.2).toString)
println("c / 1.3 = "+(c / 1.3).toString)
println("d / 1.4 = "+(d / 1.4).toString)
println("e / 1.5 = "+(e / 1.5).toString)
// %(that: Quaternion)
println("--- %(that: Quaternion) ---")
println("a % b = "+(a % b).toString)
println("b % c = "+(b % c).toString)
println("c % d = "+(c % d).toString)
println("d % e = "+(d % e).toString)
// %(that: Int)
println("--- %(that: Int) ---")
println("a % 5 = "+(a % 5).toString)
println("b % 6 = "+(b % 6).toString)
println("c % 7 = "+(c % 7).toString)
println("d % 8 = "+(d % 8).toString)
println("ab % 4 = "+(ab % 4).toString)
// %(that: Long)
println("--- %(that: Long) ---")
println("a % 5L = "+(a % 5L).toString)
println("b % 6L = "+(b % 6L).toString)
println("c % 7L = "+(c % 7L).toString)
println("d % 8L = "+(d % 8L).toString)
println("e % 4L = "+(e % 9L).toString)
// %(that: Float)
println("--- %(that: Float) ---")
println("a % 1.5f = "+(a % 1.5f).toString)
println("b % 2.0f = "+(b % 2.0f).toString)
println("c % 2.5f = "+(c % 2.5f).toString)
println("d % 3.0f = "+(d % 3.0f).toString)
println("e % 4.0f = "+(e % 4.0f).toString)
// %(that: Double)
println("--- %(that: Double) ---")
println("a % 1.5 = "+(a % 1.5).toString)
println("b % 2.0 = "+(b % 2.0).toString)
println("c % 2.5 = "+(c % 2.5).toString)
println("d % 3.0 = "+(d % 3.0).toString)
println("e % 4.0 = "+(d % 4.0).toString)
// unary_- = Quaternion(-real,-imag,this.imagUnit)
println("--- unary_- ---")
println("-a = "+(-a).toString)
println("-b = "+(-b).toString)
println("-c = "+(-c).toString)
println("-d = "+(-d).toString)
println("-e = "+(-e).toString)
// abs = math.sqrt(real*real+imag*imag)
println("--- abs ---")
println("a.abs = "+a.abs.toString)
println("b.abs = "+b.abs.toString)
println("c.abs = "+c.abs.toString)
println("d.abs = "+d.abs.toString)
println("e.abs = "+e.abs.toString)
// conjugate = Quaternion(real,-imag,this.imagUnit)
println("--- conjugate ---")
println("a.conjugate = "+a.conjugate.toString)
println("b.conjugate = "+b.conjugate.toString)
println("c.conjugate = "+c.conjugate.toString)
println("d.conjugate = "+d.conjugate.toString)
println("e.conjugate = "+e.conjugate.toString)
println("aa.conjugate*(ac-Quaternion.one*7) = "+(aa.conjugate*(ac-Quaternion.one*7)).toString)
// log = Quaternion(math.log(abs))
println("--- log ---")
println("a.log = "+a.log.toString)
println("b.log = "+b.log.toString)
println("c.log = "+c.log.toString)
println("d.log = "+d.log.toString)
println("e.log = "+e.log.toString)
// exp = {
println("--- exp ---")
println("a.exp = "+a.exp.toString)
println("b.exp = "+b.exp.toString)
println("c.exp = "+c.exp.toString)
println("d.exp = "+d.exp.toString)
println("e.exp = "+e.exp.toString)
println("aa.exp = "+aa.exp.toString)
println("aa.exp.log = "+aa.exp.log.toString)
println("aa.exp.log != aa: "+(aa.exp.log == aa))
// pow(that: Double)
println("--- pow(that: Double) ---")
println("a pow 0.2 = "+(a pow 0.2).toString)
println("b pow 0.4 = "+(b pow 0.4).toString)
println("c pow 0.6 = "+(c pow 0.6).toString)
println("d pow 0.8 = "+(d pow 0.8).toString)
println("e pow 1.1 = "+(e pow 1.1).toString)
println("a.pow(0.2) = "+a.pow(0.2).toString)
println("b.pow(0.4) = "+b.pow(0.4).toString)
println("c.pow(0.6) = "+c.pow(0.6).toString)
println("d.pow(0.8) = "+d.pow(0.8).toString)
println("e.pow(1.1) = "+e.pow(1.1).toString)
// pow(that: Int)
println("--- pow(that: Int) ---")
println("aa.pow(3) = "+aa.pow(3).toString)
// pow(that: Quaternion)
println("--- pow(that: Quaternion) ---")
println("a pow b = "+(a pow b).toString)
println("b pow c = "+(b pow c).toString)
println("c pow d = "+(c pow d).toString)
println("d pow e = "+(d pow e).toString)
println("e pow a = "+(e pow a).toString)
println("a.pow(b) = "+a.pow(b).toString)
println("b.pow(c) = "+b.pow(c).toString)
println("c.pow(d) = "+c.pow(d).toString)
println("d.pow(e) = "+d.pow(e).toString)
println("e.pow(a) = "+e.pow(a).toString)
println("aa.pow(aa) = "+aa.pow(aa).toString)
// **(that: Quaternion)
println("--- **(that: Quaternion) ---")
println("a ** b = "+(a ** b).toString)
println("b ** c = "+(b ** c).toString)
println("c ** d = "+(c ** d).toString)
println("d ** e = "+(d ** e).toString)
println("e ** a = "+(d ** e).toString)
println("aa ** ab = "+(aa**ab).toString)
// **(that: Double)
println("--- **(that: Double) ---")
println("a ** 0.2 = "+(a ** 0.2).toString)
println("b ** 0.4 = "+(b ** 0.4).toString)
println("c ** 0.6 = "+(c ** 0.6).toString)
println("d ** 0.8 = "+(d ** 0.8).toString)
println("e ** 1.1 = "+(e ** 1.1).toString)
// **(that: Int)
println("--- **(that: Int) ---")
println("aa ** 2 = "+(aa ** 2).toString)
// equals(that: Any)
println("--- equals(that: Any) ---")
println("a equals b: "+(a equals b).toString)
println("b equals c: "+(b equals c).toString)
println("c equals d: "+(c equals d).toString)
println("d equals e: "+(d equals e).toString)
println("e equals a: "+(e equals a).toString)
println("a equals a: "+(a equals a).toString)
println("b equals b: "+(b equals b).toString)
println("c equals c: "+(c equals c).toString)
println("d equals d: "+(d equals d).toString)
println("e equals e: "+(e equals e).toString)
println("ad == 1: "+(ad == 1).toString)
println("ad.equals(1): "+ad.equals(1))
println("aa != aa: "+(aa!=aa))
// <>(that: Quaternion)
println("--- <>(that: Quaternion) ---")
println("a <> b: "+(a <> b).toString)
println("b <> c: "+(b <> c).toString)
println("c <> d: "+(c <> d).toString)
println("d <> e: "+(d <> e).toString)
println("aa <> aa: "+(aa<>aa).toString)
// exp(that: Quaternion)
println("--- Quaternion.exp(that: Quaternion)---")
println("Quaternion.exp(a) = "+Quaternion.exp(a).toString)
println("Quaternion.exp(b) = "+Quaternion.exp(b).toString)
println("Quaternion.exp(c) = "+Quaternion.exp(c).toString)
println("Quaternion.exp(d) = "+Quaternion.exp(d).toString)
println("Quaternion.exp(e) = "+Quaternion.exp(e).toString)
println("Quaternion.exp(aa) = "+Quaternion.exp(aa).toString)
// log(that: Quaternion)
println("--- Quaternion.log(that: Quaternion)---")
println("Quaternion.log(a) = "+Quaternion.log(a).toString)
println("Quaternion.log(b) = "+Quaternion.log(b).toString)
println("Quaternion.log(c) = "+Quaternion.log(c).toString)
println("Quaternion.log(d) = "+Quaternion.log(d).toString)
println("Quaternion.log(e) = "+Quaternion.log(e).toString)
// Imaginary Unit and others.
println("--- Imaginary Unit and others. ---")
val aj = new Quaternion(2,-5,-1,6)
println("aj = "+aj)
println("aj * aa = "+(aj*aa).toString)
println("aa * aj = "+(aa*aj).toString)
println("aj.pow(0) = "+aj.pow(0))
println("aj.pow(2) = "+aj.pow(2))
println("aj * aj = "+(aj*aj))
println("aj.pow(2).log = "+aj.pow(2).log)
println("aj.pow(2).pow(0.5) = "+aj.pow(2).pow(0.5))
// -- Doubleクラスにメソッドを追加してみた。--
println("Append some methods to handle Quaternion")
println("g*a = "+(g*a).toString)
println("g/a = "+(g/a).toString)
println("g+a = "+(g+a).toString)
val g1: Double = 1
val g2: Double = 1
println("g1 equals g2: "+(g1 equals g2).toString)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.