[Haskell-cafe] Re: [Haskell] ANNOUNCE: An index-aware linear
algebra library in Haskell
Frederik Eaton
frederik at a5.repetae.net
Sun Apr 16 16:56:23 EDT 2006
Hi Alberto,
Those are good questions, I've added some examples which hopefully
clarify the situation. Input and output of vectors is not a strong
point of the library, but I don't think there is a good alternative to
the way I do it.
http://ofb.net/~frederik/futility/src/Vector/read-example.hs
(also, your example exposed some missing functionality. I've added
three new functions; in addition to listMat, now there are listMatCol,
listMatRow, and listMatSquare. Hopefully these should cover almost all
use cases.
http://ofb.net/~frederik/futility/src/Vector/Base.hs
)
By the way, here is how I would download and run the thing, although
you seem to have figured it out:
$ wget http://ofb.net/~frederik/futility/futility-devel.tar.gz
$ tar -xvzf futility-devel.tar.gz
$ cd futility-devel/
$ ghc -fth --make Vector/read-example.hs -o read-example
$ ./read-example
<# 11.0, 23.0; 14.0, 30.0; 15.0, 33.0; 18.0, 40.0 #>
<1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0>
<1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0>
<# 1.0, 5.0; 2.0, 6.0; 3.0, 6.0; 4.0, 7.0 #>
<# 11.0, 23.0; 14.0, 30.0; 15.0, 33.0; 18.0, 40.0 #>
<# -3.0, -5.0; -5.0, -7.0 #>
<# 7.0, 10.0; 15.0, 22.0 #>
As for your questions:
> Vector/examples.hs:35:11:
> GHC stage restriction: `x'
> is used in a top-level splice, and must be imported, not defined locally
> In the first argument of `dAM', namely `x'
> In the definition of `m1': m1 = $[splice](dAM x)
This is a shortcoming of Template Haskell - it will not let you call a
function from a splice if that function is defined in the same file as
the splice. It should be possible to remove this shortcoming, but I
don't know what is planned.
> m2 = listMat x
This is not how listMat is used, see the example file above.
listMat takes a list of lists and a function, and passes the matrix
version of the list of lists to the function.
> I would also like to create a matrix from a data file:
See 'v3' in the example.
Cheers,
Frederik
On Sun, Apr 16, 2006 at 05:06:55PM +0200, Alberto Ruiz wrote:
> It is really counterintuitive! I will study carefully your library and the
> "Implicit Configurations" paper. Using static dimension checking we can write
> very solid code for matrix computations...
>
> However, I don't know how to write some definitions. For instance, this is ok:
>
> m = $(dAM [[1,2,3]])
>
> but with:
>
> x = [[1,2,3]] :: [[Double]]
> m1 = $(dAM x)
> m2 = listMat x
>
> main = do
> print m1
> print m2
>
> I get:
>
> Vector/examples.hs:35:11:
> GHC stage restriction: `x'
> is used in a top-level splice, and must be imported, not defined locally
> In the first argument of `dAM', namely `x'
> In the definition of `m1': m1 = $[splice](dAM x)
>
> Vector/examples.hs:40:10:
> Inferred type is less polymorphic than expected
> Quantified type variable `m' escapes
> Quantified type variable `n' escapes
> Expected type: (v (L m, L n) -> w) -> t
> Inferred type: (forall n1 m1.
> (ReflectNum n1, ReflectNum m1) =>
> v (L m1, L n1) -> w)
> -> w
> In the first argument of `print', namely `m2'
> In the result of a 'do' expression: print m2
>
>
> I would also like to create a matrix from a data file:
>
> main = do
> let m1 = $(dAM [[1,2],[3,4::Double]])
> s <- readFile "data.txt"
> let list = read s :: [[Double]]
> --let m2 = $(dAM list)
> let m2 = listMat list
> print $ m2 *> trans m1
>
> But I get a similar error. Perhaps I must provide information about the
> expected dimensions, but I don't know how to do it.
>
> --
> Alberto
>
> On Saturday 15 April 2006 22:09, Frederik Eaton wrote:
> > Yes, certainly... Otherwise the library would not be much use! If it
> > seems counterintuitive, as it did to me at first, you should check out
> > the "Implicit Configurations" paper, which uses modular arithmetic as
> > an example. My version of their code is in
> >
> > http://ofb.net/~frederik/futility/src/Prepose.hs
> >
> > The function I mainly use is:
> >
> > reifyIntegral :: Integral a => a -> (forall s. ReflectNum s => s -> w) -> w
> >
> > which turns an integral value into a type of the ReflectNum class
> > which represents that value, and calls the provided polymorphic
> > function with a dummy value (actually 'undefined') of that type; then
> > returning the function's result.
> >
> > Frederik
> >
> > On Sat, Apr 15, 2006 at 06:14:44PM +0200, Alberto Ruiz wrote:
> > > On Friday 14 April 2006 17:02, Frederik Eaton wrote:
> > > > An index-aware linear algebra library in Haskell
> > >
> > > Excellent work!
> > >
> > > Is it possible to create a vector or matrix whose size is not known at
> > > compile time?
> > >
>
--
http://ofb.net/~frederik/
More information about the Haskell-Cafe
mailing list