- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
vector math parameterized class in scala
Tue, 2009-07-07, 23:41
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.scalavec3.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.
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.scalavec3.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.
Wed, 2009-07-08, 00:07
#2
Re: vector math parameterized class in scala
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
> 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.
>>
>
>
>
Wed, 2009-07-08, 00:17
#3
Re: vector math parameterized class in scala
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@pobox.com> wrote:
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@pobox.com> 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@pobox.com> 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.
Wed, 2009-07-08, 01:07
#4
Re: vector math parameterized class in scala
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@gmail.com> wrote:
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@gmail.com> 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@pobox.com> 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@pobox.com> 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.
Wed, 2009-07-08, 01:27
#5
Re: vector math parameterized class in scala
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@googlemail.com> wrote:
--j
On Tue, Jul 7, 2009 at 6:57 PM, Kevin Wright <kev.lee.wright@googlemail.com> 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@gmail.com> 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@pobox.com> 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@pobox.com> 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.
Wed, 2009-07-08, 02:57
#6
Re: vector math parameterized class in scala
> 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 > 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
> wrote:
> Take a look at Scala 2.8's Numeric trait:
>
> http://lampsvn.epfl.ch/trac/scala/browser/scala/trunk/src/library/scala/...
>
> --j
>
>
> On Tue, Jul 7, 2009 at 5:56 PM, Jonathan Bachrach
> 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
> 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.
>
>
>
>
Wed, 2009-07-08, 04:52
#7
Re: vector math parameterized class in scala
Or look at Scalala, which is doing just that (pimping MTJ).
http://www.scalanlp.org/wiki/index.php?title=Scalala
--j
On Tue, Jul 7, 2009 at 8:55 PM, Steve Lianoglou <mailinglist.honeypot@gmail.com> wrote:
http://www.scalanlp.org/wiki/index.php?title=Scalala
--j
On Tue, Jul 7, 2009 at 8:55 PM, Steve Lianoglou <mailinglist.honeypot@gmail.com> 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@googlemail.com> 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@gmail.com> 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@pobox.com> 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@pobox.com> 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.
Wed, 2009-07-08, 08:27
#8
Re: vector math parameterized class in scala
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
Wed, 2009-07-08, 15:47
#9
Re: vector math parameterized class in scala
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
Wed, 2009-07-08, 16:27
#10
Re: vector math parameterized class in scala
>> 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-consolida...
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 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.
>