Newbie Scala question about simple math array operations

前端 未结 4 1432
自闭症患者
自闭症患者 2021-02-14 22:17

Newbie Scala Question:

Say I want to do this [Java code] in Scala:

public static double[] abs(double[] r, double[] im) {
  double t[] = new double[r.leng         


        
4条回答
  •  没有蜡笔的小新
    2021-02-14 23:15

    After Edit:

    OK I have got running what I wanted to do. Will take two Lists of any type of number and return an Array of Doubles.

    def abs[A](r:List[A], im:List[A])(implicit numeric: Numeric[A]):Array[Double] = {
      var t = new Array[Double](r.length)
      for( i <- r.indices) {          
        t(i) = math.sqrt(numeric.toDouble(r(i))*numeric.toDouble(r(i))+numeric.toDouble(im(i))*numeric.toDouble(im(i)))
      }
      t
    }
    

提交回复
热议问题