3

我有一个数值求解函数Double => DoubleNumeric[T]

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

  • 如何进行除法;Numeric[T]只有加号、减号等运算符。
  • 为什么编译器找不到implicit evidence$1: Numeric[Double]函数(请参阅下面的编译器输出)

理想情况下,我想说“A两者B都是Double,但如果我将它们混合在一起,请告诉我”。

这是代码:

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])参数,但无法拥有两个这样的参数(forAB)。

这是编译器所说的(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

感谢您的任何想法。

4

3 回答 3

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.0in 类型的东西A,您需要手动组装它们Fractional.one或使用Fractional.fromInt.

值得指出的另一件事是使用math.Fractional.Implicitsand Ordering.Implicits,它允许您使用普通的数学语法(+、<、/ 等),而不是调用像plusand之类的函数div

于 2012-12-12T12:33:46.623 回答
0

如果你想让编译器告诉你什么时候类型参数AB不一样,只需使用一个类型参数:

class Sweep[A: Numeric]( fDiff: A => A, initialSeed: A, initialStep: A, bEps: A )
于 2012-12-12T11:58:29.667 回答
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)

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

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

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))
}
于 2012-12-12T12:06:20.763 回答