- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个数字求解函数 (Double => Double
),我试图聪明地使用 Numeric[T]
来将两种数字分开.
事实证明这并不容易。剩下的问题是:
Numeric[T]
只有加号、减号等运算符。 隐含证据$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])
参数,但是无法有两个这样的参数(A
和 B
).
这是编译器所说的(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
感谢您的任何想法。
最佳答案
您想使用 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))
请注意,要在 A
类型中获取类似 -2.0
的内容,您需要从 Fractional.one
手动组装它们或使用Fractional.fromInt
。
另一件值得指出的事情是 math.Fractional.Implicits
和 Ordering.Implicits
的使用,这将允许您使用正常的数学语法(+、<、/等),而不是调用 plus
和 div
等函数。
关于scala - Scala Numeric[T] 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13839022/
我以默认数字 js 格式存储数字和货币格式。我允许用户根据他们的区域设置拥有自己的格式。我正在尝试弄清楚如何将他们的自定义格式转换回默认格式,以便一致地存储它,以防他们更改语言环境。 例如设置电流:
我以默认数字 js 格式存储数字和货币格式。我允许用户根据他们的区域设置拥有自己的格式。我正在尝试弄清楚如何将他们的自定义格式转换回默认格式,以便一致地存储它,以防他们更改语言环境。 例如设置电流:
我正在为 as.POSIXct.numeric 苦苦挣扎。以下代码有效: dates <- structure(c(1649285787, 1651134684), class = c("POSIXc
我正在使用 Azavea Numeric Scala 库进行通用数学运算。但是,我无法将它们与 Scala Collections API 一起使用,因为它们需要 scala Numeric,并且
我在仔细阅读文档时注意到,Numeric 类有一个方法 arg,其解释为“如果值为正,则返回 0,否则返回 pi。直接在这个上面的docs 是一个名为 angle 的方法,其解释相同。 我不明白这个方
我试图总结两列并获得错误:二进制运算符的非数字参数。但是,这两列都是数字。因此,我不明白为什么会收到此错误消息。谢谢你的帮助? data_all% mutate(total='5min'+'10min
我正在尝试创建六个站点上物种丰富度的热图。 我有一个站点与物种的矩阵,其中有数字丰度数据。 但是,当我运行代码时,R返回一个错误,指出我的矩阵是非数字的。 谁能想到这个?我感到难过。 导出的数据框链接
我正在使用一个返回 float 的java函数 private float getEventScaledX(float eventX, float resX) { return (eventX
我有一个函数接收 SearchCriteria 的集合对象:列名或键、运算符( = ### && :letter_date <= ### 我在两个日期之间查找。我不能两次使用相同的参数名称,因此我在遍
我正在尝试将值插入到表中,但我不断收到错误消息 ORA-01858: a non-numeric character was found where a numeric was expected 这是
sort 提供两种数字排序。这是来自手册页: -g, --general-numeric-sort compare according to general numerica
我遇到了一个奇怪的问题,将日期列与 SYSDATE 进行比较会产生以下错误: 01858. 00000 - "a non-numeric character was found where a nu
这是表格 CREATE TABLE Employee (EmpID number(5) primary key, SIN Number(9) Not null, Las
在 bash 中按 alt + numeric 会得到 (arg [numeric]) 那是什么? (这类问题更适合问人,而不是试图“猜测”正确的术语以通过互联网搜索文档)。 最佳答案 您要搜索的字词
当我执行准备好的语句时,我得到了 ORA-01858:在需要数字的地方发现了一个非数字字符。当我搜索一些论坛时,他们说这是因为日期数据类型。我准备好的声明是 insert into OPRS_ZON
我正在使用 Elasticsearch Java API 创建索引并编写搜索查询。 索引是在各种字段上创建的。字段之一是创建索引的数字(整数)。 现在我们得到的输入是字符串的形式。我们必须在所有字段中
我想重新映射启用 Numlock 的数字小键盘,使其表现得像关闭 Numlock 的数字小键盘,包括能够在按住 Shift/Ctrl 的情况下扩展选择范围。 我遇到的问题如下 Numpad8::Up
所以我有一个训练集,其中一个属性的域如下: A = {Type1, Type2, Type3, ... ,Type5} 如果域保持这种形式,我就无法应用线性回归,因为数学假设不可能成立,例如: H =
为什么我会通过 Java 应用程序执行此查询从 Oracle 收到上述 SQL 错误? SELECT * FROM TestSchema.TestTable WHERE userid = :userI
我在 JSP 中添加了以下代码,并在将实体保存到数据库时出现一些异常。 $('.validateWeight').click(function(event) { var id = event.tar
我是一名优秀的程序员,十分优秀!