Scala数字的问题[T]

时间:2012-12-12 11:43:57

标签: scala types

我有一个数值求解函数(Double => Double),我试图变得聪明并使用Numeric[T]来保持两种数字分开。

这并不容易。剩下的问题是:

  • 如何分工; Numeric[T]只有加号,减号等操作符。
  • 为什么编译器找不到implicit evidence$1: Numeric[Double]函数(参见下面的编译器输出)

理想情况下,我想说," AB都是Double,但请告诉我我是否将它们相互混合了#34;

以下是代码:

import scala.annotation.tailrec

class Sweep[A: Numeric, B: Numeric]( fDiff: A => B, initialSeed: A, initialStep: A, bEps: B )
{
  val anum= evidence$1
  val bnum= evidence$2

  assert( anum.signum(initialStep) > 0 )
  assert( bnum.lt( fDiff(initialSeed), fDiff( anum.plus(initialSeed,initialStep) )) )   // check that it's an increasing function

  @tailrec
  private def sweep( seed: A, step: A ): A = {
    val bDiff= fDiff(seed)

    if ( bnum.lt( bnum.abs(bDiff), bEps) ) {  // done
      seed
    } else if ( bnum.signum(bDiff) != anum.signum(step) ) {
      sweep( anum.plus(seed,step), step )   // continue, same step and direction ('bDiff' should go smaller)
    } else {
      val newStep = anum.toDouble(step) / -2.0
      sweep( anum.minus(seed,newStep), newStep )    // reverse, smaller step
    }
  }

  // Make sure we take the initial step in the right direction
  //  
  private lazy val stepSign= -bnum.signum( fDiff(initialSeed) )

  def apply: A = sweep( initialSeed, stepSign * initialStep )
}

object TestX extends App {

  val t= new Sweep( (a: Double) => (a*a)-2, 1.0, 0.5, 1e-3 )()

  println( t, math.sqrt(2.0) )
}

我已经尝试使用较旧的(implicit anum: Numeric[A])参数,但无法使用两个(AB)。

这是编译器所说的内容(Scala 2.9):

fsc -deprecation -d out-make -unchecked src/xxx.scala
src/xxx.scala:25: error: type mismatch;
 found   : newStep.type (with underlying type Double)
 required: A
      sweep( anum.minus(seed,newStep), newStep )    // reverse, smaller step
                             ^
src/xxx.scala:33: error: overloaded method value * with alternatives:
  (x: Double)Double <and>
  (x: Float)Float <and>
  (x: Long)Long <and>
  (x: Int)Int <and>
  (x: Char)Int <and>
  (x: Short)Int <and>
  (x: Byte)Int
 cannot be applied to (A)
  def apply: A = sweep( initialSeed, stepSign * initialStep )
                                              ^
src/xxx.scala:38: error: not enough arguments for constructor Sweep: (implicit evidence$1: Numeric[Double], implicit evidence$2: Numeric[Double])Sweep[Double,Double].
Unspecified value parameters evidence$1, evidence$2.
  val t= new Sweep( (a: Double) => (a*a)-2, 1.0, 0.5, 1e-3 )()
         ^
three errors found

感谢任何想法。

3 个答案:

答案 0 :(得分:3)

您希望使用Fractional代替Numeric。以下内容为我编译:

import scala.annotation.tailrec
import math.Fractional.Implicits._
import Ordering.Implicits._

class Sweep[A: Fractional, B: Fractional](fDiff: A => B, initialSeed: A, initialStep: A, bEps: B) {
  val aFractional = implicitly[Fractional[A]]

  assert(initialStep.signum > 0)
  assert(fDiff(initialSeed) < fDiff(initialSeed + initialStep))

  @tailrec
  private def sweep(seed: A, step: A): A = {
    val bDiff = fDiff(seed)
    if (bDiff.abs < bEps) {
      seed
    } else if (bDiff.signum != step.signum) {
      sweep(seed + step, step)
    } else {
      val one = aFractional.one
      val newStep = step / aFractional.fromInt(-2)
      sweep(seed - newStep, newStep)
    }
  }

  private lazy val stepSign = aFractional.fromInt(-fDiff(initialSeed).signum)
  def apply: A = sweep(initialSeed, stepSign * initialStep)
}

val sweep = new Sweep((a: Double) => (a*a)-2, 1.0, 0.5, 1e-3)
println(sweep.apply, math.sqrt(2.0))

请注意,要在-2.0类型中获取A之类的内容,您需要从Fractional.one手动汇总它们或使用Fractional.fromInt

值得指出的另一件事是使用math.Fractional.ImplicitsOrdering.Implicits,这将允许您使用普通的数学语法(+,&lt;,/等),而不是像{一样调用函数{1}}和plus

答案 1 :(得分:0)

如果您希望编译器告诉您何时类型参数AB不相同,只需使用一个类型参数:

class Sweep[A: Numeric]( fDiff: A => A, initialSeed: A, initialStep: A, bEps: A )

答案 2 :(得分:0)

问题似乎是你在这里使用Double ......

val newStep = anum.toDouble(step) / -2.0

...虽然您想使用Numeric并在下一行实际使用它。

对于除法,请查看Numeric的子类型IntegralFractional

编译器找不到隐式证据,因为你明确地传递了无:

new Sweep((a: Double) => (a*a)-2, 1.0, 0.5, 1e-3)()

删除显式空参数列表修复了:

new Sweep((a: Double) => (a*a)-2, 1.0, 0.5, 1e-3)

我不确定不要混合AB的要求,因为您已经在代码的多个位置执行此操作。

我不确定这是你想要的,但以下代码有效:

import scala.annotation.tailrec

class Sweep[A: Fractional](fDiff: A => A, initialSeed: A, initialStep: A, bEps: A) {
  val num = implicitly[Fractional[A]]

  assert(num.signum(initialStep) > 0)
  assert(num.lt(fDiff(initialSeed), fDiff(num.plus(initialSeed, initialStep)))) // check that it's an increasing function

  @tailrec
  private def sweep(seed: A, step: A): A = {
    val bDiff = fDiff(seed)

    if (num.lt(num.abs(bDiff), bEps)) { // done
      seed
    } else if (num.signum(bDiff) != num.signum(step)) {
      sweeimport scala.annotation.tailrec

class Sweep[A: Fractional](fDiff: A => A, initialSeed: A, initialStep: A, bEps: A) {
  val num = implicitly[Fractional[A]]

  assert(num.signum(initialStep) > 0)
  assert(num.lt(fDiff(initialSeed), fDiff(num.plus(initialSeed, initialStep)))) // check that it's an increasing function

  @tailrec
  private def sweep(seed: A, step: A): A = {
    val bDiff = fDiff(seed)

    if (num.lt(num.abs(bDiff), bEps)) { // done
      seed
    } else if (num.signum(bDiff) != num.signum(step)) {
      sweep(num.plus(seed, step), step) // continue, same step and direction ('bDiff' should go smaller)
    } else {
      val newStep = num.div(step, num.fromInt(-2))
      sweep(num.minus(seed, newStep), newStep) // reverse, smaller step
    }
  }

  // Make sure we take the initial step in the right direction
  private lazy val stepSign = -num.signum(fDiff(initialSeed))

  def apply: A = sweep(initialSeed, num.times(num.fromInt(stepSign), initialStep))
}

object TestX extends App {

  val t = new Sweep((a: Double) => (a * a) - 2, 1.0, 0.5, 1e-3)

  println(t, math.sqrt(2.0))
}

p(num.plus(seed, step), step) // continue, same step and direction ('bDiff' should go smaller)
    } else {
      val newStep = num.div(step, num.fromInt(-2))
      sweep(num.minus(seed, newStep), newStep) // reverse, smaller step
    }
  }

  // Make sure we take the initial step in the right direction
  private lazy val stepSign = -num.signum(fDiff(initialSeed))

  def apply: A = sweep(initialSeed, num.times(num.fromInt(stepSign), initialStep))
}

object TestX extends App {

  val t = new Sweep((a: Double) => (a * a) - 2, 1.0, 0.5, 1e-3)

  println(t, math.sqrt(2.0))
}