Construction of short vectors

Hello, cafe I have question about vector package. Currently I'm playing with data types which are isomorphic to vectors of fixed length. Think about vector in N-dimensional space, list of parameters to function R^n → R, set of measurements with uncertainties, etc. They have different semantics but exactly the same representation and it's often advantageous to think about them as N-dimensional vectors. Then constructor like one below arise naturally. And I don't know how to write them properly. It's possible to use fromList but then list could be allocated which is obviously wasteful.
vector2 :: Double -> Double -> Vec2D vector2 x y = ... -- Vec2D is some wrapper around Vector data type
Another question is there any specific problems with short vectors? They could be just 2 elements long. I mean performance problems

On Fri, Jun 25, 2010 at 12:41:48AM +0400, Alexey Khudyakov wrote:
Then constructor like one below arise naturally. And I don't know how to write them properly. It's possible to use fromList but then list could be allocated which is obviously wasteful.
Did you see the generated core? I think you should give a try to the following simple code: import qualified Data.Vector.Generic as VG -- vector == 0.6.* vector2 :: Double -> Double -> Vec2D vector2 x y = Vec2D (VG.fromListN 2 [x,y])
Another question is there any specific problems with short vectors? They could be just 2 elements long. I mean performance problems
Probably there will be more overhead than defining data Vec2D = Vec2D {-# UNPACK #-} !Double {-# UNPACK #-} !Double You should profile to see how much difference there is between those representations. Cheers, -- Felipe.

On Fri, 25 Jun 2010 18:30:06 -0300
Felipe Lessa
On Fri, Jun 25, 2010 at 12:41:48AM +0400, Alexey Khudyakov wrote:
Then constructor like one below arise naturally. And I don't know how to write them properly. It's possible to use fromList but then list could be allocated which is obviously wasteful.
Did you see the generated core? I think you should give a try to the following simple code:
import qualified Data.Vector.Generic as VG -- vector == 0.6.*
vector2 :: Double -> Double -> Vec2D vector2 x y = Vec2D (VG.fromListN 2 [x,y])
I tryed to look at ghc core output but it was totally incomprehensible. Is there any documentation about how to inspect and interpret generated code, good practices etc?
Another question is there any specific problems with short vectors? They could be just 2 elements long. I mean performance problems
Probably there will be more overhead than defining
data Vec2D = Vec2D {-# UNPACK #-} !Double {-# UNPACK #-} !Double
You should profile to see how much difference there is between those representations.
Sure. This price of code generality.

On 25/06/2010, at 06:41, Alexey Khudyakov wrote:
Then constructor like one below arise naturally. And I don't know how to write them properly. It's possible to use fromList but then list could be allocated which is obviously wasteful.
vector2 :: Double -> Double -> Vec2D vector2 x y = ... -- Vec2D is some wrapper around Vector data type
Your best bet is probably singleton x ++ singleton y. Unfortunately, GHC doesn't seem to provide any real way of specifying array literals.
Another question is there any specific problems with short vectors? They could be just 2 elements long. I mean performance problems
A data type like this one should be faster: data Vec2D = Vec2D {-# UNPACK #-} !Double {-# UNPACK #-} !Double Firstly, this needs one less indirection for accessing the components. Secondly, GHC's simplifier knows a lot more about algebraic data types than it does about arrays so the above definition will often lead to better optimisations. Whether or not the difference is significant probably depends on the program. Roman

On Sun, 27 Jun 2010 19:55:21 +1000
Roman Leshchinskiy
On 25/06/2010, at 06:41, Alexey Khudyakov wrote:
Then constructor like one below arise naturally. And I don't know how to write them properly. It's possible to use fromList but then list could be allocated which is obviously wasteful.
vector2 :: Double -> Double -> Vec2D vector2 x y = ... -- Vec2D is some wrapper around Vector data type
Your best bet is probably singleton x ++ singleton y. Unfortunately, GHC doesn't seem to provide any real way of specifying array literals.
Thank you for advice. And what about using ST monad?
vector2 :: Double -> Double -> Vec2D vector2 x y = Vec2D $ create $ do a <- new 2 write a 0 x write a 1 y return a
Another question is there any specific problems with short vectors? They could be just 2 elements long. I mean performance problems
A data type like this one should be faster:
data Vec2D = Vec2D {-# UNPACK #-} !Double {-# UNPACK #-} !Double
Firstly, this needs one less indirection for accessing the components. Secondly, GHC's simplifier knows a lot more about algebraic data types than it does about arrays so the above definition will often lead to better optimisations. Whether or not the difference is significant probably depends on the program.
This is of course faster but what I really want is vectors with length
parametrized by type. This way I can write generic code. Uniform
representation is requirement for that.
--
Alexey Khudyakov

On Jun 27, 2010, at 12:29 PM, Alexey Khudyakov wrote:
This is of course faster but what I really want is vectors with length parametrized by type. This way I can write generic code. Uniform representation is requirement for that.
You're going to need dependent types, or a similar construction, for that. "Do We Need Dependent Types?" http://www.brics.dk/RS/01/10/index.html

On Mon, Jun 28, 2010 at 12:00 AM, Alexander Solla
On Jun 27, 2010, at 12:29 PM, Alexey Khudyakov wrote:
This is of course faster but what I really want is vectors with length parametrized by type. This way I can write generic code. Uniform representation is requirement for that.
You're going to need dependent types, or a similar construction, for that.
"Do We Need Dependent Types?" http://www.brics.dk/RS/01/10/index.html
Dependent types would be nice but there isn't anything usable out there. Newtype wrapper parametrized by type level number works fine so far. If you interested sources are available here: http://bitbucket.org/Shimuuar/nvector http://bitbucket.org/Shimuuar/type-numbers Thank you for the link. It seems that part of paper describes applicative functors.

On Sun, Jun 27, 2010 at 4:44 PM, Alexey Khudyakov
Dependent types would be nice but there isn't anything usable out there. Newtype wrapper parametrized by type level number works fine so far.
If you interested sources are available here: http://bitbucket.org/Shimuuar/nvector http://bitbucket.org/Shimuuar/type-numbers
I haven't looked to see how complete your code is, but feel free to take over the vector-static [1] project if you wish to use some existing code. I haven't taken the time yet to say so on the Hackage page, but it's not currently being maintained. - Jake [1] http://hackage.haskell.org/package/vector-static

On Mon, Jun 28, 2010 at 7:02 PM, Jake McArthur
On Sun, Jun 27, 2010 at 4:44 PM, Alexey Khudyakov
wrote: Dependent types would be nice but there isn't anything usable out there. Newtype wrapper parametrized by type level number works fine so far.
If you interested sources are available here: http://bitbucket.org/Shimuuar/nvector http://bitbucket.org/Shimuuar/type-numbers
I haven't looked to see how complete your code is, but feel free to take over the vector-static [1] project if you wish to use some existing code. I haven't taken the time yet to say so on the Hackage page, but it's not currently being maintained.
I'm aware about this package but I didn't use it because it's completely undocumented. Also my code is a bit different. Vector is parametrized by two phantom type parameters. One is vector length another is type of vector. I want to be able to differentiate between different vectors (euclidean vector and polynomial coefficients have different types) There is also one rather wild idea. Since function for fixed vectors only wraps function from vector in theory it could be possible to generate most of code.
participants (5)
-
Alexander Solla
-
Alexey Khudyakov
-
Felipe Lessa
-
Jake McArthur
-
Roman Leshchinskiy