当前位置: 首页>>代码示例>>Scala>>正文


Scala abs类代码示例

本文整理汇总了Scala中scala.math.abs的典型用法代码示例。如果您正苦于以下问题:Scala abs类的具体用法?Scala abs怎么用?Scala abs使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了abs类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。

示例1: connectionScore

//设置package包名称以及导入依赖的类
package com.dataintuitive.luciuscore

import com.dataintuitive.luciuscore.Model._

import scala.math.{abs, max}


  def connectionScore(rv1: RankVector, rv2: RankVector): Double = {
    connectionStrength(rv1, rv2) / maxConnectionStrength(rv1, rv2)
  }

  def connectionStrength(rv1: RankVector, rv2: RankVector): Double =
    rv1
      .zip(rv2)
      .map { case (i, j) => i * j }
      .sum

  def maxConnectionStrength(rv1: RankVector, rv2: RankVector): Double = {
    val maxr = rv1.map(abs(_)).foldLeft(0.0)(max(_, _))
    val maxq = rv2.map(abs(_)).foldLeft(0.0)(max(_, _))
    (maxr to (maxr - maxq) by -1)
      .zip(maxq to 0 by -1)
      .map { case (i, j) => i * j }
      .sum
  }

} 
开发者ID:data-intuitive,项目名称:LuciusCore,代码行数:28,代码来源:ZhangScoreFunctions.scala

示例2: Filter

//设置package包名称以及导入依赖的类
package de.sciss.fscape

import scala.math.{Pi, abs, sin}

object Filter {
  def createAntiAliasFilter(impResp: Array[Double], impRespD: Array[Double],
                            halfWinSize: Int, rollOff: Double,
                            kaiserBeta: Double, samplesPerCrossing: Int): Double = {
    createLowPassFilter(impResp, 0.5 * rollOff, halfWinSize, kaiserBeta, samplesPerCrossing)

    if (impRespD != null) {
      var i = 0
      while (i < halfWinSize - 1) {
        impRespD(i) = impResp(i + 1) - impResp(i)
        i += 1
      }
      impRespD(i) = -impResp(i)
    }
    var dcGain = 0.0
    var j = samplesPerCrossing
    while (j < halfWinSize) {
      dcGain += impResp(j)
      j += samplesPerCrossing
    }
    dcGain = 2 * dcGain + impResp(0)

    1.0 / abs(dcGain)
  }

  def createLowPassFilter(impResp: Array[Double], freq: Double, halfWinSize: Int, kaiserBeta: Double,
                          samplesPerCrossing: Int): Unit = {
    val dNum		    = samplesPerCrossing.toDouble
    val smpRate		  = freq * 2.0

    // ideal lpf = infinite sinc-function; create truncated version
    impResp(0) = smpRate.toFloat
    var i = 1
    while (i < halfWinSize) {
      val d = Pi * i / dNum
      impResp(i) = (sin(smpRate * d) / d).toFloat
      i += 1
    }

    // apply Kaiser window
    import de.sciss.fscape.graph.GenWindow.Kaiser
    Kaiser.mul(winSize = halfWinSize * 2, winOff = halfWinSize, buf = impResp, bufOff = 0, len = halfWinSize,
      param = kaiserBeta)
  }
} 
开发者ID:Sciss,项目名称:FScape-next,代码行数:50,代码来源:Filter.scala

示例3: RowComparer

//设置package包名称以及导入依赖的类
package com.github.mrpowers.spark.fast.tests

import org.apache.spark.sql.Row

import scala.math.abs

object RowComparer {

  def areRowsEqual(r1: Row, r2: Row, tol: Double): Boolean = {

    if (r1.length != r2.length) {
      return false
    } else {
      var idx = 0
      val length = r1.length
      while (idx < length) {
        if (r1.isNullAt(idx) != r2.isNullAt(idx)) return false

        if (!r1.isNullAt(idx)) {
          val o1 = r1.get(idx)
          val o2 = r2.get(idx)
          o1 match {
            case b1: Array[Byte] =>
              if (!java.util.Arrays.equals(b1, o2.asInstanceOf[Array[Byte]])) return false

            case f1: Float =>
              if (java.lang.Float.isNaN(f1) != java.lang.Float.isNaN(o2.asInstanceOf[Float])) return false
              if (abs(f1 - o2.asInstanceOf[Float]) > tol) return false

            case d1: Double =>
              if (java.lang.Double.isNaN(d1) != java.lang.Double.isNaN(o2.asInstanceOf[Double])) return false
              if (abs(d1 - o2.asInstanceOf[Double]) > tol) return false

            case d1: java.math.BigDecimal =>
              if (d1.compareTo(o2.asInstanceOf[java.math.BigDecimal]) != 0) return false

            case _ =>
              if (o1 != o2) return false
          }
        }
        idx += 1
      }
    }
    true

  }

} 
开发者ID:MrPowers,项目名称:spark-fast-tests,代码行数:49,代码来源:RowComparer.scala

示例4: DifferencesFilterFactory

//设置package包名称以及导入依赖的类
package com.twitter.diffy.analysis

import javax.inject.Inject

import com.twitter.util.Future
import scala.math.abs

object DifferencesFilterFactory {
  def apply(relative: Double, absolute: Double): JoinedField => Boolean = {
    (field: JoinedField) =>
      field.raw.differences > field.noise.differences &&
        field.relativeDifference > relative &&
        field.absoluteDifference > absolute
  }
}

case class JoinedDifferences @Inject() (raw: RawDifferenceCounter, noise: NoiseDifferenceCounter) {
  def endpoints: Future[Map[String, JoinedEndpoint]] = {
    raw.counter.endpoints map { _.keys } flatMap { eps =>
      Future.collect(
        eps map { ep =>
          endpoint(ep) map { ep -> _ }
        } toSeq
      ) map { _.toMap }
    }
  }

  def endpoint(endpoint: String): Future[JoinedEndpoint] = {
    Future.join(
      raw.counter.endpoint(endpoint),
      raw.counter.fields(endpoint),
      noise.counter.fields(endpoint)
    ) map { case (endpoint, rawFields, noiseFields) =>
      JoinedEndpoint(endpoint, rawFields, noiseFields)
    }
  }
}

case class JoinedEndpoint(
  endpoint: EndpointMetadata,
  original: Map[String, FieldMetadata],
  noise: Map[String, FieldMetadata])
{
  def differences = endpoint.differences
  def total = endpoint.total
  def fields: Map[String, JoinedField] = original map { case (path, field) =>
    path -> JoinedField(endpoint, field, noise.getOrElse(path, FieldMetadata.Empty))
  }
}

case class JoinedField(endpoint: EndpointMetadata, raw: FieldMetadata, noise: FieldMetadata) {
  // the percent difference out of the total # of requests
  def absoluteDifference = abs(raw.differences - noise.differences) / endpoint.total.toDouble * 100
  // the square error between this field's differences and the noisey counterpart's differences
  def relativeDifference = abs(raw.differences - noise.differences) / (raw.differences + noise.differences).toDouble * 100
} 
开发者ID:sachinmanchanda,项目名称:diffy_unicast,代码行数:57,代码来源:JoinedDifferences.scala

示例5: Edge

//设置package包名称以及导入依赖的类
package skeleton

import display.fmt
import util.{Vec, TwoD}
import scala.math.{pow, abs, sqrt}

class Edge(val a: Corner, val b: Corner) {
  def asVec(): Vec = {
    return b-a
  }
  def distance(p: TwoD): Double =  {
    abs((b.y - a.y)*p.x - (b.x - a.x)*p.y + b.x*a.y - b.y*a.x) / sqrt(pow(b.y - a.y, 2) + pow(b.x - a.x, 2))
  }
  override def toString(): String = {
    "Edge(" + a + ", " + b + ")" 
  }
  override def equals(o: Any): Boolean = o match {
    case e: Edge => e.a == a && e.b == b
    case _ => false
  }
  override def hashCode = {
    (a.x + a.y + b.x + b.y).hashCode()
  }
}


object Edge {
  val debug = new Edge(new Corner(0,0), new Corner(1,0))
  def bisector(ep: Edge, en: Edge): Vec = {
    (-ep.asVec().unit + en.asVec().unit).unit
  }
} 
开发者ID:nick-parker,项目名称:skel2d,代码行数:33,代码来源:Edge.scala

示例6: intToCandy

//设置package包名称以及导入依赖的类
package coalgebraz.example

import scala.math.abs

import scalaz._, Scalaz._

package object candycrush {

  type Game = (Board, Nat)

  // XXX: consider using `ScalaCheck` gens to avoid this crap
  def intToCandy(i: Int) = {
    val fl = abs(i) % 8 match {
      case 0 => Lemon
      case 1 => Orange
      case 2 => Mint
      case 3 => Coconut
      case 4 => Banana
      case 5 => Apple
      case 6 => Melon
      case 7 => Pear
    }
    Candy(i.toString, fl, (1, 1))
  }
} 
开发者ID:jeslg,项目名称:coalgebraz,代码行数:26,代码来源:package.scala

示例7: Exercise06

//设置package包名称以及导入依赖的类
package forimpatient.chapter18

import scala.math.abs


object Exercise06 extends App {
  println("Chapter 18 Exercise 06")

  private val array = (0 to (100, 2)).toArray
  private val value = 5
  println(getClosest(array, value))

  def getClosest(arr: Array[Int], value: Int): Int Either Int = arr.map((x: Int) => abs(value - x))
        .zipWithIndex.minBy(_._1) match {
    case (0, i) => new Left(i)
    case (_, i) => new Right(i)
  }
} 
开发者ID:Kiryna,项目名称:Scala-for-the-Impatient,代码行数:19,代码来源:Exercise06.scala

示例8: Exercise04

//设置package包名称以及导入依赖的类
package forimpatient.chapter11

import scala.math.abs


object Exercise04 extends App {
  println("Chapter 11 Exercise 04")

  val a = Money(10, 115)
  val b: Money = Money(2, 95)
  println(a)
  println(a + b)
  println(a - b)
  println(b - a)
  println(a < b)
  println(b < a)
  println(Money(1, 75) + Money(0, 50) == Money(2, 25))

  class Money(dollars: Int, cents: Int) {
    private val sign = if (dollars * 100 + cents < 0) -1 else +1
    val d: Int = abs(dollars * 100 + cents) / 100
    val c: Int = abs(dollars * 100 + cents) % 100

    override def toString = (if (sign < 0) "-$" else "$") + abs(d) + ","  + c

    def +(other: Money) = new Money(d + other.d, c + other.c)
    def -(other: Money): Money = new Money(d - other.d, c - other.c)
    def ==(other: Money) = (d * 100 + c) * sign == (other.d * 100 + other.c) * other.sign
    def <(other: Money) = (d * 100 + c) * sign < (other.d * 100 + other.c) * other.sign
    def >(other: Money) = !(this < other)
  }

  object Money{
    def apply(dollars: Int, cents: Int) = new Money(dollars, cents)
  }
} 
开发者ID:Kiryna,项目名称:Scala-for-the-Impatient,代码行数:37,代码来源:Exercise04.scala

示例9: EventActor

//设置package包名称以及导入依赖的类
package com.github.lzenczuk.akka.course.failover

import akka.actor.SupervisorStrategy.{Escalate, Restart, Resume}
import akka.actor.{Actor, ActorLogging, AllForOneStrategy, OneForOneStrategy, SupervisorStrategy}
import com.github.lzenczuk.akka.course.failover.MarketActor.MarketException

import scala.util.Random
import scala.concurrent.duration._


object EventActor {
  sealed trait EventCommand
  case class CreateMarket() extends EventCommand
}

class EventActor extends Actor with ActorLogging {

  import EventActor._
  import scala.math.abs

  override def receive = {
    case CreateMarket() =>
      log.info("creating market")
      val marketId: Long = abs(Random.nextLong())
      context.actorOf(MarketActor.props(marketId), s"Market_$marketId")
  }

  override val supervisorStrategy = AllForOneStrategy(maxNrOfRetries = 5, withinTimeRange = 1 second, false){
    case MarketException(msg) =>
      log.info(s"Market exception $msg. Restart.")
      Restart
    case t =>
      log.info("Not supported exception")
      super.supervisorStrategy.decider.applyOrElse(t, (_:Any) => Escalate)
  }
} 
开发者ID:lzenczuk,项目名称:akka-app-one,代码行数:37,代码来源:EventActor.scala


注:本文中的scala.math.abs类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。