Creating a Matrix from Vectors or Arrays? Array Math?

162 views
Skip to first unread message

Allan Brighton

unread,
May 12, 2013, 10:12:24 AM5/12/13
to scala-...@googlegroups.com
Hi,

I have some questions about how Breeze compares to Scalala.

In Scalala it was possible to create a Matrix from a number of Arrays or Vectors. For example:

  val a1 = Array(1,2,3)
  val a2 = Array(4,5,6)
  val m = Matrix(a1, a2)
  m: scalala.tensor.dense.DenseMatrix[Int] =
  1  2  3
  4  5  6 

This doesn't seem to be implemented in Breeze. Is this feature planned? (Or is it considered ambiguous?)

I also noticed that arithmetic operations on arrays changed. Array(1,2,3) :+ 2
used to add 2 to each element to get Array(3,4,5), but now it appends 2 to the array:
Array(1,2,3,2). Is that to be expected?

I noticed other differences. For example: Vector(1,2,3) used to result in a DenseVector.
Now I have to use DenseVector(1,2,3) to get that (same for Matrix and DenseMatrix).

Scalala defined arithmetic operations on arrays, lists and numbers, so I could write
5.0 :^ 3.0 instead of math.pow(5.0, 3.0) or Array(1,2,3) :* 2. Are there plans to add these operations to Breeze?

What is the reason for not automatically converting integers to double in Matrix and Vector operations
as scalala did? Is that better for performance or to avoid confusion?

Thanks,
Allan



David Hall

unread,
May 12, 2013, 11:52:05 AM5/12/13
to scala-...@googlegroups.com
On Sun, May 12, 2013 at 7:12 AM, Allan Brighton <alla...@gmail.com> wrote:
Hi,

I have some questions about how Breeze compares to Scalala.

In Scalala it was possible to create a Matrix from a number of Arrays or Vectors. For example:

  val a1 = Array(1,2,3)
  val a2 = Array(4,5,6)
  val m = Matrix(a1, a2)
  m: scalala.tensor.dense.DenseMatrix[Int] =
  1  2  3
  4  5  6   

This doesn't seem to be implemented in Breeze. Is this feature planned? (Or is it considered ambiguous?)

scala> import breeze.linalg._
import breeze.linalg._

scala> Matrix(Array(1,2,3), Array(4,5,6))
res0: breeze.linalg.Matrix[Int] =
1  2  3
4  5  6
I also noticed that arithmetic operations on arrays changed. Array(1,2,3) :+ 2
used to add 2 to each element to get Array(3,4,5), but now it appends 2 to the array:
Array(1,2,3,2). Is that to be expected?

  :+ is a method on arrays in Scala, and it's the syntax for append.  That said, import NumericOps.Arrays._ will do what you want for arrays *except for :+*. I should actually make that happen. Can you open a bug against :+?

They're not enabled by default, which I thought was how Scalala operated.
 

I noticed other differences. For example: Vector(1,2,3) used to result in a DenseVector.
Now I have to use DenseVector(1,2,3) to get that (same for Matrix and DenseMatrix).

This is more consistent with how scala collections work. Seq(1,2,3) is implemented as a List but the static type is a Seq: 

scala> Seq(1,2,3)
res6: Seq[Int] = List(1, 2, 3)

The reasoning is the same: using Vector(1,2,3) signals you don't care what implementation you use: you just want something that conforms to the Vector interface.
 

Scalala defined arithmetic operations on arrays, lists and numbers, so I could write
5.0 :^ 3.0 instead of math.pow(5.0, 3.0) or Array(1,2,3) :* 2. Are there plans to add these operations to Breeze?

I just haven't gotten around to lists and numbers. Arrays should work using the import above. If you want to port collections or scalars, I'd accept the change... (You can also open a feature request and someone will get to it eventually.)
 

What is the reason for not automatically converting integers to double in Matrix and Vector operations
as scalala did? Is that better for performance or to avoid confusion?

It was implemented extremely slowly in Scalala, backing off to a combination of iterators and for loops, even using a HashSet in the inner loop. I made the decision that I only wanted to implement things when we could make them fast, so as not to cause performance surprises. But, again, I haven't gotten around to it yet. 

-- David
 

Thanks,
Allan



--
You received this message because you are subscribed to the Google Groups "Scala Breeze" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-breeze...@googlegroups.com.
To post to this group, send email to scala-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/scala-breeze/-/ZTkQMPaIJ28J.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages