In [1]:
import $ivy.`org.scalanlp::breeze:0.13.1`
import $ivy.`org.scalanlp::breeze-natives:0.13.1`
import $ivy.`com.chuusai::shapeless:2.3.2`


Out[1]:
import $ivy.$                            

import $ivy.$                                    

import $ivy.$                             

In [2]:
import breeze.linalg.{eig, eigSym, svd, DenseMatrix => BDM, DenseVector => BDV, Vector => BV}
import breeze.numerics.pow
import breeze.linalg._
import breeze.numerics._
// import shapeless._


Out[2]:
import breeze.linalg.{eig, eigSym, svd, DenseMatrix => BDM, DenseVector => BDV, Vector => BV}

import breeze.numerics.pow

import breeze.linalg._

import breeze.numerics._
// import shapeless._

In [3]:
val dm = DenseMatrix((1.0,2.0,3.0,4.0), (5.0,6.0,7.0,8.0), (9.0,10.0,11.0,12.0), (13.0,14.0,15.0,16.0))


Out[3]:
dm: DenseMatrix[Double] = 1.0   2.0   3.0   4.0   
5.0   6.0   7.0   8.0   
9.0   10.0  11.0  12.0  
13.0  14.0  15.0  16.0  

In [4]:
dm(::,2)


Out[4]:
res3: DenseVector[Double] = DenseVector(3.0, 7.0, 11.0, 15.0)

In [5]:
val indices = IndexedSeq(0,2)


Out[5]:
indices: IndexedSeq[Int] = Vector(0, 2)

In [6]:
dm(::,indices)


Out[6]:
res5: SliceMatrix[Int, Int, Double] = 1.0   3.0   
5.0   7.0   
9.0   11.0  
13.0  15.0  

In [7]:
val v = sqrt(DenseVector(2,3))
val m = dm(::, indices).toDenseMatrix


Out[7]:
v: DenseVector[Double] = DenseVector(1.4142135623730951, 1.7320508075688772)
m: DenseMatrix[Double] = 1.0   3.0   
5.0   7.0   
9.0   11.0  
13.0  15.0  

In [8]:
m.rows


Out[8]:
res7: Int = 4

In [8]:
// m(::, *) :* v

In [9]:
val dm = DenseMatrix( (2, 4, 6), (3, 6, 9) )
val dv = DenseVector(2,3)
dm(::, *)
dm(::, *) :* dv


Out[9]:
dm: DenseMatrix[Int] = 2  4  6  
3  6  9  
dv: DenseVector[Int] = DenseVector(2, 3)
res8_2: BroadcastedColumns[DenseMatrix[Int], DenseVector[Int]] = BroadcastedColumns(
  2  4  6  
3  6  9  
)
res8_3: DenseMatrix[Int] = 4  8   12  
9  18  27  

In [10]:
val adder = DenseVector(1, 2, 3, 4)

  val result = DenseMatrix.fill[Int](3, 4)(5).mapPairs({
    case ((row, col), value) => {
        print( row + ":" +col)
        println("--->" + value + ":" + adder(col))
      value + adder(col)
    }
  })


0:0--->5:1
1:0--->5:1
2:0--->5:1
0:1--->5:2
1:1--->5:2
2:1--->5:2
0:2--->5:3
1:2--->5:3
2:2--->5:3
0:3--->5:4
1:3--->5:4
2:3--->5:4
Out[10]:
adder: DenseVector[Int] = DenseVector(1, 2, 3, 4)
result: DenseMatrix[Int] = 6  7  8  9  
6  7  8  9  
6  7  8  9  

In [11]:
val result = m.mapPairs({
    case ((row, col), value) => {
      value * v(col)
    }
  })


Out[11]:
result: DenseMatrix[Double] = 1.4142135623730951  5.196152422706632   
7.0710678118654755  12.12435565298214   
12.727922061357857  19.05255888325765   
18.38477631085024   25.980762113533157  

In [12]:
val p = List(4,3,5,6,7)
val q = List(8,3,0,1,9)
val r = List(3,8,1,0,3)
val i = List(1,4,3,2,5)


Out[12]:
p: List[Int] = List(4, 3, 5, 6, 7)
q: List[Int] = List(8, 3, 0, 1, 9)
r: List[Int] = List(3, 8, 1, 0, 3)
i: List[Int] = List(1, 4, 3, 2, 5)

In [12]:
// (p,q,r,i).zipped Error

In [12]:
val list = 42 :: "foo" :: HNil


cmd12.sc:1: not found: value HNil
val list = 42 :: "foo" :: HNil
                          ^
Compilation Failed

In [12]:
list.head
list.tail.head
list(1)
list


cmd12.sc:1: not found: value list
val res12_0 = list.head
              ^cmd12.sc:2: not found: value list
val res12_1 = list.tail.head
              ^cmd12.sc:3: not found: value list
val res12_2 = list(1)
              ^cmd12.sc:4: not found: value list
val res12_3 = list
              ^
Compilation Failed

In [13]:
class BiMap[K, V]
implicit val stringToVec = new BiMap[String, BV[Double]]
implicit val stringToArrayDouble = new BiMap[String, Array[Double]]
implicit val stringToArrayString = new BiMap[String, Array[String]]
implicit val stringToString = new BiMap[String, String]


Out[13]:
defined class BiMap
stringToVec: BiMap[String, Vector[Double]] = $sess.cmd12Wrapper$Helper$BiMap@c70ae2e
stringToArrayDouble: BiMap[String, Array[Double]] = $sess.cmd12Wrapper$Helper$BiMap@3a37731e
stringToArrayString: BiMap[String, Array[String]] = $sess.cmd12Wrapper$Helper$BiMap@1043493e
stringToString: BiMap[String, String] = $sess.cmd12Wrapper$Helper$BiMap@5d234ce9

In [13]:
val hm1 = HMap[BiMap]("saliney" -> BV[Double](1.0,2.0,3.0), "term" -> BV[Double](3.5,6.8,7.8))


cmd13.sc:1: not found: value HMap
val hm1 = HMap[BiMap]("saliney" -> BV[Double](1.0,2.0,3.0), "term" -> BV[Double](3.5,6.8,7.8))
          ^
Compilation Failed

In [13]:
hm1.get("saliney")


cmd13.sc:1: not found: value hm1
val res13 = hm1.get("saliney")
            ^
Compilation Failed

In [14]:
val tvalues: Array[Double] = Array(1.866393526974307, 2.864048126935307, 4.032486069215076, 7.876169953355888, 4.875333799256043, 14.316322626848278)
val pvalues: Array[Double] = Array(0.064020056478447, 0.004808399479386827, 8.914865448939047E-5, 7.489564524121306E-13, 2.8363794106756046E-6, 0.0)


Out[14]:
tvalues: Array[Double] = Array(
  1.866393526974307,
  2.864048126935307,
  4.032486069215076,
  7.876169953355888,
  4.875333799256043,
  14.316322626848278
)
pvalues: Array[Double] = Array(
  0.064020056478447,
  0.004808399479386827,
  8.914865448939047E-5,
  7.489564524121306E-13,
  2.8363794106756046E-6,
  0.0
)

In [15]:
val zippedArray = Array(tvalues, pvalues).transpose


Out[15]:
zippedArray: Array[Array[Double]] = Array(
  Array(1.866393526974307, 0.064020056478447),
  Array(2.864048126935307, 0.004808399479386827),
  Array(4.032486069215076, 8.914865448939047E-5),
  Array(7.876169953355888, 7.489564524121306E-13),
  Array(4.875333799256043, 2.8363794106756046E-6),
  Array(14.316322626848278, 0.0)
)

In [16]:
val strngArray = Array.fill(tvalues.length)("Default")


Out[16]:
strngArray: Array[String] = Array("Default", "Default", "Default", "Default", "Default", "Default")

In [16]:
val zippedArray1 = Array(tvalues, pvalues, strngArray).transpose


cmd16.sc:1: No implicit view available from Array[_ >: String with Double] => Array[U].
val zippedArray1 = Array(tvalues, pvalues, strngArray).transpose
                                                       ^
Compilation Failed

In [17]:
val lambdaStep = 0.01
val lambdaSeq = BigDecimal("0.00") to BigDecimal("1.0") by BigDecimal(lambdaStep)


Out[17]:
lambdaStep: Double = 0.01
lambdaSeq: collection.immutable.NumericRange.Inclusive[BigDecimal] = NumericRange.Inclusive(
  0.00,
  0.01,
  0.02,
  0.03,
  0.04,
  0.05,
  0.06,
  0.07,
  0.08,
  0.09,
  0.10,
...

In [18]:
val m = BDM(
    (0.23, 0.20, 0.10, 0.92, 0.33, 0.42),
    (0.10, 0.43, 0.23, 0.15, 0.22, 0.12),
    (0.20, 0.13, 0.25, 0.85, 0.02, 0.32),
    (0.43, 0.65, 0.23, 0.45, 0.10, 0.33),
    (0.31, 0.87, 0.45, 0.63, 0.28, 0.16),
    (0.12, 0.84, 0.33, 0.45, 0.56, 0.83),
    (0.40, 0.22, 0.12, 0.87, 0.35, 0.78))


Out[18]:
m: DenseMatrix[Double] = 0.23  0.2   0.1   0.92  0.33  0.42  
0.1   0.43  0.23  0.15  0.22  0.12  
0.2   0.13  0.25  0.85  0.02  0.32  
0.43  0.65  0.23  0.45  0.1   0.33  
0.31  0.87  0.45  0.63  0.28  0.16  
0.12  0.84  0.33  0.45  0.56  0.83  
0.4   0.22  0.12  0.87  0.35  0.78  

In [24]:
// Let's work in a mix functional style and iterator working with columns
  // look at this example

  val a = m(::, 0) // get the firts column
    .toArray // pass to scala array for functional usage, you can use then to List
    .zipWithIndex // now you have and array like [(value0,0),(value1,1) ... (valuen,n)]
    .sortWith((x, y) => x._1 > y._1) // sort by bigger number
    .take(5) // get only 5 first numbers
    .map(x => x._2) // finally get the indexes


Out[24]:
a: Array[Int] = Array(3, 6, 4, 0, 2)

In [27]:
val a = Array(0.23,  0.2,   0.1,   0.92,  0.33,  0.42).zipWithIndex.sortWith((x, y) => x._1 > y._1)
    .take(5)
    .map(x => x._2)


Out[27]:
a: Array[Int] = Array(3, 5, 4, 0, 1)

In [1]:
1 to 6


Out[1]:
res0: Range.Inclusive = Range.Inclusive(1, 2, 3, 4, 5, 6)

In [29]:
//now we have to loop for each colum
  // prepare the matrix and get the Vector(indexes,Array[Int],Array[Int])

  val listsOfIndexes = for (i <- Range(0, m.rows))
    yield m(i, ::).inner.toArray
    .zipWithIndex
    .sortWith((x, y) => x._1 > y._1)
    .take(5)
    .map(x => x._2)

  //finally conver to a DenseMatrix

  val mIndex = BDM(listsOfIndexes.map(_.toArray): _*)

  println(mIndex)


3  5  4  0  1  
1  2  4  3  5  
3  5  2  0  1  
1  3  0  5  2  
1  3  2  0  4  
1  5  4  3  2  
3  5  0  4  1  
Out[29]:
listsOfIndexes: collection.immutable.IndexedSeq[Array[Int]] = Vector(
  Array(3, 5, 4, 0, 1),
  Array(1, 2, 4, 3, 5),
  Array(3, 5, 2, 0, 1),
  Array(1, 3, 0, 5, 2),
  Array(1, 3, 2, 0, 4),
  Array(1, 5, 4, 3, 2),
  Array(3, 5, 0, 4, 1)
)
mIndex: DenseMatrix[Int] = 3  5  4  0  1  
1  2  4  3  5  
3  5  2  0  1  
1  3  0  5  2  
1  3  2  0  4  
1  5  4  3  2  
3  5  0  4  1  

In [3]:
val a1 = Array(1,2,3)
val a2 = Array(1,2,3)
var curatedTermIndex: Array[Int] = Array()


Out[3]:
a1: Array[Int] = Array(1, 2, 3)
a2: Array[Int] = Array(1, 2, 3)
curatedTermIndex: Array[Int] = Array()

In [3]:
curatedTermIndex :: a1


cmd3.sc:1: value :: is not a member of Array[Int]
val res3 = curatedTermIndex :: a1
                            ^
Compilation Failed

In [7]:
curatedTermIndex = curatedTermIndex++:a1++:a2

In [8]:
curatedTermIndex


Out[8]:
res7: Array[Int] = Array(1, 2, 3, 1, 2, 3, 1, 2, 3)

In [2]:
import scala.reflect.runtime.universe._

def classAccessors[T: TypeTag]: List[MethodSymbol] = typeOf[T].members.collect {
  case m: MethodSymbol if m.isCaseAccessor => m
}.toList


cmd2.sc:3: reference to typeOf is ambiguous;
it is imported twice in the same scope by
import scala.reflect.runtime.universe._
and import _root_.jupyter.api.JupyterAPIHolder.value.{codeColors, exit, publish, tprintColors, show, typeOf}
def classAccessors[T: TypeTag]: List[MethodSymbol] = typeOf[T].members.collect {
                                                     ^
Compilation Failed

In [ ]: