Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

new feature: 2 * MathVector(1, 2, 3) is possible

  • Loading branch information...
commit 2c717783aa71380f9d4bc476660c7142da8ac6c0 1 parent 7cd95fd
@schleichardt authored
View
8 src/main/scala/info/schleichardt/math/MathVector.scala
@@ -47,3 +47,11 @@ class MathVector(val content: Seq[Double]) {
}
}
}
+
+object MathVectorScalar {
+ implicit def implicitConversionDoubleToMath(double: Double) = MathVectorScalar(double)
+}
+
+case class MathVectorScalar(value: Double) {
+ def *(vector: MathVector) = vector * value
+}
View
28 src/test/scala/VectorSpec.scala
@@ -2,16 +2,28 @@ import info.schleichardt.math.MathVector
import org.specs._
import org.specs.runner._
import org.junit.runner.RunWith
+import info.schleichardt.math.MathVectorScalar._
@RunWith(classOf[JUnitSuiteRunner])
class VectorSpec extends Specification with JUnit with ScalaTest {
- "add vectors" in {
- MathVector(1,2,3) + MathVector(5, 5, 5) must_== MathVector(6, 7, 8)
- }
- "sub vectors" in {
- MathVector(6, 7, 8) - MathVector(5, 5, 5) must_== MathVector(1,2,3)
- }
- "dot product vectors" in {
- MathVector(6, 7, 8) * MathVector(5, 5, 5) must_== 6 * 5 + 7 * 5 + 8 * 5
+ "vectors" can {
+ "be added to other vectors" in {
+ MathVector(1,2,3) + MathVector(5, 5, 5) must_== MathVector(6, 7, 8)
+ }
+ "subtracted from other vectors" in {
+ MathVector(6, 7, 8) - MathVector(5, 5, 5) must_== MathVector(1,2,3)
+ }
+ "be multiplied with others (dot product)" in {
+ MathVector(6, 7, 8) * MathVector(5, 5, 5) must_== 6 * 5 + 7 * 5 + 8 * 5
+ }
+ "be multiplied with others (cross product)" in {
+ MathVector(1, -5, 2) x MathVector(2, 0, 3) must_== MathVector(-15, 1, 10)
+ }
+ "be multiplied with a number (vector on left side)" in {
+ MathVector(1, -5, 2) * 2 must_== MathVector(2, -10, 4)
+ }
+ "be multiplied with a number (vector on right side)" in {
+ 2 * MathVector(1, -5, 2) must_== MathVector(2, -10, 4)
+ }
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.