Skip to content

Commit

Permalink
Bump Typeleve's cats version.
Browse files Browse the repository at this point in the history
  • Loading branch information
pfcoperez committed Feb 5, 2018
1 parent efb6d9d commit a442ae6
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 34 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ scalaVersion := projectScalaVersion
libraryDependencies ++= Seq(
"org.scalatest" %% "scalatest" % "3.0.0",
"org.scalacheck" %% "scalacheck" % "1.13.2" % "test",
"org.typelevel" %% "cats" % "0.9.0",
"org.typelevel" %% "cats-core" % "1.0.1",
"com.storm-enroute" %% "scalameter" % "0.7"
)

Expand Down
28 changes: 14 additions & 14 deletions src/main/scala/org/pfcoperez/dailyalgorithm/Algebra.scala
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,15 @@ object Algebra {
object DirectComputation extends ConvolutionMethod {

/**
* Direct 2D convolution computation
*
* O(n*m*f^2),
* n = no rows, m = no columns, f = kernel number of rows = kernel number of columns.
*
**/
* Direct 2D convolution computation
*
* O(n*m*f^2),
* n = no rows, m = no columns, f = kernel number of rows = kernel number of columns.
*
*/
def convolutionImplementation[T](M: Matrix[T], Kernel: Matrix[T], stride: Int)(
implicit num: Numeric[T], clsTag: ClassTag[T]
): Matrix[T] = {
implicit
num: Numeric[T], clsTag: ClassTag[T]): Matrix[T] = {
import num._
val (f, _) = size(Kernel)
val (m, n) = size(M)
Expand Down Expand Up @@ -263,7 +263,7 @@ object Algebra {

import org.pfcoperez.dailyalgorithm.Algebra.Matrix.NumericMatrix.{ MultiplicationMethod, DeterminantMethod }
val numericTypeclass = implicitly[Numeric[T]]
import numericTypeclass.{mkNumericOps, zero}
import numericTypeclass.{ mkNumericOps, zero }

def *(that: Matrix[T])(implicit multiplicationMethod: MultiplicationMethod): Matrix[T] =
multiplicationMethod.multiply(m, that)
Expand All @@ -287,16 +287,16 @@ object Algebra {

private val zeroRow: Array[T] = new Array[T] {
def apply(j: Int): T = zero
def length: Int = coreM + 2*padding
def length: Int = coreM + 2 * padding
}

def apply(i: Int): Array[T] =
if((0 <= i && i < padding) || (coreN <= i && i < coreN+padding)) zeroRow
if ((0 <= i && i < padding) || (coreN <= i && i < coreN + padding)) zeroRow
else new Array[T] {
def apply(j: Int): T =
if((0 <= j && j < padding) || (coreM <= j && j < coreM+padding)) zero
else m(i-padding)(j-padding)
def length: Int = coreN + 2*padding
if ((0 <= j && j < padding) || (coreM <= j && j < coreM + padding)) zero
else m(i - padding)(j - padding)
def length: Int = coreN + 2 * padding
}

def length: Int = m.headOption.map(_.length).getOrElse(0)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
package org.pfcoperez.dailyalgorithm.algebra

import org.scalatest.{Matchers, WordSpec}
import org.scalatest.{ Matchers, WordSpec }

import org.pfcoperez.dailyalgorithm.Algebra.Matrix._
import org.pfcoperez.dailyalgorithm.Algebra.Matrix

import NumericMatrix.Implicits._


class ConvolutionSpec extends WordSpec with Matchers {

def identityKernel(f: Int): Matrix[Int] = {
require(f % 2 == 1)
positionalValues(f, f) { (i, j) =>
if(i == f/2 && i == j) 1 else 0
if (i == f / 2 && i == j) 1 else 0
}
}

import MatrixMultiplicationSpec.randomIntMatrix
import DefaultConvolutionMethod.convolve


"Matrix convolution" when {

"used with identity kernels" should {
Expand All @@ -33,8 +31,8 @@ class ConvolutionSpec extends WordSpec with Matchers {
val m = randomIntMatrix(nM, nM)
val k = identityKernel(f)

val expected = positionalValues(nM-2, mM-2) { (i, j) =>
m(i+1)(j+1)
val expected = positionalValues(nM - 2, mM - 2) { (i, j) =>
m(i + 1)(j + 1)
}

println(PrintableMatrix(k))
Expand All @@ -44,7 +42,7 @@ class ConvolutionSpec extends WordSpec with Matchers {
println(PrintableMatrix(expected))
println

convolve(m, k) should equal (expected)
convolve(m, k) should equal(expected)

}

Expand All @@ -57,7 +55,7 @@ class ConvolutionSpec extends WordSpec with Matchers {
val k = identityKernel(f)

val expected = positionalValues(2, 2) { (i, j) =>
m(1+i*2)(1+j*2)
m(1 + i * 2)(1 + j * 2)
}

println(PrintableMatrix(k))
Expand All @@ -67,7 +65,7 @@ class ConvolutionSpec extends WordSpec with Matchers {
println(PrintableMatrix(expected))
println

convolve(m, k, 2) should equal (expected)
convolve(m, k, 2) should equal(expected)

}

Expand Down Expand Up @@ -99,6 +97,4 @@ class ConvolutionSpec extends WordSpec with Matchers {

}



}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package org.pfcoperez.dailyalgorithm.algebra
import org.pfcoperez.dailyalgorithm.Algebra.Matrix._
import org.pfcoperez.dailyalgorithm.Algebra.Matrix.NumericMatrix

import org.scalatest.{FlatSpec, Matchers}
import org.scalatest.{ FlatSpec, Matchers }

class PaddingSpec extends FlatSpec with Matchers {

Expand All @@ -12,8 +12,9 @@ class PaddingSpec extends FlatSpec with Matchers {
val padding = 2
val (n, m) = (13, 23)

val matrix = positionalValues(n,m) { case (i, j) =>
i*m + j + 1
val matrix = positionalValues(n, m) {
case (i, j) =>
i * m + j + 1
}

val paddedMatrix = matrix.padded(padding)
Expand All @@ -22,12 +23,12 @@ class PaddingSpec extends FlatSpec with Matchers {
i < padding || i >= n || j < padding || j >= m

for {
i <- 0 until (n+padding)
j <- 0 until (m+padding)
i <- 0 until (n + padding)
j <- 0 until (m + padding)
} {
val value = paddedMatrix(i)(j)

if(isPaddedPosition(i, j)) value shouldBe 0
if (isPaddedPosition(i, j)) value shouldBe 0
else value should not be 0
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ class StateAutomataSpec extends WordSpec with Matchers with Inspectors with Insi

"StateAutomata" when {

def justOutput[State, Output](res: Stream[Either[Error, AutomatonStep[State, Output]]]
): Stream[Either[Error, Option[Output]]] = res.map(_.right.map(_.output))
def justOutput[State, Output](res: Stream[Either[Error, AutomatonStep[State, Output]]]): Stream[Either[Error, Option[Output]]] = res.map(_.right.map(_.output))

"modeling finite state machines" should {

Expand Down

0 comments on commit a442ae6

Please sign in to comment.