我尝试使用spire,一个数学框架,但我有一条错误消息:
import spire.algebra._
import spire.implicits._
trait AbGroup[A] extends Group[A]
final class Rationnel_Quadratique(val n1: Int = 2)(val coef: (Int, Int)) {
override def toString = {
coef match {
case (c, i) =>
s"$c + $i?$n"
}
}
def a() = coef._1
def b() = coef._2
def n() = n1
}
object Rationnel_Quadratique {
def apply(coef: (Int, Int),n: Int = 2)= {
new Rationnel_Quadratique(n)(coef)
}
}
object AbGroup {
implicit object RQAbGroup extends AbGroup[Rationnel_Quadratique] {
def +(a: Rationnel_Quadratique, b: Rationnel_Quadratique): Rationnel_Quadratique = Rationnel_Quadratique(coef=(a.a() + b.a(), a.b() + b.b()))
def inverse(a: Rationnel_Quadratique): Rationnel_Quadratique = Rationnel_Quadratique((-a.a(), -a.b()))
def id: Rationnel_Quadratique = Rationnel_Quadratique((0, 0))
}
}
object euler66_2 extends App {
val c = Rationnel_Quadratique((1, 2))
val d = Rationnel_Quadratique((3, 4))
val e = c + d
println(e)
}
Run Code Online (Sandbox Code Playgroud)
该程序预计会增加1 +2√2和3 +4√2,但我有这个错误:
找不到spire类型的证据参数的隐含值.algebra.AdditiveSemigroup [Rationnel_Quadratique] val e = c + d ^
我认为有一些必要的东西我错过了(使用含义吗?)
看起来你没有正确使用Spire.
Spire已经有了一个AbGroup类型,所以你应该使用它而不是重新定义自己的类型.这是一个使用我创建的简单类型的示例X.
import spire.implicits._
import spire.algebra._
case class X(n: BigInt)
object X {
implicit object XAbGroup extends AbGroup[X] {
def id: X = X(BigInt(0))
def op(lhs: X, rhs: X): X = X(lhs.n + rhs.n)
def inverse(lhs: X): X = X(-lhs.n)
}
}
def test(a: X, b: X): X = a |+| b
Run Code Online (Sandbox Code Playgroud)
请注意,对于组(以及半群和幺半群),您要使用|+|而不是+.要得到加分,你需要定义一些东西AdditiveSemigroup(例如Semiring,或Ring,Field或者什么).
如果有意义的话,你也会使用.inverse而|-|不是一元和二元-.
查看您的代码,我也不确定您的实际数字类型是否正确.如果我想添加两个具有不同值的数字,会发生什么n?
无论如何,希望这能为你解决一些问题.
编辑:既然你似乎也对Scala语法感到困惑,那么让我尝试草拟一些可能有用的设计.首先,总有一个更通用的解决方案:
import spire.implicits._
import spire.algebra._
import spire.math._
case class RQ(m: Map[Natural, SafeLong]) {
override def toString: String = m.map {
case (k, v) => if (k == 1) s"$v" else s"$v?$k" }.mkString(" + ")
}
object RQ {
implicit def abgroup[R <: Radical](implicit r: R): AbGroup[RQ] =
new AbGroup[RQ] {
def id: RQ = RQ(Map.empty)
def op(lhs: RQ, rhs: RQ): RQ = RQ(lhs.m + rhs.m)
def inverse(lhs: RQ): RQ = RQ(-lhs.m)
}
}
object Test {
def main(args: Array[String]) {
implicit val radical = _2
val x = RQ(Map(Natural(1) -> 1, Natural(2) -> 2))
val y = RQ(Map(Natural(1) -> 3, Natural(2) -> 4))
println(x)
println(y)
println(x |+| y)
}
}
Run Code Online (Sandbox Code Playgroud)
这允许您以一些间接为代价,毫无问题地将不同的根添加在一起.您还可以使用以下内容更贴近您的设计:
import spire.implicits._
import spire.algebra._
abstract class Radical(val n: Int) { override def toString: String = n.toString }
case object _2 extends Radical(2)
case object _3 extends Radical(3)
case class RQ[R <: Radical](a: Int, b: Int)(implicit r: R) {
override def toString: String = s"$a + $b?$r"
}
object RQ {
implicit def abgroup[R <: Radical](implicit r: R): AbGroup[RQ[R]] =
new AbGroup[RQ[R]] {
def id: RQ[R] = RQ[R](0, 0)
def op(lhs: RQ[R], rhs: RQ[R]): RQ[R] = RQ[R](lhs.a + rhs.a, lhs.b + rhs.b)
def inverse(lhs: RQ[R]): RQ[R] = RQ[R](-lhs.a, -lhs.b)
}
}
object Test {
def main(args: Array[String]) {
implicit val radical = _2
val x = RQ[_2.type](1, 2)
val y = RQ[_2.type](3, 4)
println(x)
println(y)
println(x |+| y)
}
}
Run Code Online (Sandbox Code Playgroud)
这种方法创建一个假类型来表示您正在使用的任何基础(例如√2)并QR对该类型进行参数化.通过这种方式,您可以确保没有人会尝试执行无效的添加.
希望其中一种方法对您有用.