Skip to content

Instantly share code, notes, and snippets.

@dlwh
dlwh / gist:9042715
Last active August 29, 2015 13:56
Semiring Dense operations
implicit def opMulMatrix_DM_DV_Semiring[T:Semiring:ClassTag:DefaultArrayValue]:OpMulMatrix.Impl2[DenseMatrix[T], DenseVector[T], DenseVector[T]] = new OpMulMatrix.Impl2[DenseMatrix[T], DenseVector[T], DenseVector[T]] {
implicit val ring = implicitly[Semiring[T]]
override def apply(a: DenseMatrix[T], b: DenseVector[T]) = {
require(a.cols == b.length)
val res = DenseVector.zeros[T](a.rows)
var c = 0
while(c < a.cols) {
var r = 0
while (r < a.rows) {
val v = a(r, c)
@expand
@expand.exclude(Complex, OpMod)
@expand.exclude(BigInt, OpPow)
@expand.valify
implicit def dv_dv_Op[@expand.args(Int, Double, Float, Long, BigInt, Complex) T,
@expand.args(OpAdd, OpSub, OpMulScalar, OpDiv, OpSet, OpMod, OpPow) Op <: OpType]
(implicit @expand.sequence[Op]({_ + _}, {_ - _}, {_ * _}, {_ / _}, {(a,b) => b}, {_ % _}, {_ pow _})
op: BinaryOp[T, T, Op, T]):BinaryOp[DenseVector[T], DenseVector[T], Op, DenseVector[T]] = {
new BinaryOp[DenseVector[T], DenseVector[T], Op, DenseVector[T]] {
def apply(a: DenseVector[T], b: DenseVector[T]): DenseVector[T] = {
@dlwh
dlwh / hs_err_pid*.log
Created September 19, 2013 17:33
qr crasher error log
#
# A fatal error has been detected by the Java Runtime Environment:
#
# SIGSEGV (0xb) at pc=0x00007f387a5541d8, pid=23871, tid=139880111486720
#
# JRE version: 7.0_05-b06
# Java VM: Java HotSpot(TM) 64-Bit Server VM (23.1-b03 mixed mode linux-amd64 compressed oops)
# Problematic frame:
# V [libjvm.so+0x5741d8] jni_GetPrimitiveArrayCritical+0xc8
#
class DiagMatrix[Vec, S](val diagonalVector: Vec)(implicit evidence: Vec <:< Vector[S]) extends Matrix [S]

(The evidence implicit is because you can't write class DiagMatrix[Vec <: Vector[S], S] and expect it to work. Why? because it's apparently too hard to implement.) You would probably want type aliases like

type DenseDiagMatrix[S] = DiagMatrix[DenseVector[S], S]
@dlwh
dlwh / gist:3830758
Created October 4, 2012 00:23
enable gen-idea
mkdir -p ~/.sbt/plugins/ && cat "
resolvers += "sbt-idea-repo" at "http://mpeltonen.github.com/maven/"
addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.1.0")
" >> ~/.sbt/plugins/build.sbt
mkdir ~/bin/
echo 'java -Xms512M -Xmx1G -Xss1M -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=384M -jar `dirname $0`/sbt-launch.jar "$@"' > ~/bin/sbt
@dlwh
dlwh / foo.scala
Created September 9, 2012 06:48
Selecting enabling for operators in Breeze... Enable
trait Enable[T] {
def get: T
}
trait EnableLowPriority {
implicit val eager = new Enable[Eager]{def get = new Eager}
}
object Enable extends EnableLowPriority {
object Lazy {
@dlwh
dlwh / demo.scala
Created September 2, 2012 22:18
plot option
val f = Figure()
val plot = f.plot(0)
// Option A
plot.hold = true
plot.scatter(...)
plot.scatter(...)
f.visible = true
plot.hold = false
@dlwh
dlwh / t5632.scala
Created March 30, 2012 23:17
5632 patch
--- a/src/library/scala/collection/mutable/HashTable.scala
+++ b/src/library/scala/collection/mutable/HashTable.scala
@@ -366,7 +366,7 @@ private[collection] object HashTable {
private[collection] final def newThreshold(_loadFactor: Int, size: Int) = ((size.toLong * _loadFactor) / loadFactorDenum).toInt
- private[collection] final def sizeForThreshold(_loadFactor: Int, thr: Int) = thr * loadFactorDenum / _loadFactor
+ private[collection] final def sizeForThreshold(_loadFactor: Int, thr: Int) = ((thr.toLong * loadFactorDenum) / _loadFactor).toInt
private[collection] final def capacity(expectedSize: Int) = if (expectedSize == 0) 1 else powerOfTwo(expectedSize)
@dlwh
dlwh / HW2.scala
Created February 19, 2012 18:34
Assignment 2
import scalanlp.optimize.FirstOrderMinimizer.OptParams
import scalanlp.config.{GenerateHelp, CommandLineParser}
import collection.mutable.ArrayBuffer
import io.Source
import collection.immutable.BitSet
import scalanlp.util.{Iterators, Index}
import java.io._
import scalala.tensor.dense.DenseVector
import scalanlp.optimize.{CachedBatchDiffFunction, BatchDiffFunction, DiffFunction}
@dlwh
dlwh / gist:1756914
Created February 7, 2012 03:13
conll coref
nw/wsj/00/wsj_0020 0 0 The DT (TOP_(S_(NP_* - - - - * * (ARG0* * * * (11
nw/wsj/00/wsj_0020 0 1 U.S. NNP *) - - - - (GPE) * *) * * * 11)
nw/wsj/00/wsj_0020 0 2 , , * - - - - * * * * * * -
nw/wsj/00/wsj_0020 0 3 claiming VBG (S_(VP_* claim 01 2 - * (V*) (ARGM-ADV* * * * -
nw/wsj/00/wsj_0020 0 4 some DT (NP_(NP_* - - - - * (ARG1* * * * * -
nw/wsj/00/wsj_0020 0 5 su