├── .gitignore ├── LICENSE ├── README ├── build.sbt ├── project ├── build.properties └── plugins.sbt ├── sbt └── src ├── main ├── java │ └── scalala │ │ └── library │ │ └── random │ │ └── MersenneTwisterFast.java ├── resources │ └── scalala.scala └── scala │ └── scalala │ ├── ScalalaConsole.scala │ ├── collection │ └── sparse │ │ └── SparseArray.scala │ ├── generic │ ├── collection │ │ ├── CanAppendColumns.scala │ │ ├── CanBuildTensorForBinaryOp.scala │ │ ├── CanBuildTensorFrom.scala │ │ ├── CanCopy.scala │ │ ├── CanCreateZerosLike.scala │ │ ├── CanFilterValues.scala │ │ ├── CanGetActiveValues.scala │ │ ├── CanGetDouble.scala │ │ ├── CanGetValue.scala │ │ ├── CanJoin.scala │ │ ├── CanMapKeyValuePairs.scala │ │ ├── CanMapValues.scala │ │ ├── CanSliceMatrix.scala │ │ ├── CanSliceTensor.scala │ │ ├── CanSliceVector.scala │ │ ├── CanTranspose.scala │ │ ├── CanView.scala │ │ ├── CanViewAsTensor1.scala │ │ ├── CanViewAsTensor2.scala │ │ ├── CanViewAsVector.scala │ │ └── CanZipMapValues.scala │ └── math │ │ ├── CanAbs.scala │ │ ├── CanExp.scala │ │ ├── CanLog.scala │ │ ├── CanMean.scala │ │ ├── CanNorm.scala │ │ ├── CanSoftmax.scala │ │ ├── CanSqrt.scala │ │ └── CanVariance.scala │ ├── library │ ├── Library.scala │ ├── LinearAlgebra.scala │ ├── Numerics.scala │ ├── Plotting.scala │ ├── Random.scala │ ├── Statistics.scala │ ├── Storage.scala │ └── plotting │ │ ├── Dataset.scala │ │ ├── ExportGraphics.scala │ │ ├── Figure.scala │ │ ├── Figures.scala │ │ ├── HistogramBins.scala │ │ ├── PaintScale.scala │ │ ├── PaintScaleFactory.scala │ │ └── XYPlot.scala │ ├── operators │ ├── BinaryOp.scala │ ├── BinaryOpRegistry.scala │ ├── BinaryUpdateOp.scala │ ├── CanCast.scala │ ├── OpType.scala │ ├── Ops.scala │ ├── Shape.scala │ ├── TupleOps.scala │ ├── UnaryOp.scala │ ├── ValuesMonadic.scala │ ├── bundles │ │ └── VectorSpace.scala │ └── codegen │ │ ├── BinaryOpGenerator.scala │ │ ├── DynamicCompiler.scala │ │ └── GeneratedBinaryOp.scala │ ├── scalar │ ├── Complex.scala │ ├── RichScalar.scala │ ├── Scalar.scala │ ├── ScalarDecimal.scala │ └── package.scala │ └── tensor │ ├── CRSTensor2.scala │ ├── Counter.scala │ ├── Counter2.scala │ ├── DiagonalMatrix.scala │ ├── DomainFunction.scala │ ├── LiteralRow.scala │ ├── Matrix.scala │ ├── MatrixSingularException.scala │ ├── MatrixTranspose.scala │ ├── SelectAll.scala │ ├── Tensor.scala │ ├── Tensor1.scala │ ├── Tensor1Col.scala │ ├── Tensor1Proxy.scala │ ├── Tensor1Row.scala │ ├── Tensor1Slice.scala │ ├── Tensor2.scala │ ├── Tensor2Transpose.scala │ ├── TensorN.scala │ ├── TensorProxy.scala │ ├── TensorSlice.scala │ ├── TensorView.scala │ ├── Vector.scala │ ├── VectorCol.scala │ ├── VectorProxy.scala │ ├── VectorRow.scala │ ├── VectorSlice.scala │ ├── dense │ ├── ArrayArrayMatrix.scala │ ├── DenseArrayTensor.scala │ ├── DenseMatrix.scala │ └── DenseVector.scala │ ├── domain │ ├── CanBuildDomain2.scala │ ├── CanGetDomain.scala │ ├── Domain.scala │ ├── Domain1.scala │ ├── Domain2.scala │ ├── DomainN.scala │ ├── IndexDomain.scala │ ├── IterableDomain.scala │ ├── SetDomain.scala │ ├── TableDomain.scala │ └── UnionDomain.scala │ ├── generic │ ├── TensorBuilder.scala │ ├── TensorKeysMonadic.scala │ ├── TensorNonZeroKeysMonadic.scala │ ├── TensorNonZeroMonadic.scala │ ├── TensorNonZeroPairsMonadic.scala │ ├── TensorNonZeroTriplesMonadic.scala │ ├── TensorNonZeroValuesMonadic.scala │ ├── TensorPairsMonadic.scala │ ├── TensorTriplesMonadic.scala │ └── TensorValuesMonadic.scala │ ├── mutable │ ├── CRSTensor2.scala │ ├── Counter.scala │ ├── Counter2.scala │ ├── Matrix.scala │ ├── MatrixTranspose.scala │ ├── Tensor.scala │ ├── Tensor1.scala │ ├── Tensor1Col.scala │ ├── Tensor1Row.scala │ ├── Tensor1Slice.scala │ ├── Tensor2.scala │ ├── Tensor2Transpose.scala │ ├── TensorProxy.scala │ ├── TensorSlice.scala │ ├── Vector.scala │ ├── VectorCol.scala │ ├── VectorRow.scala │ └── VectorSlice.scala │ ├── package.scala │ └── sparse │ ├── SparseArrayTensor.scala │ └── SparseVector.scala └── test └── scala └── scalala ├── collection └── sparse │ └── SparseArrayTest.scala ├── generic ├── CanAssignIntoTest.scala └── collection │ ├── CanGetDoubleTest.scala │ ├── CanGetValueTest.scala │ └── CanMapValuesTest.scala ├── library ├── LibraryTest.scala ├── LinearAlgebraTest.scala ├── NumericsTest.scala ├── RandomTest.scala ├── StatisticsTest.scala └── StorageTest.scala ├── operators ├── ArrayTest.scala ├── SparseArrayTest.scala ├── TupleTest.scala └── bundles │ └── VectorSpaceTest.scala ├── scalar └── ComplexTest.scala └── tensor ├── CRSTensor2Test.scala ├── Counter2Test.scala ├── CounterTest.scala ├── DiagonalMatrixTest.scala ├── Tensor2Test.scala ├── TensorTest.scala ├── dense ├── DenseMatrixTest.scala ├── DenseVectorConstructorTest.scala └── DenseVectorTest.scala └── sparse └── SparseVectorTest.scala /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | .idea/ 3 | .idea_modules/ 4 | .manager/ 5 | workspace.xml 6 | bin/ 7 | docs/ 8 | target/ 9 | lib_managed/ 10 | src_managed/ 11 | project/boot/ 12 | project/plugins/project/ 13 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | Scalala is a high performance numeric linear algebra library for Scala, 2 | with rich Matlab-like operators on vectors and matrices; a library of 3 | numerical routines; support for plotting. 4 | 5 | This software is released under the LGPL. See LICENSE for details. 6 | 7 | (c) 2008- | Daniel Ramage | dramage | http://cs.stanford.edu/~dramage 8 | 9 | With contributions from: 10 | David Hall 11 | Jason Zaugg 12 | Alexander Lehmann 13 | Jonathan Merritt 14 | aerskine 15 | And others (email me if you've contributed code and aren't listed) 16 | 17 | Building upon and/or borrowing from solid libraries: 18 | JFreeChart http://www.jfree.org/jfreechart/ 19 | Netlib http://code.google.com/p/netlib-java/ 20 | MTJ http://code.google.com/p/matrix-toolkits-java/ 21 | iText http://www.itextpdf.com/ 22 | 23 | The source currently lives on github. You can download a copy: 24 | 25 | git clone https://github.com/scalala/Scalala.git 26 | 27 | Scalala is built with sbt, which is included in the repository or 28 | can be downloaded from https://github.com/harrah/xsbt/wiki. 29 | 30 | Run sbt (./sbt) and invoke one or more of the following targets: 31 | update -- Downloads scalala's dependencies 32 | compile -- Builds the library 33 | test -- Runs the unit tests 34 | doc -- Builds scaladoc for the public API 35 | proguard -- Builds a distributable jar 36 | gen-idea -- Generate an IntelliJ IDEA project 37 | 38 | For project maintainers, the project can be deployed with: 39 | publish 40 | 41 | To run an interactive console, either do so through sbt (console or 42 | console-quick) or run sbt's proguard target, and then: 43 | java -jar target/scala_2.8.1/scalala*.min.jar 44 | 45 | Documentation is available here: 46 | https://github.com/scalala/Scalala/wiki/Scalala 47 | 48 | And informal support is available here: 49 | http://groups.google.com/group/scalala 50 | 51 | -------------------------------------------------------------------------------- /build.sbt: -------------------------------------------------------------------------------- 1 | 2 | name := "scalala" 3 | 4 | version := "1.0.0.RC3-SNAPSHOT" 5 | 6 | organization := "org.scalala" 7 | 8 | scalaVersion := "2.9.2" 9 | 10 | crossScalaVersions := Seq("2.8.2", "2.9.1", "2.9.2") 11 | 12 | resolvers ++= Seq( 13 | "ScalaNLP Maven2" at "http://repo.scalanlp.org/repo", 14 | "Scala Tools Snapshots" at "http://scala-tools.org/repo-snapshots/" 15 | ) 16 | 17 | libraryDependencies ++= Seq( 18 | "com.googlecode.netlib-java" % "netlib-java" % "0.9.3", 19 | "jfree" % "jcommon" % "1.0.16", 20 | "jfree" % "jfreechart" % "1.0.13", 21 | "org.apache.xmlgraphics" % "xmlgraphics-commons" % "1.3.1", // for eps gen 22 | // "org.apache.xmlgraphics" % "batik-dom" % "1.7", // for svg gen 23 | // "org.apache.xmlgraphics" % "batik-svggen" % "1.7", // for svg gen 24 | "com.lowagie" % "itext" % "2.1.5" intransitive(), // for pdf gen 25 | "junit" % "junit" % "4.5" % "test" 26 | ) 27 | 28 | libraryDependencies <<= (scalaVersion, libraryDependencies) { (sv, deps) => 29 | deps :+ ("org.scala-lang" % "scala-compiler" % sv) 30 | } 31 | 32 | libraryDependencies <<= (scalaVersion, libraryDependencies) { (sv, deps) => 33 | val Scala210 = """2\.10\.0.*""".r 34 | sv match { 35 | case "2.9.1" | "2.9.2" | Scala210() => 36 | deps :+ ("jline" % "jline" % "0.9.94") // ("org.scala-lang" % "jline" % "2.9.1") 37 | case x if x.startsWith("2.8") => 38 | deps :+ ("jline" % "jline" % "0.9.94") 39 | case x => error("Unsupported Scala version " + x) 40 | } 41 | } 42 | 43 | libraryDependencies <<= (scalaVersion, libraryDependencies) { (sv, deps) => 44 | val Scala210 = """2\.10\.0.*""".r 45 | sv match { 46 | case Scala210() => deps 47 | case "2.9.1" | "2.9.2" => 48 | (deps :+ ("org.scalatest" % "scalatest" % "1.4.RC2" % "test") 49 | :+ ("org.scala-tools.testing" % "scalacheck_2.9.1" % "1.9" % "test")) 50 | case x if x.startsWith("2.8") => 51 | (deps :+ ("org.scalatest" % "scalatest" % "1.3" % "test") 52 | :+ ("org.scala-tools.testing" % "scalacheck_2.8.1" % "1.8" % "test")) 53 | case x => error("Unsupported Scala version " + x) 54 | } 55 | } 56 | 57 | publishTo <<= (version) { version: String => 58 | val nexus = "http://nexus.scala-tools.org/content/repositories/" 59 | if (version.trim.endsWith("SNAPSHOT")) Some("snapshots" at nexus + "snapshots/") 60 | else Some("releases" at nexus + "releases/") 61 | } 62 | 63 | credentials += Credentials(Path.userHome / ".ivy2" / ".credentials") 64 | 65 | javacOptions ++= Seq("-source", "1.5", "-target", "1.5") 66 | 67 | scalacOptions ++= Seq("-no-specialization","-deprecation","-target:jvm-1.5") 68 | 69 | initialCommands := scala.io.Source.fromFile("src/main/resources/scalala.scala").getLines.mkString("\n") 70 | 71 | mainClass in (Compile,packageBin) := Some("scalala.ScalalaConsole") 72 | 73 | javaOptions += "-Xmx2g" 74 | 75 | seq(ProguardPlugin.proguardSettings :_*) 76 | 77 | proguardOptions ++= Seq ( 78 | "-keep class scalala.** { *; }", 79 | "-keep class org.jfree.** { *; }", 80 | keepMain("scalala.ScalalaConsole$"), 81 | keepMain("scala.tools.nsc.MainGenericRunner"), 82 | "-dontoptimize", 83 | "-dontobfuscate", 84 | keepLimitedSerializability, 85 | keepAllScala, 86 | "-keep class ch.epfl.** { *; }", 87 | "-keep interface scala.ScalaObject" 88 | ) 89 | 90 | // seq(com.github.retronym.SbtOneJar.oneJarSettings: _*) 91 | 92 | -------------------------------------------------------------------------------- /project/build.properties: -------------------------------------------------------------------------------- 1 | sbt.version=0.11.3 -------------------------------------------------------------------------------- /project/plugins.sbt: -------------------------------------------------------------------------------- 1 | 2 | resolvers += "Proguard plugin repo" at "http://siasia.github.com/maven2" 3 | 4 | libraryDependencies <+= sbtVersion(v => "com.github.siasia" %% "xsbt-proguard-plugin" % ("0.11.2-0.1.1")) 5 | 6 | addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.0.0") 7 | -------------------------------------------------------------------------------- /src/main/resources/scalala.scala: -------------------------------------------------------------------------------- 1 | import scalala.scalar._; 2 | import scalala.tensor.::; 3 | import scalala.tensor.mutable._; 4 | import scalala.tensor.dense._; 5 | import scalala.tensor.sparse._; 6 | import scalala.library.Library._; 7 | import scalala.library.LinearAlgebra._; 8 | import scalala.library.Statistics._; 9 | import scalala.library.Plotting._; 10 | import scalala.operators.Implicits._; 11 | 12 | -------------------------------------------------------------------------------- /src/main/scala/scalala/generic/collection/CanAppendColumns.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package generic; 22 | package collection; 23 | 24 | import scalala.scalar._; 25 | import scalala.tensor.domain.DomainException; 26 | import scalala.collection.sparse.{SparseArray,DefaultArrayValue}; 27 | 28 | /** 29 | * Construction delegate for appending columns with A | B. 30 | * 31 | * @author dramage 32 | */ 33 | trait CanAppendColumns[A,-B,+That] extends Function2[A,B,That]; 34 | 35 | -------------------------------------------------------------------------------- /src/main/scala/scalala/generic/collection/CanBuildTensorForBinaryOp.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package generic; 22 | package collection; 23 | 24 | import scalala.tensor.domain.CanGetDomain; 25 | import scalala.tensor.generic.TensorBuilder; 26 | import scalala.tensor.Tensor; 27 | 28 | /** 29 | * Trait for building a new tensor from either A or B depending on the 30 | * given Op. Default to building tensor for left operand. 31 | * 32 | * @author dramage 33 | */ 34 | trait CanBuildTensorForBinaryOp[-A, -B, D, K, V, Op, +To] { 35 | def apply(a : A, b : B, domain : D) : TensorBuilder[K,V,To]; 36 | } 37 | 38 | object CanBuildTensorForBinaryOp { 39 | implicit def canBuildTensorLeft[A,B,D,K,V,Op,To] 40 | (implicit va : A=>Tensor[_,_], bf : CanBuildTensorFrom[A,D,K,V,To]) 41 | : CanBuildTensorForBinaryOp[A,B,D,K,V,Op,To] 42 | = new CanBuildTensorForBinaryOp[A,B,D,K,V,Op,To] { 43 | def apply(a : A, b : B, domain : D) = bf.apply(a, domain); 44 | } 45 | } 46 | 47 | -------------------------------------------------------------------------------- /src/main/scala/scalala/generic/collection/CanCopy.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package generic; 22 | package collection; 23 | 24 | 25 | import scalala.scalar.Scalar; 26 | 27 | /** 28 | * Marker for being able to copy a collection 29 | * 30 | * @author dlwh 31 | */ 32 | trait CanCopy[T] { 33 | // Should not inherit from T=>T because those get used by the compiler. 34 | def apply(t: T):T 35 | } 36 | 37 | object CanCopy { 38 | 39 | class OpArray[@specialized V:ClassManifest:Scalar] 40 | extends CanCreateZerosLike[Array[V],Array[V]] { 41 | override def apply(from : Array[V]) = { 42 | Array.fill(from.length)(implicitly[Scalar[V]].zero); 43 | } 44 | } 45 | 46 | class OpMapValues[From,A](implicit op : CanCopy[A], map : CanMapValues[From,A,A,From]) extends CanCopy[From] { 47 | def apply(v : From) = map.map(v, op.apply(_)); 48 | } 49 | 50 | implicit def opMapValues[From,A](implicit map : CanMapValues[From,A,A,From], op : CanCopy[A]) 51 | : CanCopy[From] = new OpMapValues[From,A]()(op, map); 52 | 53 | implicit def OpArrayAny[V:ClassManifest:Scalar] : OpArray[V] = 54 | new OpArray[V]; 55 | 56 | implicit object OpArrayI extends OpArray[Int]; 57 | implicit object OpArrayS extends OpArray[Short]; 58 | implicit object OpArrayL extends OpArray[Long]; 59 | implicit object OpArrayF extends OpArray[Float]; 60 | implicit object OpArrayD extends OpArray[Double]; 61 | 62 | implicit def canCopyScalar[V:Scalar]:CanCopy[V] = new CanCopy[V] { 63 | def apply(v1: V) = v1; 64 | } 65 | } 66 | 67 | -------------------------------------------------------------------------------- /src/main/scala/scalala/generic/collection/CanCreateZerosLike.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package generic; 22 | package collection; 23 | 24 | import scalala.collection.sparse.{SparseArray,DefaultArrayValue}; 25 | import scalala.scalar.Scalar; 26 | 27 | /** 28 | * Marker for being able to create a collection of the same shape as 29 | * the given input but with zero values everywhere. 30 | * 31 | * @author dramage 32 | */ 33 | trait CanCreateZerosLike[-From, +To] { 34 | // Should not inherit from Form=>To because the compiler will try to use it to coerce types. 35 | def apply(from: From):To 36 | }; 37 | 38 | object CanCreateZerosLike { 39 | 40 | class OpArray[@specialized V:ClassManifest:Scalar] 41 | extends CanCreateZerosLike[Array[V],Array[V]] { 42 | override def apply(from : Array[V]) = { 43 | Array.fill(from.length)(implicitly[Scalar[V]].zero); 44 | } 45 | } 46 | 47 | class OpMapValues[From,A,To](implicit op : Scalar[A], map : CanMapValues[From,A,A,To]) extends CanCreateZerosLike[From,To] { 48 | def apply(v : From) = map.map(v, _ => op.zero); 49 | } 50 | 51 | implicit def opMapValues[From,A,To](implicit map : CanMapValues[From,A,A,To], op : Scalar[A]) 52 | : CanCreateZerosLike[From,To] = new OpMapValues[From,A,To]()(op, map); 53 | 54 | implicit def OpArrayAny[V:ClassManifest:Scalar] : OpArray[V] = 55 | new OpArray[V]; 56 | 57 | implicit object OpArrayI extends OpArray[Int]; 58 | implicit object OpArrayS extends OpArray[Short]; 59 | implicit object OpArrayL extends OpArray[Long]; 60 | implicit object OpArrayF extends OpArray[Float]; 61 | implicit object OpArrayD extends OpArray[Double]; 62 | } 63 | 64 | -------------------------------------------------------------------------------- /src/main/scala/scalala/generic/collection/CanGetActiveValues.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package generic; 22 | package collection; 23 | 24 | import scalala.collection.sparse.SparseArray; 25 | 26 | /** 27 | * Marker for being able to get the domain (keys) of a collection. 28 | * 29 | * @author dramage 30 | */ 31 | trait CanGetActiveValues[-Coll, @specialized(Int,Long) K, @specialized(Int,Long,Float,Double) V] { 32 | def apply(coll : Coll) : Iterator[(K,V)]; 33 | } 34 | 35 | object CanGetActiveValues { 36 | class OpArray[V] extends CanGetActiveValues[Array[V],Int,V] { 37 | override def apply(coll : Array[V]) = 38 | Iterator.range(0, coll.length).map(i => (i, coll(i))); 39 | } 40 | 41 | implicit def opArray[V] = new OpArray[V]; 42 | 43 | implicit object OpArrayI extends OpArray[Int]; 44 | implicit object OpArrayS extends OpArray[Short]; 45 | implicit object OpArrayL extends OpArray[Long]; 46 | implicit object OpArrayF extends OpArray[Float]; 47 | implicit object OpArrayD extends OpArray[Double]; 48 | 49 | class OpSparseArray[V] extends CanGetActiveValues[SparseArray[V],Int,V] { 50 | override def apply(coll : SparseArray[V]) = 51 | coll.activeIterator; 52 | } 53 | 54 | implicit def opSparseArray[V] = new OpSparseArray[V]; 55 | 56 | implicit object OpSparseArrayI extends OpSparseArray[Int]; 57 | implicit object OpSparseArrayS extends OpSparseArray[Short]; 58 | implicit object OpSparseArrayL extends OpSparseArray[Long]; 59 | implicit object OpSparseArrayF extends OpSparseArray[Float]; 60 | implicit object OpSparseArrayD extends OpSparseArray[Double]; 61 | } 62 | -------------------------------------------------------------------------------- /src/main/scala/scalala/generic/collection/CanGetDouble.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package generic; 22 | package collection; 23 | 24 | /** 25 | * Marker for being able to get the value of a map at a key as a double. 26 | * 27 | * @author dramage 28 | */ 29 | trait CanGetDouble[-Coll, @specialized(Int,Long) -K] { 30 | def apply(coll : Coll, key : K) : Double; 31 | } 32 | 33 | object CanGetDouble { 34 | type Op[Coll,K] = CanGetDouble[Coll,K]; 35 | 36 | implicit object OpArrayI extends Op[Array[Int],Int] 37 | { override def apply(coll : Array[Int], key : Int) = coll(key); } 38 | 39 | implicit object OpArrayS extends Op[Array[Short],Int] 40 | { override def apply(coll : Array[Short], key : Int) = coll(key); } 41 | 42 | implicit object OpArrayL extends Op[Array[Long],Int] 43 | { override def apply(coll : Array[Long], key : Int) = coll(key); } 44 | 45 | implicit object OpArrayF extends Op[Array[Float],Int] 46 | { override def apply(coll : Array[Float], key : Int) = coll(key); } 47 | 48 | implicit object OpArrayD extends Op[Array[Double],Int] 49 | { override def apply(coll : Array[Double], key : Int) = coll(key); } 50 | 51 | implicit def opIndexedSeq[@specialized V](implicit cv : V => Double) = 52 | new OpIndexedSeq[V]; 53 | 54 | class OpIndexedSeq[@specialized V](implicit cv : V => Double) extends CanGetDouble[IndexedSeq[V], Int] { 55 | def apply(coll : IndexedSeq[V], key : Int) = coll(key); 56 | } 57 | 58 | implicit object OpIndexedSeqI extends OpIndexedSeq[Int]; 59 | implicit object OpIndexedSeqC extends OpIndexedSeq[Char]; 60 | implicit object OpIndexedSeqS extends OpIndexedSeq[Short]; 61 | implicit object OpIndexedSeqL extends OpIndexedSeq[Long]; 62 | implicit object OpIndexedSeqF extends OpIndexedSeq[Float]; 63 | implicit object OpIndexedSeqD extends OpIndexedSeq[Double]; 64 | 65 | implicit def opMap[K,V](implicit cv : V => Double) = 66 | new OpMap[K,V]; 67 | 68 | class OpMap[K,V](implicit cv : V => Double) extends CanGetDouble[scala.collection.Map[K,V], K] { 69 | def apply(coll : scala.collection.Map[K,V], key : K) = coll(key); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/main/scala/scalala/generic/collection/CanGetValue.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package generic; 22 | package collection; 23 | 24 | /** 25 | * Marker for being able to get the value of a map at a key. 26 | * 27 | * @author dramage 28 | */ 29 | trait CanGetValue[-Coll, @specialized(Int,Long) -K, @specialized(Int,Long,Float,Double) +V] { 30 | def apply(coll : Coll, key : K) : V; 31 | } 32 | 33 | object CanGetValue { 34 | implicit def opArray[@specialized V] = 35 | new OpArray[V]; 36 | 37 | class OpArray[@specialized V] extends CanGetValue[Array[V], Int, V] { 38 | def apply(coll : Array[V], key : Int) = coll(key); 39 | } 40 | 41 | implicit object OpArrayI extends OpArray[Int]; 42 | implicit object OpArrayC extends OpArray[Char]; 43 | implicit object OpArrayS extends OpArray[Short]; 44 | implicit object OpArrayL extends OpArray[Long]; 45 | implicit object OpArrayF extends OpArray[Float]; 46 | implicit object OpArrayD extends OpArray[Double]; 47 | implicit object OpArrayB extends OpArray[Boolean]; 48 | 49 | implicit def opIndexedSeq[@specialized V] = 50 | new OpIndexedSeq[V]; 51 | 52 | class OpIndexedSeq[@specialized V] extends CanGetValue[IndexedSeq[V], Int, V] { 53 | def apply(coll : IndexedSeq[V], key : Int) = coll(key); 54 | } 55 | 56 | implicit object OpIndexedSeqI extends OpIndexedSeq[Int]; 57 | implicit object OpIndexedSeqC extends OpIndexedSeq[Char]; 58 | implicit object OpIndexedSeqS extends OpIndexedSeq[Short]; 59 | implicit object OpIndexedSeqL extends OpIndexedSeq[Long]; 60 | implicit object OpIndexedSeqF extends OpIndexedSeq[Float]; 61 | implicit object OpIndexedSeqD extends OpIndexedSeq[Double]; 62 | implicit object OpIndexedSeqB extends OpIndexedSeq[Boolean]; 63 | 64 | implicit def opMap[K,V] = 65 | new OpMap[K,V]; 66 | 67 | class OpMap[K,V] extends CanGetValue[scala.collection.Map[K,V], K, V] { 68 | def apply(coll : scala.collection.Map[K,V], key : K) = coll(key); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/main/scala/scalala/generic/collection/CanJoin.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package generic; 22 | package collection; 23 | 24 | /** 25 | * Trait for applying a function to two tensors across their underlying 26 | * shared keys -- either all keys or non-zero keys. The tensors must 27 | * have the same domain. 28 | * 29 | * @author dramage 30 | */ 31 | trait CanJoin[-A, -B, @specialized(Int,Long) K, 32 | @specialized(Int,Long,Float,Double) V1, 33 | @specialized(Int,Long,Float,Double) V2] { 34 | /** Joins on all keys in the domain. */ 35 | def joinAll[RV](a : A, b : B, fn : (K,V1,V2)=>RV) : Unit; 36 | 37 | /** Joins when both a and b are non-zero. */ 38 | def joinBothNonZero[RV](a : A, b : B, fn : (K,V1,V2)=>RV) : Unit; 39 | 40 | /** Joins when either a or b is non-zero. */ 41 | def joinEitherNonZero[RV](a : A, b : B, fn : (K,V1,V2)=>RV) : Unit; 42 | } 43 | 44 | -------------------------------------------------------------------------------- /src/main/scala/scalala/generic/collection/CanMapKeyValuePairs.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package generic; 22 | package collection; 23 | 24 | /** 25 | * Marker for being able to map the keys and values in a value collection 26 | * to new values. 27 | * 28 | * @author dramage 29 | */ 30 | trait CanMapKeyValuePairs[-From, +K, +A, -B, +To] { 31 | /** Maps all key-value pairs from the given collection. */ 32 | def map(from : From, fn : ((K,A) => B)) : To; 33 | 34 | /** Maps all non-zero key-value pairs from the given collection. */ 35 | def mapNonZero(from : From, fn : ((K,A) => B)) : To; 36 | } 37 | -------------------------------------------------------------------------------- /src/main/scala/scalala/generic/collection/CanSliceMatrix.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package generic; 22 | package collection; 23 | 24 | /** 25 | * Builder trait for a slicing a view of a matrix. 26 | * 27 | * @author dramage 28 | */ 29 | trait CanSliceMatrix[-From, A1, A2, +To] { 30 | def apply(from : From, keys1 : Seq[A1], keys2 : Seq[A2]) : To; 31 | } 32 | 33 | 34 | /** 35 | * Builder trait for a slicing a row from a matrix. 36 | * 37 | * @author dramage 38 | */ 39 | trait CanSliceRow[-From, K, +To] { 40 | def apply(from : From, row : K) : To; 41 | } 42 | 43 | 44 | /** 45 | * Builder trait for a slicing a column from a matrix. 46 | * 47 | * @author dramage 48 | */ 49 | trait CanSliceCol[-From, K, +To] { 50 | def apply(from : From, col : K) : To; 51 | } 52 | -------------------------------------------------------------------------------- /src/main/scala/scalala/generic/collection/CanSliceTensor.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package generic; 22 | package collection; 23 | 24 | /** 25 | * Supports sliced view of a Tensor. 26 | * 27 | * @author dramage 28 | */ 29 | trait CanSliceTensor[-From, A1, A2, +To] { 30 | def apply(from : From, keymap : scala.collection.Map[A2,A1]) : To; 31 | } 32 | -------------------------------------------------------------------------------- /src/main/scala/scalala/generic/collection/CanSliceVector.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package generic; 22 | package collection; 23 | 24 | /** 25 | * Capability trait for slicing a Vector from something. 26 | * 27 | * @author dramage 28 | */ 29 | trait CanSliceVector[-From, A, +To] { 30 | def apply(from : From, keys : Seq[A]) : To; 31 | } 32 | -------------------------------------------------------------------------------- /src/main/scala/scalala/generic/collection/CanView.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package generic; 22 | package collection; 23 | 24 | /** 25 | * Trait for constructing a lazy view of a given Tensor. 26 | * 27 | * @author dramage 28 | */ 29 | trait CanView[-From, +To] { 30 | def apply(from : From) : To; 31 | } 32 | -------------------------------------------------------------------------------- /src/main/scala/scalala/generic/collection/CanViewAsTensor1.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package generic; 22 | package collection; 23 | 24 | import scalala.collection.sparse.SparseArray; 25 | 26 | import scalala.scalar.Scalar; 27 | import scalala.tensor.Tensor1; 28 | import scalala.tensor.dense.DenseVectorCol; 29 | import scalala.tensor.sparse.SparseVectorCol; 30 | 31 | /** 32 | * View something as a Tensor1. 33 | * 34 | * @author dramage 35 | */ 36 | trait CanViewAsTensor1[-From,K,V] { 37 | def apply(from : From) : Tensor1[K,V]; 38 | } 39 | 40 | object CanViewAsTensor1 { 41 | // 42 | // View arrays 43 | // 44 | 45 | class ArrayTensor1[V:ClassManifest:Scalar] 46 | extends CanViewAsTensor1[Array[V],Int,V] { 47 | def apply(from : Array[V]) = new DenseVectorCol[V](from); 48 | } 49 | 50 | implicit def mkArrayTensor1[V:ClassManifest:Scalar] = 51 | new ArrayTensor1[V](); 52 | 53 | implicit object ArrayI extends ArrayTensor1[Int]; 54 | implicit object ArrayS extends ArrayTensor1[Short]; 55 | implicit object ArrayL extends ArrayTensor1[Long]; 56 | implicit object ArrayF extends ArrayTensor1[Float]; 57 | implicit object ArrayD extends ArrayTensor1[Double]; 58 | implicit object ArrayB extends ArrayTensor1[Boolean]; 59 | 60 | // 61 | // View sparse arrays 62 | // 63 | 64 | class SparseArrayTensor1[V:ClassManifest:Scalar] 65 | extends CanViewAsTensor1[SparseArray[V],Int,V] { 66 | def apply(from : SparseArray[V]) = new SparseVectorCol[V](from); 67 | } 68 | 69 | implicit def mkSparseArrayTensor1[V:ClassManifest:Scalar] = 70 | new SparseArrayTensor1[V](); 71 | 72 | implicit object SparseArrayI extends SparseArrayTensor1[Int]; 73 | implicit object SparseArrayS extends SparseArrayTensor1[Short]; 74 | implicit object SparseArrayL extends SparseArrayTensor1[Long]; 75 | implicit object SparseArrayF extends SparseArrayTensor1[Float]; 76 | implicit object SparseArrayD extends SparseArrayTensor1[Double]; 77 | implicit object SparseArrayB extends SparseArrayTensor1[Boolean]; 78 | 79 | // 80 | // View pre-constructed Tensor1 instances 81 | // 82 | 83 | class Tensor1Tensor1[K,V:Scalar] 84 | extends CanViewAsTensor1[Tensor1[K,V],K,V] { 85 | def apply(from : Tensor1[K,V]) = from; 86 | } 87 | 88 | implicit def mkTensor1Tensor1[K,V:Scalar] = 89 | new Tensor1Tensor1[K,V](); 90 | } 91 | -------------------------------------------------------------------------------- /src/main/scala/scalala/generic/collection/CanViewAsTensor2.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package generic; 22 | package collection; 23 | 24 | import scalala.scalar.Scalar; 25 | import scalala.tensor.Tensor2; 26 | import scalala.tensor.dense.ArrayArrayMatrix; 27 | 28 | /** 29 | * View something as a Tensor1. 30 | * 31 | * @author dramage 32 | */ 33 | trait CanViewAsTensor2[-From,K1,K2,V] { 34 | def apply(from : From) : Tensor2[K1,K2,V]; 35 | } 36 | 37 | object CanViewAsTensor2 { 38 | // 39 | // View arrays 40 | // 41 | 42 | class ArrayArrayTensor2[V:ClassManifest:Scalar] 43 | extends CanViewAsTensor2[Array[Array[V]],Int,Int,V] { 44 | def apply(from : Array[Array[V]]) = new ArrayArrayMatrix[V](from); 45 | } 46 | 47 | implicit def mkArrayArrayTensor2[V:ClassManifest:Scalar] = 48 | new ArrayArrayTensor2[V](); 49 | 50 | implicit object ArrayArrayI extends ArrayArrayTensor2[Int]; 51 | implicit object ArrayArrayS extends ArrayArrayTensor2[Short]; 52 | implicit object ArrayArrayL extends ArrayArrayTensor2[Long]; 53 | implicit object ArrayArrayF extends ArrayArrayTensor2[Float]; 54 | implicit object ArrayArrayD extends ArrayArrayTensor2[Double]; 55 | implicit object ArrayArrayB extends ArrayArrayTensor2[Boolean]; 56 | 57 | // 58 | // View pre-constructed Tensor2 instances 59 | // 60 | 61 | class Tensor2Tensor2[K1,K2,V:Scalar] 62 | extends CanViewAsTensor2[Tensor2[K1,K2,V],K1,K2,V] { 63 | def apply(from : Tensor2[K1,K2,V]) = from; 64 | } 65 | 66 | implicit def mkTensor2Tensor2[K1,K2,V:Scalar] = 67 | new Tensor2Tensor2[K1,K2,V](); 68 | } 69 | -------------------------------------------------------------------------------- /src/main/scala/scalala/generic/collection/CanViewAsVector.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package generic; 22 | package collection; 23 | 24 | import scalala.collection.sparse.SparseArray; 25 | 26 | import scalala.scalar.Scalar; 27 | import scalala.tensor.Vector; 28 | import scalala.tensor.dense.DenseVectorCol; 29 | import scalala.tensor.sparse.SparseVectorCol; 30 | 31 | /** 32 | * View something as a Vector. 33 | * 34 | * @author dramage 35 | */ 36 | trait CanViewAsVector[-From,V] { 37 | def apply(from : From) : Vector[V]; 38 | } 39 | 40 | object CanViewAsVector { 41 | // 42 | // View arrays 43 | // 44 | 45 | class ArrayVector[V:ClassManifest:Scalar] 46 | extends CanViewAsVector[Array[V],V] { 47 | def apply(from : Array[V]) = new DenseVectorCol[V](from); 48 | } 49 | 50 | implicit def mkArrayVector[V:ClassManifest:Scalar] = 51 | new ArrayVector[V](); 52 | 53 | implicit object ArrayI extends ArrayVector[Int]; 54 | implicit object ArrayS extends ArrayVector[Short]; 55 | implicit object ArrayL extends ArrayVector[Long]; 56 | implicit object ArrayF extends ArrayVector[Float]; 57 | implicit object ArrayD extends ArrayVector[Double]; 58 | implicit object ArrayB extends ArrayVector[Boolean]; 59 | 60 | // 61 | // View sparse arrays 62 | // 63 | 64 | class SparseArrayVector[V:ClassManifest:Scalar] 65 | extends CanViewAsVector[SparseArray[V],V] { 66 | def apply(from : SparseArray[V]) = new SparseVectorCol[V](from); 67 | } 68 | 69 | implicit def mkSparseArrayVector[V:ClassManifest:Scalar] = 70 | new SparseArrayVector[V](); 71 | 72 | implicit object SparseArrayI extends SparseArrayVector[Int]; 73 | implicit object SparseArrayS extends SparseArrayVector[Short]; 74 | implicit object SparseArrayL extends SparseArrayVector[Long]; 75 | implicit object SparseArrayF extends SparseArrayVector[Float]; 76 | implicit object SparseArrayD extends SparseArrayVector[Double]; 77 | implicit object SparseArrayB extends SparseArrayVector[Boolean]; 78 | 79 | // 80 | // View pre-constructed Vector instances 81 | // 82 | 83 | class VectorVector[V:Scalar] 84 | extends CanViewAsVector[Vector[V],V] { 85 | def apply(from : Vector[V]) = from; 86 | } 87 | 88 | implicit def mkVectorVector[V:Scalar] = 89 | new VectorVector[V](); 90 | } 91 | 92 | -------------------------------------------------------------------------------- /src/main/scala/scalala/generic/math/CanAbs.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | 21 | package scalala; 22 | package generic; 23 | package math; 24 | 25 | import scalala.operators.UnaryOp 26 | import collection.CanMapValues 27 | 28 | 29 | /** 30 | * Operator type for abs(A). 31 | * 32 | * @author dramage 33 | */ 34 | trait OpAbs extends operators.OpType; 35 | object OpAbs extends OpAbs; 36 | 37 | 38 | /** 39 | * Construction delegate for abs(A). 40 | * 41 | * @author dlwh 42 | */ 43 | trait CanAbs[-A,+RV] extends UnaryOp[A,OpAbs,RV] { 44 | def opType = OpAbs; 45 | def apply(v: A):RV; 46 | } 47 | 48 | object CanAbs { 49 | implicit object OpI extends CanAbs[Int,Double] { 50 | def apply(v : Int) = scala.math.abs(v); 51 | } 52 | 53 | implicit object OpL extends CanAbs[Long,Double] { 54 | def apply(v : Long) = scala.math.abs(v); 55 | } 56 | 57 | implicit object OpF extends CanAbs[Float,Double] { 58 | def apply(v : Float) = scala.math.abs(v); 59 | } 60 | 61 | implicit object OpD extends CanAbs[Double,Double] { 62 | def apply(v : Double) = scala.math.abs(v); 63 | } 64 | 65 | class OpMapValues[From,A,B,To](implicit op : CanAbs[A,B], map : CanMapValues[From,A,B,To]) extends CanAbs[From,To] { 66 | def apply(v : From) = map.map(v, op.apply(_)); 67 | } 68 | 69 | implicit def opMapValues[From,A,B,To](implicit map : CanMapValues[From,A,B,To], op : CanAbs[A,B]) 70 | : CanAbs[From,To] = new OpMapValues[From,A,B,To]()(op, map); 71 | 72 | implicit object OpArrayI extends OpMapValues[Array[Int],Int,Double,Array[Double]]()(OpI,CanMapValues.OpArrayID); 73 | implicit object OpArrayL extends OpMapValues[Array[Long],Long,Double,Array[Double]]()(OpL,CanMapValues.OpArrayLD); 74 | implicit object OpArrayF extends OpMapValues[Array[Float],Float,Double,Array[Double]]()(OpF,CanMapValues.OpArrayFD); 75 | implicit object OpArrayD extends OpMapValues[Array[Double],Double,Double,Array[Double]]()(OpD,CanMapValues.OpArrayDD); 76 | } -------------------------------------------------------------------------------- /src/main/scala/scalala/generic/math/CanLog.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | 21 | package scalala; 22 | package generic; 23 | package math; 24 | 25 | import collection.CanMapValues; 26 | import scalala.operators.UnaryOp 27 | import tensor.{Matrix, Vector} 28 | ; 29 | 30 | /** 31 | * Operator type for log(A). 32 | * 33 | * @author dramage 34 | */ 35 | trait OpLog extends operators.OpType; 36 | object OpLog extends OpLog; 37 | 38 | /** 39 | * Constructiond delegate for log(A). 40 | * 41 | * @author dramage 42 | */ 43 | trait CanLog[-A,+RV] extends UnaryOp[A,OpLog,RV] { 44 | def opType = OpLog; 45 | } 46 | 47 | object CanLog { 48 | implicit object OpI extends CanLog[Int,Double] { 49 | def apply(v : Int) = scala.math.log(v); 50 | } 51 | 52 | implicit object OpL extends CanLog[Long,Double] { 53 | def apply(v : Long) = scala.math.log(v); 54 | } 55 | 56 | implicit object OpF extends CanLog[Float,Double] { 57 | def apply(v : Float) = scala.math.log(v); 58 | } 59 | 60 | implicit object OpD extends CanLog[Double,Double] { 61 | def apply(v : Double) = scala.math.log(v); 62 | } 63 | 64 | class OpMapValues[From,A,B,To](implicit op : CanLog[A,B], map : CanMapValues[From,A,B,To]) extends CanLog[From,To] { 65 | def apply(v : From) = map.map(v, op.apply(_)); 66 | } 67 | 68 | implicit def opMapValues[From,A,B,To](implicit map : CanMapValues[From,A,B,To], op : CanLog[A,B]) 69 | : CanLog[From,To] = new OpMapValues[From,A,B,To]()(op, map); 70 | 71 | implicit object OpArrayI extends OpMapValues[Array[Int],Int,Double,Array[Double]]()(OpI,CanMapValues.OpArrayID); 72 | implicit object OpArrayL extends OpMapValues[Array[Long],Long,Double,Array[Double]]()(OpL,CanMapValues.OpArrayLD); 73 | implicit object OpArrayF extends OpMapValues[Array[Float],Float,Double,Array[Double]]()(OpF,CanMapValues.OpArrayFD); 74 | implicit object OpArrayD extends OpMapValues[Array[Double],Double,Double,Array[Double]]()(OpD,CanMapValues.OpArrayDD); 75 | 76 | implicit object OpVectorI extends OpMapValues[Vector[Int],Int,Double,Vector[Double]]() 77 | implicit object OpVectorL extends OpMapValues[Vector[Long],Long,Double,Vector[Double]]() 78 | implicit object OpVectorF extends OpMapValues[Vector[Float],Float,Double,Vector[Double]]() 79 | implicit object OpVectorD extends OpMapValues[Vector[Double],Double,Double,Vector[Double]]() 80 | 81 | implicit object OpMatrixI extends OpMapValues[Matrix[Int],Int,Double,Matrix[Double]]() 82 | implicit object OpMatrixL extends OpMapValues[Matrix[Long],Long,Double,Matrix[Double]]() 83 | implicit object OpMatrixF extends OpMapValues[Matrix[Float],Float,Double,Matrix[Double]]() 84 | implicit object OpMatrixD extends OpMapValues[Matrix[Double],Double,Double,Matrix[Double]]() 85 | } 86 | 87 | -------------------------------------------------------------------------------- /src/main/scala/scalala/generic/math/CanNorm.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | 21 | package scalala; 22 | package generic; 23 | package math; 24 | 25 | import collection.CanViewAsTensor1; 26 | 27 | /** 28 | * Construction delegate for getting the norm of a value of type From. 29 | * 30 | * @author dramage 31 | */ 32 | trait CanNorm[-From] extends ((From,Double)=>Double); 33 | 34 | object CanNorm { 35 | implicit def mkTensor1Norm[T](implicit tt : CanViewAsTensor1[T,_,_]) 36 | : CanNorm[T] = new CanNorm[T] { 37 | def apply(t : T, n : Double) : Double = 38 | tt(t).norm(n); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/scala/scalala/generic/math/CanSoftmax.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | 21 | package scalala; 22 | package generic; 23 | package math; 24 | 25 | import collection.CanViewAsTensor1; 26 | 27 | /** 28 | * Construction delegate for getting the softmax of a type. 29 | * 30 | * @author dlwh 31 | */ 32 | trait CanSoftmax[-From] { 33 | // shouldn't be apply, because function1's are bad implicits 34 | def softmax(x: From):Double 35 | } 36 | 37 | object CanSoftmax { 38 | implicit def mkTensor1Softmax[K,T](implicit tt : CanViewAsTensor1[T,K,Double]) 39 | : CanSoftmax[T] = new CanSoftmax[T] { 40 | def softmax(t : T) : Double = { 41 | val value = tt(t); 42 | val max = value.max; 43 | val part = value.valuesIterator.foldLeft(0.0)((acc,v) => acc + scala.math.exp(v - max)); 44 | max + scala.math.log(part); 45 | } 46 | } 47 | 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/main/scala/scalala/generic/math/CanSqrt.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | 21 | package scalala; 22 | package generic; 23 | package math; 24 | 25 | import collection.CanMapValues; 26 | 27 | import scalala.operators.{UnaryOp,OpType}; 28 | 29 | trait OpSqrt extends OpType; 30 | object OpSqrt extends OpSqrt; 31 | 32 | /** 33 | * Constructiond delegate for sqrt(A). 34 | * 35 | * @author dramage 36 | */ 37 | trait CanSqrt[-A,+RV] extends UnaryOp[A,OpSqrt,RV] { 38 | def opType = OpSqrt; 39 | } 40 | 41 | object CanSqrt { 42 | implicit object OpI extends CanSqrt[Int,Double] { 43 | def apply(v : Int) = scala.math.sqrt(v); 44 | } 45 | 46 | implicit object OpL extends CanSqrt[Long,Double] { 47 | def apply(v : Long) = scala.math.sqrt(v); 48 | } 49 | 50 | implicit object OpF extends CanSqrt[Float,Double] { 51 | def apply(v : Float) = scala.math.sqrt(v); 52 | } 53 | 54 | implicit object OpD extends CanSqrt[Double,Double] { 55 | def apply(v : Double) = scala.math.sqrt(v); 56 | } 57 | 58 | class OpMapValues[From,A,B,To](implicit op : CanSqrt[A,B], map : CanMapValues[From,A,B,To]) extends CanSqrt[From,To] { 59 | def apply(v : From) = map.map(v, op.apply(_)); 60 | } 61 | 62 | implicit def opMapValues[From,A,B,To](implicit map : CanMapValues[From,A,B,To], op : CanSqrt[A,B]) 63 | : CanSqrt[From,To] = new OpMapValues[From,A,B,To]()(op, map); 64 | 65 | implicit object OpArrayI extends OpMapValues[Array[Int],Int,Double,Array[Double]]()(OpI,CanMapValues.OpArrayID); 66 | implicit object OpArrayL extends OpMapValues[Array[Long],Long,Double,Array[Double]]()(OpL,CanMapValues.OpArrayLD); 67 | implicit object OpArrayF extends OpMapValues[Array[Float],Float,Double,Array[Double]]()(OpF,CanMapValues.OpArrayFD); 68 | implicit object OpArrayD extends OpMapValues[Array[Double],Double,Double,Array[Double]]()(OpD,CanMapValues.OpArrayDD); 69 | } 70 | 71 | -------------------------------------------------------------------------------- /src/main/scala/scalala/generic/math/CanVariance.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | 21 | package scalala; 22 | package generic; 23 | package math; 24 | 25 | import scalala.generic.collection.CanCreateZerosLike; 26 | import scalala.operators._; 27 | import scalala.tensor.Tensor; 28 | import scalala.scalar.ScalarDecimal; 29 | 30 | /** 31 | * Construction delegate for variance(From). 32 | * 33 | * @author dramage 34 | */ 35 | trait CanVariance[-From,+To] extends (From=>To); 36 | 37 | object CanVariance { 38 | /** 39 | * Numerically stable one-pass sample variance computation. 40 | * 41 | * From http://www.cs.berkeley.edu/~mhoemmen/cs194/Tutorials/variance.pdf 42 | */ 43 | implicit def TraversableOnceVarianceScalar[S](implicit view : S=>Double) 44 | : CanVariance[TraversableOnce[S],Double] 45 | = new CanVariance[TraversableOnce[S],Double] { 46 | def apply(values : TraversableOnce[S]) = { 47 | var m = 0.0; 48 | var q = 0.0; 49 | var k = 0; 50 | for (x <- values) { 51 | k += 1; 52 | val xMm = x - m; 53 | val xMmDk = xMm / k; 54 | m = m + xMmDk; 55 | q = q + (k - 1) * xMm * xMmDk 56 | } 57 | q / (k - 1); 58 | } 59 | } 60 | 61 | implicit def ArrayVarianceScalar[S](implicit view : S=>Double) 62 | : CanVariance[Array[S],Double] 63 | = new CanVariance[Array[S],Double] { 64 | def apply(values : Array[S]) = { 65 | var m = 0.0; 66 | var q = 0.0; 67 | var k = 0; 68 | while (k < values.length) { 69 | val x = values(k); 70 | k += 1; 71 | val xMm = x - m; 72 | val xMmDk = xMm / k; 73 | m = m + xMmDk; 74 | q = q + (k - 1) * xMm * xMmDk 75 | } 76 | q / (k - 1); 77 | } 78 | } 79 | 80 | implicit def TensorVariance[T,V,D](implicit view : T=>Tensor[_,V], 81 | sd : ScalarDecimal[V,D], 82 | sub : BinaryOp[V,D,OpSub,D], 83 | add : BinaryOp[D,D,OpAdd,D], 84 | div : BinaryOp[D,Int,OpDiv,D], 85 | mul1 : BinaryOp[D,D,OpMul,D], 86 | mul2 : BinaryOp[D,Int,OpMul,D]) 87 | : CanVariance[T,D] 88 | = new CanVariance[T,D] { 89 | override def apply(tensor : T) = { 90 | var m = sd.decimal.zero; 91 | var q = sd.decimal.zero; 92 | var k = 0; 93 | // TODO: this could be more efficient by using foreachNonZeroValue 94 | tensor.foreachValue(x => { 95 | k += 1; 96 | val xMm = sub(x, m); 97 | val xMmDk = div(xMm, k); 98 | m = add(m, xMmDk); 99 | q = add(q, mul2(mul1(xMm,xMmDk), k-1)); 100 | }); 101 | div(q, k-1); 102 | } 103 | } 104 | } 105 | 106 | -------------------------------------------------------------------------------- /src/main/scala/scalala/library/Random.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | 21 | package scalala; 22 | package library; 23 | 24 | import random.MersenneTwisterFast 25 | 26 | /** 27 | *

Random number generation. This class uses the MersenneTwisterFast 28 | * implementation by Sean Luke http://www.cs.gmu.edu/~sean/research/ 29 | * as its underlying random number generator. The Mersenne Twister 30 | * is very fast with an excellent pseudo-random distribution, but it 31 | * is not cryptographically strong.

32 | * 33 | *

Each random number generating method accepts a 34 | * MersenneTwisterFast implementation as an implicit argument, defaulting 35 | * to the Random.mt instance. That instance's seed is set 36 | * with the long value held in the scalala.library.random.seed System property. 37 | * If the property is not defined, the current time in milliseconds is used 38 | * as the random seed.

39 | * 40 | *

The MersenneTwisterFast implementation is not thread-safe, so all 41 | * accessors to an instance (mt) wrap calls in a mt.synchronized 42 | * block. Therefore, calling a vector constructor is substantially faster 43 | * than calling rand() many times.

44 | * 45 | *

The seed can be set with the system property during Java invocation, e.g. by 46 | * -Dscalala.library.random.seed=1l

47 | * 48 | * @author dramage,afwlehmann 49 | */ 50 | object Random { 51 | /** 52 | * Returns a pseudo-random number from [0,1). 53 | */ 54 | def rand()(implicit mt: MersenneTwisterFast): Double = mt.synchronized { 55 | mt.nextDouble() 56 | } 57 | 58 | /** 59 | * Returns a pseudo-random gaussian variable. 60 | * */ 61 | def randn()(implicit mt: MersenneTwisterFast) = mt.synchronized { 62 | mt.nextGaussian() 63 | } 64 | 65 | /** 66 | * Returns a pseudo-random integer between 0 (incl.) and `max` (excl.). 67 | */ 68 | def randi(max: Int)(implicit mt: MersenneTwisterFast) = mt.synchronized { 69 | mt.nextInt(max) 70 | } 71 | 72 | lazy val seed : Long = { 73 | val prop = System.getProperty("scalala.library.random.seed"); 74 | if (prop != null) prop.toLong else System.currentTimeMillis; 75 | } 76 | 77 | implicit val mt : MersenneTwisterFast = 78 | new MersenneTwisterFast(seed); 79 | } 80 | -------------------------------------------------------------------------------- /src/main/scala/scalala/library/plotting/Figures.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package library; 22 | package plotting; 23 | 24 | import scala.collection.mutable.ArrayBuffer; 25 | 26 | /** Class that holds a collection of Figure instances */ 27 | class Figures { 28 | private val figures = ArrayBuffer[Option[Figure]](); 29 | 30 | /** Returns the number of the given figure */ 31 | def number(figure : Figure) : Int = figures.indexOf(Some(figure)); 32 | 33 | /** Returns the current figure. Defaults to 1, but allows 0. */ 34 | var figure_ : Int = 1; 35 | def figure : Figure = figures(figure_).get; 36 | def figure_=(number : Int) : Unit = { 37 | while (figures.length <= number) { 38 | figures += None; 39 | } 40 | if (figures(number) == None) { 41 | figures(number) = Some(new Figure(this)); 42 | } 43 | figure_ = number; 44 | } 45 | 46 | /** Returns the current figure's current plot */ 47 | def plot : XYPlot = figure.plot; 48 | 49 | // Set the current figure to figure 1 50 | figure = 1; 51 | } 52 | 53 | object Figures { 54 | lazy val global = new Figures(); 55 | } 56 | 57 | -------------------------------------------------------------------------------- /src/main/scala/scalala/library/plotting/HistogramBins.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package library; 22 | package plotting; 23 | 24 | import scalala.generic.collection.CanViewAsTensor1; 25 | 26 | /** 27 | * Bins for a histogram. These can be implicitly constructed from: 28 | *
29 |  *   x : HistogramBins = 10  // 10 dynamically determined histogram bins
30 |  *   x : HistogramBins = Array(1.0,2.0,3.2) // five buckets wit the given splits
31 |  *   x : HistogramBins = (0,100,10) // ten bins evenly dividing 0 to 100.
32 |  * 
33 | * 34 | * @author dramage 35 | */ 36 | sealed trait HistogramBins; 37 | 38 | /** 39 | * Set of histograms for binning data using the given splits. 40 | * 41 | * @author dramage 42 | */ 43 | case class StaticHistogramBins(splits : Array[Double]) 44 | extends HistogramBins { 45 | /** Returns the bin for the given value, between 0 and splits.length inclusive. */ 46 | def bin(value : Double) = { 47 | var i = 0; 48 | while (i < splits.length && value > splits(i)) { 49 | i += 1; 50 | } 51 | i; 52 | } 53 | } 54 | 55 | /** 56 | * Create a set of StaticHistogramBins from a number and an (eventual) 57 | * lower and upper bound. 58 | * 59 | * @author dramage 60 | */ 61 | case class DynamicHistogramBins(number : Int = 10) 62 | extends HistogramBins { 63 | def apply(lower : Double, upper : Double) = 64 | StaticHistogramBins(Array.tabulate(number-1)(i => lower + ((i + 1.0) / (number)) * (upper - lower))); 65 | } 66 | 67 | /** 68 | * Static constructors for HistogramBins. 69 | * 70 | * @author dramage 71 | */ 72 | object HistogramBins { 73 | implicit def fromNumber(number : Int) : HistogramBins = 74 | DynamicHistogramBins(number); 75 | 76 | // implicit def fromSplits[S,K,V](splits : S)(implicit tt : CanViewAsTensor1[S,K,V], v : V=>Double) : HistogramBins = 77 | // StaticHistogramBins(t.domain(splits).map(d => v(t.get(splits, d))).toArray); 78 | 79 | implicit def fromRange(minMaxCount : (Double,Double,Int)) : HistogramBins = 80 | DynamicHistogramBins(minMaxCount._3)(minMaxCount._1, minMaxCount._2); 81 | } 82 | -------------------------------------------------------------------------------- /src/main/scala/scalala/library/plotting/PaintScaleFactory.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package library; 22 | package plotting; 23 | 24 | import java.awt.{Color,Paint,TexturePaint}; 25 | import java.awt.geom.Rectangle2D; 26 | import java.awt.image.BufferedImage; 27 | 28 | /** 29 | * Constructs a PaintScale for the given type T by examining a set of its 30 | * values. 31 | * 32 | * @author dramage 33 | */ 34 | trait PaintScaleFactory[T] extends (Traversable[T] => PaintScale[T]); 35 | 36 | /** 37 | * Creates a GradientPaintScale from the min and max of a set of data points. 38 | * bound are supplied. 39 | * 40 | * @author dramage 41 | */ 42 | case class GradientPaintScaleFactory[T] 43 | (gradient : Array[Color] = PaintScale.WhiteToBlack) 44 | (implicit view : T=>Double) 45 | extends PaintScaleFactory[T] { 46 | override def apply(items : Traversable[T]) : PaintScale[T] = { 47 | var min = items.head; 48 | var max = items.head; 49 | for (item <- items) { 50 | if (!view(item).isNaN) { 51 | if (item < min) min = item; 52 | if (item > max) max = item; 53 | } 54 | } 55 | GradientPaintScale(min, max, gradient); 56 | } 57 | } 58 | 59 | /** 60 | * Creates a categorical paint scale using the Category20 palette borrowed from 61 | * Protovis. http://vis.stanford.edu/protovis/docs/color.html 62 | * 63 | * Beware that category colors can be reused if the number of distinct items 64 | * is greater than 20. 65 | * 66 | * @author dramage 67 | */ 68 | case class CategoricalPaintScaleFactory[T]() extends PaintScaleFactory[T] { 69 | override def apply(items : Traversable[T]) : PaintScale[T] = { 70 | val distinct = items.toList.distinct; 71 | CategoricalPaintScale[T](Map() ++ (distinct zip Stream.continually(PaintScale.Category20.values.toList).flatten)); 72 | } 73 | } 74 | 75 | object PaintScaleFactory { 76 | 77 | /** 78 | * Ignores incoming data, instead returns the provided PaintScale when 79 | * queried as a PaintScaleFactory. 80 | */ 81 | implicit def singletonFactoryForPaintScale[S,T](paintScale : S) 82 | (implicit view : S=>PaintScale[T]) 83 | : PaintScaleFactory[T] = new PaintScaleFactory[T] { 84 | def apply(items : Traversable[T]) = view(paintScale); 85 | } 86 | } 87 | 88 | -------------------------------------------------------------------------------- /src/main/scala/scalala/operators/BinaryOpRegistry.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | 21 | package scalala; 22 | package operators; 23 | 24 | import scala.annotation.implicitNotFound; 25 | import scala.collection.generic.CanBuildFrom; 26 | 27 | import scala.collection.mutable.HashMap; 28 | 29 | object BinaryOpRegistry { 30 | val resultTypes = 31 | HashMap[(Manifest[_],Manifest[_],Manifest[_]),Manifest[_]](); 32 | 33 | val ops = 34 | HashMap[(Manifest[_],Manifest[_],Manifest[_]),BinaryOp[_,_,_,_]](); 35 | 36 | def register[A,B,O<:OpType,To](implicit gen : BinaryOp[A,B,O,To], a : Manifest[A], b : Manifest[B], op : Manifest[O], to : Manifest[To]) = { 37 | resultTypes((a,b,op)) = to 38 | ops((a,b,op)) = gen 39 | } 40 | 41 | def getResultType[A,B,O<:OpType](implicit a : Manifest[A], b : Manifest[B], op : Manifest[O]) = 42 | resultTypes((a,b,op)); 43 | 44 | def getBinaryOp[A,B,O<:OpType,To](implicit a : Manifest[A], b : Manifest[B], op : Manifest[O], to : Manifest[To]) : BinaryOp[A,B,O,To] = { 45 | require(getResultType[A,B,O] == to, "Mismatched result type: "+to); 46 | ops((a,b,op)).asInstanceOf[BinaryOp[A,B,O,To]] 47 | } 48 | } 49 | 50 | -------------------------------------------------------------------------------- /src/main/scala/scalala/operators/CanCast.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | 21 | package scalala; 22 | package operators; 23 | 24 | import scala.annotation.implicitNotFound; 25 | 26 | /** 27 | * Cast a value from type A to B. 28 | * 29 | * @author dramage 30 | */ 31 | @implicitNotFound(msg="Could not cast ${A} to ${B}") 32 | trait CanCast[-A,+B] extends (A => B); 33 | 34 | object CanCast { 35 | implicit object CanCastII extends CanCast[Int,Int] 36 | { override def apply(v : Int) = v; } 37 | 38 | implicit object CanCastIL extends CanCast[Int,Long] 39 | { override def apply(v : Int) = v; } 40 | 41 | implicit object CanCastIF extends CanCast[Int,Float] 42 | { override def apply(v : Int) = v; } 43 | 44 | implicit object CanCastID extends CanCast[Int,Double] 45 | { override def apply(v : Int) = v; } 46 | 47 | implicit object CanCastLL extends CanCast[Long,Long] 48 | { override def apply(v : Long) = v; } 49 | 50 | implicit object CanCastLD extends CanCast[Long,Double] 51 | { override def apply(v : Long) = v; } 52 | 53 | implicit object CanCastFF extends CanCast[Float,Float] 54 | { override def apply(v : Float) = v; } 55 | 56 | implicit object CanCastFD extends CanCast[Float,Double] 57 | { override def apply(v : Float) = v; } 58 | 59 | implicit object CanCastDD extends CanCast[Double,Double] 60 | { override def apply(v : Double) = v; } 61 | 62 | object CanCastIdentityAnyRef extends CanCast[AnyRef,AnyRef] 63 | { override def apply(v : AnyRef) = v; } 64 | 65 | implicit def CanCastIdentity[V] : CanCast[V,V] = 66 | CanCastIdentity.asInstanceOf[CanCast[V,V]]; 67 | } 68 | 69 | -------------------------------------------------------------------------------- /src/main/scala/scalala/operators/Shape.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | 21 | package scalala; 22 | package operators; 23 | 24 | import scala.annotation.implicitNotFound; 25 | import scalala.scalar.Scalar; 26 | 27 | /** 28 | * Marker trait describing the shape of a value V as having 29 | * shape signature S. This class is never instantiated and 30 | * is used only as an implicit to ensure compatibility between 31 | * shapes of arguments when operating on collections. 32 | * 33 | * @author dramage 34 | */ 35 | @implicitNotFound(msg="Could not recover shape of ${V}") 36 | trait Shape[-V,S]; 37 | 38 | trait LowPriorityShapeImplicits { 39 | /** Arbitrary objects have shape Unit. */ 40 | @inline implicit def any[V] : Shape[V,Unit] = null; 41 | } 42 | 43 | object Shape extends LowPriorityShapeImplicits { 44 | def apply[V,S](v : V)(implicit shape : Shape[V,S]) = shape; 45 | 46 | /** Tuples have shape (Unit,Unit, ...). */ 47 | @inline implicit def tuple2[V1,V2] 48 | : Shape[(V1,V2),(Unit,Unit)] = null; 49 | 50 | /** Tuples have shape (Unit,Unit, ...). */ 51 | @inline implicit def tuple3[V1,V2,V3] 52 | : Shape[(V1,V2,V3),(Unit,Unit,Unit)] = null; 53 | 54 | /** Tuples have shape (Unit,Unit, ...). */ 55 | @inline implicit def tuple4[V1,V2,V3,V4] 56 | : Shape[(V1,V2,V3,V4),(Unit,Unit,Unit,Unit)] = null; 57 | 58 | // TODO: add more tuples 59 | 60 | /** Maps have shape (KeyShape=>ValueShape). */ 61 | @inline implicit def map[K,KeyShape,V,ValueShape] 62 | (implicit kShape : Shape[K,KeyShape], vShape : Shape[V,ValueShape]) 63 | : Shape[scala.collection.Map[K,V],(KeyShape=>ValueShape)] = null; 64 | 65 | /** Seqs have shape (Unit=>ValueShape). */ 66 | @inline implicit def seq[V,ValueShape](implicit vShape : Shape[V,ValueShape]) 67 | : Shape[scala.collection.Seq[V],(Unit=>ValueShape)] = null; 68 | 69 | /** Arrays have shape (Unit=>ValueShape). */ 70 | @inline implicit def array[V,ValueShape](implicit vShape : Shape[V,ValueShape]) 71 | : Shape[Array[V],(Unit=>ValueShape)] = null; 72 | } 73 | 74 | /** 75 | * Marker trait that says that A and B have statically 76 | * compatible shape. This trait is never instantiated and is 77 | * used only as an implicit to ensure that types A and B have 78 | * compatible shapes for performing operations. 79 | * 80 | * @author dramage 81 | */ 82 | @implicitNotFound(msg="Types ${A} and ${B} have incompatible shape") 83 | sealed trait CompatibleShape[A,B] 84 | 85 | object CompatibleShape { 86 | @inline implicit def apply[A,SA,B,SB] 87 | (implicit sa : Shape[A,SA], sb : Shape[B,SB], eq : =:=[SA,SB]) 88 | : CompatibleShape[A,B] = null; 89 | } 90 | 91 | -------------------------------------------------------------------------------- /src/main/scala/scalala/operators/ValuesMonadic.scala: -------------------------------------------------------------------------------- 1 | package scalala.operators 2 | 3 | import scalala.generic.collection.CanMapValues 4 | 5 | /** 6 | * 7 | * Represents objects that can have their values mapped. 8 | * @author dlwh 9 | */ 10 | trait ValuesMonadic[+This,V] { 11 | def repr: This; 12 | def map[TT>:This,O,That](fn : V => O) 13 | (implicit bf : CanMapValues[TT, V, O, That]) : That = 14 | bf.map(repr,fn); 15 | 16 | } 17 | 18 | trait HasValuesMonadic[+This,V] { 19 | def values: ValuesMonadic[This,V]; 20 | } 21 | 22 | object HasValuesMonadic { 23 | implicit def arrayHasValues[V](arr: Array[V]): HasValuesMonadic[Array[V], V] = new HasValuesMonadic[Array[V],V] { 24 | def values = new ValuesMonadic[Array[V],V] { 25 | def repr = arr; 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/scala/scalala/scalar/RichScalar.scala: -------------------------------------------------------------------------------- 1 | package scalala.scalar 2 | 3 | class RichScalar(value: Double) { 4 | def +(c: Complex): Complex = Complex(value, 0) + c; 5 | def -(c: Complex): Complex = Complex(value, 0) - c; 6 | def *(c: Complex): Complex = Complex(value, 0) * c; 7 | def /(c: Complex): Complex = Complex(value, 0) / c; 8 | } 9 | -------------------------------------------------------------------------------- /src/main/scala/scalala/scalar/ScalarDecimal.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package scalar; 22 | 23 | import scala.annotation.implicitNotFound; 24 | import scalala.collection.sparse.DefaultArrayValue; 25 | 26 | /** 27 | * Get a decimal value for the given scalar type. 28 | * 29 | * @author dramage 30 | */ 31 | @implicitNotFound(msg="No decimal defined for ${SV}") 32 | trait ScalarDecimal[SV,DV] { 33 | def decimal : Scalar[DV]; 34 | } 35 | 36 | object ScalarDecimal { 37 | class DefaultScalarDecimal[SV:Scalar,DV:Scalar] extends ScalarDecimal[SV,DV] { 38 | def decimal = implicitly[Scalar[DV]]; 39 | } 40 | 41 | implicit object ScalarDecimalBD extends DefaultScalarDecimal[Boolean,Double]; 42 | implicit object ScalarDecimalID extends DefaultScalarDecimal[Int,Double]; 43 | implicit object ScalarDecimalSD extends DefaultScalarDecimal[Short,Double]; 44 | implicit object ScalarDecimalLD extends DefaultScalarDecimal[Long,Double]; 45 | implicit object ScalarDecimalFD extends DefaultScalarDecimal[Float,Double]; 46 | implicit object ScalarDecimalDD extends DefaultScalarDecimal[Double,Double]; 47 | } 48 | 49 | -------------------------------------------------------------------------------- /src/main/scala/scalala/scalar/package.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | 22 | /** 23 | * Defines i. 24 | * 25 | * @author dramage 26 | */ 27 | package object scalar { 28 | val i = Complex.i; 29 | 30 | implicit def richInt(value : Int) = 31 | new RichScalar(value); 32 | 33 | implicit def richLong(value : Long) = 34 | new RichScalar(value); 35 | 36 | implicit def richFloat(value : Float) = 37 | new RichScalar(value); 38 | 39 | implicit def richDouble(value : Double) = 40 | new RichScalar(value); 41 | } 42 | -------------------------------------------------------------------------------- /src/main/scala/scalala/tensor/DiagonalMatrix.scala: -------------------------------------------------------------------------------- 1 | package scalala 2 | package tensor 3 | 4 | import domain.TableDomain 5 | import scalar.Scalar 6 | 7 | /** 8 | * A matrix with values only on its diagonal, as defined by the 9 | * given vector. 10 | * 11 | * @author dlwh, dramage 12 | */ 13 | class DiagonalMatrix[Vec,V](val diag : Vec) 14 | (implicit val scalar: Scalar[V], view: Vec<:U)) : Boolean = { 29 | diag.foreachNonZeroKey((i : Int) => fn((i,i))); 30 | false; 31 | } 32 | 33 | override def foreachNonZeroValue[U](fn : (V=>U)) : Boolean = { 34 | diag.foreachNonZeroValue(fn); 35 | false; 36 | } 37 | 38 | override def foreachNonZeroPair[U](fn : ((Int,Int),V)=>U) : Boolean = { 39 | diag.foreachNonZeroPair((i : Int, v : V) => fn((i,i),v)); 40 | false; 41 | } 42 | 43 | override def foreachNonZeroTriple[U](fn : (Int,Int,V)=>U) : Boolean = 44 | diag.foreachNonZeroPair((i : Int, v : V) => fn(i,i,v)); 45 | 46 | override def keysIteratorNonZero = 47 | diag.keysIteratorNonZero.map(i => (i,i)); 48 | 49 | override def valuesIteratorNonZero = 50 | diag.valuesIteratorNonZero; 51 | 52 | override def pairsIteratorNonZero = 53 | diag.pairsIteratorNonZero.map(tup => ((tup._1,tup._1),tup._2)); 54 | 55 | override def triplesIteratorNonZero = 56 | diag.pairsIteratorNonZero.map(tup => (tup._1, tup._1, tup._2)); 57 | } 58 | 59 | -------------------------------------------------------------------------------- /src/main/scala/scalala/tensor/DomainFunction.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package tensor; 22 | 23 | import domain._; 24 | 25 | /** 26 | * A function over a Domain of elements of type A returning values 27 | * of type B. This type extends PartialFunction by restricting the 28 | * domain of possible input functions to an explicitly represented domain. 29 | * 30 | * @author dramage 31 | */ 32 | trait DomainFunction 33 | [@specialized(Int,Long,Float,Double) K, 34 | @specialized(Int,Long,Float,Double,Boolean) +V, 35 | +D <: Domain[K]] 36 | extends PartialFunction[K, V] { 37 | 38 | /** The domain over which this function is defined. */ 39 | def domain : D; 40 | 41 | /** Returns true if the key is in the function's domain. */ 42 | override def isDefinedAt(key : K) = 43 | domain(key); 44 | 45 | /** @throws DomainException if key is not in the domain. */ 46 | def checkKey(key : K) : Unit = { 47 | if (!isDefinedAt(key)) 48 | throw new DomainException("Key " + key + " not in domain"); 49 | } 50 | 51 | /** @throws DomainException if domain is not equal to this domain. */ 52 | def checkDomain(domain : Domain[K]) : Unit = { 53 | if (this.domain != domain) 54 | throw new DomainException("Incompatible domain: "+domain + " for " + this.domain); 55 | } 56 | 57 | def apply(key : K) : V; 58 | } 59 | 60 | -------------------------------------------------------------------------------- /src/main/scala/scalala/tensor/MatrixSingularException.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package tensor; 22 | 23 | /** 24 | * Thrown when trying to sovle using a singular matrix. 25 | * 26 | * @author dramage 27 | */ 28 | class MatrixSingularException(msg : String) extends RuntimeException(msg) { 29 | def this() = this(null); 30 | } 31 | 32 | 33 | -------------------------------------------------------------------------------- /src/main/scala/scalala/tensor/MatrixTranspose.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package tensor; 22 | 23 | import domain.{IndexDomain,TableDomain}; 24 | 25 | import scalala.scalar.Scalar; 26 | import scalala.generic.collection._; 27 | import scalala.operators._; 28 | 29 | /** 30 | * A Transpose of any Matrix type is a Matrix. 31 | * 32 | * @author dramage 33 | */ 34 | trait MatrixTransposeLike 35 | [@specialized(Int,Long,Float,Double) V, +Coll <: Matrix[V], +This <: MatrixTranspose[V,Coll]] 36 | extends Tensor2TransposeLike[Int,Int,V,IndexDomain,IndexDomain,TableDomain,TableDomain,Coll,This] 37 | with MatrixLike[V,This] { 38 | override def domain = underlying.domain.transpose.asInstanceOf[TableDomain]; 39 | 40 | override def numRows = underlying.numCols; 41 | override def numCols = underlying.numRows; 42 | 43 | override def t : Coll = 44 | underlying; 45 | } 46 | 47 | /** 48 | * A Transpose of any Matrix type is a Matrix. 49 | * 50 | * @author dramage 51 | */ 52 | trait MatrixTranspose[@specialized(Int,Long,Float,Double) B, +Coll <: Matrix[B]] 53 | extends Tensor2Transpose[Int,Int,B,Coll] 54 | with Matrix[B] with MatrixTransposeLike[B, Coll, MatrixTranspose[B, Coll]]; 55 | 56 | object MatrixTranspose { 57 | class Impl[B, +Coll <: Matrix[B]] 58 | (override val underlying : Coll) 59 | (implicit override val scalar : Scalar[B]) 60 | extends MatrixTranspose[B,Coll]; 61 | } 62 | 63 | -------------------------------------------------------------------------------- /src/main/scala/scalala/tensor/SelectAll.scala: -------------------------------------------------------------------------------- 1 | package scalala.tensor 2 | 3 | /** 4 | * For selecting all elements from a matrix row or column. 5 | * See :: literal. 6 | * 7 | * @author dramage 8 | */ 9 | sealed trait SelectAll; 10 | 11 | /** 12 | * For selecting all elements from a matrix row or column. 13 | * 14 | * @author dramage 15 | */ 16 | object :: extends SelectAll; 17 | -------------------------------------------------------------------------------- /src/main/scala/scalala/tensor/Tensor1Col.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package tensor; 22 | 23 | import domain.{IterableDomain,Domain1,IndexDomain,CanGetDomain,CanBuildDomain2}; 24 | import generic.TensorBuilder; 25 | 26 | import scalala.operators._; 27 | import scalala.scalar.Scalar; 28 | import scalala.generic.collection.CanBuildTensorFrom; 29 | 30 | /** 31 | * Implementation trait for a one-axis tensor shaped as a column. 32 | * 33 | * @author dramage 34 | */ 35 | trait Tensor1ColLike 36 | [@specialized(Int,Long) K, @specialized(Int,Long,Float,Double) V, 37 | +D<:Domain1[K], +This<:Tensor1Col[K,V]] 38 | extends Tensor1Like[K,V,D,This] with operators.ColOps[This] { self => 39 | override def newBuilder[K2,V2:Scalar](domain : IterableDomain[K2]) = domain match { 40 | case that : IndexDomain => 41 | mutable.Vector[V2](that).asBuilder; 42 | case that : Domain1[_] => 43 | mutable.Tensor1Col[K2,V2](that).asBuilder; 44 | case _ => 45 | super.newBuilder[K2,V2](domain); 46 | } 47 | 48 | def t : Tensor1Row[K,V] = 49 | new Tensor1Row.View[K,V](repr); 50 | } 51 | 52 | /** 53 | * One-axis tensor shaped as a column. 54 | * 55 | * @author dramage 56 | */ 57 | trait Tensor1Col[@specialized(Int,Long) K, @specialized(Int,Long,Float,Double) V] 58 | extends Tensor1[K,V] with Tensor1ColLike[K,V,Domain1[K],Tensor1Col[K,V]]; 59 | 60 | object Tensor1Col { 61 | class View[K,V](override val inner : Tensor1Row[K,V]) 62 | extends Tensor1Proxy[K,V,Tensor1Row[K,V]] with Tensor1Col[K,V] 63 | with Tensor1Like[K,V,Domain1[K],View[K,V]] { 64 | override def repr : View[K,V] = this; 65 | override def t : Tensor1Row[K,V] = inner; 66 | } 67 | 68 | implicit def canMulTensor1ColByRow[K1,K2,V1,V2,RV,A,DA,B,DB,DThat,That] 69 | (implicit viewA : A=>Tensor1Col[K1,V1], viewB : B=>Tensor1Row[K2,V2], 70 | dA : CanGetDomain[A,DA], dB : CanGetDomain[B,DB], 71 | dThat : CanBuildDomain2[DA,DB,DThat], 72 | mul : BinaryOp[V1,V2,OpMul,RV], 73 | bf : CanBuildTensorFrom[A, DThat, (K1,K2), RV, That]) 74 | : BinaryOp[A,B,OpMulColVectorBy,That] 75 | = new BinaryOp[A,B,OpMulColVectorBy,That] { 76 | override def opType = OpMulColVectorBy; 77 | override def apply(a : A, b : B) = { 78 | val builder = bf(a, dThat(a.domain.asInstanceOf[DA], b.domain.asInstanceOf[DB])); 79 | a.foreachNonZeroPair((i,va) => b.foreachNonZeroPair((j,vb) => builder((i,j)) = mul(va,vb))); 80 | builder.result; 81 | } 82 | } 83 | } 84 | 85 | -------------------------------------------------------------------------------- /src/main/scala/scalala/tensor/Tensor1Proxy.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package tensor; 22 | 23 | import domain.Domain1; 24 | 25 | import scalala.scalar.Scalar; 26 | import scalala.operators._; 27 | 28 | /** 29 | * Implementation trait for proxies to a Tensor1 instance. 30 | * 31 | * @author dramage 32 | */ 33 | trait Tensor1ProxyLike 34 | [@specialized(Int,Long)K, @specialized(Int,Long,Float,Double) V, 35 | +D<:Domain1[K], Inner<:Tensor1[K,V], +This<:Tensor1[K,V]] 36 | extends TensorProxyLike[K,V,D,Inner,This] with Tensor1Like[K,V,D,This] { 37 | override def norm(n : Double) = 38 | inner.norm(n); 39 | } 40 | 41 | /** 42 | * Proxy to a Tensor1 instance. 43 | * 44 | * @author dramage 45 | */ 46 | trait Tensor1Proxy 47 | [@specialized(Int,Long)K, @specialized(Int,Long,Float,Double) V, Inner<:Tensor1[K,V]] 48 | extends TensorProxy[K,V,Inner] with Tensor1[K,V] with Tensor1ProxyLike[K,V,Domain1[K],Inner,Tensor1Proxy[K,V,Inner]]; 49 | 50 | -------------------------------------------------------------------------------- /src/main/scala/scalala/tensor/Tensor1Slice.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package tensor; 22 | 23 | import domain.{IterableDomain,Domain1}; 24 | 25 | /** 26 | * Implementation trait for a Tensor1 view of a slice of keys from a Tensor. 27 | * 28 | * @author dramage 29 | */ 30 | trait Tensor1SliceLike 31 | [@specialized(Int,Long) K1, +D1<:IterableDomain[K1], 32 | @specialized(Int,Long) K2, +D2<:Domain1[K2], 33 | @specialized(Int,Long,Float,Double,Boolean) V, +Coll<:Tensor[K1,V], 34 | +This<:Tensor1Slice[K1,K2,V,Coll]] 35 | extends TensorSliceLike[K1, D1, K2, D2, V, Coll, This] 36 | with Tensor1Like[K2, V, D2, This]; 37 | 38 | /** 39 | * A Tensor1 view of a slice of keys from a Tensor. 40 | * 41 | * @author dramage 42 | */ 43 | trait Tensor1Slice 44 | [@specialized(Int,Long) K1, @specialized(Int,Long) K2, 45 | @specialized(Int,Long,Float,Double,Boolean) V, +Coll<:Tensor[K1,V]] 46 | extends TensorSlice[K1,K2,V,Coll] with Tensor1[K2,V] 47 | with Tensor1SliceLike[K1, IterableDomain[K1], K2, Domain1[K2], V, Coll, Tensor1Slice[K1, K2, V, Coll]]; 48 | 49 | -------------------------------------------------------------------------------- /src/main/scala/scalala/tensor/Tensor2Transpose.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package tensor; 22 | 23 | import scalar.Scalar; 24 | 25 | import domain.{Domain1,Domain2}; 26 | 27 | /** 28 | * Implementation trait for a transposed view of an underlying Tensor2. 29 | * 30 | * @author dramage 31 | */ 32 | trait Tensor2TransposeLike 33 | [@specialized(Int) K2, @specialized(Int) K1, 34 | @specialized(Int,Long,Float,Double,Boolean) V, 35 | +D2<:Domain1[K2], 36 | +D1<:Domain1[K1], 37 | +T<:Domain2[K2,K1], 38 | +D<:Domain2[K1,K2], 39 | +Coll<:Tensor2[K1,K2,V], 40 | +This<:Tensor2Transpose[K2,K1,V,Coll]] 41 | extends TensorSliceLike[(K1,K2),D,(K2,K1),T,V,Coll,This] 42 | with Tensor2Like[K2,K1,V,D2,D1,T,D,This] { 43 | self => 44 | 45 | override def domain = underlying.domain.transpose.asInstanceOf[T]; 46 | override def size = underlying.size; 47 | 48 | /* final */ override def lookup(tup : (K2,K1)) = tup.swap; 49 | 50 | override def apply(i : K2, j : K1) = underlying.apply(j, i); 51 | 52 | override def t : Coll = 53 | underlying; 54 | } 55 | 56 | /** 57 | * Transposed view of an underlying Tensor2. 58 | * 59 | * @author dramage 60 | */ 61 | trait Tensor2Transpose 62 | [@specialized(Int) K2, @specialized(Int) K1, 63 | @specialized(Int,Long,Float,Double,Boolean) V, 64 | +Coll <: Tensor2[K1,K2,V]] 65 | extends TensorSlice[(K1,K2),(K2,K1),V,Coll] 66 | with Tensor2[K2,K1,V] 67 | with Tensor2TransposeLike[K2,K1,V,Domain1[K2],Domain1[K1],Domain2[K2,K1],Domain2[K1,K2],Coll,Tensor2Transpose[K2,K1,V,Coll]]; 68 | 69 | 70 | object Tensor2Transpose { 71 | /** Default implementation. */ 72 | class Impl[K2, K1, V, +Coll <: Tensor2[K1,K2,V]] 73 | (override val underlying : Coll) 74 | (override implicit val scalar : Scalar[V]) 75 | extends Tensor2Transpose[K2,K1,V,Coll]; 76 | } 77 | 78 | -------------------------------------------------------------------------------- /src/main/scala/scalala/tensor/TensorN.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package tensor; 22 | 23 | import scalar.Scalar; 24 | 25 | import domain.DomainN; 26 | 27 | /** 28 | * Tensors indexed by a sequence of keys. 29 | * 30 | * @author dramage 31 | */ 32 | trait TensorNLike[@specialized(Int) K, @specialized(Int,Long,Float,Double,Boolean) V, +This<:TensorN[K,V]] 33 | extends TensorLike[Seq[K],V,DomainN[K],This] { 34 | /** Gets the value indexed by (i,j). */ 35 | /* final */ def apply(k : K*) : V = 36 | apply(k : Seq[K]); 37 | 38 | 39 | override protected def canEqual(other : Any) : Boolean = other match { 40 | case that : TensorN[_,_] => true; 41 | case _ => false; 42 | } 43 | } 44 | 45 | trait TensorN[@specialized(Int) K, @specialized(Int,Long,Float,Double,Boolean) V] 46 | extends Tensor[Seq[K],V] with TensorNLike[K,V,TensorN[K,V]] 47 | 48 | object TensorN; 49 | -------------------------------------------------------------------------------- /src/main/scala/scalala/tensor/TensorSlice.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package tensor; 22 | 23 | import scalar.Scalar; 24 | 25 | import domain.{IterableDomain,SetDomain}; 26 | 27 | /** 28 | * Implementation trait for slices of an underlying Tensor. A slice 29 | * is a pass-through view of a (mapped) subset of the original Tensor's 30 | * domain. 31 | * 32 | * @author dramage 33 | */ 34 | trait TensorSliceLike 35 | [@specialized(Int,Long) K1, +D1<:IterableDomain[K1], 36 | @specialized(Int,Long) K2, +D2<:IterableDomain[K2], 37 | @specialized(Int,Long,Float,Double,Boolean) V, +Coll<:Tensor[K1,V], 38 | +This<:TensorSlice[K1,K2,V,Coll]] 39 | extends TensorLike[K2,V,D2,This] { 40 | self => 41 | 42 | /** The collection underlying this view. */ 43 | protected def underlying: Coll; 44 | 45 | /** Maps the keys of this domain map to the keys of the underlying maps's. */ 46 | def lookup(key : K2) : K1; 47 | 48 | override def apply(key : K2) = 49 | underlying.apply(lookup(key)); 50 | 51 | override def newBuilder[K2,V2:Scalar](domain : IterableDomain[K2]) = 52 | underlying.newBuilder[K2,V2](domain); 53 | } 54 | 55 | /** 56 | * Pass-through view of a (key-mapped) subset of an underlying Tensor. 57 | * 58 | * @author dramage 59 | */ 60 | trait TensorSlice 61 | [@specialized(Int,Long) K1, @specialized(Int,Long) K2, 62 | @specialized(Int,Long,Float,Double,Boolean) V, +Coll <: Tensor[K1, V]] 63 | extends Tensor[K2,V] 64 | with TensorSliceLike[K1, IterableDomain[K1], K2, IterableDomain[K2], V, Coll, TensorSlice[K1, K2, V, Coll]]; 65 | 66 | object TensorSlice { 67 | class FromKeyMap[K1, K2, V, +Coll<:Tensor[K1, V]] 68 | (override val underlying : Coll, keymap : scala.collection.Map[K2,K1]) 69 | (override implicit val scalar : Scalar[V]) 70 | extends TensorSlice[K1, K2, V, Coll] { 71 | override def lookup(key : K2) = keymap(key); 72 | override def domain = new SetDomain(keymap.keySet); 73 | override def size = keymap.size; 74 | } 75 | } 76 | 77 | -------------------------------------------------------------------------------- /src/main/scala/scalala/tensor/VectorProxy.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package tensor; 22 | 23 | import domain.IndexDomain; 24 | 25 | /** 26 | * Implementation trait for proxies to a Vector instance. 27 | * 28 | * @author dramage 29 | */ 30 | trait VectorProxyLike[@specialized(Int,Long,Float,Double) V, Inner<:Vector[V], +This<:Vector[V]] 31 | extends Tensor1ProxyLike[Int,V,IndexDomain,Inner,This] with VectorLike[V,This] { 32 | override def length = inner.length; 33 | } 34 | 35 | /** 36 | * Proxy to a Vector instance. 37 | * 38 | * @author dramage 39 | */ 40 | trait VectorProxy[@specialized(Int,Long,Float,Double) V, Inner<:Vector[V]] 41 | extends Tensor1Proxy[Int,V,Inner] with Vector[V] with VectorProxyLike[V,Inner,VectorProxy[V,Inner]]; 42 | -------------------------------------------------------------------------------- /src/main/scala/scalala/tensor/VectorRow.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package tensor; 22 | 23 | import scalar.Scalar; 24 | 25 | import domain.IndexDomain; 26 | import generic.TensorBuilder; 27 | 28 | import scalala.generic.collection.{CanSliceCol}; 29 | import scalala.operators._; 30 | 31 | import dense.{DenseVector,DenseVectorRow}; 32 | 33 | /** 34 | * Implementation trait for a row vector. 35 | * 36 | * @author dramage 37 | */ 38 | trait VectorRowLike[@specialized(Int,Long,Float,Double) V, +This<:VectorRow[V]] 39 | extends VectorLike[V,This] with Tensor1RowLike[Int,V,IndexDomain,This] { 40 | 41 | override def t : VectorCol[V] = 42 | new VectorCol.View[V](repr); 43 | 44 | /** Returns a copy of this vector as a DenseVectorRow. */ 45 | override def toDense : DenseVectorRow[V] = { 46 | val rv = DenseVector.zeros(length).t; 47 | rv := repr; 48 | rv; 49 | } 50 | 51 | def toString(maxWidth : Int = ScalalaConsole.terminalWidth, 52 | mkValueString : V=>String = buildMkValueString) : String = { 53 | def colWidth(col : Int) = mkValueString(this(col)).length+2; 54 | 55 | val colWidths = new scala.collection.mutable.ArrayBuffer[Int]; 56 | var col = 0; 57 | while (col < size && colWidths.sum < maxWidth) { 58 | colWidths += colWidth(col); 59 | col += 1; 60 | } 61 | // make space for "... (K total)" 62 | if (colWidths.size < length) { 63 | while (colWidths.sum + maxWidth.toString.length + 12 >= maxWidth) { 64 | colWidths.remove(colWidths.length - 1); 65 | } 66 | } 67 | 68 | val newline = System.getProperty("line.separator"); 69 | 70 | var rv = new StringBuilder; 71 | for (col <- 0 until colWidths.length) { 72 | val cell = mkValueString(this(col)); 73 | rv.append(cell); 74 | rv.append(" " * (colWidths(col) - cell.length)); 75 | if (col == colWidths.length - 1) { 76 | if (col < size - 1) { 77 | rv.append("..."); 78 | rv.append(" ("); 79 | rv.append(length); 80 | rv.append(" total)"); 81 | } 82 | } 83 | } 84 | rv.append(newline); 85 | rv.toString; 86 | } 87 | 88 | override def toString = 89 | toString(maxWidth = ScalalaConsole.terminalWidth, 90 | mkValueString = buildMkValueString); 91 | } 92 | 93 | /** 94 | * A vector shaped as a row. 95 | * 96 | * @author dramage 97 | */ 98 | trait VectorRow[@specialized(Int,Long,Float,Double) V] 99 | extends Vector[V] with Tensor1Row[Int,V] with VectorRowLike[V,VectorRow[V]]; 100 | 101 | object VectorRow { 102 | class View[V](override val inner : Vector[V]) 103 | extends VectorProxy[V,Vector[V]] with VectorRow[V] 104 | with VectorLike[V,View[V]] { 105 | override def repr : View[V] = this; 106 | } 107 | } 108 | 109 | -------------------------------------------------------------------------------- /src/main/scala/scalala/tensor/dense/ArrayArrayMatrix.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package tensor; 22 | package dense; 23 | 24 | import domain.TableDomain; 25 | 26 | import scalala.scalar.Scalar; 27 | 28 | /** 29 | * A matrix backed by an array of arrays of values. 30 | * Assumes row-major storage. 31 | * 32 | * @author dramage 33 | */ 34 | class ArrayArrayMatrix[B](val data : Array[Array[B]]) 35 | (implicit override val scalar : Scalar[B]) 36 | extends mutable.Matrix[B] with mutable.MatrixLike[B,ArrayArrayMatrix[B]] { 37 | 38 | if (data.map(_.length).distinct.size != 1) { 39 | throw new IllegalArgumentException("All rows must be same length"); 40 | } 41 | 42 | override def numRows = 43 | data.length; 44 | 45 | override def numCols = 46 | data(0).length; 47 | 48 | override def apply(i : Int, j : Int) = 49 | data(i)(j); 50 | 51 | override def update(i : Int, j : Int, v : B) = 52 | data(i)(j) = v; 53 | } 54 | -------------------------------------------------------------------------------- /src/main/scala/scalala/tensor/dense/DenseArrayTensor.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package tensor; 22 | package dense; 23 | 24 | import domain.{IterableDomain,IndexDomain,TableDomain}; 25 | import generic.TensorBuilder; 26 | 27 | import scalala.scalar.Scalar; 28 | import scalala.generic.collection._; 29 | import scalala.operators._; 30 | 31 | /** 32 | * Implementation trait for a tensor backed by a dense array of values. 33 | * 34 | * @author dramage 35 | */ 36 | trait DenseArrayTensorLike 37 | [@specialized(Int,Long) K, @specialized(Int,Long,Float,Double,Boolean) V, 38 | +D<:IterableDomain[K], 39 | +This<:DenseArrayTensor[K,V]] 40 | extends mutable.TensorLike[K,V,D,This] { 41 | def data : Array[V]; 42 | 43 | override def newBuilder[K2,V2:Scalar](domain : IterableDomain[K2]) 44 | : TensorBuilder[K2,V2,Tensor[K2,V2]] = domain match { 45 | case that : IndexDomain => DenseVector.zeros[V2](that.size).asBuilder; 46 | case that : TableDomain => DenseMatrix.zeros[V2](that.numRows, that.numCols).asBuilder; 47 | case _ => super.newBuilder[K2,V2](domain); 48 | } 49 | 50 | override def foreachNonZeroValue[U](fn : (V=>U)) = { 51 | foreachValue(fn); 52 | true; 53 | } 54 | } 55 | 56 | /** 57 | * Mutable tensor backed by a dense array of values. 58 | * 59 | * @author dramage 60 | */ 61 | trait DenseArrayTensor 62 | [@specialized(Int,Long) K, @specialized(Int,Long,Float,Double,Boolean) V] 63 | extends mutable.Tensor[K,V] 64 | with DenseArrayTensorLike[K,V,IterableDomain[K],DenseArrayTensor[K,V]]; 65 | -------------------------------------------------------------------------------- /src/main/scala/scalala/tensor/domain/CanBuildDomain2.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package tensor; 22 | package domain; 23 | 24 | /** 25 | * Marker trait for statically building a Domain2 for two input domains. 26 | * 27 | * @author dramage 28 | */ 29 | trait CanBuildDomain2[-A,-B,RV] extends ((A,B) => RV); 30 | 31 | trait CanBuildDomain2ImplicitsLevel0 { 32 | implicit def buildGeneral[K1,K2] 33 | : CanBuildDomain2[Domain1[K1],Domain1[K2],Domain2[K1,K2]] 34 | = new CanBuildDomain2[Domain1[K1],Domain1[K2],Domain2[K1,K2]] { 35 | def apply(a : Domain1[K1], b : Domain1[K2]) = 36 | a.product[K2,Domain1[K2]](b); 37 | } 38 | } 39 | 40 | trait CanBuildDomain2ImplicitsLevel1 extends CanBuildDomain2ImplicitsLevel0 { 41 | implicit object BuildIndexIndex 42 | extends CanBuildDomain2[IndexDomain,IndexDomain,TableDomain] { 43 | override def apply(a : IndexDomain, b : IndexDomain) = 44 | TableDomain(a.size, b.size); 45 | } 46 | } 47 | 48 | object CanBuildDomain2 extends CanBuildDomain2ImplicitsLevel1; 49 | 50 | -------------------------------------------------------------------------------- /src/main/scala/scalala/tensor/domain/CanGetDomain.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package tensor; 22 | package domain; 23 | 24 | /** 25 | * Marker trait for statically getting the domain of a given tensor. 26 | * 27 | * @author dramage 28 | */ 29 | trait CanGetDomain[-From,Domain] extends (From => Domain); 30 | 31 | trait CanGetDomainImplicitsLevel0 { 32 | implicit def domainForTensor[K,T](implicit view : T => Tensor[K,_]) 33 | : CanGetDomain[T,IterableDomain[K]] 34 | = new CanGetDomain[T,IterableDomain[K]] { def apply(t : T) = t.domain; } 35 | } 36 | 37 | trait CanGetDomainImplicitsLevel1 extends CanGetDomainImplicitsLevel0 { 38 | implicit def domainForTensor1[K,T](implicit view : T => Tensor1[K,_]) 39 | : CanGetDomain[T,Domain1[K]] 40 | = new CanGetDomain[T,Domain1[K]] { def apply(t : T) = t.domain; } 41 | 42 | implicit def domainForTensor2[K1,K2,T](implicit view : T => Tensor2[K1,K2,_]) 43 | : CanGetDomain[T,Domain2[K1,K2]] 44 | = new CanGetDomain[T,Domain2[K1,K2]] { def apply(t : T) = t.domain; } 45 | } 46 | 47 | trait CanGetDomainImplicitsLevel2 extends CanGetDomainImplicitsLevel1 { 48 | implicit def domainForVector[T](implicit view : T => Vector[_]) 49 | : CanGetDomain[T,IndexDomain] 50 | = new CanGetDomain[T,IndexDomain] { def apply(t : T) = t.domain; } 51 | 52 | implicit def domainForMatrix[T](implicit view : T => Matrix[_]) 53 | = new CanGetDomain[T,TableDomain] { def apply(t : T) = t.domain; } 54 | } 55 | 56 | object CanGetDomain extends CanGetDomainImplicitsLevel2 { 57 | def apply[T] = new { 58 | def apply[D](implicit df : CanGetDomain[T,D]) = df; 59 | } 60 | } 61 | 62 | /** 63 | * Capability trait for statically getting the domain2 of a Tensor2. 64 | * 65 | * @author dramage 66 | */ 67 | trait CanGetDomain2[-From,D1,D2,Domain] extends (From => Domain) { 68 | def _1(from : From) : D1; 69 | def _2(from : From) : D2; 70 | } 71 | 72 | trait CanGetDomain2ImplicitsLevel0 { 73 | implicit def domainForTensor2[K1,K2,T](implicit view : T => Tensor2[K1,K2,_]) 74 | : CanGetDomain2[T,Domain1[K1],Domain1[K2],Domain2[K1,K2]] 75 | = new CanGetDomain2[T,Domain1[K1],Domain1[K2],Domain2[K1,K2]] { 76 | def apply(t : T) = t.domain; 77 | def _1(t : T) = t.domain._1; 78 | def _2(t : T) = t.domain._2; 79 | } 80 | } 81 | 82 | trait CanGetDomain2ImplicitsLevel1 extends CanGetDomain2ImplicitsLevel0 { 83 | implicit def domainForMatrix[T](implicit view : T => Matrix[_]) 84 | : CanGetDomain2[T,IndexDomain,IndexDomain,TableDomain] 85 | = new CanGetDomain2[T,IndexDomain,IndexDomain,TableDomain] { 86 | def apply(t : T) = t.domain; 87 | def _1(t : T) = t.domain._1; 88 | def _2(t : T) = t.domain._2; 89 | } 90 | } 91 | 92 | object CanGetDomain2 extends CanGetDomain2ImplicitsLevel1 { 93 | def apply[T] = new { 94 | def apply[D1,D2,D](implicit df : CanGetDomain2[T,D1,D2,D]) = df; 95 | } 96 | } 97 | 98 | -------------------------------------------------------------------------------- /src/main/scala/scalala/tensor/domain/Domain.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package tensor; 22 | package domain; 23 | 24 | /** 25 | * Implementation trait for domains of a DomainFunction, representing 26 | * a restriction on values of type A. 27 | * 28 | * @author dramage 29 | */ 30 | trait DomainLike[@specialized(Int,Long,Float,Double) A, +This<:Domain[A]] 31 | extends (A => Boolean) { 32 | 33 | def repr : This = 34 | this.asInstanceOf[This]; 35 | 36 | /** Calls contains(key). */ 37 | final override def apply(key : A) = contains(key); 38 | 39 | /** Returns true if the given element is part of the set. */ 40 | def contains(key : A) : Boolean; 41 | } 42 | 43 | /** 44 | * Domains of a DomainFunction, representing a restriction on values of type A. 45 | * 46 | * @author dramage 47 | */ 48 | trait Domain[@specialized(Int,Long,Float,Double) A] 49 | extends DomainLike[A, Domain[A]]; 50 | 51 | /** 52 | * An exception thrown when encountering an invalid domain. 53 | * 54 | * @author dramage 55 | */ 56 | class DomainException(msg : String) extends RuntimeException(msg) { 57 | def this() = this(null); 58 | } 59 | 60 | -------------------------------------------------------------------------------- /src/main/scala/scalala/tensor/domain/Domain1.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package tensor; 22 | package domain; 23 | 24 | /** 25 | * A domain that explicitly has only one element, i.e. A is not a tuple. 26 | * 27 | * @author dramage 28 | */ 29 | trait Domain1Like[@specialized(Int,Long) A, +This<:Domain1[A]] 30 | extends IterableDomainLike[A,This] { outer => 31 | override def repr: This = this.asInstanceOf[This]; 32 | 33 | /** Constructs the union of this and the other domain. */ 34 | override def union(that : IterableDomain[A]) : IterableDomain[A] = that match { 35 | case d1 : Domain1[_] => this.union(d1.asInstanceOf[Domain1[A]]); 36 | case _ => super.union(that); 37 | } 38 | 39 | /** Constructs the union of this and the other domain. */ 40 | def union(that : Domain1[A]) : Domain1[A] = { 41 | new Domain1[A] with UnionDomainLike[A,Domain1[A]] { 42 | override def a = outer.repr; 43 | override def b = that; 44 | } 45 | } 46 | 47 | def product[B,That<:Domain1[B]](that : That) = 48 | Domain2(repr,that); 49 | } 50 | 51 | /** 52 | * A domain that explicitly has only one element, i.e. A is not a tuple. 53 | * 54 | * @author dramage 55 | */ 56 | trait Domain1[@specialized(Int,Long) A] 57 | extends IterableDomain[A] with Domain1Like[A,Domain1[A]]; 58 | 59 | -------------------------------------------------------------------------------- /src/main/scala/scalala/tensor/domain/DomainN.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package tensor; 22 | package domain; 23 | 24 | /** 25 | * A domain indexed by sequences of underyling key type K. 26 | * 27 | * @author dramage 28 | */ 29 | case class DomainN[@specialized(Int) K](components : Seq[IterableDomain[K]]) 30 | extends IterableDomain[Seq[K]] with IterableDomainLike[Seq[K],DomainN[K]] { 31 | 32 | override def size = 33 | components.map(_.size).reduceLeft(_ * _); 34 | 35 | override def union(other : IterableDomain[Seq[K]]) = other match { 36 | case that : DomainN[_] => { 37 | val casted = that.asInstanceOf[DomainN[K]]; 38 | require(this.components.size == casted.components.size, "Can only take the union of product domains of the same size"); 39 | DomainN((this.components zip casted.components) map (tup => tup._1 union tup._2)); 40 | } 41 | case _ => super.union(other); 42 | } 43 | 44 | override def foreach[O](fn : (Seq[K] => O)) = { 45 | def unroll(key : List[K], remaining : Seq[IterableDomain[K]]) { 46 | require(remaining.length > 0); 47 | if (remaining.length == 1) { 48 | remaining.head.foreach(e => fn(key :+ e)); 49 | } else { 50 | remaining.head.foreach(e => unroll(key :+ e, remaining.tail)); 51 | } 52 | } 53 | unroll(List.empty[K], components); 54 | } 55 | 56 | /** Iterators all elements of this domain. */ 57 | override def iterator = { 58 | def unroll(remaining : Seq[IterableDomain[K]]) : Iterator[Seq[K]] = { 59 | require(remaining.length > 0); 60 | if (remaining.length == 1) { 61 | remaining.head.iterator.map(k => List(k)); 62 | } else { 63 | for (k <- remaining.head.iterator; 64 | rest <- unroll(remaining.tail)) 65 | yield List(k) ++ rest; 66 | } 67 | } 68 | 69 | unroll(components); 70 | } 71 | 72 | /** Returns true if a1 is in the row space and a2 is in the col space. */ 73 | def contains(k : Seq[K]) = 74 | (components.length == k.length) && (components zip k).forall(tup => tup._1 contains tup._2); 75 | 76 | override def equals(other : Any) = other match { 77 | case that : DomainN[_] => 78 | this.components == that.components 79 | case base : Domain[_] => 80 | super.equals(base); 81 | case _ => false; 82 | } 83 | } 84 | 85 | -------------------------------------------------------------------------------- /src/main/scala/scalala/tensor/domain/IndexDomain.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package tensor; 22 | package domain; 23 | 24 | /** 25 | * The domain of indices: ints starting from 0 up to a bounded size. 26 | * 27 | * @author dramage 28 | */ 29 | case class IndexDomain(override val size : Int) 30 | extends Domain1[Int] with Domain1Like[Int,IndexDomain] { 31 | override def foreach[O](fn : Int=>O) = { 32 | var i = 0; 33 | while (i < size) { 34 | fn(i); 35 | i += 1; 36 | } 37 | } 38 | 39 | override def product[B,That<:Domain1[B]](that : That) = that match { 40 | case IndexDomain(otherSize) => TableDomain(size,otherSize); 41 | case _ => super.product[B,That](that); 42 | } 43 | 44 | override def toIndexedSeq[B>:Int] = 45 | Range(0,size); 46 | 47 | override def contains(key : Int) = 48 | key >= 0 && key < size; 49 | 50 | override def union(other : IterableDomain[Int]) = other match { 51 | case that : IndexDomain => IndexDomain(this.size max that.size); 52 | case _ => super.union(other); 53 | } 54 | 55 | override def iterator = 56 | Iterator.range(0, size); 57 | 58 | override def toString = 59 | "IndexDomain("+size+")"; 60 | 61 | override def equals(other : Any) = other match { 62 | case IndexDomain(s) => this.size == s; 63 | case _ => super.equals(other); 64 | } 65 | } 66 | 67 | -------------------------------------------------------------------------------- /src/main/scala/scalala/tensor/domain/IterableDomain.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package tensor; 22 | package domain; 23 | 24 | /** 25 | * Implementation trait for a domain that can be traversed. Default 26 | * equality check returns true if this eq other or if this and other 27 | * traverse the same elements in the same order. 28 | * 29 | * @author dramage 30 | */ 31 | trait IterableDomainLike[@specialized(Int,Long) A, +This<:IterableDomain[A]] 32 | extends DomainLike[A,This] with Iterable[A] { outer => 33 | override def repr: This = this.asInstanceOf[This]; 34 | /** Applies the given function to every element of the domain. */ 35 | def foreach[O](f : A => O); 36 | 37 | /** Iterates over the elements of the domain. */ 38 | def iterator : Iterator[A]; 39 | 40 | /** Constructs the union of this and the other domain. */ 41 | def union(that : IterableDomain[A]) : IterableDomain[A] = { 42 | new IterableDomain[A] with UnionDomainLike[A,IterableDomain[A]] { 43 | override def a = outer.repr; 44 | override def b = that; 45 | } 46 | } 47 | 48 | /** Number of elements in the domain. */ 49 | def size : Int; 50 | 51 | // def toIndexedSeq = 52 | // iterator.toIndexedSeq; 53 | 54 | // def toArray(implicit mf : Manifest[A]) = 55 | // iterator.toArray; 56 | 57 | // def filter(fn : A => Boolean) : List[A] = 58 | // iterator.filter(fn).toList; 59 | 60 | // def max(implicit ord : Ordering[A]) = 61 | // iterator.max(ord); 62 | 63 | // def min(implicit ord : Ordering[A]) = 64 | // iterator.min(ord); 65 | 66 | override def equals(other : Any) = other match { 67 | case that : IterableDomain[_] => 68 | (this eq that) || (this.iterator zip that.iterator).forall(tup => tup._1 == tup._2); 69 | case _ => 70 | false; 71 | } 72 | } 73 | 74 | /** 75 | * A domain that can be traversed. 76 | * 77 | * @author dramage 78 | */ 79 | trait IterableDomain[@specialized(Int,Long) A] 80 | extends Domain[A] with IterableDomainLike[A,IterableDomain[A]]; 81 | 82 | -------------------------------------------------------------------------------- /src/main/scala/scalala/tensor/domain/SetDomain.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package tensor; 22 | package domain; 23 | 24 | /** 25 | * The domain of elements from a specific set. 26 | * 27 | * @author dramage 28 | */ 29 | case class SetDomain[@specialized(Int,Long) A](set : scala.collection.Set[A]) 30 | extends Domain1[A] with Domain1Like[A,SetDomain[A]] { 31 | 32 | override def size = 33 | set.size; 34 | 35 | override def foreach[O](fn : A=>O) = 36 | set.foreach(fn); 37 | 38 | override def iterator = 39 | set.iterator; 40 | 41 | override def contains(key : A) : Boolean = 42 | set.contains(key); 43 | 44 | override def equals(other : Any) = other match { 45 | case SetDomain(s) => this.set == s; 46 | case that : Domain[_] => super.equals(that); 47 | case _ => false; 48 | } 49 | } 50 | 51 | -------------------------------------------------------------------------------- /src/main/scala/scalala/tensor/domain/TableDomain.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package tensor; 22 | package domain; 23 | 24 | /** 25 | * An immutable Domain2 indexed by rows and columns. 26 | * 27 | * @author dramage 28 | */ 29 | case class TableDomain(numRows : Int, numCols : Int) 30 | extends Product2[IndexDomain,IndexDomain] with Domain2[Int,Int] 31 | with Domain2Like[Int,Int,IndexDomain,IndexDomain,TableDomain,TableDomain] { 32 | 33 | override val _1 : IndexDomain = IndexDomain(numRows); 34 | 35 | override val _2 : IndexDomain = IndexDomain(numCols); 36 | 37 | override def transpose = 38 | TableDomain(numCols, numRows); 39 | 40 | override def foreach[O](fn : (((Int,Int))=>O)) = { 41 | var i = 0; 42 | while (i < numRows) { 43 | var j = 0; 44 | while (j < numCols) { 45 | fn((i,j)); 46 | j += 1; 47 | } 48 | i += 1; 49 | } 50 | } 51 | 52 | override def union(other : IterableDomain[(Int,Int)]) : IterableDomain[(Int,Int)] = other match { 53 | case that : TableDomain => 54 | TableDomain(this.numRows max that.numRows, this.numCols max that.numCols); 55 | case _ => super.union(other); 56 | } 57 | 58 | override def toString = 59 | "TableDomain("+numRows+","+numCols+")"; 60 | 61 | override def equals(other : Any) = other match { 62 | case TableDomain(nr,nc) => this.numRows == nr && this.numCols == nc; 63 | case _ => super.equals(other); 64 | } 65 | } 66 | 67 | -------------------------------------------------------------------------------- /src/main/scala/scalala/tensor/domain/UnionDomain.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package tensor; 22 | package domain; 23 | 24 | /** 25 | * Trait that represents the union of two domains. 26 | * 27 | * @author dramage 28 | */ 29 | trait UnionDomainLike[@specialized(Int,Long) A, +This<:IterableDomain[A]] 30 | extends IterableDomainLike[A, This] { 31 | def a : IterableDomain[A]; 32 | def b : IterableDomain[A]; 33 | 34 | override def size = { 35 | var s = 0; 36 | foreach(k => { s += 1; }) 37 | s 38 | } 39 | 40 | override def foreach[O](fn : A=>O) = { 41 | a.foreach(fn); 42 | b.foreach(k => if (!a.contains(k)) fn(k)); 43 | } 44 | 45 | override def iterator = 46 | a.iterator ++ b.iterator.filterNot(a.contains); 47 | 48 | override def contains(key : A) : Boolean = 49 | a.contains(key) || b.contains(key); 50 | 51 | override def equals(other : Any) = other match { 52 | case that : UnionDomainLike[_,_] => this.a == that.a && this.b == that.b; 53 | case _ => super.equals(other); 54 | } 55 | } 56 | 57 | -------------------------------------------------------------------------------- /src/main/scala/scalala/tensor/generic/TensorBuilder.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package tensor; 22 | package generic; 23 | 24 | /** 25 | * Builds a Tensor of type To after being given a series of 26 | * key, value pairs. 27 | * 28 | * @author dramage 29 | */ 30 | trait TensorBuilder[@specialized(Int,Long) K, @specialized(Int,Long,Float,Double) V,+To] { 31 | def update(key : K, value : V); 32 | 33 | def result : To; 34 | } 35 | 36 | -------------------------------------------------------------------------------- /src/main/scala/scalala/tensor/generic/TensorKeysMonadic.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package tensor; 22 | package generic; 23 | 24 | import domain.CanGetDomain; 25 | import scalala.generic.collection._; 26 | 27 | /** 28 | * Support for comprehensions on keys from an underlying tensor. This 29 | * class can be implicitly viewed as an Iterable[K]. 30 | * 31 | * @author dramage 32 | */ 33 | trait TensorKeysMonadic 34 | [@specialized(Int,Long) K, @specialized(Int,Long,Float,Double) V, 35 | +This<:Tensor[K,V]] { 36 | 37 | def repr : This; 38 | 39 | /** Calls repr.foreachKey. */ 40 | def foreach[U](fn : K => U) = 41 | repr.foreachKey(fn); 42 | 43 | /** Calls repr.size. */ 44 | def size = 45 | repr.size; 46 | 47 | /** Calls repr.keysIterator. */ 48 | def iterator = 49 | repr.keysIterator; 50 | 51 | /** Constructs a filtered view of this tensor. */ 52 | def filter[D,That](p : K => Boolean) = 53 | withFilter(p); 54 | 55 | /** Constructs a filtered view of this tensor. */ 56 | def withFilter(p : K => Boolean) = 57 | new TensorKeysMonadic.Filtered[K,V,This](repr, p); 58 | } 59 | 60 | object TensorKeysMonadic { 61 | /** Filtered view of the keys in a Tensor. Does not support map. */ 62 | class Filtered 63 | [@specialized(Int,Long) K, @specialized(Int,Long,Float,Double) V, +This<:Tensor[K,V]] 64 | (val repr : This, p : K=>Boolean) { 65 | def foreach[U](fn : K => U) = 66 | repr.foreachKey(k => if (p(k)) fn(k)); 67 | 68 | def withFilter(q : K => Boolean) = 69 | new Filtered[K,V,This](repr, k => p(k) && q(k)); 70 | 71 | // def map[U,D,That](fn : K => U) 72 | // (implicit df : CanGetDomain[This,D], bf : CanBuildTensorFrom[This,D,K,U,That]) = { 73 | // val builder = bf(repr, repr.domain.asInstanceOf[D]); 74 | // repr.foreachPair((k,v) => if (p(k)) builder(k) = fn(k)); 75 | // builder.result; 76 | // } 77 | // 78 | // def strict[D,That] 79 | // (implicit df : CanGetDomain[This,D], bf : CanBuildTensorFrom[This,D,K,V,That]) = { 80 | // val builder = bf(repr, repr.domain.asInstanceOf[D]); 81 | // repr.foreachPair((k,v) => if (p(k)) builder(k) = v); 82 | // builder.result; 83 | // } 84 | } 85 | 86 | implicit def asIterable[K, V, T<:Tensor[K,V]](values : TensorKeysMonadic[K,V,T]) = { 87 | new Iterable[K] { 88 | def self = values.repr; 89 | override def foreach[U](fn : K => U) = self.foreachKey(fn); 90 | override def iterator = self.keysIterator; 91 | } 92 | } 93 | } 94 | 95 | -------------------------------------------------------------------------------- /src/main/scala/scalala/tensor/generic/TensorNonZeroKeysMonadic.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package tensor; 22 | package generic; 23 | 24 | import domain.CanGetDomain; 25 | import scalala.generic.collection._; 26 | 27 | /** 28 | * Support for comprehensions on all non-zero-valued keys (and some zeros) from 29 | * an underlying tensor. This class can be implicitly viewed as an Iterable[K]. 30 | * 31 | * @author dramage 32 | */ 33 | trait TensorNonZeroKeysMonadic 34 | [@specialized(Int,Long) K, @specialized(Int,Long,Float,Double) V, 35 | +This<:Tensor[K,V]] { 36 | 37 | def repr : This; 38 | 39 | /** Calls repr.foreachKey. */ 40 | def foreach[U](fn : K => U) = 41 | repr.foreachNonZeroKey(fn); 42 | 43 | /** Calls repr.nonzeroSize. */ 44 | def size = 45 | repr.nonzeroSize; 46 | 47 | /** Calls repr.keysIterator. */ 48 | def iterator = 49 | repr.keysIteratorNonZero; 50 | 51 | /** Constructs a filtered view of this tensor. */ 52 | def filter[D,That](p : K => Boolean) = 53 | withFilter(p); 54 | 55 | /** Constructs a filtered view of this tensor. */ 56 | def withFilter(p : K => Boolean) = 57 | new TensorNonZeroKeysMonadic.Filtered[K,V,This](repr, p); 58 | } 59 | 60 | object TensorNonZeroKeysMonadic { 61 | /** Filtered view of the keys in a Tensor. Does not support map. */ 62 | class Filtered 63 | [@specialized(Int,Long) K, @specialized(Int,Long,Float,Double) V, +This<:Tensor[K,V]] 64 | (val repr : This, p : K=>Boolean) { 65 | def foreach[U](fn : K => U) = 66 | repr.foreachNonZeroKey(k => if (p(k)) fn(k)); 67 | 68 | def withFilter(q : K => Boolean) = 69 | new Filtered[K,V,This](repr, k => p(k) && q(k)); 70 | } 71 | 72 | implicit def asIterable[K, V, T<:Tensor[K,V]](values : TensorNonZeroKeysMonadic[K,V,T]) = { 73 | new Iterable[K] { 74 | def self = values.repr; 75 | override def foreach[U](fn : K => U) = self.foreachNonZeroKey(fn); 76 | override def iterator = self.keysIteratorNonZero; 77 | } 78 | } 79 | } 80 | 81 | -------------------------------------------------------------------------------- /src/main/scala/scalala/tensor/generic/TensorNonZeroMonadic.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package tensor; 22 | package generic; 23 | 24 | import domain.CanGetDomain; 25 | import scalala.generic.collection._ 26 | import operators.HasValuesMonadic 27 | ; 28 | 29 | /** 30 | * For comprehensions on pairs of values from an underlying tensor. This 31 | * class can be implicitly viewed as a Map[K,V]. 32 | * 33 | * @author dramage 34 | */ 35 | trait TensorNonZeroMonadic 36 | [@specialized(Int,Long) K, @specialized(Int,Long,Float,Double) V, 37 | +This<:Tensor[K,V]] extends HasValuesMonadic[This,V] { self => 38 | 39 | /** Underlying tensor. */ 40 | def repr : This; 41 | 42 | /** Calls repr.nonzeroSize. */ 43 | def size = 44 | repr.nonzeroSize; 45 | 46 | /** Gets a Monadic for the nonzero pairs. */ 47 | def pairs : TensorNonZeroPairsMonadic[K,V,This] = 48 | new TensorNonZeroPairsMonadic[K,V,This] { override def repr = self.repr }; 49 | 50 | /** Gets a Monadic for the nonzero keys. */ 51 | def keys : TensorNonZeroKeysMonadic[K,V,This] = 52 | new TensorNonZeroKeysMonadic[K,V,This] { override def repr = self.repr }; 53 | 54 | /** Gets a Monadic for the nonzero values. */ 55 | def values : TensorNonZeroValuesMonadic[K,V,This] = 56 | new TensorNonZeroValuesMonadic[K,V,This] { override def repr = self.repr }; 57 | } 58 | 59 | -------------------------------------------------------------------------------- /src/main/scala/scalala/tensor/mutable/CRSTensor2.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package tensor; 22 | package mutable; 23 | 24 | import domain._; 25 | 26 | import scalala.scalar.Scalar; 27 | import scalala.generic.collection._ 28 | import tensor.Counter2.Curried 29 | import scala.collection.mutable.HashMap 30 | ; 31 | 32 | 33 | /** 34 | * A mutable Tensor2 with an open row key domain that maps to an arbitrary Tensor1 as its rows. 35 | * 36 | * CRS = Compress Row Storage 37 | * 38 | * @author dlwh 39 | */ 40 | trait CRSTensor2Like 41 | [K1, @specialized(Int,Long) K2, @specialized(Int,Long,Float,Double) V, 42 | +M1[VV]<:Curried[scala.collection.mutable.Map,K1]#Result[VV], 43 | +T<:scalala.tensor.mutable.Tensor1[K2,V], 44 | +This<:CRSTensor2[K1,K2,V,T]] 45 | extends tensor.CRSTensor2Like[K1,K2,V,M1,T,This] with Tensor2Like[K1,K2,V,SetDomain[K1], SetDomain[K2], Domain2[K1,K2], Domain2[K2,K1], This] { self => 46 | 47 | def update(k1 : K1, k2: K2, v : V) = 48 | innerGetOrElseUpdate(k1,data)(k2) = v; 49 | 50 | private[mutable] def innerGetOrElseUpdate[M](k:K1, m: scala.collection.mutable.Map[K1,M]): M = { 51 | m.getOrElseUpdate(k,m.default(k)) 52 | } 53 | } 54 | 55 | trait CRSTensor2 56 | [K1, @specialized(Int,Long) K2, @specialized(Int,Long,Float,Double) V, +T<:Tensor1[K2,V]] 57 | extends tensor.CRSTensor2[K1,K2,V,T] with Tensor2[K1,K2,V] 58 | with CRSTensor2Like[K1,K2, V,Curried[scala.collection.mutable.Map,K1]#Result,T,CRSTensor2[K1,K2,V,T]]; 59 | 60 | object CRSTensor2 { 61 | class Impl[K1, @specialized(Int,Long) K2, @specialized(Int,Long,Float,Double) V, T<:Tensor1[K2,V]] 62 | (override val data : scala.collection.mutable.Map[K1,T], 63 | override val k2domain: Domain1[K2]) 64 | (implicit override val scalar : Scalar[V], zeros: CanCreateZerosLike[T,T]) 65 | extends CRSTensor2[K1,K2,V,T]; 66 | 67 | /** 68 | * Returns a new empty CRSTensor2 using the "template" to create rows. The actual rows are zero'd out versions of 69 | * the template. 70 | */ 71 | def apply[K1,K2,V,T<:Tensor1[K2,V]](template: T)(implicit view: CanViewAsTensor1[T,K2,V], 72 | zeros: CanCreateZerosLike[T,T], 73 | scalar: Scalar[V]) : mutable.CRSTensor2[K1,K2,V,T] = { 74 | val map = new HashMap[K1,T] { 75 | override def default(k: K1) = zeros(template); 76 | } 77 | new Impl[K1,K2,V,T](map, template.domain); 78 | } 79 | 80 | implicit def canSliceRow[K1,K2,V,T<:Tensor1[K2,V]] 81 | : CanSliceRow[CRSTensor2[K1,K2,V,T],K1,T] 82 | = new CanSliceRow[CRSTensor2[K1,K2,V,T],K1,T] { 83 | override def apply(from : CRSTensor2[K1,K2,V,T], row : K1) = from.innerGetOrElseUpdate(row,from.data); 84 | } 85 | 86 | 87 | } 88 | 89 | -------------------------------------------------------------------------------- /src/main/scala/scalala/tensor/mutable/MatrixTranspose.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package tensor; 22 | package mutable; 23 | 24 | import domain.{IndexDomain,TableDomain}; 25 | 26 | import scalala.scalar.Scalar; 27 | import scalala.generic.collection._; 28 | import scalala.operators._; 29 | 30 | /** 31 | * A Transpose of any Matrix type is a Matrix. 32 | * 33 | * @author dramage 34 | */ 35 | trait MatrixTransposeLike 36 | [@specialized(Int,Long,Float,Double) V, +Coll <: Matrix[V], +This <: MatrixTranspose[V,Coll]] 37 | extends tensor.MatrixTransposeLike[V,Coll,This] 38 | with Tensor2TransposeLike[Int,Int,V,IndexDomain,IndexDomain,TableDomain,TableDomain,Coll,This] 39 | with MatrixLike[V,This] { 40 | override def domain = underlying.domain.transpose.asInstanceOf[TableDomain]; 41 | 42 | override def t : Coll = 43 | underlying; 44 | } 45 | 46 | /** 47 | * A Transpose of any Matrix type is a Matrix. 48 | * 49 | * @author dramage 50 | */ 51 | trait MatrixTranspose[@specialized(Int,Long,Float,Double) V, +Coll <: Matrix[V]] 52 | extends tensor.MatrixTranspose[V,Coll] 53 | with Tensor2Transpose[Int,Int,V,Coll] 54 | with Matrix[V] with MatrixTransposeLike[V, Coll, MatrixTranspose[V, Coll]]; 55 | 56 | object MatrixTranspose { 57 | class Impl[V, +Coll <: Matrix[V]] 58 | (override val underlying : Coll) 59 | (implicit override val scalar : Scalar[V]) 60 | extends MatrixTranspose[V,Coll]; 61 | } 62 | 63 | -------------------------------------------------------------------------------- /src/main/scala/scalala/tensor/mutable/Tensor1.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package tensor; 22 | package mutable; 23 | 24 | import domain.Domain1; 25 | 26 | import scalala.scalar.Scalar; 27 | 28 | /** 29 | * Implementation trait for mutable Tensor1 instances. 30 | * 31 | * @author dramage 32 | */ 33 | trait Tensor1Like 34 | [@specialized(Int,Long) K, @specialized(Int,Long,Float,Double) V, 35 | +D<:Domain1[K], +This<:Tensor1[K,V]] 36 | extends tensor.Tensor1Like[K,V,D,This] with TensorLike[K,V,D,This]; 37 | 38 | /** 39 | * Mutable tensor.Tensor1. 40 | * 41 | * @author dramage 42 | */ 43 | trait Tensor1 44 | [@specialized(Int,Long) K, @specialized(Int,Long,Float,Double) V] 45 | extends tensor.Tensor1[K,V] with Tensor[K,V] 46 | with Tensor1Like[K,V,Domain1[K],Tensor1[K,V]]; 47 | 48 | object Tensor1 { 49 | /** Constructs a closed-domain tensor for the given domain. */ 50 | def apply[K,V:Scalar](domain : Domain1[K]) = 51 | Tensor1Col(domain); 52 | } 53 | 54 | -------------------------------------------------------------------------------- /src/main/scala/scalala/tensor/mutable/Tensor1Col.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package tensor; 22 | package mutable; 23 | 24 | import domain.{Domain1,IndexDomain}; 25 | 26 | import scalala.scalar.Scalar; 27 | 28 | /** 29 | * Implementation trait for mutable Tensor1Col instances. 30 | * 31 | * @author dramage 32 | */ 33 | trait Tensor1ColLike 34 | [@specialized(Int,Long) K, @specialized(Int,Long,Float,Double) V, 35 | +D<:Domain1[K], +This<:Tensor1Col[K,V]] 36 | extends tensor.Tensor1ColLike[K,V,D,This] with Tensor1Like[K,V,D,This] { 37 | 38 | override def t : Tensor1Row[K,V] = 39 | new Tensor1Row.View[K,V](repr); 40 | } 41 | 42 | /** 43 | * Mutable tensor.Tensor1Col. 44 | * 45 | * @author dramage 46 | */ 47 | trait Tensor1Col 48 | [@specialized(Int,Long) K, @specialized(Int,Long,Float,Double) V] 49 | extends tensor.Tensor1Col[K,V] with Tensor1[K,V] 50 | with Tensor1ColLike[K,V,Domain1[K],Tensor1Col[K,V]]; 51 | 52 | object Tensor1Col { 53 | /** Constructs a closed-domain tensor for the given domain. */ 54 | def apply[K,V:Scalar](domain : Domain1[K]) : Tensor1Col[K,V] = domain match { 55 | case d : IndexDomain => VectorCol(d); 56 | case _ => new Impl(domain, scala.collection.mutable.Map[K,V]()); 57 | } 58 | 59 | class Impl[K,V:Scalar]( 60 | override val domain : Domain1[K], 61 | override protected val data : scala.collection.mutable.Map[K,V]) 62 | extends Tensor.Impl[K,V](domain, data) with Tensor1Col[K,V]; 63 | 64 | class View[K,V](override val inner : Tensor1Row[K,V]) 65 | extends Tensor1Proxy[K,V,Tensor1Row[K,V]] with Tensor1Col[K,V] 66 | with Tensor1Like[K,V,Domain1[K],View[K,V]] { 67 | override def repr : View[K,V] = this; 68 | override def t : Tensor1Row[K,V] = inner; 69 | } 70 | } 71 | 72 | -------------------------------------------------------------------------------- /src/main/scala/scalala/tensor/mutable/Tensor1Row.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package tensor; 22 | package mutable; 23 | 24 | import domain.{Domain1,IndexDomain}; 25 | 26 | import scalala.generic.collection.{CanSliceVector}; 27 | 28 | import scalala.scalar.Scalar; 29 | 30 | /** 31 | * Implementation trait for mutable Tensor1Row instances. 32 | * 33 | * @author dramage 34 | */ 35 | trait Tensor1RowLike 36 | [@specialized(Int,Long) K, @specialized(Int,Long,Float,Double) V, 37 | +D<:Domain1[K], +This<:Tensor1Row[K,V]] 38 | extends tensor.Tensor1RowLike[K,V,D,This] with Tensor1Like[K,V,D,This] { 39 | 40 | override def t : Tensor1Col[K,V] = 41 | new Tensor1Col.View[K,V](repr); 42 | } 43 | 44 | /** 45 | * Mutable tensor.Tensor1Row. 46 | * 47 | * @author dramage 48 | */ 49 | trait Tensor1Row 50 | [@specialized(Int,Long) K, @specialized(Int,Long,Float,Double) V] 51 | extends tensor.Tensor1Row[K,V] with Tensor1[K,V] 52 | with Tensor1RowLike[K,V,Domain1[K],Tensor1Row[K,V]]; 53 | 54 | object Tensor1Row { 55 | /** Constructs a closed-domain tensor for the given domain. */ 56 | def apply[K,V:Scalar](domain : Domain1[K]) : Tensor1Row[K,V] = domain match { 57 | case d : IndexDomain => VectorRow(d); 58 | case _ => new Impl(domain, scala.collection.mutable.Map[K,V]()); 59 | } 60 | 61 | class Impl[K,V:Scalar]( 62 | override val domain : Domain1[K], 63 | override protected val data : scala.collection.mutable.Map[K,V]) 64 | extends Tensor.Impl[K,V](domain, data) with Tensor1Row[K,V]; 65 | 66 | class View[K,V](override val inner : Tensor1Col[K,V]) 67 | extends Tensor1Proxy[K,V,Tensor1Col[K,V]] with Tensor1Row[K,V] 68 | with Tensor1Like[K,V,Domain1[K],View[K,V]] { 69 | override def repr : View[K,V] = this; 70 | override def t : Tensor1Col[K,V] = inner; 71 | } 72 | 73 | implicit def canSliceVectorRow[K, V:Scalar] = 74 | new CanSliceVector[Tensor[K,V], K, VectorRow[V]] { 75 | override def apply(from : Tensor[K,V], keys : Seq[K]) = 76 | new VectorRowSlice.FromKeySeq[K,V,Tensor[K,V]](from, keys); 77 | } 78 | } 79 | 80 | -------------------------------------------------------------------------------- /src/main/scala/scalala/tensor/mutable/Tensor1Slice.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package tensor; 22 | package mutable; 23 | 24 | import domain.{IterableDomain,Domain1}; 25 | 26 | import scalala.scalar.Scalar; 27 | 28 | /** 29 | * Implementation trait for a Tensor1 view of a slice of keys from a Tensor. 30 | * 31 | * @author dramage 32 | */ 33 | trait Tensor1SliceLike 34 | [@specialized(Int,Long) K1, +D1<:IterableDomain[K1], 35 | @specialized(Int,Long) K2, +D2<:Domain1[K2], 36 | @specialized(Int,Long,Float,Double,Boolean) V, +Coll<:Tensor[K1,V], 37 | +This<:Tensor1Slice[K1,K2,V,Coll]] 38 | extends tensor.Tensor1SliceLike[K1,D1,K2,D2,V,Coll,This] 39 | with TensorSliceLike[K1, D1, K2, D2, V, Coll, This] 40 | with Tensor1Like[K2, V, D2, This]; 41 | 42 | /** 43 | * K Tensor1 view of a slice of keys from a Tensor. 44 | * 45 | * @author dramage 46 | */ 47 | trait Tensor1Slice 48 | [@specialized(Int,Long) K1, @specialized(Int,Long) K2, 49 | @specialized(Int,Long,Float,Double,Boolean) V, +Coll<:Tensor[K1,V]] 50 | extends tensor.Tensor1Slice[K1,K2,V,Coll] 51 | with TensorSlice[K1,K2,V,Coll] with Tensor1[K2,V] 52 | with Tensor1SliceLike[K1, IterableDomain[K1], K2, Domain1[K2], V, Coll, Tensor1Slice[K1, K2, V, Coll]]; 53 | 54 | object Tensor1Slice { 55 | /** This view is a no-op but is needed for correct implicit resolution. */ 56 | implicit def asTensor1[K,V](slice : Tensor1Slice[K,_,V,_]) : Tensor1[K,V] = 57 | slice; 58 | } 59 | 60 | -------------------------------------------------------------------------------- /src/main/scala/scalala/tensor/mutable/Tensor2Transpose.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package tensor; 22 | package mutable; 23 | 24 | import domain.{Domain1,Domain2}; 25 | 26 | import scalala.scalar.Scalar; 27 | import scalala.generic.collection._; 28 | import scalala.operators._; 29 | 30 | /** 31 | * Implementation trait for a transposed view of an underlying MutableTensor2. 32 | * 33 | * @author dramage 34 | */ 35 | trait Tensor2TransposeLike 36 | [@specialized(Int) K2, @specialized(Int) K1, 37 | @specialized(Int,Long,Float,Double,Boolean) V, 38 | +D2<:Domain1[K2], +D1<:Domain1[K1], +T<:Domain2[K2,K1], +D<:Domain2[K1,K2], 39 | +Coll<:Tensor2[K1,K2,V], 40 | +This<:Tensor2Transpose[K2,K1,V,Coll]] 41 | extends tensor.Tensor2TransposeLike[K2,K1,V,D2,D1,T,D,Coll,This] 42 | with TensorSliceLike[(K1,K2),D,(K2,K1),T,V,Coll,This] 43 | with Tensor2Like[K2,K1,V,D2,D1,T,D,This] { 44 | self => 45 | 46 | override def update(i : K2, j : K1, value : V) = 47 | underlying.update(j, i, value); 48 | 49 | override def t : Coll = 50 | underlying; 51 | } 52 | 53 | /** 54 | * Transposed view of an undelrying MutableTensor2. 55 | * 56 | * @author dramage 57 | */ 58 | trait Tensor2Transpose 59 | [@specialized(Int) K2, @specialized(Int) K1, 60 | @specialized(Int,Long,Float,Double,Boolean) V, 61 | +Coll <: Tensor2[K1,K2,V]] 62 | extends tensor.Tensor2Transpose[K2,K1,V,Coll] 63 | with TensorSlice[(K1,K2),(K2,K1),V,Coll] 64 | with Tensor2[K2,K1,V] 65 | with Tensor2TransposeLike[K2,K1,V,Domain1[K2],Domain1[K1],Domain2[K2,K1],Domain2[K1,K2],Coll,Tensor2Transpose[K2,K1,V,Coll]]; 66 | 67 | object Tensor2Transpose { 68 | /** Default implementation. */ 69 | class Impl[K2, K1, V:Scalar, +Coll <: Tensor2[K1,K2,V]](underlying : Coll) 70 | extends tensor.Tensor2Transpose.Impl[K2,K1,V,Coll](underlying) 71 | with Tensor2Transpose[K2,K1,V,Coll]; 72 | } 73 | 74 | -------------------------------------------------------------------------------- /src/main/scala/scalala/tensor/mutable/TensorSlice.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package tensor; 22 | package mutable; 23 | 24 | import domain._; 25 | 26 | import scalala.scalar.Scalar; 27 | 28 | /** 29 | * Implementation trait for slices of an underlying Tensor. A slice 30 | * is a pass-through view of a (mapped) subset of the original Tensor's 31 | * domain. 32 | * 33 | * @author dramage 34 | */ 35 | trait TensorSliceLike 36 | [@specialized(Int,Long) K1, +D1<:IterableDomain[K1], 37 | @specialized(Int,Long) K2, +D2<:IterableDomain[K2], 38 | @specialized(Int,Long,Float,Double,Boolean) V, 39 | +Coll<:Tensor[K1,V], 40 | +This<:TensorSlice[K1,K2,V,Coll]] 41 | extends tensor.TensorSliceLike[K1,D1,K2,D2,V,Coll,This] 42 | with TensorLike[K2,V,D2,This] { 43 | 44 | override def update(key : K2, value : V) = 45 | underlying.update(lookup(key), value); 46 | } 47 | 48 | /** 49 | * Pass-through view of a (key-mapped) subset of an underlying Tensor. 50 | * 51 | * @author dramage 52 | */ 53 | trait TensorSlice 54 | [@specialized(Int,Long) K1, @specialized(Int,Long) K2, 55 | @specialized(Int,Long,Float,Double,Boolean) V, +Coll <: Tensor[K1, V]] 56 | extends tensor.TensorSlice[K1,K2,V,Coll] with Tensor[K2,V] 57 | with TensorSliceLike[K1, IterableDomain[K1], K2, IterableDomain[K2], V, Coll, TensorSlice[K1, K2, V, Coll]]; 58 | 59 | 60 | object TensorSlice { 61 | class FromKeyMap[K1, K2, V:Scalar, +Coll<:Tensor[K1, V]] 62 | (underlying : Coll, keymap : scala.collection.Map[K2,K1]) 63 | extends tensor.TensorSlice.FromKeyMap[K1,K2,V,Coll](underlying, keymap) 64 | with TensorSlice[K1,K2,V,Coll]; 65 | } 66 | 67 | -------------------------------------------------------------------------------- /src/main/scala/scalala/tensor/mutable/VectorCol.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package tensor; 22 | package mutable; 23 | 24 | import domain.IndexDomain; 25 | 26 | import scalala.scalar.Scalar; 27 | import scalala.generic.collection._; 28 | import scalala.operators._; 29 | 30 | /** 31 | * Implementation trait for mutable VectorCol instances. 32 | * 33 | * @author dramage 34 | */ 35 | trait VectorColLike[@specialized(Int,Long,Float,Double) V, +This<:VectorCol[V]] 36 | extends tensor.VectorColLike[V,This] 37 | with Tensor1ColLike[Int,V,IndexDomain,This] with VectorLike[V,This] { 38 | 39 | override def t : VectorRow[V] = 40 | new VectorRow.View[V](repr); 41 | } 42 | 43 | /** 44 | * Mutable tensor.VectorCol. 45 | * 46 | * @author dramage 47 | */ 48 | trait VectorCol[@specialized(Int,Long,Float,Double) V] 49 | extends tensor.VectorCol[V] with Tensor1Col[Int,V] with Vector[V] 50 | with VectorColLike[V,VectorCol[V]]; 51 | 52 | object VectorCol { 53 | def apply[V:Scalar](domain : IndexDomain) = 54 | dense.DenseVectorCol[V](domain); 55 | 56 | class View[V](override val inner : Vector[V]) 57 | extends VectorProxy[V,Vector[V]] with tensor.VectorProxy[V,Vector[V]] with VectorCol[V] with VectorLike[V,View[V]] { 58 | override def repr : View[V] = this; 59 | } 60 | } 61 | 62 | -------------------------------------------------------------------------------- /src/main/scala/scalala/tensor/mutable/VectorRow.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package tensor; 22 | package mutable; 23 | 24 | import domain.IndexDomain; 25 | 26 | import scalala.scalar.Scalar; 27 | import scalala.generic.collection._; 28 | import scalala.operators._; 29 | 30 | /** 31 | * Implementation trait for mutable VectorRow instances. 32 | * 33 | * @author dramage 34 | */ 35 | trait VectorRowLike[@specialized(Int,Long,Float,Double) V, +This<:VectorRow[V]] 36 | extends tensor.VectorRowLike[V,This] 37 | with Tensor1RowLike[Int,V,IndexDomain,This] with VectorLike[V,This] { 38 | override def t : VectorCol[V] = 39 | new VectorCol.View[V](repr); 40 | } 41 | 42 | /** 43 | * Mutable tensor.VectorRow. 44 | * 45 | * @author dramage 46 | */ 47 | trait VectorRow[@specialized(Int,Long,Float,Double) V] 48 | extends tensor.VectorRow[V] with Tensor1Row[Int,V] with Vector[V] 49 | with VectorRowLike[V,VectorRow[V]]; 50 | 51 | object VectorRow { 52 | def apply[V:Scalar](domain : IndexDomain) = 53 | dense.DenseVectorRow[V](domain); 54 | 55 | class View[V](override val inner : Vector[V]) 56 | extends VectorProxy[V,Vector[V]] with tensor.VectorProxy[V,Vector[V]] with VectorRow[V] with VectorLike[V,View[V]] { 57 | override def repr : View[V] = this; 58 | } 59 | } 60 | 61 | -------------------------------------------------------------------------------- /src/main/scala/scalala/tensor/package.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | 22 | package object tensor { 23 | } 24 | -------------------------------------------------------------------------------- /src/main/scala/scalala/tensor/sparse/SparseArrayTensor.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package tensor; 22 | package sparse; 23 | 24 | import domain.{DomainLike,IterableDomain}; 25 | 26 | import scalala.collection.sparse.SparseArray; 27 | import scalala.scalar.Scalar; 28 | 29 | /** 30 | * Implementation trait for a tensor backed by a sparse array of values. 31 | * 32 | * @author dramage 33 | */ 34 | trait SparseArrayTensorLike 35 | [@specialized(Int,Long) A, @specialized(Int,Long,Float,Double,Boolean) B, 36 | +D<:IterableDomain[A] with DomainLike[A,D], 37 | +This<:SparseArrayTensor[A,B]] 38 | extends mutable.TensorLike[A,B,D,This] { 39 | def data : SparseArray[B]; 40 | 41 | override def nonzeroSize = 42 | data.activeLength; 43 | 44 | /** Assigns the given value to all elements of this map. */ 45 | def :=(value : B) = { 46 | if (value == data.default) { 47 | data.clear; 48 | } else { 49 | var i = 0; 50 | while (i < data.length) { 51 | data(i) = value; 52 | i += 1; 53 | } 54 | } 55 | } 56 | 57 | /** Tranforms all values in this map by applying the given function. */ 58 | override def transformValues(f : B=>B) = 59 | data.transform(f); 60 | } 61 | 62 | /** 63 | * Mutable tensor by a sparse array of values. 64 | * 65 | * @author dramage 66 | */ 67 | trait SparseArrayTensor 68 | [@specialized(Int,Long) A, @specialized(Int,Long,Float,Double,Boolean) B] 69 | extends mutable.Tensor[A,B] 70 | with SparseArrayTensorLike[A,B,IterableDomain[A],SparseArrayTensor[A,B]]; 71 | -------------------------------------------------------------------------------- /src/test/scala/scalala/collection/sparse/SparseArrayTest.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala.collection.sparse; 21 | 22 | import org.scalacheck._ 23 | import org.scalatest._; 24 | import org.scalatest.junit._; 25 | import org.scalatest.prop._; 26 | import org.junit.runner.RunWith 27 | 28 | @RunWith(classOf[JUnitRunner]) 29 | class SparseArrayTest extends FunSuite with Checkers { 30 | test("Map") { 31 | val x = SparseArray(1,0,2,0,3,0,-1,-2,-3); 32 | x.compact; 33 | val y = x.map(_ + 1); 34 | assert(x.length === y.length); 35 | assert(x.activeLength === x.length - 3); 36 | assert(y.activeLength === y.length - 1); 37 | assert(y.toList === List(2,1,3,1,4,1,0,-1,-2)); 38 | } 39 | 40 | test("Filter") { 41 | val x = SparseArray(1,0,2,0,3,0,-1,-2,-3); 42 | x.compact; 43 | assert(x.filter(_ % 2 == 1).toList === List(1,3)); 44 | assert(x.filter(_ % 2 == 1).activeLength === 2); 45 | assert(x.filter(_ % 2 == 0).toList === List(0,2,0,0,-2)); 46 | assert(x.filter(_ % 2 == 0).activeLength === 2); 47 | assert(x.filter(_ > 0).toList === List(1,2,3)); 48 | assert(x.filter(_ > 0).activeLength === 3); 49 | assert(x.filter(_ >= 0).toList === List(1,0,2,0,3,0)); 50 | assert(x.filter(_ >= 0).activeLength === 3); 51 | 52 | val y = SparseArray(0,1,0,0,-1,-2,-3,-5); 53 | y.compact; 54 | assert(y.filter(_ > 0).toList === List(1)); 55 | assert(y.filter(_ >= 0).toList === List(0,1,0,0)); 56 | } 57 | } 58 | 59 | -------------------------------------------------------------------------------- /src/test/scala/scalala/generic/CanAssignIntoTest.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package generic; 22 | 23 | import org.scalacheck._ 24 | import org.scalatest._; 25 | import org.scalatest.junit._; 26 | import org.scalatest.prop._; 27 | import org.junit.runner.RunWith 28 | 29 | @RunWith(classOf[JUnitRunner]) 30 | class CanAssignIntoTest extends FunSuite with Checkers { 31 | 32 | import operators.Implicits._; 33 | 34 | test("Array[Int]") { 35 | val a = Array(0,0,0); 36 | val aRef = a; 37 | 38 | val b = Array(1,2,3); 39 | val bRef = b; 40 | 41 | a := b; 42 | assert(a.toList === b.toList); 43 | assert(!(a eq b)); 44 | assert((a eq aRef)); 45 | assert((b eq bRef)); 46 | } 47 | 48 | test("Array[Array[Int]]") { 49 | val a = Array(Array(0,0),Array(0,0),Array(0,0)); 50 | val aRef = a; 51 | 52 | val b = Array(Array(1,2),Array(3,4),Array(5,6)); 53 | val bRef = b; 54 | 55 | a := b; 56 | assert(a.map(_.toList).toList === b.map(_.toList).toList); 57 | assert(!(a eq b)); 58 | assert((a zip b).forall(tup => !(tup._1 eq tup._2))); 59 | assert((a eq aRef)); 60 | assert((b eq bRef)); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/test/scala/scalala/generic/collection/CanGetDoubleTest.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package generic; 22 | package collection; 23 | 24 | import org.scalacheck._ 25 | import org.scalatest._; 26 | import org.scalatest.junit._; 27 | import org.scalatest.prop._; 28 | import org.junit.runner.RunWith; 29 | 30 | @RunWith(classOf[JUnitRunner]) 31 | class CanGetDoubleTest extends FunSuite with Checkers { 32 | def get[Coll,K](coll : Coll, k : K)(implicit get : CanGetDouble[Coll,K]) = 33 | get(coll, k); 34 | 35 | test("array") { 36 | assert(get(Array(1,2,3),1) === 2.0); 37 | } 38 | 39 | test("map") { 40 | assert(get(Map('a'->1,'b'->2), 'b') === 2.0); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/test/scala/scalala/generic/collection/CanGetValueTest.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package generic; 22 | package collection; 23 | 24 | import org.scalacheck._ 25 | import org.scalatest._; 26 | import org.scalatest.junit._; 27 | import org.scalatest.prop._; 28 | import org.junit.runner.RunWith; 29 | 30 | @RunWith(classOf[JUnitRunner]) 31 | class CanGetValueTest extends FunSuite with Checkers { 32 | def get[Coll,K,V](coll : Coll, k : K)(implicit get : CanGetValue[Coll,K,V]) = 33 | get(coll, k); 34 | 35 | test("array") { 36 | assert(get(Array(1.0,2.0,3.0),1) === 2.0); 37 | } 38 | 39 | test("map") { 40 | assert(get(Map('a'->1,'b'->2), 'b') === 2); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/test/scala/scalala/generic/collection/CanMapValuesTest.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package generic; 22 | package collection; 23 | 24 | import org.scalacheck._ 25 | import org.scalatest._; 26 | import org.scalatest.junit._; 27 | import org.scalatest.prop._; 28 | import org.junit.runner.RunWith; 29 | 30 | @RunWith(classOf[JUnitRunner]) 31 | class CanMapValuesTest extends FunSuite with Checkers { 32 | def toDoubles[From,A,To](coll : From)(implicit map : CanMapValues[From,A,Double,To], cv : A=>Double) = 33 | map.map(coll, cv); 34 | 35 | test("array") { 36 | assert(toDoubles(Array(1,2,3)).toList === List(1.0,2.0,3.0)); 37 | } 38 | 39 | test("map") { 40 | assert(toDoubles(scala.collection.Map('a'->2,'b'->3)) === Map('a'->2.0,'b'->3.0)); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/test/scala/scalala/library/NumericsTest.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala.library 21 | 22 | import org.scalatest._ 23 | import org.scalatest.junit._ 24 | import org.scalatest.prop._ 25 | import org.scalatest.matchers.ShouldMatchers 26 | import org.junit.runner.RunWith 27 | import Library._ 28 | import Numerics._ 29 | import org.scalacheck.{Prop, Arbitrary} 30 | 31 | @RunWith(classOf[JUnitRunner]) 32 | class NumericsTest extends FunSuite with Checkers with ShouldMatchers { 33 | 34 | test("logSum") { 35 | logSum(log(5), log(2)) should be (log(7) plusOrMinus 1e-10) 36 | logSum(log(2), log(5)) should be (log(7) plusOrMinus 1e-10) 37 | logSum(Double.NegativeInfinity, log(5)) should be (log(5) plusOrMinus 1e-10) 38 | logSum(log(5), Double.NegativeInfinity) should be (log(5) plusOrMinus 1e-10) 39 | logSum(Double.NegativeInfinity, Double.NegativeInfinity) should be (Double.NegativeInfinity) 40 | 41 | logSum(log(1), log(2), log(3)) should be (log(6) plusOrMinus 1e-10) 42 | logSum(log(1), log(2), Double.NegativeInfinity) should be (log(3) plusOrMinus(1e-10)) 43 | 44 | val s = Array.tabulate[Double](5)(i => log1p(i)) 45 | logSum(s.iterator, s.max) should be (log(15) plusOrMinus 1e-10) 46 | logSum(s) should be (log(15) plusOrMinus 1e-10) 47 | logSum(Double.NegativeInfinity +: s) should be (log(15) plusOrMinus 1e-10) 48 | logSum(s :+ Double.NegativeInfinity) should be (log(15) plusOrMinus 1e-10) 49 | } 50 | 51 | test("logDiff") { 52 | logDiff(log(5), log(2)) should be (log(3) plusOrMinus 1e-10) 53 | logDiff(log(5), log(5)) should be (Double.NegativeInfinity) 54 | 55 | evaluating { 56 | logDiff(log(5), log(6)) 57 | } should produce [IllegalArgumentException] 58 | } 59 | 60 | import Arbitrary._; 61 | implicit def ae(x: Double) = new { 62 | def =~=(y: Double) = (x-y).abs/x < 1E-6; 63 | } 64 | 65 | // TODO 2.9 filter out Double.MaxValue. 66 | /*test("logsumming is approximately associative") { 67 | check(Prop.forAll { (a: Double, b:Double, c: Double) => 68 | logSum(a,logSum(b,c)) =~= logSum(logSum(a,b),c); 69 | }) 70 | check(Prop.forAll { (a: Double, b:Double, c: Double) => 71 | logSum(a,logSum(b,c)) =~= logSum(a,b,c); 72 | }) 73 | } 74 | 75 | test("sum distributes over logsum") { 76 | check(Prop.forAll { (a: Double, b:Double, c: Double) => 77 | (a + logSum(b,c)) =~= (logSum(a + b,a+c)); 78 | }) 79 | }*/ 80 | 81 | } 82 | -------------------------------------------------------------------------------- /src/test/scala/scalala/library/RandomTest.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala.library 21 | 22 | import org.scalatest._ 23 | import org.scalatest.junit._ 24 | import org.scalatest.prop._ 25 | import org.junit.runner.RunWith 26 | import scalala.library.Random._ 27 | import scalala.library.Library._ 28 | import scalala.tensor.dense._ 29 | 30 | @RunWith(classOf[JUnitRunner]) 31 | class RandomTest extends FunSuite with Checkers { 32 | 33 | test("MultivariateGaussian") { 34 | // specify rng explicitly so that we don't ever fail this test 35 | implicit val mt = new random.MersenneTwisterFast(0l); 36 | 37 | val Sigma = DenseMatrix((3., 4.), (4., 16.)) 38 | val mu = DenseVector(77.,-3.) 39 | val X = DenseMatrix.randn(mu, Sigma, 50000)(mt) 40 | 41 | val (chkSigma, chkMu) = covariance(X, Axis.Horizontal) 42 | assert(chkMu forallPairs ( (i,v) => math.abs(v-mu(i)) < 1e-1 ),chkMu) 43 | assert(chkSigma forallPairs ( (idx,v) => math.abs(v-Sigma(idx)) < 1e-1 ), chkSigma) 44 | } 45 | } 46 | 47 | -------------------------------------------------------------------------------- /src/test/scala/scalala/library/StatisticsTest.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala.library 21 | 22 | import org.scalatest._; 23 | import org.scalatest.matchers.ShouldMatchers; 24 | import org.scalatest.junit._; 25 | import org.scalatest.prop._; 26 | import org.junit.runner.RunWith; 27 | 28 | import Statistics._ 29 | import scalala.tensor.mutable.{Matrix, Vector} 30 | 31 | @RunWith(classOf[JUnitRunner]) 32 | class StatisticsTest extends FunSuite with Checkers with ShouldMatchers { 33 | test("corrcoef") { 34 | corrcoef(Vector(1,2,3), Vector(2,3,3.4)) should be (0.97072 plusOrMinus 1e-5); 35 | assert(corrcoef(Vector[Double](), Vector[Double]()).isNaN); 36 | } 37 | 38 | test("kendall") { 39 | val a = Vector(2.5,2.5,2.5,2.5,5,6.5,6.5,10,10,10,10,10,14,14,14,16,17); 40 | val b = Vector(1,1,1,1,2,1,1,2,1,1,1,1,1,1,2,2,2); 41 | kendall(a,b) should be (0.40754 plusOrMinus 1e-5); 42 | 43 | val x = Vector(1.5,1.5,3,4,6,6,6,8,9.5,9.5,11,12); 44 | val y = Vector(2.5,2.5,7,4.5,1,4.5,6,11.5,11.5,8.5,8.5,10); 45 | kendall(x,y) should be (0.55286 plusOrMinus 1e-5); 46 | } 47 | } 48 | 49 | -------------------------------------------------------------------------------- /src/test/scala/scalala/library/StorageTest.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | 21 | package scalala.library 22 | 23 | import org.scalatest._ 24 | import org.scalatest.junit._ 25 | import org.scalatest.prop._ 26 | import org.scalatest.matchers.ShouldMatchers 27 | import org.junit.runner.RunWith 28 | import scalala.tensor.dense.DenseMatrix 29 | import java.io.{ByteArrayInputStream, ByteArrayOutputStream} 30 | 31 | @RunWith(classOf[JUnitRunner]) 32 | class StorageTest extends FunSuite with Checkers with ShouldMatchers { 33 | 34 | test("StoreTxtAndLoadTxt") { 35 | val os = new ByteArrayOutputStream 36 | 37 | val m = DenseMatrix.randn(5,5) 38 | Storage.storetxt(os, m) 39 | val n = Storage.loadtxt(new ByteArrayInputStream(os.toByteArray)) 40 | m foreachPair ((idx,v) => n(idx) should be (v plusOrMinus 1e-15)) 41 | 42 | os.reset() 43 | Storage.storetxt(os, m) 44 | val o = Storage.loadtxt(new ByteArrayInputStream(os.toByteArray), skipRows=2) 45 | o.numRows should be (m.numRows-2) 46 | o foreachPair ((idx,v) => m(idx._1+2,idx._2) should be (v plusOrMinus 1e-15)) 47 | 48 | os.reset() 49 | Storage.storetxt(os, m) 50 | val p = Storage.loadtxt(new ByteArrayInputStream(os.toByteArray), columns=Seq(1,3)) 51 | p.numCols should be (2) 52 | p foreachPair { 53 | case ((row,0), v) => m(row,1) should be (v plusOrMinus 1e-15) 54 | case ((row,1), v) => m(row,3) should be (v plusOrMinus 1e-15) 55 | case _ => true should be (false) // signal failure 56 | } 57 | 58 | val is = new ByteArrayInputStream("1.0 2.0 #test\n#foo\n3.0 4.0".getBytes) 59 | val r = Storage.loadtxt(is) 60 | r.numRows should be (2) 61 | r.numCols should be (2) 62 | r(0,0) should be (1.0) 63 | r(0,1) should be (2.0) 64 | r(1,0) should be (3.0) 65 | r(1,1) should be (4.0) 66 | } 67 | 68 | } 69 | -------------------------------------------------------------------------------- /src/test/scala/scalala/operators/ArrayTest.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package operators; 22 | 23 | import org.scalacheck._ 24 | import org.scalatest._; 25 | import org.scalatest.junit._; 26 | import org.scalatest.prop._; 27 | import org.junit.runner.RunWith 28 | 29 | import Implicits._; 30 | 31 | @RunWith(classOf[JUnitRunner]) 32 | class ArrayTest extends FunSuite with Checkers { 33 | 34 | def mk[V:ClassManifest](values : V*) = 35 | Array[V](values :_*); 36 | 37 | test("Negation") { 38 | val a = mk(1,2,3); 39 | assert((-a).toList === List(-1,-2,-3)); 40 | } 41 | 42 | test("Collection Ops") { 43 | val a = mk(1,2,3); 44 | val b = mk(4,5,6); 45 | 46 | assert((a + b).toList === List(5,7,9)); 47 | assert((a - b).toList === List(-3,-3,-3)); 48 | assert((a :* b).toList === List(4,10,18)); 49 | assert((b :/ a).toList === List(4,2,2)); 50 | assert((a :/ b.map(_.toDouble)).toList === List(0.25,0.4,0.5)); 51 | assert((b :% a).toList === List(0,1,0)); 52 | } 53 | 54 | test("Scalar Ops") { 55 | val a = mk(1,2,3); 56 | 57 | assert((a + 1).toList === List(2.0,3.0,4.0)); 58 | assert((a - 1).toList === List(0,1,2)); 59 | assert((a * 3.0).toList === List(3.0,6.0,9.0)); 60 | assert((a / 2.0).toList === List(0.5,1.0,1.5)); 61 | assert((a % 2).toList === List(1,0,1)); 62 | } 63 | 64 | test("Binary Ops") { 65 | val a = mk(0,-1,2); 66 | val b = mk(0.0,0.0,3.0); 67 | assert((a :&& b).toList === List(false,false,true)); 68 | assert((a :|| b).toList === List(false,true,true)); 69 | assert((a :^^ b).toList === List(false,true,false)); 70 | } 71 | } 72 | 73 | -------------------------------------------------------------------------------- /src/test/scala/scalala/operators/SparseArrayTest.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package operators; 22 | 23 | import org.scalacheck._ 24 | import org.scalatest._; 25 | import org.scalatest.junit._; 26 | import org.scalatest.prop._; 27 | import org.junit.runner.RunWith 28 | 29 | @RunWith(classOf[JUnitRunner]) 30 | class SparseArrayTest extends FunSuite with Checkers { 31 | 32 | import scalala.collection.sparse.{SparseArray,DefaultArrayValue}; 33 | 34 | def mk[V:ClassManifest:DefaultArrayValue](values : V*) = 35 | SparseArray(values :_*); 36 | 37 | test("Negation") { 38 | val a = mk(1,0,3); 39 | assert((-a).toList === List(-1,0,-3)); 40 | } 41 | 42 | test("Zeros") { 43 | val a = mk(1,0,3); 44 | val b = mk(4,5,0); 45 | 46 | assert((a :+ b).toList === List(5,5,3)); 47 | assert((b :+ a).toList === List(5,5,3)); 48 | assert((a :- b).toList === List(-3,-5,3)); 49 | assert((a :* b).toList === List(4,0,0)); 50 | } 51 | 52 | test("Collection Ops") { 53 | val a = mk(1,2,3); 54 | val b = mk(4,5,6); 55 | 56 | assert((a :+ b).toList === List(5,7,9)); 57 | assert((a :- b).toList === List(-3,-3,-3)); 58 | assert((a :* b).toList === List(4,10,18)); 59 | assert((b :/ a).toList === List(4,2,2)); 60 | assert((a :/ b.map(_.toDouble)).toList === List(0.25,0.4,0.5)); 61 | assert((b :% a).toList === List(0,1,0)); 62 | } 63 | 64 | test("Scalar Ops") { 65 | val a = mk(1,2,3); 66 | 67 | assert((a :+ 1.0).toList === List(2.0,3.0,4.0)); 68 | assert((a :- 1).toList === List(0,1,2)); 69 | assert((a :* 3.0).toList === List(3.0,6.0,9.0)); 70 | assert((a :/ 2.0).toList === List(0.5,1.0,1.5)); 71 | assert((a :% 2).toList === List(1,0,1)); 72 | } 73 | } 74 | 75 | -------------------------------------------------------------------------------- /src/test/scala/scalala/operators/TupleTest.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala; 21 | package operators; 22 | 23 | import org.scalacheck._ 24 | import org.scalatest._; 25 | import org.scalatest.junit._; 26 | import org.scalatest.prop._; 27 | import org.junit.runner.RunWith 28 | 29 | import Implicits._; 30 | 31 | @RunWith(classOf[JUnitRunner]) 32 | class TupleTest extends FunSuite with Checkers { 33 | 34 | test("Neg") { 35 | assert(-(1,2.0,3f) === (-1, -2.0, -3f)); 36 | } 37 | 38 | test("Add") { 39 | assert((1,2,3l) + (3.0,2,-1) === (4.0,4,2l)); 40 | assert((1,2,3,4) + 1 === (2,3,4,5)); 41 | assert(2f :+ (1,2.0,3,4,6) === (3f,4.0,5f,6f,8f)); 42 | } 43 | 44 | test("Sub") { 45 | assert((1,2,3l) - (3.0,2,-1) === (-2.0,0,4l)); 46 | assert((1,2,3,4) - 1 === (0,1,2,3)); 47 | assert(7.0 :- (1,2,3,4) === (6.0,5.0,4.0,3.0)); 48 | } 49 | 50 | test("Mul") { 51 | assert((1,2) :* (2,4) === (2,8)); 52 | assert((1,2,3,4) :* 2.0 === (2.0,4.0,6.0,8.0)); 53 | assert(0.5f :* (1,2,3,4) === (0.5f,1.0f,1.5f,2.0f)); 54 | } 55 | 56 | test("Div") { 57 | assert((4,2) :/ (2,2) === (2,1)); 58 | assert((1,2,3,4) :/ 2.0 === (0.5,1.0,1.5,2.0)); 59 | assert(2f :/ (1,2,4) === (2f,1f,0.5f)); 60 | } 61 | 62 | test("Mod") { 63 | assert((3,5) :% 2 === (1,1)); 64 | } 65 | 66 | test("Compare") { 67 | assert(((1,2,3) :< (3,2,1)) === (true,false,false)); 68 | assert(((1,2,3) :<= (3,2,1)) === (true,true,false)); 69 | assert(((1,2,3) :> (3,2,1)) === (false,false,true)); 70 | assert(((1,2,3) :>= (3,2,1)) === (false,true,true)); 71 | assert(((1,2,3) :== (3,2,1)) === (false,true,false)); 72 | assert(((1,2,3) :!= (3,2,1)) === (true,false,true)); 73 | } 74 | } 75 | 76 | -------------------------------------------------------------------------------- /src/test/scala/scalala/operators/bundles/VectorSpaceTest.scala: -------------------------------------------------------------------------------- 1 | package scalala 2 | package operators 3 | package bundles 4 | /* 5 | * Distributed as part of Scalala, a linear algebra library. 6 | * 7 | * Copyright (C) 2008- Daniel Ramage 8 | * 9 | * This library is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 2.1 of the License, or (at your option) any later version. 13 | * 14 | * This library is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this library; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 22 | */ 23 | 24 | import org.scalacheck._ 25 | import org.scalatest._; 26 | import org.scalatest.junit._; 27 | import org.scalatest.prop._; 28 | import org.junit.runner.RunWith 29 | import tensor.dense.{DenseVectorCol, DenseVector} 30 | 31 | // This test basically just makes sure things compile 32 | @RunWith(classOf[JUnitRunner]) 33 | class VectorSpaceTest extends FunSuite with Checkers { 34 | type DVectorSpace[V] = MutableVectorSpace[Double,V]; 35 | def vsAdd[V:DVectorSpace](v1: V, v2: V) = { 36 | val dv = implicitly[DVectorSpace[V]]; 37 | import dv._; 38 | v1 + v2; 39 | } 40 | test("VectorSpace basically works") { 41 | VectorSpace.make[DenseVectorCol[Double],Double]; 42 | val r = vsAdd(DenseVector(1.,2.,3.),DenseVector(3.,4.,5.)) 43 | assert(r === DenseVector(4.,6.,8.)); 44 | } 45 | } 46 | 47 | -------------------------------------------------------------------------------- /src/test/scala/scalala/scalar/ComplexTest.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala.scalar; 21 | 22 | import org.scalacheck._ 23 | import org.scalatest._; 24 | import org.scalatest.junit._; 25 | import org.scalatest.prop._; 26 | import org.junit.runner.RunWith 27 | 28 | @RunWith(classOf[JUnitRunner]) 29 | class ComplexTest extends FunSuite with Checkers { 30 | 31 | test("Add") { 32 | assert((1 + 2*i) + (2 + 3*i) === (3 + 5*i)); 33 | } 34 | 35 | test("Sub") { 36 | assert((1 + 2*i) - (2 + 3*i) === (-1 - i)); 37 | } 38 | 39 | test("Div") { 40 | assert((5 + 10*i) / (3 - 4*i) === (-1 + 2*i)); 41 | } 42 | 43 | test("Mul") { 44 | assert((1 + 2*i) * (-3 + 6*i) === -15); 45 | assert((1 + 5*i) * (-3 + 2*i) === (-13 - 13*i)); 46 | } 47 | 48 | test("Neg") { 49 | assert(-(1 + 2*i) === (-1 - 2*i)); 50 | } 51 | 52 | test("Abs/Conj") { 53 | assert((3 + 4*i).abs === 5); 54 | val c = (1.7 + 2.1*i); 55 | assert(c * c.conjugate === 7.3); 56 | } 57 | 58 | test("List[Complex].sum (test ComplexIsFractional)") { 59 | val x = List((5 + 7*i), (1 + 3*i), (13 + 17*i)) 60 | assert(x.sum === (19 + 27*i)) 61 | } 62 | 63 | test("List[Complex].product (test ComplexIsFractional)") { 64 | val x = List((5 + 7*i), (1 + 3*i), (13 + 17*i)) 65 | assert(x.product === (-582 + 14*i)) 66 | } 67 | 68 | test("List[Complex].sorted (test ComplexOrdering)") { 69 | val x = List((5 + 7*i), (1 + 3*i), (13 + 17*i)) 70 | assert(x.sorted === List((1 + 3*i), (5 + 7*i), (13 + 17*i))) 71 | } 72 | 73 | test("Only permit conversion to built-in numeric types when purely real.") { 74 | val a = 5 + 0*i // should allow conversion 75 | val b = 5 + 5*i // should bork with an IllegalArgumentException 76 | 77 | def toDouble[Complex](c: Complex)(implicit n: Numeric[Complex]): Double = 78 | n.toDouble(c) 79 | def toFloat[Complex](c: Complex)(implicit n: Numeric[Complex]): Float = 80 | n.toFloat(c) 81 | def toInt[Complex](c: Complex)(implicit n: Numeric[Complex]): Int = 82 | n.toInt(c) 83 | def toLong[Complex](c: Complex)(implicit n: Numeric[Complex]): Long = 84 | n.toLong(c) 85 | 86 | assert(toDouble(a) === 5) 87 | assert(toFloat(a) === 5) 88 | assert(toInt(a) === 5) 89 | assert(toLong(a) === 5) 90 | intercept[IllegalArgumentException] { toDouble(b) } 91 | intercept[IllegalArgumentException] { toFloat(b) } 92 | intercept[IllegalArgumentException] { toInt(b) } 93 | intercept[IllegalArgumentException] { toLong(b) } 94 | } 95 | 96 | } 97 | -------------------------------------------------------------------------------- /src/test/scala/scalala/tensor/CRSTensor2Test.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala.tensor; 21 | 22 | 23 | import dense.{DenseVectorCol, DenseVector} 24 | import org.scalatest._; 25 | import org.scalatest.junit._; 26 | import org.scalatest.prop._; 27 | import org.junit.runner.RunWith 28 | 29 | import domain._; 30 | 31 | @RunWith(classOf[JUnitRunner]) 32 | class CRSTensor2Test extends FunSuite with Checkers { 33 | def typeOf[X](value : X)(implicit m : scala.reflect.Manifest[X]) = 34 | m.toString; 35 | def show[X](value : X)(implicit m : scala.reflect.Manifest[X]) = { 36 | println(typeOf(value) + ":"); 37 | println(value); 38 | println(); 39 | } 40 | 41 | test("Getting and setting") { 42 | val x = mutable.CRSTensor2[String,Int,Double,DenseVectorCol[Double]](DenseVector.zeros(3)); 43 | x("a",1) = 3.0; 44 | x("b",2) = 7.75; 45 | x("c",2) = 8.0; 46 | 47 | assert(x.valuesIterator.toSet === Set(3.0,8.0,7.75,0.0)); 48 | } 49 | 50 | test("Slice rows") { 51 | val x = mutable.CRSTensor2[String,Int,Double,DenseVectorCol[Double]](DenseVector.zeros(3)); 52 | x("a",::) := DenseVector(1.,2.,3.) 53 | 54 | // require expected static type 55 | val s1 : DenseVectorCol[Double] = x("a",::); 56 | assert(s1 === DenseVector(1.,2.,3.)); 57 | 58 | // write-through 59 | s1(1) = 4; 60 | assert(x("a",1) === 4.0); 61 | 62 | } 63 | 64 | /* 65 | test("Addition") { 66 | // require expected static type 67 | val x = mutable.CRSTensor2[String,Int,Double,DenseVectorCol[Double]](DenseVector.zeros(3)); 68 | x("a") := DenseVector(1.,2.,3.); 69 | Counter2(("a","a",1),("b","b",2)) + Counter2(("a","a",3)); 70 | assert(v1 === Counter2(("a","a",4),("b","b",2))); 71 | 72 | // require expected static type 73 | val v2 : mutable.Counter2[String,Char,Int] = 74 | Counter2(("a",'a',3)) + Counter2(("a",'a',1),("b",'b',2)); 75 | assert(v2 === Counter2(("a",'a',4),("b",'b',2))); 76 | } 77 | 78 | test("AddInto") { 79 | val x = Counter2[String,String,Int](); 80 | x += Counter2(("a","a",1)); 81 | assert(x === Counter2(("a","a",1))); 82 | x += Counter2(("a","a",2),("a","b",4)); 83 | assert(x === Counter2(("a","a",3),("a","b",4))); 84 | } 85 | 86 | test("Subtraction") { 87 | assert(mutable.Counter2(("a","a",1),("b","b",2)) - mutable.Counter2(("a","a",3)) === Counter2(("a","a",-2), ("b","b",2))); 88 | assert(mutable.Counter2(("a","a",3)) - mutable.Counter2(("a","a",1),("b","b",2)) === Counter2(("a","a",2), ("b","b",-2))); 89 | } 90 | 91 | test("Multiplication") { 92 | assert(mutable.Counter2(("a","a",1),("b","b",2)) :* mutable.Counter2(("a","a",3)) === Counter2(("a","a",3))); 93 | assert(mutable.Counter2(("a","a",3)) :* mutable.Counter2(("a","a",1),("b","b",2)) === Counter2(("a","a",3))); 94 | } 95 | */ 96 | } 97 | -------------------------------------------------------------------------------- /src/test/scala/scalala/tensor/CounterTest.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala.tensor; 21 | 22 | import org.scalacheck._ 23 | import org.scalatest._; 24 | import org.scalatest.junit._; 25 | import org.scalatest.prop._; 26 | import org.junit.runner.RunWith 27 | 28 | @RunWith(classOf[JUnitRunner]) 29 | class CounterTest extends FunSuite with Checkers { 30 | val TOLERANCE = 1e-4; 31 | def assertClose(a : Double, b : Double) = 32 | assert(math.abs(a - b) < TOLERANCE); 33 | 34 | test("Addition") { 35 | assert(mutable.Counter("a"->1,"b"->2) + mutable.Counter("a"->3) === Counter("a"->4,"b"->2)); 36 | assert(mutable.Counter("a"->3) + mutable.Counter("a"->1,"b"->2) === Counter("a"->4,"b"->2)); 37 | } 38 | 39 | test("Subtraction") { 40 | assert(mutable.Counter("a"->1,"b"->2) - mutable.Counter("a"->3) === Counter("a" -> -2, "b" -> 2)); 41 | assert(mutable.Counter("a"->3) - mutable.Counter("a"->1,"b"->2) === Counter("a" -> 2, "b" -> -2)); 42 | } 43 | 44 | test("Multiplication") { 45 | assert(mutable.Counter("a"->1,"b"->2) :* mutable.Counter("a"->3) === Counter("a"->3)); 46 | assert(mutable.Counter("a"->3) :* mutable.Counter("a"->1,"b"->2) === Counter("a"->3)); 47 | } 48 | 49 | test("MulInner") { 50 | val a = mutable.Counter(1->0.56390,2->0.36231,3->0.14601,4->0.60294,5->0.14535); 51 | val b = mutable.Counter(1->0.15951,2->0.83671,3->0.56002,4->0.57797,5->0.54450); 52 | assertClose(a dot b, .90249); 53 | } 54 | 55 | test("Zero + non zero is nonzero") { 56 | val a = mutable.Counter[Int,Double](1->0.0) 57 | val b = mutable.Counter(1->0.15951); 58 | assert(a + b === b, (a + b).toString + " not equal " + b) 59 | } 60 | 61 | test("Mean") { 62 | assert(Counter(0->0,1->1,2->2).mean === 1.0); 63 | assert(Counter(0->0.0,1->3.0).mean === 1.5); 64 | assert(Counter(0->3l).mean === 3.0); 65 | } 66 | 67 | test("assignment checks both domains") { 68 | val a = Counter[Int,Int]() 69 | val b = Counter[Int,Int](3->4) 70 | a := b 71 | assert(a === b) 72 | } 73 | } 74 | 75 | -------------------------------------------------------------------------------- /src/test/scala/scalala/tensor/DiagonalMatrixTest.scala: -------------------------------------------------------------------------------- 1 | package scalala.tensor 2 | 3 | /* 4 | * Distributed as part of Scalala, a linear algebra library. 5 | * 6 | * Copyright (C) 2008- Daniel Ramage 7 | * 8 | * This library is free software; you can redistribute it and/or 9 | * modify it under the terms of the GNU Lesser General Public 10 | * License as published by the Free Software Foundation; either 11 | * version 2.1 of the License, or (at your option) any later version. 12 | * 13 | * This library is distributed in the hope that it will be useful, 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 | * Lesser General Public License for more details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public 19 | * License along with this library; if not, write to the Free Software 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 21 | */ 22 | 23 | import dense.{DenseVector, DenseMatrix} 24 | import org.scalatest._; 25 | import org.scalatest.junit._; 26 | import org.scalatest.prop._; 27 | import org.junit.runner.RunWith 28 | import scalala.library.LinearAlgebra.diag; 29 | 30 | @RunWith(classOf[JUnitRunner]) 31 | class DiagonalMatrixTest extends FunSuite with Checkers { 32 | 33 | 34 | test("Multiply") { 35 | val a = DenseMatrix((1, 2, 3),(4, 5, 6),(7,8,9)); 36 | val c = diag(DenseVector(6,2,3)); 37 | val ac :DenseMatrix[Int] = a * c; // check types; 38 | assert(a * c === DenseMatrix( (6,4,9),(24,10,18),(42,16,27))); 39 | } 40 | 41 | 42 | 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/test/scala/scalala/tensor/Tensor2Test.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala.tensor; 21 | 22 | 23 | import org.scalacheck._ 24 | import org.scalatest._; 25 | import org.scalatest.junit._; 26 | import org.scalatest.prop._; 27 | import org.junit.runner.RunWith 28 | 29 | import domain._; 30 | 31 | @RunWith(classOf[JUnitRunner]) 32 | class Tensor2Test extends FunSuite with Checkers { 33 | def mkTensor2() = { 34 | val domain = Domain2(SetDomain(Set("a","b","c")), SetDomain(Set(1,2))); 35 | mutable.Tensor2[String,Int,Double](domain); 36 | } 37 | 38 | def typeOf[X](value : X)(implicit m : scala.reflect.Manifest[X]) = 39 | m.toString; 40 | def show[X](value : X)(implicit m : scala.reflect.Manifest[X]) = { 41 | println(typeOf(value) + ":"); 42 | println(value); 43 | println(); 44 | } 45 | 46 | test("Getting and setting") { 47 | val x = mkTensor2(); 48 | x("a",1) = 3.0; 49 | x("b",2) = 7.75; 50 | x("c",2) = 8.0; 51 | 52 | assert(x.valuesIterator.toList === List(3.0,0,0,7.75,0,8.0)); 53 | } 54 | 55 | test("Transpose") { 56 | val x = mkTensor2(); 57 | x(("a",1),("b",2),("c",2)) := List(3.0,7.75,8.0); 58 | 59 | assert(x.t.valuesIterator.toList === List(3.0,0,0,0,7.75,8.0)); 60 | assert(x.t.t === x); 61 | assert(x.t.t eq x); 62 | 63 | x.t(2,"a") = 1; 64 | assert(x("a",2) === 1); 65 | } 66 | 67 | test("Slice table") { 68 | val x= mkTensor2(); 69 | x(("a",1),("b",2),("c",2)) := List(3.0,7.75,8.0); 70 | 71 | val table = x(List("a","b","c"),List(1,2)); 72 | 73 | assert(table.domain === TableDomain(3,2)); 74 | 75 | table(1,0) = 5; 76 | assert(x("b",1) === 5); 77 | } 78 | 79 | test("Slice rows and columns") { 80 | val x= mkTensor2(); 81 | x(("a",1),("b",2),("c",2)) := List(3.0,7.75,8.0); 82 | 83 | assert(x("a", ::).isInstanceOf[Tensor1[_,_]]); 84 | assert(x("a", ::).toMap === Map(1->3.0, 2->0)) 85 | 86 | assert(x(::, 2).isInstanceOf[Tensor1[_,_]]); 87 | assert(x(::, 2).toMap === Map("a" -> 0.0, "b" -> 7.75, "c" -> 8.0)) 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /src/test/scala/scalala/tensor/TensorTest.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed as part of Scalala, a linear algebra library. 3 | * 4 | * Copyright (C) 2008- Daniel Ramage 5 | * 6 | * This library is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public 17 | * License along with this library; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 19 | */ 20 | package scalala.tensor; 21 | 22 | import org.scalacheck._ 23 | import org.scalatest._; 24 | import org.scalatest.junit._; 25 | import org.scalatest.prop._; 26 | import org.junit.runner.RunWith 27 | 28 | @RunWith(classOf[JUnitRunner]) 29 | class TensorTest extends FunSuite with Checkers { 30 | def mkMapTensor() = 31 | mutable.Tensor[String,Int](domain.SetDomain(Set("a","b","c"))); 32 | 33 | test("Get and set values") { 34 | val x = mkMapTensor(); 35 | assert(x("a") === 0); 36 | assert(x("b") === 0); 37 | assert(x("c") === 0); 38 | x("a") = 5; x("b") = 7; x("c") = 2; 39 | assert(x("a") === 5); 40 | assert(x("b") === 7); 41 | assert(x("c") === 2); 42 | } 43 | 44 | test("Slicing to a sequence") { 45 | val x = mkMapTensor(); 46 | x("a","b","c") := List(1,2,3); 47 | assert(x("a") === 1); 48 | assert(x("b") === 2); 49 | assert(x("c") === 3); 50 | 51 | assert(x("c","a","b","b","a").valuesIterator.toList === List(3,1,2,2,1)); 52 | } 53 | 54 | test("Slicing to new keys") { 55 | val x = mkMapTensor(); 56 | x("a","b","c") := List(1,2,3); 57 | val y = x(0->"a", 1->"b", 2->"c"); 58 | assert(y(0) === x("a")); 59 | assert(y(1) === x("b")); 60 | assert(y(2) === x("c")); 61 | } 62 | 63 | test("Map values") { 64 | val x = mkMapTensor(); 65 | x("a","b","c") := List(1,2,3); 66 | assert(x.mapValues(_ % 2 == 0).valuesIterator.toList === List(false,true,false)); 67 | } 68 | 69 | test("Filter values") { 70 | val x = mkMapTensor(); 71 | x("a","b","c") := List(1,2,3); 72 | x(x.findAll(_ >= 2)) := 0; 73 | assert(x.valuesIterator.toList === List(1,0,0)); 74 | } 75 | 76 | test("Find") { 77 | val x = mkMapTensor(); 78 | x("a","b","c") := List(1,2,3); 79 | assert(x.findAll(_ >= 2).toList === List("b","c")); 80 | } 81 | 82 | test("Overlapping slice assignments") { 83 | val x = mkMapTensor(); 84 | x("a","b","c") := List(1,2,3); 85 | x("a","b") := x("b","c"); 86 | assert(x.valuesIterator.toList === List(2,3,3)); 87 | } 88 | 89 | test("Sorting") { 90 | val x = mkMapTensor(); 91 | x("a","b","c") := List(5,7,3); 92 | 93 | assert(x.argsort.toList === List("c","a","b")); 94 | 95 | assert(x.sorted.valuesIterator.toList === List(3,5,7)); 96 | } 97 | 98 | test("Min/Max") { 99 | val x = mkMapTensor(); 100 | x("a","b","c") := List(5,7,3); 101 | assert(x.min === 3); 102 | assert(x.max === 7); 103 | assert(x.argmin === "c"); 104 | assert(x.argmax === "b"); 105 | } 106 | } 107 | 108 | -------------------------------------------------------------------------------- /src/test/scala/scalala/tensor/sparse/SparseVectorTest.scala: -------------------------------------------------------------------------------- 1 | package scalala.tensor.sparse 2 | 3 | import org.scalatest._ 4 | import org.scalatest.matchers.ShouldMatchers 5 | import org.scalatest.junit._ 6 | import org.junit.runner.RunWith 7 | import scalala.tensor.dense.DenseVector 8 | 9 | /** 10 | * 11 | * @author dlwh 12 | */ 13 | @RunWith(classOf[JUnitRunner]) 14 | class SparseVectorTest extends FunSuite with ShouldMatchers { 15 | 16 | val TOLERANCE = 1e-4; 17 | def assertClose(a : Double, b : Double) = 18 | assert(math.abs(a - b) < TOLERANCE, a + " vs. " + b); 19 | 20 | test("MulInner") { 21 | val a = SparseVector(0.56390,0.36231,0.14601,0.60294,0.14535); 22 | val b = SparseVector(0.15951,0.83671,0.56002,0.57797,0.54450); 23 | val bd = DenseVector(0.15951,0.83671,0.56002,0.57797,0.54450); 24 | val bdSplit = DenseVector(0., 0.15951, 0., 0.83671,0., 0.56002, 0., 0.57797, 0., 0.54450); 25 | val bdd = bdSplit(1 to 9 by 2) 26 | assertClose(a dot b, .90249); 27 | assertClose(a dot bd, .90249); 28 | assertClose(bd dot a, .90249); 29 | assertClose(bdd dot a, .90249); 30 | assertClose(a.t * b, .90249); 31 | } 32 | 33 | test("Subtraction") { 34 | val a = SparseVector(0.56390,0.36231,0.14601,0.60294,0.14535); 35 | val ad = DenseVector(0.56390,0.36231,0.14601,0.60294,0.14535); 36 | val b = SparseVector(0.15951,0.83671,0.56002,0.57797,0.54450); 37 | val bd = DenseVector(0.15951,0.83671,0.56002,0.57797,0.54450); 38 | val bdd = bd - ad 39 | b -= a 40 | bd -= a 41 | assertClose(b.norm(2), bd.norm(2)) 42 | assertClose(bdd.norm(2), bd.norm(2)) 43 | } 44 | 45 | } --------------------------------------------------------------------------------