• scala sparseVetor, SprseMatrix 实现



    def rand(seed:Int):Double={

    val rand=new Random(seed)
    rand.nextDouble()
    }



    def rand2(size:Int,seed:Int):SparseVector={
    val rand=new Random(seed)
    val ret_indices=Array.range(0,size)
    val ret_values=Array.tabulate[Double](size)(i=>rand.nextDouble()) //从函数创建数组
    Vectors.sparse(size, ret_indices, ret_values).asInstanceOf[SparseVector]
    }

    调用:val w=rand2(4,3)
    返回,SparseVector有3个要素函数,size,indices,values.
    取某一部分就调用那3个函数。比如:w.size=4,w.indices=(1,2,3,4)




    def rand3(numRows:Int,numCols:Int,seed:Int):SparseMatrix={
    val rand=new Random(seed)
    val ret_rowIndices=new ArrayBuffer[Int]()
    for (i <-0 to numCols-1) {
    ret_rowIndices ++= Array.range(0,numRows)
    }
    val ret_colPtrs=Array.range(0,numCols+1).map(x=>x * numRows)
    val ret_values=Array.tabulate[Double](numRows*numCols)(i=>rand.nextDouble())
    Matrices.sparse(numRows, numCols, ret_colPtrs.toArray,ret_rowIndices.toArray, ret_values.toArray).asInstanceOf[SparseMatrix]
    }




  • 相关阅读:
    Swift
    Swift
    第二章_session管理
    HDU-1387-Team Queue
    Install Orace 11g on Solaris 10 Sparc 64 bit
    Linux 多学习过程
    dispatch_once认识分析
    关于包围神经猫的想法实现
    嵌入在网站上Flash播放机(2)
    初学者应学会如何加快seo
  • 原文地址:https://www.cnblogs.com/zhangbojiangfeng/p/7462632.html
Copyright © 2020-2023  润新知