[scala] vector math parameterized class in scala

View: New views
11 Messages — Rating Filter:   Alert me  

[scala] vector math parameterized class in scala

by Jonathan Bachrach-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

i'm trying to define a vector math class using parameterized classes in scala and am having trouble.  here's an abbreviated version of what i wrote:

class Vec3[T <: Number] (xa: T, ya: T, za: T) {
  val x: T = xa; val y: T = ya; val z: T = za
  def + (o: Vec3[T]): Vec3[T] = new Vec3(x + o.x, y + o.y, z + o.z) 
  def * (s: T): Vec3[T] = new Vec3(x * s, y * s, z * s) 
  def len (): T = x + y + z 
  def == (o: Vec3[T]): Boolean = x == o.x && y == o.y && z == o.z 
  def < (o: Vec3[T]): Boolean = x < o.x && y < o.y && z < o.z 
}

and here's what scalac says:

rala-692> scalac vec3.scala
vec3.scala:3: error: type mismatch;
 found   : T
 required: String
  def + (o: Vec3[T]): Vec3[T] = new Vec3(x + o.x, y + o.y, z + o.z) 
                                               ^
vec3.scala:3: error: type mismatch;
 found   : T
 required: String
  def + (o: Vec3[T]): Vec3[T] = new Vec3(x + o.x, y + o.y, z + o.z) 
                                                        ^
vec3.scala:3: error: type mismatch;
 found   : T
 required: String
  def + (o: Vec3[T]): Vec3[T] = new Vec3(x + o.x, y + o.y, z + o.z) 
                                                                 ^
vec3.scala:4: error: value * is not a member of T
  def * (s: T): Vec3[T] = new Vec3(x * s, y * s, z * s) 
                                     ^
vec3.scala:4: error: value * is not a member of T
  def * (s: T): Vec3[T] = new Vec3(x * s, y * s, z * s) 
                                            ^
vec3.scala:4: error: value * is not a member of T
  def * (s: T): Vec3[T] = new Vec3(x * s, y * s, z * s) 
                                                   ^
vec3.scala:5: error: type mismatch;
 found   : T
 required: String
  def len (): T = x + y + z 
                      ^
vec3.scala:7: error: value < is not a member of T
  def < (o: Vec3[T]): Boolean = x < o.x && y < o.y && z < o.z 
                                  ^
8 errors found

i had added the T <: Number to try to make it so that the + would be compatible but it still thinks + only works with strings.  i'm not even completely sure that Number is a real Scala Class.  i'm obviously not understanding how to write parameterized classes in scala.  any help would be appreciated.


Re: [scala] vector math parameterized class in scala

by Marcelo Fukushima :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

i think the problem is that Number does not define '+' and '*' methods
- and all the compiler knows about your T is that its a Number
(java.lang.Number)


On Tue, Jul 7, 2009 at 7:40 PM, Jonathan Bachrach<jrb@...> wrote:

> i'm trying to define a vector math class using parameterized classes in
> scala and am having trouble.  here's an abbreviated version of what i wrote:
> class Vec3[T <: Number] (xa: T, ya: T, za: T) {
>   val x: T = xa; val y: T = ya; val z: T = za
>   def + (o: Vec3[T]): Vec3[T] = new Vec3(x + o.x, y + o.y, z + o.z)
>   def * (s: T): Vec3[T] = new Vec3(x * s, y * s, z * s)
>   def len (): T = x + y + z
>   def == (o: Vec3[T]): Boolean = x == o.x && y == o.y && z == o.z
>   def < (o: Vec3[T]): Boolean = x < o.x && y < o.y && z < o.z
> }
> and here's what scalac says:
> rala-692> scalac vec3.scala
> vec3.scala:3: error: type mismatch;
>  found   : T
>  required: String
>   def + (o: Vec3[T]): Vec3[T] = new Vec3(x + o.x, y + o.y, z + o.z)
>                                                ^
> vec3.scala:3: error: type mismatch;
>  found   : T
>  required: String
>   def + (o: Vec3[T]): Vec3[T] = new Vec3(x + o.x, y + o.y, z + o.z)
>                                                         ^
> vec3.scala:3: error: type mismatch;
>  found   : T
>  required: String
>   def + (o: Vec3[T]): Vec3[T] = new Vec3(x + o.x, y + o.y, z + o.z)
>                                                                  ^
> vec3.scala:4: error: value * is not a member of T
>   def * (s: T): Vec3[T] = new Vec3(x * s, y * s, z * s)
>                                      ^
> vec3.scala:4: error: value * is not a member of T
>   def * (s: T): Vec3[T] = new Vec3(x * s, y * s, z * s)
>                                             ^
> vec3.scala:4: error: value * is not a member of T
>   def * (s: T): Vec3[T] = new Vec3(x * s, y * s, z * s)
>                                                    ^
> vec3.scala:5: error: type mismatch;
>  found   : T
>  required: String
>   def len (): T = x + y + z
>                       ^
> vec3.scala:7: error: value < is not a member of T
>   def < (o: Vec3[T]): Boolean = x < o.x && y < o.y && z < o.z
>                                   ^
> 8 errors found
> i had added the T <: Number to try to make it so that the + would be
> compatible but it still thinks + only works with strings.  i'm not even
> completely sure that Number is a real Scala Class.  i'm obviously not
> understanding how to write parameterized classes in scala.  any help would
> be appreciated.
>



--
http://mapsdev.blogspot.com/
Marcelo Takeshi Fukushima

Re: [scala] vector math parameterized class in scala

by Jonathan Bachrach-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

i see.  that's unfortunate.  how would you recommend writing a vector  
math parameterized class in scala then (short of actually copying the  
code)?

On Jul 7, 2009, at 3:50 PM, Marcelo Fukushima wrote:

> i think the problem is that Number does not define '+' and '*' methods
> - and all the compiler knows about your T is that its a Number
> (java.lang.Number)
>
>
> On Tue, Jul 7, 2009 at 7:40 PM, Jonathan Bachrach<jrb@...>  
> wrote:
>> i'm trying to define a vector math class using parameterized  
>> classes in
>> scala and am having trouble.  here's an abbreviated version of what  
>> i wrote:
>> class Vec3[T <: Number] (xa: T, ya: T, za: T) {
>>   val x: T = xa; val y: T = ya; val z: T = za
>>   def + (o: Vec3[T]): Vec3[T] = new Vec3(x + o.x, y + o.y, z + o.z)
>>   def * (s: T): Vec3[T] = new Vec3(x * s, y * s, z * s)
>>   def len (): T = x + y + z
>>   def == (o: Vec3[T]): Boolean = x == o.x && y == o.y && z == o.z
>>   def < (o: Vec3[T]): Boolean = x < o.x && y < o.y && z < o.z
>> }
>> and here's what scalac says:
>> rala-692> scalac vec3.scala
>> vec3.scala:3: error: type mismatch;
>>  found   : T
>>  required: String
>>   def + (o: Vec3[T]): Vec3[T] = new Vec3(x + o.x, y + o.y, z + o.z)
>>                                                ^
>> vec3.scala:3: error: type mismatch;
>>  found   : T
>>  required: String
>>   def + (o: Vec3[T]): Vec3[T] = new Vec3(x + o.x, y + o.y, z + o.z)
>>                                                         ^
>> vec3.scala:3: error: type mismatch;
>>  found   : T
>>  required: String
>>   def + (o: Vec3[T]): Vec3[T] = new Vec3(x + o.x, y + o.y, z + o.z)
>>                                                                  ^
>> vec3.scala:4: error: value * is not a member of T
>>   def * (s: T): Vec3[T] = new Vec3(x * s, y * s, z * s)
>>                                      ^
>> vec3.scala:4: error: value * is not a member of T
>>   def * (s: T): Vec3[T] = new Vec3(x * s, y * s, z * s)
>>                                             ^
>> vec3.scala:4: error: value * is not a member of T
>>   def * (s: T): Vec3[T] = new Vec3(x * s, y * s, z * s)
>>                                                    ^
>> vec3.scala:5: error: type mismatch;
>>  found   : T
>>  required: String
>>   def len (): T = x + y + z
>>                       ^
>> vec3.scala:7: error: value < is not a member of T
>>   def < (o: Vec3[T]): Boolean = x < o.x && y < o.y && z < o.z
>>                                   ^
>> 8 errors found
>> i had added the T <: Number to try to make it so that the + would be
>> compatible but it still thinks + only works with strings.  i'm not  
>> even
>> completely sure that Number is a real Scala Class.  i'm obviously not
>> understanding how to write parameterized classes in scala.  any  
>> help would
>> be appreciated.
>>
>
>
>
> --
> http://mapsdev.blogspot.com/
> Marcelo Takeshi Fukushima


Re: [scala] vector math parameterized class in scala

by Jorge Ortiz-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Take a look at Scala 2.8's Numeric trait:


--j

On Tue, Jul 7, 2009 at 5:56 PM, Jonathan Bachrach <jrb@...> wrote:
i see.  that's unfortunate.  how would you recommend writing a vector math parameterized class in scala then (short of actually copying the code)?


On Jul 7, 2009, at 3:50 PM, Marcelo Fukushima wrote:

i think the problem is that Number does not define '+' and '*' methods
- and all the compiler knows about your T is that its a Number
(java.lang.Number)


On Tue, Jul 7, 2009 at 7:40 PM, Jonathan Bachrach<jrb@...> wrote:
i'm trying to define a vector math class using parameterized classes in
scala and am having trouble.  here's an abbreviated version of what i wrote:
class Vec3[T <: Number] (xa: T, ya: T, za: T) {
 val x: T = xa; val y: T = ya; val z: T = za
 def + (o: Vec3[T]): Vec3[T] = new Vec3(x + o.x, y + o.y, z + o.z)
 def * (s: T): Vec3[T] = new Vec3(x * s, y * s, z * s)
 def len (): T = x + y + z
 def == (o: Vec3[T]): Boolean = x == o.x && y == o.y && z == o.z
 def < (o: Vec3[T]): Boolean = x < o.x && y < o.y && z < o.z
}
and here's what scalac says:
rala-692> scalac vec3.scala
vec3.scala:3: error: type mismatch;
 found   : T
 required: String
 def + (o: Vec3[T]): Vec3[T] = new Vec3(x + o.x, y + o.y, z + o.z)
                                              ^
vec3.scala:3: error: type mismatch;
 found   : T
 required: String
 def + (o: Vec3[T]): Vec3[T] = new Vec3(x + o.x, y + o.y, z + o.z)
                                                       ^
vec3.scala:3: error: type mismatch;
 found   : T
 required: String
 def + (o: Vec3[T]): Vec3[T] = new Vec3(x + o.x, y + o.y, z + o.z)
                                                                ^
vec3.scala:4: error: value * is not a member of T
 def * (s: T): Vec3[T] = new Vec3(x * s, y * s, z * s)
                                    ^
vec3.scala:4: error: value * is not a member of T
 def * (s: T): Vec3[T] = new Vec3(x * s, y * s, z * s)
                                           ^
vec3.scala:4: error: value * is not a member of T
 def * (s: T): Vec3[T] = new Vec3(x * s, y * s, z * s)
                                                  ^
vec3.scala:5: error: type mismatch;
 found   : T
 required: String
 def len (): T = x + y + z
                     ^
vec3.scala:7: error: value < is not a member of T
 def < (o: Vec3[T]): Boolean = x < o.x && y < o.y && z < o.z
                                 ^
8 errors found
i had added the T <: Number to try to make it so that the + would be
compatible but it still thinks + only works with strings.  i'm not even
completely sure that Number is a real Scala Class.  i'm obviously not
understanding how to write parameterized classes in scala.  any help would
be appreciated.




--
http://mapsdev.blogspot.com/
Marcelo Takeshi Fukushima



Re: [scala] vector math parameterized class in scala

by Kevin Wright-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

You've also missed out on case classes and type inference, although this isn't causing your problem...

Using view bounds with the implicit conversions in Numeric should do the trick!

case class Vec3[T <:% Numeric] (x: T, y: T, z: T) {
  def + (o: Vec3[T]) = new Vec3(x + o.x, y + o.y, z + o.z) 
  def * (s: T): Vec3[T] = new Vec3(x * s, y * s, z * s) 
  def len = x + y + z 
  def == (o: Vec3[T]) = x == o.x && y == o.y && z == o.z 
  def < (o: Vec3[T]) = x < o.x && y < o.y && z < o.z 
}

On Tue, Jul 7, 2009 at 11:59 PM, Jorge Ortiz <jorge.ortiz@...> wrote:
Take a look at Scala 2.8's Numeric trait:


--j


On Tue, Jul 7, 2009 at 5:56 PM, Jonathan Bachrach <jrb@...> wrote:
i see.  that's unfortunate.  how would you recommend writing a vector math parameterized class in scala then (short of actually copying the code)?


On Jul 7, 2009, at 3:50 PM, Marcelo Fukushima wrote:

i think the problem is that Number does not define '+' and '*' methods
- and all the compiler knows about your T is that its a Number
(java.lang.Number)


On Tue, Jul 7, 2009 at 7:40 PM, Jonathan Bachrach<jrb@...> wrote:
i'm trying to define a vector math class using parameterized classes in
scala and am having trouble.  here's an abbreviated version of what i wrote:
class Vec3[T <: Number] (xa: T, ya: T, za: T) {
 val x: T = xa; val y: T = ya; val z: T = za
 def + (o: Vec3[T]): Vec3[T] = new Vec3(x + o.x, y + o.y, z + o.z)
 def * (s: T): Vec3[T] = new Vec3(x * s, y * s, z * s)
 def len (): T = x + y + z
 def == (o: Vec3[T]): Boolean = x == o.x && y == o.y && z == o.z
 def < (o: Vec3[T]): Boolean = x < o.x && y < o.y && z < o.z
}
and here's what scalac says:
rala-692> scalac vec3.scala
vec3.scala:3: error: type mismatch;
 found   : T
 required: String
 def + (o: Vec3[T]): Vec3[T] = new Vec3(x + o.x, y + o.y, z + o.z)
                                              ^
vec3.scala:3: error: type mismatch;
 found   : T
 required: String
 def + (o: Vec3[T]): Vec3[T] = new Vec3(x + o.x, y + o.y, z + o.z)
                                                       ^
vec3.scala:3: error: type mismatch;
 found   : T
 required: String
 def + (o: Vec3[T]): Vec3[T] = new Vec3(x + o.x, y + o.y, z + o.z)
                                                                ^
vec3.scala:4: error: value * is not a member of T
 def * (s: T): Vec3[T] = new Vec3(x * s, y * s, z * s)
                                    ^
vec3.scala:4: error: value * is not a member of T
 def * (s: T): Vec3[T] = new Vec3(x * s, y * s, z * s)
                                           ^
vec3.scala:4: error: value * is not a member of T
 def * (s: T): Vec3[T] = new Vec3(x * s, y * s, z * s)
                                                  ^
vec3.scala:5: error: type mismatch;
 found   : T
 required: String
 def len (): T = x + y + z
                     ^
vec3.scala:7: error: value < is not a member of T
 def < (o: Vec3[T]): Boolean = x < o.x && y < o.y && z < o.z
                                 ^
8 errors found
i had added the T <: Number to try to make it so that the + would be
compatible but it still thinks + only works with strings.  i'm not even
completely sure that Number is a real Scala Class.  i'm obviously not
understanding how to write parameterized classes in scala.  any help would
be appreciated.




--
http://mapsdev.blogspot.com/
Marcelo Takeshi Fukushima




Re: [scala] vector math parameterized class in scala

by Jorge Ortiz-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

But I wouldn't recommend this approach (yet) if you're looking to do high-performance numerical computing...

--j

On Tue, Jul 7, 2009 at 6:57 PM, Kevin Wright <kev.lee.wright@...> wrote:
You've also missed out on case classes and type inference, although this isn't causing your problem...

Using view bounds with the implicit conversions in Numeric should do the trick!

case class Vec3[T <:% Numeric] (x: T, y: T, z: T) {
  def + (o: Vec3[T]) = new Vec3(x + o.x, y + o.y, z + o.z) 
  def * (s: T): Vec3[T] = new Vec3(x * s, y * s, z * s) 
  def len = x + y + z 
  def == (o: Vec3[T]) = x == o.x && y == o.y && z == o.z 
  def < (o: Vec3[T]) = x < o.x && y < o.y && z < o.z 
}

On Tue, Jul 7, 2009 at 11:59 PM, Jorge Ortiz <jorge.ortiz@...> wrote:
Take a look at Scala 2.8's Numeric trait:


--j


On Tue, Jul 7, 2009 at 5:56 PM, Jonathan Bachrach <jrb@...> wrote:
i see.  that's unfortunate.  how would you recommend writing a vector math parameterized class in scala then (short of actually copying the code)?


On Jul 7, 2009, at 3:50 PM, Marcelo Fukushima wrote:

i think the problem is that Number does not define '+' and '*' methods
- and all the compiler knows about your T is that its a Number
(java.lang.Number)


On Tue, Jul 7, 2009 at 7:40 PM, Jonathan Bachrach<jrb@...> wrote:
i'm trying to define a vector math class using parameterized classes in
scala and am having trouble.  here's an abbreviated version of what i wrote:
class Vec3[T <: Number] (xa: T, ya: T, za: T) {
 val x: T = xa; val y: T = ya; val z: T = za
 def + (o: Vec3[T]): Vec3[T] = new Vec3(x + o.x, y + o.y, z + o.z)
 def * (s: T): Vec3[T] = new Vec3(x * s, y * s, z * s)
 def len (): T = x + y + z
 def == (o: Vec3[T]): Boolean = x == o.x && y == o.y && z == o.z
 def < (o: Vec3[T]): Boolean = x < o.x && y < o.y && z < o.z
}
and here's what scalac says:
rala-692> scalac vec3.scala
vec3.scala:3: error: type mismatch;
 found   : T
 required: String
 def + (o: Vec3[T]): Vec3[T] = new Vec3(x + o.x, y + o.y, z + o.z)
                                              ^
vec3.scala:3: error: type mismatch;
 found   : T
 required: String
 def + (o: Vec3[T]): Vec3[T] = new Vec3(x + o.x, y + o.y, z + o.z)
                                                       ^
vec3.scala:3: error: type mismatch;
 found   : T
 required: String
 def + (o: Vec3[T]): Vec3[T] = new Vec3(x + o.x, y + o.y, z + o.z)
                                                                ^
vec3.scala:4: error: value * is not a member of T
 def * (s: T): Vec3[T] = new Vec3(x * s, y * s, z * s)
                                    ^
vec3.scala:4: error: value * is not a member of T
 def * (s: T): Vec3[T] = new Vec3(x * s, y * s, z * s)
                                           ^
vec3.scala:4: error: value * is not a member of T
 def * (s: T): Vec3[T] = new Vec3(x * s, y * s, z * s)
                                                  ^
vec3.scala:5: error: type mismatch;
 found   : T
 required: String
 def len (): T = x + y + z
                     ^
vec3.scala:7: error: value < is not a member of T
 def < (o: Vec3[T]): Boolean = x < o.x && y < o.y && z < o.z
                                 ^
8 errors found
i had added the T <: Number to try to make it so that the + would be
compatible but it still thinks + only works with strings.  i'm not even
completely sure that Number is a real Scala Class.  i'm obviously not
understanding how to write parameterized classes in scala.  any help would
be appreciated.




--
http://mapsdev.blogspot.com/
Marcelo Takeshi Fukushima





Re: [scala] vector math parameterized class in scala

by Steve Lianoglou-6 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> But I wouldn't recommend this approach (yet) if you're looking to do  
> high-performance numerical computing...

And if you do want to do high performance numerical computing, perhaps  
you might consider "pimping" one of the colt, MTJ, or jblas libraries  
to make them more scala-esque(?).

-steve

>
> --j
>
> On Tue, Jul 7, 2009 at 6:57 PM, Kevin Wright <kev.lee.wright@...
> > wrote:
> You've also missed out on case classes and type inference, although  
> this isn't causing your problem...
>
> Using view bounds with the implicit conversions in Numeric should do  
> the trick!
>
> case class Vec3[T <:% Numeric] (x: T, y: T, z: T) {
>   def + (o: Vec3[T]) = new Vec3(x + o.x, y + o.y, z + o.z)
>   def * (s: T): Vec3[T] = new Vec3(x * s, y * s, z * s)
>   def len = x + y + z
>   def == (o: Vec3[T]) = x == o.x && y == o.y && z == o.z
>   def < (o: Vec3[T]) = x < o.x && y < o.y && z < o.z
> }
>
> On Tue, Jul 7, 2009 at 11:59 PM, Jorge Ortiz <jorge.ortiz@...>  
> wrote:
> Take a look at Scala 2.8's Numeric trait:
>
> http://lampsvn.epfl.ch/trac/scala/browser/scala/trunk/src/library/scala/Numeric.scala
>
> --j
>
>
> On Tue, Jul 7, 2009 at 5:56 PM, Jonathan Bachrach <jrb@...>  
> wrote:
> i see.  that's unfortunate.  how would you recommend writing a  
> vector math parameterized class in scala then (short of actually  
> copying the code)?
>
>
> On Jul 7, 2009, at 3:50 PM, Marcelo Fukushima wrote:
>
> i think the problem is that Number does not define '+' and '*' methods
> - and all the compiler knows about your T is that its a Number
> (java.lang.Number)
>
>
> On Tue, Jul 7, 2009 at 7:40 PM, Jonathan Bachrach<jrb@...>  
> wrote:
> i'm trying to define a vector math class using parameterized classes  
> in
> scala and am having trouble.  here's an abbreviated version of what  
> i wrote:
> class Vec3[T <: Number] (xa: T, ya: T, za: T) {
>  val x: T = xa; val y: T = ya; val z: T = za
>  def + (o: Vec3[T]): Vec3[T] = new Vec3(x + o.x, y + o.y, z + o.z)
>  def * (s: T): Vec3[T] = new Vec3(x * s, y * s, z * s)
>  def len (): T = x + y + z
>  def == (o: Vec3[T]): Boolean = x == o.x && y == o.y && z == o.z
>  def < (o: Vec3[T]): Boolean = x < o.x && y < o.y && z < o.z
> }
> and here's what scalac says:
> rala-692> scalac vec3.scala
> vec3.scala:3: error: type mismatch;
>  found   : T
>  required: String
>  def + (o: Vec3[T]): Vec3[T] = new Vec3(x + o.x, y + o.y, z + o.z)
>                                               ^
> vec3.scala:3: error: type mismatch;
>  found   : T
>  required: String
>  def + (o: Vec3[T]): Vec3[T] = new Vec3(x + o.x, y + o.y, z + o.z)
>                                                        ^
> vec3.scala:3: error: type mismatch;
>  found   : T
>  required: String
>  def + (o: Vec3[T]): Vec3[T] = new Vec3(x + o.x, y + o.y, z + o.z)
>                                                                 ^
> vec3.scala:4: error: value * is not a member of T
>  def * (s: T): Vec3[T] = new Vec3(x * s, y * s, z * s)
>                                     ^
> vec3.scala:4: error: value * is not a member of T
>  def * (s: T): Vec3[T] = new Vec3(x * s, y * s, z * s)
>                                            ^
> vec3.scala:4: error: value * is not a member of T
>  def * (s: T): Vec3[T] = new Vec3(x * s, y * s, z * s)
>                                                   ^
> vec3.scala:5: error: type mismatch;
>  found   : T
>  required: String
>  def len (): T = x + y + z
>                      ^
> vec3.scala:7: error: value < is not a member of T
>  def < (o: Vec3[T]): Boolean = x < o.x && y < o.y && z < o.z
>                                  ^
> 8 errors found
> i had added the T <: Number to try to make it so that the + would be
> compatible but it still thinks + only works with strings.  i'm not  
> even
> completely sure that Number is a real Scala Class.  i'm obviously not
> understanding how to write parameterized classes in scala.  any help  
> would
> be appreciated.
>
>
>
>
> --
> http://mapsdev.blogspot.com/
> Marcelo Takeshi Fukushima
>

Re: [scala] vector math parameterized class in scala

by Jorge Ortiz-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Or look at Scalala, which is doing just that (pimping MTJ).


--j

On Tue, Jul 7, 2009 at 8:55 PM, Steve Lianoglou <mailinglist.honeypot@...> wrote:
But I wouldn't recommend this approach (yet) if you're looking to do high-performance numerical computing...

And if you do want to do high performance numerical computing, perhaps you might consider "pimping" one of the colt, MTJ, or jblas libraries to make them more scala-esque(?).

-steve



--j

On Tue, Jul 7, 2009 at 6:57 PM, Kevin Wright <kev.lee.wright@...> wrote:
You've also missed out on case classes and type inference, although this isn't causing your problem...

Using view bounds with the implicit conversions in Numeric should do the trick!

case class Vec3[T <:% Numeric] (x: T, y: T, z: T) {
 def + (o: Vec3[T]) = new Vec3(x + o.x, y + o.y, z + o.z)
 def * (s: T): Vec3[T] = new Vec3(x * s, y * s, z * s)
 def len = x + y + z
 def == (o: Vec3[T]) = x == o.x && y == o.y && z == o.z
 def < (o: Vec3[T]) = x < o.x && y < o.y && z < o.z
}

On Tue, Jul 7, 2009 at 11:59 PM, Jorge Ortiz <jorge.ortiz@...> wrote:
Take a look at Scala 2.8's Numeric trait:

http://lampsvn.epfl.ch/trac/scala/browser/scala/trunk/src/library/scala/Numeric.scala

--j


On Tue, Jul 7, 2009 at 5:56 PM, Jonathan Bachrach <jrb@...> wrote:
i see.  that's unfortunate.  how would you recommend writing a vector math parameterized class in scala then (short of actually copying the code)?


On Jul 7, 2009, at 3:50 PM, Marcelo Fukushima wrote:

i think the problem is that Number does not define '+' and '*' methods
- and all the compiler knows about your T is that its a Number
(java.lang.Number)


On Tue, Jul 7, 2009 at 7:40 PM, Jonathan Bachrach<jrb@...> wrote:
i'm trying to define a vector math class using parameterized classes in
scala and am having trouble.  here's an abbreviated version of what i wrote:
class Vec3[T <: Number] (xa: T, ya: T, za: T) {
 val x: T = xa; val y: T = ya; val z: T = za
 def + (o: Vec3[T]): Vec3[T] = new Vec3(x + o.x, y + o.y, z + o.z)
 def * (s: T): Vec3[T] = new Vec3(x * s, y * s, z * s)
 def len (): T = x + y + z
 def == (o: Vec3[T]): Boolean = x == o.x && y == o.y && z == o.z
 def < (o: Vec3[T]): Boolean = x < o.x && y < o.y && z < o.z
}
and here's what scalac says:
rala-692> scalac vec3.scala
vec3.scala:3: error: type mismatch;
 found   : T
 required: String
 def + (o: Vec3[T]): Vec3[T] = new Vec3(x + o.x, y + o.y, z + o.z)
                                             ^
vec3.scala:3: error: type mismatch;
 found   : T
 required: String
 def + (o: Vec3[T]): Vec3[T] = new Vec3(x + o.x, y + o.y, z + o.z)
                                                      ^
vec3.scala:3: error: type mismatch;
 found   : T
 required: String
 def + (o: Vec3[T]): Vec3[T] = new Vec3(x + o.x, y + o.y, z + o.z)
                                                               ^
vec3.scala:4: error: value * is not a member of T
 def * (s: T): Vec3[T] = new Vec3(x * s, y * s, z * s)
                                   ^
vec3.scala:4: error: value * is not a member of T
 def * (s: T): Vec3[T] = new Vec3(x * s, y * s, z * s)
                                          ^
vec3.scala:4: error: value * is not a member of T
 def * (s: T): Vec3[T] = new Vec3(x * s, y * s, z * s)
                                                 ^
vec3.scala:5: error: type mismatch;
 found   : T
 required: String
 def len (): T = x + y + z
                    ^
vec3.scala:7: error: value < is not a member of T
 def < (o: Vec3[T]): Boolean = x < o.x && y < o.y && z < o.z
                                ^
8 errors found
i had added the T <: Number to try to make it so that the + would be
compatible but it still thinks + only works with strings.  i'm not even
completely sure that Number is a real Scala Class.  i'm obviously not
understanding how to write parameterized classes in scala.  any help would
be appreciated.




--
http://mapsdev.blogspot.com/
Marcelo Takeshi Fukushima



Re: [scala] vector math parameterized class in scala

by Landei :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Jonathan Bachrach-2 wrote:
i'm trying to define a vector math class using parameterized classes  
in scala and am having trouble.  here's an abbreviated version of what  
i wrote:

class Vec3[T <: Number] (xa: T, ya: T, za: T) {
   val x: T = xa; val y: T = ya; val z: T = za
   def + (o: Vec3[T]): Vec3[T] = new Vec3(x + o.x, y + o.y, z + o.z)
   def * (s: T): Vec3[T] = new Vec3(x * s, y * s, z * s)
   def len (): T = x + y + z
   def == (o: Vec3[T]): Boolean = x == o.x && y == o.y && z == o.z
   def < (o: Vec3[T]): Boolean = x < o.x && y < o.y && z < o.z
}

and here's what scalac says:

rala-692> scalac vec3.scala
vec3.scala:3: error: type mismatch;
  found   : T
  required: String
   def + (o: Vec3[T]): Vec3[T] = new Vec3(x + o.x, y + o.y, z + o.z)
                                                ^
vec3.scala:3: error: type mismatch;
  found   : T
  required: String
   def + (o: Vec3[T]): Vec3[T] = new Vec3(x + o.x, y + o.y, z + o.z)
                                                         ^
vec3.scala:3: error: type mismatch;
  found   : T
  required: String
   def + (o: Vec3[T]): Vec3[T] = new Vec3(x + o.x, y + o.y, z + o.z)
                                                                  ^
vec3.scala:4: error: value * is not a member of T
   def * (s: T): Vec3[T] = new Vec3(x * s, y * s, z * s)
                                      ^
vec3.scala:4: error: value * is not a member of T
   def * (s: T): Vec3[T] = new Vec3(x * s, y * s, z * s)
                                             ^
vec3.scala:4: error: value * is not a member of T
   def * (s: T): Vec3[T] = new Vec3(x * s, y * s, z * s)
                                                    ^
vec3.scala:5: error: type mismatch;
  found   : T
  required: String
   def len (): T = x + y + z
                       ^
vec3.scala:7: error: value < is not a member of T
   def < (o: Vec3[T]): Boolean = x < o.x && y < o.y && z < o.z
                                   ^
8 errors found

i had added the T <: Number to try to make it so that the + would be  
compatible but it still thinks + only works with strings.  i'm not  
even completely sure that Number is a real Scala Class.  i'm obviously  
not understanding how to write parameterized classes in scala.  any  
help would be appreciated.
Hi!

There is a blog series about matrices, which uses a cute trick for abstracting over numeric types:

http://dcsobral.blogspot.com/2009/07/matrices-6.html

Cheers,
Daniel

Re: [scala] vector math parameterized class in scala

by Steve Lianoglou-6 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


On Jul 7, 2009, at 11:47 PM, Jorge Ortiz wrote:

> Or look at Scalala, which is doing just that (pimping MTJ).
>
> http://www.scalanlp.org/wiki/index.php?title=Scalala

Wow, I'd totally upmod your comment if I could. That's awesome, thanks  
for the pointer!

-steve

--
Steve Lianoglou
Graduate Student: Physiology, Biophysics and Systems Biology
Weill Medical College of Cornell University

Contact Info: http://cbio.mskcc.org/~lianos/contact




Re: [scala] vector math parameterized class in scala

by Steve Lianoglou-6 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

>> Or look at Scalala, which is doing just that (pimping MTJ).
>>
>> http://www.scalanlp.org/wiki/index.php?title=Scalala

After looking at the code, it doesn't actually look like they're using  
MTJ at all, but they're rather building everything from scratch,  
though it seems they're using netlib/lapack.

Still, it's quite cool. I would think one could get a lot of mileage  
wrapping MTJ, since it has mucho features, and I've heard rumors of it  
being the replacement matrix library for apache commons math  
2.0[1] ... perhaps I'll ping the scalala folks to get their take on it.

-steve

[1] If interested: http://www.nabble.com/commons-math%2C-matrix-toolkits-java-and-consolidation-to23537813.html#a23574199